pinctrl-wpcm450.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2016-2018 Nuvoton Technology corporation.
  3. // Copyright (c) 2016, Dell Inc
  4. // Copyright (c) 2021-2022 Jonathan Neuschäfer
  5. //
  6. // This driver uses the following registers:
  7. // - Pin mux registers, in the GCR (general control registers) block
  8. // - GPIO registers, specific to each GPIO bank
  9. // - GPIO event (interrupt) registers, located centrally in the GPIO register
  10. // block, shared between all GPIO banks
  11. #include <linux/device.h>
  12. #include <linux/fwnode.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include "../core.h"
  26. /* GCR registers */
  27. #define WPCM450_GCR_MFSEL1 0x0c
  28. #define WPCM450_GCR_MFSEL2 0x10
  29. #define WPCM450_GCR_NONE 0
  30. /* GPIO event (interrupt) registers */
  31. #define WPCM450_GPEVTYPE 0x00
  32. #define WPCM450_GPEVPOL 0x04
  33. #define WPCM450_GPEVDBNC 0x08
  34. #define WPCM450_GPEVEN 0x0c
  35. #define WPCM450_GPEVST 0x10
  36. #define WPCM450_NUM_BANKS 8
  37. #define WPCM450_NUM_GPIOS 128
  38. #define WPCM450_NUM_GPIO_IRQS 4
  39. struct wpcm450_pinctrl;
  40. struct wpcm450_bank;
  41. struct wpcm450_gpio {
  42. struct gpio_chip gc;
  43. struct wpcm450_pinctrl *pctrl;
  44. struct irq_chip irqc;
  45. const struct wpcm450_bank *bank;
  46. };
  47. struct wpcm450_pinctrl {
  48. struct pinctrl_dev *pctldev;
  49. struct device *dev;
  50. struct irq_domain *domain;
  51. struct regmap *gcr_regmap;
  52. void __iomem *gpio_base;
  53. struct wpcm450_gpio gpio_bank[WPCM450_NUM_BANKS];
  54. unsigned long both_edges;
  55. /*
  56. * This spin lock protects registers and struct wpcm450_pinctrl fields
  57. * against concurrent access.
  58. */
  59. raw_spinlock_t lock;
  60. };
  61. struct wpcm450_bank {
  62. /* Range of GPIOs in this port */
  63. u8 base;
  64. u8 length;
  65. /* Register offsets (0 = register doesn't exist in this port) */
  66. u8 cfg0, cfg1, cfg2;
  67. u8 blink;
  68. u8 dataout, datain;
  69. /* Interrupt bit mapping */
  70. u8 first_irq_bit; /* First bit in GPEVST that belongs to this bank */
  71. u8 num_irqs; /* Number of IRQ-capable GPIOs in this bank */
  72. u8 first_irq_gpio; /* First IRQ-capable GPIO in this bank */
  73. };
  74. static const struct wpcm450_bank wpcm450_banks[WPCM450_NUM_BANKS] = {
  75. /* range cfg0 cfg1 cfg2 blink out in IRQ map */
  76. { 0, 16, 0x14, 0x18, 0, 0, 0x1c, 0x20, 0, 16, 0 },
  77. { 16, 16, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 16, 2, 8 },
  78. { 32, 16, 0x3c, 0x40, 0x44, 0, 0x48, 0x4c, 0, 0, 0 },
  79. { 48, 16, 0x50, 0x54, 0x58, 0, 0x5c, 0x60, 0, 0, 0 },
  80. { 64, 16, 0x64, 0x68, 0x6c, 0, 0x70, 0x74, 0, 0, 0 },
  81. { 80, 16, 0x78, 0x7c, 0x80, 0, 0x84, 0x88, 0, 0, 0 },
  82. { 96, 18, 0, 0, 0, 0, 0, 0x8c, 0, 0, 0 },
  83. { 114, 14, 0x90, 0x94, 0x98, 0, 0x9c, 0xa0, 0, 0, 0 },
  84. };
  85. static int wpcm450_gpio_irq_bitnum(struct wpcm450_gpio *gpio, struct irq_data *d)
  86. {
  87. const struct wpcm450_bank *bank = gpio->bank;
  88. int hwirq = irqd_to_hwirq(d);
  89. if (hwirq < bank->first_irq_gpio)
  90. return -EINVAL;
  91. if (hwirq - bank->first_irq_gpio >= bank->num_irqs)
  92. return -EINVAL;
  93. return hwirq - bank->first_irq_gpio + bank->first_irq_bit;
  94. }
  95. static int wpcm450_irq_bitnum_to_gpio(struct wpcm450_gpio *gpio, int bitnum)
  96. {
  97. const struct wpcm450_bank *bank = gpio->bank;
  98. if (bitnum < bank->first_irq_bit)
  99. return -EINVAL;
  100. if (bitnum - bank->first_irq_bit > bank->num_irqs)
  101. return -EINVAL;
  102. return bitnum - bank->first_irq_bit + bank->first_irq_gpio;
  103. }
  104. static void wpcm450_gpio_irq_ack(struct irq_data *d)
  105. {
  106. struct wpcm450_gpio *gpio = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  107. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  108. unsigned long flags;
  109. int bit;
  110. bit = wpcm450_gpio_irq_bitnum(gpio, d);
  111. if (bit < 0)
  112. return;
  113. raw_spin_lock_irqsave(&pctrl->lock, flags);
  114. iowrite32(BIT(bit), pctrl->gpio_base + WPCM450_GPEVST);
  115. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  116. }
  117. static void wpcm450_gpio_irq_mask(struct irq_data *d)
  118. {
  119. struct wpcm450_gpio *gpio = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  120. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  121. unsigned long flags;
  122. unsigned long even;
  123. int bit;
  124. bit = wpcm450_gpio_irq_bitnum(gpio, d);
  125. if (bit < 0)
  126. return;
  127. raw_spin_lock_irqsave(&pctrl->lock, flags);
  128. even = ioread32(pctrl->gpio_base + WPCM450_GPEVEN);
  129. __assign_bit(bit, &even, 0);
  130. iowrite32(even, pctrl->gpio_base + WPCM450_GPEVEN);
  131. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  132. }
  133. static void wpcm450_gpio_irq_unmask(struct irq_data *d)
  134. {
  135. struct wpcm450_gpio *gpio = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  136. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  137. unsigned long flags;
  138. unsigned long even;
  139. int bit;
  140. bit = wpcm450_gpio_irq_bitnum(gpio, d);
  141. if (bit < 0)
  142. return;
  143. raw_spin_lock_irqsave(&pctrl->lock, flags);
  144. even = ioread32(pctrl->gpio_base + WPCM450_GPEVEN);
  145. __assign_bit(bit, &even, 1);
  146. iowrite32(even, pctrl->gpio_base + WPCM450_GPEVEN);
  147. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  148. }
  149. /*
  150. * This is an implementation of the gpio_chip->get() function, for use in
  151. * wpcm450_gpio_fix_evpol. Unfortunately, we can't use the bgpio-provided
  152. * implementation there, because it would require taking gpio_chip->bgpio_lock,
  153. * which is a spin lock, but wpcm450_gpio_fix_evpol must work in contexts where
  154. * a raw spin lock is held.
  155. */
  156. static int wpcm450_gpio_get(struct wpcm450_gpio *gpio, int offset)
  157. {
  158. void __iomem *reg = gpio->pctrl->gpio_base + gpio->bank->datain;
  159. unsigned long flags;
  160. u32 level;
  161. raw_spin_lock_irqsave(&gpio->pctrl->lock, flags);
  162. level = !!(ioread32(reg) & BIT(offset));
  163. raw_spin_unlock_irqrestore(&gpio->pctrl->lock, flags);
  164. return level;
  165. }
  166. /*
  167. * Since the GPIO controller does not support dual-edge triggered interrupts
  168. * (IRQ_TYPE_EDGE_BOTH), they are emulated using rising/falling edge triggered
  169. * interrupts. wpcm450_gpio_fix_evpol sets the interrupt polarity for the
  170. * specified emulated dual-edge triggered interrupts, so that the next edge can
  171. * be detected.
  172. */
  173. static void wpcm450_gpio_fix_evpol(struct wpcm450_gpio *gpio, unsigned long all)
  174. {
  175. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  176. unsigned int bit;
  177. for_each_set_bit(bit, &all, 32) {
  178. int offset = wpcm450_irq_bitnum_to_gpio(gpio, bit);
  179. unsigned long evpol;
  180. unsigned long flags;
  181. int level;
  182. do {
  183. level = wpcm450_gpio_get(gpio, offset);
  184. /* Switch event polarity to the opposite of the current level */
  185. raw_spin_lock_irqsave(&pctrl->lock, flags);
  186. evpol = ioread32(pctrl->gpio_base + WPCM450_GPEVPOL);
  187. __assign_bit(bit, &evpol, !level);
  188. iowrite32(evpol, pctrl->gpio_base + WPCM450_GPEVPOL);
  189. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  190. } while (wpcm450_gpio_get(gpio, offset) != level);
  191. }
  192. }
  193. static int wpcm450_gpio_set_irq_type(struct irq_data *d, unsigned int flow_type)
  194. {
  195. struct wpcm450_gpio *gpio = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  196. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  197. unsigned long evtype, evpol;
  198. unsigned long flags;
  199. int ret = 0;
  200. int bit;
  201. bit = wpcm450_gpio_irq_bitnum(gpio, d);
  202. if (bit < 0)
  203. return bit;
  204. irq_set_handler_locked(d, handle_level_irq);
  205. raw_spin_lock_irqsave(&pctrl->lock, flags);
  206. evtype = ioread32(pctrl->gpio_base + WPCM450_GPEVTYPE);
  207. evpol = ioread32(pctrl->gpio_base + WPCM450_GPEVPOL);
  208. __assign_bit(bit, &pctrl->both_edges, 0);
  209. switch (flow_type) {
  210. case IRQ_TYPE_LEVEL_LOW:
  211. __assign_bit(bit, &evtype, 1);
  212. __assign_bit(bit, &evpol, 0);
  213. break;
  214. case IRQ_TYPE_LEVEL_HIGH:
  215. __assign_bit(bit, &evtype, 1);
  216. __assign_bit(bit, &evpol, 1);
  217. break;
  218. case IRQ_TYPE_EDGE_FALLING:
  219. __assign_bit(bit, &evtype, 0);
  220. __assign_bit(bit, &evpol, 0);
  221. break;
  222. case IRQ_TYPE_EDGE_RISING:
  223. __assign_bit(bit, &evtype, 0);
  224. __assign_bit(bit, &evpol, 1);
  225. break;
  226. case IRQ_TYPE_EDGE_BOTH:
  227. __assign_bit(bit, &evtype, 0);
  228. __assign_bit(bit, &pctrl->both_edges, 1);
  229. break;
  230. default:
  231. ret = -EINVAL;
  232. }
  233. iowrite32(evtype, pctrl->gpio_base + WPCM450_GPEVTYPE);
  234. iowrite32(evpol, pctrl->gpio_base + WPCM450_GPEVPOL);
  235. /* clear the event status for good measure */
  236. iowrite32(BIT(bit), pctrl->gpio_base + WPCM450_GPEVST);
  237. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  238. /* fix event polarity after clearing event status */
  239. wpcm450_gpio_fix_evpol(gpio, BIT(bit));
  240. return ret;
  241. }
  242. static const struct irq_chip wpcm450_gpio_irqchip = {
  243. .name = "WPCM450-GPIO-IRQ",
  244. .irq_ack = wpcm450_gpio_irq_ack,
  245. .irq_unmask = wpcm450_gpio_irq_unmask,
  246. .irq_mask = wpcm450_gpio_irq_mask,
  247. .irq_set_type = wpcm450_gpio_set_irq_type,
  248. };
  249. static void wpcm450_gpio_irqhandler(struct irq_desc *desc)
  250. {
  251. struct wpcm450_gpio *gpio = gpiochip_get_data(irq_desc_get_handler_data(desc));
  252. struct wpcm450_pinctrl *pctrl = gpio->pctrl;
  253. struct irq_chip *chip = irq_desc_get_chip(desc);
  254. unsigned long pending;
  255. unsigned long flags;
  256. unsigned long ours;
  257. unsigned int bit;
  258. ours = GENMASK(gpio->bank->num_irqs - 1, 0) << gpio->bank->first_irq_bit;
  259. raw_spin_lock_irqsave(&pctrl->lock, flags);
  260. pending = ioread32(pctrl->gpio_base + WPCM450_GPEVST);
  261. pending &= ioread32(pctrl->gpio_base + WPCM450_GPEVEN);
  262. pending &= ours;
  263. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  264. if (pending & pctrl->both_edges)
  265. wpcm450_gpio_fix_evpol(gpio, pending & pctrl->both_edges);
  266. chained_irq_enter(chip, desc);
  267. for_each_set_bit(bit, &pending, 32) {
  268. int offset = wpcm450_irq_bitnum_to_gpio(gpio, bit);
  269. generic_handle_domain_irq(gpio->gc.irq.domain, offset);
  270. }
  271. chained_irq_exit(chip, desc);
  272. }
  273. static int smb0_pins[] = { 115, 114 };
  274. static int smb1_pins[] = { 117, 116 };
  275. static int smb2_pins[] = { 119, 118 };
  276. static int smb3_pins[] = { 30, 31 };
  277. static int smb4_pins[] = { 28, 29 };
  278. static int smb5_pins[] = { 26, 27 };
  279. static int scs1_pins[] = { 32 };
  280. static int scs2_pins[] = { 33 };
  281. static int scs3_pins[] = { 34 };
  282. static int bsp_pins[] = { 41, 42 };
  283. static int hsp1_pins[] = { 43, 44, 45, 46, 47, 61, 62, 63 };
  284. static int hsp2_pins[] = { 48, 49, 50, 51, 52, 53, 54, 55 };
  285. static int r1err_pins[] = { 56 };
  286. static int r1md_pins[] = { 57, 58 };
  287. static int rmii2_pins[] = { 84, 85, 86, 87, 88, 89 };
  288. static int r2err_pins[] = { 90 };
  289. static int r2md_pins[] = { 91, 92 };
  290. static int kbcc_pins[] = { 94, 93 };
  291. static int clko_pins[] = { 96 };
  292. static int smi_pins[] = { 97 };
  293. static int uinc_pins[] = { 19 };
  294. static int mben_pins[] = {};
  295. static int gspi_pins[] = { 12, 13, 14, 15 };
  296. static int sspi_pins[] = { 12, 13, 14, 15 };
  297. static int xcs1_pins[] = { 35 };
  298. static int xcs2_pins[] = { 36 };
  299. static int sdio_pins[] = { 7, 22, 43, 44, 45, 46, 47, 60 };
  300. static int fi0_pins[] = { 64 };
  301. static int fi1_pins[] = { 65 };
  302. static int fi2_pins[] = { 66 };
  303. static int fi3_pins[] = { 67 };
  304. static int fi4_pins[] = { 68 };
  305. static int fi5_pins[] = { 69 };
  306. static int fi6_pins[] = { 70 };
  307. static int fi7_pins[] = { 71 };
  308. static int fi8_pins[] = { 72 };
  309. static int fi9_pins[] = { 73 };
  310. static int fi10_pins[] = { 74 };
  311. static int fi11_pins[] = { 75 };
  312. static int fi12_pins[] = { 76 };
  313. static int fi13_pins[] = { 77 };
  314. static int fi14_pins[] = { 78 };
  315. static int fi15_pins[] = { 79 };
  316. static int pwm0_pins[] = { 80 };
  317. static int pwm1_pins[] = { 81 };
  318. static int pwm2_pins[] = { 82 };
  319. static int pwm3_pins[] = { 83 };
  320. static int pwm4_pins[] = { 20 };
  321. static int pwm5_pins[] = { 21 };
  322. static int pwm6_pins[] = { 16 };
  323. static int pwm7_pins[] = { 17 };
  324. static int hg0_pins[] = { 20 };
  325. static int hg1_pins[] = { 21 };
  326. static int hg2_pins[] = { 22 };
  327. static int hg3_pins[] = { 23 };
  328. static int hg4_pins[] = { 24 };
  329. static int hg5_pins[] = { 25 };
  330. static int hg6_pins[] = { 59 };
  331. static int hg7_pins[] = { 60 };
  332. #define WPCM450_GRPS \
  333. WPCM450_GRP(smb3), \
  334. WPCM450_GRP(smb4), \
  335. WPCM450_GRP(smb5), \
  336. WPCM450_GRP(scs1), \
  337. WPCM450_GRP(scs2), \
  338. WPCM450_GRP(scs3), \
  339. WPCM450_GRP(smb0), \
  340. WPCM450_GRP(smb1), \
  341. WPCM450_GRP(smb2), \
  342. WPCM450_GRP(bsp), \
  343. WPCM450_GRP(hsp1), \
  344. WPCM450_GRP(hsp2), \
  345. WPCM450_GRP(r1err), \
  346. WPCM450_GRP(r1md), \
  347. WPCM450_GRP(rmii2), \
  348. WPCM450_GRP(r2err), \
  349. WPCM450_GRP(r2md), \
  350. WPCM450_GRP(kbcc), \
  351. WPCM450_GRP(clko), \
  352. WPCM450_GRP(smi), \
  353. WPCM450_GRP(uinc), \
  354. WPCM450_GRP(gspi), \
  355. WPCM450_GRP(mben), \
  356. WPCM450_GRP(xcs2), \
  357. WPCM450_GRP(xcs1), \
  358. WPCM450_GRP(sdio), \
  359. WPCM450_GRP(sspi), \
  360. WPCM450_GRP(fi0), \
  361. WPCM450_GRP(fi1), \
  362. WPCM450_GRP(fi2), \
  363. WPCM450_GRP(fi3), \
  364. WPCM450_GRP(fi4), \
  365. WPCM450_GRP(fi5), \
  366. WPCM450_GRP(fi6), \
  367. WPCM450_GRP(fi7), \
  368. WPCM450_GRP(fi8), \
  369. WPCM450_GRP(fi9), \
  370. WPCM450_GRP(fi10), \
  371. WPCM450_GRP(fi11), \
  372. WPCM450_GRP(fi12), \
  373. WPCM450_GRP(fi13), \
  374. WPCM450_GRP(fi14), \
  375. WPCM450_GRP(fi15), \
  376. WPCM450_GRP(pwm0), \
  377. WPCM450_GRP(pwm1), \
  378. WPCM450_GRP(pwm2), \
  379. WPCM450_GRP(pwm3), \
  380. WPCM450_GRP(pwm4), \
  381. WPCM450_GRP(pwm5), \
  382. WPCM450_GRP(pwm6), \
  383. WPCM450_GRP(pwm7), \
  384. WPCM450_GRP(hg0), \
  385. WPCM450_GRP(hg1), \
  386. WPCM450_GRP(hg2), \
  387. WPCM450_GRP(hg3), \
  388. WPCM450_GRP(hg4), \
  389. WPCM450_GRP(hg5), \
  390. WPCM450_GRP(hg6), \
  391. WPCM450_GRP(hg7), \
  392. enum {
  393. #define WPCM450_GRP(x) fn_ ## x
  394. WPCM450_GRPS
  395. /* add placeholder for none/gpio */
  396. WPCM450_GRP(gpio),
  397. WPCM450_GRP(none),
  398. #undef WPCM450_GRP
  399. };
  400. static struct group_desc wpcm450_groups[] = {
  401. #define WPCM450_GRP(x) { .name = #x, .pins = x ## _pins, \
  402. .num_pins = ARRAY_SIZE(x ## _pins) }
  403. WPCM450_GRPS
  404. #undef WPCM450_GRP
  405. };
  406. #define WPCM450_SFUNC(a) WPCM450_FUNC(a, #a)
  407. #define WPCM450_FUNC(a, b...) static const char *a ## _grp[] = { b }
  408. #define WPCM450_MKFUNC(nm) { .name = #nm, .ngroups = ARRAY_SIZE(nm ## _grp), \
  409. .groups = nm ## _grp }
  410. struct wpcm450_func {
  411. const char *name;
  412. const unsigned int ngroups;
  413. const char *const *groups;
  414. };
  415. WPCM450_SFUNC(smb3);
  416. WPCM450_SFUNC(smb4);
  417. WPCM450_SFUNC(smb5);
  418. WPCM450_SFUNC(scs1);
  419. WPCM450_SFUNC(scs2);
  420. WPCM450_SFUNC(scs3);
  421. WPCM450_SFUNC(smb0);
  422. WPCM450_SFUNC(smb1);
  423. WPCM450_SFUNC(smb2);
  424. WPCM450_SFUNC(bsp);
  425. WPCM450_SFUNC(hsp1);
  426. WPCM450_SFUNC(hsp2);
  427. WPCM450_SFUNC(r1err);
  428. WPCM450_SFUNC(r1md);
  429. WPCM450_SFUNC(rmii2);
  430. WPCM450_SFUNC(r2err);
  431. WPCM450_SFUNC(r2md);
  432. WPCM450_SFUNC(kbcc);
  433. WPCM450_SFUNC(clko);
  434. WPCM450_SFUNC(smi);
  435. WPCM450_SFUNC(uinc);
  436. WPCM450_SFUNC(gspi);
  437. WPCM450_SFUNC(mben);
  438. WPCM450_SFUNC(xcs2);
  439. WPCM450_SFUNC(xcs1);
  440. WPCM450_SFUNC(sdio);
  441. WPCM450_SFUNC(sspi);
  442. WPCM450_SFUNC(fi0);
  443. WPCM450_SFUNC(fi1);
  444. WPCM450_SFUNC(fi2);
  445. WPCM450_SFUNC(fi3);
  446. WPCM450_SFUNC(fi4);
  447. WPCM450_SFUNC(fi5);
  448. WPCM450_SFUNC(fi6);
  449. WPCM450_SFUNC(fi7);
  450. WPCM450_SFUNC(fi8);
  451. WPCM450_SFUNC(fi9);
  452. WPCM450_SFUNC(fi10);
  453. WPCM450_SFUNC(fi11);
  454. WPCM450_SFUNC(fi12);
  455. WPCM450_SFUNC(fi13);
  456. WPCM450_SFUNC(fi14);
  457. WPCM450_SFUNC(fi15);
  458. WPCM450_SFUNC(pwm0);
  459. WPCM450_SFUNC(pwm1);
  460. WPCM450_SFUNC(pwm2);
  461. WPCM450_SFUNC(pwm3);
  462. WPCM450_SFUNC(pwm4);
  463. WPCM450_SFUNC(pwm5);
  464. WPCM450_SFUNC(pwm6);
  465. WPCM450_SFUNC(pwm7);
  466. WPCM450_SFUNC(hg0);
  467. WPCM450_SFUNC(hg1);
  468. WPCM450_SFUNC(hg2);
  469. WPCM450_SFUNC(hg3);
  470. WPCM450_SFUNC(hg4);
  471. WPCM450_SFUNC(hg5);
  472. WPCM450_SFUNC(hg6);
  473. WPCM450_SFUNC(hg7);
  474. #define WPCM450_GRP(x) #x
  475. WPCM450_FUNC(gpio, WPCM450_GRPS);
  476. #undef WPCM450_GRP
  477. /* Function names */
  478. static struct wpcm450_func wpcm450_funcs[] = {
  479. WPCM450_MKFUNC(smb3),
  480. WPCM450_MKFUNC(smb4),
  481. WPCM450_MKFUNC(smb5),
  482. WPCM450_MKFUNC(scs1),
  483. WPCM450_MKFUNC(scs2),
  484. WPCM450_MKFUNC(scs3),
  485. WPCM450_MKFUNC(smb0),
  486. WPCM450_MKFUNC(smb1),
  487. WPCM450_MKFUNC(smb2),
  488. WPCM450_MKFUNC(bsp),
  489. WPCM450_MKFUNC(hsp1),
  490. WPCM450_MKFUNC(hsp2),
  491. WPCM450_MKFUNC(r1err),
  492. WPCM450_MKFUNC(r1md),
  493. WPCM450_MKFUNC(rmii2),
  494. WPCM450_MKFUNC(r2err),
  495. WPCM450_MKFUNC(r2md),
  496. WPCM450_MKFUNC(kbcc),
  497. WPCM450_MKFUNC(clko),
  498. WPCM450_MKFUNC(smi),
  499. WPCM450_MKFUNC(uinc),
  500. WPCM450_MKFUNC(gspi),
  501. WPCM450_MKFUNC(mben),
  502. WPCM450_MKFUNC(xcs2),
  503. WPCM450_MKFUNC(xcs1),
  504. WPCM450_MKFUNC(sdio),
  505. WPCM450_MKFUNC(sspi),
  506. WPCM450_MKFUNC(fi0),
  507. WPCM450_MKFUNC(fi1),
  508. WPCM450_MKFUNC(fi2),
  509. WPCM450_MKFUNC(fi3),
  510. WPCM450_MKFUNC(fi4),
  511. WPCM450_MKFUNC(fi5),
  512. WPCM450_MKFUNC(fi6),
  513. WPCM450_MKFUNC(fi7),
  514. WPCM450_MKFUNC(fi8),
  515. WPCM450_MKFUNC(fi9),
  516. WPCM450_MKFUNC(fi10),
  517. WPCM450_MKFUNC(fi11),
  518. WPCM450_MKFUNC(fi12),
  519. WPCM450_MKFUNC(fi13),
  520. WPCM450_MKFUNC(fi14),
  521. WPCM450_MKFUNC(fi15),
  522. WPCM450_MKFUNC(pwm0),
  523. WPCM450_MKFUNC(pwm1),
  524. WPCM450_MKFUNC(pwm2),
  525. WPCM450_MKFUNC(pwm3),
  526. WPCM450_MKFUNC(pwm4),
  527. WPCM450_MKFUNC(pwm5),
  528. WPCM450_MKFUNC(pwm6),
  529. WPCM450_MKFUNC(pwm7),
  530. WPCM450_MKFUNC(hg0),
  531. WPCM450_MKFUNC(hg1),
  532. WPCM450_MKFUNC(hg2),
  533. WPCM450_MKFUNC(hg3),
  534. WPCM450_MKFUNC(hg4),
  535. WPCM450_MKFUNC(hg5),
  536. WPCM450_MKFUNC(hg6),
  537. WPCM450_MKFUNC(hg7),
  538. WPCM450_MKFUNC(gpio),
  539. };
  540. #define WPCM450_PINCFG(a, b, c, d, e, f, g) \
  541. [a] = { .fn0 = fn_ ## b, .reg0 = WPCM450_GCR_ ## c, .bit0 = d, \
  542. .fn1 = fn_ ## e, .reg1 = WPCM450_GCR_ ## f, .bit1 = g }
  543. struct wpcm450_pincfg {
  544. int fn0, reg0, bit0;
  545. int fn1, reg1, bit1;
  546. };
  547. static const struct wpcm450_pincfg pincfg[] = {
  548. /* PIN FUNCTION 1 FUNCTION 2 */
  549. WPCM450_PINCFG(0, none, NONE, 0, none, NONE, 0),
  550. WPCM450_PINCFG(1, none, NONE, 0, none, NONE, 0),
  551. WPCM450_PINCFG(2, none, NONE, 0, none, NONE, 0),
  552. WPCM450_PINCFG(3, none, NONE, 0, none, NONE, 0),
  553. WPCM450_PINCFG(4, none, NONE, 0, none, NONE, 0),
  554. WPCM450_PINCFG(5, none, NONE, 0, none, NONE, 0),
  555. WPCM450_PINCFG(6, none, NONE, 0, none, NONE, 0),
  556. WPCM450_PINCFG(7, none, NONE, 0, sdio, MFSEL1, 30),
  557. WPCM450_PINCFG(8, none, NONE, 0, none, NONE, 0),
  558. WPCM450_PINCFG(9, none, NONE, 0, none, NONE, 0),
  559. WPCM450_PINCFG(10, none, NONE, 0, none, NONE, 0),
  560. WPCM450_PINCFG(11, none, NONE, 0, none, NONE, 0),
  561. WPCM450_PINCFG(12, gspi, MFSEL1, 24, sspi, MFSEL1, 31),
  562. WPCM450_PINCFG(13, gspi, MFSEL1, 24, sspi, MFSEL1, 31),
  563. WPCM450_PINCFG(14, gspi, MFSEL1, 24, sspi, MFSEL1, 31),
  564. WPCM450_PINCFG(15, gspi, MFSEL1, 24, sspi, MFSEL1, 31),
  565. WPCM450_PINCFG(16, none, NONE, 0, pwm6, MFSEL2, 22),
  566. WPCM450_PINCFG(17, none, NONE, 0, pwm7, MFSEL2, 23),
  567. WPCM450_PINCFG(18, none, NONE, 0, none, NONE, 0),
  568. WPCM450_PINCFG(19, uinc, MFSEL1, 23, none, NONE, 0),
  569. WPCM450_PINCFG(20, hg0, MFSEL2, 24, pwm4, MFSEL2, 20),
  570. WPCM450_PINCFG(21, hg1, MFSEL2, 25, pwm5, MFSEL2, 21),
  571. WPCM450_PINCFG(22, hg2, MFSEL2, 26, none, NONE, 0),
  572. WPCM450_PINCFG(23, hg3, MFSEL2, 27, none, NONE, 0),
  573. WPCM450_PINCFG(24, hg4, MFSEL2, 28, none, NONE, 0),
  574. WPCM450_PINCFG(25, hg5, MFSEL2, 29, none, NONE, 0),
  575. WPCM450_PINCFG(26, smb5, MFSEL1, 2, none, NONE, 0),
  576. WPCM450_PINCFG(27, smb5, MFSEL1, 2, none, NONE, 0),
  577. WPCM450_PINCFG(28, smb4, MFSEL1, 1, none, NONE, 0),
  578. WPCM450_PINCFG(29, smb4, MFSEL1, 1, none, NONE, 0),
  579. WPCM450_PINCFG(30, smb3, MFSEL1, 0, none, NONE, 0),
  580. WPCM450_PINCFG(31, smb3, MFSEL1, 0, none, NONE, 0),
  581. WPCM450_PINCFG(32, scs1, MFSEL1, 3, none, NONE, 0),
  582. WPCM450_PINCFG(33, scs2, MFSEL1, 4, none, NONE, 0),
  583. WPCM450_PINCFG(34, scs3, MFSEL1, 5, none, NONE, 0),
  584. WPCM450_PINCFG(35, xcs1, MFSEL1, 29, none, NONE, 0),
  585. WPCM450_PINCFG(36, xcs2, MFSEL1, 28, none, NONE, 0),
  586. WPCM450_PINCFG(37, none, NONE, 0, none, NONE, 0), /* DVO */
  587. WPCM450_PINCFG(38, none, NONE, 0, none, NONE, 0), /* DVO */
  588. WPCM450_PINCFG(39, none, NONE, 0, none, NONE, 0), /* DVO */
  589. WPCM450_PINCFG(40, none, NONE, 0, none, NONE, 0), /* DVO */
  590. WPCM450_PINCFG(41, bsp, MFSEL1, 9, none, NONE, 0),
  591. WPCM450_PINCFG(42, bsp, MFSEL1, 9, none, NONE, 0),
  592. WPCM450_PINCFG(43, hsp1, MFSEL1, 10, sdio, MFSEL1, 30),
  593. WPCM450_PINCFG(44, hsp1, MFSEL1, 10, sdio, MFSEL1, 30),
  594. WPCM450_PINCFG(45, hsp1, MFSEL1, 10, sdio, MFSEL1, 30),
  595. WPCM450_PINCFG(46, hsp1, MFSEL1, 10, sdio, MFSEL1, 30),
  596. WPCM450_PINCFG(47, hsp1, MFSEL1, 10, sdio, MFSEL1, 30),
  597. WPCM450_PINCFG(48, hsp2, MFSEL1, 11, none, NONE, 0),
  598. WPCM450_PINCFG(49, hsp2, MFSEL1, 11, none, NONE, 0),
  599. WPCM450_PINCFG(50, hsp2, MFSEL1, 11, none, NONE, 0),
  600. WPCM450_PINCFG(51, hsp2, MFSEL1, 11, none, NONE, 0),
  601. WPCM450_PINCFG(52, hsp2, MFSEL1, 11, none, NONE, 0),
  602. WPCM450_PINCFG(53, hsp2, MFSEL1, 11, none, NONE, 0),
  603. WPCM450_PINCFG(54, hsp2, MFSEL1, 11, none, NONE, 0),
  604. WPCM450_PINCFG(55, hsp2, MFSEL1, 11, none, NONE, 0),
  605. WPCM450_PINCFG(56, r1err, MFSEL1, 12, none, NONE, 0),
  606. WPCM450_PINCFG(57, r1md, MFSEL1, 13, none, NONE, 0),
  607. WPCM450_PINCFG(58, r1md, MFSEL1, 13, none, NONE, 0),
  608. WPCM450_PINCFG(59, hg6, MFSEL2, 30, none, NONE, 0),
  609. WPCM450_PINCFG(60, hg7, MFSEL2, 31, sdio, MFSEL1, 30),
  610. WPCM450_PINCFG(61, hsp1, MFSEL1, 10, none, NONE, 0),
  611. WPCM450_PINCFG(62, hsp1, MFSEL1, 10, none, NONE, 0),
  612. WPCM450_PINCFG(63, hsp1, MFSEL1, 10, none, NONE, 0),
  613. WPCM450_PINCFG(64, fi0, MFSEL2, 0, none, NONE, 0),
  614. WPCM450_PINCFG(65, fi1, MFSEL2, 1, none, NONE, 0),
  615. WPCM450_PINCFG(66, fi2, MFSEL2, 2, none, NONE, 0),
  616. WPCM450_PINCFG(67, fi3, MFSEL2, 3, none, NONE, 0),
  617. WPCM450_PINCFG(68, fi4, MFSEL2, 4, none, NONE, 0),
  618. WPCM450_PINCFG(69, fi5, MFSEL2, 5, none, NONE, 0),
  619. WPCM450_PINCFG(70, fi6, MFSEL2, 6, none, NONE, 0),
  620. WPCM450_PINCFG(71, fi7, MFSEL2, 7, none, NONE, 0),
  621. WPCM450_PINCFG(72, fi8, MFSEL2, 8, none, NONE, 0),
  622. WPCM450_PINCFG(73, fi9, MFSEL2, 9, none, NONE, 0),
  623. WPCM450_PINCFG(74, fi10, MFSEL2, 10, none, NONE, 0),
  624. WPCM450_PINCFG(75, fi11, MFSEL2, 11, none, NONE, 0),
  625. WPCM450_PINCFG(76, fi12, MFSEL2, 12, none, NONE, 0),
  626. WPCM450_PINCFG(77, fi13, MFSEL2, 13, none, NONE, 0),
  627. WPCM450_PINCFG(78, fi14, MFSEL2, 14, none, NONE, 0),
  628. WPCM450_PINCFG(79, fi15, MFSEL2, 15, none, NONE, 0),
  629. WPCM450_PINCFG(80, pwm0, MFSEL2, 16, none, NONE, 0),
  630. WPCM450_PINCFG(81, pwm1, MFSEL2, 17, none, NONE, 0),
  631. WPCM450_PINCFG(82, pwm2, MFSEL2, 18, none, NONE, 0),
  632. WPCM450_PINCFG(83, pwm3, MFSEL2, 19, none, NONE, 0),
  633. WPCM450_PINCFG(84, rmii2, MFSEL1, 14, none, NONE, 0),
  634. WPCM450_PINCFG(85, rmii2, MFSEL1, 14, none, NONE, 0),
  635. WPCM450_PINCFG(86, rmii2, MFSEL1, 14, none, NONE, 0),
  636. WPCM450_PINCFG(87, rmii2, MFSEL1, 14, none, NONE, 0),
  637. WPCM450_PINCFG(88, rmii2, MFSEL1, 14, none, NONE, 0),
  638. WPCM450_PINCFG(89, rmii2, MFSEL1, 14, none, NONE, 0),
  639. WPCM450_PINCFG(90, r2err, MFSEL1, 15, none, NONE, 0),
  640. WPCM450_PINCFG(91, r2md, MFSEL1, 16, none, NONE, 0),
  641. WPCM450_PINCFG(92, r2md, MFSEL1, 16, none, NONE, 0),
  642. WPCM450_PINCFG(93, kbcc, MFSEL1, 17, none, NONE, 0),
  643. WPCM450_PINCFG(94, kbcc, MFSEL1, 17, none, NONE, 0),
  644. WPCM450_PINCFG(95, none, NONE, 0, none, NONE, 0),
  645. WPCM450_PINCFG(96, none, NONE, 0, none, NONE, 0),
  646. WPCM450_PINCFG(97, none, NONE, 0, none, NONE, 0),
  647. WPCM450_PINCFG(98, none, NONE, 0, none, NONE, 0),
  648. WPCM450_PINCFG(99, none, NONE, 0, none, NONE, 0),
  649. WPCM450_PINCFG(100, none, NONE, 0, none, NONE, 0),
  650. WPCM450_PINCFG(101, none, NONE, 0, none, NONE, 0),
  651. WPCM450_PINCFG(102, none, NONE, 0, none, NONE, 0),
  652. WPCM450_PINCFG(103, none, NONE, 0, none, NONE, 0),
  653. WPCM450_PINCFG(104, none, NONE, 0, none, NONE, 0),
  654. WPCM450_PINCFG(105, none, NONE, 0, none, NONE, 0),
  655. WPCM450_PINCFG(106, none, NONE, 0, none, NONE, 0),
  656. WPCM450_PINCFG(107, none, NONE, 0, none, NONE, 0),
  657. WPCM450_PINCFG(108, none, NONE, 0, none, NONE, 0), /* DVO */
  658. WPCM450_PINCFG(109, none, NONE, 0, none, NONE, 0), /* DVO */
  659. WPCM450_PINCFG(110, none, NONE, 0, none, NONE, 0), /* DVO */
  660. WPCM450_PINCFG(111, none, NONE, 0, none, NONE, 0), /* DVO */
  661. WPCM450_PINCFG(112, none, NONE, 0, none, NONE, 0), /* DVO */
  662. WPCM450_PINCFG(113, none, NONE, 0, none, NONE, 0), /* DVO */
  663. WPCM450_PINCFG(114, smb0, MFSEL1, 6, none, NONE, 0),
  664. WPCM450_PINCFG(115, smb0, MFSEL1, 6, none, NONE, 0),
  665. WPCM450_PINCFG(116, smb1, MFSEL1, 7, none, NONE, 0),
  666. WPCM450_PINCFG(117, smb1, MFSEL1, 7, none, NONE, 0),
  667. WPCM450_PINCFG(118, smb2, MFSEL1, 8, none, NONE, 0),
  668. WPCM450_PINCFG(119, smb2, MFSEL1, 8, none, NONE, 0),
  669. WPCM450_PINCFG(120, none, NONE, 0, none, NONE, 0), /* DVO */
  670. WPCM450_PINCFG(121, none, NONE, 0, none, NONE, 0), /* DVO */
  671. WPCM450_PINCFG(122, none, NONE, 0, none, NONE, 0), /* DVO */
  672. WPCM450_PINCFG(123, none, NONE, 0, none, NONE, 0), /* DVO */
  673. WPCM450_PINCFG(124, none, NONE, 0, none, NONE, 0), /* DVO */
  674. WPCM450_PINCFG(125, none, NONE, 0, none, NONE, 0), /* DVO */
  675. WPCM450_PINCFG(126, none, NONE, 0, none, NONE, 0), /* DVO */
  676. WPCM450_PINCFG(127, none, NONE, 0, none, NONE, 0), /* DVO */
  677. };
  678. #define WPCM450_PIN(n) PINCTRL_PIN(n, "gpio" #n)
  679. static const struct pinctrl_pin_desc wpcm450_pins[] = {
  680. WPCM450_PIN(0), WPCM450_PIN(1), WPCM450_PIN(2), WPCM450_PIN(3),
  681. WPCM450_PIN(4), WPCM450_PIN(5), WPCM450_PIN(6), WPCM450_PIN(7),
  682. WPCM450_PIN(8), WPCM450_PIN(9), WPCM450_PIN(10), WPCM450_PIN(11),
  683. WPCM450_PIN(12), WPCM450_PIN(13), WPCM450_PIN(14), WPCM450_PIN(15),
  684. WPCM450_PIN(16), WPCM450_PIN(17), WPCM450_PIN(18), WPCM450_PIN(19),
  685. WPCM450_PIN(20), WPCM450_PIN(21), WPCM450_PIN(22), WPCM450_PIN(23),
  686. WPCM450_PIN(24), WPCM450_PIN(25), WPCM450_PIN(26), WPCM450_PIN(27),
  687. WPCM450_PIN(28), WPCM450_PIN(29), WPCM450_PIN(30), WPCM450_PIN(31),
  688. WPCM450_PIN(32), WPCM450_PIN(33), WPCM450_PIN(34), WPCM450_PIN(35),
  689. WPCM450_PIN(36), WPCM450_PIN(37), WPCM450_PIN(38), WPCM450_PIN(39),
  690. WPCM450_PIN(40), WPCM450_PIN(41), WPCM450_PIN(42), WPCM450_PIN(43),
  691. WPCM450_PIN(44), WPCM450_PIN(45), WPCM450_PIN(46), WPCM450_PIN(47),
  692. WPCM450_PIN(48), WPCM450_PIN(49), WPCM450_PIN(50), WPCM450_PIN(51),
  693. WPCM450_PIN(52), WPCM450_PIN(53), WPCM450_PIN(54), WPCM450_PIN(55),
  694. WPCM450_PIN(56), WPCM450_PIN(57), WPCM450_PIN(58), WPCM450_PIN(59),
  695. WPCM450_PIN(60), WPCM450_PIN(61), WPCM450_PIN(62), WPCM450_PIN(63),
  696. WPCM450_PIN(64), WPCM450_PIN(65), WPCM450_PIN(66), WPCM450_PIN(67),
  697. WPCM450_PIN(68), WPCM450_PIN(69), WPCM450_PIN(70), WPCM450_PIN(71),
  698. WPCM450_PIN(72), WPCM450_PIN(73), WPCM450_PIN(74), WPCM450_PIN(75),
  699. WPCM450_PIN(76), WPCM450_PIN(77), WPCM450_PIN(78), WPCM450_PIN(79),
  700. WPCM450_PIN(80), WPCM450_PIN(81), WPCM450_PIN(82), WPCM450_PIN(83),
  701. WPCM450_PIN(84), WPCM450_PIN(85), WPCM450_PIN(86), WPCM450_PIN(87),
  702. WPCM450_PIN(88), WPCM450_PIN(89), WPCM450_PIN(90), WPCM450_PIN(91),
  703. WPCM450_PIN(92), WPCM450_PIN(93), WPCM450_PIN(94), WPCM450_PIN(95),
  704. WPCM450_PIN(96), WPCM450_PIN(97), WPCM450_PIN(98), WPCM450_PIN(99),
  705. WPCM450_PIN(100), WPCM450_PIN(101), WPCM450_PIN(102), WPCM450_PIN(103),
  706. WPCM450_PIN(104), WPCM450_PIN(105), WPCM450_PIN(106), WPCM450_PIN(107),
  707. WPCM450_PIN(108), WPCM450_PIN(109), WPCM450_PIN(110), WPCM450_PIN(111),
  708. WPCM450_PIN(112), WPCM450_PIN(113), WPCM450_PIN(114), WPCM450_PIN(115),
  709. WPCM450_PIN(116), WPCM450_PIN(117), WPCM450_PIN(118), WPCM450_PIN(119),
  710. WPCM450_PIN(120), WPCM450_PIN(121), WPCM450_PIN(122), WPCM450_PIN(123),
  711. WPCM450_PIN(124), WPCM450_PIN(125), WPCM450_PIN(126), WPCM450_PIN(127),
  712. };
  713. /* Enable mode in pin group */
  714. static void wpcm450_setfunc(struct regmap *gcr_regmap, const unsigned int *pin,
  715. int npins, int func)
  716. {
  717. const struct wpcm450_pincfg *cfg;
  718. int i;
  719. for (i = 0; i < npins; i++) {
  720. cfg = &pincfg[pin[i]];
  721. if (func == fn_gpio || cfg->fn0 == func || cfg->fn1 == func) {
  722. if (cfg->reg0)
  723. regmap_update_bits(gcr_regmap, cfg->reg0,
  724. BIT(cfg->bit0),
  725. (cfg->fn0 == func) ? BIT(cfg->bit0) : 0);
  726. if (cfg->reg1)
  727. regmap_update_bits(gcr_regmap, cfg->reg1,
  728. BIT(cfg->bit1),
  729. (cfg->fn1 == func) ? BIT(cfg->bit1) : 0);
  730. }
  731. }
  732. }
  733. static int wpcm450_get_groups_count(struct pinctrl_dev *pctldev)
  734. {
  735. return ARRAY_SIZE(wpcm450_groups);
  736. }
  737. static const char *wpcm450_get_group_name(struct pinctrl_dev *pctldev,
  738. unsigned int selector)
  739. {
  740. return wpcm450_groups[selector].name;
  741. }
  742. static int wpcm450_get_group_pins(struct pinctrl_dev *pctldev,
  743. unsigned int selector,
  744. const unsigned int **pins,
  745. unsigned int *npins)
  746. {
  747. *npins = wpcm450_groups[selector].num_pins;
  748. *pins = wpcm450_groups[selector].pins;
  749. return 0;
  750. }
  751. static int wpcm450_dt_node_to_map(struct pinctrl_dev *pctldev,
  752. struct device_node *np_config,
  753. struct pinctrl_map **map,
  754. u32 *num_maps)
  755. {
  756. return pinconf_generic_dt_node_to_map(pctldev, np_config,
  757. map, num_maps,
  758. PIN_MAP_TYPE_INVALID);
  759. }
  760. static void wpcm450_dt_free_map(struct pinctrl_dev *pctldev,
  761. struct pinctrl_map *map, u32 num_maps)
  762. {
  763. kfree(map);
  764. }
  765. static const struct pinctrl_ops wpcm450_pinctrl_ops = {
  766. .get_groups_count = wpcm450_get_groups_count,
  767. .get_group_name = wpcm450_get_group_name,
  768. .get_group_pins = wpcm450_get_group_pins,
  769. .dt_node_to_map = wpcm450_dt_node_to_map,
  770. .dt_free_map = wpcm450_dt_free_map,
  771. };
  772. static int wpcm450_get_functions_count(struct pinctrl_dev *pctldev)
  773. {
  774. return ARRAY_SIZE(wpcm450_funcs);
  775. }
  776. static const char *wpcm450_get_function_name(struct pinctrl_dev *pctldev,
  777. unsigned int function)
  778. {
  779. return wpcm450_funcs[function].name;
  780. }
  781. static int wpcm450_get_function_groups(struct pinctrl_dev *pctldev,
  782. unsigned int function,
  783. const char * const **groups,
  784. unsigned int * const ngroups)
  785. {
  786. *ngroups = wpcm450_funcs[function].ngroups;
  787. *groups = wpcm450_funcs[function].groups;
  788. return 0;
  789. }
  790. static int wpcm450_pinmux_set_mux(struct pinctrl_dev *pctldev,
  791. unsigned int function,
  792. unsigned int group)
  793. {
  794. struct wpcm450_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  795. wpcm450_setfunc(pctrl->gcr_regmap, wpcm450_groups[group].pins,
  796. wpcm450_groups[group].num_pins, function);
  797. return 0;
  798. }
  799. static const struct pinmux_ops wpcm450_pinmux_ops = {
  800. .get_functions_count = wpcm450_get_functions_count,
  801. .get_function_name = wpcm450_get_function_name,
  802. .get_function_groups = wpcm450_get_function_groups,
  803. .set_mux = wpcm450_pinmux_set_mux,
  804. };
  805. static int debounce_bitnum(int gpio)
  806. {
  807. if (gpio >= 0 && gpio < 16)
  808. return gpio;
  809. return -EINVAL;
  810. }
  811. static int wpcm450_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
  812. unsigned long *config)
  813. {
  814. struct wpcm450_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  815. enum pin_config_param param = pinconf_to_config_param(*config);
  816. unsigned long flags;
  817. int bit;
  818. u32 reg;
  819. switch (param) {
  820. case PIN_CONFIG_INPUT_DEBOUNCE:
  821. bit = debounce_bitnum(pin);
  822. if (bit < 0)
  823. return bit;
  824. raw_spin_lock_irqsave(&pctrl->lock, flags);
  825. reg = ioread32(pctrl->gpio_base + WPCM450_GPEVDBNC);
  826. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  827. *config = pinconf_to_config_packed(param, !!(reg & BIT(bit)));
  828. return 0;
  829. default:
  830. return -ENOTSUPP;
  831. }
  832. }
  833. static int wpcm450_config_set_one(struct wpcm450_pinctrl *pctrl,
  834. unsigned int pin, unsigned long config)
  835. {
  836. enum pin_config_param param = pinconf_to_config_param(config);
  837. unsigned long flags;
  838. unsigned long reg;
  839. int bit;
  840. int arg;
  841. switch (param) {
  842. case PIN_CONFIG_INPUT_DEBOUNCE:
  843. bit = debounce_bitnum(pin);
  844. if (bit < 0)
  845. return bit;
  846. arg = pinconf_to_config_argument(config);
  847. raw_spin_lock_irqsave(&pctrl->lock, flags);
  848. reg = ioread32(pctrl->gpio_base + WPCM450_GPEVDBNC);
  849. __assign_bit(bit, &reg, arg);
  850. iowrite32(reg, pctrl->gpio_base + WPCM450_GPEVDBNC);
  851. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  852. return 0;
  853. default:
  854. return -ENOTSUPP;
  855. }
  856. }
  857. static int wpcm450_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  858. unsigned long *configs, unsigned int num_configs)
  859. {
  860. struct wpcm450_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  861. int ret;
  862. while (num_configs--) {
  863. ret = wpcm450_config_set_one(pctrl, pin, *configs++);
  864. if (ret)
  865. return ret;
  866. }
  867. return 0;
  868. }
  869. static const struct pinconf_ops wpcm450_pinconf_ops = {
  870. .is_generic = true,
  871. .pin_config_get = wpcm450_config_get,
  872. .pin_config_set = wpcm450_config_set,
  873. };
  874. static struct pinctrl_desc wpcm450_pinctrl_desc = {
  875. .name = "wpcm450-pinctrl",
  876. .pins = wpcm450_pins,
  877. .npins = ARRAY_SIZE(wpcm450_pins),
  878. .pctlops = &wpcm450_pinctrl_ops,
  879. .pmxops = &wpcm450_pinmux_ops,
  880. .confops = &wpcm450_pinconf_ops,
  881. .owner = THIS_MODULE,
  882. };
  883. static int wpcm450_gpio_set_config(struct gpio_chip *chip,
  884. unsigned int offset, unsigned long config)
  885. {
  886. struct wpcm450_gpio *gpio = gpiochip_get_data(chip);
  887. return wpcm450_config_set_one(gpio->pctrl, offset, config);
  888. }
  889. static int wpcm450_gpio_add_pin_ranges(struct gpio_chip *chip)
  890. {
  891. struct wpcm450_gpio *gpio = gpiochip_get_data(chip);
  892. const struct wpcm450_bank *bank = gpio->bank;
  893. return gpiochip_add_pin_range(&gpio->gc, dev_name(gpio->pctrl->dev),
  894. 0, bank->base, bank->length);
  895. }
  896. static int wpcm450_gpio_register(struct platform_device *pdev,
  897. struct wpcm450_pinctrl *pctrl)
  898. {
  899. struct device *dev = &pdev->dev;
  900. struct fwnode_handle *child;
  901. int ret;
  902. pctrl->gpio_base = devm_platform_ioremap_resource(pdev, 0);
  903. if (IS_ERR(pctrl->gpio_base))
  904. return dev_err_probe(dev, PTR_ERR(pctrl->gpio_base),
  905. "Resource fail for GPIO controller\n");
  906. device_for_each_child_node(dev, child) {
  907. void __iomem *dat = NULL;
  908. void __iomem *set = NULL;
  909. void __iomem *dirout = NULL;
  910. unsigned long flags = 0;
  911. const struct wpcm450_bank *bank;
  912. struct wpcm450_gpio *gpio;
  913. struct gpio_irq_chip *girq;
  914. u32 reg;
  915. int i;
  916. if (!fwnode_property_read_bool(child, "gpio-controller"))
  917. continue;
  918. ret = fwnode_property_read_u32(child, "reg", &reg);
  919. if (ret < 0)
  920. return ret;
  921. if (reg >= WPCM450_NUM_BANKS)
  922. return dev_err_probe(dev, -EINVAL,
  923. "GPIO index %d out of range!\n", reg);
  924. gpio = &pctrl->gpio_bank[reg];
  925. gpio->pctrl = pctrl;
  926. bank = &wpcm450_banks[reg];
  927. gpio->bank = bank;
  928. dat = pctrl->gpio_base + bank->datain;
  929. if (bank->dataout) {
  930. set = pctrl->gpio_base + bank->dataout;
  931. dirout = pctrl->gpio_base + bank->cfg0;
  932. } else {
  933. flags = BGPIOF_NO_OUTPUT;
  934. }
  935. ret = bgpio_init(&gpio->gc, dev, 4,
  936. dat, set, NULL, dirout, NULL, flags);
  937. if (ret < 0)
  938. return dev_err_probe(dev, ret, "GPIO initialization failed\n");
  939. gpio->gc.ngpio = bank->length;
  940. gpio->gc.set_config = wpcm450_gpio_set_config;
  941. gpio->gc.fwnode = child;
  942. gpio->gc.add_pin_ranges = wpcm450_gpio_add_pin_ranges;
  943. gpio->irqc = wpcm450_gpio_irqchip;
  944. girq = &gpio->gc.irq;
  945. girq->chip = &gpio->irqc;
  946. girq->parent_handler = wpcm450_gpio_irqhandler;
  947. girq->parents = devm_kcalloc(dev, WPCM450_NUM_GPIO_IRQS,
  948. sizeof(*girq->parents), GFP_KERNEL);
  949. if (!girq->parents)
  950. return -ENOMEM;
  951. girq->default_type = IRQ_TYPE_NONE;
  952. girq->handler = handle_bad_irq;
  953. girq->num_parents = 0;
  954. for (i = 0; i < WPCM450_NUM_GPIO_IRQS; i++) {
  955. int irq;
  956. irq = fwnode_irq_get(child, i);
  957. if (irq < 0)
  958. break;
  959. if (!irq)
  960. continue;
  961. girq->parents[i] = irq;
  962. girq->num_parents++;
  963. }
  964. ret = devm_gpiochip_add_data(dev, &gpio->gc, gpio);
  965. if (ret)
  966. return dev_err_probe(dev, ret, "Failed to add GPIO chip\n");
  967. }
  968. return 0;
  969. }
  970. static int wpcm450_pinctrl_probe(struct platform_device *pdev)
  971. {
  972. struct device *dev = &pdev->dev;
  973. struct wpcm450_pinctrl *pctrl;
  974. int ret;
  975. pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
  976. if (!pctrl)
  977. return -ENOMEM;
  978. pctrl->dev = &pdev->dev;
  979. raw_spin_lock_init(&pctrl->lock);
  980. dev_set_drvdata(dev, pctrl);
  981. pctrl->gcr_regmap =
  982. syscon_regmap_lookup_by_compatible("nuvoton,wpcm450-gcr");
  983. if (IS_ERR(pctrl->gcr_regmap))
  984. return dev_err_probe(dev, PTR_ERR(pctrl->gcr_regmap),
  985. "Failed to find nuvoton,wpcm450-gcr\n");
  986. pctrl->pctldev = devm_pinctrl_register(dev,
  987. &wpcm450_pinctrl_desc, pctrl);
  988. if (IS_ERR(pctrl->pctldev))
  989. return dev_err_probe(dev, PTR_ERR(pctrl->pctldev),
  990. "Failed to register pinctrl device\n");
  991. ret = wpcm450_gpio_register(pdev, pctrl);
  992. if (ret < 0)
  993. return ret;
  994. return 0;
  995. }
  996. static const struct of_device_id wpcm450_pinctrl_match[] = {
  997. { .compatible = "nuvoton,wpcm450-pinctrl" },
  998. { }
  999. };
  1000. MODULE_DEVICE_TABLE(of, wpcm450_pinctrl_match);
  1001. static struct platform_driver wpcm450_pinctrl_driver = {
  1002. .probe = wpcm450_pinctrl_probe,
  1003. .driver = {
  1004. .name = "wpcm450-pinctrl",
  1005. .of_match_table = wpcm450_pinctrl_match,
  1006. },
  1007. };
  1008. module_platform_driver(wpcm450_pinctrl_driver);
  1009. MODULE_LICENSE("GPL v2");
  1010. MODULE_AUTHOR("Jonathan Neuschäfer <[email protected]>");
  1011. MODULE_DESCRIPTION("Nuvoton WPCM450 Pinctrl and GPIO driver");