pinctrl-artpec6.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Driver for the Axis ARTPEC-6 pin controller
  3. *
  4. * Author: Chris Paterson <chris.paterson@linux.pieboy.co.uk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <linux/slab.h>
  21. #include "core.h"
  22. #include "pinconf.h"
  23. #include "pinctrl-utils.h"
  24. #define ARTPEC6_LAST_PIN 97 /* 97 pins in pinmux */
  25. #define ARTPEC6_MAX_MUXABLE 35 /* Last pin with muxable function */
  26. /* Pinmux control register bit definitions */
  27. #define ARTPEC6_PINMUX_UDC0_MASK 0x00000001
  28. #define ARTPEC6_PINMUX_UDC0_SHIFT 0
  29. #define ARTPEC6_PINMUX_UDC1_MASK 0x00000002
  30. #define ARTPEC6_PINMUX_UDC1_SHIFT 1
  31. #define ARTPEC6_PINMUX_DRV_MASK 0x00000060
  32. #define ARTPEC6_PINMUX_DRV_SHIFT 5
  33. #define ARTPEC6_PINMUX_SEL_MASK 0x00003000
  34. #define ARTPEC6_PINMUX_SEL_SHIFT 12
  35. /* Pinmux configurations */
  36. #define ARTPEC6_CONFIG_0 0
  37. #define ARTPEC6_CONFIG_1 1
  38. #define ARTPEC6_CONFIG_2 2
  39. #define ARTPEC6_CONFIG_3 3
  40. /* Pin drive strength options */
  41. #define ARTPEC6_DRIVE_4mA 4
  42. #define ARTPEC6_DRIVE_4mA_SET 0
  43. #define ARTPEC6_DRIVE_6mA 6
  44. #define ARTPEC6_DRIVE_6mA_SET 1
  45. #define ARTPEC6_DRIVE_8mA 8
  46. #define ARTPEC6_DRIVE_8mA_SET 2
  47. #define ARTPEC6_DRIVE_9mA 9
  48. #define ARTPEC6_DRIVE_9mA_SET 3
  49. struct artpec6_pmx {
  50. struct device *dev;
  51. struct pinctrl_dev *pctl;
  52. void __iomem *base;
  53. struct pinctrl_pin_desc *pins;
  54. unsigned int num_pins;
  55. const struct artpec6_pin_group *pin_groups;
  56. unsigned int num_pin_groups;
  57. const struct artpec6_pmx_func *functions;
  58. unsigned int num_functions;
  59. };
  60. struct artpec6_pin_group {
  61. const char *name;
  62. const unsigned int *pins;
  63. const unsigned int num_pins;
  64. unsigned char config;
  65. };
  66. struct artpec6_pmx_func {
  67. const char *name;
  68. const char * const *groups;
  69. const unsigned int num_groups;
  70. };
  71. /* pins */
  72. static struct pinctrl_pin_desc artpec6_pins[] = {
  73. PINCTRL_PIN(0, "GPIO0"),
  74. PINCTRL_PIN(1, "GPIO1"),
  75. PINCTRL_PIN(2, "GPIO2"),
  76. PINCTRL_PIN(3, "GPIO3"),
  77. PINCTRL_PIN(4, "GPIO4"),
  78. PINCTRL_PIN(5, "GPIO5"),
  79. PINCTRL_PIN(6, "GPIO6"),
  80. PINCTRL_PIN(7, "GPIO7"),
  81. PINCTRL_PIN(8, "GPIO8"),
  82. PINCTRL_PIN(9, "GPIO9"),
  83. PINCTRL_PIN(10, "GPIO10"),
  84. PINCTRL_PIN(11, "GPIO11"),
  85. PINCTRL_PIN(12, "GPIO12"),
  86. PINCTRL_PIN(13, "GPIO13"),
  87. PINCTRL_PIN(14, "GPIO14"),
  88. PINCTRL_PIN(15, "GPIO15"),
  89. PINCTRL_PIN(16, "GPIO16"),
  90. PINCTRL_PIN(17, "GPIO17"),
  91. PINCTRL_PIN(18, "GPIO18"),
  92. PINCTRL_PIN(19, "GPIO19"),
  93. PINCTRL_PIN(20, "GPIO20"),
  94. PINCTRL_PIN(21, "GPIO21"),
  95. PINCTRL_PIN(22, "GPIO22"),
  96. PINCTRL_PIN(23, "GPIO23"),
  97. PINCTRL_PIN(24, "GPIO24"),
  98. PINCTRL_PIN(25, "GPIO25"),
  99. PINCTRL_PIN(26, "GPIO26"),
  100. PINCTRL_PIN(27, "GPIO27"),
  101. PINCTRL_PIN(28, "GPIO28"),
  102. PINCTRL_PIN(29, "GPIO29"),
  103. PINCTRL_PIN(30, "GPIO30"),
  104. PINCTRL_PIN(31, "GPIO31"),
  105. PINCTRL_PIN(32, "UART3_TXD"),
  106. PINCTRL_PIN(33, "UART3_RXD"),
  107. PINCTRL_PIN(34, "UART3_RTS"),
  108. PINCTRL_PIN(35, "UART3_CTS"),
  109. PINCTRL_PIN(36, "NF_ALE"),
  110. PINCTRL_PIN(37, "NF_CE0_N"),
  111. PINCTRL_PIN(38, "NF_CE1_N"),
  112. PINCTRL_PIN(39, "NF_CLE"),
  113. PINCTRL_PIN(40, "NF_RE_N"),
  114. PINCTRL_PIN(41, "NF_WE_N"),
  115. PINCTRL_PIN(42, "NF_WP0_N"),
  116. PINCTRL_PIN(43, "NF_WP1_N"),
  117. PINCTRL_PIN(44, "NF_IO0"),
  118. PINCTRL_PIN(45, "NF_IO1"),
  119. PINCTRL_PIN(46, "NF_IO2"),
  120. PINCTRL_PIN(47, "NF_IO3"),
  121. PINCTRL_PIN(48, "NF_IO4"),
  122. PINCTRL_PIN(49, "NF_IO5"),
  123. PINCTRL_PIN(50, "NF_IO6"),
  124. PINCTRL_PIN(51, "NF_IO7"),
  125. PINCTRL_PIN(52, "NF_RB0_N"),
  126. PINCTRL_PIN(53, "SDIO0_CLK"),
  127. PINCTRL_PIN(54, "SDIO0_CMD"),
  128. PINCTRL_PIN(55, "SDIO0_DAT0"),
  129. PINCTRL_PIN(56, "SDIO0_DAT1"),
  130. PINCTRL_PIN(57, "SDIO0_DAT2"),
  131. PINCTRL_PIN(58, "SDIO0_DAT3"),
  132. PINCTRL_PIN(59, "SDI0_CD"),
  133. PINCTRL_PIN(60, "SDI0_WP"),
  134. PINCTRL_PIN(61, "SDIO1_CLK"),
  135. PINCTRL_PIN(62, "SDIO1_CMD"),
  136. PINCTRL_PIN(63, "SDIO1_DAT0"),
  137. PINCTRL_PIN(64, "SDIO1_DAT1"),
  138. PINCTRL_PIN(65, "SDIO1_DAT2"),
  139. PINCTRL_PIN(66, "SDIO1_DAT3"),
  140. PINCTRL_PIN(67, "SDIO1_CD"),
  141. PINCTRL_PIN(68, "SDIO1_WP"),
  142. PINCTRL_PIN(69, "GBE_REFCLk"),
  143. PINCTRL_PIN(70, "GBE_GTX_CLK"),
  144. PINCTRL_PIN(71, "GBE_TX_CLK"),
  145. PINCTRL_PIN(72, "GBE_TX_EN"),
  146. PINCTRL_PIN(73, "GBE_TX_ER"),
  147. PINCTRL_PIN(74, "GBE_TXD0"),
  148. PINCTRL_PIN(75, "GBE_TXD1"),
  149. PINCTRL_PIN(76, "GBE_TXD2"),
  150. PINCTRL_PIN(77, "GBE_TXD3"),
  151. PINCTRL_PIN(78, "GBE_TXD4"),
  152. PINCTRL_PIN(79, "GBE_TXD5"),
  153. PINCTRL_PIN(80, "GBE_TXD6"),
  154. PINCTRL_PIN(81, "GBE_TXD7"),
  155. PINCTRL_PIN(82, "GBE_RX_CLK"),
  156. PINCTRL_PIN(83, "GBE_RX_DV"),
  157. PINCTRL_PIN(84, "GBE_RX_ER"),
  158. PINCTRL_PIN(85, "GBE_RXD0"),
  159. PINCTRL_PIN(86, "GBE_RXD1"),
  160. PINCTRL_PIN(87, "GBE_RXD2"),
  161. PINCTRL_PIN(88, "GBE_RXD3"),
  162. PINCTRL_PIN(89, "GBE_RXD4"),
  163. PINCTRL_PIN(90, "GBE_RXD5"),
  164. PINCTRL_PIN(91, "GBE_RXD6"),
  165. PINCTRL_PIN(92, "GBE_RXD7"),
  166. PINCTRL_PIN(93, "GBE_CRS"),
  167. PINCTRL_PIN(94, "GBE_COL"),
  168. PINCTRL_PIN(95, "GBE_MDC"),
  169. PINCTRL_PIN(96, "GBE_MDIO"),
  170. };
  171. static const unsigned int cpuclkout_pins0[] = { 0 };
  172. static const unsigned int udlclkout_pins0[] = { 1 };
  173. static const unsigned int i2c1_pins0[] = { 2, 3 };
  174. static const unsigned int i2c2_pins0[] = { 4, 5 };
  175. static const unsigned int i2c3_pins0[] = { 6, 7 };
  176. static const unsigned int i2s0_pins0[] = { 8, 9, 10, 11 };
  177. static const unsigned int i2s1_pins0[] = { 12, 13, 14, 15 };
  178. static const unsigned int i2srefclk_pins0[] = { 19 };
  179. static const unsigned int spi0_pins0[] = { 12, 13, 14, 15 };
  180. static const unsigned int spi1_pins0[] = { 16, 17, 18, 19 };
  181. static const unsigned int pciedebug_pins0[] = { 12, 13, 14, 15 };
  182. static const unsigned int uart0_pins0[] = { 16, 17, 18, 19, 20,
  183. 21, 22, 23, 24, 25 };
  184. static const unsigned int uart0_pins1[] = { 20, 21, 22, 23 };
  185. static const unsigned int uart1_pins0[] = { 24, 25, 26, 27 };
  186. static const unsigned int uart2_pins0[] = { 26, 27, 28, 29, 30,
  187. 31, 32, 33, 34, 35 };
  188. static const unsigned int uart2_pins1[] = { 28, 29, 30, 31 };
  189. static const unsigned int uart3_pins0[] = { 32, 33, 34, 35 };
  190. static const unsigned int uart4_pins0[] = { 20, 21, 22, 23 };
  191. static const unsigned int uart5_pins0[] = { 28, 29, 30, 31 };
  192. static const unsigned int nand_pins0[] = { 36, 37, 38, 39, 40, 41,
  193. 42, 43, 44, 45, 46, 47,
  194. 48, 49, 50, 51, 52 };
  195. static const unsigned int sdio0_pins0[] = { 53, 54, 55, 56, 57, 58, 59, 60 };
  196. static const unsigned int sdio1_pins0[] = { 61, 62, 63, 64, 65, 66, 67, 68 };
  197. static const unsigned int ethernet_pins0[] = { 69, 70, 71, 72, 73, 74, 75,
  198. 76, 77, 78, 79, 80, 81, 82,
  199. 83, 84, 85, 86, 87, 88, 89,
  200. 90, 91, 92, 93, 94, 95, 96 };
  201. static const struct artpec6_pin_group artpec6_pin_groups[] = {
  202. {
  203. .name = "cpuclkoutgrp0",
  204. .pins = cpuclkout_pins0,
  205. .num_pins = ARRAY_SIZE(cpuclkout_pins0),
  206. .config = ARTPEC6_CONFIG_1,
  207. },
  208. {
  209. .name = "udlclkoutgrp0",
  210. .pins = udlclkout_pins0,
  211. .num_pins = ARRAY_SIZE(udlclkout_pins0),
  212. .config = ARTPEC6_CONFIG_1,
  213. },
  214. {
  215. .name = "i2c1grp0",
  216. .pins = i2c1_pins0,
  217. .num_pins = ARRAY_SIZE(i2c1_pins0),
  218. .config = ARTPEC6_CONFIG_1,
  219. },
  220. {
  221. .name = "i2c2grp0",
  222. .pins = i2c2_pins0,
  223. .num_pins = ARRAY_SIZE(i2c2_pins0),
  224. .config = ARTPEC6_CONFIG_1,
  225. },
  226. {
  227. .name = "i2c3grp0",
  228. .pins = i2c3_pins0,
  229. .num_pins = ARRAY_SIZE(i2c3_pins0),
  230. .config = ARTPEC6_CONFIG_1,
  231. },
  232. {
  233. .name = "i2s0grp0",
  234. .pins = i2s0_pins0,
  235. .num_pins = ARRAY_SIZE(i2s0_pins0),
  236. .config = ARTPEC6_CONFIG_1,
  237. },
  238. {
  239. .name = "i2s1grp0",
  240. .pins = i2s1_pins0,
  241. .num_pins = ARRAY_SIZE(i2s1_pins0),
  242. .config = ARTPEC6_CONFIG_1,
  243. },
  244. {
  245. .name = "i2srefclkgrp0",
  246. .pins = i2srefclk_pins0,
  247. .num_pins = ARRAY_SIZE(i2srefclk_pins0),
  248. .config = ARTPEC6_CONFIG_3,
  249. },
  250. {
  251. .name = "spi0grp0",
  252. .pins = spi0_pins0,
  253. .num_pins = ARRAY_SIZE(spi0_pins0),
  254. .config = ARTPEC6_CONFIG_2,
  255. },
  256. {
  257. .name = "spi1grp0",
  258. .pins = spi1_pins0,
  259. .num_pins = ARRAY_SIZE(spi1_pins0),
  260. .config = ARTPEC6_CONFIG_2,
  261. },
  262. {
  263. .name = "pciedebuggrp0",
  264. .pins = pciedebug_pins0,
  265. .num_pins = ARRAY_SIZE(pciedebug_pins0),
  266. .config = ARTPEC6_CONFIG_3,
  267. },
  268. {
  269. .name = "uart0grp0",
  270. .pins = uart0_pins0,
  271. .num_pins = ARRAY_SIZE(uart0_pins0),
  272. .config = ARTPEC6_CONFIG_1,
  273. },
  274. {
  275. .name = "uart0grp1",
  276. .pins = uart0_pins1,
  277. .num_pins = ARRAY_SIZE(uart0_pins1),
  278. .config = ARTPEC6_CONFIG_1,
  279. },
  280. {
  281. .name = "uart1grp0",
  282. .pins = uart1_pins0,
  283. .num_pins = ARRAY_SIZE(uart1_pins0),
  284. .config = ARTPEC6_CONFIG_2,
  285. },
  286. {
  287. .name = "uart2grp0",
  288. .pins = uart2_pins0,
  289. .num_pins = ARRAY_SIZE(uart2_pins0),
  290. .config = ARTPEC6_CONFIG_1,
  291. },
  292. {
  293. .name = "uart2grp1",
  294. .pins = uart2_pins1,
  295. .num_pins = ARRAY_SIZE(uart2_pins1),
  296. .config = ARTPEC6_CONFIG_1,
  297. },
  298. {
  299. .name = "uart3grp0",
  300. .pins = uart3_pins0,
  301. .num_pins = ARRAY_SIZE(uart3_pins0),
  302. .config = ARTPEC6_CONFIG_0,
  303. },
  304. {
  305. .name = "uart4grp0",
  306. .pins = uart4_pins0,
  307. .num_pins = ARRAY_SIZE(uart4_pins0),
  308. .config = ARTPEC6_CONFIG_2,
  309. },
  310. {
  311. .name = "uart5grp0",
  312. .pins = uart5_pins0,
  313. .num_pins = ARRAY_SIZE(uart5_pins0),
  314. .config = ARTPEC6_CONFIG_2,
  315. },
  316. {
  317. .name = "uart5nocts",
  318. .pins = uart5_pins0,
  319. .num_pins = ARRAY_SIZE(uart5_pins0) - 1,
  320. .config = ARTPEC6_CONFIG_2,
  321. },
  322. {
  323. .name = "nandgrp0",
  324. .pins = nand_pins0,
  325. .num_pins = ARRAY_SIZE(nand_pins0),
  326. .config = ARTPEC6_CONFIG_0,
  327. },
  328. {
  329. .name = "sdio0grp0",
  330. .pins = sdio0_pins0,
  331. .num_pins = ARRAY_SIZE(sdio0_pins0),
  332. .config = ARTPEC6_CONFIG_0,
  333. },
  334. {
  335. .name = "sdio1grp0",
  336. .pins = sdio1_pins0,
  337. .num_pins = ARRAY_SIZE(sdio1_pins0),
  338. .config = ARTPEC6_CONFIG_0,
  339. },
  340. {
  341. .name = "ethernetgrp0",
  342. .pins = ethernet_pins0,
  343. .num_pins = ARRAY_SIZE(ethernet_pins0),
  344. .config = ARTPEC6_CONFIG_0,
  345. },
  346. };
  347. struct pin_register {
  348. unsigned int start;
  349. unsigned int end;
  350. unsigned int reg_base;
  351. };
  352. /*
  353. * The register map has two holes where the pin number
  354. * no longer fits directly with the register offset.
  355. * This table allows us to map this easily.
  356. */
  357. static const struct pin_register pin_register[] = {
  358. { 0, 35, 0x0 }, /* 0x0 - 0x8c */
  359. { 36, 52, 0x100 }, /* 0x100 - 0x140 */
  360. { 53, 96, 0x180 }, /* 0x180 - 0x22c */
  361. };
  362. static unsigned int artpec6_pmx_reg_offset(unsigned int pin)
  363. {
  364. int i;
  365. for (i = 0; i < ARRAY_SIZE(pin_register); i++) {
  366. if (pin <= pin_register[i].end) {
  367. return (pin - pin_register[i].start) * 4 +
  368. pin_register[i].reg_base;
  369. }
  370. }
  371. /*
  372. * Anything we return here is wrong, but we can only
  373. * get here if pin is outside registered range.
  374. */
  375. pr_err("%s: Impossible pin %d\n", __func__, pin);
  376. return 0;
  377. }
  378. static int artpec6_get_groups_count(struct pinctrl_dev *pctldev)
  379. {
  380. return ARRAY_SIZE(artpec6_pin_groups);
  381. }
  382. static const char *artpec6_get_group_name(struct pinctrl_dev *pctldev,
  383. unsigned int group)
  384. {
  385. return artpec6_pin_groups[group].name;
  386. }
  387. static int artpec6_get_group_pins(struct pinctrl_dev *pctldev,
  388. unsigned int group,
  389. const unsigned int **pins,
  390. unsigned int *num_pins)
  391. {
  392. *pins = (unsigned int *)artpec6_pin_groups[group].pins;
  393. *num_pins = artpec6_pin_groups[group].num_pins;
  394. return 0;
  395. }
  396. static int artpec6_pconf_drive_mA_to_field(unsigned int mA)
  397. {
  398. switch (mA) {
  399. case ARTPEC6_DRIVE_4mA:
  400. return ARTPEC6_DRIVE_4mA_SET;
  401. case ARTPEC6_DRIVE_6mA:
  402. return ARTPEC6_DRIVE_6mA_SET;
  403. case ARTPEC6_DRIVE_8mA:
  404. return ARTPEC6_DRIVE_8mA_SET;
  405. case ARTPEC6_DRIVE_9mA:
  406. return ARTPEC6_DRIVE_9mA_SET;
  407. default:
  408. return -EINVAL;
  409. }
  410. }
  411. static unsigned int artpec6_pconf_drive_field_to_mA(int field)
  412. {
  413. switch (field) {
  414. case ARTPEC6_DRIVE_4mA_SET:
  415. return ARTPEC6_DRIVE_4mA;
  416. case ARTPEC6_DRIVE_6mA_SET:
  417. return ARTPEC6_DRIVE_6mA;
  418. case ARTPEC6_DRIVE_8mA_SET:
  419. return ARTPEC6_DRIVE_8mA;
  420. case ARTPEC6_DRIVE_9mA_SET:
  421. return ARTPEC6_DRIVE_9mA;
  422. default:
  423. /* Shouldn't happen */
  424. return 0;
  425. }
  426. }
  427. static const struct pinctrl_ops artpec6_pctrl_ops = {
  428. .get_group_pins = artpec6_get_group_pins,
  429. .get_groups_count = artpec6_get_groups_count,
  430. .get_group_name = artpec6_get_group_name,
  431. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  432. .dt_free_map = pinctrl_utils_free_map,
  433. };
  434. static const char * const gpiogrps[] = {
  435. "cpuclkoutgrp0", "udlclkoutgrp0", "i2c1grp0", "i2c2grp0",
  436. "i2c3grp0", "i2s0grp0", "i2s1grp0", "i2srefclkgrp0",
  437. "spi0grp0", "spi1grp0", "pciedebuggrp0", "uart0grp0",
  438. "uart0grp1", "uart1grp0", "uart2grp0", "uart2grp1",
  439. "uart4grp0", "uart5grp0",
  440. };
  441. static const char * const cpuclkoutgrps[] = { "cpuclkoutgrp0" };
  442. static const char * const udlclkoutgrps[] = { "udlclkoutgrp0" };
  443. static const char * const i2c1grps[] = { "i2c1grp0" };
  444. static const char * const i2c2grps[] = { "i2c2grp0" };
  445. static const char * const i2c3grps[] = { "i2c3grp0" };
  446. static const char * const i2s0grps[] = { "i2s0grp0" };
  447. static const char * const i2s1grps[] = { "i2s1grp0" };
  448. static const char * const i2srefclkgrps[] = { "i2srefclkgrp0" };
  449. static const char * const spi0grps[] = { "spi0grp0" };
  450. static const char * const spi1grps[] = { "spi1grp0" };
  451. static const char * const pciedebuggrps[] = { "pciedebuggrp0" };
  452. static const char * const uart0grps[] = { "uart0grp0", "uart0grp1" };
  453. static const char * const uart1grps[] = { "uart1grp0" };
  454. static const char * const uart2grps[] = { "uart2grp0", "uart2grp1" };
  455. static const char * const uart3grps[] = { "uart3grp0" };
  456. static const char * const uart4grps[] = { "uart4grp0" };
  457. static const char * const uart5grps[] = { "uart5grp0", "uart5nocts" };
  458. static const char * const nandgrps[] = { "nandgrp0" };
  459. static const char * const sdio0grps[] = { "sdio0grp0" };
  460. static const char * const sdio1grps[] = { "sdio1grp0" };
  461. static const char * const ethernetgrps[] = { "ethernetgrp0" };
  462. static const struct artpec6_pmx_func artpec6_pmx_functions[] = {
  463. {
  464. .name = "gpio",
  465. .groups = gpiogrps,
  466. .num_groups = ARRAY_SIZE(gpiogrps),
  467. },
  468. {
  469. .name = "cpuclkout",
  470. .groups = cpuclkoutgrps,
  471. .num_groups = ARRAY_SIZE(cpuclkoutgrps),
  472. },
  473. {
  474. .name = "udlclkout",
  475. .groups = udlclkoutgrps,
  476. .num_groups = ARRAY_SIZE(udlclkoutgrps),
  477. },
  478. {
  479. .name = "i2c1",
  480. .groups = i2c1grps,
  481. .num_groups = ARRAY_SIZE(i2c1grps),
  482. },
  483. {
  484. .name = "i2c2",
  485. .groups = i2c2grps,
  486. .num_groups = ARRAY_SIZE(i2c2grps),
  487. },
  488. {
  489. .name = "i2c3",
  490. .groups = i2c3grps,
  491. .num_groups = ARRAY_SIZE(i2c3grps),
  492. },
  493. {
  494. .name = "i2s0",
  495. .groups = i2s0grps,
  496. .num_groups = ARRAY_SIZE(i2s0grps),
  497. },
  498. {
  499. .name = "i2s1",
  500. .groups = i2s1grps,
  501. .num_groups = ARRAY_SIZE(i2s1grps),
  502. },
  503. {
  504. .name = "i2srefclk",
  505. .groups = i2srefclkgrps,
  506. .num_groups = ARRAY_SIZE(i2srefclkgrps),
  507. },
  508. {
  509. .name = "spi0",
  510. .groups = spi0grps,
  511. .num_groups = ARRAY_SIZE(spi0grps),
  512. },
  513. {
  514. .name = "spi1",
  515. .groups = spi1grps,
  516. .num_groups = ARRAY_SIZE(spi1grps),
  517. },
  518. {
  519. .name = "pciedebug",
  520. .groups = pciedebuggrps,
  521. .num_groups = ARRAY_SIZE(pciedebuggrps),
  522. },
  523. {
  524. .name = "uart0",
  525. .groups = uart0grps,
  526. .num_groups = ARRAY_SIZE(uart0grps),
  527. },
  528. {
  529. .name = "uart1",
  530. .groups = uart1grps,
  531. .num_groups = ARRAY_SIZE(uart1grps),
  532. },
  533. {
  534. .name = "uart2",
  535. .groups = uart2grps,
  536. .num_groups = ARRAY_SIZE(uart2grps),
  537. },
  538. {
  539. .name = "uart3",
  540. .groups = uart3grps,
  541. .num_groups = ARRAY_SIZE(uart3grps),
  542. },
  543. {
  544. .name = "uart4",
  545. .groups = uart4grps,
  546. .num_groups = ARRAY_SIZE(uart4grps),
  547. },
  548. {
  549. .name = "uart5",
  550. .groups = uart5grps,
  551. .num_groups = ARRAY_SIZE(uart5grps),
  552. },
  553. {
  554. .name = "nand",
  555. .groups = nandgrps,
  556. .num_groups = ARRAY_SIZE(nandgrps),
  557. },
  558. {
  559. .name = "sdio0",
  560. .groups = sdio0grps,
  561. .num_groups = ARRAY_SIZE(sdio0grps),
  562. },
  563. {
  564. .name = "sdio1",
  565. .groups = sdio1grps,
  566. .num_groups = ARRAY_SIZE(sdio1grps),
  567. },
  568. {
  569. .name = "ethernet",
  570. .groups = ethernetgrps,
  571. .num_groups = ARRAY_SIZE(ethernetgrps),
  572. },
  573. };
  574. static int artpec6_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  575. {
  576. return ARRAY_SIZE(artpec6_pmx_functions);
  577. }
  578. static const char *artpec6_pmx_get_fname(struct pinctrl_dev *pctldev,
  579. unsigned int function)
  580. {
  581. return artpec6_pmx_functions[function].name;
  582. }
  583. static int artpec6_pmx_get_fgroups(struct pinctrl_dev *pctldev,
  584. unsigned int function,
  585. const char * const **groups,
  586. unsigned int * const num_groups)
  587. {
  588. *groups = artpec6_pmx_functions[function].groups;
  589. *num_groups = artpec6_pmx_functions[function].num_groups;
  590. return 0;
  591. }
  592. static void artpec6_pmx_select_func(struct pinctrl_dev *pctldev,
  593. unsigned int function, unsigned int group,
  594. bool enable)
  595. {
  596. unsigned int regval, val;
  597. unsigned int reg;
  598. int i;
  599. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  600. for (i = 0; i < artpec6_pin_groups[group].num_pins; i++) {
  601. /*
  602. * Registers for pins above a ARTPEC6_MAX_MUXABLE
  603. * do not have a SEL field and are always selected.
  604. */
  605. if (artpec6_pin_groups[group].pins[i] > ARTPEC6_MAX_MUXABLE)
  606. continue;
  607. if (!strcmp(artpec6_pmx_get_fname(pctldev, function), "gpio")) {
  608. /* GPIO is always config 0 */
  609. val = ARTPEC6_CONFIG_0 << ARTPEC6_PINMUX_SEL_SHIFT;
  610. } else {
  611. if (enable)
  612. val = artpec6_pin_groups[group].config
  613. << ARTPEC6_PINMUX_SEL_SHIFT;
  614. else
  615. val = ARTPEC6_CONFIG_0
  616. << ARTPEC6_PINMUX_SEL_SHIFT;
  617. }
  618. reg = artpec6_pmx_reg_offset(artpec6_pin_groups[group].pins[i]);
  619. regval = readl(pmx->base + reg);
  620. regval &= ~ARTPEC6_PINMUX_SEL_MASK;
  621. regval |= val;
  622. writel(regval, pmx->base + reg);
  623. }
  624. }
  625. int artpec6_pmx_enable(struct pinctrl_dev *pctldev, unsigned int function,
  626. unsigned int group)
  627. {
  628. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  629. dev_dbg(pmx->dev, "enabling %s function for pin group %s\n",
  630. artpec6_pmx_get_fname(pctldev, function),
  631. artpec6_get_group_name(pctldev, group));
  632. artpec6_pmx_select_func(pctldev, function, group, true);
  633. return 0;
  634. }
  635. void artpec6_pmx_disable(struct pinctrl_dev *pctldev, unsigned int function,
  636. unsigned int group)
  637. {
  638. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  639. dev_dbg(pmx->dev, "disabling %s function for pin group %s\n",
  640. artpec6_pmx_get_fname(pctldev, function),
  641. artpec6_get_group_name(pctldev, group));
  642. artpec6_pmx_select_func(pctldev, function, group, false);
  643. }
  644. static int artpec6_pmx_request_gpio(struct pinctrl_dev *pctldev,
  645. struct pinctrl_gpio_range *range,
  646. unsigned int pin)
  647. {
  648. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  649. unsigned int reg = artpec6_pmx_reg_offset(pin);
  650. u32 val;
  651. if (pin >= 32)
  652. return -EINVAL;
  653. val = readl_relaxed(pmx->base + reg);
  654. val &= ~ARTPEC6_PINMUX_SEL_MASK;
  655. val |= ARTPEC6_CONFIG_0 << ARTPEC6_PINMUX_SEL_SHIFT;
  656. writel_relaxed(val, pmx->base + reg);
  657. return 0;
  658. }
  659. static const struct pinmux_ops artpec6_pmx_ops = {
  660. .get_functions_count = artpec6_pmx_get_functions_count,
  661. .get_function_name = artpec6_pmx_get_fname,
  662. .get_function_groups = artpec6_pmx_get_fgroups,
  663. .set_mux = artpec6_pmx_enable,
  664. .gpio_request_enable = artpec6_pmx_request_gpio,
  665. };
  666. static int artpec6_pconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  667. unsigned long *config)
  668. {
  669. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  670. enum pin_config_param param = pinconf_to_config_param(*config);
  671. unsigned int regval;
  672. /* Check for valid pin */
  673. if (pin >= pmx->num_pins) {
  674. dev_dbg(pmx->dev, "pinconf is not supported for pin %s\n",
  675. pmx->pins[pin].name);
  676. return -ENOTSUPP;
  677. }
  678. dev_dbg(pmx->dev, "getting configuration for pin %s\n",
  679. pmx->pins[pin].name);
  680. /* Read pin register values */
  681. regval = readl(pmx->base + artpec6_pmx_reg_offset(pin));
  682. /* If valid, get configuration for parameter */
  683. switch (param) {
  684. case PIN_CONFIG_BIAS_DISABLE:
  685. if (!(regval & ARTPEC6_PINMUX_UDC1_MASK))
  686. return -EINVAL;
  687. break;
  688. case PIN_CONFIG_BIAS_PULL_UP:
  689. case PIN_CONFIG_BIAS_PULL_DOWN:
  690. if (regval & ARTPEC6_PINMUX_UDC1_MASK)
  691. return -EINVAL;
  692. regval = regval & ARTPEC6_PINMUX_UDC0_MASK;
  693. if ((param == PIN_CONFIG_BIAS_PULL_UP && !regval) ||
  694. (param == PIN_CONFIG_BIAS_PULL_DOWN && regval))
  695. return -EINVAL;
  696. break;
  697. case PIN_CONFIG_DRIVE_STRENGTH:
  698. regval = (regval & ARTPEC6_PINMUX_DRV_MASK)
  699. >> ARTPEC6_PINMUX_DRV_SHIFT;
  700. regval = artpec6_pconf_drive_field_to_mA(regval);
  701. *config = pinconf_to_config_packed(param, regval);
  702. break;
  703. default:
  704. return -ENOTSUPP;
  705. }
  706. return 0;
  707. }
  708. /*
  709. * Valid combinations of param and arg:
  710. *
  711. * param arg
  712. * PIN_CONFIG_BIAS_DISABLE: x (disable bias)
  713. * PIN_CONFIG_BIAS_PULL_UP: 1 (pull up bias + enable)
  714. * PIN_CONFIG_BIAS_PULL_DOWN: 1 (pull down bias + enable)
  715. * PIN_CONFIG_DRIVE_STRENGTH: x (4mA, 6mA, 8mA, 9mA)
  716. *
  717. * All other args are invalid. All other params are not supported.
  718. */
  719. static int artpec6_pconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  720. unsigned long *configs, unsigned int num_configs)
  721. {
  722. struct artpec6_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  723. enum pin_config_param param;
  724. unsigned int arg;
  725. unsigned int regval;
  726. unsigned int *reg;
  727. int i;
  728. /* Check for valid pin */
  729. if (pin >= pmx->num_pins) {
  730. dev_dbg(pmx->dev, "pinconf is not supported for pin %s\n",
  731. pmx->pins[pin].name);
  732. return -ENOTSUPP;
  733. }
  734. dev_dbg(pmx->dev, "setting configuration for pin %s\n",
  735. pmx->pins[pin].name);
  736. reg = pmx->base + artpec6_pmx_reg_offset(pin);
  737. /* For each config */
  738. for (i = 0; i < num_configs; i++) {
  739. int drive;
  740. param = pinconf_to_config_param(configs[i]);
  741. arg = pinconf_to_config_argument(configs[i]);
  742. switch (param) {
  743. case PIN_CONFIG_BIAS_DISABLE:
  744. regval = readl(reg);
  745. regval |= (1 << ARTPEC6_PINMUX_UDC1_SHIFT);
  746. writel(regval, reg);
  747. break;
  748. case PIN_CONFIG_BIAS_PULL_UP:
  749. if (arg != 1) {
  750. dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
  751. __func__, arg);
  752. return -EINVAL;
  753. }
  754. regval = readl(reg);
  755. regval |= (arg << ARTPEC6_PINMUX_UDC0_SHIFT);
  756. regval &= ~ARTPEC6_PINMUX_UDC1_MASK; /* Enable */
  757. writel(regval, reg);
  758. break;
  759. case PIN_CONFIG_BIAS_PULL_DOWN:
  760. if (arg != 1) {
  761. dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
  762. __func__, arg);
  763. return -EINVAL;
  764. }
  765. regval = readl(reg);
  766. regval &= ~(arg << ARTPEC6_PINMUX_UDC0_SHIFT);
  767. regval &= ~ARTPEC6_PINMUX_UDC1_MASK; /* Enable */
  768. writel(regval, reg);
  769. break;
  770. case PIN_CONFIG_DRIVE_STRENGTH:
  771. drive = artpec6_pconf_drive_mA_to_field(arg);
  772. if (drive < 0) {
  773. dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
  774. __func__, arg);
  775. return -EINVAL;
  776. }
  777. regval = readl(reg);
  778. regval &= ~ARTPEC6_PINMUX_DRV_MASK;
  779. regval |= (drive << ARTPEC6_PINMUX_DRV_SHIFT);
  780. writel(regval, reg);
  781. break;
  782. default:
  783. dev_dbg(pmx->dev, "parameter not supported\n");
  784. return -ENOTSUPP;
  785. }
  786. }
  787. return 0;
  788. }
  789. static int artpec6_pconf_group_set(struct pinctrl_dev *pctldev,
  790. unsigned int group, unsigned long *configs,
  791. unsigned int num_configs)
  792. {
  793. unsigned int num_pins, current_pin;
  794. int ret;
  795. dev_dbg(pctldev->dev, "setting group %s configuration\n",
  796. artpec6_get_group_name(pctldev, group));
  797. num_pins = artpec6_pin_groups[group].num_pins;
  798. for (current_pin = 0; current_pin < num_pins; current_pin++) {
  799. ret = artpec6_pconf_set(pctldev,
  800. artpec6_pin_groups[group].pins[current_pin],
  801. configs, num_configs);
  802. if (ret < 0)
  803. return ret;
  804. }
  805. return 0;
  806. }
  807. static const struct pinconf_ops artpec6_pconf_ops = {
  808. .is_generic = true,
  809. .pin_config_get = artpec6_pconf_get,
  810. .pin_config_set = artpec6_pconf_set,
  811. .pin_config_group_set = artpec6_pconf_group_set,
  812. };
  813. static struct pinctrl_desc artpec6_desc = {
  814. .name = "artpec6-pinctrl",
  815. .owner = THIS_MODULE,
  816. .pins = artpec6_pins,
  817. .npins = ARRAY_SIZE(artpec6_pins),
  818. .pctlops = &artpec6_pctrl_ops,
  819. .pmxops = &artpec6_pmx_ops,
  820. .confops = &artpec6_pconf_ops,
  821. };
  822. /* The reset values say 4mA, but we want 8mA as default. */
  823. static void artpec6_pmx_reset(struct artpec6_pmx *pmx)
  824. {
  825. void __iomem *base = pmx->base;
  826. int i;
  827. for (i = 0; i < ARTPEC6_LAST_PIN; i++) {
  828. u32 val;
  829. val = readl_relaxed(base + artpec6_pmx_reg_offset(i));
  830. val &= ~ARTPEC6_PINMUX_DRV_MASK;
  831. val |= ARTPEC6_DRIVE_8mA_SET << ARTPEC6_PINMUX_DRV_SHIFT;
  832. writel_relaxed(val, base + artpec6_pmx_reg_offset(i));
  833. }
  834. }
  835. static int artpec6_pmx_probe(struct platform_device *pdev)
  836. {
  837. struct artpec6_pmx *pmx;
  838. struct resource *res;
  839. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  840. if (!pmx)
  841. return -ENOMEM;
  842. pmx->dev = &pdev->dev;
  843. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  844. pmx->base = devm_ioremap_resource(&pdev->dev, res);
  845. if (IS_ERR(pmx->base))
  846. return PTR_ERR(pmx->base);
  847. artpec6_pmx_reset(pmx);
  848. pmx->pins = artpec6_pins;
  849. pmx->num_pins = ARRAY_SIZE(artpec6_pins);
  850. pmx->functions = artpec6_pmx_functions;
  851. pmx->num_functions = ARRAY_SIZE(artpec6_pmx_functions);
  852. pmx->pin_groups = artpec6_pin_groups;
  853. pmx->num_pin_groups = ARRAY_SIZE(artpec6_pin_groups);
  854. pmx->pctl = pinctrl_register(&artpec6_desc, &pdev->dev, pmx);
  855. if (IS_ERR(pmx->pctl)) {
  856. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  857. return PTR_ERR(pmx->pctl);
  858. }
  859. platform_set_drvdata(pdev, pmx);
  860. dev_info(&pdev->dev, "initialised Axis ARTPEC-6 pinctrl driver\n");
  861. return 0;
  862. }
  863. static int artpec6_pmx_remove(struct platform_device *pdev)
  864. {
  865. struct artpec6_pmx *pmx = platform_get_drvdata(pdev);
  866. pinctrl_unregister(pmx->pctl);
  867. return 0;
  868. }
  869. static const struct of_device_id artpec6_pinctrl_match[] = {
  870. { .compatible = "axis,artpec6-pinctrl" },
  871. {},
  872. };
  873. static struct platform_driver artpec6_pmx_driver = {
  874. .driver = {
  875. .name = "artpec6-pinctrl",
  876. .of_match_table = artpec6_pinctrl_match,
  877. },
  878. .probe = artpec6_pmx_probe,
  879. .remove = artpec6_pmx_remove,
  880. };
  881. static int __init artpec6_pmx_init(void)
  882. {
  883. return platform_driver_register(&artpec6_pmx_driver);
  884. }
  885. arch_initcall(artpec6_pmx_init);