pinctrl-rza1.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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/A1 types
  276. */
  277. /**
  278. * rza1_mux_conf - describes a pin multiplexing operation
  279. *
  280. * @id: the pin identifier from 0 to RZA1_NPINS
  281. * @port: the port where pin sits on
  282. * @pin: pin id
  283. * @mux_func: alternate function id number
  284. * @mux_flags: alternate function flags
  285. * @value: output value to set the pin to
  286. */
  287. struct rza1_mux_conf {
  288. u16 id;
  289. u8 port;
  290. u8 pin;
  291. u8 mux_func;
  292. u8 mux_flags;
  293. u8 value;
  294. };
  295. /**
  296. * rza1_port - describes a pin port
  297. *
  298. * This is mostly useful to lock register writes per-bank and not globally.
  299. *
  300. * @lock: protect access to HW registers
  301. * @id: port number
  302. * @base: logical address base
  303. * @pins: pins sitting on this port
  304. */
  305. struct rza1_port {
  306. spinlock_t lock;
  307. unsigned int id;
  308. void __iomem *base;
  309. struct pinctrl_pin_desc *pins;
  310. };
  311. /**
  312. * rza1_pinctrl - RZ pincontroller device
  313. *
  314. * @dev: parent device structure
  315. * @mutex: protect [pinctrl|pinmux]_generic functions
  316. * @base: logical address base
  317. * @nports: number of pin controller ports
  318. * @ports: pin controller banks
  319. * @pins: pin array for pinctrl core
  320. * @desc: pincontroller desc for pinctrl core
  321. * @pctl: pinctrl device
  322. * @data: device specific data
  323. */
  324. struct rza1_pinctrl {
  325. struct device *dev;
  326. struct mutex mutex;
  327. void __iomem *base;
  328. unsigned int nport;
  329. struct rza1_port *ports;
  330. struct pinctrl_pin_desc *pins;
  331. struct pinctrl_desc desc;
  332. struct pinctrl_dev *pctl;
  333. const void *data;
  334. };
  335. /* ----------------------------------------------------------------------------
  336. * RZ/A1 pinmux flags
  337. */
  338. static inline bool rza1_pinmux_get_bidir(unsigned int port,
  339. unsigned int pin,
  340. unsigned int func,
  341. const struct rza1_bidir_entry *table)
  342. {
  343. const struct rza1_bidir_entry *entry = &table[port];
  344. const struct rza1_bidir_pin *bidir_pin;
  345. unsigned int i;
  346. for (i = 0; i < entry->npins; ++i) {
  347. bidir_pin = &entry->pins[i];
  348. if (bidir_pin->pin == pin && bidir_pin->func == func)
  349. return true;
  350. }
  351. return false;
  352. }
  353. static inline int rza1_pinmux_get_swio(unsigned int port,
  354. unsigned int pin,
  355. unsigned int func,
  356. const struct rza1_swio_entry *table)
  357. {
  358. const struct rza1_swio_pin *swio_pin;
  359. unsigned int i;
  360. for (i = 0; i < table->npins; ++i) {
  361. swio_pin = &table->pins[i];
  362. if (swio_pin->port == port && swio_pin->pin == pin &&
  363. swio_pin->func == func)
  364. return swio_pin->input;
  365. }
  366. return -ENOENT;
  367. }
  368. /**
  369. * rza1_pinmux_get_flags() - return pinmux flags associated to a pin
  370. */
  371. static unsigned int rza1_pinmux_get_flags(unsigned int port, unsigned int pin,
  372. unsigned int func,
  373. struct rza1_pinctrl *rza1_pctl)
  374. {
  375. const struct rza1_pinmux_conf *pmx_conf = rza1_pctl->data;
  376. const struct rza1_bidir_entry *bidir_entries = pmx_conf->bidir_entries;
  377. const struct rza1_swio_entry *swio_entries = pmx_conf->swio_entries;
  378. unsigned int pmx_flags = 0;
  379. int ret;
  380. if (rza1_pinmux_get_bidir(port, pin, func, bidir_entries))
  381. pmx_flags |= MUX_FLAGS_BIDIR;
  382. ret = rza1_pinmux_get_swio(port, pin, func, swio_entries);
  383. if (ret == 0)
  384. pmx_flags |= MUX_FLAGS_SWIO_OUTPUT;
  385. else if (ret > 0)
  386. pmx_flags |= MUX_FLAGS_SWIO_INPUT;
  387. return pmx_flags;
  388. }
  389. /* ----------------------------------------------------------------------------
  390. * RZ/A1 SoC operations
  391. */
  392. /**
  393. * rza1_set_bit() - un-locked set/clear a single bit in pin configuration
  394. * registers
  395. */
  396. static inline void rza1_set_bit(struct rza1_port *port, unsigned int reg,
  397. unsigned int bit, bool set)
  398. {
  399. void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
  400. u16 val = ioread16(mem);
  401. if (set)
  402. val |= BIT(bit);
  403. else
  404. val &= ~BIT(bit);
  405. iowrite16(val, mem);
  406. }
  407. static inline unsigned int rza1_get_bit(struct rza1_port *port,
  408. unsigned int reg, unsigned int bit)
  409. {
  410. void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
  411. return ioread16(mem) & BIT(bit);
  412. }
  413. /**
  414. * rza1_pin_reset() - reset a pin to default initial state
  415. *
  416. * Reset pin state disabling input buffer and bi-directional control,
  417. * and configure it as input port.
  418. * Note that pin is now configured with direction as input but with input
  419. * buffer disabled. This implies the pin value cannot be read in this state.
  420. *
  421. * @port: port where pin sits on
  422. * @pin: pin offset
  423. */
  424. static void rza1_pin_reset(struct rza1_port *port, unsigned int pin)
  425. {
  426. unsigned long irqflags;
  427. spin_lock_irqsave(&port->lock, irqflags);
  428. rza1_set_bit(port, RZA1_PIBC_REG, pin, 0);
  429. rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
  430. rza1_set_bit(port, RZA1_PM_REG, pin, 1);
  431. rza1_set_bit(port, RZA1_PMC_REG, pin, 0);
  432. rza1_set_bit(port, RZA1_PIPC_REG, pin, 0);
  433. spin_unlock_irqrestore(&port->lock, irqflags);
  434. }
  435. static inline int rza1_pin_get_direction(struct rza1_port *port,
  436. unsigned int pin)
  437. {
  438. unsigned long irqflags;
  439. int input;
  440. spin_lock_irqsave(&port->lock, irqflags);
  441. input = rza1_get_bit(port, RZA1_PM_REG, pin);
  442. spin_unlock_irqrestore(&port->lock, irqflags);
  443. return !!input;
  444. }
  445. /**
  446. * rza1_pin_set_direction() - set I/O direction on a pin in port mode
  447. *
  448. * When running in output port mode keep PBDC enabled to allow reading the
  449. * pin value from PPR.
  450. *
  451. * @port: port where pin sits on
  452. * @pin: pin offset
  453. * @input: input enable/disable flag
  454. */
  455. static inline void rza1_pin_set_direction(struct rza1_port *port,
  456. unsigned int pin, bool input)
  457. {
  458. unsigned long irqflags;
  459. spin_lock_irqsave(&port->lock, irqflags);
  460. rza1_set_bit(port, RZA1_PIBC_REG, pin, 1);
  461. if (input) {
  462. rza1_set_bit(port, RZA1_PM_REG, pin, 1);
  463. rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
  464. } else {
  465. rza1_set_bit(port, RZA1_PM_REG, pin, 0);
  466. rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
  467. }
  468. spin_unlock_irqrestore(&port->lock, irqflags);
  469. }
  470. static inline void rza1_pin_set(struct rza1_port *port, unsigned int pin,
  471. unsigned int value)
  472. {
  473. unsigned long irqflags;
  474. spin_lock_irqsave(&port->lock, irqflags);
  475. rza1_set_bit(port, RZA1_P_REG, pin, !!value);
  476. spin_unlock_irqrestore(&port->lock, irqflags);
  477. }
  478. static inline int rza1_pin_get(struct rza1_port *port, unsigned int pin)
  479. {
  480. unsigned long irqflags;
  481. int val;
  482. spin_lock_irqsave(&port->lock, irqflags);
  483. val = rza1_get_bit(port, RZA1_PPR_REG, pin);
  484. spin_unlock_irqrestore(&port->lock, irqflags);
  485. return val;
  486. }
  487. /**
  488. * rza1_pin_mux_single() - configure pin multiplexing on a single pin
  489. *
  490. * @pinctrl: RZ/A1 pin controller device
  491. * @mux_conf: pin multiplexing descriptor
  492. */
  493. static int rza1_pin_mux_single(struct rza1_pinctrl *rza1_pctl,
  494. struct rza1_mux_conf *mux_conf)
  495. {
  496. struct rza1_port *port = &rza1_pctl->ports[mux_conf->port];
  497. unsigned int pin = mux_conf->pin;
  498. u8 mux_func = mux_conf->mux_func;
  499. u8 mux_flags = mux_conf->mux_flags;
  500. u8 mux_flags_from_table;
  501. rza1_pin_reset(port, pin);
  502. /* SWIO pinmux flags coming from DT are high precedence */
  503. mux_flags_from_table = rza1_pinmux_get_flags(port->id, pin, mux_func,
  504. rza1_pctl);
  505. if (mux_flags)
  506. mux_flags |= (mux_flags_from_table & MUX_FLAGS_BIDIR);
  507. else
  508. mux_flags = mux_flags_from_table;
  509. if (mux_flags & MUX_FLAGS_BIDIR)
  510. rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
  511. /*
  512. * Enable alternate function mode and select it.
  513. *
  514. * Be careful here: the pin mux sub-nodes in device tree
  515. * enumerate alternate functions from 1 to 8;
  516. * subtract 1 before using macros to match registers configuration
  517. * which expects numbers from 0 to 7 instead.
  518. *
  519. * ----------------------------------------------------
  520. * Alternate mode selection table:
  521. *
  522. * PMC PFC PFCE PFCAE (mux_func - 1)
  523. * 1 0 0 0 0
  524. * 1 1 0 0 1
  525. * 1 0 1 0 2
  526. * 1 1 1 0 3
  527. * 1 0 0 1 4
  528. * 1 1 0 1 5
  529. * 1 0 1 1 6
  530. * 1 1 1 1 7
  531. * ----------------------------------------------------
  532. */
  533. mux_func -= 1;
  534. rza1_set_bit(port, RZA1_PFC_REG, pin, mux_func & MUX_FUNC_PFC_MASK);
  535. rza1_set_bit(port, RZA1_PFCE_REG, pin, mux_func & MUX_FUNC_PFCE_MASK);
  536. rza1_set_bit(port, RZA1_PFCEA_REG, pin, mux_func & MUX_FUNC_PFCEA_MASK);
  537. /*
  538. * All alternate functions except a few need PIPCn = 1.
  539. * If PIPCn has to stay disabled (SW IO mode), configure PMn according
  540. * to I/O direction specified by pin configuration -after- PMC has been
  541. * set to one.
  542. */
  543. if (mux_flags & (MUX_FLAGS_SWIO_INPUT | MUX_FLAGS_SWIO_OUTPUT))
  544. rza1_set_bit(port, RZA1_PM_REG, pin,
  545. mux_flags & MUX_FLAGS_SWIO_INPUT);
  546. else
  547. rza1_set_bit(port, RZA1_PIPC_REG, pin, 1);
  548. rza1_set_bit(port, RZA1_PMC_REG, pin, 1);
  549. return 0;
  550. }
  551. /* ----------------------------------------------------------------------------
  552. * gpio operations
  553. */
  554. /**
  555. * rza1_gpio_request() - configure pin in port mode
  556. *
  557. * Configure a pin as gpio (port mode).
  558. * After reset, the pin is in input mode with input buffer disabled.
  559. * To use the pin as input or output, set_direction shall be called first
  560. *
  561. * @chip: gpio chip where the gpio sits on
  562. * @gpio: gpio offset
  563. */
  564. static int rza1_gpio_request(struct gpio_chip *chip, unsigned int gpio)
  565. {
  566. struct rza1_port *port = gpiochip_get_data(chip);
  567. rza1_pin_reset(port, gpio);
  568. return 0;
  569. }
  570. /**
  571. * rza1_gpio_disable_free() - reset a pin
  572. *
  573. * Surprisingly, disable_free a gpio, is equivalent to request it.
  574. * Reset pin to port mode, with input buffer disabled. This overwrites all
  575. * port direction settings applied with set_direction
  576. *
  577. * @chip: gpio chip where the gpio sits on
  578. * @gpio: gpio offset
  579. */
  580. static void rza1_gpio_free(struct gpio_chip *chip, unsigned int gpio)
  581. {
  582. struct rza1_port *port = gpiochip_get_data(chip);
  583. rza1_pin_reset(port, gpio);
  584. }
  585. static int rza1_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
  586. {
  587. struct rza1_port *port = gpiochip_get_data(chip);
  588. return rza1_pin_get_direction(port, gpio);
  589. }
  590. static int rza1_gpio_direction_input(struct gpio_chip *chip,
  591. unsigned int gpio)
  592. {
  593. struct rza1_port *port = gpiochip_get_data(chip);
  594. rza1_pin_set_direction(port, gpio, true);
  595. return 0;
  596. }
  597. static int rza1_gpio_direction_output(struct gpio_chip *chip,
  598. unsigned int gpio,
  599. int value)
  600. {
  601. struct rza1_port *port = gpiochip_get_data(chip);
  602. /* Set value before driving pin direction */
  603. rza1_pin_set(port, gpio, value);
  604. rza1_pin_set_direction(port, gpio, false);
  605. return 0;
  606. }
  607. /**
  608. * rza1_gpio_get() - read a gpio pin value
  609. *
  610. * Read gpio pin value through PPR register.
  611. * Requires bi-directional mode to work when reading the value of a pin
  612. * in output mode
  613. *
  614. * @chip: gpio chip where the gpio sits on
  615. * @gpio: gpio offset
  616. */
  617. static int rza1_gpio_get(struct gpio_chip *chip, unsigned int gpio)
  618. {
  619. struct rza1_port *port = gpiochip_get_data(chip);
  620. return rza1_pin_get(port, gpio);
  621. }
  622. static void rza1_gpio_set(struct gpio_chip *chip, unsigned int gpio,
  623. int value)
  624. {
  625. struct rza1_port *port = gpiochip_get_data(chip);
  626. rza1_pin_set(port, gpio, value);
  627. }
  628. static const struct gpio_chip rza1_gpiochip_template = {
  629. .request = rza1_gpio_request,
  630. .free = rza1_gpio_free,
  631. .get_direction = rza1_gpio_get_direction,
  632. .direction_input = rza1_gpio_direction_input,
  633. .direction_output = rza1_gpio_direction_output,
  634. .get = rza1_gpio_get,
  635. .set = rza1_gpio_set,
  636. };
  637. /* ----------------------------------------------------------------------------
  638. * pinctrl operations
  639. */
  640. /**
  641. * rza1_dt_node_pin_count() - Count number of pins in a dt node or in all its
  642. * children sub-nodes
  643. *
  644. * @np: device tree node to parse
  645. */
  646. static int rza1_dt_node_pin_count(struct device_node *np)
  647. {
  648. struct device_node *child;
  649. struct property *of_pins;
  650. unsigned int npins;
  651. of_pins = of_find_property(np, "pinmux", NULL);
  652. if (of_pins)
  653. return of_pins->length / sizeof(u32);
  654. npins = 0;
  655. for_each_child_of_node(np, child) {
  656. of_pins = of_find_property(child, "pinmux", NULL);
  657. if (!of_pins)
  658. return -EINVAL;
  659. npins += of_pins->length / sizeof(u32);
  660. }
  661. return npins;
  662. }
  663. /**
  664. * rza1_parse_pmx_function() - parse a pin mux sub-node
  665. *
  666. * @rza1_pctl: RZ/A1 pin controller device
  667. * @np: of pmx sub-node
  668. * @mux_confs: array of pin mux configurations to fill with parsed info
  669. * @grpins: array of pin ids to mux
  670. */
  671. static int rza1_parse_pinmux_node(struct rza1_pinctrl *rza1_pctl,
  672. struct device_node *np,
  673. struct rza1_mux_conf *mux_confs,
  674. unsigned int *grpins)
  675. {
  676. struct pinctrl_dev *pctldev = rza1_pctl->pctl;
  677. char const *prop_name = "pinmux";
  678. unsigned long *pin_configs;
  679. unsigned int npin_configs;
  680. struct property *of_pins;
  681. unsigned int npins;
  682. u8 pinmux_flags;
  683. unsigned int i;
  684. int ret;
  685. of_pins = of_find_property(np, prop_name, NULL);
  686. if (!of_pins) {
  687. dev_dbg(rza1_pctl->dev, "Missing %s property\n", prop_name);
  688. return -ENOENT;
  689. }
  690. npins = of_pins->length / sizeof(u32);
  691. /*
  692. * Collect pin configuration properties: they apply to all pins in
  693. * this sub-node
  694. */
  695. ret = pinconf_generic_parse_dt_config(np, pctldev, &pin_configs,
  696. &npin_configs);
  697. if (ret) {
  698. dev_err(rza1_pctl->dev,
  699. "Unable to parse pin configuration options for %s\n",
  700. np->name);
  701. return ret;
  702. }
  703. /*
  704. * Create a mask with pinmux flags from pin configuration;
  705. * very few pins (TIOC[0-4][A|B|C|D] require SWIO direction
  706. * specified in device tree.
  707. */
  708. pinmux_flags = 0;
  709. for (i = 0; i < npin_configs && pinmux_flags == 0; i++)
  710. switch (pinconf_to_config_param(pin_configs[i])) {
  711. case PIN_CONFIG_INPUT_ENABLE:
  712. pinmux_flags |= MUX_FLAGS_SWIO_INPUT;
  713. break;
  714. case PIN_CONFIG_OUTPUT:
  715. pinmux_flags |= MUX_FLAGS_SWIO_OUTPUT;
  716. default:
  717. break;
  718. }
  719. kfree(pin_configs);
  720. /* Collect pin positions and their mux settings. */
  721. for (i = 0; i < npins; ++i) {
  722. u32 of_pinconf;
  723. struct rza1_mux_conf *mux_conf = &mux_confs[i];
  724. ret = of_property_read_u32_index(np, prop_name, i, &of_pinconf);
  725. if (ret)
  726. return ret;
  727. mux_conf->id = of_pinconf & MUX_PIN_ID_MASK;
  728. mux_conf->port = RZA1_PIN_ID_TO_PORT(mux_conf->id);
  729. mux_conf->pin = RZA1_PIN_ID_TO_PIN(mux_conf->id);
  730. mux_conf->mux_func = MUX_FUNC(of_pinconf);
  731. mux_conf->mux_flags = pinmux_flags;
  732. if (mux_conf->port >= RZA1_NPORTS ||
  733. mux_conf->pin >= RZA1_PINS_PER_PORT) {
  734. dev_err(rza1_pctl->dev,
  735. "Wrong port %u pin %u for %s property\n",
  736. mux_conf->port, mux_conf->pin, prop_name);
  737. return -EINVAL;
  738. }
  739. grpins[i] = mux_conf->id;
  740. }
  741. return npins;
  742. }
  743. /**
  744. * rza1_dt_node_to_map() - map a pin mux node to a function/group
  745. *
  746. * Parse and register a pin mux function.
  747. *
  748. * @pctldev: pin controller device
  749. * @np: device tree node to parse
  750. * @map: pointer to pin map (output)
  751. * @num_maps: number of collected maps (output)
  752. */
  753. static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
  754. struct device_node *np,
  755. struct pinctrl_map **map,
  756. unsigned int *num_maps)
  757. {
  758. struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
  759. struct rza1_mux_conf *mux_confs, *mux_conf;
  760. unsigned int *grpins, *grpin;
  761. struct device_node *child;
  762. const char *grpname;
  763. const char **fngrps;
  764. int ret, npins;
  765. npins = rza1_dt_node_pin_count(np);
  766. if (npins < 0) {
  767. dev_err(rza1_pctl->dev, "invalid pinmux node structure\n");
  768. return -EINVAL;
  769. }
  770. /*
  771. * Functions are made of 1 group only;
  772. * in fact, functions and groups are identical for this pin controller
  773. * except that functions carry an array of per-pin mux configuration
  774. * settings.
  775. */
  776. mux_confs = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*mux_confs),
  777. GFP_KERNEL);
  778. grpins = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*grpins),
  779. GFP_KERNEL);
  780. fngrps = devm_kzalloc(rza1_pctl->dev, sizeof(*fngrps), GFP_KERNEL);
  781. if (!mux_confs || !grpins || !fngrps)
  782. return -ENOMEM;
  783. /*
  784. * Parse the pinmux node.
  785. * If the node does not contain "pinmux" property (-ENOENT)
  786. * that property shall be specified in all its children sub-nodes.
  787. */
  788. mux_conf = &mux_confs[0];
  789. grpin = &grpins[0];
  790. ret = rza1_parse_pinmux_node(rza1_pctl, np, mux_conf, grpin);
  791. if (ret == -ENOENT)
  792. for_each_child_of_node(np, child) {
  793. ret = rza1_parse_pinmux_node(rza1_pctl, child, mux_conf,
  794. grpin);
  795. if (ret < 0)
  796. return ret;
  797. grpin += ret;
  798. mux_conf += ret;
  799. }
  800. else if (ret < 0)
  801. return ret;
  802. /* Register pin group and function name to pinctrl_generic */
  803. grpname = np->name;
  804. fngrps[0] = grpname;
  805. mutex_lock(&rza1_pctl->mutex);
  806. ret = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
  807. NULL);
  808. if (ret) {
  809. mutex_unlock(&rza1_pctl->mutex);
  810. return ret;
  811. }
  812. ret = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
  813. mux_confs);
  814. if (ret)
  815. goto remove_group;
  816. mutex_unlock(&rza1_pctl->mutex);
  817. dev_info(rza1_pctl->dev, "Parsed function and group %s with %d pins\n",
  818. grpname, npins);
  819. /* Create map where to retrieve function and mux settings from */
  820. *num_maps = 0;
  821. *map = kzalloc(sizeof(**map), GFP_KERNEL);
  822. if (!*map) {
  823. ret = -ENOMEM;
  824. goto remove_function;
  825. }
  826. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  827. (*map)->data.mux.group = np->name;
  828. (*map)->data.mux.function = np->name;
  829. *num_maps = 1;
  830. return 0;
  831. remove_function:
  832. mutex_lock(&rza1_pctl->mutex);
  833. pinmux_generic_remove_last_function(pctldev);
  834. remove_group:
  835. pinctrl_generic_remove_last_group(pctldev);
  836. mutex_unlock(&rza1_pctl->mutex);
  837. dev_info(rza1_pctl->dev, "Unable to parse function and group %s\n",
  838. grpname);
  839. return ret;
  840. }
  841. static void rza1_dt_free_map(struct pinctrl_dev *pctldev,
  842. struct pinctrl_map *map, unsigned int num_maps)
  843. {
  844. kfree(map);
  845. }
  846. static const struct pinctrl_ops rza1_pinctrl_ops = {
  847. .get_groups_count = pinctrl_generic_get_group_count,
  848. .get_group_name = pinctrl_generic_get_group_name,
  849. .get_group_pins = pinctrl_generic_get_group_pins,
  850. .dt_node_to_map = rza1_dt_node_to_map,
  851. .dt_free_map = rza1_dt_free_map,
  852. };
  853. /* ----------------------------------------------------------------------------
  854. * pinmux operations
  855. */
  856. /**
  857. * rza1_set_mux() - retrieve pins from a group and apply their mux settings
  858. *
  859. * @pctldev: pin controller device
  860. * @selector: function selector
  861. * @group: group selector
  862. */
  863. static int rza1_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  864. unsigned int group)
  865. {
  866. struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
  867. struct rza1_mux_conf *mux_confs;
  868. struct function_desc *func;
  869. struct group_desc *grp;
  870. int i;
  871. grp = pinctrl_generic_get_group(pctldev, group);
  872. if (!grp)
  873. return -EINVAL;
  874. func = pinmux_generic_get_function(pctldev, selector);
  875. if (!func)
  876. return -EINVAL;
  877. mux_confs = (struct rza1_mux_conf *)func->data;
  878. for (i = 0; i < grp->num_pins; ++i) {
  879. int ret;
  880. ret = rza1_pin_mux_single(rza1_pctl, &mux_confs[i]);
  881. if (ret)
  882. return ret;
  883. }
  884. return 0;
  885. }
  886. static const struct pinmux_ops rza1_pinmux_ops = {
  887. .get_functions_count = pinmux_generic_get_function_count,
  888. .get_function_name = pinmux_generic_get_function_name,
  889. .get_function_groups = pinmux_generic_get_function_groups,
  890. .set_mux = rza1_set_mux,
  891. .strict = true,
  892. };
  893. /* ----------------------------------------------------------------------------
  894. * RZ/A1 pin controller driver operations
  895. */
  896. static unsigned int rza1_count_gpio_chips(struct device_node *np)
  897. {
  898. struct device_node *child;
  899. unsigned int count = 0;
  900. for_each_child_of_node(np, child) {
  901. if (!of_property_read_bool(child, "gpio-controller"))
  902. continue;
  903. count++;
  904. }
  905. return count;
  906. }
  907. /**
  908. * rza1_parse_gpiochip() - parse and register a gpio chip and pin range
  909. *
  910. * The gpio controller subnode shall provide a "gpio-ranges" list property as
  911. * defined by gpio device tree binding documentation.
  912. *
  913. * @rza1_pctl: RZ/A1 pin controller device
  914. * @np: of gpio-controller node
  915. * @chip: gpio chip to register to gpiolib
  916. * @range: pin range to register to pinctrl core
  917. */
  918. static int rza1_parse_gpiochip(struct rza1_pinctrl *rza1_pctl,
  919. struct device_node *np,
  920. struct gpio_chip *chip,
  921. struct pinctrl_gpio_range *range)
  922. {
  923. const char *list_name = "gpio-ranges";
  924. struct of_phandle_args of_args;
  925. unsigned int gpioport;
  926. u32 pinctrl_base;
  927. int ret;
  928. ret = of_parse_phandle_with_fixed_args(np, list_name, 3, 0, &of_args);
  929. if (ret) {
  930. dev_err(rza1_pctl->dev, "Unable to parse %s list property\n",
  931. list_name);
  932. return ret;
  933. }
  934. /*
  935. * Find out on which port this gpio-chip maps to by inspecting the
  936. * second argument of the "gpio-ranges" property.
  937. */
  938. pinctrl_base = of_args.args[1];
  939. gpioport = RZA1_PIN_ID_TO_PORT(pinctrl_base);
  940. if (gpioport >= RZA1_NPORTS) {
  941. dev_err(rza1_pctl->dev,
  942. "Invalid values in property %s\n", list_name);
  943. return -EINVAL;
  944. }
  945. *chip = rza1_gpiochip_template;
  946. chip->base = -1;
  947. chip->label = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL, "%s",
  948. np->name);
  949. chip->ngpio = of_args.args[2];
  950. chip->of_node = np;
  951. chip->parent = rza1_pctl->dev;
  952. range->id = gpioport;
  953. range->name = chip->label;
  954. range->pin_base = range->base = pinctrl_base;
  955. range->npins = of_args.args[2];
  956. range->gc = chip;
  957. ret = devm_gpiochip_add_data(rza1_pctl->dev, chip,
  958. &rza1_pctl->ports[gpioport]);
  959. if (ret)
  960. return ret;
  961. pinctrl_add_gpio_range(rza1_pctl->pctl, range);
  962. dev_info(rza1_pctl->dev, "Parsed gpiochip %s with %d pins\n",
  963. chip->label, chip->ngpio);
  964. return 0;
  965. }
  966. /**
  967. * rza1_gpio_register() - parse DT to collect gpio-chips and gpio-ranges
  968. *
  969. * @rza1_pctl: RZ/A1 pin controller device
  970. */
  971. static int rza1_gpio_register(struct rza1_pinctrl *rza1_pctl)
  972. {
  973. struct device_node *np = rza1_pctl->dev->of_node;
  974. struct pinctrl_gpio_range *gpio_ranges;
  975. struct gpio_chip *gpio_chips;
  976. struct device_node *child;
  977. unsigned int ngpiochips;
  978. unsigned int i;
  979. int ret;
  980. ngpiochips = rza1_count_gpio_chips(np);
  981. if (ngpiochips == 0) {
  982. dev_dbg(rza1_pctl->dev, "No gpiochip registered\n");
  983. return 0;
  984. }
  985. gpio_chips = devm_kcalloc(rza1_pctl->dev, ngpiochips,
  986. sizeof(*gpio_chips), GFP_KERNEL);
  987. gpio_ranges = devm_kcalloc(rza1_pctl->dev, ngpiochips,
  988. sizeof(*gpio_ranges), GFP_KERNEL);
  989. if (!gpio_chips || !gpio_ranges)
  990. return -ENOMEM;
  991. i = 0;
  992. for_each_child_of_node(np, child) {
  993. if (!of_property_read_bool(child, "gpio-controller"))
  994. continue;
  995. ret = rza1_parse_gpiochip(rza1_pctl, child, &gpio_chips[i],
  996. &gpio_ranges[i]);
  997. if (ret)
  998. goto gpiochip_remove;
  999. ++i;
  1000. }
  1001. dev_info(rza1_pctl->dev, "Registered %u gpio controllers\n", i);
  1002. return 0;
  1003. gpiochip_remove:
  1004. for (; i > 0; i--)
  1005. devm_gpiochip_remove(rza1_pctl->dev, &gpio_chips[i - 1]);
  1006. return ret;
  1007. }
  1008. /**
  1009. * rza1_pinctrl_register() - Enumerate pins, ports and gpiochips; register
  1010. * them to pinctrl and gpio cores.
  1011. *
  1012. * @rza1_pctl: RZ/A1 pin controller device
  1013. */
  1014. static int rza1_pinctrl_register(struct rza1_pinctrl *rza1_pctl)
  1015. {
  1016. struct pinctrl_pin_desc *pins;
  1017. struct rza1_port *ports;
  1018. unsigned int i;
  1019. int ret;
  1020. pins = devm_kcalloc(rza1_pctl->dev, RZA1_NPINS, sizeof(*pins),
  1021. GFP_KERNEL);
  1022. ports = devm_kcalloc(rza1_pctl->dev, RZA1_NPORTS, sizeof(*ports),
  1023. GFP_KERNEL);
  1024. if (!pins || !ports)
  1025. return -ENOMEM;
  1026. rza1_pctl->pins = pins;
  1027. rza1_pctl->desc.pins = pins;
  1028. rza1_pctl->desc.npins = RZA1_NPINS;
  1029. rza1_pctl->ports = ports;
  1030. for (i = 0; i < RZA1_NPINS; ++i) {
  1031. unsigned int pin = RZA1_PIN_ID_TO_PIN(i);
  1032. unsigned int port = RZA1_PIN_ID_TO_PORT(i);
  1033. pins[i].number = i;
  1034. pins[i].name = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL,
  1035. "P%u-%u", port, pin);
  1036. if (i % RZA1_PINS_PER_PORT == 0) {
  1037. /*
  1038. * Setup ports;
  1039. * they provide per-port lock and logical base address.
  1040. */
  1041. unsigned int port_id = RZA1_PIN_ID_TO_PORT(i);
  1042. ports[port_id].id = port_id;
  1043. ports[port_id].base = rza1_pctl->base;
  1044. ports[port_id].pins = &pins[i];
  1045. spin_lock_init(&ports[port_id].lock);
  1046. }
  1047. }
  1048. ret = devm_pinctrl_register_and_init(rza1_pctl->dev, &rza1_pctl->desc,
  1049. rza1_pctl, &rza1_pctl->pctl);
  1050. if (ret) {
  1051. dev_err(rza1_pctl->dev,
  1052. "RZ/A1 pin controller registration failed\n");
  1053. return ret;
  1054. }
  1055. ret = pinctrl_enable(rza1_pctl->pctl);
  1056. if (ret) {
  1057. dev_err(rza1_pctl->dev,
  1058. "RZ/A1 pin controller failed to start\n");
  1059. return ret;
  1060. }
  1061. ret = rza1_gpio_register(rza1_pctl);
  1062. if (ret) {
  1063. dev_err(rza1_pctl->dev, "RZ/A1 GPIO registration failed\n");
  1064. return ret;
  1065. }
  1066. return 0;
  1067. }
  1068. static int rza1_pinctrl_probe(struct platform_device *pdev)
  1069. {
  1070. struct rza1_pinctrl *rza1_pctl;
  1071. struct resource *res;
  1072. int ret;
  1073. rza1_pctl = devm_kzalloc(&pdev->dev, sizeof(*rza1_pctl), GFP_KERNEL);
  1074. if (!rza1_pctl)
  1075. return -ENOMEM;
  1076. rza1_pctl->dev = &pdev->dev;
  1077. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1078. rza1_pctl->base = devm_ioremap_resource(&pdev->dev, res);
  1079. if (IS_ERR(rza1_pctl->base))
  1080. return PTR_ERR(rza1_pctl->base);
  1081. mutex_init(&rza1_pctl->mutex);
  1082. platform_set_drvdata(pdev, rza1_pctl);
  1083. rza1_pctl->desc.name = DRIVER_NAME;
  1084. rza1_pctl->desc.pctlops = &rza1_pinctrl_ops;
  1085. rza1_pctl->desc.pmxops = &rza1_pinmux_ops;
  1086. rza1_pctl->desc.owner = THIS_MODULE;
  1087. rza1_pctl->data = of_device_get_match_data(&pdev->dev);
  1088. ret = rza1_pinctrl_register(rza1_pctl);
  1089. if (ret)
  1090. return ret;
  1091. dev_info(&pdev->dev,
  1092. "RZ/A1 pin controller and gpio successfully registered\n");
  1093. return 0;
  1094. }
  1095. static const struct of_device_id rza1_pinctrl_of_match[] = {
  1096. {
  1097. .compatible = "renesas,r7s72100-ports",
  1098. .data = &rza1h_pmx_conf,
  1099. },
  1100. { }
  1101. };
  1102. static struct platform_driver rza1_pinctrl_driver = {
  1103. .driver = {
  1104. .name = DRIVER_NAME,
  1105. .of_match_table = rza1_pinctrl_of_match,
  1106. },
  1107. .probe = rza1_pinctrl_probe,
  1108. };
  1109. static int __init rza1_pinctrl_init(void)
  1110. {
  1111. return platform_driver_register(&rza1_pinctrl_driver);
  1112. }
  1113. core_initcall(rza1_pinctrl_init);
  1114. MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
  1115. MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
  1116. MODULE_LICENSE("GPL v2");