blob: 88df9e61a7375bf19fd02f6e056659d2d0546982 [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
Patrick Delaunaye8635c52020-06-04 14:30:30 +0200156static int stmfx_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
157 ulong flags)
158{
159 int ret = -ENOTSUPP;
160
161 if (flags & GPIOD_IS_OUT) {
162 if (flags & GPIOD_OPEN_SOURCE)
163 return -ENOTSUPP;
164 if (flags & GPIOD_OPEN_DRAIN)
165 ret = stmfx_conf_set_type(dev, offset, 0);
166 else /* PUSH-PULL */
167 ret = stmfx_conf_set_type(dev, offset, 1);
168 if (ret)
169 return ret;
170 ret = stmfx_gpio_direction_output(dev, offset,
171 GPIOD_FLAGS_OUTPUT(flags));
172 } else if (flags & GPIOD_IS_IN) {
173 ret = stmfx_gpio_direction_input(dev, offset);
174 if (ret)
175 return ret;
176 if (flags & GPIOD_PULL_UP) {
177 ret = stmfx_conf_set_type(dev, offset, 1);
178 if (ret)
179 return ret;
180 ret = stmfx_conf_set_pupd(dev, offset, 1);
181 } else if (flags & GPIOD_PULL_DOWN) {
182 ret = stmfx_conf_set_type(dev, offset, 1);
183 if (ret)
184 return ret;
185 ret = stmfx_conf_set_pupd(dev, offset, 0);
186 }
187 }
188
189 return ret;
190}
191
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100192static int stmfx_gpio_probe(struct udevice *dev)
193{
194 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
195 struct ofnode_phandle_args args;
196 u8 sys_ctrl;
197
198 uc_priv->bank_name = "stmfx";
199 uc_priv->gpio_count = STMFX_MAX_GPIO + STMFX_MAX_AGPIO;
200 if (!dev_read_phandle_with_args(dev, "gpio-ranges",
201 NULL, 3, 0, &args)) {
202 uc_priv->gpio_count = args.args[2];
203 }
204
205 /* enable GPIO function */
206 sys_ctrl = STMFX_REG_SYS_CTRL_GPIO_EN;
207 if (uc_priv->gpio_count > STMFX_MAX_GPIO)
208 sys_ctrl |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
209 stmfx_write(dev, STMFX_REG_SYS_CTRL, sys_ctrl);
210
211 return 0;
212}
213
214static const struct dm_gpio_ops stmfx_gpio_ops = {
215 .set_value = stmfx_gpio_set,
216 .get_value = stmfx_gpio_get,
217 .get_function = stmfx_gpio_get_function,
218 .direction_input = stmfx_gpio_direction_input,
219 .direction_output = stmfx_gpio_direction_output,
Patrick Delaunaye8635c52020-06-04 14:30:30 +0200220 .set_dir_flags = stmfx_gpio_set_dir_flags,
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100221};
222
223U_BOOT_DRIVER(stmfx_gpio) = {
224 .name = "stmfx-gpio",
225 .id = UCLASS_GPIO,
226 .probe = stmfx_gpio_probe,
227 .ops = &stmfx_gpio_ops,
228};
229
230#if CONFIG_IS_ENABLED(PINCONF)
231static const struct pinconf_param stmfx_pinctrl_conf_params[] = {
232 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
233 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 0 },
234 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 0 },
235 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 0 },
236 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
237 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
238 { "output-high", PIN_CONFIG_OUTPUT, 1 },
239 { "output-low", PIN_CONFIG_OUTPUT, 0 },
240};
241
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100242static int stmfx_pinctrl_conf_set(struct udevice *dev, unsigned int pin,
243 unsigned int param, unsigned int arg)
244{
245 int ret, dir;
246 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
247
248 dir = stmfx_gpio_get_function(plat->gpio, pin);
249
250 if (dir < 0)
251 return dir;
252
253 switch (param) {
254 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
255 case PIN_CONFIG_BIAS_DISABLE:
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200256 case PIN_CONFIG_DRIVE_PUSH_PULL:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200257 ret = stmfx_conf_set_type(dev, pin, 0);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200258 break;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100259 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200260 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200261 if (ret)
262 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200263 ret = stmfx_conf_set_pupd(dev, pin, 0);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100264 break;
265 case PIN_CONFIG_BIAS_PULL_UP:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200266 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunay461a9cd2019-07-30 19:16:11 +0200267 if (ret)
268 return ret;
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200269 ret = stmfx_conf_set_pupd(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100270 break;
271 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
Patrick Delaunay0aab9d32020-06-04 14:30:28 +0200272 ret = stmfx_conf_set_type(dev, pin, 1);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100273 break;
274 case PIN_CONFIG_OUTPUT:
275 ret = stmfx_gpio_direction_output(plat->gpio, pin, arg);
276 break;
277 default:
278 return -ENOTSUPP;
279 }
280
281 return ret;
282}
283#endif
284
285static int stmfx_pinctrl_get_pins_count(struct udevice *dev)
286{
287 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
288 struct gpio_dev_priv *uc_priv;
289
290 uc_priv = dev_get_uclass_priv(plat->gpio);
291
292 return uc_priv->gpio_count;
293}
294
295/*
296 * STMFX pins[15:0] are called "gpio[15:0]"
297 * and STMFX pins[23:16] are called "agpio[7:0]"
298 */
299#define MAX_PIN_NAME_LEN 7
300static char pin_name[MAX_PIN_NAME_LEN];
301static const char *stmfx_pinctrl_get_pin_name(struct udevice *dev,
302 unsigned int selector)
303{
304 if (selector < STMFX_MAX_GPIO)
305 snprintf(pin_name, MAX_PIN_NAME_LEN, "gpio%u", selector);
306 else
307 snprintf(pin_name, MAX_PIN_NAME_LEN, "agpio%u", selector - 16);
308 return pin_name;
309}
310
311static int stmfx_pinctrl_get_pin_muxing(struct udevice *dev,
312 unsigned int selector,
313 char *buf, int size)
314{
315 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
316 int func;
317
318 func = stmfx_gpio_get_function(plat->gpio, selector);
319 if (func < 0)
320 return func;
321
322 snprintf(buf, size, "%s", func == GPIOF_INPUT ? "input" : "output");
323
324 return 0;
325}
326
327static int stmfx_pinctrl_bind(struct udevice *dev)
328{
329 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
330
331 return device_bind_driver_to_node(dev->parent,
332 "stmfx-gpio", "stmfx-gpio",
333 dev_ofnode(dev), &plat->gpio);
334};
335
336static int stmfx_pinctrl_probe(struct udevice *dev)
337{
338 struct stmfx_pinctrl *plat = dev_get_platdata(dev);
339
340 return device_probe(plat->gpio);
341};
342
343const struct pinctrl_ops stmfx_pinctrl_ops = {
344 .get_pins_count = stmfx_pinctrl_get_pins_count,
345 .get_pin_name = stmfx_pinctrl_get_pin_name,
346 .set_state = pinctrl_generic_set_state,
347 .get_pin_muxing = stmfx_pinctrl_get_pin_muxing,
348#if CONFIG_IS_ENABLED(PINCONF)
349 .pinconf_set = stmfx_pinctrl_conf_set,
350 .pinconf_num_params = ARRAY_SIZE(stmfx_pinctrl_conf_params),
351 .pinconf_params = stmfx_pinctrl_conf_params,
352#endif
353};
354
355static const struct udevice_id stmfx_pinctrl_match[] = {
356 { .compatible = "st,stmfx-0300-pinctrl", },
357};
358
359U_BOOT_DRIVER(stmfx_pinctrl) = {
360 .name = "stmfx-pinctrl",
361 .id = UCLASS_PINCTRL,
362 .of_match = of_match_ptr(stmfx_pinctrl_match),
363 .bind = stmfx_pinctrl_bind,
364 .probe = stmfx_pinctrl_probe,
365 .ops = &stmfx_pinctrl_ops,
366 .platdata_auto_alloc_size = sizeof(struct stmfx_pinctrl),
367};
368
369static int stmfx_chip_init(struct udevice *dev)
370{
371 u8 id;
372 u8 version[2];
373 int ret;
374 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
375
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100376 ret = dm_i2c_reg_read(dev, STMFX_REG_CHIP_ID);
377 if (ret < 0) {
378 dev_err(dev, "error reading chip id: %d\n", ret);
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100379 return ret;
380 }
Patrick Delaunaye4c076f2020-01-28 10:44:14 +0100381 id = (u8)ret;
Patrick Delaunayd65291b2019-03-11 11:13:15 +0100382 /*
383 * Check that ID is the complement of the I2C address:
384 * STMFX I2C address follows the 7-bit format (MSB), that's why
385 * client->addr is shifted.
386 *
387 * STMFX_I2C_ADDR| STMFX | Linux
388 * input pin | I2C device address | I2C device address
389 *---------------------------------------------------------
390 * 0 | b: 1000 010x h:0x84 | 0x42
391 * 1 | b: 1000 011x h:0x86 | 0x43
392 */
393 if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (chip->chip_addr << 1)) {
394 dev_err(dev, "unknown chip id: %#x\n", id);
395 return -EINVAL;
396 }
397
398 ret = dm_i2c_read(dev, STMFX_REG_FW_VERSION_MSB,
399 version, sizeof(version));
400 if (ret) {
401 dev_err(dev, "error reading fw version: %d\n", ret);
402 return ret;
403 }
404
405 dev_info(dev, "STMFX id: %#x, fw version: %x.%02x\n",
406 id, version[0], version[1]);
407
408 ret = dm_i2c_reg_read(dev, STMFX_REG_SYS_CTRL);
409
410 if (ret < 0)
411 return ret;
412
413 ret = dm_i2c_reg_write(dev, STMFX_REG_SYS_CTRL,
414 ret | STMFX_REG_SYS_CTRL_SWRST);
415 if (ret)
416 return ret;
417
418 mdelay(STMFX_BOOT_TIME_MS);
419
420 return ret;
421}
422
423static int stmfx_probe(struct udevice *dev)
424{
425 struct udevice *vdd;
426 int ret;
427
428 ret = device_get_supply_regulator(dev, "vdd-supply", &vdd);
429 if (ret && ret != -ENOENT) {
430 dev_err(dev, "vdd regulator error:%d\n", ret);
431 return ret;
432 }
433 if (!ret) {
434 ret = regulator_set_enable(vdd, true);
435 if (ret) {
436 dev_err(dev, "vdd enable failed: %d\n", ret);
437 return ret;
438 }
439 }
440
441 return stmfx_chip_init(dev);
442}
443
444static const struct udevice_id stmfx_match[] = {
445 { .compatible = "st,stmfx-0300", },
446};
447
448U_BOOT_DRIVER(stmfx) = {
449 .name = "stmfx",
450 .id = UCLASS_I2C_GENERIC,
451 .of_match = of_match_ptr(stmfx_match),
452 .probe = stmfx_probe,
453 .bind = dm_scan_fdt_dev,
454};