pinctrl-sunxi.c 25 KB

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