1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
5 * Driver for STMicroelectronics Multi-Function eXpander (STMFX) GPIO expander
6 * based on Linux driver : pinctrl/pinctrl-stmfx.c
12 #include <dm/device.h>
13 #include <dm/device-internal.h>
14 #include <dm/device_compat.h>
16 #include <dm/pinctrl.h>
17 #include <linux/bitfield.h>
18 #include <power/regulator.h>
20 /* STMFX pins = GPIO[15:0] + aGPIO[7:0] */
21 #define STMFX_MAX_GPIO 16
22 #define STMFX_MAX_AGPIO 8
25 #define STMFX_REG_CHIP_ID 0x00 /* R */
26 #define STMFX_REG_FW_VERSION_MSB 0x01 /* R */
27 #define STMFX_REG_FW_VERSION_LSB 0x02 /* R */
28 #define STMFX_REG_SYS_CTRL 0x40 /* RW */
30 /* MFX boot time is around 10ms, so after reset, we have to wait this delay */
31 #define STMFX_BOOT_TIME_MS 10
34 /* GPIO_STATE1 0x10, GPIO_STATE2 0x11, GPIO_STATE3 0x12 */
35 #define STMFX_REG_GPIO_STATE 0x10 /* R */
36 /* GPIO_DIR1 0x60, GPIO_DIR2 0x61, GPIO_DIR3 0x63 */
37 #define STMFX_REG_GPIO_DIR 0x60 /* RW */
38 /* GPIO_TYPE1 0x64, GPIO_TYPE2 0x65, GPIO_TYPE3 0x66 */
39 #define STMFX_REG_GPIO_TYPE 0x64 /* RW */
40 /* GPIO_PUPD1 0x68, GPIO_PUPD2 0x69, GPIO_PUPD3 0x6A */
41 #define STMFX_REG_GPIO_PUPD 0x68 /* RW */
42 /* GPO_SET1 0x6C, GPO_SET2 0x6D, GPO_SET3 0x6E */
43 #define STMFX_REG_GPO_SET 0x6C /* RW */
44 /* GPO_CLR1 0x70, GPO_CLR2 0x71, GPO_CLR3 0x72 */
45 #define STMFX_REG_GPO_CLR 0x70 /* RW */
47 /* STMFX_REG_CHIP_ID bitfields */
48 #define STMFX_REG_CHIP_ID_MASK GENMASK(7, 0)
50 /* STMFX_REG_SYS_CTRL bitfields */
51 #define STMFX_REG_SYS_CTRL_GPIO_EN BIT(0)
52 #define STMFX_REG_SYS_CTRL_ALTGPIO_EN BIT(3)
53 #define STMFX_REG_SYS_CTRL_SWRST BIT(7)
55 #define NR_GPIO_REGS 3
56 #define NR_GPIOS_PER_REG 8
57 #define get_reg(offset) ((offset) / NR_GPIOS_PER_REG)
58 #define get_shift(offset) ((offset) % NR_GPIOS_PER_REG)
59 #define get_mask(offset) (BIT(get_shift(offset)))
61 struct stmfx_pinctrl {
65 static int stmfx_read(struct udevice *dev, uint offset)
67 return dm_i2c_reg_read(dev_get_parent(dev), offset);
70 static int stmfx_write(struct udevice *dev, uint offset, unsigned int val)
72 return dm_i2c_reg_write(dev_get_parent(dev), offset, val);
75 static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
77 u32 reg = STMFX_REG_GPIO_STATE + get_reg(offset);
78 u32 mask = get_mask(offset);
81 ret = stmfx_read(dev, reg);
83 return ret < 0 ? ret : !!(ret & mask);
86 static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
88 u32 reg = value ? STMFX_REG_GPO_SET : STMFX_REG_GPO_CLR;
89 u32 mask = get_mask(offset);
91 return stmfx_write(dev, reg + get_reg(offset), mask);
94 static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset)
96 u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
97 u32 mask = get_mask(offset);
100 ret = stmfx_read(dev, reg);
104 /* On stmfx, gpio pins direction is (0)input, (1)output. */
106 return ret & mask ? GPIOF_OUTPUT : GPIOF_INPUT;
109 static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset)
111 u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
112 u32 mask = get_mask(offset);
115 ret = stmfx_read(dev, reg);
121 return stmfx_write(dev, reg, ret & ~mask);
124 static int stmfx_gpio_direction_output(struct udevice *dev,
125 unsigned int offset, int value)
127 u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
128 u32 mask = get_mask(offset);
131 ret = stmfx_gpio_set(dev, offset, value);
135 ret = stmfx_read(dev, reg);
139 return stmfx_write(dev, reg, ret | mask);
142 static int stmfx_gpio_probe(struct udevice *dev)
144 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
145 struct ofnode_phandle_args args;
148 uc_priv->bank_name = "stmfx";
149 uc_priv->gpio_count = STMFX_MAX_GPIO + STMFX_MAX_AGPIO;
150 if (!dev_read_phandle_with_args(dev, "gpio-ranges",
151 NULL, 3, 0, &args)) {
152 uc_priv->gpio_count = args.args[2];
155 /* enable GPIO function */
156 sys_ctrl = STMFX_REG_SYS_CTRL_GPIO_EN;
157 if (uc_priv->gpio_count > STMFX_MAX_GPIO)
158 sys_ctrl |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
159 stmfx_write(dev, STMFX_REG_SYS_CTRL, sys_ctrl);
164 static const struct dm_gpio_ops stmfx_gpio_ops = {
165 .set_value = stmfx_gpio_set,
166 .get_value = stmfx_gpio_get,
167 .get_function = stmfx_gpio_get_function,
168 .direction_input = stmfx_gpio_direction_input,
169 .direction_output = stmfx_gpio_direction_output,
172 U_BOOT_DRIVER(stmfx_gpio) = {
173 .name = "stmfx-gpio",
175 .probe = stmfx_gpio_probe,
176 .ops = &stmfx_gpio_ops,
179 #if CONFIG_IS_ENABLED(PINCONF)
180 static const struct pinconf_param stmfx_pinctrl_conf_params[] = {
181 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
182 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 0 },
183 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 0 },
184 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 0 },
185 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
186 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
187 { "output-high", PIN_CONFIG_OUTPUT, 1 },
188 { "output-low", PIN_CONFIG_OUTPUT, 0 },
191 static int stmfx_pinctrl_set_pupd(struct udevice *dev,
192 unsigned int pin, u32 pupd)
194 u8 reg = STMFX_REG_GPIO_PUPD + get_reg(pin);
195 u32 mask = get_mask(pin);
198 ret = stmfx_read(dev, reg);
201 ret = (ret & ~mask) | (pupd ? mask : 0);
203 return stmfx_write(dev, reg, ret);
206 static int stmfx_pinctrl_set_type(struct udevice *dev,
207 unsigned int pin, u32 type)
209 u8 reg = STMFX_REG_GPIO_TYPE + get_reg(pin);
210 u32 mask = get_mask(pin);
213 ret = stmfx_read(dev, reg);
216 ret = (ret & ~mask) | (type ? mask : 0);
218 return stmfx_write(dev, reg, ret);
221 static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin,
222 unsigned int param, unsigned int arg)
225 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
227 dir = stmfx_gpio_get_function(plat->gpio, pin);
233 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
234 case PIN_CONFIG_BIAS_DISABLE:
235 case PIN_CONFIG_DRIVE_PUSH_PULL:
236 ret = stmfx_pinctrl_set_type(dev, pin, 0);
238 case PIN_CONFIG_BIAS_PULL_DOWN:
239 ret = stmfx_pinctrl_set_type(dev, pin, 1);
242 ret = stmfx_pinctrl_set_pupd(dev, pin, 0);
244 case PIN_CONFIG_BIAS_PULL_UP:
245 ret = stmfx_pinctrl_set_type(dev, pin, 1);
248 ret = stmfx_pinctrl_set_pupd(dev, pin, 1);
250 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
251 ret = stmfx_pinctrl_set_type(dev, pin, 1);
253 case PIN_CONFIG_OUTPUT:
254 ret = stmfx_gpio_direction_output(plat->gpio, pin, arg);
264 static int stmfx_pinctrl_get_pins_count(struct udevice *dev)
266 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
267 struct gpio_dev_priv *uc_priv;
269 uc_priv = dev_get_uclass_priv(plat->gpio);
271 return uc_priv->gpio_count;
275 * STMFX pins[15:0] are called "gpio[15:0]"
276 * and STMFX pins[23:16] are called "agpio[7:0]"
278 #define MAX_PIN_NAME_LEN 7
279 static char pin_name[MAX_PIN_NAME_LEN];
280 static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev,
281 unsigned int selector)
283 if (selector < STMFX_MAX_GPIO)
284 snprintf(pin_name, MAX_PIN_NAME_LEN, "gpio%u", selector);
286 snprintf(pin_name, MAX_PIN_NAME_LEN, "agpio%u", selector - 16);
290 static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev,
291 unsigned int selector,
294 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
297 func = stmfx_gpio_get_function(plat->gpio, selector);
301 snprintf(buf, size, "%s", func == GPIOF_INPUT ? "input" : "output");
306 static int stmfx_pinctrl_bind(struct udevice *dev)
308 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
310 return device_bind_driver_to_node(dev->parent,
311 "stmfx-gpio", "stmfx-gpio",
312 dev_ofnode(dev), &plat->gpio);
315 static int stmfx_pinctrl_probe(struct udevice *dev)
317 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
319 return device_probe(plat->gpio);
322 const 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,
334 static const struct udevice_id stmfx_pinctrl_match[] = {
335 { .compatible = "st,stmfx-0300-pinctrl", },
338 U_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),
348 static int stmfx_chip_init(struct udevice *dev)
353 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
355 ret = dm_i2c_reg_read(dev, STMFX_REG_CHIP_ID);
357 dev_err(dev, "error reading chip id: %d\n", ret);
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.
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
372 if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (chip->chip_addr << 1)) {
373 dev_err(dev, "unknown chip id: %#x\n", id);
377 ret = dm_i2c_read(dev, STMFX_REG_FW_VERSION_MSB,
378 version, sizeof(version));
380 dev_err(dev, "error reading fw version: %d\n", ret);
384 dev_info(dev, "STMFX id: %#x, fw version: %x.%02x\n",
385 id, version[0], version[1]);
387 ret = dm_i2c_reg_read(dev, STMFX_REG_SYS_CTRL);
392 ret = dm_i2c_reg_write(dev, STMFX_REG_SYS_CTRL,
393 ret | STMFX_REG_SYS_CTRL_SWRST);
397 mdelay(STMFX_BOOT_TIME_MS);
402 static int stmfx_probe(struct udevice *dev)
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);
413 ret = regulator_set_enable(vdd, true);
415 dev_err(dev, "vdd enable failed: %d\n", ret);
420 return stmfx_chip_init(dev);
423 static const struct udevice_id stmfx_match[] = {
424 { .compatible = "st,stmfx-0300", },
427 U_BOOT_DRIVER(stmfx) = {
429 .id = UCLASS_I2C_GENERIC,
430 .of_match = of_match_ptr(stmfx_match),
431 .probe = stmfx_probe,
432 .bind = dm_scan_fdt_dev,