pinctrl-rza1.c 37 KB

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