Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 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 | */ |
Patrick Delaunay | 4c59560 | 2021-11-10 18:14:10 +0100 | [diff] [blame] | 8 | |
| 9 | #define LOG_CATEGORY UCLASS_PINCTRL |
| 10 | |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <dm.h> |
Patrick Delaunay | 4c59560 | 2021-11-10 18:14:10 +0100 | [diff] [blame] | 13 | #include <log.h> |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 14 | #include <i2c.h> |
| 15 | #include <asm/gpio.h> |
| 16 | #include <dm/device.h> |
| 17 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 19 | #include <dm/lists.h> |
| 20 | #include <dm/pinctrl.h> |
| 21 | #include <linux/bitfield.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 24 | #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 | |
| 67 | struct stmfx_pinctrl { |
| 68 | struct udevice *gpio; |
| 69 | }; |
| 70 | |
| 71 | static int stmfx_read(struct udevice *dev, uint offset) |
| 72 | { |
| 73 | return dm_i2c_reg_read(dev_get_parent(dev), offset); |
| 74 | } |
| 75 | |
| 76 | static 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 Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 81 | static int stmfx_read_reg(struct udevice *dev, u8 reg_base, uint offset) |
Patrick Delaunay | 84115cd | 2020-06-04 14:30:27 +0200 | [diff] [blame] | 82 | { |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 83 | u8 reg = reg_base + get_reg(offset); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 84 | u32 mask = get_mask(offset); |
| 85 | int ret; |
| 86 | |
| 87 | ret = stmfx_read(dev, reg); |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 88 | if (ret < 0) |
| 89 | return ret; |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 90 | |
| 91 | return ret < 0 ? ret : !!(ret & mask); |
| 92 | } |
| 93 | |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 94 | static int stmfx_write_reg(struct udevice *dev, u8 reg_base, uint offset, |
| 95 | uint val) |
| 96 | { |
| 97 | u8 reg = reg_base + get_reg(offset); |
| 98 | u32 mask = get_mask(offset); |
| 99 | int ret; |
| 100 | |
| 101 | ret = stmfx_read(dev, reg); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | ret = (ret & ~mask) | (val ? mask : 0); |
| 105 | |
| 106 | return stmfx_write(dev, reg, ret); |
| 107 | } |
| 108 | |
| 109 | static int stmfx_conf_set_pupd(struct udevice *dev, unsigned int offset, |
| 110 | uint pupd) |
| 111 | { |
| 112 | return stmfx_write_reg(dev, STMFX_REG_GPIO_PUPD, offset, pupd); |
| 113 | } |
| 114 | |
Patrick Delaunay | 8d895ef | 2020-06-04 14:30:31 +0200 | [diff] [blame] | 115 | static 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 Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 120 | static 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 | } |
| 125 | |
Patrick Delaunay | 8d895ef | 2020-06-04 14:30:31 +0200 | [diff] [blame] | 126 | static 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 Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 131 | static int stmfx_gpio_get(struct udevice *dev, unsigned int offset) |
| 132 | { |
| 133 | return stmfx_read_reg(dev, STMFX_REG_GPIO_STATE, offset); |
| 134 | } |
| 135 | |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 136 | static 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 | |
| 144 | static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 145 | { |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 146 | int ret = stmfx_read_reg(dev, STMFX_REG_GPIO_DIR, offset); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 147 | |
| 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | /* On stmfx, gpio pins direction is (0)input, (1)output. */ |
| 151 | |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 152 | return ret ? GPIOF_OUTPUT : GPIOF_INPUT; |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset) |
| 156 | { |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 157 | return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 0); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int stmfx_gpio_direction_output(struct udevice *dev, |
| 161 | unsigned int offset, int value) |
| 162 | { |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 163 | int ret = stmfx_gpio_set(dev, offset, value); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
Patrick Delaunay | fabb6e1 | 2020-06-04 14:30:29 +0200 | [diff] [blame] | 167 | return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 1); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Simon Glass | 13979fc | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 170 | static int stmfx_gpio_set_flags(struct udevice *dev, unsigned int offset, |
| 171 | ulong flags) |
Patrick Delaunay | 22b3fe4 | 2020-06-04 14:30:30 +0200 | [diff] [blame] | 172 | { |
| 173 | int ret = -ENOTSUPP; |
| 174 | |
| 175 | if (flags & GPIOD_IS_OUT) { |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 176 | bool value = flags & GPIOD_IS_OUT_ACTIVE; |
| 177 | |
Patrick Delaunay | 22b3fe4 | 2020-06-04 14:30:30 +0200 | [diff] [blame] | 178 | 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 Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 186 | ret = stmfx_gpio_direction_output(dev, offset, value); |
Patrick Delaunay | 22b3fe4 | 2020-06-04 14:30:30 +0200 | [diff] [blame] | 187 | } 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 Glass | 9648789 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 207 | static int stmfx_gpio_get_flags(struct udevice *dev, unsigned int offset, |
| 208 | ulong *flagsp) |
Patrick Delaunay | 8d895ef | 2020-06-04 14:30:31 +0200 | [diff] [blame] | 209 | { |
| 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 Glass | 9648789 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 241 | *flagsp = dir_flags; |
Patrick Delaunay | 8d895ef | 2020-06-04 14:30:31 +0200 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 246 | static 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 | |
| 268 | static 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 Glass | 13979fc | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 274 | .set_flags = stmfx_gpio_set_flags, |
Simon Glass | 9648789 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 275 | .get_flags = stmfx_gpio_get_flags, |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | U_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) |
| 286 | static 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 Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 297 | static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin, |
| 298 | unsigned int param, unsigned int arg) |
| 299 | { |
| 300 | int ret, dir; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 301 | struct stmfx_pinctrl *plat = dev_get_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 302 | |
| 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 Delaunay | abee80d | 2019-07-30 19:16:11 +0200 | [diff] [blame] | 311 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 312 | ret = stmfx_conf_set_type(dev, pin, 0); |
Patrick Delaunay | abee80d | 2019-07-30 19:16:11 +0200 | [diff] [blame] | 313 | break; |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 314 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 315 | ret = stmfx_conf_set_type(dev, pin, 1); |
Patrick Delaunay | abee80d | 2019-07-30 19:16:11 +0200 | [diff] [blame] | 316 | if (ret) |
| 317 | return ret; |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 318 | ret = stmfx_conf_set_pupd(dev, pin, 0); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 319 | break; |
| 320 | case PIN_CONFIG_BIAS_PULL_UP: |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 321 | ret = stmfx_conf_set_type(dev, pin, 1); |
Patrick Delaunay | abee80d | 2019-07-30 19:16:11 +0200 | [diff] [blame] | 322 | if (ret) |
| 323 | return ret; |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 324 | ret = stmfx_conf_set_pupd(dev, pin, 1); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 325 | break; |
| 326 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
Patrick Delaunay | 067c739 | 2020-06-04 14:30:28 +0200 | [diff] [blame] | 327 | ret = stmfx_conf_set_type(dev, pin, 1); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 328 | 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 | |
| 340 | static int stmfx_pinctrl_get_pins_count(struct udevice *dev) |
| 341 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 342 | struct stmfx_pinctrl *plat = dev_get_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 343 | 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 Chotard | 54f5bf3 | 2021-01-20 13:43:39 +0100 | [diff] [blame] | 351 | * STMFX pins[15:0] are called "gpio[15:0]" |
| 352 | * and STMFX pins[23:16] are called "agpio[7:0]" |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 353 | */ |
Patrice Chotard | ca5cc31 | 2021-01-20 13:43:40 +0100 | [diff] [blame] | 354 | static char pin_name[PINNAME_SIZE]; |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 355 | static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev, |
| 356 | unsigned int selector) |
| 357 | { |
| 358 | if (selector < STMFX_MAX_GPIO) |
Patrice Chotard | ca5cc31 | 2021-01-20 13:43:40 +0100 | [diff] [blame] | 359 | snprintf(pin_name, PINNAME_SIZE, "gpio%u", selector); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 360 | else |
Patrice Chotard | ca5cc31 | 2021-01-20 13:43:40 +0100 | [diff] [blame] | 361 | snprintf(pin_name, PINNAME_SIZE, "agpio%u", selector - 16); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 362 | return pin_name; |
| 363 | } |
| 364 | |
Patrick Delaunay | 5593333 | 2020-06-04 14:30:32 +0200 | [diff] [blame] | 365 | static 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 Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 393 | static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev, |
| 394 | unsigned int selector, |
| 395 | char *buf, int size) |
| 396 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 397 | struct stmfx_pinctrl *plat = dev_get_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 398 | int func; |
| 399 | |
| 400 | func = stmfx_gpio_get_function(plat->gpio, selector); |
| 401 | if (func < 0) |
| 402 | return func; |
| 403 | |
Patrick Delaunay | 5593333 | 2020-06-04 14:30:32 +0200 | [diff] [blame] | 404 | snprintf(buf, size, "%s ", func == GPIOF_INPUT ? "input" : "output"); |
| 405 | |
| 406 | strncat(buf, stmfx_pinctrl_get_pin_conf(dev, selector, func), size); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int stmfx_pinctrl_bind(struct udevice *dev) |
| 412 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 413 | struct stmfx_pinctrl *plat = dev_get_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 414 | |
Patrick Delaunay | c2a8181 | 2020-10-28 10:51:56 +0100 | [diff] [blame] | 415 | /* subnode name is not explicit: use father name */ |
| 416 | device_set_name(dev, dev->parent->name); |
| 417 | |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 418 | return device_bind_driver_to_node(dev->parent, |
Patrick Delaunay | c2a8181 | 2020-10-28 10:51:56 +0100 | [diff] [blame] | 419 | "stmfx-gpio", dev->parent->name, |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 420 | dev_ofnode(dev), &plat->gpio); |
| 421 | }; |
| 422 | |
| 423 | static int stmfx_pinctrl_probe(struct udevice *dev) |
| 424 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 425 | struct stmfx_pinctrl *plat = dev_get_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 426 | |
| 427 | return device_probe(plat->gpio); |
| 428 | }; |
| 429 | |
| 430 | const 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 | |
| 442 | static const struct udevice_id stmfx_pinctrl_match[] = { |
| 443 | { .compatible = "st,stmfx-0300-pinctrl", }, |
| 444 | }; |
| 445 | |
| 446 | U_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 Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 453 | .plat_auto = sizeof(struct stmfx_pinctrl), |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static int stmfx_chip_init(struct udevice *dev) |
| 457 | { |
| 458 | u8 id; |
| 459 | u8 version[2]; |
| 460 | int ret; |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 461 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 462 | |
Patrick Delaunay | b100892 | 2020-01-28 10:44:14 +0100 | [diff] [blame] | 463 | 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 Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 466 | return ret; |
| 467 | } |
Patrick Delaunay | b100892 | 2020-01-28 10:44:14 +0100 | [diff] [blame] | 468 | id = (u8)ret; |
Patrick Delaunay | 8262435 | 2019-03-11 11:13:15 +0100 | [diff] [blame] | 469 | /* |
| 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 | |
| 510 | static 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 | |
| 531 | static const struct udevice_id stmfx_match[] = { |
| 532 | { .compatible = "st,stmfx-0300", }, |
| 533 | }; |
| 534 | |
| 535 | U_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 | }; |