pinctrl-rp1.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Raspberry Pi RP1 GPIO unit
  4. *
  5. * Copyright (C) 2023 Raspberry Pi Ltd.
  6. *
  7. * This driver is inspired by:
  8. * pinctrl-bcm2835.c, please see original file for copyright information
  9. */
  10. #include <linux/gpio/driver.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/pinctrl/pinconf.h>
  13. #include <linux/pinctrl/pinmux.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/regmap.h>
  17. #include "pinmux.h"
  18. #include "pinconf.h"
  19. #include "pinctrl-utils.h"
  20. #define MODULE_NAME "pinctrl-rp1"
  21. #define RP1_NUM_GPIOS 54
  22. #define RP1_NUM_BANKS 3
  23. #define RP1_INT_EDGE_FALLING BIT(0)
  24. #define RP1_INT_EDGE_RISING BIT(1)
  25. #define RP1_INT_LEVEL_LOW BIT(2)
  26. #define RP1_INT_LEVEL_HIGH BIT(3)
  27. #define RP1_INT_MASK GENMASK(3, 0)
  28. #define RP1_INT_EDGE_BOTH (RP1_INT_EDGE_FALLING | \
  29. RP1_INT_EDGE_RISING)
  30. #define RP1_FSEL_COUNT 9
  31. #define RP1_FSEL_ALT0 0x00
  32. #define RP1_FSEL_GPIO 0x05
  33. #define RP1_FSEL_NONE 0x09
  34. #define RP1_FSEL_NONE_HW 0x1f
  35. #define RP1_PAD_DRIVE_2MA 0x0
  36. #define RP1_PAD_DRIVE_4MA 0x1
  37. #define RP1_PAD_DRIVE_8MA 0x2
  38. #define RP1_PAD_DRIVE_12MA 0x3
  39. enum {
  40. RP1_PUD_OFF = 0,
  41. RP1_PUD_DOWN = 1,
  42. RP1_PUD_UP = 2,
  43. };
  44. enum {
  45. RP1_DIR_OUTPUT = 0,
  46. RP1_DIR_INPUT = 1,
  47. };
  48. enum {
  49. RP1_OUTOVER_PERI = 0,
  50. RP1_OUTOVER_INVPERI = 1,
  51. RP1_OUTOVER_LOW = 2,
  52. RP1_OUTOVER_HIGH = 3,
  53. };
  54. enum {
  55. RP1_OEOVER_PERI = 0,
  56. RP1_OEOVER_INVPERI = 1,
  57. RP1_OEOVER_DISABLE = 2,
  58. RP1_OEOVER_ENABLE = 3,
  59. };
  60. enum {
  61. RP1_INOVER_PERI = 0,
  62. RP1_INOVER_INVPERI = 1,
  63. RP1_INOVER_LOW = 2,
  64. RP1_INOVER_HIGH = 3,
  65. };
  66. enum {
  67. RP1_GPIO_CTRL_IRQRESET_SET = 0,
  68. RP1_GPIO_CTRL_INT_CLR = 1,
  69. RP1_GPIO_CTRL_INT_SET = 2,
  70. RP1_GPIO_CTRL_OEOVER = 3,
  71. RP1_GPIO_CTRL_FUNCSEL = 4,
  72. RP1_GPIO_CTRL_OUTOVER = 5,
  73. RP1_GPIO_CTRL = 6,
  74. };
  75. enum {
  76. RP1_INTE_SET = 0,
  77. RP1_INTE_CLR = 1,
  78. };
  79. enum {
  80. RP1_RIO_OUT_SET = 0,
  81. RP1_RIO_OUT_CLR = 1,
  82. RP1_RIO_OE = 2,
  83. RP1_RIO_OE_SET = 3,
  84. RP1_RIO_OE_CLR = 4,
  85. RP1_RIO_IN = 5,
  86. };
  87. enum {
  88. RP1_PAD_SLEWFAST = 0,
  89. RP1_PAD_SCHMITT = 1,
  90. RP1_PAD_PULL = 2,
  91. RP1_PAD_DRIVE = 3,
  92. RP1_PAD_IN_ENABLE = 4,
  93. RP1_PAD_OUT_DISABLE = 5,
  94. };
  95. static const struct reg_field rp1_gpio_fields[] = {
  96. [RP1_GPIO_CTRL_IRQRESET_SET] = REG_FIELD(0x2004, 28, 28),
  97. [RP1_GPIO_CTRL_INT_CLR] = REG_FIELD(0x3004, 20, 23),
  98. [RP1_GPIO_CTRL_INT_SET] = REG_FIELD(0x2004, 20, 23),
  99. [RP1_GPIO_CTRL_OEOVER] = REG_FIELD(0x0004, 14, 15),
  100. [RP1_GPIO_CTRL_FUNCSEL] = REG_FIELD(0x0004, 0, 4),
  101. [RP1_GPIO_CTRL_OUTOVER] = REG_FIELD(0x0004, 12, 13),
  102. [RP1_GPIO_CTRL] = REG_FIELD(0x0004, 0, 31),
  103. };
  104. static const struct reg_field rp1_inte_fields[] = {
  105. [RP1_INTE_SET] = REG_FIELD(0x2000, 0, 0),
  106. [RP1_INTE_CLR] = REG_FIELD(0x3000, 0, 0),
  107. };
  108. static const struct reg_field rp1_rio_fields[] = {
  109. [RP1_RIO_OUT_SET] = REG_FIELD(0x2000, 0, 0),
  110. [RP1_RIO_OUT_CLR] = REG_FIELD(0x3000, 0, 0),
  111. [RP1_RIO_OE] = REG_FIELD(0x0004, 0, 0),
  112. [RP1_RIO_OE_SET] = REG_FIELD(0x2004, 0, 0),
  113. [RP1_RIO_OE_CLR] = REG_FIELD(0x3004, 0, 0),
  114. [RP1_RIO_IN] = REG_FIELD(0x0008, 0, 0),
  115. };
  116. static const struct reg_field rp1_pad_fields[] = {
  117. [RP1_PAD_SLEWFAST] = REG_FIELD(0, 0, 0),
  118. [RP1_PAD_SCHMITT] = REG_FIELD(0, 1, 1),
  119. [RP1_PAD_PULL] = REG_FIELD(0, 2, 3),
  120. [RP1_PAD_DRIVE] = REG_FIELD(0, 4, 5),
  121. [RP1_PAD_IN_ENABLE] = REG_FIELD(0, 6, 6),
  122. [RP1_PAD_OUT_DISABLE] = REG_FIELD(0, 7, 7),
  123. };
  124. #define FUNC(f) \
  125. [func_##f] = #f
  126. #define RP1_MAX_FSEL 8
  127. #define PIN(i, f0, f1, f2, f3, f4, f5, f6, f7, f8) \
  128. [i] = { \
  129. .funcs = { \
  130. func_##f0, \
  131. func_##f1, \
  132. func_##f2, \
  133. func_##f3, \
  134. func_##f4, \
  135. func_##f5, \
  136. func_##f6, \
  137. func_##f7, \
  138. func_##f8, \
  139. }, \
  140. }
  141. #define LEGACY_MAP(n, f0, f1, f2, f3, f4, f5) \
  142. [n] = { \
  143. func_gpio, \
  144. func_gpio, \
  145. func_##f5, \
  146. func_##f4, \
  147. func_##f0, \
  148. func_##f1, \
  149. func_##f2, \
  150. func_##f3, \
  151. }
  152. enum funcs {
  153. func_alt0,
  154. func_alt1,
  155. func_alt2,
  156. func_alt3,
  157. func_alt4,
  158. func_gpio,
  159. func_alt6,
  160. func_alt7,
  161. func_alt8,
  162. func_none,
  163. func_aaud,
  164. func_dpi,
  165. func_dsi0_te_ext,
  166. func_dsi1_te_ext,
  167. func_gpclk0,
  168. func_gpclk1,
  169. func_gpclk2,
  170. func_gpclk3,
  171. func_gpclk4,
  172. func_gpclk5,
  173. func_i2c0,
  174. func_i2c1,
  175. func_i2c2,
  176. func_i2c3,
  177. func_i2c4,
  178. func_i2c5,
  179. func_i2c6,
  180. func_i2s0,
  181. func_i2s1,
  182. func_i2s2,
  183. func_ir,
  184. func_mic,
  185. func_pcie_clkreq_n,
  186. func_pio,
  187. func_proc_rio,
  188. func_pwm0,
  189. func_pwm1,
  190. func_sd0,
  191. func_sd1,
  192. func_spi0,
  193. func_spi1,
  194. func_spi2,
  195. func_spi3,
  196. func_spi4,
  197. func_spi5,
  198. func_spi6,
  199. func_spi7,
  200. func_spi8,
  201. func_uart0,
  202. func_uart1,
  203. func_uart2,
  204. func_uart3,
  205. func_uart4,
  206. func_uart5,
  207. func_vbus0,
  208. func_vbus1,
  209. func_vbus2,
  210. func_vbus3,
  211. func__,
  212. func_count = func__,
  213. func_invalid = func__,
  214. };
  215. struct rp1_pin_funcs {
  216. u8 funcs[RP1_FSEL_COUNT];
  217. };
  218. struct rp1_iobank_desc {
  219. int min_gpio;
  220. int num_gpios;
  221. int gpio_offset;
  222. int inte_offset;
  223. int ints_offset;
  224. int rio_offset;
  225. int pads_offset;
  226. };
  227. struct rp1_pin_info {
  228. u8 num;
  229. u8 bank;
  230. u8 offset;
  231. u8 fsel;
  232. u8 irq_type;
  233. struct regmap_field *gpio[ARRAY_SIZE(rp1_gpio_fields)];
  234. struct regmap_field *rio[ARRAY_SIZE(rp1_rio_fields)];
  235. struct regmap_field *inte[ARRAY_SIZE(rp1_inte_fields)];
  236. struct regmap_field *pad[ARRAY_SIZE(rp1_pad_fields)];
  237. };
  238. struct rp1_pinctrl {
  239. struct device *dev;
  240. void __iomem *gpio_base;
  241. void __iomem *rio_base;
  242. void __iomem *pads_base;
  243. int irq[RP1_NUM_BANKS];
  244. struct rp1_pin_info pins[RP1_NUM_GPIOS];
  245. struct pinctrl_dev *pctl_dev;
  246. struct gpio_chip gpio_chip;
  247. struct pinctrl_gpio_range gpio_range;
  248. raw_spinlock_t irq_lock[RP1_NUM_BANKS];
  249. };
  250. /* pins are just named GPIO0..GPIO53 */
  251. #define RP1_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
  252. static struct pinctrl_pin_desc rp1_gpio_pins[] = {
  253. RP1_GPIO_PIN(0),
  254. RP1_GPIO_PIN(1),
  255. RP1_GPIO_PIN(2),
  256. RP1_GPIO_PIN(3),
  257. RP1_GPIO_PIN(4),
  258. RP1_GPIO_PIN(5),
  259. RP1_GPIO_PIN(6),
  260. RP1_GPIO_PIN(7),
  261. RP1_GPIO_PIN(8),
  262. RP1_GPIO_PIN(9),
  263. RP1_GPIO_PIN(10),
  264. RP1_GPIO_PIN(11),
  265. RP1_GPIO_PIN(12),
  266. RP1_GPIO_PIN(13),
  267. RP1_GPIO_PIN(14),
  268. RP1_GPIO_PIN(15),
  269. RP1_GPIO_PIN(16),
  270. RP1_GPIO_PIN(17),
  271. RP1_GPIO_PIN(18),
  272. RP1_GPIO_PIN(19),
  273. RP1_GPIO_PIN(20),
  274. RP1_GPIO_PIN(21),
  275. RP1_GPIO_PIN(22),
  276. RP1_GPIO_PIN(23),
  277. RP1_GPIO_PIN(24),
  278. RP1_GPIO_PIN(25),
  279. RP1_GPIO_PIN(26),
  280. RP1_GPIO_PIN(27),
  281. RP1_GPIO_PIN(28),
  282. RP1_GPIO_PIN(29),
  283. RP1_GPIO_PIN(30),
  284. RP1_GPIO_PIN(31),
  285. RP1_GPIO_PIN(32),
  286. RP1_GPIO_PIN(33),
  287. RP1_GPIO_PIN(34),
  288. RP1_GPIO_PIN(35),
  289. RP1_GPIO_PIN(36),
  290. RP1_GPIO_PIN(37),
  291. RP1_GPIO_PIN(38),
  292. RP1_GPIO_PIN(39),
  293. RP1_GPIO_PIN(40),
  294. RP1_GPIO_PIN(41),
  295. RP1_GPIO_PIN(42),
  296. RP1_GPIO_PIN(43),
  297. RP1_GPIO_PIN(44),
  298. RP1_GPIO_PIN(45),
  299. RP1_GPIO_PIN(46),
  300. RP1_GPIO_PIN(47),
  301. RP1_GPIO_PIN(48),
  302. RP1_GPIO_PIN(49),
  303. RP1_GPIO_PIN(50),
  304. RP1_GPIO_PIN(51),
  305. RP1_GPIO_PIN(52),
  306. RP1_GPIO_PIN(53),
  307. };
  308. #define PIN_ARRAY(...) \
  309. (const unsigned int []) {__VA_ARGS__}
  310. #define PIN_ARRAY_SIZE(...) \
  311. (sizeof((unsigned int[]) {__VA_ARGS__}) / sizeof(unsigned int))
  312. #define RP1_GROUP(name, ...) \
  313. PINCTRL_PINGROUP(#name, PIN_ARRAY(__VA_ARGS__), \
  314. PIN_ARRAY_SIZE(__VA_ARGS__))
  315. static const struct pingroup rp1_gpio_groups[] = {
  316. RP1_GROUP(uart0, 14, 15),
  317. RP1_GROUP(uart0_ctrl, 4, 5, 6, 7, 16, 17),
  318. RP1_GROUP(uart1, 0, 1),
  319. RP1_GROUP(uart1_ctrl, 2, 3),
  320. RP1_GROUP(uart2, 4, 5),
  321. RP1_GROUP(uart2_ctrl, 6, 7),
  322. RP1_GROUP(uart3, 8, 9),
  323. RP1_GROUP(uart3_ctrl, 10, 11),
  324. RP1_GROUP(uart4, 12, 13),
  325. RP1_GROUP(uart4_ctrl, 14, 15),
  326. RP1_GROUP(uart5_0, 30, 31),
  327. RP1_GROUP(uart5_0_ctrl, 32, 33),
  328. RP1_GROUP(uart5_1, 36, 37),
  329. RP1_GROUP(uart5_1_ctrl, 38, 39),
  330. RP1_GROUP(uart5_2, 40, 41),
  331. RP1_GROUP(uart5_2_ctrl, 42, 43),
  332. RP1_GROUP(uart5_3, 48, 49),
  333. RP1_GROUP(sd0, 22, 23, 24, 25, 26, 27),
  334. RP1_GROUP(sd1, 28, 29, 30, 31, 32, 33),
  335. RP1_GROUP(i2s0, 18, 19, 20, 21),
  336. RP1_GROUP(i2s0_dual, 18, 19, 20, 21, 22, 23),
  337. RP1_GROUP(i2s0_quad, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27),
  338. RP1_GROUP(i2s1, 18, 19, 20, 21),
  339. RP1_GROUP(i2s1_dual, 18, 19, 20, 21, 22, 23),
  340. RP1_GROUP(i2s1_quad, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27),
  341. RP1_GROUP(i2s2_0, 28, 29, 30, 31),
  342. RP1_GROUP(i2s2_0_dual, 28, 29, 30, 31, 32, 33),
  343. RP1_GROUP(i2s2_1, 42, 43, 44, 45),
  344. RP1_GROUP(i2s2_1_dual, 42, 43, 44, 45, 46, 47),
  345. RP1_GROUP(i2c4_0, 28, 29),
  346. RP1_GROUP(i2c4_1, 34, 35),
  347. RP1_GROUP(i2c4_2, 40, 41),
  348. RP1_GROUP(i2c4_3, 46, 47),
  349. RP1_GROUP(i2c6_0, 38, 39),
  350. RP1_GROUP(i2c6_1, 51, 52),
  351. RP1_GROUP(i2c5_0, 30, 31),
  352. RP1_GROUP(i2c5_1, 36, 37),
  353. RP1_GROUP(i2c5_2, 44, 45),
  354. RP1_GROUP(i2c5_3, 49, 50),
  355. RP1_GROUP(i2c0_0, 0, 1),
  356. RP1_GROUP(i2c0_1, 8, 9),
  357. RP1_GROUP(i2c1_0, 2, 3),
  358. RP1_GROUP(i2c1_1, 10, 11),
  359. RP1_GROUP(i2c2_0, 4, 5),
  360. RP1_GROUP(i2c2_1, 12, 13),
  361. RP1_GROUP(i2c3_0, 6, 7),
  362. RP1_GROUP(i2c3_1, 14, 15),
  363. RP1_GROUP(i2c3_2, 22, 23),
  364. RP1_GROUP(dpi_16bit, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  365. 11, 12, 13, 14, 15, 16, 17, 18, 19),
  366. RP1_GROUP(dpi_16bit_cpadhi, 0, 1, 2, 3, 4, 5, 6, 7, 8,
  367. 12, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24),
  368. RP1_GROUP(dpi_16bit_pad666, 0, 1, 2, 3, 5, 6, 7, 8, 9,
  369. 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25),
  370. RP1_GROUP(dpi_18bit, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  371. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21),
  372. RP1_GROUP(dpi_18bit_cpadhi, 0, 1, 2, 3, 4, 5, 6, 7, 8,
  373. 9, 12, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24,
  374. 25),
  375. RP1_GROUP(dpi_24bit, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  376. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  377. 22, 23, 24, 25, 26, 27),
  378. RP1_GROUP(spi0, 9, 10, 11),
  379. RP1_GROUP(spi0_quad, 0, 1, 9, 10, 11),
  380. RP1_GROUP(spi1, 19, 20, 21),
  381. RP1_GROUP(spi2, 1, 2, 3),
  382. RP1_GROUP(spi3, 5, 6, 7),
  383. RP1_GROUP(spi4, 9, 10, 11),
  384. RP1_GROUP(spi5, 13, 14, 15),
  385. RP1_GROUP(spi6_0, 28, 29, 30),
  386. RP1_GROUP(spi6_1, 40, 41, 42),
  387. RP1_GROUP(spi7_0, 46, 47, 48),
  388. RP1_GROUP(spi7_1, 49, 50, 51),
  389. RP1_GROUP(spi8_0, 37, 38, 39),
  390. RP1_GROUP(spi8_1, 49, 50, 51),
  391. RP1_GROUP(aaud_0, 12, 13),
  392. RP1_GROUP(aaud_1, 38, 39),
  393. RP1_GROUP(aaud_2, 40, 41),
  394. RP1_GROUP(aaud_3, 49, 50),
  395. RP1_GROUP(aaud_4, 51, 52),
  396. RP1_GROUP(vbus0_0, 28, 29),
  397. RP1_GROUP(vbus0_1, 34, 35),
  398. RP1_GROUP(vbus1, 42, 43),
  399. RP1_GROUP(vbus2, 50, 51),
  400. RP1_GROUP(vbus3, 52, 53),
  401. RP1_GROUP(mic_0, 25, 26, 27),
  402. RP1_GROUP(mic_1, 34, 35, 36),
  403. RP1_GROUP(mic_2, 37, 38, 39),
  404. RP1_GROUP(mic_3, 46, 47, 48),
  405. RP1_GROUP(ir, 2, 3),
  406. };
  407. #define GRP_ARRAY(...) \
  408. (const char * []) {__VA_ARGS__}
  409. #define GRP_ARRAY_SIZE(...) \
  410. (sizeof((char *[]) {__VA_ARGS__}) / sizeof(char *))
  411. #define RP1_FNC(f, ...) \
  412. [func_##f] = PINCTRL_PINFUNCTION(#f, GRP_ARRAY(__VA_ARGS__), \
  413. GRP_ARRAY_SIZE(__VA_ARGS__))
  414. #define RP1_NULL_FNC(f) \
  415. [func_##f] = PINCTRL_PINFUNCTION(#f, NULL, 0)
  416. #define RP1_ALL_LEGACY_PINS \
  417. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", \
  418. "gpio5", "gpio6", "gpio7", "gpio8", "gpio9", \
  419. "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", \
  420. "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", \
  421. "gpio20", "gpio21", "gpio22", "gpio32", "gpio24", \
  422. "gpio25", "gpio26", "gpio27"
  423. #define RP1_ALL_PINS RP1_ALL_LEGACY_PINS, \
  424. "gpio28", "gpio29", "gpio30", "gpio31", "gpio32", \
  425. "gpio33", "gpio34", "gpio35", "gpio36", "gpio37", \
  426. "gpio38", "gpio39", "gpio40", "gpio41", "gpio42", \
  427. "gpio43", "gpio44", "gpio45", "gpio46", "gpio47", \
  428. "gpio48", "gpio49", "gpio50", "gpio51", "gpio52", \
  429. "gpio53"
  430. static const struct pinfunction rp1_func_names[] = {
  431. RP1_NULL_FNC(alt0),
  432. RP1_NULL_FNC(alt1),
  433. RP1_NULL_FNC(alt2),
  434. RP1_NULL_FNC(alt3),
  435. RP1_NULL_FNC(alt4),
  436. RP1_FNC(gpio, RP1_ALL_PINS),
  437. RP1_NULL_FNC(alt6),
  438. RP1_NULL_FNC(alt7),
  439. RP1_NULL_FNC(alt8),
  440. RP1_NULL_FNC(none),
  441. RP1_FNC(aaud, "aaud_0", "aaud_1", "aaud_2", "aaud_3", "aaud_4",
  442. "gpio12", "gpio13", "gpio38", "gpio39", "gpio40", "gpio41",
  443. "gpio49", "gpio50", "gpio51", "gpio52"),
  444. RP1_FNC(dpi, "dpi_16bit", "dpi_16bit_cpadhi",
  445. "dpi_16bit_pad666", "dpi_18bit, dpi_18bit_cpadhi",
  446. "dpi_24bit", RP1_ALL_LEGACY_PINS),
  447. RP1_FNC(dsi0_te_ext, "gpio16", "gpio38", "gpio46"),
  448. RP1_FNC(dsi1_te_ext, "gpio17", "gpio39", "gpio47"),
  449. RP1_FNC(gpclk0, "gpio4", "gpio20"),
  450. RP1_FNC(gpclk1, "gpio5", "gpio18", "gpio21"),
  451. RP1_FNC(gpclk2, "gpio6"),
  452. RP1_FNC(gpclk3, "gpio32", "gpio34", "gpio46"),
  453. RP1_FNC(gpclk4, "gpio33", "gpio43"),
  454. RP1_FNC(gpclk5, "gpio42", "gpio44", "gpio47"),
  455. RP1_FNC(i2c0, "i2c0_0", "i2c0_1", "gpio0", "gpio1", "gpio8", "gpio9"),
  456. RP1_FNC(i2c1, "i2c1_0", "i2c1_1", "gpio2", "gpio3", "gpio10", "gpio11"),
  457. RP1_FNC(i2c2, "i2c2_0", "i2c2_1", "gpio4", "gpio5", "gpio12", "gpio13"),
  458. RP1_FNC(i2c3, "i2c3_0", "i2c3_1", "i2c3_2", "gpio6", "gpio7", "gpio14",
  459. "gpio15", "gpio22", "gpio23"),
  460. RP1_FNC(i2c4, "i2c4_0", "i2c4_1", "i2c4_2", "i2c4_3", "gpio28",
  461. "gpio29", "gpio34", "gpio35", "gpio40", "gpio41", "gpio46",
  462. "gpio47"),
  463. RP1_FNC(i2c5, "i2c5_0", "i2c5_1", "i2c5_2", "i2c5_3", "gpio30",
  464. "gpio31", "gpio36", "gpio37", "gpio44", "gpio45", "gpio49",
  465. "gpio50"),
  466. RP1_FNC(i2c6, "i2c6_0", "i2c6_1", "gpio38", "gpio39", "gpio51",
  467. "gpio52"),
  468. RP1_FNC(i2s0, "i2s0", "i2s0_dual", "i2s0_quad", "gpio18", "gpio19",
  469. "gpio20", "gpio21", "gpio22", "gpio23", "gpio24", "gpio25",
  470. "gpio26", "gpio27"),
  471. RP1_FNC(i2s1, "i2s1", "i2s1_dual", "i2s1_quad", "gpio18", "gpio19",
  472. "gpio20", "gpio21", "gpio22", "gpio23", "gpio24", "gpio25",
  473. "gpio26", "gpio27"),
  474. RP1_FNC(i2s2, "i2s2_0", "i2s2_0_dual", "i2s2_1", "i2s2_1_dual",
  475. "gpio28", "gpio29", "gpio30", "gpio31", "gpio32", "gpio33",
  476. "gpio42", "gpio43", "gpio44", "gpio45", "gpio46", "gpio47"),
  477. RP1_FNC(ir, "gpio2", "gpio3"),
  478. RP1_FNC(mic, "mic_0", "mic_1", "mic_2", "mic_3", "gpio25", "gpio26",
  479. "gpio27", "gpio34", "gpio35", "gpio36", "gpio37", "gpio38",
  480. "gpio39", "gpio46", "gpio47", "gpio48"),
  481. RP1_FNC(pcie_clkreq_n, "gpio36", "gpio37", "gpio48", "gpio53"),
  482. RP1_FNC(pio, RP1_ALL_LEGACY_PINS),
  483. RP1_FNC(proc_rio, RP1_ALL_PINS),
  484. RP1_FNC(pwm0, "gpio12", "gpio13", "gpio14", "gpio15", "gpio18",
  485. "gpio19"),
  486. RP1_FNC(pwm1, "gpio34", "gpio35", "gpio40", "gpio41", "gpio44",
  487. "gpio45", "gpio48"),
  488. RP1_FNC(sd0, "sd0", "gpio22", "gpio23", "gpio24", "gpio25", "gpio26",
  489. "gpio27"),
  490. RP1_FNC(sd1, "sd1", "gpio28", "gpio29", "gpio30", "gpio31", "gpio32",
  491. "gpio33"),
  492. RP1_FNC(spi0, "spi0", "spi0_quad", "gpio0", "gpio1", "gpio2", "gpio3",
  493. "gpio7", "gpio8", "gpio9", "gpio10", "gpio11"),
  494. RP1_FNC(spi1, "spi1", "gpio19", "gpio20", "gpio21", "gpio16", "gpio17",
  495. "gpio18", "gpio27"),
  496. RP1_FNC(spi2, "spi2", "gpio0", "gpio1", "gpio2", "gpio3", "gpio24"),
  497. RP1_FNC(spi3, "spi3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio25"),
  498. RP1_FNC(spi4, "spi4", "gpio8", "gpio9", "gpio10", "gpio11"),
  499. RP1_FNC(spi5, "spi5", "gpio12", "gpio13", "gpio14", "gpio15", "gpio26"),
  500. RP1_FNC(spi6, "spi6_0", "spi6_1", "gpio28", "gpio29", "gpio30",
  501. "gpio31", "gpio32", "gpio33", "gpio40", "gpio41", "gpio42",
  502. "gpio43", "gpio44", "gpio45"),
  503. RP1_FNC(spi7, "spi7_0", "spi7_1", "gpio45", "gpio46", "gpio47",
  504. "gpio48", "gpio49", "gpio50", "gpio51", "gpio53"),
  505. RP1_FNC(spi8, "spi8_0", "spi8_1", "gpio35", "gpio36", "gpio37",
  506. "gpio38", "gpio39", "gpio49", "gpio50", "gpio51", "gpio52",
  507. "gpio53"),
  508. RP1_FNC(uart0, "uart0", "uart0_ctrl", "gpio4", "gpio5", "gpio6",
  509. "gpio7", "gpio14", "gpio15", "gpio16", "gpio17"),
  510. RP1_FNC(uart1, "uart1", "uart1_ctrl", "gpio0", "gpio1", "gpio2",
  511. "gpio3"),
  512. RP1_FNC(uart2, "uart2", "uart2_ctrl", "gpio4", "gpio5", "gpio6",
  513. "gpio7"),
  514. RP1_FNC(uart3, "uart3", "uart3_ctrl", "gpio8", "gpio9", "gpio10",
  515. "gpio11"),
  516. RP1_FNC(uart4, "uart4", "uart4_ctrl", "gpio12", "gpio13", "gpio14",
  517. "gpio15"),
  518. RP1_FNC(uart5, "uart5_0", "uart5_0_ctrl", "uart5_1", "uart5_1_ctrl",
  519. "uart5_2", "uart5_2_ctrl", "uart5_3"),
  520. RP1_FNC(vbus0, "vbus0_0", "vbus0_1", "gpio28", "gpio29", "gpio34",
  521. "gpio35"),
  522. RP1_FNC(vbus1, "vbus1", "gpio42", "gpio43"),
  523. RP1_FNC(vbus2, "vbus2", "gpio50", "gpio51"),
  524. RP1_FNC(vbus3, "vbus3", "gpio52", "gpio53"),
  525. RP1_NULL_FNC(invalid), //[func_invalid] = "?"
  526. };
  527. static const struct rp1_pin_funcs rp1_gpio_pin_funcs[] = {
  528. PIN(0, spi0, dpi, uart1, i2c0, _, gpio, proc_rio, pio, spi2),
  529. PIN(1, spi0, dpi, uart1, i2c0, _, gpio, proc_rio, pio, spi2),
  530. PIN(2, spi0, dpi, uart1, i2c1, ir, gpio, proc_rio, pio, spi2),
  531. PIN(3, spi0, dpi, uart1, i2c1, ir, gpio, proc_rio, pio, spi2),
  532. PIN(4, gpclk0, dpi, uart2, i2c2, uart0, gpio, proc_rio, pio, spi3),
  533. PIN(5, gpclk1, dpi, uart2, i2c2, uart0, gpio, proc_rio, pio, spi3),
  534. PIN(6, gpclk2, dpi, uart2, i2c3, uart0, gpio, proc_rio, pio, spi3),
  535. PIN(7, spi0, dpi, uart2, i2c3, uart0, gpio, proc_rio, pio, spi3),
  536. PIN(8, spi0, dpi, uart3, i2c0, _, gpio, proc_rio, pio, spi4),
  537. PIN(9, spi0, dpi, uart3, i2c0, _, gpio, proc_rio, pio, spi4),
  538. PIN(10, spi0, dpi, uart3, i2c1, _, gpio, proc_rio, pio, spi4),
  539. PIN(11, spi0, dpi, uart3, i2c1, _, gpio, proc_rio, pio, spi4),
  540. PIN(12, pwm0, dpi, uart4, i2c2, aaud, gpio, proc_rio, pio, spi5),
  541. PIN(13, pwm0, dpi, uart4, i2c2, aaud, gpio, proc_rio, pio, spi5),
  542. PIN(14, pwm0, dpi, uart4, i2c3, uart0, gpio, proc_rio, pio, spi5),
  543. PIN(15, pwm0, dpi, uart4, i2c3, uart0, gpio, proc_rio, pio, spi5),
  544. PIN(16, spi1, dpi, dsi0_te_ext, _, uart0, gpio, proc_rio, pio, _),
  545. PIN(17, spi1, dpi, dsi1_te_ext, _, uart0, gpio, proc_rio, pio, _),
  546. PIN(18, spi1, dpi, i2s0, pwm0, i2s1, gpio, proc_rio, pio, gpclk1),
  547. PIN(19, spi1, dpi, i2s0, pwm0, i2s1, gpio, proc_rio, pio, _),
  548. PIN(20, spi1, dpi, i2s0, gpclk0, i2s1, gpio, proc_rio, pio, _),
  549. PIN(21, spi1, dpi, i2s0, gpclk1, i2s1, gpio, proc_rio, pio, _),
  550. PIN(22, sd0, dpi, i2s0, i2c3, i2s1, gpio, proc_rio, pio, _),
  551. PIN(23, sd0, dpi, i2s0, i2c3, i2s1, gpio, proc_rio, pio, _),
  552. PIN(24, sd0, dpi, i2s0, _, i2s1, gpio, proc_rio, pio, spi2),
  553. PIN(25, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi3),
  554. PIN(26, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi5),
  555. PIN(27, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi1),
  556. PIN(28, sd1, i2c4, i2s2, spi6, vbus0, gpio, proc_rio, _, _),
  557. PIN(29, sd1, i2c4, i2s2, spi6, vbus0, gpio, proc_rio, _, _),
  558. PIN(30, sd1, i2c5, i2s2, spi6, uart5, gpio, proc_rio, _, _),
  559. PIN(31, sd1, i2c5, i2s2, spi6, uart5, gpio, proc_rio, _, _),
  560. PIN(32, sd1, gpclk3, i2s2, spi6, uart5, gpio, proc_rio, _, _),
  561. PIN(33, sd1, gpclk4, i2s2, spi6, uart5, gpio, proc_rio, _, _),
  562. PIN(34, pwm1, gpclk3, vbus0, i2c4, mic, gpio, proc_rio, _, _),
  563. PIN(35, spi8, pwm1, vbus0, i2c4, mic, gpio, proc_rio, _, _),
  564. PIN(36, spi8, uart5, pcie_clkreq_n, i2c5, mic, gpio, proc_rio, _, _),
  565. PIN(37, spi8, uart5, mic, i2c5, pcie_clkreq_n, gpio, proc_rio, _, _),
  566. PIN(38, spi8, uart5, mic, i2c6, aaud, gpio, proc_rio, dsi0_te_ext, _),
  567. PIN(39, spi8, uart5, mic, i2c6, aaud, gpio, proc_rio, dsi1_te_ext, _),
  568. PIN(40, pwm1, uart5, i2c4, spi6, aaud, gpio, proc_rio, _, _),
  569. PIN(41, pwm1, uart5, i2c4, spi6, aaud, gpio, proc_rio, _, _),
  570. PIN(42, gpclk5, uart5, vbus1, spi6, i2s2, gpio, proc_rio, _, _),
  571. PIN(43, gpclk4, uart5, vbus1, spi6, i2s2, gpio, proc_rio, _, _),
  572. PIN(44, gpclk5, i2c5, pwm1, spi6, i2s2, gpio, proc_rio, _, _),
  573. PIN(45, pwm1, i2c5, spi7, spi6, i2s2, gpio, proc_rio, _, _),
  574. PIN(46, gpclk3, i2c4, spi7, mic, i2s2, gpio, proc_rio, dsi0_te_ext, _),
  575. PIN(47, gpclk5, i2c4, spi7, mic, i2s2, gpio, proc_rio, dsi1_te_ext, _),
  576. PIN(48, pwm1, pcie_clkreq_n, spi7, mic, uart5, gpio, proc_rio, _, _),
  577. PIN(49, spi8, spi7, i2c5, aaud, uart5, gpio, proc_rio, _, _),
  578. PIN(50, spi8, spi7, i2c5, aaud, vbus2, gpio, proc_rio, _, _),
  579. PIN(51, spi8, spi7, i2c6, aaud, vbus2, gpio, proc_rio, _, _),
  580. PIN(52, spi8, _, i2c6, aaud, vbus3, gpio, proc_rio, _, _),
  581. PIN(53, spi8, spi7, _, pcie_clkreq_n, vbus3, gpio, proc_rio, _, _),
  582. };
  583. static const u8 legacy_fsel_map[][8] = {
  584. LEGACY_MAP(0, i2c0, _, dpi, spi2, uart1, _),
  585. LEGACY_MAP(1, i2c0, _, dpi, spi2, uart1, _),
  586. LEGACY_MAP(2, i2c1, _, dpi, spi2, uart1, _),
  587. LEGACY_MAP(3, i2c1, _, dpi, spi2, uart1, _),
  588. LEGACY_MAP(4, gpclk0, _, dpi, spi3, uart2, i2c2),
  589. LEGACY_MAP(5, gpclk1, _, dpi, spi3, uart2, i2c2),
  590. LEGACY_MAP(6, gpclk2, _, dpi, spi3, uart2, i2c3),
  591. LEGACY_MAP(7, spi0, _, dpi, spi3, uart2, i2c3),
  592. LEGACY_MAP(8, spi0, _, dpi, _, uart3, i2c0),
  593. LEGACY_MAP(9, spi0, _, dpi, _, uart3, i2c0),
  594. LEGACY_MAP(10, spi0, _, dpi, _, uart3, i2c1),
  595. LEGACY_MAP(11, spi0, _, dpi, _, uart3, i2c1),
  596. LEGACY_MAP(12, pwm0, _, dpi, spi5, uart4, i2c2),
  597. LEGACY_MAP(13, pwm0, _, dpi, spi5, uart4, i2c2),
  598. LEGACY_MAP(14, uart0, _, dpi, spi5, uart4, _),
  599. LEGACY_MAP(15, uart0, _, dpi, spi5, uart4, _),
  600. LEGACY_MAP(16, _, _, dpi, uart0, spi1, _),
  601. LEGACY_MAP(17, _, _, dpi, uart0, spi1, _),
  602. LEGACY_MAP(18, i2s0, _, dpi, _, spi1, pwm0),
  603. LEGACY_MAP(19, i2s0, _, dpi, _, spi1, pwm0),
  604. LEGACY_MAP(20, i2s0, _, dpi, _, spi1, gpclk0),
  605. LEGACY_MAP(21, i2s0, _, dpi, _, spi1, gpclk1),
  606. LEGACY_MAP(22, sd0, _, dpi, _, _, i2c3),
  607. LEGACY_MAP(23, sd0, _, dpi, _, _, i2c3),
  608. LEGACY_MAP(24, sd0, _, dpi, _, _, spi2),
  609. LEGACY_MAP(25, sd0, _, dpi, _, _, spi3),
  610. LEGACY_MAP(26, sd0, _, dpi, _, _, spi5),
  611. LEGACY_MAP(27, sd0, _, dpi, _, _, _),
  612. };
  613. static const char * const irq_type_names[] = {
  614. [IRQ_TYPE_NONE] = "none",
  615. [IRQ_TYPE_EDGE_RISING] = "edge-rising",
  616. [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
  617. [IRQ_TYPE_EDGE_BOTH] = "edge-both",
  618. [IRQ_TYPE_LEVEL_HIGH] = "level-high",
  619. [IRQ_TYPE_LEVEL_LOW] = "level-low",
  620. };
  621. static bool persist_gpio_outputs = true;
  622. module_param(persist_gpio_outputs, bool, 0644);
  623. MODULE_PARM_DESC(persist_gpio_outputs, "Enable GPIO_OUT persistence when pin is freed");
  624. static const struct rp1_iobank_desc rp1_iobanks[RP1_NUM_BANKS] = {
  625. /* gpio inte ints rio pads */
  626. { 0, 28, 0x0000, 0x011c, 0x0124, 0x0000, 0x0004 },
  627. { 28, 6, 0x4000, 0x411c, 0x4124, 0x4000, 0x4004 },
  628. { 34, 20, 0x8000, 0x811c, 0x8124, 0x8000, 0x8004 },
  629. };
  630. static int rp1_pinconf_set(struct pinctrl_dev *pctldev,
  631. unsigned int offset, unsigned long *configs,
  632. unsigned int num_configs);
  633. static struct rp1_pin_info *rp1_get_pin(struct gpio_chip *chip,
  634. unsigned int offset)
  635. {
  636. struct rp1_pinctrl *pc = gpiochip_get_data(chip);
  637. if (pc && offset < RP1_NUM_GPIOS)
  638. return &pc->pins[offset];
  639. return NULL;
  640. }
  641. static struct rp1_pin_info *rp1_get_pin_pctl(struct pinctrl_dev *pctldev,
  642. unsigned int offset)
  643. {
  644. struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  645. if (pc && offset < RP1_NUM_GPIOS)
  646. return &pc->pins[offset];
  647. return NULL;
  648. }
  649. static void rp1_input_enable(struct rp1_pin_info *pin, int value)
  650. {
  651. regmap_field_write(pin->pad[RP1_PAD_IN_ENABLE], !!value);
  652. }
  653. static void rp1_output_enable(struct rp1_pin_info *pin, int value)
  654. {
  655. regmap_field_write(pin->pad[RP1_PAD_OUT_DISABLE], !value);
  656. }
  657. static u32 rp1_get_fsel(struct rp1_pin_info *pin)
  658. {
  659. u32 oeover, fsel;
  660. regmap_field_read(pin->gpio[RP1_GPIO_CTRL_OEOVER], &oeover);
  661. regmap_field_read(pin->gpio[RP1_GPIO_CTRL_FUNCSEL], &fsel);
  662. if (oeover != RP1_OEOVER_PERI || fsel >= RP1_FSEL_COUNT)
  663. fsel = RP1_FSEL_NONE;
  664. return fsel;
  665. }
  666. static void rp1_set_fsel(struct rp1_pin_info *pin, u32 fsel)
  667. {
  668. if (fsel >= RP1_FSEL_COUNT)
  669. fsel = RP1_FSEL_NONE_HW;
  670. rp1_input_enable(pin, 1);
  671. rp1_output_enable(pin, 1);
  672. if (fsel == RP1_FSEL_NONE) {
  673. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_OEOVER], RP1_OEOVER_DISABLE);
  674. } else {
  675. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_OUTOVER], RP1_OUTOVER_PERI);
  676. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_OEOVER], RP1_OEOVER_PERI);
  677. }
  678. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_FUNCSEL], fsel);
  679. }
  680. static int rp1_get_dir(struct rp1_pin_info *pin)
  681. {
  682. unsigned int val;
  683. regmap_field_read(pin->rio[RP1_RIO_OE], &val);
  684. return !val ? RP1_DIR_INPUT : RP1_DIR_OUTPUT;
  685. }
  686. static void rp1_set_dir(struct rp1_pin_info *pin, bool is_input)
  687. {
  688. int reg = is_input ? RP1_RIO_OE_CLR : RP1_RIO_OE_SET;
  689. regmap_field_write(pin->rio[reg], 1);
  690. }
  691. static int rp1_get_value(struct rp1_pin_info *pin)
  692. {
  693. unsigned int val;
  694. regmap_field_read(pin->rio[RP1_RIO_IN], &val);
  695. return !!val;
  696. }
  697. static void rp1_set_value(struct rp1_pin_info *pin, int value)
  698. {
  699. /* Assume the pin is already an output */
  700. int reg = value ? RP1_RIO_OUT_SET : RP1_RIO_OUT_CLR;
  701. regmap_field_write(pin->rio[reg], 1);
  702. }
  703. static int rp1_gpio_get(struct gpio_chip *chip, unsigned int offset)
  704. {
  705. struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
  706. int ret;
  707. if (!pin)
  708. return -EINVAL;
  709. ret = rp1_get_value(pin);
  710. return ret;
  711. }
  712. static int rp1_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  713. {
  714. struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
  715. if (pin)
  716. rp1_set_value(pin, value);
  717. return 0;
  718. }
  719. static int rp1_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  720. {
  721. struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
  722. u32 fsel;
  723. if (!pin)
  724. return -EINVAL;
  725. fsel = rp1_get_fsel(pin);
  726. if (fsel != RP1_FSEL_GPIO)
  727. return -EINVAL;
  728. return (rp1_get_dir(pin) == RP1_DIR_OUTPUT) ?
  729. GPIO_LINE_DIRECTION_OUT :
  730. GPIO_LINE_DIRECTION_IN;
  731. }
  732. static int rp1_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  733. {
  734. struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
  735. if (!pin)
  736. return -EINVAL;
  737. rp1_set_dir(pin, RP1_DIR_INPUT);
  738. rp1_set_fsel(pin, RP1_FSEL_GPIO);
  739. return 0;
  740. }
  741. static int rp1_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
  742. int value)
  743. {
  744. struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
  745. if (!pin)
  746. return -EINVAL;
  747. rp1_set_value(pin, value);
  748. rp1_set_dir(pin, RP1_DIR_OUTPUT);
  749. rp1_set_fsel(pin, RP1_FSEL_GPIO);
  750. return 0;
  751. }
  752. static int rp1_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  753. unsigned long config)
  754. {
  755. struct rp1_pinctrl *pc = gpiochip_get_data(chip);
  756. unsigned long configs[] = { config };
  757. return rp1_pinconf_set(pc->pctl_dev, offset, configs,
  758. ARRAY_SIZE(configs));
  759. }
  760. static const struct gpio_chip rp1_gpio_chip = {
  761. .label = MODULE_NAME,
  762. .owner = THIS_MODULE,
  763. .request = gpiochip_generic_request,
  764. .free = gpiochip_generic_free,
  765. .direction_input = rp1_gpio_direction_input,
  766. .direction_output = rp1_gpio_direction_output,
  767. .get_direction = rp1_gpio_get_direction,
  768. .get = rp1_gpio_get,
  769. .set = rp1_gpio_set,
  770. .base = -1,
  771. .set_config = rp1_gpio_set_config,
  772. .ngpio = RP1_NUM_GPIOS,
  773. .can_sleep = false,
  774. };
  775. static void rp1_gpio_irq_handler(struct irq_desc *desc)
  776. {
  777. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  778. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  779. struct rp1_pinctrl *pc = gpiochip_get_data(chip);
  780. const struct rp1_iobank_desc *bank;
  781. int irq = irq_desc_get_irq(desc);
  782. unsigned long ints;
  783. int bit_pos;
  784. if (pc->irq[0] == irq)
  785. bank = &rp1_iobanks[0];
  786. else if (pc->irq[1] == irq)
  787. bank = &rp1_iobanks[1];
  788. else
  789. bank = &rp1_iobanks[2];
  790. chained_irq_enter(host_chip, desc);
  791. ints = readl(pc->gpio_base + bank->ints_offset);
  792. for_each_set_bit(bit_pos, &ints, 32) {
  793. struct rp1_pin_info *pin = rp1_get_pin(chip, bit_pos);
  794. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_IRQRESET_SET], 1);
  795. generic_handle_irq(irq_find_mapping(pc->gpio_chip.irq.domain,
  796. bank->gpio_offset + bit_pos));
  797. }
  798. chained_irq_exit(host_chip, desc);
  799. }
  800. static void rp1_gpio_irq_config(struct rp1_pin_info *pin, bool enable)
  801. {
  802. int reg = enable ? RP1_INTE_SET : RP1_INTE_CLR;
  803. regmap_field_write(pin->inte[reg], 1);
  804. if (!enable)
  805. /* Clear any latched events */
  806. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_IRQRESET_SET], 1);
  807. }
  808. static void rp1_gpio_irq_enable(struct irq_data *data)
  809. {
  810. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  811. unsigned int gpio = irqd_to_hwirq(data);
  812. struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
  813. rp1_gpio_irq_config(pin, true);
  814. }
  815. static void rp1_gpio_irq_disable(struct irq_data *data)
  816. {
  817. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  818. unsigned int gpio = irqd_to_hwirq(data);
  819. struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
  820. rp1_gpio_irq_config(pin, false);
  821. }
  822. static int rp1_irq_set_type(struct rp1_pin_info *pin, unsigned int type)
  823. {
  824. u32 irq_flags;
  825. switch (type) {
  826. case IRQ_TYPE_NONE:
  827. irq_flags = 0;
  828. break;
  829. case IRQ_TYPE_EDGE_RISING:
  830. irq_flags = RP1_INT_EDGE_RISING;
  831. break;
  832. case IRQ_TYPE_EDGE_FALLING:
  833. irq_flags = RP1_INT_EDGE_FALLING;
  834. break;
  835. case IRQ_TYPE_EDGE_BOTH:
  836. irq_flags = RP1_INT_EDGE_RISING | RP1_INT_EDGE_FALLING;
  837. break;
  838. case IRQ_TYPE_LEVEL_HIGH:
  839. irq_flags = RP1_INT_LEVEL_HIGH;
  840. break;
  841. case IRQ_TYPE_LEVEL_LOW:
  842. irq_flags = RP1_INT_LEVEL_LOW;
  843. break;
  844. default:
  845. return -EINVAL;
  846. }
  847. /* Clear them all */
  848. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_INT_CLR], RP1_INT_MASK);
  849. /* Set those that are needed */
  850. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_INT_SET], irq_flags);
  851. pin->irq_type = type;
  852. return 0;
  853. }
  854. static int rp1_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  855. {
  856. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  857. unsigned int gpio = irqd_to_hwirq(data);
  858. struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
  859. struct rp1_pinctrl *pc = gpiochip_get_data(chip);
  860. int bank = pin->bank;
  861. unsigned long flags;
  862. int ret;
  863. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  864. ret = rp1_irq_set_type(pin, type);
  865. if (!ret) {
  866. if (type & IRQ_TYPE_EDGE_BOTH)
  867. irq_set_handler_locked(data, handle_edge_irq);
  868. else
  869. irq_set_handler_locked(data, handle_level_irq);
  870. }
  871. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  872. return ret;
  873. }
  874. static void rp1_gpio_irq_ack(struct irq_data *data)
  875. {
  876. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  877. unsigned int gpio = irqd_to_hwirq(data);
  878. struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
  879. /* Clear any latched events */
  880. regmap_field_write(pin->gpio[RP1_GPIO_CTRL_IRQRESET_SET], 1);
  881. }
  882. static int rp1_gpio_irq_set_affinity(struct irq_data *data, const struct cpumask *dest, bool force)
  883. {
  884. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  885. struct rp1_pinctrl *pc = gpiochip_get_data(chip);
  886. const struct rp1_iobank_desc *bank;
  887. struct irq_data *parent_data = NULL;
  888. int i;
  889. for (i = 0; i < 3; i++) {
  890. bank = &rp1_iobanks[i];
  891. if (data->hwirq >= bank->min_gpio &&
  892. data->hwirq < bank->min_gpio + bank->num_gpios) {
  893. parent_data = irq_get_irq_data(pc->irq[i]);
  894. break;
  895. }
  896. }
  897. if (parent_data && parent_data->chip->irq_set_affinity)
  898. return parent_data->chip->irq_set_affinity(parent_data, dest, force);
  899. return -EINVAL;
  900. }
  901. static struct irq_chip rp1_gpio_irq_chip = {
  902. .name = MODULE_NAME,
  903. .irq_enable = rp1_gpio_irq_enable,
  904. .irq_disable = rp1_gpio_irq_disable,
  905. .irq_set_type = rp1_gpio_irq_set_type,
  906. .irq_ack = rp1_gpio_irq_ack,
  907. .irq_mask = rp1_gpio_irq_disable,
  908. .irq_unmask = rp1_gpio_irq_enable,
  909. .irq_set_affinity = rp1_gpio_irq_set_affinity,
  910. .flags = IRQCHIP_IMMUTABLE,
  911. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  912. };
  913. static int rp1_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  914. {
  915. return ARRAY_SIZE(rp1_gpio_groups) + ARRAY_SIZE(rp1_gpio_pins);
  916. }
  917. static const char *rp1_pctl_get_group_name(struct pinctrl_dev *pctldev,
  918. unsigned int selector)
  919. {
  920. unsigned int ngroups = ARRAY_SIZE(rp1_gpio_groups);
  921. if (selector < ngroups)
  922. return rp1_gpio_groups[selector].name;
  923. return rp1_gpio_pins[selector - ngroups].name;
  924. }
  925. static enum funcs rp1_get_fsel_func(unsigned int pin, unsigned int fsel)
  926. {
  927. if (pin < RP1_NUM_GPIOS) {
  928. if (fsel < RP1_FSEL_COUNT)
  929. return rp1_gpio_pin_funcs[pin].funcs[fsel];
  930. else if (fsel == RP1_FSEL_NONE)
  931. return func_none;
  932. }
  933. return func_invalid;
  934. }
  935. static int rp1_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  936. unsigned int selector,
  937. const unsigned int **pins,
  938. unsigned int *num_pins)
  939. {
  940. unsigned int ngroups = ARRAY_SIZE(rp1_gpio_groups);
  941. if (selector < ngroups) {
  942. *pins = rp1_gpio_groups[selector].pins;
  943. *num_pins = rp1_gpio_groups[selector].npins;
  944. } else {
  945. *pins = &rp1_gpio_pins[selector - ngroups].number;
  946. *num_pins = 1;
  947. }
  948. return 0;
  949. }
  950. static void rp1_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  951. struct seq_file *s,
  952. unsigned int offset)
  953. {
  954. struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  955. struct gpio_chip *chip = &pc->gpio_chip;
  956. struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
  957. u32 fsel = rp1_get_fsel(pin);
  958. enum funcs func = rp1_get_fsel_func(offset, fsel);
  959. int value = rp1_get_value(pin);
  960. int irq = irq_find_mapping(chip->irq.domain, offset);
  961. seq_printf(s, "function %s (%s) in %s; irq %d (%s)",
  962. rp1_func_names[fsel].name, rp1_func_names[func].name,
  963. value ? "hi" : "lo",
  964. irq, irq_type_names[pin->irq_type]);
  965. }
  966. static void rp1_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  967. struct pinctrl_map *maps, unsigned int num_maps)
  968. {
  969. int i;
  970. for (i = 0; i < num_maps; i++)
  971. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  972. kfree(maps[i].data.configs.configs);
  973. kfree(maps);
  974. }
  975. static int rp1_pctl_legacy_map_func(struct rp1_pinctrl *pc,
  976. struct device_node *np, u32 pin, u32 fnum,
  977. struct pinctrl_map *maps,
  978. unsigned int *num_maps)
  979. {
  980. struct pinctrl_map *map = &maps[*num_maps];
  981. enum funcs func;
  982. if (fnum >= ARRAY_SIZE(legacy_fsel_map[0])) {
  983. dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
  984. return -EINVAL;
  985. }
  986. if (pin < ARRAY_SIZE(legacy_fsel_map)) {
  987. func = legacy_fsel_map[pin][fnum];
  988. } else if (fnum < 2) {
  989. func = func_gpio;
  990. } else {
  991. dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
  992. np, pin);
  993. return -EINVAL;
  994. }
  995. map->type = PIN_MAP_TYPE_MUX_GROUP;
  996. map->data.mux.group = rp1_pctl_get_group_name(pc->pctl_dev,
  997. ARRAY_SIZE(rp1_gpio_groups)
  998. + pin);
  999. map->data.mux.function = rp1_func_names[func].name;
  1000. (*num_maps)++;
  1001. return 0;
  1002. }
  1003. static int rp1_pctl_legacy_map_pull(struct rp1_pinctrl *pc,
  1004. struct device_node *np, u32 pin, u32 pull,
  1005. struct pinctrl_map *maps,
  1006. unsigned int *num_maps)
  1007. {
  1008. struct pinctrl_map *map = &maps[*num_maps];
  1009. enum pin_config_param param;
  1010. unsigned long *configs;
  1011. switch (pull) {
  1012. case RP1_PUD_OFF:
  1013. param = PIN_CONFIG_BIAS_DISABLE;
  1014. break;
  1015. case RP1_PUD_DOWN:
  1016. param = PIN_CONFIG_BIAS_PULL_DOWN;
  1017. break;
  1018. case RP1_PUD_UP:
  1019. param = PIN_CONFIG_BIAS_PULL_UP;
  1020. break;
  1021. default:
  1022. dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
  1023. return -EINVAL;
  1024. }
  1025. configs = kzalloc_obj(*configs);
  1026. if (!configs)
  1027. return -ENOMEM;
  1028. configs[0] = pinconf_to_config_packed(param, 0);
  1029. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  1030. map->data.configs.group_or_pin = rp1_gpio_pins[pin].name;
  1031. map->data.configs.configs = configs;
  1032. map->data.configs.num_configs = 1;
  1033. (*num_maps)++;
  1034. return 0;
  1035. }
  1036. static int rp1_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  1037. struct device_node *np,
  1038. struct pinctrl_map **map,
  1039. unsigned int *num_maps)
  1040. {
  1041. struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  1042. struct property *pins, *funcs, *pulls;
  1043. int num_pins, num_funcs, num_pulls, maps_per_pin;
  1044. struct pinctrl_map *maps;
  1045. unsigned long *configs = NULL;
  1046. const char *function = NULL;
  1047. unsigned int reserved_maps;
  1048. int num_configs = 0;
  1049. int i, err;
  1050. u32 pin, func, pull;
  1051. /* Check for legacy pin declaration */
  1052. pins = of_find_property(np, "brcm,pins", NULL);
  1053. if (!pins) /* Assume generic bindings in this node */
  1054. return pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
  1055. funcs = of_find_property(np, "brcm,function", NULL);
  1056. if (!funcs)
  1057. of_property_read_string(np, "function", &function);
  1058. pulls = of_find_property(np, "brcm,pull", NULL);
  1059. if (!pulls)
  1060. pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
  1061. if (!function && !funcs && !num_configs && !pulls) {
  1062. dev_err(pc->dev,
  1063. "%pOF: no function, brcm,function, brcm,pull, etc.\n",
  1064. np);
  1065. return -EINVAL;
  1066. }
  1067. num_pins = pins->length / 4;
  1068. num_funcs = funcs ? (funcs->length / 4) : 0;
  1069. num_pulls = pulls ? (pulls->length / 4) : 0;
  1070. if (num_funcs > 1 && num_funcs != num_pins) {
  1071. dev_err(pc->dev,
  1072. "%pOF: brcm,function must have 1 or %d entries\n",
  1073. np, num_pins);
  1074. return -EINVAL;
  1075. }
  1076. if (num_pulls > 1 && num_pulls != num_pins) {
  1077. dev_err(pc->dev,
  1078. "%pOF: brcm,pull must have 1 or %d entries\n",
  1079. np, num_pins);
  1080. return -EINVAL;
  1081. }
  1082. maps_per_pin = 0;
  1083. if (function || num_funcs)
  1084. maps_per_pin++;
  1085. if (num_configs || num_pulls)
  1086. maps_per_pin++;
  1087. reserved_maps = num_pins * maps_per_pin;
  1088. maps = kzalloc_objs(*maps, reserved_maps);
  1089. if (!maps)
  1090. return -ENOMEM;
  1091. *num_maps = 0;
  1092. for (i = 0; i < num_pins; i++) {
  1093. err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  1094. if (err)
  1095. goto out;
  1096. if (num_funcs) {
  1097. err = of_property_read_u32_index(np, "brcm,function",
  1098. (num_funcs > 1) ? i : 0,
  1099. &func);
  1100. if (err)
  1101. goto out;
  1102. err = rp1_pctl_legacy_map_func(pc, np, pin, func,
  1103. maps, num_maps);
  1104. } else if (function) {
  1105. err = pinctrl_utils_add_map_mux(pctldev, &maps,
  1106. &reserved_maps, num_maps,
  1107. rp1_gpio_groups[pin].name,
  1108. function);
  1109. }
  1110. if (err)
  1111. goto out;
  1112. if (num_pulls) {
  1113. err = of_property_read_u32_index(np, "brcm,pull",
  1114. (num_pulls > 1) ? i : 0,
  1115. &pull);
  1116. if (err)
  1117. goto out;
  1118. err = rp1_pctl_legacy_map_pull(pc, np, pin, pull,
  1119. maps, num_maps);
  1120. } else if (num_configs) {
  1121. err = pinctrl_utils_add_map_configs(pctldev, &maps,
  1122. &reserved_maps, num_maps,
  1123. rp1_gpio_groups[pin].name,
  1124. configs, num_configs,
  1125. PIN_MAP_TYPE_CONFIGS_PIN);
  1126. }
  1127. if (err)
  1128. goto out;
  1129. }
  1130. *map = maps;
  1131. return 0;
  1132. out:
  1133. rp1_pctl_dt_free_map(pctldev, maps, reserved_maps);
  1134. return err;
  1135. }
  1136. static const struct pinctrl_ops rp1_pctl_ops = {
  1137. .get_groups_count = rp1_pctl_get_groups_count,
  1138. .get_group_name = rp1_pctl_get_group_name,
  1139. .get_group_pins = rp1_pctl_get_group_pins,
  1140. .pin_dbg_show = rp1_pctl_pin_dbg_show,
  1141. .dt_node_to_map = rp1_pctl_dt_node_to_map,
  1142. .dt_free_map = rp1_pctl_dt_free_map,
  1143. };
  1144. static int rp1_pmx_free(struct pinctrl_dev *pctldev, unsigned int offset)
  1145. {
  1146. struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
  1147. u32 fsel = rp1_get_fsel(pin);
  1148. /* Return all pins to GPIO_IN, unless persist_gpio_outputs is set */
  1149. if (persist_gpio_outputs && fsel == RP1_FSEL_GPIO)
  1150. return 0;
  1151. rp1_set_dir(pin, RP1_DIR_INPUT);
  1152. rp1_set_fsel(pin, RP1_FSEL_GPIO);
  1153. return 0;
  1154. }
  1155. static int rp1_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  1156. {
  1157. return func_count;
  1158. }
  1159. static const char *rp1_pmx_get_function_name(struct pinctrl_dev *pctldev,
  1160. unsigned int selector)
  1161. {
  1162. return (selector < func_count) ? rp1_func_names[selector].name : NULL;
  1163. }
  1164. static int rp1_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  1165. unsigned int selector,
  1166. const char * const **groups,
  1167. unsigned * const num_groups)
  1168. {
  1169. *groups = rp1_func_names[selector].groups;
  1170. *num_groups = rp1_func_names[selector].ngroups;
  1171. return 0;
  1172. }
  1173. static int rp1_pmx_set(struct pinctrl_dev *pctldev, unsigned int func_selector,
  1174. unsigned int group_selector)
  1175. {
  1176. struct rp1_pin_info *pin;
  1177. const unsigned int *pins;
  1178. const u8 *pin_funcs;
  1179. unsigned int num_pins;
  1180. int offset, fsel;
  1181. rp1_pctl_get_group_pins(pctldev, group_selector, &pins, &num_pins);
  1182. for (offset = 0; offset < num_pins; ++offset) {
  1183. pin = rp1_get_pin_pctl(pctldev, pins[offset]);
  1184. /* func_selector is an enum funcs, so needs translation */
  1185. if (func_selector >= RP1_FSEL_COUNT) {
  1186. /* Convert to an fsel number */
  1187. pin_funcs = rp1_gpio_pin_funcs[pin->num].funcs;
  1188. for (fsel = 0; fsel < RP1_FSEL_COUNT; fsel++) {
  1189. if (pin_funcs[fsel] == func_selector)
  1190. break;
  1191. }
  1192. } else {
  1193. fsel = (int)func_selector;
  1194. }
  1195. if (fsel >= RP1_FSEL_COUNT && fsel != RP1_FSEL_NONE)
  1196. return -EINVAL;
  1197. rp1_set_fsel(pin, fsel);
  1198. }
  1199. return 0;
  1200. }
  1201. static void rp1_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  1202. struct pinctrl_gpio_range *range,
  1203. unsigned int offset)
  1204. {
  1205. (void)rp1_pmx_free(pctldev, offset);
  1206. }
  1207. static int rp1_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  1208. struct pinctrl_gpio_range *range,
  1209. unsigned int offset,
  1210. bool input)
  1211. {
  1212. struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
  1213. rp1_set_dir(pin, input);
  1214. rp1_set_fsel(pin, RP1_FSEL_GPIO);
  1215. return 0;
  1216. }
  1217. static const struct pinmux_ops rp1_pmx_ops = {
  1218. .free = rp1_pmx_free,
  1219. .get_functions_count = rp1_pmx_get_functions_count,
  1220. .get_function_name = rp1_pmx_get_function_name,
  1221. .get_function_groups = rp1_pmx_get_function_groups,
  1222. .set_mux = rp1_pmx_set,
  1223. .gpio_disable_free = rp1_pmx_gpio_disable_free,
  1224. .gpio_set_direction = rp1_pmx_gpio_set_direction,
  1225. };
  1226. static void rp1_pull_config_set(struct rp1_pin_info *pin, unsigned int arg)
  1227. {
  1228. regmap_field_write(pin->pad[RP1_PAD_PULL], arg & 0x3);
  1229. }
  1230. static int rp1_pinconf_set(struct pinctrl_dev *pctldev, unsigned int offset,
  1231. unsigned long *configs, unsigned int num_configs)
  1232. {
  1233. struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
  1234. u32 param, arg;
  1235. int i;
  1236. if (!pin)
  1237. return -EINVAL;
  1238. for (i = 0; i < num_configs; i++) {
  1239. param = pinconf_to_config_param(configs[i]);
  1240. arg = pinconf_to_config_argument(configs[i]);
  1241. switch (param) {
  1242. case PIN_CONFIG_BIAS_DISABLE:
  1243. rp1_pull_config_set(pin, RP1_PUD_OFF);
  1244. break;
  1245. case PIN_CONFIG_BIAS_PULL_DOWN:
  1246. rp1_pull_config_set(pin, RP1_PUD_DOWN);
  1247. break;
  1248. case PIN_CONFIG_BIAS_PULL_UP:
  1249. rp1_pull_config_set(pin, RP1_PUD_UP);
  1250. break;
  1251. case PIN_CONFIG_INPUT_ENABLE:
  1252. rp1_input_enable(pin, arg);
  1253. break;
  1254. case PIN_CONFIG_OUTPUT_ENABLE:
  1255. rp1_output_enable(pin, arg);
  1256. break;
  1257. case PIN_CONFIG_LEVEL:
  1258. rp1_set_value(pin, arg);
  1259. rp1_set_dir(pin, RP1_DIR_OUTPUT);
  1260. rp1_set_fsel(pin, RP1_FSEL_GPIO);
  1261. break;
  1262. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  1263. regmap_field_write(pin->pad[RP1_PAD_SCHMITT], !!arg);
  1264. break;
  1265. case PIN_CONFIG_SLEW_RATE:
  1266. regmap_field_write(pin->pad[RP1_PAD_SLEWFAST], !!arg);
  1267. break;
  1268. case PIN_CONFIG_DRIVE_STRENGTH:
  1269. switch (arg) {
  1270. case 2:
  1271. arg = RP1_PAD_DRIVE_2MA;
  1272. break;
  1273. case 4:
  1274. arg = RP1_PAD_DRIVE_4MA;
  1275. break;
  1276. case 8:
  1277. arg = RP1_PAD_DRIVE_8MA;
  1278. break;
  1279. case 12:
  1280. arg = RP1_PAD_DRIVE_12MA;
  1281. break;
  1282. default:
  1283. return -ENOTSUPP;
  1284. }
  1285. regmap_field_write(pin->pad[RP1_PAD_DRIVE], arg);
  1286. break;
  1287. default:
  1288. return -ENOTSUPP;
  1289. } /* switch param type */
  1290. } /* for each config */
  1291. return 0;
  1292. }
  1293. static int rp1_pinconf_get(struct pinctrl_dev *pctldev, unsigned int offset,
  1294. unsigned long *config)
  1295. {
  1296. struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
  1297. enum pin_config_param param = pinconf_to_config_param(*config);
  1298. u32 padctrl;
  1299. u32 arg;
  1300. if (!pin)
  1301. return -EINVAL;
  1302. switch (param) {
  1303. case PIN_CONFIG_INPUT_ENABLE:
  1304. regmap_field_read(pin->pad[RP1_PAD_IN_ENABLE], &padctrl);
  1305. arg = !!padctrl;
  1306. break;
  1307. case PIN_CONFIG_OUTPUT_ENABLE:
  1308. regmap_field_read(pin->pad[RP1_PAD_OUT_DISABLE], &padctrl);
  1309. arg = !padctrl;
  1310. break;
  1311. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  1312. regmap_field_read(pin->pad[RP1_PAD_SCHMITT], &padctrl);
  1313. arg = !!padctrl;
  1314. break;
  1315. case PIN_CONFIG_SLEW_RATE:
  1316. regmap_field_read(pin->pad[RP1_PAD_SLEWFAST], &padctrl);
  1317. arg = !!padctrl;
  1318. break;
  1319. case PIN_CONFIG_DRIVE_STRENGTH:
  1320. regmap_field_read(pin->pad[RP1_PAD_DRIVE], &padctrl);
  1321. switch (padctrl) {
  1322. case RP1_PAD_DRIVE_2MA:
  1323. arg = 2;
  1324. break;
  1325. case RP1_PAD_DRIVE_4MA:
  1326. arg = 4;
  1327. break;
  1328. case RP1_PAD_DRIVE_8MA:
  1329. arg = 8;
  1330. break;
  1331. case RP1_PAD_DRIVE_12MA:
  1332. arg = 12;
  1333. break;
  1334. }
  1335. break;
  1336. case PIN_CONFIG_BIAS_DISABLE:
  1337. regmap_field_read(pin->pad[RP1_PAD_PULL], &padctrl);
  1338. arg = ((padctrl == RP1_PUD_OFF));
  1339. break;
  1340. case PIN_CONFIG_BIAS_PULL_DOWN:
  1341. regmap_field_read(pin->pad[RP1_PAD_PULL], &padctrl);
  1342. arg = ((padctrl == RP1_PUD_DOWN));
  1343. break;
  1344. case PIN_CONFIG_BIAS_PULL_UP:
  1345. regmap_field_read(pin->pad[RP1_PAD_PULL], &padctrl);
  1346. arg = ((padctrl == RP1_PUD_UP));
  1347. break;
  1348. default:
  1349. return -ENOTSUPP;
  1350. }
  1351. *config = pinconf_to_config_packed(param, arg);
  1352. return 0;
  1353. }
  1354. static int rp1_pinconf_group_get(struct pinctrl_dev *pctldev, unsigned int selector,
  1355. unsigned long *config)
  1356. {
  1357. const unsigned int *pins;
  1358. unsigned int npins;
  1359. int ret;
  1360. ret = rp1_pctl_get_group_pins(pctldev, selector, &pins, &npins);
  1361. if (ret < 0)
  1362. return ret;
  1363. if (!npins)
  1364. return -ENODEV;
  1365. ret = rp1_pinconf_get(pctldev, pins[0], config);
  1366. return ret;
  1367. }
  1368. static int rp1_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned int selector,
  1369. unsigned long *configs, unsigned int num_configs)
  1370. {
  1371. const unsigned int *pins;
  1372. unsigned int npins;
  1373. int ret, i;
  1374. ret = rp1_pctl_get_group_pins(pctldev, selector, &pins, &npins);
  1375. if (ret < 0)
  1376. return ret;
  1377. for (i = 0; i < npins; i++) {
  1378. ret = rp1_pinconf_set(pctldev, pins[i], configs, num_configs);
  1379. if (ret < 0)
  1380. return ret;
  1381. }
  1382. return 0;
  1383. }
  1384. static const struct pinconf_ops rp1_pinconf_ops = {
  1385. .is_generic = true,
  1386. .pin_config_get = rp1_pinconf_get,
  1387. .pin_config_set = rp1_pinconf_set,
  1388. .pin_config_group_get = rp1_pinconf_group_get,
  1389. .pin_config_group_set = rp1_pinconf_group_set,
  1390. };
  1391. static struct pinctrl_desc rp1_pinctrl_desc = {
  1392. .name = MODULE_NAME,
  1393. .pins = rp1_gpio_pins,
  1394. .npins = ARRAY_SIZE(rp1_gpio_pins),
  1395. .pctlops = &rp1_pctl_ops,
  1396. .pmxops = &rp1_pmx_ops,
  1397. .confops = &rp1_pinconf_ops,
  1398. .owner = THIS_MODULE,
  1399. };
  1400. static struct pinctrl_gpio_range rp1_pinctrl_gpio_range = {
  1401. .name = MODULE_NAME,
  1402. .npins = RP1_NUM_GPIOS,
  1403. };
  1404. static const struct of_device_id rp1_pinctrl_match[] = {
  1405. {
  1406. .compatible = "raspberrypi,rp1-gpio",
  1407. .data = &rp1_pinconf_ops,
  1408. },
  1409. {},
  1410. };
  1411. MODULE_DEVICE_TABLE(of, rp1_pinctrl_match);
  1412. static struct rp1_pinctrl rp1_pinctrl_data = {};
  1413. static const struct regmap_range rp1_gpio_reg_ranges[] = {
  1414. /* BANK 0 */
  1415. regmap_reg_range(0x2004, 0x20dc),
  1416. regmap_reg_range(0x3004, 0x30dc),
  1417. regmap_reg_range(0x0004, 0x00dc),
  1418. regmap_reg_range(0x0124, 0x0124),
  1419. regmap_reg_range(0x211c, 0x211c),
  1420. regmap_reg_range(0x311c, 0x311c),
  1421. /* BANK 1 */
  1422. regmap_reg_range(0x6004, 0x602c),
  1423. regmap_reg_range(0x7004, 0x702c),
  1424. regmap_reg_range(0x4004, 0x402c),
  1425. regmap_reg_range(0x4124, 0x4124),
  1426. regmap_reg_range(0x611c, 0x611c),
  1427. regmap_reg_range(0x711c, 0x711c),
  1428. /* BANK 2 */
  1429. regmap_reg_range(0xa004, 0xa09c),
  1430. regmap_reg_range(0xb004, 0xb09c),
  1431. regmap_reg_range(0x8004, 0x809c),
  1432. regmap_reg_range(0x8124, 0x8124),
  1433. regmap_reg_range(0xa11c, 0xa11c),
  1434. regmap_reg_range(0xb11c, 0xb11c),
  1435. };
  1436. static const struct regmap_range rp1_rio_reg_ranges[] = {
  1437. /* BANK 0 */
  1438. regmap_reg_range(0x2000, 0x2004),
  1439. regmap_reg_range(0x3000, 0x3004),
  1440. regmap_reg_range(0x0004, 0x0008),
  1441. /* BANK 1 */
  1442. regmap_reg_range(0x6000, 0x6004),
  1443. regmap_reg_range(0x7000, 0x7004),
  1444. regmap_reg_range(0x4004, 0x4008),
  1445. /* BANK 2 */
  1446. regmap_reg_range(0xa000, 0xa004),
  1447. regmap_reg_range(0xb000, 0xb004),
  1448. regmap_reg_range(0x8004, 0x8008),
  1449. };
  1450. static const struct regmap_range rp1_pads_reg_ranges[] = {
  1451. /* BANK 0 */
  1452. regmap_reg_range(0x0004, 0x0070),
  1453. /* BANK 1 */
  1454. regmap_reg_range(0x4004, 0x4018),
  1455. /* BANK 2 */
  1456. regmap_reg_range(0x8004, 0x8050),
  1457. };
  1458. static const struct regmap_access_table rp1_gpio_reg_table = {
  1459. .yes_ranges = rp1_gpio_reg_ranges,
  1460. .n_yes_ranges = ARRAY_SIZE(rp1_gpio_reg_ranges),
  1461. };
  1462. static const struct regmap_access_table rp1_rio_reg_table = {
  1463. .yes_ranges = rp1_rio_reg_ranges,
  1464. .n_yes_ranges = ARRAY_SIZE(rp1_rio_reg_ranges),
  1465. };
  1466. static const struct regmap_access_table rp1_pads_reg_table = {
  1467. .yes_ranges = rp1_pads_reg_ranges,
  1468. .n_yes_ranges = ARRAY_SIZE(rp1_pads_reg_ranges),
  1469. };
  1470. static const struct regmap_config rp1_pinctrl_gpio_regmap_cfg = {
  1471. .reg_bits = 32,
  1472. .val_bits = 32,
  1473. .reg_stride = 4,
  1474. .rd_table = &rp1_gpio_reg_table,
  1475. .name = "rp1-gpio",
  1476. .max_register = 0xb11c,
  1477. };
  1478. static const struct regmap_config rp1_pinctrl_rio_regmap_cfg = {
  1479. .reg_bits = 32,
  1480. .val_bits = 32,
  1481. .reg_stride = 4,
  1482. .rd_table = &rp1_rio_reg_table,
  1483. .name = "rp1-rio",
  1484. .max_register = 0xb004,
  1485. };
  1486. static const struct regmap_config rp1_pinctrl_pads_regmap_cfg = {
  1487. .reg_bits = 32,
  1488. .val_bits = 32,
  1489. .reg_stride = 4,
  1490. .rd_table = &rp1_pads_reg_table,
  1491. .name = "rp1-pads",
  1492. .max_register = 0x8050,
  1493. };
  1494. static int rp1_gen_regfield(struct device *dev,
  1495. const struct reg_field *array,
  1496. size_t array_size,
  1497. int reg_off,
  1498. int pin_off,
  1499. bool additive_offset,
  1500. struct regmap *regmap,
  1501. struct regmap_field *out[])
  1502. {
  1503. struct reg_field regfield;
  1504. int k;
  1505. for (k = 0; k < array_size; k++) {
  1506. regfield = array[k];
  1507. regfield.reg = (additive_offset ? regfield.reg : 0) + reg_off;
  1508. if (pin_off >= 0) {
  1509. regfield.lsb = pin_off;
  1510. regfield.msb = regfield.lsb;
  1511. }
  1512. out[k] = devm_regmap_field_alloc(dev, regmap, regfield);
  1513. if (IS_ERR(out[k]))
  1514. return PTR_ERR(out[k]);
  1515. }
  1516. return 0;
  1517. }
  1518. static int rp1_pinctrl_probe(struct platform_device *pdev)
  1519. {
  1520. struct regmap *gpio_regmap, *rio_regmap, *pads_regmap;
  1521. struct rp1_pinctrl *pc = &rp1_pinctrl_data;
  1522. struct device *dev = &pdev->dev;
  1523. struct device_node *np = dev->of_node;
  1524. struct gpio_irq_chip *girq;
  1525. int err, i;
  1526. pc->dev = dev;
  1527. pc->gpio_chip = rp1_gpio_chip;
  1528. pc->gpio_chip.parent = dev;
  1529. pc->gpio_base = devm_platform_ioremap_resource(pdev, 0);
  1530. if (IS_ERR(pc->gpio_base))
  1531. return dev_err_probe(dev, PTR_ERR(pc->gpio_base), "could not get GPIO IO memory\n");
  1532. pc->rio_base = devm_platform_ioremap_resource(pdev, 1);
  1533. if (IS_ERR(pc->rio_base))
  1534. return dev_err_probe(dev, PTR_ERR(pc->rio_base), "could not get RIO IO memory\n");
  1535. pc->pads_base = devm_platform_ioremap_resource(pdev, 2);
  1536. if (IS_ERR(pc->pads_base))
  1537. return dev_err_probe(dev, PTR_ERR(pc->pads_base), "could not get PADS IO memory\n");
  1538. gpio_regmap = devm_regmap_init_mmio(dev, pc->gpio_base,
  1539. &rp1_pinctrl_gpio_regmap_cfg);
  1540. if (IS_ERR(gpio_regmap))
  1541. return dev_err_probe(dev, PTR_ERR(gpio_regmap), "could not init GPIO regmap\n");
  1542. rio_regmap = devm_regmap_init_mmio(dev, pc->rio_base,
  1543. &rp1_pinctrl_rio_regmap_cfg);
  1544. if (IS_ERR(rio_regmap))
  1545. return dev_err_probe(dev, PTR_ERR(rio_regmap), "could not init RIO regmap\n");
  1546. pads_regmap = devm_regmap_init_mmio(dev, pc->pads_base,
  1547. &rp1_pinctrl_pads_regmap_cfg);
  1548. if (IS_ERR(pads_regmap))
  1549. return dev_err_probe(dev, PTR_ERR(pads_regmap), "could not init PADS regmap\n");
  1550. for (i = 0; i < RP1_NUM_BANKS; i++) {
  1551. const struct rp1_iobank_desc *bank = &rp1_iobanks[i];
  1552. int j;
  1553. for (j = 0; j < bank->num_gpios; j++) {
  1554. struct rp1_pin_info *pin =
  1555. &pc->pins[bank->min_gpio + j];
  1556. int reg_off;
  1557. pin->num = bank->min_gpio + j;
  1558. pin->bank = i;
  1559. pin->offset = j;
  1560. reg_off = bank->gpio_offset + pin->offset *
  1561. sizeof(u32) * 2;
  1562. err = rp1_gen_regfield(dev,
  1563. rp1_gpio_fields,
  1564. ARRAY_SIZE(rp1_gpio_fields),
  1565. reg_off,
  1566. -1,
  1567. true,
  1568. gpio_regmap,
  1569. pin->gpio);
  1570. if (err)
  1571. return dev_err_probe(dev, err,
  1572. "Unable to allocate regmap for gpio\n");
  1573. reg_off = bank->inte_offset;
  1574. err = rp1_gen_regfield(dev,
  1575. rp1_inte_fields,
  1576. ARRAY_SIZE(rp1_inte_fields),
  1577. reg_off,
  1578. pin->offset,
  1579. true,
  1580. gpio_regmap,
  1581. pin->inte);
  1582. if (err)
  1583. return dev_err_probe(dev, err,
  1584. "Unable to allocate regmap for inte\n");
  1585. reg_off = bank->rio_offset;
  1586. err = rp1_gen_regfield(dev,
  1587. rp1_rio_fields,
  1588. ARRAY_SIZE(rp1_rio_fields),
  1589. reg_off,
  1590. pin->offset,
  1591. true,
  1592. rio_regmap,
  1593. pin->rio);
  1594. if (err)
  1595. return dev_err_probe(dev, err,
  1596. "Unable to allocate regmap for rio\n");
  1597. reg_off = bank->pads_offset + pin->offset * sizeof(u32);
  1598. err = rp1_gen_regfield(dev,
  1599. rp1_pad_fields,
  1600. ARRAY_SIZE(rp1_pad_fields),
  1601. reg_off,
  1602. -1,
  1603. false,
  1604. pads_regmap,
  1605. pin->pad);
  1606. if (err)
  1607. return dev_err_probe(dev, err,
  1608. "Unable to allocate regmap for pad\n");
  1609. }
  1610. raw_spin_lock_init(&pc->irq_lock[i]);
  1611. }
  1612. pc->pctl_dev = devm_pinctrl_register(dev, &rp1_pinctrl_desc, pc);
  1613. if (IS_ERR(pc->pctl_dev))
  1614. return dev_err_probe(dev, PTR_ERR(pc->pctl_dev),
  1615. "Could not register pin controller\n");
  1616. girq = &pc->gpio_chip.irq;
  1617. girq->chip = &rp1_gpio_irq_chip;
  1618. girq->parent_handler = rp1_gpio_irq_handler;
  1619. girq->num_parents = RP1_NUM_BANKS;
  1620. girq->parents = pc->irq;
  1621. girq->default_type = IRQ_TYPE_NONE;
  1622. girq->handler = handle_level_irq;
  1623. /*
  1624. * Use the same handler for all groups: this is necessary
  1625. * since we use one gpiochip to cover all lines - the
  1626. * irq handler then needs to figure out which group and
  1627. * bank that was firing the IRQ and look up the per-group
  1628. * and bank data.
  1629. */
  1630. for (i = 0; i < RP1_NUM_BANKS; i++) {
  1631. pc->irq[i] = irq_of_parse_and_map(np, i);
  1632. if (!pc->irq[i]) {
  1633. girq->num_parents = i;
  1634. break;
  1635. }
  1636. }
  1637. platform_set_drvdata(pdev, pc);
  1638. err = devm_gpiochip_add_data(dev, &pc->gpio_chip, pc);
  1639. if (err)
  1640. return dev_err_probe(dev, err, "could not add GPIO chip\n");
  1641. pc->gpio_range = rp1_pinctrl_gpio_range;
  1642. pc->gpio_range.base = pc->gpio_chip.base;
  1643. pc->gpio_range.gc = &pc->gpio_chip;
  1644. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  1645. return 0;
  1646. }
  1647. static struct platform_driver rp1_pinctrl_driver = {
  1648. .probe = rp1_pinctrl_probe,
  1649. .driver = {
  1650. .name = MODULE_NAME,
  1651. .of_match_table = rp1_pinctrl_match,
  1652. .suppress_bind_attrs = true,
  1653. },
  1654. };
  1655. module_platform_driver(rp1_pinctrl_driver);
  1656. MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
  1657. MODULE_AUTHOR("Andrea della Porta <andrea.porta@suse.com>");
  1658. MODULE_DESCRIPTION("RP1 pinctrl/gpio driver");
  1659. MODULE_LICENSE("GPL");