pinctrl-at91-pio4.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Atmel PIO4 pinctrl driver
  4. *
  5. * Copyright (C) 2016 Atmel Corporation
  6. * Wenyou.Yang <wenyou.yang@atmel.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <asm/global_data.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/lists.h>
  13. #include <dm/pinctrl.h>
  14. #include <linux/bitops.h>
  15. #include <linux/io.h>
  16. #include <linux/err.h>
  17. #include <dm/uclass-internal.h>
  18. #include <mach/atmel_pio4.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * Warning:
  22. * In order to not introduce confusion between Atmel PIO groups and pinctrl
  23. * framework groups, Atmel PIO groups will be called banks.
  24. */
  25. struct atmel_pio4_plat {
  26. struct atmel_pio4_port *reg_base;
  27. unsigned int slew_rate_support;
  28. };
  29. /*
  30. * Table keeping track of the pinctrl driver's slew rate support and the
  31. * corresponding index into the struct udevice_id of the gpio_atmel_pio4 GPIO
  32. * driver. This has been done in order to align the DT of U-Boot with the DT of
  33. * Linux. In Linux, a phandle from a '-gpio' DT property is linked to the
  34. * pinctrl driver, unlike U-Boot which redirects this phandle to a corresponding
  35. * UCLASS_GPIO driver. Thus, in order to link the two, a hook to the bind method
  36. * of the pinctrl driver in U-Boot has been added. This bind method will attach
  37. * the GPIO driver to the pinctrl DT node using this table.
  38. * @slew_rate_support pinctrl driver's slew rate support
  39. * @gdidx index into the GPIO driver's struct udevide_id
  40. * (needed in order to properly bind with driver_data)
  41. */
  42. struct atmel_pinctrl_data {
  43. unsigned int slew_rate_support;
  44. int gdidx;
  45. };
  46. static const struct pinconf_param conf_params[] = {
  47. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  48. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  49. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  50. { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
  51. { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
  52. { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
  53. { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
  54. { "atmel,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  55. { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
  56. };
  57. static u32 atmel_pinctrl_get_pinconf(struct udevice *config,
  58. struct atmel_pio4_plat *plat)
  59. {
  60. const struct pinconf_param *params;
  61. u32 param, arg, conf = 0;
  62. u32 i;
  63. u32 val;
  64. for (i = 0; i < ARRAY_SIZE(conf_params); i++) {
  65. params = &conf_params[i];
  66. if (!dev_read_prop(config, params->property, NULL))
  67. continue;
  68. param = params->param;
  69. arg = params->default_value;
  70. /* Keep slew rate enabled by default. */
  71. if (plat->slew_rate_support)
  72. conf |= ATMEL_PIO_SR;
  73. switch (param) {
  74. case PIN_CONFIG_BIAS_DISABLE:
  75. conf &= (~ATMEL_PIO_PUEN_MASK);
  76. conf &= (~ATMEL_PIO_PDEN_MASK);
  77. break;
  78. case PIN_CONFIG_BIAS_PULL_UP:
  79. conf |= ATMEL_PIO_PUEN_MASK;
  80. break;
  81. case PIN_CONFIG_BIAS_PULL_DOWN:
  82. conf |= ATMEL_PIO_PDEN_MASK;
  83. break;
  84. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  85. if (arg == 0)
  86. conf &= (~ATMEL_PIO_OPD_MASK);
  87. else
  88. conf |= ATMEL_PIO_OPD_MASK;
  89. break;
  90. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  91. if (arg == 0)
  92. conf |= ATMEL_PIO_SCHMITT_MASK;
  93. else
  94. conf &= (~ATMEL_PIO_SCHMITT_MASK);
  95. break;
  96. case PIN_CONFIG_INPUT_DEBOUNCE:
  97. if (arg == 0) {
  98. conf &= (~ATMEL_PIO_IFEN_MASK);
  99. conf &= (~ATMEL_PIO_IFSCEN_MASK);
  100. } else {
  101. conf |= ATMEL_PIO_IFEN_MASK;
  102. conf |= ATMEL_PIO_IFSCEN_MASK;
  103. }
  104. break;
  105. case PIN_CONFIG_DRIVE_STRENGTH:
  106. dev_read_u32(config, params->property, &val);
  107. conf &= (~ATMEL_PIO_DRVSTR_MASK);
  108. conf |= (val << ATMEL_PIO_DRVSTR_OFFSET)
  109. & ATMEL_PIO_DRVSTR_MASK;
  110. break;
  111. case PIN_CONFIG_SLEW_RATE:
  112. if (!plat->slew_rate_support)
  113. break;
  114. dev_read_u32(config, params->property, &val);
  115. /* And disable it if requested. */
  116. if (val == 0)
  117. conf &= ~ATMEL_PIO_SR;
  118. break;
  119. default:
  120. printf("%s: Unsupported configuration parameter: %u\n",
  121. __func__, param);
  122. break;
  123. }
  124. }
  125. return conf;
  126. }
  127. static inline struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
  128. u32 bank)
  129. {
  130. struct atmel_pio4_plat *plat = dev_get_plat(dev);
  131. struct atmel_pio4_port *bank_base =
  132. (struct atmel_pio4_port *)((u32)plat->reg_base +
  133. ATMEL_PIO_BANK_OFFSET * bank);
  134. return bank_base;
  135. }
  136. #define MAX_PINMUX_ENTRIES 40
  137. static int atmel_process_config_dev(struct udevice *dev, struct udevice *config)
  138. {
  139. struct atmel_pio4_plat *plat = dev_get_plat(dev);
  140. int node = dev_of_offset(config);
  141. struct atmel_pio4_port *bank_base;
  142. u32 offset, func, bank, line;
  143. u32 cells[MAX_PINMUX_ENTRIES];
  144. u32 i, conf;
  145. int count;
  146. conf = atmel_pinctrl_get_pinconf(config, plat);
  147. /*
  148. * The only case where this function returns a negative error value
  149. * is when there is no "pinmux" property attached to this node
  150. */
  151. count = fdtdec_get_int_array_count(gd->fdt_blob, node, "pinmux",
  152. cells, ARRAY_SIZE(cells));
  153. if (count < 0)
  154. return count;
  155. if (count > MAX_PINMUX_ENTRIES)
  156. return -EINVAL;
  157. for (i = 0 ; i < count; i++) {
  158. offset = ATMEL_GET_PIN_NO(cells[i]);
  159. func = ATMEL_GET_PIN_FUNC(cells[i]);
  160. bank = ATMEL_PIO_BANK(offset);
  161. line = ATMEL_PIO_LINE(offset);
  162. bank_base = atmel_pio4_bank_base(dev, bank);
  163. writel(BIT(line), &bank_base->mskr);
  164. conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
  165. conf |= (func & ATMEL_PIO_CFGR_FUNC_MASK);
  166. writel(conf, &bank_base->cfgr);
  167. }
  168. return 0;
  169. }
  170. static int atmel_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  171. {
  172. int node = dev_of_offset(config);
  173. struct udevice *subconfig;
  174. int subnode, subnode_count = 0, ret;
  175. /*
  176. * If this function returns a negative error code then that means
  177. * that either the "pinmux" property of the node is missing, which is
  178. * the case for pinctrl nodes that do not have all the pins with the
  179. * same configuration and are split in multiple subnodes, or something
  180. * else went wrong and we have to stop. For the latter case, it would
  181. * mean that the node failed even though it has no subnodes.
  182. */
  183. ret = atmel_process_config_dev(dev, config);
  184. if (!ret)
  185. return ret;
  186. /*
  187. * If we reach here, it means that the subnode pinctrl's DT has multiple
  188. * subnodes. If it does not, then something else went wrong in the
  189. * previous call to atmel_process_config_dev.
  190. */
  191. fdt_for_each_subnode(subnode, gd->fdt_blob, node) {
  192. /* Get subnode as an udevice */
  193. ret = uclass_find_device_by_of_offset(UCLASS_PINCONFIG, subnode,
  194. &subconfig);
  195. if (ret)
  196. return ret;
  197. /*
  198. * If this time the function returns an error code on a subnode
  199. * then something is totally wrong so abort.
  200. */
  201. ret = atmel_process_config_dev(dev, subconfig);
  202. if (ret)
  203. return ret;
  204. subnode_count++;
  205. }
  206. /*
  207. * If we somehow got here and we do not have any subnodes, abort.
  208. */
  209. if (!subnode_count)
  210. return -EINVAL;
  211. return 0;
  212. }
  213. const struct pinctrl_ops atmel_pinctrl_ops = {
  214. .set_state = atmel_pinctrl_set_state,
  215. };
  216. static int atmel_pinctrl_probe(struct udevice *dev)
  217. {
  218. struct atmel_pio4_plat *plat = dev_get_plat(dev);
  219. struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev);
  220. fdt_addr_t addr_base;
  221. addr_base = dev_read_addr(dev);
  222. if (addr_base == FDT_ADDR_T_NONE)
  223. return -EINVAL;
  224. plat->reg_base = (struct atmel_pio4_port *)addr_base;
  225. plat->slew_rate_support = priv->slew_rate_support;
  226. return 0;
  227. }
  228. static int atmel_pinctrl_bind(struct udevice *dev)
  229. {
  230. struct udevice *g;
  231. struct driver *drv;
  232. ofnode node = dev_ofnode(dev);
  233. struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev);
  234. if (!IS_ENABLED(CONFIG_ATMEL_PIO4))
  235. return 0;
  236. /* Obtain a handle to the GPIO driver */
  237. drv = lists_driver_lookup_name("gpio_atmel_pio4");
  238. if (!drv)
  239. return -ENOENT;
  240. /*
  241. * Bind the GPIO driver to the pinctrl DT node, together
  242. * with its corresponding driver_data.
  243. */
  244. return device_bind_with_driver_data(dev, drv, drv->name,
  245. drv->of_match[priv->gdidx].data,
  246. node, &g);
  247. }
  248. static const struct atmel_pinctrl_data atmel_sama5d2_pinctrl_data = {
  249. .gdidx = 0,
  250. };
  251. static const struct atmel_pinctrl_data microchip_sama7g5_pinctrl_data = {
  252. .slew_rate_support = 1,
  253. .gdidx = 1,
  254. };
  255. static const struct udevice_id atmel_pinctrl_match[] = {
  256. { .compatible = "atmel,sama5d2-pinctrl",
  257. .data = (ulong)&atmel_sama5d2_pinctrl_data, },
  258. { .compatible = "microchip,sama7g5-pinctrl",
  259. .data = (ulong)&microchip_sama7g5_pinctrl_data, },
  260. {}
  261. };
  262. U_BOOT_DRIVER(atmel_pinctrl) = {
  263. .name = "pinctrl_atmel_pio4",
  264. .id = UCLASS_PINCTRL,
  265. .of_match = atmel_pinctrl_match,
  266. .bind = atmel_pinctrl_bind,
  267. .probe = atmel_pinctrl_probe,
  268. .plat_auto = sizeof(struct atmel_pio4_plat),
  269. .ops = &atmel_pinctrl_ops,
  270. };