pinctrl-meson.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/device-internal.h>
  8. #include <dm/lists.h>
  9. #include <dm/pinctrl.h>
  10. #include <fdt_support.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/sizes.h>
  14. #include <asm/gpio.h>
  15. #include "pinctrl-meson.h"
  16. DECLARE_GLOBAL_DATA_PTR;
  17. static const char *meson_pinctrl_dummy_name = "_dummy";
  18. static int meson_pinctrl_get_groups_count(struct udevice *dev)
  19. {
  20. struct meson_pinctrl *priv = dev_get_priv(dev);
  21. return priv->data->num_groups;
  22. }
  23. static const char *meson_pinctrl_get_group_name(struct udevice *dev,
  24. unsigned selector)
  25. {
  26. struct meson_pinctrl *priv = dev_get_priv(dev);
  27. if (!priv->data->groups[selector].name)
  28. return meson_pinctrl_dummy_name;
  29. return priv->data->groups[selector].name;
  30. }
  31. static int meson_pinmux_get_functions_count(struct udevice *dev)
  32. {
  33. struct meson_pinctrl *priv = dev_get_priv(dev);
  34. return priv->data->num_funcs;
  35. }
  36. static const char *meson_pinmux_get_function_name(struct udevice *dev,
  37. unsigned selector)
  38. {
  39. struct meson_pinctrl *priv = dev_get_priv(dev);
  40. return priv->data->funcs[selector].name;
  41. }
  42. static void meson_pinmux_disable_other_groups(struct meson_pinctrl *priv,
  43. unsigned int pin, int sel_group)
  44. {
  45. struct meson_pmx_group *group;
  46. void __iomem *addr;
  47. int i, j;
  48. for (i = 0; i < priv->data->num_groups; i++) {
  49. group = &priv->data->groups[i];
  50. if (group->is_gpio || i == sel_group)
  51. continue;
  52. for (j = 0; j < group->num_pins; j++) {
  53. if (group->pins[j] == pin) {
  54. /* We have found a group using the pin */
  55. debug("pinmux: disabling %s\n", group->name);
  56. addr = priv->reg_mux + group->reg * 4;
  57. writel(readl(addr) & ~BIT(group->bit), addr);
  58. }
  59. }
  60. }
  61. }
  62. static int meson_pinmux_group_set(struct udevice *dev,
  63. unsigned group_selector,
  64. unsigned func_selector)
  65. {
  66. struct meson_pinctrl *priv = dev_get_priv(dev);
  67. const struct meson_pmx_group *group;
  68. const struct meson_pmx_func *func;
  69. void __iomem *addr;
  70. int i;
  71. group = &priv->data->groups[group_selector];
  72. func = &priv->data->funcs[func_selector];
  73. debug("pinmux: set group %s func %s\n", group->name, func->name);
  74. /*
  75. * Disable groups using the same pins.
  76. * The selected group is not disabled to avoid glitches.
  77. */
  78. for (i = 0; i < group->num_pins; i++) {
  79. meson_pinmux_disable_other_groups(priv,
  80. group->pins[i],
  81. group_selector);
  82. }
  83. /* Function 0 (GPIO) doesn't need any additional setting */
  84. if (func_selector) {
  85. addr = priv->reg_mux + group->reg * 4;
  86. writel(readl(addr) | BIT(group->bit), addr);
  87. }
  88. return 0;
  89. }
  90. const struct pinctrl_ops meson_pinctrl_ops = {
  91. .get_groups_count = meson_pinctrl_get_groups_count,
  92. .get_group_name = meson_pinctrl_get_group_name,
  93. .get_functions_count = meson_pinmux_get_functions_count,
  94. .get_function_name = meson_pinmux_get_function_name,
  95. .pinmux_group_set = meson_pinmux_group_set,
  96. .set_state = pinctrl_generic_set_state,
  97. };
  98. static int meson_gpio_calc_reg_and_bit(struct udevice *dev, unsigned int offset,
  99. enum meson_reg_type reg_type,
  100. unsigned int *reg, unsigned int *bit)
  101. {
  102. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  103. struct meson_bank *bank = NULL;
  104. struct meson_reg_desc *desc;
  105. unsigned int pin;
  106. int i;
  107. pin = priv->data->pin_base + offset;
  108. for (i = 0; i < priv->data->num_banks; i++) {
  109. if (pin >= priv->data->banks[i].first &&
  110. pin <= priv->data->banks[i].last) {
  111. bank = &priv->data->banks[i];
  112. break;
  113. }
  114. }
  115. if (!bank)
  116. return -EINVAL;
  117. desc = &bank->regs[reg_type];
  118. *reg = desc->reg * 4;
  119. *bit = desc->bit + pin - bank->first;
  120. return 0;
  121. }
  122. static int meson_gpio_get(struct udevice *dev, unsigned int offset)
  123. {
  124. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  125. unsigned int reg, bit;
  126. int ret;
  127. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_IN, &reg, &bit);
  128. if (ret)
  129. return ret;
  130. return !!(readl(priv->reg_gpio + reg) & BIT(bit));
  131. }
  132. static int meson_gpio_set(struct udevice *dev, unsigned int offset, int value)
  133. {
  134. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  135. unsigned int reg, bit;
  136. int ret;
  137. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_OUT, &reg, &bit);
  138. if (ret)
  139. return ret;
  140. clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
  141. return 0;
  142. }
  143. static int meson_gpio_get_direction(struct udevice *dev, unsigned int offset)
  144. {
  145. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  146. unsigned int reg, bit, val;
  147. int ret;
  148. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, &reg, &bit);
  149. if (ret)
  150. return ret;
  151. val = readl(priv->reg_gpio + reg);
  152. return (val & BIT(bit)) ? GPIOF_INPUT : GPIOF_OUTPUT;
  153. }
  154. static int meson_gpio_direction_input(struct udevice *dev, unsigned int offset)
  155. {
  156. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  157. unsigned int reg, bit;
  158. int ret;
  159. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, &reg, &bit);
  160. if (ret)
  161. return ret;
  162. clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), 1);
  163. return 0;
  164. }
  165. static int meson_gpio_direction_output(struct udevice *dev,
  166. unsigned int offset, int value)
  167. {
  168. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  169. unsigned int reg, bit;
  170. int ret;
  171. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, &reg, &bit);
  172. if (ret)
  173. return ret;
  174. clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), 0);
  175. ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_OUT, &reg, &bit);
  176. if (ret)
  177. return ret;
  178. clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
  179. return 0;
  180. }
  181. static int meson_gpio_probe(struct udevice *dev)
  182. {
  183. struct meson_pinctrl *priv = dev_get_priv(dev->parent);
  184. struct gpio_dev_priv *uc_priv;
  185. uc_priv = dev_get_uclass_priv(dev);
  186. uc_priv->bank_name = priv->data->name;
  187. uc_priv->gpio_count = priv->data->num_pins;
  188. return 0;
  189. }
  190. static const struct dm_gpio_ops meson_gpio_ops = {
  191. .set_value = meson_gpio_set,
  192. .get_value = meson_gpio_get,
  193. .get_function = meson_gpio_get_direction,
  194. .direction_input = meson_gpio_direction_input,
  195. .direction_output = meson_gpio_direction_output,
  196. };
  197. static struct driver meson_gpio_driver = {
  198. .name = "meson-gpio",
  199. .id = UCLASS_GPIO,
  200. .probe = meson_gpio_probe,
  201. .ops = &meson_gpio_ops,
  202. };
  203. static fdt_addr_t parse_address(int offset, const char *name, int na, int ns)
  204. {
  205. int index, len = 0;
  206. const fdt32_t *reg;
  207. index = fdt_stringlist_search(gd->fdt_blob, offset, "reg-names", name);
  208. if (index < 0)
  209. return FDT_ADDR_T_NONE;
  210. reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
  211. if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns))))
  212. return FDT_ADDR_T_NONE;
  213. reg += index * (na + ns);
  214. return fdt_translate_address((void *)gd->fdt_blob, offset, reg);
  215. }
  216. int meson_pinctrl_probe(struct udevice *dev)
  217. {
  218. struct meson_pinctrl *priv = dev_get_priv(dev);
  219. struct uclass_driver *drv;
  220. struct udevice *gpio_dev;
  221. fdt_addr_t addr;
  222. int node, gpio = -1, len;
  223. int na, ns;
  224. char *name;
  225. na = fdt_address_cells(gd->fdt_blob, dev_of_offset(dev->parent));
  226. if (na < 1) {
  227. debug("bad #address-cells\n");
  228. return -EINVAL;
  229. }
  230. ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
  231. if (ns < 1) {
  232. debug("bad #size-cells\n");
  233. return -EINVAL;
  234. }
  235. fdt_for_each_subnode(node, gd->fdt_blob, dev_of_offset(dev)) {
  236. if (fdt_getprop(gd->fdt_blob, node, "gpio-controller", &len)) {
  237. gpio = node;
  238. break;
  239. }
  240. }
  241. if (!gpio) {
  242. debug("gpio node not found\n");
  243. return -EINVAL;
  244. }
  245. addr = parse_address(gpio, "mux", na, ns);
  246. if (addr == FDT_ADDR_T_NONE) {
  247. debug("mux address not found\n");
  248. return -EINVAL;
  249. }
  250. priv->reg_mux = (void __iomem *)addr;
  251. addr = parse_address(gpio, "gpio", na, ns);
  252. if (addr == FDT_ADDR_T_NONE) {
  253. debug("gpio address not found\n");
  254. return -EINVAL;
  255. }
  256. priv->reg_gpio = (void __iomem *)addr;
  257. priv->data = (struct meson_pinctrl_data *)dev_get_driver_data(dev);
  258. /* Lookup GPIO driver */
  259. drv = lists_uclass_lookup(UCLASS_GPIO);
  260. if (!drv) {
  261. puts("Cannot find GPIO driver\n");
  262. return -ENOENT;
  263. }
  264. name = calloc(1, 32);
  265. sprintf(name, "meson-gpio");
  266. /* Create child device UCLASS_GPIO and bind it */
  267. device_bind(dev, &meson_gpio_driver, name, NULL, gpio, &gpio_dev);
  268. dev_set_of_offset(gpio_dev, gpio);
  269. return 0;
  270. }