pinctrl-ralink.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 John Crispin <[email protected]>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/device.h>
  7. #include <linux/io.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/pinctrl/pinctrl.h>
  12. #include <linux/pinctrl/pinconf.h>
  13. #include <linux/pinctrl/pinconf-generic.h>
  14. #include <linux/pinctrl/pinmux.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include <linux/pinctrl/machine.h>
  17. #include <asm/mach-ralink/ralink_regs.h>
  18. #include <asm/mach-ralink/mt7620.h>
  19. #include "pinctrl-ralink.h"
  20. #include "../core.h"
  21. #include "../pinctrl-utils.h"
  22. #define SYSC_REG_GPIO_MODE 0x60
  23. #define SYSC_REG_GPIO_MODE2 0x64
  24. struct ralink_priv {
  25. struct device *dev;
  26. struct pinctrl_pin_desc *pads;
  27. struct pinctrl_desc *desc;
  28. struct ralink_pmx_func **func;
  29. int func_count;
  30. struct ralink_pmx_group *groups;
  31. const char **group_names;
  32. int group_count;
  33. u8 *gpio;
  34. int max_pins;
  35. };
  36. static int ralink_get_group_count(struct pinctrl_dev *pctrldev)
  37. {
  38. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  39. return p->group_count;
  40. }
  41. static const char *ralink_get_group_name(struct pinctrl_dev *pctrldev,
  42. unsigned int group)
  43. {
  44. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  45. return (group >= p->group_count) ? NULL : p->group_names[group];
  46. }
  47. static int ralink_get_group_pins(struct pinctrl_dev *pctrldev,
  48. unsigned int group,
  49. const unsigned int **pins,
  50. unsigned int *num_pins)
  51. {
  52. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  53. if (group >= p->group_count)
  54. return -EINVAL;
  55. *pins = p->groups[group].func[0].pins;
  56. *num_pins = p->groups[group].func[0].pin_count;
  57. return 0;
  58. }
  59. static const struct pinctrl_ops ralink_pctrl_ops = {
  60. .get_groups_count = ralink_get_group_count,
  61. .get_group_name = ralink_get_group_name,
  62. .get_group_pins = ralink_get_group_pins,
  63. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  64. .dt_free_map = pinconf_generic_dt_free_map,
  65. };
  66. static int ralink_pmx_func_count(struct pinctrl_dev *pctrldev)
  67. {
  68. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  69. return p->func_count;
  70. }
  71. static const char *ralink_pmx_func_name(struct pinctrl_dev *pctrldev,
  72. unsigned int func)
  73. {
  74. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  75. return p->func[func]->name;
  76. }
  77. static int ralink_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
  78. unsigned int func,
  79. const char * const **groups,
  80. unsigned int * const num_groups)
  81. {
  82. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  83. if (p->func[func]->group_count == 1)
  84. *groups = &p->group_names[p->func[func]->groups[0]];
  85. else
  86. *groups = p->group_names;
  87. *num_groups = p->func[func]->group_count;
  88. return 0;
  89. }
  90. static int ralink_pmx_group_enable(struct pinctrl_dev *pctrldev,
  91. unsigned int func, unsigned int group)
  92. {
  93. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  94. u32 mode = 0;
  95. u32 reg = SYSC_REG_GPIO_MODE;
  96. int i;
  97. int shift;
  98. /* dont allow double use */
  99. if (p->groups[group].enabled) {
  100. dev_err(p->dev, "%s is already enabled\n",
  101. p->groups[group].name);
  102. return 0;
  103. }
  104. p->groups[group].enabled = 1;
  105. p->func[func]->enabled = 1;
  106. shift = p->groups[group].shift;
  107. if (shift >= 32) {
  108. shift -= 32;
  109. reg = SYSC_REG_GPIO_MODE2;
  110. }
  111. mode = rt_sysc_r32(reg);
  112. mode &= ~(p->groups[group].mask << shift);
  113. /* mark the pins as gpio */
  114. for (i = 0; i < p->groups[group].func[0].pin_count; i++)
  115. p->gpio[p->groups[group].func[0].pins[i]] = 1;
  116. /* function 0 is gpio and needs special handling */
  117. if (func == 0) {
  118. mode |= p->groups[group].gpio << shift;
  119. } else {
  120. for (i = 0; i < p->func[func]->pin_count; i++)
  121. p->gpio[p->func[func]->pins[i]] = 0;
  122. mode |= p->func[func]->value << shift;
  123. }
  124. rt_sysc_w32(mode, reg);
  125. return 0;
  126. }
  127. static int ralink_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
  128. struct pinctrl_gpio_range *range,
  129. unsigned int pin)
  130. {
  131. struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
  132. if (!p->gpio[pin]) {
  133. dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static const struct pinmux_ops ralink_pmx_group_ops = {
  139. .get_functions_count = ralink_pmx_func_count,
  140. .get_function_name = ralink_pmx_func_name,
  141. .get_function_groups = ralink_pmx_group_get_groups,
  142. .set_mux = ralink_pmx_group_enable,
  143. .gpio_request_enable = ralink_pmx_group_gpio_request_enable,
  144. };
  145. static struct pinctrl_desc ralink_pctrl_desc = {
  146. .owner = THIS_MODULE,
  147. .name = "ralink-pinctrl",
  148. .pctlops = &ralink_pctrl_ops,
  149. .pmxops = &ralink_pmx_group_ops,
  150. };
  151. static struct ralink_pmx_func gpio_func = {
  152. .name = "gpio",
  153. };
  154. static int ralink_pinctrl_index(struct ralink_priv *p)
  155. {
  156. struct ralink_pmx_group *mux = p->groups;
  157. int i, j, c = 0;
  158. /* count the mux functions */
  159. while (mux->name) {
  160. p->group_count++;
  161. mux++;
  162. }
  163. /* allocate the group names array needed by the gpio function */
  164. p->group_names = devm_kcalloc(p->dev, p->group_count,
  165. sizeof(char *), GFP_KERNEL);
  166. if (!p->group_names)
  167. return -ENOMEM;
  168. for (i = 0; i < p->group_count; i++) {
  169. p->group_names[i] = p->groups[i].name;
  170. p->func_count += p->groups[i].func_count;
  171. }
  172. /* we have a dummy function[0] for gpio */
  173. p->func_count++;
  174. /* allocate our function and group mapping index buffers */
  175. p->func = devm_kcalloc(p->dev, p->func_count,
  176. sizeof(*p->func), GFP_KERNEL);
  177. gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
  178. GFP_KERNEL);
  179. if (!p->func || !gpio_func.groups)
  180. return -ENOMEM;
  181. /* add a backpointer to the function so it knows its group */
  182. gpio_func.group_count = p->group_count;
  183. for (i = 0; i < gpio_func.group_count; i++)
  184. gpio_func.groups[i] = i;
  185. p->func[c] = &gpio_func;
  186. c++;
  187. /* add remaining functions */
  188. for (i = 0; i < p->group_count; i++) {
  189. for (j = 0; j < p->groups[i].func_count; j++) {
  190. p->func[c] = &p->groups[i].func[j];
  191. p->func[c]->groups = devm_kzalloc(p->dev, sizeof(int),
  192. GFP_KERNEL);
  193. if (!p->func[c]->groups)
  194. return -ENOMEM;
  195. p->func[c]->groups[0] = i;
  196. p->func[c]->group_count = 1;
  197. c++;
  198. }
  199. }
  200. return 0;
  201. }
  202. static int ralink_pinctrl_pins(struct ralink_priv *p)
  203. {
  204. int i, j;
  205. /*
  206. * loop over the functions and initialize the pins array.
  207. * also work out the highest pin used.
  208. */
  209. for (i = 0; i < p->func_count; i++) {
  210. int pin;
  211. if (!p->func[i]->pin_count)
  212. continue;
  213. p->func[i]->pins = devm_kcalloc(p->dev,
  214. p->func[i]->pin_count,
  215. sizeof(int),
  216. GFP_KERNEL);
  217. if (!p->func[i]->pins)
  218. return -ENOMEM;
  219. for (j = 0; j < p->func[i]->pin_count; j++)
  220. p->func[i]->pins[j] = p->func[i]->pin_first + j;
  221. pin = p->func[i]->pin_first + p->func[i]->pin_count;
  222. if (pin > p->max_pins)
  223. p->max_pins = pin;
  224. }
  225. /* the buffer that tells us which pins are gpio */
  226. p->gpio = devm_kcalloc(p->dev, p->max_pins, sizeof(u8), GFP_KERNEL);
  227. /* the pads needed to tell pinctrl about our pins */
  228. p->pads = devm_kcalloc(p->dev, p->max_pins,
  229. sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
  230. if (!p->pads || !p->gpio)
  231. return -ENOMEM;
  232. memset(p->gpio, 1, sizeof(u8) * p->max_pins);
  233. for (i = 0; i < p->func_count; i++) {
  234. if (!p->func[i]->pin_count)
  235. continue;
  236. for (j = 0; j < p->func[i]->pin_count; j++)
  237. p->gpio[p->func[i]->pins[j]] = 0;
  238. }
  239. /* pin 0 is always a gpio */
  240. p->gpio[0] = 1;
  241. /* set the pads */
  242. for (i = 0; i < p->max_pins; i++) {
  243. /* strlen("ioXY") + 1 = 5 */
  244. char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
  245. if (!name)
  246. return -ENOMEM;
  247. snprintf(name, 5, "io%d", i);
  248. p->pads[i].number = i;
  249. p->pads[i].name = name;
  250. }
  251. p->desc->pins = p->pads;
  252. p->desc->npins = p->max_pins;
  253. return 0;
  254. }
  255. int ralink_pinctrl_init(struct platform_device *pdev,
  256. struct ralink_pmx_group *data)
  257. {
  258. struct ralink_priv *p;
  259. struct pinctrl_dev *dev;
  260. int err;
  261. if (!data)
  262. return -ENOTSUPP;
  263. /* setup the private data */
  264. p = devm_kzalloc(&pdev->dev, sizeof(struct ralink_priv), GFP_KERNEL);
  265. if (!p)
  266. return -ENOMEM;
  267. p->dev = &pdev->dev;
  268. p->desc = &ralink_pctrl_desc;
  269. p->groups = data;
  270. platform_set_drvdata(pdev, p);
  271. /* init the device */
  272. err = ralink_pinctrl_index(p);
  273. if (err) {
  274. dev_err(&pdev->dev, "failed to load index\n");
  275. return err;
  276. }
  277. err = ralink_pinctrl_pins(p);
  278. if (err) {
  279. dev_err(&pdev->dev, "failed to load pins\n");
  280. return err;
  281. }
  282. dev = pinctrl_register(p->desc, &pdev->dev, p);
  283. return PTR_ERR_OR_ZERO(dev);
  284. }