blob: 5d15424b845c69f7dc2aae49a6ac1d073967c40a [file] [log] [blame]
Patrick Delaunayd65291b2019-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 Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Patrick Delaunayd65291b2019-03-11 11:13:15 +010015#include <dm/lists.h>
16#include <dm/pinctrl.h>
17#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Patrick Delaunayd65291b2019-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 Delaunaybcd108e2020-06-04 14:30:29 +020077static int stmfx_read_reg(struct udevice *dev, u8 reg_base, uint offset)
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020078{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020079 u8 reg = reg_base + get_reg(offset);
80 u32 mask = get_mask(offset);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020081 int ret;
82
83 ret = stmfx_read(dev, reg);
84 if (ret < 0)
85 return ret;
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020086
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020087 return ret < 0 ? ret : !!(ret & mask);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020088}
89
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020090static int stmfx_write_reg(struct udevice *dev, u8 reg_base, uint offset,
91 uint val)
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020092{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +020093 u8 reg = reg_base + get_reg(offset);
94 u32 mask = get_mask(offset);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +020095 int ret;
96
97 ret = stmfx_read(dev, reg);
98 if (ret < 0)
99 return ret;
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200100 ret = (ret & ~mask) | (val ? mask : 0);
Patrick Delaunay2fcd0372020-06-04 14:30:27 +0200101
102 return stmfx_write(dev, reg, ret);
103}
104
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200105static int stmfx_conf_set_pupd(struct udevice *dev, unsigned int offset,
106 uint pupd)
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100107{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200108 return stmfx_write_reg(dev, STMFX_REG_GPIO_PUPD, offset, pupd);
109}
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100110
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200111static int stmfx_conf_set_type(struct udevice *dev, unsigned int offset,
112 uint type)
113{
114 return stmfx_write_reg(dev, STMFX_REG_GPIO_TYPE, offset, type);
115}
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100116
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200117static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
118{
119 return stmfx_read_reg(dev, STMFX_REG_GPIO_STATE, offset);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100120}
121
122static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
123{
124 u32 reg = value ? STMFX_REG_GPO_SET : STMFX_REG_GPO_CLR;
125 u32 mask = get_mask(offset);
126
127 return stmfx_write(dev, reg + get_reg(offset), mask);
128}
129
130static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset)
131{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200132 int ret = stmfx_read_reg(dev, STMFX_REG_GPIO_DIR, offset);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100133
134 if (ret < 0)
135 return ret;
136 /* On stmfx, gpio pins direction is (0)input, (1)output. */
137
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200138 return ret ? GPIOF_OUTPUT : GPIOF_INPUT;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100139}
140
141static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset)
142{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200143 return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 0);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100144}
145
146static int stmfx_gpio_direction_output(struct udevice *dev,
147 unsigned int offset, int value)
148{
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200149 int ret = stmfx_gpio_set(dev, offset, value);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100150 if (ret < 0)
151 return ret;
152
Patrick Delaunaybcd108e2020-06-04 14:30:29 +0200153 return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100154}
155
156static int stmfx_gpio_probe(struct udevice *dev)
157{
158 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
159 struct ofnode_phandle_args args;
160 u8 sys_ctrl;
161
162 uc_priv->bank_name = "stmfx";
163 uc_priv->gpio_count = STMFX_MAX_GPIO + STMFX_MAX_AGPIO;
164 if (!dev_read_phandle_with_args(dev, "gpio-ranges",
165 NULL, 3, 0, &args)) {
166 uc_priv->gpio_count = args.args[2];
167 }
168
169 /* enable GPIO function */
170 sys_ctrl = STMFX_REG_SYS_CTRL_GPIO_EN;
171 if (uc_priv->gpio_count > STMFX_MAX_GPIO)
172 sys_ctrl |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
173 stmfx_write(dev, STMFX_REG_SYS_CTRL, sys_ctrl);
174
175 return 0;
176}
177
178static const struct dm_gpio_ops stmfx_gpio_ops = {
179 .set_value = stmfx_gpio_set,
180 .get_value = stmfx_gpio_get,
181 .get_function = stmfx_gpio_get_function,
182 .direction_input = stmfx_gpio_direction_input,
183 .direction_output = stmfx_gpio_direction_output,
184};
185
186U_BOOT_DRIVER(stmfx_gpio) = {
187 .name = "stmfx-gpio",
188 .id = UCLASS_GPIO,
189 .probe = stmfx_gpio_probe,
190 .ops = &stmfx_gpio_ops,
191};
192
193#if CONFIG_IS_ENABLED(PINCONF)
194static const struct pinconf_param stmfx_pinctrl_conf_params[] = {
195 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
196 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 0 },
197 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 0 },
198 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 0 },
199 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
200 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
201 { "output-high", PIN_CONFIG_OUTPUT, 1 },
202 { "output-low", PIN_CONFIG_OUTPUT, 0 },
203};
204
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100205static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin,
206 unsigned int param, unsigned int arg)
207{
208 int ret, dir;
209 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
210
211 dir = stmfx_gpio_get_function(plat->gpio, pin);
212
213 if (dir < 0)
214 return dir;
215
216 switch (param) {
217 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
218 case PIN_CONFIG_BIAS_DISABLE:
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200219 case PIN_CONFIG_DRIVE_PUSH_PULL:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200220 ret = stmfx_conf_set_type(dev, pin, 0);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200221 break;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100222 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200223 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200224 if (ret)
225 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200226 ret = stmfx_conf_set_pupd(dev, pin, 0);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100227 break;
228 case PIN_CONFIG_BIAS_PULL_UP:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200229 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200230 if (ret)
231 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200232 ret = stmfx_conf_set_pupd(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100233 break;
234 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200235 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100236 break;
237 case PIN_CONFIG_OUTPUT:
238 ret = stmfx_gpio_direction_output(plat->gpio, pin, arg);
239 break;
240 default:
241 return -ENOTSUPP;
242 }
243
244 return ret;
245}
246#endif
247
248static int stmfx_pinctrl_get_pins_count(struct udevice *dev)
249{
250 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
251 struct gpio_dev_priv *uc_priv;
252
253 uc_priv = dev_get_uclass_priv(plat->gpio);
254
255 return uc_priv->gpio_count;
256}
257
258/*
259 * STMFX pins[15:0] are called "gpio[15:0]"
260 * and STMFX pins[23:16] are called "agpio[7:0]"
261 */
262#define MAX_PIN_NAME_LEN 7
263static char pin_name[MAX_PIN_NAME_LEN];
264static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev,
265 unsigned int selector)
266{
267 if (selector < STMFX_MAX_GPIO)
268 snprintf(pin_name, MAX_PIN_NAME_LEN, "gpio%u", selector);
269 else
270 snprintf(pin_name, MAX_PIN_NAME_LEN, "agpio%u", selector - 16);
271 return pin_name;
272}
273
274static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev,
275 unsigned int selector,
276 char *buf, int size)
277{
278 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
279 int func;
280
281 func = stmfx_gpio_get_function(plat->gpio, selector);
282 if (func < 0)
283 return func;
284
285 snprintf(buf, size, "%s", func == GPIOF_INPUT ? "input" : "output");
286
287 return 0;
288}
289
290static int stmfx_pinctrl_bind(struct udevice *dev)
291{
292 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
293
294 return device_bind_driver_to_node(dev->parent,
295 "stmfx-gpio", "stmfx-gpio",
296 dev_ofnode(dev), &plat->gpio);
297};
298
299static int stmfx_pinctrl_probe(struct udevice *dev)
300{
301 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
302
303 return device_probe(plat->gpio);
304};
305
306const struct pinctrl_ops stmfx_pinctrl_ops = {
307 .get_pins_count = stmfx_pinctrl_get_pins_count,
308 .get_pin_name = stmfx_pinctrl_get_pin_name,
309 .set_state = pinctrl_generic_set_state,
310 .get_pin_muxing = stmfx_pinctrl_get_pin_muxing,
311#if CONFIG_IS_ENABLED(PINCONF)
312 .pinconf_set = stmfx_pinctrl_conf_set,
313 .pinconf_num_params = ARRAY_SIZE(stmfx_pinctrl_conf_params),
314 .pinconf_params = stmfx_pinctrl_conf_params,
315#endif
316};
317
318static const struct udevice_id stmfx_pinctrl_match[] = {
319 { .compatible = "st,stmfx-0300-pinctrl", },
320};
321
322U_BOOT_DRIVER(stmfx_pinctrl) = {
323 .name = "stmfx-pinctrl",
324 .id = UCLASS_PINCTRL,
325 .of_match = of_match_ptr(stmfx_pinctrl_match),
326 .bind = stmfx_pinctrl_bind,
327 .probe = stmfx_pinctrl_probe,
328 .ops = &stmfx_pinctrl_ops,
329 .platdata_auto_alloc_size = sizeof(struct stmfx_pinctrl),
330};
331
332static int stmfx_chip_init(struct udevice *dev)
333{
334 u8 id;
335 u8 version[2];
336 int ret;
337 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
338
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100339 ret = dm_i2c_reg_read(dev, STMFX_REG_CHIP_ID);
340 if (ret < 0) {
341 dev_err(dev, "error reading chip id: %d\n", ret);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100342 return ret;
343 }
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100344 id = (u8)ret;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100345 /*
346 * Check that ID is the complement of the I2C address:
347 * STMFX I2C address follows the 7-bit format (MSB), that's why
348 * client->addr is shifted.
349 *
350 * STMFX_I2C_ADDR| STMFX | Linux
351 * input pin | I2C device address | I2C device address
352 *---------------------------------------------------------
353 * 0 | b: 1000 010x h:0x84 | 0x42
354 * 1 | b: 1000 011x h:0x86 | 0x43
355 */
356 if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (chip->chip_addr << 1)) {
357 dev_err(dev, "unknown chip id: %#x\n", id);
358 return -EINVAL;
359 }
360
361 ret = dm_i2c_read(dev, STMFX_REG_FW_VERSION_MSB,
362 version, sizeof(version));
363 if (ret) {
364 dev_err(dev, "error reading fw version: %d\n", ret);
365 return ret;
366 }
367
368 dev_info(dev, "STMFX id: %#x, fw version: %x.%02x\n",
369 id, version[0], version[1]);
370
371 ret = dm_i2c_reg_read(dev, STMFX_REG_SYS_CTRL);
372
373 if (ret < 0)
374 return ret;
375
376 ret = dm_i2c_reg_write(dev, STMFX_REG_SYS_CTRL,
377 ret | STMFX_REG_SYS_CTRL_SWRST);
378 if (ret)
379 return ret;
380
381 mdelay(STMFX_BOOT_TIME_MS);
382
383 return ret;
384}
385
386static int stmfx_probe(struct udevice *dev)
387{
388 struct udevice *vdd;
389 int ret;
390
391 ret = device_get_supply_regulator(dev, "vdd-supply", &vdd);
392 if (ret && ret != -ENOENT) {
393 dev_err(dev, "vdd regulator error:%d\n", ret);
394 return ret;
395 }
396 if (!ret) {
397 ret = regulator_set_enable(vdd, true);
398 if (ret) {
399 dev_err(dev, "vdd enable failed: %d\n", ret);
400 return ret;
401 }
402 }
403
404 return stmfx_chip_init(dev);
405}
406
407static const struct udevice_id stmfx_match[] = {
408 { .compatible = "st,stmfx-0300", },
409};
410
411U_BOOT_DRIVER(stmfx) = {
412 .name = "stmfx",
413 .id = UCLASS_I2C_GENERIC,
414 .of_match = of_match_ptr(stmfx_match),
415 .probe = stmfx_probe,
416 .bind = dm_scan_fdt_dev,
417};