blob: 509e2a80e9a590c64ae807beb1c1b15d1860b63c [file] [log] [blame]
Patrick Delaunayd65291b2019-03-11 11:13:15 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 *
5 * Driver for STMicroelectronics Multi-Function eXpander (STMFX) GPIO expander
6 * based on Linux driver : pinctrl/pinctrl-stmfx.c
7 */
Patrick Delaunay67af77a2021-11-10 18:14:10 +01008
9#define LOG_CATEGORY UCLASS_PINCTRL
10
Patrick Delaunayd65291b2019-03-11 11:13:15 +010011#include <common.h>
12#include <dm.h>
Patrick Delaunay67af77a2021-11-10 18:14:10 +010013#include <log.h>
Patrick Delaunayd65291b2019-03-11 11:13:15 +010014#include <i2c.h>
15#include <asm/gpio.h>
16#include <dm/device.h>
17#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Patrick Delaunayd65291b2019-03-11 11:13:15 +010019#include <dm/lists.h>
20#include <dm/pinctrl.h>
21#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060022#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Patrick Delaunayd65291b2019-03-11 11:13:15 +010024#include <power/regulator.h>
25
26/* STMFX pins = GPIO[15:0] + aGPIO[7:0] */
27#define STMFX_MAX_GPIO 16
28#define STMFX_MAX_AGPIO 8
29
30/* General */
31#define STMFX_REG_CHIP_ID 0x00 /* R */
32#define STMFX_REG_FW_VERSION_MSB 0x01 /* R */
33#define STMFX_REG_FW_VERSION_LSB 0x02 /* R */
34#define STMFX_REG_SYS_CTRL 0x40 /* RW */
35
36/* MFX boot time is around 10ms, so after reset, we have to wait this delay */
37#define STMFX_BOOT_TIME_MS 10
38
39/* GPIOs expander */
40/* GPIO_STATE1 0x10, GPIO_STATE2 0x11, GPIO_STATE3 0x12 */
41#define STMFX_REG_GPIO_STATE 0x10 /* R */
42/* GPIO_DIR1 0x60, GPIO_DIR2 0x61, GPIO_DIR3 0x63 */
43#define STMFX_REG_GPIO_DIR 0x60 /* RW */
44/* GPIO_TYPE1 0x64, GPIO_TYPE2 0x65, GPIO_TYPE3 0x66 */
45#define STMFX_REG_GPIO_TYPE 0x64 /* RW */
46/* GPIO_PUPD1 0x68, GPIO_PUPD2 0x69, GPIO_PUPD3 0x6A */
47#define STMFX_REG_GPIO_PUPD 0x68 /* RW */
48/* GPO_SET1 0x6C, GPO_SET2 0x6D, GPO_SET3 0x6E */
49#define STMFX_REG_GPO_SET 0x6C /* RW */
50/* GPO_CLR1 0x70, GPO_CLR2 0x71, GPO_CLR3 0x72 */
51#define STMFX_REG_GPO_CLR 0x70 /* RW */
52
53/* STMFX_REG_CHIP_ID bitfields */
54#define STMFX_REG_CHIP_ID_MASK GENMASK(7, 0)
55
56/* STMFX_REG_SYS_CTRL bitfields */
57#define STMFX_REG_SYS_CTRL_GPIO_EN BIT(0)
58#define STMFX_REG_SYS_CTRL_ALTGPIO_EN BIT(3)
59#define STMFX_REG_SYS_CTRL_SWRST BIT(7)
60
61#define NR_GPIO_REGS 3
62#define NR_GPIOS_PER_REG 8
63#define get_reg(offset) ((offset) / NR_GPIOS_PER_REG)
64#define get_shift(offset) ((offset) % NR_GPIOS_PER_REG)
65#define get_mask(offset) (BIT(get_shift(offset)))
66
67struct stmfx_pinctrl {
68 struct udevice *gpio;
69};
70
71static int stmfx_read(struct udevice *dev, uint offset)
72{
73 return dm_i2c_reg_read(dev_get_parent(dev), offset);
74}
75
76static int stmfx_write(struct udevice *dev, uint offset, unsigned int val)
77{
78 return dm_i2c_reg_write(dev_get_parent(dev), offset, val);
79}
80
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020081static int stmfx_read_reg(struct udevice *dev, u8 reg_base, uint offset)
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020082{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020083 u8 reg = reg_base + get_reg(offset);
84 u32 mask = get_mask(offset);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020085 int ret;
86
87 ret = stmfx_read(dev, reg);
88 if (ret < 0)
89 return ret;
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020090
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020091 return ret < 0 ? ret : !!(ret & mask);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020092}
93
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020094static int stmfx_write_reg(struct udevice *dev, u8 reg_base, uint offset,
95 uint val)
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020096{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020097 u8 reg = reg_base + get_reg(offset);
98 u32 mask = get_mask(offset);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020099 int ret;
100
101 ret = stmfx_read(dev, reg);
102 if (ret < 0)
103 return ret;
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200104 ret = (ret & ~mask) | (val ? mask : 0);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +0200105
106 return stmfx_write(dev, reg, ret);
107}
108
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200109static int stmfx_conf_set_pupd(struct udevice *dev, unsigned int offset,
110 uint pupd)
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100111{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200112 return stmfx_write_reg(dev, STMFX_REG_GPIO_PUPD, offset, pupd);
113}
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100114
Patrick Delaunay85500262020-06-04 14:30:31 +0200115static int stmfx_conf_get_pupd(struct udevice *dev, unsigned int offset)
116{
117 return stmfx_read_reg(dev, STMFX_REG_GPIO_PUPD, offset);
118}
119
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200120static int stmfx_conf_set_type(struct udevice *dev, unsigned int offset,
121 uint type)
122{
123 return stmfx_write_reg(dev, STMFX_REG_GPIO_TYPE, offset, type);
124}
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100125
Patrick Delaunay85500262020-06-04 14:30:31 +0200126static int stmfx_conf_get_type(struct udevice *dev, unsigned int offset)
127{
128 return stmfx_read_reg(dev, STMFX_REG_GPIO_TYPE, offset);
129}
130
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200131static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
132{
133 return stmfx_read_reg(dev, STMFX_REG_GPIO_STATE, offset);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100134}
135
136static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
137{
138 u32 reg = value ? STMFX_REG_GPO_SET : STMFX_REG_GPO_CLR;
139 u32 mask = get_mask(offset);
140
141 return stmfx_write(dev, reg + get_reg(offset), mask);
142}
143
144static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset)
145{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200146 int ret = stmfx_read_reg(dev, STMFX_REG_GPIO_DIR, offset);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100147
148 if (ret < 0)
149 return ret;
150 /* On stmfx, gpio pins direction is (0)input, (1)output. */
151
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200152 return ret ? GPIOF_OUTPUT : GPIOF_INPUT;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100153}
154
155static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset)
156{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200157 return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 0);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100158}
159
160static int stmfx_gpio_direction_output(struct udevice *dev,
161 unsigned int offset, int value)
162{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200163 int ret = stmfx_gpio_set(dev, offset, value);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100164 if (ret < 0)
165 return ret;
166
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200167 return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100168}
169
Simon Glass54befdd2021-02-04 21:21:55 -0700170static int stmfx_gpio_set_flags(struct udevice *dev, unsigned int offset,
171 ulong flags)
Patrick Delaunaye8635c52020-06-04 14:30:30 +0200172{
173 int ret = -ENOTSUPP;
174
175 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700176 bool value = flags & GPIOD_IS_OUT_ACTIVE;
177
Patrick Delaunaye8635c52020-06-04 14:30:30 +0200178 if (flags & GPIOD_OPEN_SOURCE)
179 return -ENOTSUPP;
180 if (flags & GPIOD_OPEN_DRAIN)
181 ret = stmfx_conf_set_type(dev, offset, 0);
182 else /* PUSH-PULL */
183 ret = stmfx_conf_set_type(dev, offset, 1);
184 if (ret)
185 return ret;
Simon Glass7b893f92021-02-04 21:22:03 -0700186 ret = stmfx_gpio_direction_output(dev, offset, value);
Patrick Delaunaye8635c52020-06-04 14:30:30 +0200187 } else if (flags & GPIOD_IS_IN) {
188 ret = stmfx_gpio_direction_input(dev, offset);
189 if (ret)
190 return ret;
191 if (flags & GPIOD_PULL_UP) {
192 ret = stmfx_conf_set_type(dev, offset, 1);
193 if (ret)
194 return ret;
195 ret = stmfx_conf_set_pupd(dev, offset, 1);
196 } else if (flags & GPIOD_PULL_DOWN) {
197 ret = stmfx_conf_set_type(dev, offset, 1);
198 if (ret)
199 return ret;
200 ret = stmfx_conf_set_pupd(dev, offset, 0);
201 }
202 }
203
204 return ret;
205}
206
Simon Glassd063ce92021-02-04 21:21:56 -0700207static int stmfx_gpio_get_flags(struct udevice *dev, unsigned int offset,
208 ulong *flagsp)
Patrick Delaunay85500262020-06-04 14:30:31 +0200209{
210 ulong dir_flags = 0;
211 int ret;
212
213 if (stmfx_gpio_get_function(dev, offset) == GPIOF_OUTPUT) {
214 dir_flags |= GPIOD_IS_OUT;
215 ret = stmfx_conf_get_type(dev, offset);
216 if (ret < 0)
217 return ret;
218 if (ret == 0)
219 dir_flags |= GPIOD_OPEN_DRAIN;
220 /* 1 = push-pull (default), open source not supported */
221 ret = stmfx_gpio_get(dev, offset);
222 if (ret < 0)
223 return ret;
224 if (ret)
225 dir_flags |= GPIOD_IS_OUT_ACTIVE;
226 } else {
227 dir_flags |= GPIOD_IS_IN;
228 ret = stmfx_conf_get_type(dev, offset);
229 if (ret < 0)
230 return ret;
231 if (ret == 1) {
232 ret = stmfx_conf_get_pupd(dev, offset);
233 if (ret < 0)
234 return ret;
235 if (ret == 1)
236 dir_flags |= GPIOD_PULL_UP;
237 else
238 dir_flags |= GPIOD_PULL_DOWN;
239 }
240 }
Simon Glassd063ce92021-02-04 21:21:56 -0700241 *flagsp = dir_flags;
Patrick Delaunay85500262020-06-04 14:30:31 +0200242
243 return 0;
244}
245
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100246static int stmfx_gpio_probe(struct udevice *dev)
247{
248 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
249 struct ofnode_phandle_args args;
250 u8 sys_ctrl;
251
252 uc_priv->bank_name = "stmfx";
253 uc_priv->gpio_count = STMFX_MAX_GPIO + STMFX_MAX_AGPIO;
254 if (!dev_read_phandle_with_args(dev, "gpio-ranges",
255 NULL, 3, 0, &args)) {
256 uc_priv->gpio_count = args.args[2];
257 }
258
259 /* enable GPIO function */
260 sys_ctrl = STMFX_REG_SYS_CTRL_GPIO_EN;
261 if (uc_priv->gpio_count > STMFX_MAX_GPIO)
262 sys_ctrl |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
263 stmfx_write(dev, STMFX_REG_SYS_CTRL, sys_ctrl);
264
265 return 0;
266}
267
268static const struct dm_gpio_ops stmfx_gpio_ops = {
269 .set_value = stmfx_gpio_set,
270 .get_value = stmfx_gpio_get,
271 .get_function = stmfx_gpio_get_function,
272 .direction_input = stmfx_gpio_direction_input,
273 .direction_output = stmfx_gpio_direction_output,
Simon Glass54befdd2021-02-04 21:21:55 -0700274 .set_flags = stmfx_gpio_set_flags,
Simon Glassd063ce92021-02-04 21:21:56 -0700275 .get_flags = stmfx_gpio_get_flags,
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100276};
277
278U_BOOT_DRIVER(stmfx_gpio) = {
279 .name = "stmfx-gpio",
280 .id = UCLASS_GPIO,
281 .probe = stmfx_gpio_probe,
282 .ops = &stmfx_gpio_ops,
283};
284
285#if CONFIG_IS_ENABLED(PINCONF)
286static const struct pinconf_param stmfx_pinctrl_conf_params[] = {
287 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
288 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 0 },
289 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 0 },
290 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 0 },
291 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
292 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
293 { "output-high", PIN_CONFIG_OUTPUT, 1 },
294 { "output-low", PIN_CONFIG_OUTPUT, 0 },
295};
296
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100297static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin,
298 unsigned int param, unsigned int arg)
299{
300 int ret, dir;
Simon Glassfa20e932020-12-03 16:55:20 -0700301 struct stmfx_pinctrl *plat = dev_get_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100302
303 dir = stmfx_gpio_get_function(plat->gpio, pin);
304
305 if (dir < 0)
306 return dir;
307
308 switch (param) {
309 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
310 case PIN_CONFIG_BIAS_DISABLE:
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200311 case PIN_CONFIG_DRIVE_PUSH_PULL:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200312 ret = stmfx_conf_set_type(dev, pin, 0);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200313 break;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100314 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200315 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200316 if (ret)
317 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200318 ret = stmfx_conf_set_pupd(dev, pin, 0);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100319 break;
320 case PIN_CONFIG_BIAS_PULL_UP:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200321 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200322 if (ret)
323 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200324 ret = stmfx_conf_set_pupd(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100325 break;
326 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200327 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100328 break;
329 case PIN_CONFIG_OUTPUT:
330 ret = stmfx_gpio_direction_output(plat->gpio, pin, arg);
331 break;
332 default:
333 return -ENOTSUPP;
334 }
335
336 return ret;
337}
338#endif
339
340static int stmfx_pinctrl_get_pins_count(struct udevice *dev)
341{
Simon Glassfa20e932020-12-03 16:55:20 -0700342 struct stmfx_pinctrl *plat = dev_get_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100343 struct gpio_dev_priv *uc_priv;
344
345 uc_priv = dev_get_uclass_priv(plat->gpio);
346
347 return uc_priv->gpio_count;
348}
349
350/*
Patrice Chotardc0550bd2021-01-20 13:43:39 +0100351 * STMFX pins[15:0] are called "gpio[15:0]"
352 * and STMFX pins[23:16] are called "agpio[7:0]"
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100353 */
Patrice Chotard63360d7c2021-01-20 13:43:40 +0100354static char pin_name[PINNAME_SIZE];
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100355static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev,
356 unsigned int selector)
357{
358 if (selector < STMFX_MAX_GPIO)
Patrice Chotard63360d7c2021-01-20 13:43:40 +0100359 snprintf(pin_name, PINNAME_SIZE, "gpio%u", selector);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100360 else
Patrice Chotard63360d7c2021-01-20 13:43:40 +0100361 snprintf(pin_name, PINNAME_SIZE, "agpio%u", selector - 16);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100362 return pin_name;
363}
364
Patrick Delaunayffe2f302020-06-04 14:30:32 +0200365static const char *stmfx_pinctrl_get_pin_conf(struct udevice *dev,
366 unsigned int pin, int func)
367{
368 int pupd, type;
369
370 type = stmfx_conf_get_type(dev, pin);
371 if (type < 0)
372 return "";
373
374 if (func == GPIOF_OUTPUT) {
375 if (type)
376 return "drive-open-drain";
377 else
378 return ""; /* default: push-pull*/
379 }
380 if (!type)
381 return ""; /* default: bias-disable*/
382
383 pupd = stmfx_conf_get_pupd(dev, pin);
384 if (pupd < 0)
385 return "";
386
387 if (pupd)
388 return "bias-pull-up";
389 else
390 return "bias-pull-down";
391}
392
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100393static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev,
394 unsigned int selector,
395 char *buf, int size)
396{
Simon Glassfa20e932020-12-03 16:55:20 -0700397 struct stmfx_pinctrl *plat = dev_get_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100398 int func;
399
400 func = stmfx_gpio_get_function(plat->gpio, selector);
401 if (func < 0)
402 return func;
403
Patrick Delaunayffe2f302020-06-04 14:30:32 +0200404 snprintf(buf, size, "%s ", func == GPIOF_INPUT ? "input" : "output");
405
406 strncat(buf, stmfx_pinctrl_get_pin_conf(dev, selector, func), size);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100407
408 return 0;
409}
410
411static int stmfx_pinctrl_bind(struct udevice *dev)
412{
Simon Glassfa20e932020-12-03 16:55:20 -0700413 struct stmfx_pinctrl *plat = dev_get_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100414
Patrick Delaunay23a46bf2020-10-28 10:51:56 +0100415 /* subnode name is not explicit: use father name */
416 device_set_name(dev, dev->parent->name);
417
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100418 return device_bind_driver_to_node(dev->parent,
Patrick Delaunay23a46bf2020-10-28 10:51:56 +0100419 "stmfx-gpio", dev->parent->name,
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100420 dev_ofnode(dev), &plat->gpio);
421};
422
423static int stmfx_pinctrl_probe(struct udevice *dev)
424{
Simon Glassfa20e932020-12-03 16:55:20 -0700425 struct stmfx_pinctrl *plat = dev_get_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100426
427 return device_probe(plat->gpio);
428};
429
430const struct pinctrl_ops stmfx_pinctrl_ops = {
431 .get_pins_count = stmfx_pinctrl_get_pins_count,
432 .get_pin_name = stmfx_pinctrl_get_pin_name,
433 .set_state = pinctrl_generic_set_state,
434 .get_pin_muxing = stmfx_pinctrl_get_pin_muxing,
435#if CONFIG_IS_ENABLED(PINCONF)
436 .pinconf_set = stmfx_pinctrl_conf_set,
437 .pinconf_num_params = ARRAY_SIZE(stmfx_pinctrl_conf_params),
438 .pinconf_params = stmfx_pinctrl_conf_params,
439#endif
440};
441
442static const struct udevice_id stmfx_pinctrl_match[] = {
443 { .compatible = "st,stmfx-0300-pinctrl", },
444};
445
446U_BOOT_DRIVER(stmfx_pinctrl) = {
447 .name = "stmfx-pinctrl",
448 .id = UCLASS_PINCTRL,
449 .of_match = of_match_ptr(stmfx_pinctrl_match),
450 .bind = stmfx_pinctrl_bind,
451 .probe = stmfx_pinctrl_probe,
452 .ops = &stmfx_pinctrl_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700453 .plat_auto = sizeof(struct stmfx_pinctrl),
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100454};
455
456static int stmfx_chip_init(struct udevice *dev)
457{
458 u8 id;
459 u8 version[2];
460 int ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700461 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100462
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100463 ret = dm_i2c_reg_read(dev, STMFX_REG_CHIP_ID);
464 if (ret < 0) {
465 dev_err(dev, "error reading chip id: %d\n", ret);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100466 return ret;
467 }
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100468 id = (u8)ret;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100469 /*
470 * Check that ID is the complement of the I2C address:
471 * STMFX I2C address follows the 7-bit format (MSB), that's why
472 * client->addr is shifted.
473 *
474 * STMFX_I2C_ADDR| STMFX | Linux
475 * input pin | I2C device address | I2C device address
476 *---------------------------------------------------------
477 * 0 | b: 1000 010x h:0x84 | 0x42
478 * 1 | b: 1000 011x h:0x86 | 0x43
479 */
480 if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (chip->chip_addr << 1)) {
481 dev_err(dev, "unknown chip id: %#x\n", id);
482 return -EINVAL;
483 }
484
485 ret = dm_i2c_read(dev, STMFX_REG_FW_VERSION_MSB,
486 version, sizeof(version));
487 if (ret) {
488 dev_err(dev, "error reading fw version: %d\n", ret);
489 return ret;
490 }
491
492 dev_info(dev, "STMFX id: %#x, fw version: %x.%02x\n",
493 id, version[0], version[1]);
494
495 ret = dm_i2c_reg_read(dev, STMFX_REG_SYS_CTRL);
496
497 if (ret < 0)
498 return ret;
499
500 ret = dm_i2c_reg_write(dev, STMFX_REG_SYS_CTRL,
501 ret | STMFX_REG_SYS_CTRL_SWRST);
502 if (ret)
503 return ret;
504
505 mdelay(STMFX_BOOT_TIME_MS);
506
507 return ret;
508}
509
510static int stmfx_probe(struct udevice *dev)
511{
512 struct udevice *vdd;
513 int ret;
514
515 ret = device_get_supply_regulator(dev, "vdd-supply", &vdd);
516 if (ret && ret != -ENOENT) {
517 dev_err(dev, "vdd regulator error:%d\n", ret);
518 return ret;
519 }
520 if (!ret) {
521 ret = regulator_set_enable(vdd, true);
522 if (ret) {
523 dev_err(dev, "vdd enable failed: %d\n", ret);
524 return ret;
525 }
526 }
527
528 return stmfx_chip_init(dev);
529}
530
531static const struct udevice_id stmfx_match[] = {
532 { .compatible = "st,stmfx-0300", },
533};
534
535U_BOOT_DRIVER(stmfx) = {
536 .name = "stmfx",
537 .id = UCLASS_I2C_GENERIC,
538 .of_match = of_match_ptr(stmfx_match),
539 .probe = stmfx_probe,
540 .bind = dm_scan_fdt_dev,
541};