pinctrl-sunxi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * Allwinner A1X SoCs pinctrl driver.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/pinctrl/machine.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinconf-generic.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include "core.h"
  25. #include "pinctrl-sunxi.h"
  26. static const struct sunxi_desc_pin sun5i_a13_pins[] = {
  27. /* Hole */
  28. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB0,
  29. SUNXI_FUNCTION(0x0, "gpio_in"),
  30. SUNXI_FUNCTION(0x1, "gpio_out")),
  31. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB1,
  32. SUNXI_FUNCTION(0x0, "gpio_in"),
  33. SUNXI_FUNCTION(0x1, "gpio_out")),
  34. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB2,
  35. SUNXI_FUNCTION(0x0, "gpio_in"),
  36. SUNXI_FUNCTION(0x1, "gpio_out")),
  37. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB3,
  38. SUNXI_FUNCTION(0x0, "gpio_in"),
  39. SUNXI_FUNCTION(0x1, "gpio_out")),
  40. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB4,
  41. SUNXI_FUNCTION(0x0, "gpio_in"),
  42. SUNXI_FUNCTION(0x1, "gpio_out")),
  43. /* Hole */
  44. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB10,
  45. SUNXI_FUNCTION(0x0, "gpio_in"),
  46. SUNXI_FUNCTION(0x1, "gpio_out")),
  47. /* Hole */
  48. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB15,
  49. SUNXI_FUNCTION(0x0, "gpio_in"),
  50. SUNXI_FUNCTION(0x1, "gpio_out")),
  51. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB16,
  52. SUNXI_FUNCTION(0x0, "gpio_in"),
  53. SUNXI_FUNCTION(0x1, "gpio_out")),
  54. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB17,
  55. SUNXI_FUNCTION(0x0, "gpio_in"),
  56. SUNXI_FUNCTION(0x1, "gpio_out")),
  57. SUNXI_PIN(SUNXI_PINCTRL_PIN_PB18,
  58. SUNXI_FUNCTION(0x0, "gpio_in"),
  59. SUNXI_FUNCTION(0x1, "gpio_out")),
  60. /* Hole */
  61. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC0,
  62. SUNXI_FUNCTION(0x0, "gpio_in"),
  63. SUNXI_FUNCTION(0x1, "gpio_out")),
  64. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC1,
  65. SUNXI_FUNCTION(0x0, "gpio_in"),
  66. SUNXI_FUNCTION(0x1, "gpio_out")),
  67. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC2,
  68. SUNXI_FUNCTION(0x0, "gpio_in"),
  69. SUNXI_FUNCTION(0x1, "gpio_out")),
  70. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC3,
  71. SUNXI_FUNCTION(0x0, "gpio_in"),
  72. SUNXI_FUNCTION(0x1, "gpio_out")),
  73. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC4,
  74. SUNXI_FUNCTION(0x0, "gpio_in"),
  75. SUNXI_FUNCTION(0x1, "gpio_out")),
  76. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC5,
  77. SUNXI_FUNCTION(0x0, "gpio_in"),
  78. SUNXI_FUNCTION(0x1, "gpio_out")),
  79. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC6,
  80. SUNXI_FUNCTION(0x0, "gpio_in"),
  81. SUNXI_FUNCTION(0x1, "gpio_out")),
  82. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC7,
  83. SUNXI_FUNCTION(0x0, "gpio_in"),
  84. SUNXI_FUNCTION(0x1, "gpio_out")),
  85. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC8,
  86. SUNXI_FUNCTION(0x0, "gpio_in"),
  87. SUNXI_FUNCTION(0x1, "gpio_out")),
  88. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC9,
  89. SUNXI_FUNCTION(0x0, "gpio_in"),
  90. SUNXI_FUNCTION(0x1, "gpio_out")),
  91. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC10,
  92. SUNXI_FUNCTION(0x0, "gpio_in"),
  93. SUNXI_FUNCTION(0x1, "gpio_out")),
  94. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC11,
  95. SUNXI_FUNCTION(0x0, "gpio_in"),
  96. SUNXI_FUNCTION(0x1, "gpio_out")),
  97. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC12,
  98. SUNXI_FUNCTION(0x0, "gpio_in"),
  99. SUNXI_FUNCTION(0x1, "gpio_out")),
  100. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC13,
  101. SUNXI_FUNCTION(0x0, "gpio_in"),
  102. SUNXI_FUNCTION(0x1, "gpio_out")),
  103. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC14,
  104. SUNXI_FUNCTION(0x0, "gpio_in"),
  105. SUNXI_FUNCTION(0x1, "gpio_out")),
  106. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC15,
  107. SUNXI_FUNCTION(0x0, "gpio_in"),
  108. SUNXI_FUNCTION(0x1, "gpio_out")),
  109. /* Hole */
  110. SUNXI_PIN(SUNXI_PINCTRL_PIN_PC19,
  111. SUNXI_FUNCTION(0x0, "gpio_in"),
  112. SUNXI_FUNCTION(0x1, "gpio_out")),
  113. /* Hole */
  114. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD2,
  115. SUNXI_FUNCTION(0x0, "gpio_in"),
  116. SUNXI_FUNCTION(0x1, "gpio_out")),
  117. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD3,
  118. SUNXI_FUNCTION(0x0, "gpio_in"),
  119. SUNXI_FUNCTION(0x1, "gpio_out")),
  120. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD4,
  121. SUNXI_FUNCTION(0x0, "gpio_in"),
  122. SUNXI_FUNCTION(0x1, "gpio_out")),
  123. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD5,
  124. SUNXI_FUNCTION(0x0, "gpio_in"),
  125. SUNXI_FUNCTION(0x1, "gpio_out")),
  126. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD6,
  127. SUNXI_FUNCTION(0x0, "gpio_in"),
  128. SUNXI_FUNCTION(0x1, "gpio_out")),
  129. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD7,
  130. SUNXI_FUNCTION(0x0, "gpio_in"),
  131. SUNXI_FUNCTION(0x1, "gpio_out")),
  132. /* Hole */
  133. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD10,
  134. SUNXI_FUNCTION(0x0, "gpio_in"),
  135. SUNXI_FUNCTION(0x1, "gpio_out")),
  136. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD11,
  137. SUNXI_FUNCTION(0x0, "gpio_in"),
  138. SUNXI_FUNCTION(0x1, "gpio_out")),
  139. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD12,
  140. SUNXI_FUNCTION(0x0, "gpio_in"),
  141. SUNXI_FUNCTION(0x1, "gpio_out")),
  142. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD13,
  143. SUNXI_FUNCTION(0x0, "gpio_in"),
  144. SUNXI_FUNCTION(0x1, "gpio_out")),
  145. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD14,
  146. SUNXI_FUNCTION(0x0, "gpio_in"),
  147. SUNXI_FUNCTION(0x1, "gpio_out")),
  148. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD15,
  149. SUNXI_FUNCTION(0x0, "gpio_in"),
  150. SUNXI_FUNCTION(0x1, "gpio_out")),
  151. /* Hole */
  152. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD18,
  153. SUNXI_FUNCTION(0x0, "gpio_in"),
  154. SUNXI_FUNCTION(0x1, "gpio_out")),
  155. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD19,
  156. SUNXI_FUNCTION(0x0, "gpio_in"),
  157. SUNXI_FUNCTION(0x1, "gpio_out")),
  158. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD20,
  159. SUNXI_FUNCTION(0x0, "gpio_in"),
  160. SUNXI_FUNCTION(0x1, "gpio_out")),
  161. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD21,
  162. SUNXI_FUNCTION(0x0, "gpio_in"),
  163. SUNXI_FUNCTION(0x1, "gpio_out")),
  164. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD22,
  165. SUNXI_FUNCTION(0x0, "gpio_in"),
  166. SUNXI_FUNCTION(0x1, "gpio_out")),
  167. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD23,
  168. SUNXI_FUNCTION(0x0, "gpio_in"),
  169. SUNXI_FUNCTION(0x1, "gpio_out")),
  170. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD24,
  171. SUNXI_FUNCTION(0x0, "gpio_in"),
  172. SUNXI_FUNCTION(0x1, "gpio_out")),
  173. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD25,
  174. SUNXI_FUNCTION(0x0, "gpio_in"),
  175. SUNXI_FUNCTION(0x1, "gpio_out")),
  176. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD26,
  177. SUNXI_FUNCTION(0x0, "gpio_in"),
  178. SUNXI_FUNCTION(0x1, "gpio_out")),
  179. SUNXI_PIN(SUNXI_PINCTRL_PIN_PD27,
  180. SUNXI_FUNCTION(0x0, "gpio_in"),
  181. SUNXI_FUNCTION(0x1, "gpio_out")),
  182. /* Hole */
  183. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE0,
  184. SUNXI_FUNCTION(0x0, "gpio_in"),
  185. SUNXI_FUNCTION(0x1, "gpio_out")),
  186. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE1,
  187. SUNXI_FUNCTION(0x0, "gpio_in"),
  188. SUNXI_FUNCTION(0x1, "gpio_out")),
  189. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE2,
  190. SUNXI_FUNCTION(0x0, "gpio_in"),
  191. SUNXI_FUNCTION(0x1, "gpio_out")),
  192. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE3,
  193. SUNXI_FUNCTION(0x0, "gpio_in"),
  194. SUNXI_FUNCTION(0x1, "gpio_out")),
  195. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE4,
  196. SUNXI_FUNCTION(0x0, "gpio_in"),
  197. SUNXI_FUNCTION(0x1, "gpio_out")),
  198. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE5,
  199. SUNXI_FUNCTION(0x0, "gpio_in"),
  200. SUNXI_FUNCTION(0x1, "gpio_out")),
  201. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE6,
  202. SUNXI_FUNCTION(0x0, "gpio_in"),
  203. SUNXI_FUNCTION(0x1, "gpio_out")),
  204. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE7,
  205. SUNXI_FUNCTION(0x0, "gpio_in"),
  206. SUNXI_FUNCTION(0x1, "gpio_out")),
  207. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE8,
  208. SUNXI_FUNCTION(0x0, "gpio_in"),
  209. SUNXI_FUNCTION(0x1, "gpio_out")),
  210. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE9,
  211. SUNXI_FUNCTION(0x0, "gpio_in"),
  212. SUNXI_FUNCTION(0x1, "gpio_out")),
  213. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE10,
  214. SUNXI_FUNCTION(0x0, "gpio_in"),
  215. SUNXI_FUNCTION(0x1, "gpio_out"),
  216. SUNXI_FUNCTION(0x4, "uart1")),
  217. SUNXI_PIN(SUNXI_PINCTRL_PIN_PE11,
  218. SUNXI_FUNCTION(0x0, "gpio_in"),
  219. SUNXI_FUNCTION(0x1, "gpio_out"),
  220. SUNXI_FUNCTION(0x4, "uart1")),
  221. /* Hole */
  222. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF0,
  223. SUNXI_FUNCTION(0x0, "gpio_in"),
  224. SUNXI_FUNCTION(0x1, "gpio_out")),
  225. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF1,
  226. SUNXI_FUNCTION(0x0, "gpio_in"),
  227. SUNXI_FUNCTION(0x1, "gpio_out")),
  228. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF2,
  229. SUNXI_FUNCTION(0x0, "gpio_in"),
  230. SUNXI_FUNCTION(0x1, "gpio_out")),
  231. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF3,
  232. SUNXI_FUNCTION(0x0, "gpio_in"),
  233. SUNXI_FUNCTION(0x1, "gpio_out")),
  234. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF4,
  235. SUNXI_FUNCTION(0x0, "gpio_in"),
  236. SUNXI_FUNCTION(0x1, "gpio_out")),
  237. SUNXI_PIN(SUNXI_PINCTRL_PIN_PF5,
  238. SUNXI_FUNCTION(0x0, "gpio_in"),
  239. SUNXI_FUNCTION(0x1, "gpio_out")),
  240. /* Hole */
  241. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG0,
  242. SUNXI_FUNCTION(0x0, "gpio_in"),
  243. SUNXI_FUNCTION(0x1, "gpio_out")),
  244. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG1,
  245. SUNXI_FUNCTION(0x0, "gpio_in"),
  246. SUNXI_FUNCTION(0x1, "gpio_out")),
  247. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG2,
  248. SUNXI_FUNCTION(0x0, "gpio_in"),
  249. SUNXI_FUNCTION(0x1, "gpio_out")),
  250. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG3,
  251. SUNXI_FUNCTION(0x0, "gpio_in"),
  252. SUNXI_FUNCTION(0x1, "gpio_out"),
  253. SUNXI_FUNCTION(0x4, "uart1")),
  254. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG4,
  255. SUNXI_FUNCTION(0x0, "gpio_in"),
  256. SUNXI_FUNCTION(0x1, "gpio_out"),
  257. SUNXI_FUNCTION(0x4, "uart1")),
  258. /* Hole */
  259. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG9,
  260. SUNXI_FUNCTION(0x0, "gpio_in"),
  261. SUNXI_FUNCTION(0x1, "gpio_out")),
  262. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG10,
  263. SUNXI_FUNCTION(0x0, "gpio_in"),
  264. SUNXI_FUNCTION(0x1, "gpio_out")),
  265. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG11,
  266. SUNXI_FUNCTION(0x0, "gpio_in"),
  267. SUNXI_FUNCTION(0x1, "gpio_out")),
  268. SUNXI_PIN(SUNXI_PINCTRL_PIN_PG12,
  269. SUNXI_FUNCTION(0x0, "gpio_in"),
  270. SUNXI_FUNCTION(0x1, "gpio_out")),
  271. };
  272. static const struct sunxi_pinctrl_desc sun5i_a13_pinctrl_data = {
  273. .pins = sun5i_a13_pins,
  274. .npins = ARRAY_SIZE(sun5i_a13_pins),
  275. };
  276. static struct sunxi_pinctrl_group *
  277. sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
  278. {
  279. int i;
  280. for (i = 0; i < pctl->ngroups; i++) {
  281. struct sunxi_pinctrl_group *grp = pctl->groups + i;
  282. if (!strcmp(grp->name, group))
  283. return grp;
  284. }
  285. return NULL;
  286. }
  287. static struct sunxi_pinctrl_function *
  288. sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
  289. const char *name)
  290. {
  291. struct sunxi_pinctrl_function *func = pctl->functions;
  292. int i;
  293. for (i = 0; i < pctl->nfunctions; i++) {
  294. if (!func[i].name)
  295. break;
  296. if (!strcmp(func[i].name, name))
  297. return func + i;
  298. }
  299. return NULL;
  300. }
  301. static struct sunxi_desc_function *
  302. sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
  303. const char *pin_name,
  304. const char *func_name)
  305. {
  306. int i;
  307. for (i = 0; i < pctl->desc->npins; i++) {
  308. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  309. if (!strcmp(pin->pin.name, pin_name)) {
  310. struct sunxi_desc_function *func = pin->functions;
  311. while (func->name) {
  312. if (!strcmp(func->name, func_name))
  313. return func;
  314. func++;
  315. }
  316. }
  317. }
  318. return NULL;
  319. }
  320. static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  321. {
  322. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  323. return pctl->ngroups;
  324. }
  325. static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  326. unsigned group)
  327. {
  328. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  329. return pctl->groups[group].name;
  330. }
  331. static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  332. unsigned group,
  333. const unsigned **pins,
  334. unsigned *num_pins)
  335. {
  336. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  337. *pins = (unsigned *)&pctl->groups[group].pin;
  338. *num_pins = 1;
  339. return 0;
  340. }
  341. static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  342. struct device_node *node,
  343. struct pinctrl_map **map,
  344. unsigned *num_maps)
  345. {
  346. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  347. unsigned long *pinconfig;
  348. struct property *prop;
  349. const char *function;
  350. const char *group;
  351. int ret, nmaps, i = 0;
  352. u32 val;
  353. *map = NULL;
  354. *num_maps = 0;
  355. ret = of_property_read_string(node, "allwinner,function", &function);
  356. if (ret) {
  357. dev_err(pctl->dev,
  358. "missing allwinner,function property in node %s\n",
  359. node->name);
  360. return -EINVAL;
  361. }
  362. nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
  363. if (nmaps < 0) {
  364. dev_err(pctl->dev,
  365. "missing allwinner,pins property in node %s\n",
  366. node->name);
  367. return -EINVAL;
  368. }
  369. *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
  370. if (!map)
  371. return -ENOMEM;
  372. of_property_for_each_string(node, "allwinner,pins", prop, group) {
  373. struct sunxi_pinctrl_group *grp =
  374. sunxi_pinctrl_find_group_by_name(pctl, group);
  375. int j = 0, configlen = 0;
  376. if (!grp) {
  377. dev_err(pctl->dev, "unknown pin %s", group);
  378. continue;
  379. }
  380. if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
  381. grp->name,
  382. function)) {
  383. dev_err(pctl->dev, "unsupported function %s on pin %s",
  384. function, group);
  385. continue;
  386. }
  387. (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
  388. (*map)[i].data.mux.group = group;
  389. (*map)[i].data.mux.function = function;
  390. i++;
  391. (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  392. (*map)[i].data.configs.group_or_pin = group;
  393. if (of_find_property(node, "allwinner,drive", NULL))
  394. configlen++;
  395. if (of_find_property(node, "allwinner,pull", NULL))
  396. configlen++;
  397. pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
  398. if (!of_property_read_u32(node, "allwinner,drive", &val)) {
  399. u16 strength = (val + 1) * 10;
  400. pinconfig[j++] =
  401. pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
  402. strength);
  403. }
  404. if (!of_property_read_u32(node, "allwinner,pull", &val)) {
  405. enum pin_config_param pull = PIN_CONFIG_END;
  406. if (val == 1)
  407. pull = PIN_CONFIG_BIAS_PULL_UP;
  408. else if (val == 2)
  409. pull = PIN_CONFIG_BIAS_PULL_DOWN;
  410. pinconfig[j++] = pinconf_to_config_packed(pull, 0);
  411. }
  412. (*map)[i].data.configs.configs = pinconfig;
  413. (*map)[i].data.configs.num_configs = configlen;
  414. i++;
  415. }
  416. *num_maps = nmaps;
  417. return 0;
  418. }
  419. static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
  420. struct pinctrl_map *map,
  421. unsigned num_maps)
  422. {
  423. int i;
  424. for (i = 0; i < num_maps; i++) {
  425. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  426. kfree(map[i].data.configs.configs);
  427. }
  428. kfree(map);
  429. }
  430. static struct pinctrl_ops sunxi_pctrl_ops = {
  431. .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
  432. .dt_free_map = sunxi_pctrl_dt_free_map,
  433. .get_groups_count = sunxi_pctrl_get_groups_count,
  434. .get_group_name = sunxi_pctrl_get_group_name,
  435. .get_group_pins = sunxi_pctrl_get_group_pins,
  436. };
  437. static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
  438. unsigned group,
  439. unsigned long *config)
  440. {
  441. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  442. *config = pctl->groups[group].config;
  443. return 0;
  444. }
  445. static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
  446. unsigned group,
  447. unsigned long config)
  448. {
  449. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  450. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  451. u32 val, mask;
  452. u16 strength;
  453. u8 dlevel;
  454. switch (pinconf_to_config_param(config)) {
  455. case PIN_CONFIG_DRIVE_STRENGTH:
  456. strength = pinconf_to_config_argument(config);
  457. if (strength > 40)
  458. return -EINVAL;
  459. /*
  460. * We convert from mA to what the register expects:
  461. * 0: 10mA
  462. * 1: 20mA
  463. * 2: 30mA
  464. * 3: 40mA
  465. */
  466. dlevel = strength / 10 - 1;
  467. val = readl(pctl->membase + sunxi_dlevel_reg(g->pin));
  468. mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(g->pin);
  469. writel((val & ~mask) | dlevel << sunxi_dlevel_offset(g->pin),
  470. pctl->membase + sunxi_dlevel_reg(g->pin));
  471. break;
  472. case PIN_CONFIG_BIAS_PULL_UP:
  473. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  474. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  475. writel((val & ~mask) | 1 << sunxi_pull_offset(g->pin),
  476. pctl->membase + sunxi_pull_reg(g->pin));
  477. break;
  478. case PIN_CONFIG_BIAS_PULL_DOWN:
  479. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  480. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  481. writel((val & ~mask) | 2 << sunxi_pull_offset(g->pin),
  482. pctl->membase + sunxi_pull_reg(g->pin));
  483. break;
  484. default:
  485. break;
  486. }
  487. /* cache the config value */
  488. g->config = config;
  489. return 0;
  490. }
  491. static struct pinconf_ops sunxi_pconf_ops = {
  492. .pin_config_group_get = sunxi_pconf_group_get,
  493. .pin_config_group_set = sunxi_pconf_group_set,
  494. };
  495. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  496. {
  497. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  498. return pctl->nfunctions;
  499. }
  500. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  501. unsigned function)
  502. {
  503. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  504. return pctl->functions[function].name;
  505. }
  506. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  507. unsigned function,
  508. const char * const **groups,
  509. unsigned * const num_groups)
  510. {
  511. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  512. *groups = pctl->functions[function].groups;
  513. *num_groups = pctl->functions[function].ngroups;
  514. return 0;
  515. }
  516. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  517. unsigned pin,
  518. u8 config)
  519. {
  520. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  521. u32 val = readl(pctl->membase + sunxi_mux_reg(pin));
  522. u32 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  523. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  524. pctl->membase + sunxi_mux_reg(pin));
  525. }
  526. static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
  527. unsigned function,
  528. unsigned group)
  529. {
  530. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  531. struct sunxi_pinctrl_group *g = pctl->groups + group;
  532. struct sunxi_pinctrl_function *func = pctl->functions + function;
  533. struct sunxi_desc_function *desc =
  534. sunxi_pinctrl_desc_find_function_by_name(pctl,
  535. g->name,
  536. func->name);
  537. if (!desc)
  538. return -EINVAL;
  539. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  540. return 0;
  541. }
  542. static struct pinmux_ops sunxi_pmx_ops = {
  543. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  544. .get_function_name = sunxi_pmx_get_func_name,
  545. .get_function_groups = sunxi_pmx_get_func_groups,
  546. .enable = sunxi_pmx_enable,
  547. };
  548. static struct pinctrl_desc sunxi_pctrl_desc = {
  549. .confops = &sunxi_pconf_ops,
  550. .pctlops = &sunxi_pctrl_ops,
  551. .pmxops = &sunxi_pmx_ops,
  552. };
  553. static struct of_device_id sunxi_pinctrl_match[] = {
  554. { .compatible = "allwinner,sun5i-a13-pinctrl", .data = (void *)&sun5i_a13_pinctrl_data },
  555. {}
  556. };
  557. MODULE_DEVICE_TABLE(of, sunxi_pinctrl_match);
  558. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  559. const char *name)
  560. {
  561. struct sunxi_pinctrl_function *func = pctl->functions;
  562. while (func->name) {
  563. /* function already there */
  564. if (strcmp(func->name, name) == 0) {
  565. func->ngroups++;
  566. return -EEXIST;
  567. }
  568. func++;
  569. }
  570. func->name = name;
  571. func->ngroups = 1;
  572. pctl->nfunctions++;
  573. return 0;
  574. }
  575. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  576. {
  577. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  578. int i;
  579. pctl->ngroups = pctl->desc->npins;
  580. /* Allocate groups */
  581. pctl->groups = devm_kzalloc(&pdev->dev,
  582. pctl->ngroups * sizeof(*pctl->groups),
  583. GFP_KERNEL);
  584. if (!pctl->groups)
  585. return -ENOMEM;
  586. for (i = 0; i < pctl->desc->npins; i++) {
  587. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  588. struct sunxi_pinctrl_group *group = pctl->groups + i;
  589. group->name = pin->pin.name;
  590. group->pin = pin->pin.number;
  591. }
  592. /*
  593. * We suppose that we won't have any more functions than pins,
  594. * we'll reallocate that later anyway
  595. */
  596. pctl->functions = devm_kzalloc(&pdev->dev,
  597. pctl->desc->npins * sizeof(*pctl->functions),
  598. GFP_KERNEL);
  599. if (!pctl->functions)
  600. return -ENOMEM;
  601. /* Count functions and their associated groups */
  602. for (i = 0; i < pctl->desc->npins; i++) {
  603. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  604. struct sunxi_desc_function *func = pin->functions;
  605. while (func->name) {
  606. sunxi_pinctrl_add_function(pctl, func->name);
  607. func++;
  608. }
  609. }
  610. pctl->functions = krealloc(pctl->functions,
  611. pctl->nfunctions * sizeof(*pctl->functions),
  612. GFP_KERNEL);
  613. for (i = 0; i < pctl->desc->npins; i++) {
  614. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  615. struct sunxi_desc_function *func = pin->functions;
  616. while (func->name) {
  617. struct sunxi_pinctrl_function *func_item;
  618. const char **func_grp;
  619. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  620. func->name);
  621. if (!func_item)
  622. return -EINVAL;
  623. if (!func_item->groups) {
  624. func_item->groups =
  625. devm_kzalloc(&pdev->dev,
  626. func_item->ngroups * sizeof(*func_item->groups),
  627. GFP_KERNEL);
  628. if (!func_item->groups)
  629. return -ENOMEM;
  630. }
  631. func_grp = func_item->groups;
  632. while (*func_grp)
  633. func_grp++;
  634. *func_grp = pin->pin.name;
  635. func++;
  636. }
  637. }
  638. return 0;
  639. }
  640. static int sunxi_pinctrl_probe(struct platform_device *pdev)
  641. {
  642. struct device_node *node = pdev->dev.of_node;
  643. const struct of_device_id *device;
  644. struct pinctrl_pin_desc *pins;
  645. struct sunxi_pinctrl *pctl;
  646. int i, ret;
  647. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  648. if (!pctl)
  649. return -ENOMEM;
  650. platform_set_drvdata(pdev, pctl);
  651. pctl->membase = of_iomap(node, 0);
  652. if (!pctl->membase)
  653. return -ENOMEM;
  654. device = of_match_device(sunxi_pinctrl_match, &pdev->dev);
  655. if (!device)
  656. return -ENODEV;
  657. pctl->desc = (struct sunxi_pinctrl_desc *)device->data;
  658. ret = sunxi_pinctrl_build_state(pdev);
  659. if (ret) {
  660. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  661. return ret;
  662. }
  663. pins = devm_kzalloc(&pdev->dev,
  664. pctl->desc->npins * sizeof(*pins),
  665. GFP_KERNEL);
  666. if (!pins)
  667. return -ENOMEM;
  668. for (i = 0; i < pctl->desc->npins; i++)
  669. pins[i] = pctl->desc->pins[i].pin;
  670. sunxi_pctrl_desc.name = dev_name(&pdev->dev);
  671. sunxi_pctrl_desc.owner = THIS_MODULE;
  672. sunxi_pctrl_desc.pins = pins;
  673. sunxi_pctrl_desc.npins = pctl->desc->npins;
  674. pctl->dev = &pdev->dev;
  675. pctl->pctl_dev = pinctrl_register(&sunxi_pctrl_desc,
  676. &pdev->dev, pctl);
  677. if (!pctl->pctl_dev) {
  678. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  679. return -EINVAL;
  680. }
  681. dev_info(&pdev->dev, "initialized sunXi pin control driver\n");
  682. return 0;
  683. }
  684. static struct platform_driver sunxi_pinctrl_driver = {
  685. .probe = sunxi_pinctrl_probe,
  686. .driver = {
  687. .name = "sunxi-pinctrl",
  688. .owner = THIS_MODULE,
  689. .of_match_table = sunxi_pinctrl_match,
  690. },
  691. };
  692. module_platform_driver(sunxi_pinctrl_driver);
  693. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  694. MODULE_DESCRIPTION("Allwinner A1X pinctrl driver");
  695. MODULE_LICENSE("GPL");