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