common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / pinctrl / pinctrl-stmfx.c
1 // 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  */
8 #include <common.h>
9 #include <dm.h>
10 #include <i2c.h>
11 #include <asm/gpio.h>
12 #include <dm/device.h>
13 #include <dm/device-internal.h>
14 #include <dm/device_compat.h>
15 #include <dm/lists.h>
16 #include <dm/pinctrl.h>
17 #include <linux/bitfield.h>
18 #include <linux/delay.h>
19 #include <power/regulator.h>
20
21 /* STMFX pins = GPIO[15:0] + aGPIO[7:0] */
22 #define STMFX_MAX_GPIO                  16
23 #define STMFX_MAX_AGPIO                 8
24
25 /* General */
26 #define STMFX_REG_CHIP_ID               0x00 /* R */
27 #define STMFX_REG_FW_VERSION_MSB        0x01 /* R */
28 #define STMFX_REG_FW_VERSION_LSB        0x02 /* R */
29 #define STMFX_REG_SYS_CTRL              0x40 /* RW */
30
31 /* MFX boot time is around 10ms, so after reset, we have to wait this delay */
32 #define STMFX_BOOT_TIME_MS 10
33
34 /* GPIOs expander */
35 /* GPIO_STATE1 0x10, GPIO_STATE2 0x11, GPIO_STATE3 0x12 */
36 #define STMFX_REG_GPIO_STATE            0x10 /* R */
37 /* GPIO_DIR1 0x60, GPIO_DIR2 0x61, GPIO_DIR3 0x63 */
38 #define STMFX_REG_GPIO_DIR              0x60 /* RW */
39 /* GPIO_TYPE1 0x64, GPIO_TYPE2 0x65, GPIO_TYPE3 0x66 */
40 #define STMFX_REG_GPIO_TYPE             0x64 /* RW */
41 /* GPIO_PUPD1 0x68, GPIO_PUPD2 0x69, GPIO_PUPD3 0x6A */
42 #define STMFX_REG_GPIO_PUPD             0x68 /* RW */
43 /* GPO_SET1 0x6C, GPO_SET2 0x6D, GPO_SET3 0x6E */
44 #define STMFX_REG_GPO_SET               0x6C /* RW */
45 /* GPO_CLR1 0x70, GPO_CLR2 0x71, GPO_CLR3 0x72 */
46 #define STMFX_REG_GPO_CLR               0x70 /* RW */
47
48 /* STMFX_REG_CHIP_ID bitfields */
49 #define STMFX_REG_CHIP_ID_MASK          GENMASK(7, 0)
50
51 /* STMFX_REG_SYS_CTRL bitfields */
52 #define STMFX_REG_SYS_CTRL_GPIO_EN      BIT(0)
53 #define STMFX_REG_SYS_CTRL_ALTGPIO_EN   BIT(3)
54 #define STMFX_REG_SYS_CTRL_SWRST        BIT(7)
55
56 #define NR_GPIO_REGS                    3
57 #define NR_GPIOS_PER_REG                8
58 #define get_reg(offset)                 ((offset) / NR_GPIOS_PER_REG)
59 #define get_shift(offset)               ((offset) % NR_GPIOS_PER_REG)
60 #define get_mask(offset)                (BIT(get_shift(offset)))
61
62 struct stmfx_pinctrl {
63         struct udevice *gpio;
64 };
65
66 static int stmfx_read(struct udevice *dev, uint offset)
67 {
68         return  dm_i2c_reg_read(dev_get_parent(dev), offset);
69 }
70
71 static int stmfx_write(struct udevice *dev, uint offset, unsigned int val)
72 {
73         return dm_i2c_reg_write(dev_get_parent(dev), offset, val);
74 }
75
76 static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
77 {
78         u32 reg = STMFX_REG_GPIO_STATE + get_reg(offset);
79         u32 mask = get_mask(offset);
80         int ret;
81
82         ret = stmfx_read(dev, reg);
83
84         return ret < 0 ? ret : !!(ret & mask);
85 }
86
87 static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
88 {
89         u32 reg = value ? STMFX_REG_GPO_SET : STMFX_REG_GPO_CLR;
90         u32 mask = get_mask(offset);
91
92         return stmfx_write(dev, reg + get_reg(offset), mask);
93 }
94
95 static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset)
96 {
97         u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
98         u32 mask = get_mask(offset);
99         int ret;
100
101         ret = stmfx_read(dev, reg);
102
103         if (ret < 0)
104                 return ret;
105         /* On stmfx, gpio pins direction is (0)input, (1)output. */
106
107         return ret & mask ? GPIOF_OUTPUT : GPIOF_INPUT;
108 }
109
110 static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset)
111 {
112         u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
113         u32 mask = get_mask(offset);
114         int ret;
115
116         ret = stmfx_read(dev, reg);
117         if (ret < 0)
118                 return ret;
119
120         ret &= ~mask;
121
122         return stmfx_write(dev, reg, ret & ~mask);
123 }
124
125 static int stmfx_gpio_direction_output(struct udevice *dev,
126                                        unsigned int offset, int value)
127 {
128         u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
129         u32 mask = get_mask(offset);
130         int ret;
131
132         ret = stmfx_gpio_set(dev, offset, value);
133         if (ret < 0)
134                 return ret;
135
136         ret = stmfx_read(dev, reg);
137         if (ret < 0)
138                 return ret;
139
140         return stmfx_write(dev, reg, ret | mask);
141 }
142
143 static int stmfx_gpio_probe(struct udevice *dev)
144 {
145         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
146         struct ofnode_phandle_args args;
147         u8 sys_ctrl;
148
149         uc_priv->bank_name = "stmfx";
150         uc_priv->gpio_count = STMFX_MAX_GPIO + STMFX_MAX_AGPIO;
151         if (!dev_read_phandle_with_args(dev, "gpio-ranges",
152                                         NULL, 3, 0, &args)) {
153                 uc_priv->gpio_count = args.args[2];
154         }
155
156         /* enable GPIO function */
157         sys_ctrl = STMFX_REG_SYS_CTRL_GPIO_EN;
158         if (uc_priv->gpio_count > STMFX_MAX_GPIO)
159                 sys_ctrl |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
160         stmfx_write(dev, STMFX_REG_SYS_CTRL, sys_ctrl);
161
162         return 0;
163 }
164
165 static const struct dm_gpio_ops stmfx_gpio_ops = {
166         .set_value = stmfx_gpio_set,
167         .get_value = stmfx_gpio_get,
168         .get_function = stmfx_gpio_get_function,
169         .direction_input = stmfx_gpio_direction_input,
170         .direction_output = stmfx_gpio_direction_output,
171 };
172
173 U_BOOT_DRIVER(stmfx_gpio) = {
174         .name   = "stmfx-gpio",
175         .id     = UCLASS_GPIO,
176         .probe  = stmfx_gpio_probe,
177         .ops    = &stmfx_gpio_ops,
178 };
179
180 #if CONFIG_IS_ENABLED(PINCONF)
181 static const struct pinconf_param stmfx_pinctrl_conf_params[] = {
182         { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
183         { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 0 },
184         { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 0 },
185         { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 0 },
186         { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
187         { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
188         { "output-high", PIN_CONFIG_OUTPUT, 1 },
189         { "output-low", PIN_CONFIG_OUTPUT, 0 },
190 };
191
192 static int stmfx_pinctrl_set_pupd(struct udevice *dev,
193                                   unsigned int pin, u32 pupd)
194 {
195         u8 reg = STMFX_REG_GPIO_PUPD + get_reg(pin);
196         u32 mask = get_mask(pin);
197         int ret;
198
199         ret = stmfx_read(dev, reg);
200         if (ret < 0)
201                 return ret;
202         ret = (ret & ~mask) | (pupd ? mask : 0);
203
204         return stmfx_write(dev, reg, ret);
205 }
206
207 static int stmfx_pinctrl_set_type(struct udevice *dev,
208                                   unsigned int pin, u32 type)
209 {
210         u8 reg = STMFX_REG_GPIO_TYPE + get_reg(pin);
211         u32 mask = get_mask(pin);
212         int ret;
213
214         ret = stmfx_read(dev, reg);
215         if (ret < 0)
216                 return ret;
217         ret = (ret & ~mask) | (type ? mask : 0);
218
219         return stmfx_write(dev, reg, ret);
220 }
221
222 static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin,
223                                   unsigned int param, unsigned int arg)
224 {
225         int ret, dir;
226         struct stmfx_pinctrl *plat = dev_get_platdata(dev);
227
228         dir = stmfx_gpio_get_function(plat->gpio, pin);
229
230         if (dir < 0)
231                 return dir;
232
233         switch (param) {
234         case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
235         case PIN_CONFIG_BIAS_DISABLE:
236         case PIN_CONFIG_DRIVE_PUSH_PULL:
237                 ret = stmfx_pinctrl_set_type(dev, pin, 0);
238                 break;
239         case PIN_CONFIG_BIAS_PULL_DOWN:
240                 ret = stmfx_pinctrl_set_type(dev, pin, 1);
241                 if (ret)
242                         return ret;
243                 ret = stmfx_pinctrl_set_pupd(dev, pin, 0);
244                 break;
245         case PIN_CONFIG_BIAS_PULL_UP:
246                 ret = stmfx_pinctrl_set_type(dev, pin, 1);
247                 if (ret)
248                         return ret;
249                 ret = stmfx_pinctrl_set_pupd(dev, pin, 1);
250                 break;
251         case PIN_CONFIG_DRIVE_OPEN_DRAIN:
252                 ret = stmfx_pinctrl_set_type(dev, pin, 1);
253                 break;
254         case PIN_CONFIG_OUTPUT:
255                 ret = stmfx_gpio_direction_output(plat->gpio, pin, arg);
256                 break;
257         default:
258                 return -ENOTSUPP;
259         }
260
261         return ret;
262 }
263 #endif
264
265 static int stmfx_pinctrl_get_pins_count(struct udevice *dev)
266 {
267         struct stmfx_pinctrl *plat = dev_get_platdata(dev);
268         struct gpio_dev_priv *uc_priv;
269
270         uc_priv = dev_get_uclass_priv(plat->gpio);
271
272         return uc_priv->gpio_count;
273 }
274
275 /*
276  * STMFX pins[15:0] are called "gpio[15:0]"
277  * and STMFX pins[23:16] are called "agpio[7:0]"
278  */
279 #define MAX_PIN_NAME_LEN 7
280 static char pin_name[MAX_PIN_NAME_LEN];
281 static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev,
282                                               unsigned int selector)
283 {
284         if (selector < STMFX_MAX_GPIO)
285                 snprintf(pin_name, MAX_PIN_NAME_LEN, "gpio%u", selector);
286         else
287                 snprintf(pin_name, MAX_PIN_NAME_LEN, "agpio%u", selector - 16);
288         return pin_name;
289 }
290
291 static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev,
292                                         unsigned int selector,
293                                         char *buf, int size)
294 {
295         struct stmfx_pinctrl *plat = dev_get_platdata(dev);
296         int func;
297
298         func = stmfx_gpio_get_function(plat->gpio, selector);
299         if (func < 0)
300                 return func;
301
302         snprintf(buf, size, "%s", func == GPIOF_INPUT ? "input" : "output");
303
304         return 0;
305 }
306
307 static int stmfx_pinctrl_bind(struct udevice *dev)
308 {
309         struct stmfx_pinctrl *plat = dev_get_platdata(dev);
310
311         return device_bind_driver_to_node(dev->parent,
312                                           "stmfx-gpio", "stmfx-gpio",
313                                           dev_ofnode(dev), &plat->gpio);
314 };
315
316 static int stmfx_pinctrl_probe(struct udevice *dev)
317 {
318         struct stmfx_pinctrl *plat = dev_get_platdata(dev);
319
320         return device_probe(plat->gpio);
321 };
322
323 const struct pinctrl_ops stmfx_pinctrl_ops = {
324         .get_pins_count = stmfx_pinctrl_get_pins_count,
325         .get_pin_name = stmfx_pinctrl_get_pin_name,
326         .set_state = pinctrl_generic_set_state,
327         .get_pin_muxing = stmfx_pinctrl_get_pin_muxing,
328 #if CONFIG_IS_ENABLED(PINCONF)
329         .pinconf_set = stmfx_pinctrl_conf_set,
330         .pinconf_num_params = ARRAY_SIZE(stmfx_pinctrl_conf_params),
331         .pinconf_params = stmfx_pinctrl_conf_params,
332 #endif
333 };
334
335 static const struct udevice_id stmfx_pinctrl_match[] = {
336         { .compatible = "st,stmfx-0300-pinctrl", },
337 };
338
339 U_BOOT_DRIVER(stmfx_pinctrl) = {
340         .name = "stmfx-pinctrl",
341         .id = UCLASS_PINCTRL,
342         .of_match = of_match_ptr(stmfx_pinctrl_match),
343         .bind = stmfx_pinctrl_bind,
344         .probe = stmfx_pinctrl_probe,
345         .ops = &stmfx_pinctrl_ops,
346         .platdata_auto_alloc_size = sizeof(struct stmfx_pinctrl),
347 };
348
349 static int stmfx_chip_init(struct udevice *dev)
350 {
351         u8 id;
352         u8 version[2];
353         int ret;
354         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
355
356         ret = dm_i2c_reg_read(dev, STMFX_REG_CHIP_ID);
357         if (ret < 0) {
358                 dev_err(dev, "error reading chip id: %d\n", ret);
359                 return ret;
360         }
361         id = (u8)ret;
362         /*
363          * Check that ID is the complement of the I2C address:
364          * STMFX I2C address follows the 7-bit format (MSB), that's why
365          * client->addr is shifted.
366          *
367          * STMFX_I2C_ADDR|       STMFX         |        Linux
368          *   input pin   | I2C device address  | I2C device address
369          *---------------------------------------------------------
370          *       0       | b: 1000 010x h:0x84 |       0x42
371          *       1       | b: 1000 011x h:0x86 |       0x43
372          */
373         if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (chip->chip_addr << 1)) {
374                 dev_err(dev, "unknown chip id: %#x\n", id);
375                 return -EINVAL;
376         }
377
378         ret = dm_i2c_read(dev, STMFX_REG_FW_VERSION_MSB,
379                           version, sizeof(version));
380         if (ret) {
381                 dev_err(dev, "error reading fw version: %d\n", ret);
382                 return ret;
383         }
384
385         dev_info(dev, "STMFX id: %#x, fw version: %x.%02x\n",
386                  id, version[0], version[1]);
387
388         ret = dm_i2c_reg_read(dev, STMFX_REG_SYS_CTRL);
389
390         if (ret < 0)
391                 return ret;
392
393         ret = dm_i2c_reg_write(dev, STMFX_REG_SYS_CTRL,
394                                ret | STMFX_REG_SYS_CTRL_SWRST);
395         if (ret)
396                 return ret;
397
398         mdelay(STMFX_BOOT_TIME_MS);
399
400         return ret;
401 }
402
403 static int stmfx_probe(struct udevice *dev)
404 {
405         struct udevice *vdd;
406         int ret;
407
408         ret = device_get_supply_regulator(dev, "vdd-supply", &vdd);
409         if (ret && ret != -ENOENT) {
410                 dev_err(dev, "vdd regulator error:%d\n", ret);
411                 return ret;
412         }
413         if (!ret) {
414                 ret = regulator_set_enable(vdd, true);
415                 if (ret) {
416                         dev_err(dev, "vdd enable failed: %d\n", ret);
417                         return ret;
418                 }
419         }
420
421         return stmfx_chip_init(dev);
422 }
423
424 static const struct udevice_id stmfx_match[] = {
425         { .compatible = "st,stmfx-0300", },
426 };
427
428 U_BOOT_DRIVER(stmfx) = {
429         .name = "stmfx",
430         .id = UCLASS_I2C_GENERIC,
431         .of_match = of_match_ptr(stmfx_match),
432         .probe = stmfx_probe,
433         .bind = dm_scan_fdt_dev,
434 };