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