pinctrl-rza1.c 37 KB

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