pinctrl-rza1.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Combined GPIO and pin controller support for Renesas RZ/A1 (r7s72100) SoC
  4. *
  5. * Copyright (C) 2017 Jacopo Mondi
  6. */
  7. /*
  8. * This pin controller/gpio combined driver supports Renesas devices of RZ/A1
  9. * family.
  10. * This includes SoCs which are sub- or super- sets of this particular line,
  11. * as RZ/A1H (r7s721000), RZ/A1M (r7s721010) and RZ/A1L (r7s721020).
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/init.h>
  17. #include <linux/ioport.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include <linux/slab.h>
  26. #include "core.h"
  27. #include "devicetree.h"
  28. #include "pinconf.h"
  29. #include "pinmux.h"
  30. #define DRIVER_NAME "pinctrl-rza1"
  31. #define RZA1_P_REG 0x0000
  32. #define RZA1_PPR_REG 0x0200
  33. #define RZA1_PM_REG 0x0300
  34. #define RZA1_PMC_REG 0x0400
  35. #define RZA1_PFC_REG 0x0500
  36. #define RZA1_PFCE_REG 0x0600
  37. #define RZA1_PFCEA_REG 0x0a00
  38. #define RZA1_PIBC_REG 0x4000
  39. #define RZA1_PBDC_REG 0x4100
  40. #define RZA1_PIPC_REG 0x4200
  41. #define RZA1_ADDR(mem, reg, port) ((mem) + (reg) + ((port) * 4))
  42. #define RZA1_NPORTS 12
  43. #define RZA1_PINS_PER_PORT 16
  44. #define RZA1_NPINS (RZA1_PINS_PER_PORT * RZA1_NPORTS)
  45. #define RZA1_PIN_ID_TO_PORT(id) ((id) / RZA1_PINS_PER_PORT)
  46. #define RZA1_PIN_ID_TO_PIN(id) ((id) % RZA1_PINS_PER_PORT)
  47. /*
  48. * Use 16 lower bits [15:0] for pin identifier
  49. * Use 16 higher bits [31:16] for pin mux function
  50. */
  51. #define MUX_PIN_ID_MASK GENMASK(15, 0)
  52. #define MUX_FUNC_MASK GENMASK(31, 16)
  53. #define MUX_FUNC_OFFS 16
  54. #define MUX_FUNC(pinconf) \
  55. ((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
  56. #define MUX_FUNC_PFC_MASK BIT(0)
  57. #define MUX_FUNC_PFCE_MASK BIT(1)
  58. #define MUX_FUNC_PFCEA_MASK BIT(2)
  59. /* Pin mux flags */
  60. #define MUX_FLAGS_BIDIR BIT(0)
  61. #define MUX_FLAGS_SWIO_INPUT BIT(1)
  62. #define MUX_FLAGS_SWIO_OUTPUT BIT(2)
  63. /* ----------------------------------------------------------------------------
  64. * RZ/A1 pinmux flags
  65. */
  66. /**
  67. * rza1_bidir_pin - describe a single pin that needs bidir flag applied.
  68. */
  69. struct rza1_bidir_pin {
  70. u8 pin: 4;
  71. u8 func: 4;
  72. };
  73. /**
  74. * rza1_bidir_entry - describe a list of pins that needs bidir flag applied.
  75. * Each struct rza1_bidir_entry describes a port.
  76. */
  77. struct rza1_bidir_entry {
  78. const unsigned int npins;
  79. const struct rza1_bidir_pin *pins;
  80. };
  81. /**
  82. * rza1_swio_pin - describe a single pin that needs bidir flag applied.
  83. */
  84. struct rza1_swio_pin {
  85. u16 pin: 4;
  86. u16 port: 4;
  87. u16 func: 4;
  88. u16 input: 1;
  89. };
  90. /**
  91. * rza1_swio_entry - describe a list of pins that needs swio flag applied
  92. */
  93. struct rza1_swio_entry {
  94. const unsigned int npins;
  95. const struct rza1_swio_pin *pins;
  96. };
  97. /**
  98. * rza1_pinmux_conf - group together bidir and swio pinmux flag tables
  99. */
  100. struct rza1_pinmux_conf {
  101. const struct rza1_bidir_entry *bidir_entries;
  102. const struct rza1_swio_entry *swio_entries;
  103. };
  104. /* ----------------------------------------------------------------------------
  105. * RZ/A1H (r7s72100) pinmux flags
  106. */
  107. static const struct rza1_bidir_pin rza1h_bidir_pins_p1[] = {
  108. { .pin = 0, .func = 1 },
  109. { .pin = 1, .func = 1 },
  110. { .pin = 2, .func = 1 },
  111. { .pin = 3, .func = 1 },
  112. { .pin = 4, .func = 1 },
  113. { .pin = 5, .func = 1 },
  114. { .pin = 6, .func = 1 },
  115. { .pin = 7, .func = 1 },
  116. };
  117. static const struct rza1_bidir_pin rza1h_bidir_pins_p2[] = {
  118. { .pin = 0, .func = 1 },
  119. { .pin = 1, .func = 1 },
  120. { .pin = 2, .func = 1 },
  121. { .pin = 3, .func = 1 },
  122. { .pin = 4, .func = 1 },
  123. { .pin = 0, .func = 4 },
  124. { .pin = 1, .func = 4 },
  125. { .pin = 2, .func = 4 },
  126. { .pin = 3, .func = 4 },
  127. { .pin = 5, .func = 1 },
  128. { .pin = 6, .func = 1 },
  129. { .pin = 7, .func = 1 },
  130. { .pin = 8, .func = 1 },
  131. { .pin = 9, .func = 1 },
  132. { .pin = 10, .func = 1 },
  133. { .pin = 11, .func = 1 },
  134. { .pin = 12, .func = 1 },
  135. { .pin = 13, .func = 1 },
  136. { .pin = 14, .func = 1 },
  137. { .pin = 15, .func = 1 },
  138. { .pin = 12, .func = 4 },
  139. { .pin = 13, .func = 4 },
  140. { .pin = 14, .func = 4 },
  141. { .pin = 15, .func = 4 },
  142. };
  143. static const struct rza1_bidir_pin rza1h_bidir_pins_p3[] = {
  144. { .pin = 3, .func = 2 },
  145. { .pin = 10, .func = 7 },
  146. { .pin = 11, .func = 7 },
  147. { .pin = 13, .func = 7 },
  148. { .pin = 14, .func = 7 },
  149. { .pin = 15, .func = 7 },
  150. { .pin = 10, .func = 8 },
  151. { .pin = 11, .func = 8 },
  152. { .pin = 13, .func = 8 },
  153. { .pin = 14, .func = 8 },
  154. { .pin = 15, .func = 8 },
  155. };
  156. static const struct rza1_bidir_pin rza1h_bidir_pins_p4[] = {
  157. { .pin = 0, .func = 8 },
  158. { .pin = 1, .func = 8 },
  159. { .pin = 2, .func = 8 },
  160. { .pin = 3, .func = 8 },
  161. { .pin = 10, .func = 3 },
  162. { .pin = 11, .func = 3 },
  163. { .pin = 13, .func = 3 },
  164. { .pin = 14, .func = 3 },
  165. { .pin = 15, .func = 3 },
  166. { .pin = 10, .func = 4 },
  167. { .pin = 11, .func = 4 },
  168. { .pin = 13, .func = 4 },
  169. { .pin = 14, .func = 4 },
  170. { .pin = 15, .func = 4 },
  171. { .pin = 12, .func = 5 },
  172. { .pin = 13, .func = 5 },
  173. { .pin = 14, .func = 5 },
  174. { .pin = 15, .func = 5 },
  175. };
  176. static const struct rza1_bidir_pin rza1h_bidir_pins_p6[] = {
  177. { .pin = 0, .func = 1 },
  178. { .pin = 1, .func = 1 },
  179. { .pin = 2, .func = 1 },
  180. { .pin = 3, .func = 1 },
  181. { .pin = 4, .func = 1 },
  182. { .pin = 5, .func = 1 },
  183. { .pin = 6, .func = 1 },
  184. { .pin = 7, .func = 1 },
  185. { .pin = 8, .func = 1 },
  186. { .pin = 9, .func = 1 },
  187. { .pin = 10, .func = 1 },
  188. { .pin = 11, .func = 1 },
  189. { .pin = 12, .func = 1 },
  190. { .pin = 13, .func = 1 },
  191. { .pin = 14, .func = 1 },
  192. { .pin = 15, .func = 1 },
  193. };
  194. static const struct rza1_bidir_pin rza1h_bidir_pins_p7[] = {
  195. { .pin = 13, .func = 3 },
  196. };
  197. static const struct rza1_bidir_pin rza1h_bidir_pins_p8[] = {
  198. { .pin = 8, .func = 3 },
  199. { .pin = 9, .func = 3 },
  200. { .pin = 10, .func = 3 },
  201. { .pin = 11, .func = 3 },
  202. { .pin = 14, .func = 2 },
  203. { .pin = 15, .func = 2 },
  204. { .pin = 14, .func = 3 },
  205. { .pin = 15, .func = 3 },
  206. };
  207. static const struct rza1_bidir_pin rza1h_bidir_pins_p9[] = {
  208. { .pin = 0, .func = 2 },
  209. { .pin = 1, .func = 2 },
  210. { .pin = 4, .func = 2 },
  211. { .pin = 5, .func = 2 },
  212. { .pin = 6, .func = 2 },
  213. { .pin = 7, .func = 2 },
  214. };
  215. static const struct rza1_bidir_pin rza1h_bidir_pins_p11[] = {
  216. { .pin = 6, .func = 2 },
  217. { .pin = 7, .func = 2 },
  218. { .pin = 9, .func = 2 },
  219. { .pin = 6, .func = 4 },
  220. { .pin = 7, .func = 4 },
  221. { .pin = 9, .func = 4 },
  222. { .pin = 10, .func = 2 },
  223. { .pin = 11, .func = 2 },
  224. { .pin = 10, .func = 4 },
  225. { .pin = 11, .func = 4 },
  226. { .pin = 12, .func = 4 },
  227. { .pin = 13, .func = 4 },
  228. { .pin = 14, .func = 4 },
  229. { .pin = 15, .func = 4 },
  230. };
  231. static const struct rza1_swio_pin rza1h_swio_pins[] = {
  232. { .port = 2, .pin = 7, .func = 4, .input = 0 },
  233. { .port = 2, .pin = 11, .func = 4, .input = 0 },
  234. { .port = 3, .pin = 7, .func = 3, .input = 0 },
  235. { .port = 3, .pin = 7, .func = 8, .input = 0 },
  236. { .port = 4, .pin = 7, .func = 5, .input = 0 },
  237. { .port = 4, .pin = 7, .func = 11, .input = 0 },
  238. { .port = 4, .pin = 15, .func = 6, .input = 0 },
  239. { .port = 5, .pin = 0, .func = 1, .input = 1 },
  240. { .port = 5, .pin = 1, .func = 1, .input = 1 },
  241. { .port = 5, .pin = 2, .func = 1, .input = 1 },
  242. { .port = 5, .pin = 3, .func = 1, .input = 1 },
  243. { .port = 5, .pin = 4, .func = 1, .input = 1 },
  244. { .port = 5, .pin = 5, .func = 1, .input = 1 },
  245. { .port = 5, .pin = 6, .func = 1, .input = 1 },
  246. { .port = 5, .pin = 7, .func = 1, .input = 1 },
  247. { .port = 7, .pin = 4, .func = 6, .input = 0 },
  248. { .port = 7, .pin = 11, .func = 2, .input = 0 },
  249. { .port = 8, .pin = 10, .func = 8, .input = 0 },
  250. { .port = 10, .pin = 15, .func = 2, .input = 0 },
  251. };
  252. static const struct rza1_bidir_entry rza1h_bidir_entries[RZA1_NPORTS] = {
  253. [1] = { ARRAY_SIZE(rza1h_bidir_pins_p1), rza1h_bidir_pins_p1 },
  254. [2] = { ARRAY_SIZE(rza1h_bidir_pins_p2), rza1h_bidir_pins_p2 },
  255. [3] = { ARRAY_SIZE(rza1h_bidir_pins_p3), rza1h_bidir_pins_p3 },
  256. [4] = { ARRAY_SIZE(rza1h_bidir_pins_p4), rza1h_bidir_pins_p4 },
  257. [6] = { ARRAY_SIZE(rza1h_bidir_pins_p6), rza1h_bidir_pins_p6 },
  258. [7] = { ARRAY_SIZE(rza1h_bidir_pins_p7), rza1h_bidir_pins_p7 },
  259. [8] = { ARRAY_SIZE(rza1h_bidir_pins_p8), rza1h_bidir_pins_p8 },
  260. [9] = { ARRAY_SIZE(rza1h_bidir_pins_p9), rza1h_bidir_pins_p9 },
  261. [11] = { ARRAY_SIZE(rza1h_bidir_pins_p11), rza1h_bidir_pins_p11 },
  262. };
  263. static const struct rza1_swio_entry rza1h_swio_entries[] = {
  264. [0] = { ARRAY_SIZE(rza1h_swio_pins), rza1h_swio_pins },
  265. };
  266. /* RZ/A1H (r7s72100x) pinmux flags table */
  267. static const struct rza1_pinmux_conf rza1h_pmx_conf = {
  268. .bidir_entries = rza1h_bidir_entries,
  269. .swio_entries = rza1h_swio_entries,
  270. };
  271. /* ----------------------------------------------------------------------------
  272. * RZ/A1L (r7s72102) pinmux flags
  273. */
  274. static const struct rza1_bidir_pin rza1l_bidir_pins_p1[] = {
  275. { .pin = 0, .func = 1 },
  276. { .pin = 1, .func = 1 },
  277. { .pin = 2, .func = 1 },
  278. { .pin = 3, .func = 1 },
  279. { .pin = 4, .func = 1 },
  280. { .pin = 5, .func = 1 },
  281. { .pin = 6, .func = 1 },
  282. { .pin = 7, .func = 1 },
  283. };
  284. static const struct rza1_bidir_pin rza1l_bidir_pins_p3[] = {
  285. { .pin = 0, .func = 2 },
  286. { .pin = 1, .func = 2 },
  287. { .pin = 2, .func = 2 },
  288. { .pin = 4, .func = 2 },
  289. { .pin = 5, .func = 2 },
  290. { .pin = 10, .func = 2 },
  291. { .pin = 11, .func = 2 },
  292. { .pin = 12, .func = 2 },
  293. { .pin = 13, .func = 2 },
  294. };
  295. static const struct rza1_bidir_pin rza1l_bidir_pins_p4[] = {
  296. { .pin = 1, .func = 4 },
  297. { .pin = 2, .func = 2 },
  298. { .pin = 3, .func = 2 },
  299. { .pin = 6, .func = 2 },
  300. { .pin = 7, .func = 2 },
  301. };
  302. static const struct rza1_bidir_pin rza1l_bidir_pins_p5[] = {
  303. { .pin = 0, .func = 1 },
  304. { .pin = 1, .func = 1 },
  305. { .pin = 2, .func = 1 },
  306. { .pin = 3, .func = 1 },
  307. { .pin = 4, .func = 1 },
  308. { .pin = 5, .func = 1 },
  309. { .pin = 6, .func = 1 },
  310. { .pin = 7, .func = 1 },
  311. { .pin = 8, .func = 1 },
  312. { .pin = 9, .func = 1 },
  313. { .pin = 10, .func = 1 },
  314. { .pin = 11, .func = 1 },
  315. { .pin = 12, .func = 1 },
  316. { .pin = 13, .func = 1 },
  317. { .pin = 14, .func = 1 },
  318. { .pin = 15, .func = 1 },
  319. { .pin = 0, .func = 2 },
  320. { .pin = 1, .func = 2 },
  321. { .pin = 2, .func = 2 },
  322. { .pin = 3, .func = 2 },
  323. };
  324. static const struct rza1_bidir_pin rza1l_bidir_pins_p6[] = {
  325. { .pin = 0, .func = 1 },
  326. { .pin = 1, .func = 1 },
  327. { .pin = 2, .func = 1 },
  328. { .pin = 3, .func = 1 },
  329. { .pin = 4, .func = 1 },
  330. { .pin = 5, .func = 1 },
  331. { .pin = 6, .func = 1 },
  332. { .pin = 7, .func = 1 },
  333. { .pin = 8, .func = 1 },
  334. { .pin = 9, .func = 1 },
  335. { .pin = 10, .func = 1 },
  336. { .pin = 11, .func = 1 },
  337. { .pin = 12, .func = 1 },
  338. { .pin = 13, .func = 1 },
  339. { .pin = 14, .func = 1 },
  340. { .pin = 15, .func = 1 },
  341. };
  342. static const struct rza1_bidir_pin rza1l_bidir_pins_p7[] = {
  343. { .pin = 2, .func = 2 },
  344. { .pin = 3, .func = 2 },
  345. { .pin = 5, .func = 2 },
  346. { .pin = 6, .func = 2 },
  347. { .pin = 7, .func = 2 },
  348. { .pin = 2, .func = 3 },
  349. { .pin = 3, .func = 3 },
  350. { .pin = 5, .func = 3 },
  351. { .pin = 6, .func = 3 },
  352. { .pin = 7, .func = 3 },
  353. };
  354. static const struct rza1_bidir_pin rza1l_bidir_pins_p9[] = {
  355. { .pin = 1, .func = 2 },
  356. { .pin = 0, .func = 3 },
  357. { .pin = 1, .func = 3 },
  358. { .pin = 3, .func = 3 },
  359. { .pin = 4, .func = 3 },
  360. { .pin = 5, .func = 3 },
  361. };
  362. static const struct rza1_swio_pin rza1l_swio_pins[] = {
  363. { .port = 2, .pin = 8, .func = 2, .input = 0 },
  364. { .port = 5, .pin = 6, .func = 3, .input = 0 },
  365. { .port = 6, .pin = 6, .func = 3, .input = 0 },
  366. { .port = 6, .pin = 10, .func = 3, .input = 0 },
  367. { .port = 7, .pin = 10, .func = 2, .input = 0 },
  368. { .port = 8, .pin = 2, .func = 3, .input = 0 },
  369. };
  370. static const struct rza1_bidir_entry rza1l_bidir_entries[RZA1_NPORTS] = {
  371. [1] = { ARRAY_SIZE(rza1l_bidir_pins_p1), rza1l_bidir_pins_p1 },
  372. [3] = { ARRAY_SIZE(rza1l_bidir_pins_p3), rza1l_bidir_pins_p3 },
  373. [4] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p4 },
  374. [5] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p5 },
  375. [6] = { ARRAY_SIZE(rza1l_bidir_pins_p6), rza1l_bidir_pins_p6 },
  376. [7] = { ARRAY_SIZE(rza1l_bidir_pins_p7), rza1l_bidir_pins_p7 },
  377. [9] = { ARRAY_SIZE(rza1l_bidir_pins_p9), rza1l_bidir_pins_p9 },
  378. };
  379. static const struct rza1_swio_entry rza1l_swio_entries[] = {
  380. [0] = { ARRAY_SIZE(rza1h_swio_pins), rza1h_swio_pins },
  381. };
  382. /* RZ/A1L (r7s72102x) pinmux flags table */
  383. static const struct rza1_pinmux_conf rza1l_pmx_conf = {
  384. .bidir_entries = rza1l_bidir_entries,
  385. .swio_entries = rza1l_swio_entries,
  386. };
  387. /* ----------------------------------------------------------------------------
  388. * RZ/A1 types
  389. */
  390. /**
  391. * rza1_mux_conf - describes a pin multiplexing operation
  392. *
  393. * @id: the pin identifier from 0 to RZA1_NPINS
  394. * @port: the port where pin sits on
  395. * @pin: pin id
  396. * @mux_func: alternate function id number
  397. * @mux_flags: alternate function flags
  398. * @value: output value to set the pin to
  399. */
  400. struct rza1_mux_conf {
  401. u16 id;
  402. u8 port;
  403. u8 pin;
  404. u8 mux_func;
  405. u8 mux_flags;
  406. u8 value;
  407. };
  408. /**
  409. * rza1_port - describes a pin port
  410. *
  411. * This is mostly useful to lock register writes per-bank and not globally.
  412. *
  413. * @lock: protect access to HW registers
  414. * @id: port number
  415. * @base: logical address base
  416. * @pins: pins sitting on this port
  417. */
  418. struct rza1_port {
  419. spinlock_t lock;
  420. unsigned int id;
  421. void __iomem *base;
  422. struct pinctrl_pin_desc *pins;
  423. };
  424. /**
  425. * rza1_pinctrl - RZ pincontroller device
  426. *
  427. * @dev: parent device structure
  428. * @mutex: protect [pinctrl|pinmux]_generic functions
  429. * @base: logical address base
  430. * @nports: number of pin controller ports
  431. * @ports: pin controller banks
  432. * @pins: pin array for pinctrl core
  433. * @desc: pincontroller desc for pinctrl core
  434. * @pctl: pinctrl device
  435. * @data: device specific data
  436. */
  437. struct rza1_pinctrl {
  438. struct device *dev;
  439. struct mutex mutex;
  440. void __iomem *base;
  441. unsigned int nport;
  442. struct rza1_port *ports;
  443. struct pinctrl_pin_desc *pins;
  444. struct pinctrl_desc desc;
  445. struct pinctrl_dev *pctl;
  446. const void *data;
  447. };
  448. /* ----------------------------------------------------------------------------
  449. * RZ/A1 pinmux flags
  450. */
  451. static inline bool rza1_pinmux_get_bidir(unsigned int port,
  452. unsigned int pin,
  453. unsigned int func,
  454. const struct rza1_bidir_entry *table)
  455. {
  456. const struct rza1_bidir_entry *entry = &table[port];
  457. const struct rza1_bidir_pin *bidir_pin;
  458. unsigned int i;
  459. for (i = 0; i < entry->npins; ++i) {
  460. bidir_pin = &entry->pins[i];
  461. if (bidir_pin->pin == pin && bidir_pin->func == func)
  462. return true;
  463. }
  464. return false;
  465. }
  466. static inline int rza1_pinmux_get_swio(unsigned int port,
  467. unsigned int pin,
  468. unsigned int func,
  469. const struct rza1_swio_entry *table)
  470. {
  471. const struct rza1_swio_pin *swio_pin;
  472. unsigned int i;
  473. for (i = 0; i < table->npins; ++i) {
  474. swio_pin = &table->pins[i];
  475. if (swio_pin->port == port && swio_pin->pin == pin &&
  476. swio_pin->func == func)
  477. return swio_pin->input;
  478. }
  479. return -ENOENT;
  480. }
  481. /**
  482. * rza1_pinmux_get_flags() - return pinmux flags associated to a pin
  483. */
  484. static unsigned int rza1_pinmux_get_flags(unsigned int port, unsigned int pin,
  485. unsigned int func,
  486. struct rza1_pinctrl *rza1_pctl)
  487. {
  488. const struct rza1_pinmux_conf *pmx_conf = rza1_pctl->data;
  489. const struct rza1_bidir_entry *bidir_entries = pmx_conf->bidir_entries;
  490. const struct rza1_swio_entry *swio_entries = pmx_conf->swio_entries;
  491. unsigned int pmx_flags = 0;
  492. int ret;
  493. if (rza1_pinmux_get_bidir(port, pin, func, bidir_entries))
  494. pmx_flags |= MUX_FLAGS_BIDIR;
  495. ret = rza1_pinmux_get_swio(port, pin, func, swio_entries);
  496. if (ret == 0)
  497. pmx_flags |= MUX_FLAGS_SWIO_OUTPUT;
  498. else if (ret > 0)
  499. pmx_flags |= MUX_FLAGS_SWIO_INPUT;
  500. return pmx_flags;
  501. }
  502. /* ----------------------------------------------------------------------------
  503. * RZ/A1 SoC operations
  504. */
  505. /**
  506. * rza1_set_bit() - un-locked set/clear a single bit in pin configuration
  507. * registers
  508. */
  509. static inline void rza1_set_bit(struct rza1_port *port, unsigned int reg,
  510. unsigned int bit, bool set)
  511. {
  512. void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
  513. u16 val = ioread16(mem);
  514. if (set)
  515. val |= BIT(bit);
  516. else
  517. val &= ~BIT(bit);
  518. iowrite16(val, mem);
  519. }
  520. static inline unsigned int rza1_get_bit(struct rza1_port *port,
  521. unsigned int reg, unsigned int bit)
  522. {
  523. void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
  524. return ioread16(mem) & BIT(bit);
  525. }
  526. /**
  527. * rza1_pin_reset() - reset a pin to default initial state
  528. *
  529. * Reset pin state disabling input buffer and bi-directional control,
  530. * and configure it as input port.
  531. * Note that pin is now configured with direction as input but with input
  532. * buffer disabled. This implies the pin value cannot be read in this state.
  533. *
  534. * @port: port where pin sits on
  535. * @pin: pin offset
  536. */
  537. static void rza1_pin_reset(struct rza1_port *port, unsigned int pin)
  538. {
  539. unsigned long irqflags;
  540. spin_lock_irqsave(&port->lock, irqflags);
  541. rza1_set_bit(port, RZA1_PIBC_REG, pin, 0);
  542. rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
  543. rza1_set_bit(port, RZA1_PM_REG, pin, 1);
  544. rza1_set_bit(port, RZA1_PMC_REG, pin, 0);
  545. rza1_set_bit(port, RZA1_PIPC_REG, pin, 0);
  546. spin_unlock_irqrestore(&port->lock, irqflags);
  547. }
  548. static inline int rza1_pin_get_direction(struct rza1_port *port,
  549. unsigned int pin)
  550. {
  551. unsigned long irqflags;
  552. int input;
  553. spin_lock_irqsave(&port->lock, irqflags);
  554. input = rza1_get_bit(port, RZA1_PM_REG, pin);
  555. spin_unlock_irqrestore(&port->lock, irqflags);
  556. return !!input;
  557. }
  558. /**
  559. * rza1_pin_set_direction() - set I/O direction on a pin in port mode
  560. *
  561. * When running in output port mode keep PBDC enabled to allow reading the
  562. * pin value from PPR.
  563. *
  564. * @port: port where pin sits on
  565. * @pin: pin offset
  566. * @input: input enable/disable flag
  567. */
  568. static inline void rza1_pin_set_direction(struct rza1_port *port,
  569. unsigned int pin, bool input)
  570. {
  571. unsigned long irqflags;
  572. spin_lock_irqsave(&port->lock, irqflags);
  573. rza1_set_bit(port, RZA1_PIBC_REG, pin, 1);
  574. if (input) {
  575. rza1_set_bit(port, RZA1_PM_REG, pin, 1);
  576. rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
  577. } else {
  578. rza1_set_bit(port, RZA1_PM_REG, pin, 0);
  579. rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
  580. }
  581. spin_unlock_irqrestore(&port->lock, irqflags);
  582. }
  583. static inline void rza1_pin_set(struct rza1_port *port, unsigned int pin,
  584. unsigned int value)
  585. {
  586. unsigned long irqflags;
  587. spin_lock_irqsave(&port->lock, irqflags);
  588. rza1_set_bit(port, RZA1_P_REG, pin, !!value);
  589. spin_unlock_irqrestore(&port->lock, irqflags);
  590. }
  591. static inline int rza1_pin_get(struct rza1_port *port, unsigned int pin)
  592. {
  593. unsigned long irqflags;
  594. int val;
  595. spin_lock_irqsave(&port->lock, irqflags);
  596. val = rza1_get_bit(port, RZA1_PPR_REG, pin);
  597. spin_unlock_irqrestore(&port->lock, irqflags);
  598. return val;
  599. }
  600. /**
  601. * rza1_pin_mux_single() - configure pin multiplexing on a single pin
  602. *
  603. * @pinctrl: RZ/A1 pin controller device
  604. * @mux_conf: pin multiplexing descriptor
  605. */
  606. static int rza1_pin_mux_single(struct rza1_pinctrl *rza1_pctl,
  607. struct rza1_mux_conf *mux_conf)
  608. {
  609. struct rza1_port *port = &rza1_pctl->ports[mux_conf->port];
  610. unsigned int pin = mux_conf->pin;
  611. u8 mux_func = mux_conf->mux_func;
  612. u8 mux_flags = mux_conf->mux_flags;
  613. u8 mux_flags_from_table;
  614. rza1_pin_reset(port, pin);
  615. /* SWIO pinmux flags coming from DT are high precedence */
  616. mux_flags_from_table = rza1_pinmux_get_flags(port->id, pin, mux_func,
  617. rza1_pctl);
  618. if (mux_flags)
  619. mux_flags |= (mux_flags_from_table & MUX_FLAGS_BIDIR);
  620. else
  621. mux_flags = mux_flags_from_table;
  622. if (mux_flags & MUX_FLAGS_BIDIR)
  623. rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
  624. /*
  625. * Enable alternate function mode and select it.
  626. *
  627. * Be careful here: the pin mux sub-nodes in device tree
  628. * enumerate alternate functions from 1 to 8;
  629. * subtract 1 before using macros to match registers configuration
  630. * which expects numbers from 0 to 7 instead.
  631. *
  632. * ----------------------------------------------------
  633. * Alternate mode selection table:
  634. *
  635. * PMC PFC PFCE PFCAE (mux_func - 1)
  636. * 1 0 0 0 0
  637. * 1 1 0 0 1
  638. * 1 0 1 0 2
  639. * 1 1 1 0 3
  640. * 1 0 0 1 4
  641. * 1 1 0 1 5
  642. * 1 0 1 1 6
  643. * 1 1 1 1 7
  644. * ----------------------------------------------------
  645. */
  646. mux_func -= 1;
  647. rza1_set_bit(port, RZA1_PFC_REG, pin, mux_func & MUX_FUNC_PFC_MASK);
  648. rza1_set_bit(port, RZA1_PFCE_REG, pin, mux_func & MUX_FUNC_PFCE_MASK);
  649. rza1_set_bit(port, RZA1_PFCEA_REG, pin, mux_func & MUX_FUNC_PFCEA_MASK);
  650. /*
  651. * All alternate functions except a few need PIPCn = 1.
  652. * If PIPCn has to stay disabled (SW IO mode), configure PMn according
  653. * to I/O direction specified by pin configuration -after- PMC has been
  654. * set to one.
  655. */
  656. if (mux_flags & (MUX_FLAGS_SWIO_INPUT | MUX_FLAGS_SWIO_OUTPUT))
  657. rza1_set_bit(port, RZA1_PM_REG, pin,
  658. mux_flags & MUX_FLAGS_SWIO_INPUT);
  659. else
  660. rza1_set_bit(port, RZA1_PIPC_REG, pin, 1);
  661. rza1_set_bit(port, RZA1_PMC_REG, pin, 1);
  662. return 0;
  663. }
  664. /* ----------------------------------------------------------------------------
  665. * gpio operations
  666. */
  667. /**
  668. * rza1_gpio_request() - configure pin in port mode
  669. *
  670. * Configure a pin as gpio (port mode).
  671. * After reset, the pin is in input mode with input buffer disabled.
  672. * To use the pin as input or output, set_direction shall be called first
  673. *
  674. * @chip: gpio chip where the gpio sits on
  675. * @gpio: gpio offset
  676. */
  677. static int rza1_gpio_request(struct gpio_chip *chip, unsigned int gpio)
  678. {
  679. struct rza1_port *port = gpiochip_get_data(chip);
  680. rza1_pin_reset(port, gpio);
  681. return 0;
  682. }
  683. /**
  684. * rza1_gpio_disable_free() - reset a pin
  685. *
  686. * Surprisingly, disable_free a gpio, is equivalent to request it.
  687. * Reset pin to port mode, with input buffer disabled. This overwrites all
  688. * port direction settings applied with set_direction
  689. *
  690. * @chip: gpio chip where the gpio sits on
  691. * @gpio: gpio offset
  692. */
  693. static void rza1_gpio_free(struct gpio_chip *chip, unsigned int gpio)
  694. {
  695. struct rza1_port *port = gpiochip_get_data(chip);
  696. rza1_pin_reset(port, gpio);
  697. }
  698. static int rza1_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
  699. {
  700. struct rza1_port *port = gpiochip_get_data(chip);
  701. return rza1_pin_get_direction(port, gpio);
  702. }
  703. static int rza1_gpio_direction_input(struct gpio_chip *chip,
  704. unsigned int gpio)
  705. {
  706. struct rza1_port *port = gpiochip_get_data(chip);
  707. rza1_pin_set_direction(port, gpio, true);
  708. return 0;
  709. }
  710. static int rza1_gpio_direction_output(struct gpio_chip *chip,
  711. unsigned int gpio,
  712. int value)
  713. {
  714. struct rza1_port *port = gpiochip_get_data(chip);
  715. /* Set value before driving pin direction */
  716. rza1_pin_set(port, gpio, value);
  717. rza1_pin_set_direction(port, gpio, false);
  718. return 0;
  719. }
  720. /**
  721. * rza1_gpio_get() - read a gpio pin value
  722. *
  723. * Read gpio pin value through PPR register.
  724. * Requires bi-directional mode to work when reading the value of a pin
  725. * in output mode
  726. *
  727. * @chip: gpio chip where the gpio sits on
  728. * @gpio: gpio offset
  729. */
  730. static int rza1_gpio_get(struct gpio_chip *chip, unsigned int gpio)
  731. {
  732. struct rza1_port *port = gpiochip_get_data(chip);
  733. return rza1_pin_get(port, gpio);
  734. }
  735. static void rza1_gpio_set(struct gpio_chip *chip, unsigned int gpio,
  736. int value)
  737. {
  738. struct rza1_port *port = gpiochip_get_data(chip);
  739. rza1_pin_set(port, gpio, value);
  740. }
  741. static const struct gpio_chip rza1_gpiochip_template = {
  742. .request = rza1_gpio_request,
  743. .free = rza1_gpio_free,
  744. .get_direction = rza1_gpio_get_direction,
  745. .direction_input = rza1_gpio_direction_input,
  746. .direction_output = rza1_gpio_direction_output,
  747. .get = rza1_gpio_get,
  748. .set = rza1_gpio_set,
  749. };
  750. /* ----------------------------------------------------------------------------
  751. * pinctrl operations
  752. */
  753. /**
  754. * rza1_dt_node_pin_count() - Count number of pins in a dt node or in all its
  755. * children sub-nodes
  756. *
  757. * @np: device tree node to parse
  758. */
  759. static int rza1_dt_node_pin_count(struct device_node *np)
  760. {
  761. struct device_node *child;
  762. struct property *of_pins;
  763. unsigned int npins;
  764. of_pins = of_find_property(np, "pinmux", NULL);
  765. if (of_pins)
  766. return of_pins->length / sizeof(u32);
  767. npins = 0;
  768. for_each_child_of_node(np, child) {
  769. of_pins = of_find_property(child, "pinmux", NULL);
  770. if (!of_pins)
  771. return -EINVAL;
  772. npins += of_pins->length / sizeof(u32);
  773. }
  774. return npins;
  775. }
  776. /**
  777. * rza1_parse_pmx_function() - parse a pin mux sub-node
  778. *
  779. * @rza1_pctl: RZ/A1 pin controller device
  780. * @np: of pmx sub-node
  781. * @mux_confs: array of pin mux configurations to fill with parsed info
  782. * @grpins: array of pin ids to mux
  783. */
  784. static int rza1_parse_pinmux_node(struct rza1_pinctrl *rza1_pctl,
  785. struct device_node *np,
  786. struct rza1_mux_conf *mux_confs,
  787. unsigned int *grpins)
  788. {
  789. struct pinctrl_dev *pctldev = rza1_pctl->pctl;
  790. char const *prop_name = "pinmux";
  791. unsigned long *pin_configs;
  792. unsigned int npin_configs;
  793. struct property *of_pins;
  794. unsigned int npins;
  795. u8 pinmux_flags;
  796. unsigned int i;
  797. int ret;
  798. of_pins = of_find_property(np, prop_name, NULL);
  799. if (!of_pins) {
  800. dev_dbg(rza1_pctl->dev, "Missing %s property\n", prop_name);
  801. return -ENOENT;
  802. }
  803. npins = of_pins->length / sizeof(u32);
  804. /*
  805. * Collect pin configuration properties: they apply to all pins in
  806. * this sub-node
  807. */
  808. ret = pinconf_generic_parse_dt_config(np, pctldev, &pin_configs,
  809. &npin_configs);
  810. if (ret) {
  811. dev_err(rza1_pctl->dev,
  812. "Unable to parse pin configuration options for %pOFn\n",
  813. np);
  814. return ret;
  815. }
  816. /*
  817. * Create a mask with pinmux flags from pin configuration;
  818. * very few pins (TIOC[0-4][A|B|C|D] require SWIO direction
  819. * specified in device tree.
  820. */
  821. pinmux_flags = 0;
  822. for (i = 0; i < npin_configs && pinmux_flags == 0; i++)
  823. switch (pinconf_to_config_param(pin_configs[i])) {
  824. case PIN_CONFIG_INPUT_ENABLE:
  825. pinmux_flags |= MUX_FLAGS_SWIO_INPUT;
  826. break;
  827. case PIN_CONFIG_OUTPUT:
  828. pinmux_flags |= MUX_FLAGS_SWIO_OUTPUT;
  829. default:
  830. break;
  831. }
  832. kfree(pin_configs);
  833. /* Collect pin positions and their mux settings. */
  834. for (i = 0; i < npins; ++i) {
  835. u32 of_pinconf;
  836. struct rza1_mux_conf *mux_conf = &mux_confs[i];
  837. ret = of_property_read_u32_index(np, prop_name, i, &of_pinconf);
  838. if (ret)
  839. return ret;
  840. mux_conf->id = of_pinconf & MUX_PIN_ID_MASK;
  841. mux_conf->port = RZA1_PIN_ID_TO_PORT(mux_conf->id);
  842. mux_conf->pin = RZA1_PIN_ID_TO_PIN(mux_conf->id);
  843. mux_conf->mux_func = MUX_FUNC(of_pinconf);
  844. mux_conf->mux_flags = pinmux_flags;
  845. if (mux_conf->port >= RZA1_NPORTS ||
  846. mux_conf->pin >= RZA1_PINS_PER_PORT) {
  847. dev_err(rza1_pctl->dev,
  848. "Wrong port %u pin %u for %s property\n",
  849. mux_conf->port, mux_conf->pin, prop_name);
  850. return -EINVAL;
  851. }
  852. grpins[i] = mux_conf->id;
  853. }
  854. return npins;
  855. }
  856. /**
  857. * rza1_dt_node_to_map() - map a pin mux node to a function/group
  858. *
  859. * Parse and register a pin mux function.
  860. *
  861. * @pctldev: pin controller device
  862. * @np: device tree node to parse
  863. * @map: pointer to pin map (output)
  864. * @num_maps: number of collected maps (output)
  865. */
  866. static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
  867. struct device_node *np,
  868. struct pinctrl_map **map,
  869. unsigned int *num_maps)
  870. {
  871. struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
  872. struct rza1_mux_conf *mux_confs, *mux_conf;
  873. unsigned int *grpins, *grpin;
  874. struct device_node *child;
  875. const char *grpname;
  876. const char **fngrps;
  877. int ret, npins;
  878. int gsel, fsel;
  879. npins = rza1_dt_node_pin_count(np);
  880. if (npins < 0) {
  881. dev_err(rza1_pctl->dev, "invalid pinmux node structure\n");
  882. return -EINVAL;
  883. }
  884. /*
  885. * Functions are made of 1 group only;
  886. * in fact, functions and groups are identical for this pin controller
  887. * except that functions carry an array of per-pin mux configuration
  888. * settings.
  889. */
  890. mux_confs = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*mux_confs),
  891. GFP_KERNEL);
  892. grpins = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*grpins),
  893. GFP_KERNEL);
  894. fngrps = devm_kzalloc(rza1_pctl->dev, sizeof(*fngrps), GFP_KERNEL);
  895. if (!mux_confs || !grpins || !fngrps)
  896. return -ENOMEM;
  897. /*
  898. * Parse the pinmux node.
  899. * If the node does not contain "pinmux" property (-ENOENT)
  900. * that property shall be specified in all its children sub-nodes.
  901. */
  902. mux_conf = &mux_confs[0];
  903. grpin = &grpins[0];
  904. ret = rza1_parse_pinmux_node(rza1_pctl, np, mux_conf, grpin);
  905. if (ret == -ENOENT)
  906. for_each_child_of_node(np, child) {
  907. ret = rza1_parse_pinmux_node(rza1_pctl, child, mux_conf,
  908. grpin);
  909. if (ret < 0)
  910. return ret;
  911. grpin += ret;
  912. mux_conf += ret;
  913. }
  914. else if (ret < 0)
  915. return ret;
  916. /* Register pin group and function name to pinctrl_generic */
  917. grpname = np->name;
  918. fngrps[0] = grpname;
  919. mutex_lock(&rza1_pctl->mutex);
  920. gsel = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
  921. NULL);
  922. if (gsel < 0) {
  923. mutex_unlock(&rza1_pctl->mutex);
  924. return gsel;
  925. }
  926. fsel = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
  927. mux_confs);
  928. if (fsel < 0) {
  929. ret = fsel;
  930. goto remove_group;
  931. }
  932. dev_info(rza1_pctl->dev, "Parsed function and group %s with %d pins\n",
  933. grpname, npins);
  934. /* Create map where to retrieve function and mux settings from */
  935. *num_maps = 0;
  936. *map = kzalloc(sizeof(**map), GFP_KERNEL);
  937. if (!*map) {
  938. ret = -ENOMEM;
  939. goto remove_function;
  940. }
  941. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  942. (*map)->data.mux.group = np->name;
  943. (*map)->data.mux.function = np->name;
  944. *num_maps = 1;
  945. mutex_unlock(&rza1_pctl->mutex);
  946. return 0;
  947. remove_function:
  948. pinmux_generic_remove_function(pctldev, fsel);
  949. remove_group:
  950. pinctrl_generic_remove_group(pctldev, gsel);
  951. mutex_unlock(&rza1_pctl->mutex);
  952. dev_info(rza1_pctl->dev, "Unable to parse function and group %s\n",
  953. grpname);
  954. return ret;
  955. }
  956. static void rza1_dt_free_map(struct pinctrl_dev *pctldev,
  957. struct pinctrl_map *map, unsigned int num_maps)
  958. {
  959. kfree(map);
  960. }
  961. static const struct pinctrl_ops rza1_pinctrl_ops = {
  962. .get_groups_count = pinctrl_generic_get_group_count,
  963. .get_group_name = pinctrl_generic_get_group_name,
  964. .get_group_pins = pinctrl_generic_get_group_pins,
  965. .dt_node_to_map = rza1_dt_node_to_map,
  966. .dt_free_map = rza1_dt_free_map,
  967. };
  968. /* ----------------------------------------------------------------------------
  969. * pinmux operations
  970. */
  971. /**
  972. * rza1_set_mux() - retrieve pins from a group and apply their mux settings
  973. *
  974. * @pctldev: pin controller device
  975. * @selector: function selector
  976. * @group: group selector
  977. */
  978. static int rza1_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  979. unsigned int group)
  980. {
  981. struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
  982. struct rza1_mux_conf *mux_confs;
  983. struct function_desc *func;
  984. struct group_desc *grp;
  985. int i;
  986. grp = pinctrl_generic_get_group(pctldev, group);
  987. if (!grp)
  988. return -EINVAL;
  989. func = pinmux_generic_get_function(pctldev, selector);
  990. if (!func)
  991. return -EINVAL;
  992. mux_confs = (struct rza1_mux_conf *)func->data;
  993. for (i = 0; i < grp->num_pins; ++i) {
  994. int ret;
  995. ret = rza1_pin_mux_single(rza1_pctl, &mux_confs[i]);
  996. if (ret)
  997. return ret;
  998. }
  999. return 0;
  1000. }
  1001. static const struct pinmux_ops rza1_pinmux_ops = {
  1002. .get_functions_count = pinmux_generic_get_function_count,
  1003. .get_function_name = pinmux_generic_get_function_name,
  1004. .get_function_groups = pinmux_generic_get_function_groups,
  1005. .set_mux = rza1_set_mux,
  1006. .strict = true,
  1007. };
  1008. /* ----------------------------------------------------------------------------
  1009. * RZ/A1 pin controller driver operations
  1010. */
  1011. static unsigned int rza1_count_gpio_chips(struct device_node *np)
  1012. {
  1013. struct device_node *child;
  1014. unsigned int count = 0;
  1015. for_each_child_of_node(np, child) {
  1016. if (!of_property_read_bool(child, "gpio-controller"))
  1017. continue;
  1018. count++;
  1019. }
  1020. return count;
  1021. }
  1022. /**
  1023. * rza1_parse_gpiochip() - parse and register a gpio chip and pin range
  1024. *
  1025. * The gpio controller subnode shall provide a "gpio-ranges" list property as
  1026. * defined by gpio device tree binding documentation.
  1027. *
  1028. * @rza1_pctl: RZ/A1 pin controller device
  1029. * @np: of gpio-controller node
  1030. * @chip: gpio chip to register to gpiolib
  1031. * @range: pin range to register to pinctrl core
  1032. */
  1033. static int rza1_parse_gpiochip(struct rza1_pinctrl *rza1_pctl,
  1034. struct device_node *np,
  1035. struct gpio_chip *chip,
  1036. struct pinctrl_gpio_range *range)
  1037. {
  1038. const char *list_name = "gpio-ranges";
  1039. struct of_phandle_args of_args;
  1040. unsigned int gpioport;
  1041. u32 pinctrl_base;
  1042. int ret;
  1043. ret = of_parse_phandle_with_fixed_args(np, list_name, 3, 0, &of_args);
  1044. if (ret) {
  1045. dev_err(rza1_pctl->dev, "Unable to parse %s list property\n",
  1046. list_name);
  1047. return ret;
  1048. }
  1049. /*
  1050. * Find out on which port this gpio-chip maps to by inspecting the
  1051. * second argument of the "gpio-ranges" property.
  1052. */
  1053. pinctrl_base = of_args.args[1];
  1054. gpioport = RZA1_PIN_ID_TO_PORT(pinctrl_base);
  1055. if (gpioport >= RZA1_NPORTS) {
  1056. dev_err(rza1_pctl->dev,
  1057. "Invalid values in property %s\n", list_name);
  1058. return -EINVAL;
  1059. }
  1060. *chip = rza1_gpiochip_template;
  1061. chip->base = -1;
  1062. chip->label = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL, "%pOFn",
  1063. np);
  1064. chip->ngpio = of_args.args[2];
  1065. chip->of_node = np;
  1066. chip->parent = rza1_pctl->dev;
  1067. range->id = gpioport;
  1068. range->name = chip->label;
  1069. range->pin_base = range->base = pinctrl_base;
  1070. range->npins = of_args.args[2];
  1071. range->gc = chip;
  1072. ret = devm_gpiochip_add_data(rza1_pctl->dev, chip,
  1073. &rza1_pctl->ports[gpioport]);
  1074. if (ret)
  1075. return ret;
  1076. pinctrl_add_gpio_range(rza1_pctl->pctl, range);
  1077. dev_info(rza1_pctl->dev, "Parsed gpiochip %s with %d pins\n",
  1078. chip->label, chip->ngpio);
  1079. return 0;
  1080. }
  1081. /**
  1082. * rza1_gpio_register() - parse DT to collect gpio-chips and gpio-ranges
  1083. *
  1084. * @rza1_pctl: RZ/A1 pin controller device
  1085. */
  1086. static int rza1_gpio_register(struct rza1_pinctrl *rza1_pctl)
  1087. {
  1088. struct device_node *np = rza1_pctl->dev->of_node;
  1089. struct pinctrl_gpio_range *gpio_ranges;
  1090. struct gpio_chip *gpio_chips;
  1091. struct device_node *child;
  1092. unsigned int ngpiochips;
  1093. unsigned int i;
  1094. int ret;
  1095. ngpiochips = rza1_count_gpio_chips(np);
  1096. if (ngpiochips == 0) {
  1097. dev_dbg(rza1_pctl->dev, "No gpiochip registered\n");
  1098. return 0;
  1099. }
  1100. gpio_chips = devm_kcalloc(rza1_pctl->dev, ngpiochips,
  1101. sizeof(*gpio_chips), GFP_KERNEL);
  1102. gpio_ranges = devm_kcalloc(rza1_pctl->dev, ngpiochips,
  1103. sizeof(*gpio_ranges), GFP_KERNEL);
  1104. if (!gpio_chips || !gpio_ranges)
  1105. return -ENOMEM;
  1106. i = 0;
  1107. for_each_child_of_node(np, child) {
  1108. if (!of_property_read_bool(child, "gpio-controller"))
  1109. continue;
  1110. ret = rza1_parse_gpiochip(rza1_pctl, child, &gpio_chips[i],
  1111. &gpio_ranges[i]);
  1112. if (ret)
  1113. return ret;
  1114. ++i;
  1115. }
  1116. dev_info(rza1_pctl->dev, "Registered %u gpio controllers\n", i);
  1117. return 0;
  1118. }
  1119. /**
  1120. * rza1_pinctrl_register() - Enumerate pins, ports and gpiochips; register
  1121. * them to pinctrl and gpio cores.
  1122. *
  1123. * @rza1_pctl: RZ/A1 pin controller device
  1124. */
  1125. static int rza1_pinctrl_register(struct rza1_pinctrl *rza1_pctl)
  1126. {
  1127. struct pinctrl_pin_desc *pins;
  1128. struct rza1_port *ports;
  1129. unsigned int i;
  1130. int ret;
  1131. pins = devm_kcalloc(rza1_pctl->dev, RZA1_NPINS, sizeof(*pins),
  1132. GFP_KERNEL);
  1133. ports = devm_kcalloc(rza1_pctl->dev, RZA1_NPORTS, sizeof(*ports),
  1134. GFP_KERNEL);
  1135. if (!pins || !ports)
  1136. return -ENOMEM;
  1137. rza1_pctl->pins = pins;
  1138. rza1_pctl->desc.pins = pins;
  1139. rza1_pctl->desc.npins = RZA1_NPINS;
  1140. rza1_pctl->ports = ports;
  1141. for (i = 0; i < RZA1_NPINS; ++i) {
  1142. unsigned int pin = RZA1_PIN_ID_TO_PIN(i);
  1143. unsigned int port = RZA1_PIN_ID_TO_PORT(i);
  1144. pins[i].number = i;
  1145. pins[i].name = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL,
  1146. "P%u-%u", port, pin);
  1147. if (i % RZA1_PINS_PER_PORT == 0) {
  1148. /*
  1149. * Setup ports;
  1150. * they provide per-port lock and logical base address.
  1151. */
  1152. unsigned int port_id = RZA1_PIN_ID_TO_PORT(i);
  1153. ports[port_id].id = port_id;
  1154. ports[port_id].base = rza1_pctl->base;
  1155. ports[port_id].pins = &pins[i];
  1156. spin_lock_init(&ports[port_id].lock);
  1157. }
  1158. }
  1159. ret = devm_pinctrl_register_and_init(rza1_pctl->dev, &rza1_pctl->desc,
  1160. rza1_pctl, &rza1_pctl->pctl);
  1161. if (ret) {
  1162. dev_err(rza1_pctl->dev,
  1163. "RZ/A1 pin controller registration failed\n");
  1164. return ret;
  1165. }
  1166. ret = pinctrl_enable(rza1_pctl->pctl);
  1167. if (ret) {
  1168. dev_err(rza1_pctl->dev,
  1169. "RZ/A1 pin controller failed to start\n");
  1170. return ret;
  1171. }
  1172. ret = rza1_gpio_register(rza1_pctl);
  1173. if (ret) {
  1174. dev_err(rza1_pctl->dev, "RZ/A1 GPIO registration failed\n");
  1175. return ret;
  1176. }
  1177. return 0;
  1178. }
  1179. static int rza1_pinctrl_probe(struct platform_device *pdev)
  1180. {
  1181. struct rza1_pinctrl *rza1_pctl;
  1182. struct resource *res;
  1183. int ret;
  1184. rza1_pctl = devm_kzalloc(&pdev->dev, sizeof(*rza1_pctl), GFP_KERNEL);
  1185. if (!rza1_pctl)
  1186. return -ENOMEM;
  1187. rza1_pctl->dev = &pdev->dev;
  1188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1189. rza1_pctl->base = devm_ioremap_resource(&pdev->dev, res);
  1190. if (IS_ERR(rza1_pctl->base))
  1191. return PTR_ERR(rza1_pctl->base);
  1192. mutex_init(&rza1_pctl->mutex);
  1193. platform_set_drvdata(pdev, rza1_pctl);
  1194. rza1_pctl->desc.name = DRIVER_NAME;
  1195. rza1_pctl->desc.pctlops = &rza1_pinctrl_ops;
  1196. rza1_pctl->desc.pmxops = &rza1_pinmux_ops;
  1197. rza1_pctl->desc.owner = THIS_MODULE;
  1198. rza1_pctl->data = of_device_get_match_data(&pdev->dev);
  1199. ret = rza1_pinctrl_register(rza1_pctl);
  1200. if (ret)
  1201. return ret;
  1202. dev_info(&pdev->dev,
  1203. "RZ/A1 pin controller and gpio successfully registered\n");
  1204. return 0;
  1205. }
  1206. static const struct of_device_id rza1_pinctrl_of_match[] = {
  1207. {
  1208. /* RZ/A1H, RZ/A1M */
  1209. .compatible = "renesas,r7s72100-ports",
  1210. .data = &rza1h_pmx_conf,
  1211. },
  1212. {
  1213. /* RZ/A1L */
  1214. .compatible = "renesas,r7s72102-ports",
  1215. .data = &rza1l_pmx_conf,
  1216. },
  1217. { }
  1218. };
  1219. static struct platform_driver rza1_pinctrl_driver = {
  1220. .driver = {
  1221. .name = DRIVER_NAME,
  1222. .of_match_table = rza1_pinctrl_of_match,
  1223. },
  1224. .probe = rza1_pinctrl_probe,
  1225. };
  1226. static int __init rza1_pinctrl_init(void)
  1227. {
  1228. return platform_driver_register(&rza1_pinctrl_driver);
  1229. }
  1230. core_initcall(rza1_pinctrl_init);
  1231. MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
  1232. MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
  1233. MODULE_LICENSE("GPL v2");