pinctrl-rza1.c 36 KB

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