pinctrl-stmfx.c 13 KB

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