pinctrl-amd.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GPIO driver for AMD
  4. *
  5. * Copyright (c) 2014,2015 AMD Corporation.
  6. * Authors: Ken Xue <[email protected]>
  7. * Wu, Jeff <[email protected]>
  8. *
  9. */
  10. #include <linux/err.h>
  11. #include <linux/bug.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/compiler.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/log2.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mutex.h>
  24. #include <linux/acpi.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/list.h>
  28. #include <linux/bitops.h>
  29. #include <linux/pinctrl/pinconf.h>
  30. #include <linux/pinctrl/pinconf-generic.h>
  31. #include <linux/pinctrl/pinmux.h>
  32. #include "core.h"
  33. #include "pinctrl-utils.h"
  34. #include "pinctrl-amd.h"
  35. static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
  36. {
  37. unsigned long flags;
  38. u32 pin_reg;
  39. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  40. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  41. pin_reg = readl(gpio_dev->base + offset * 4);
  42. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  43. if (pin_reg & BIT(OUTPUT_ENABLE_OFF))
  44. return GPIO_LINE_DIRECTION_OUT;
  45. return GPIO_LINE_DIRECTION_IN;
  46. }
  47. static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  48. {
  49. unsigned long flags;
  50. u32 pin_reg;
  51. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  52. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  53. pin_reg = readl(gpio_dev->base + offset * 4);
  54. pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
  55. writel(pin_reg, gpio_dev->base + offset * 4);
  56. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  57. return 0;
  58. }
  59. static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  60. int value)
  61. {
  62. u32 pin_reg;
  63. unsigned long flags;
  64. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  65. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  66. pin_reg = readl(gpio_dev->base + offset * 4);
  67. pin_reg |= BIT(OUTPUT_ENABLE_OFF);
  68. if (value)
  69. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  70. else
  71. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  72. writel(pin_reg, gpio_dev->base + offset * 4);
  73. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  74. return 0;
  75. }
  76. static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
  77. {
  78. u32 pin_reg;
  79. unsigned long flags;
  80. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  81. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  82. pin_reg = readl(gpio_dev->base + offset * 4);
  83. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  84. return !!(pin_reg & BIT(PIN_STS_OFF));
  85. }
  86. static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
  87. {
  88. u32 pin_reg;
  89. unsigned long flags;
  90. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  91. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  92. pin_reg = readl(gpio_dev->base + offset * 4);
  93. if (value)
  94. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  95. else
  96. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  97. writel(pin_reg, gpio_dev->base + offset * 4);
  98. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  99. }
  100. static int amd_gpio_set_debounce(struct amd_gpio *gpio_dev, unsigned int offset,
  101. unsigned int debounce)
  102. {
  103. u32 time;
  104. u32 pin_reg;
  105. int ret = 0;
  106. /* Use special handling for Pin0 debounce */
  107. if (offset == 0) {
  108. pin_reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  109. if (pin_reg & INTERNAL_GPIO0_DEBOUNCE)
  110. debounce = 0;
  111. }
  112. pin_reg = readl(gpio_dev->base + offset * 4);
  113. if (debounce) {
  114. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  115. pin_reg &= ~DB_TMR_OUT_MASK;
  116. /*
  117. Debounce Debounce Timer Max
  118. TmrLarge TmrOutUnit Unit Debounce
  119. Time
  120. 0 0 61 usec (2 RtcClk) 976 usec
  121. 0 1 244 usec (8 RtcClk) 3.9 msec
  122. 1 0 15.6 msec (512 RtcClk) 250 msec
  123. 1 1 62.5 msec (2048 RtcClk) 1 sec
  124. */
  125. if (debounce < 61) {
  126. pin_reg |= 1;
  127. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  128. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  129. } else if (debounce < 976) {
  130. time = debounce / 61;
  131. pin_reg |= time & DB_TMR_OUT_MASK;
  132. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  133. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  134. } else if (debounce < 3900) {
  135. time = debounce / 244;
  136. pin_reg |= time & DB_TMR_OUT_MASK;
  137. pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
  138. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  139. } else if (debounce < 250000) {
  140. time = debounce / 15625;
  141. pin_reg |= time & DB_TMR_OUT_MASK;
  142. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  143. pin_reg |= BIT(DB_TMR_LARGE_OFF);
  144. } else if (debounce < 1000000) {
  145. time = debounce / 62500;
  146. pin_reg |= time & DB_TMR_OUT_MASK;
  147. pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
  148. pin_reg |= BIT(DB_TMR_LARGE_OFF);
  149. } else {
  150. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  151. ret = -EINVAL;
  152. }
  153. } else {
  154. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  155. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  156. pin_reg &= ~DB_TMR_OUT_MASK;
  157. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  158. }
  159. writel(pin_reg, gpio_dev->base + offset * 4);
  160. return ret;
  161. }
  162. #ifdef CONFIG_DEBUG_FS
  163. static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  164. {
  165. u32 pin_reg;
  166. u32 db_cntrl;
  167. unsigned long flags;
  168. unsigned int bank, i, pin_num;
  169. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  170. bool tmr_out_unit;
  171. bool tmr_large;
  172. char *level_trig;
  173. char *active_level;
  174. char *interrupt_mask;
  175. char *wake_cntrl0;
  176. char *wake_cntrl1;
  177. char *wake_cntrl2;
  178. char *pin_sts;
  179. char *interrupt_sts;
  180. char *wake_sts;
  181. char *orientation;
  182. char debounce_value[40];
  183. char *debounce_enable;
  184. char *wake_cntrlz;
  185. seq_printf(s, "WAKE_INT_MASTER_REG: 0x%08x\n", readl(gpio_dev->base + WAKE_INT_MASTER_REG));
  186. for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
  187. unsigned int time = 0;
  188. unsigned int unit = 0;
  189. switch (bank) {
  190. case 0:
  191. i = 0;
  192. pin_num = AMD_GPIO_PINS_BANK0;
  193. break;
  194. case 1:
  195. i = 64;
  196. pin_num = AMD_GPIO_PINS_BANK1 + i;
  197. break;
  198. case 2:
  199. i = 128;
  200. pin_num = AMD_GPIO_PINS_BANK2 + i;
  201. break;
  202. case 3:
  203. i = 192;
  204. pin_num = AMD_GPIO_PINS_BANK3 + i;
  205. break;
  206. default:
  207. /* Illegal bank number, ignore */
  208. continue;
  209. }
  210. seq_printf(s, "GPIO bank%d\n", bank);
  211. seq_puts(s, "gpio\t int|active|trigger|S0i3| S3|S4/S5| Z|wake|pull| orient| debounce|reg\n");
  212. for (; i < pin_num; i++) {
  213. seq_printf(s, "#%d\t", i);
  214. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  215. pin_reg = readl(gpio_dev->base + i * 4);
  216. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  217. if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
  218. u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
  219. ACTIVE_LEVEL_MASK;
  220. if (level == ACTIVE_LEVEL_HIGH)
  221. active_level = "↑";
  222. else if (level == ACTIVE_LEVEL_LOW)
  223. active_level = "↓";
  224. else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
  225. level == ACTIVE_LEVEL_BOTH)
  226. active_level = "b";
  227. else
  228. active_level = "?";
  229. if (pin_reg & BIT(LEVEL_TRIG_OFF))
  230. level_trig = "level";
  231. else
  232. level_trig = " edge";
  233. if (pin_reg & BIT(INTERRUPT_MASK_OFF))
  234. interrupt_mask = "😛";
  235. else
  236. interrupt_mask = "😷";
  237. if (pin_reg & BIT(INTERRUPT_STS_OFF))
  238. interrupt_sts = "🔥";
  239. else
  240. interrupt_sts = " ";
  241. seq_printf(s, "%s %s| %s| %s|",
  242. interrupt_sts,
  243. interrupt_mask,
  244. active_level,
  245. level_trig);
  246. } else
  247. seq_puts(s, " ∅| | |");
  248. if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
  249. wake_cntrl0 = "⏰";
  250. else
  251. wake_cntrl0 = " ";
  252. seq_printf(s, " %s| ", wake_cntrl0);
  253. if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
  254. wake_cntrl1 = "⏰";
  255. else
  256. wake_cntrl1 = " ";
  257. seq_printf(s, "%s|", wake_cntrl1);
  258. if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
  259. wake_cntrl2 = "⏰";
  260. else
  261. wake_cntrl2 = " ";
  262. seq_printf(s, " %s|", wake_cntrl2);
  263. if (pin_reg & BIT(WAKECNTRL_Z_OFF))
  264. wake_cntrlz = "⏰";
  265. else
  266. wake_cntrlz = " ";
  267. seq_printf(s, "%s|", wake_cntrlz);
  268. if (pin_reg & BIT(WAKE_STS_OFF))
  269. wake_sts = "🔥";
  270. else
  271. wake_sts = " ";
  272. seq_printf(s, " %s|", wake_sts);
  273. if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
  274. seq_puts(s, " ↑ |");
  275. } else if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF)) {
  276. seq_puts(s, " ↓ |");
  277. } else {
  278. seq_puts(s, " |");
  279. }
  280. if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
  281. pin_sts = "output";
  282. if (pin_reg & BIT(OUTPUT_VALUE_OFF))
  283. orientation = "↑";
  284. else
  285. orientation = "↓";
  286. } else {
  287. pin_sts = "input ";
  288. if (pin_reg & BIT(PIN_STS_OFF))
  289. orientation = "↑";
  290. else
  291. orientation = "↓";
  292. }
  293. seq_printf(s, "%s %s|", pin_sts, orientation);
  294. db_cntrl = (DB_CNTRl_MASK << DB_CNTRL_OFF) & pin_reg;
  295. if (db_cntrl) {
  296. tmr_out_unit = pin_reg & BIT(DB_TMR_OUT_UNIT_OFF);
  297. tmr_large = pin_reg & BIT(DB_TMR_LARGE_OFF);
  298. time = pin_reg & DB_TMR_OUT_MASK;
  299. if (tmr_large) {
  300. if (tmr_out_unit)
  301. unit = 62500;
  302. else
  303. unit = 15625;
  304. } else {
  305. if (tmr_out_unit)
  306. unit = 244;
  307. else
  308. unit = 61;
  309. }
  310. if ((DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF) == db_cntrl)
  311. debounce_enable = "b";
  312. else if ((DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF) == db_cntrl)
  313. debounce_enable = "↓";
  314. else
  315. debounce_enable = "↑";
  316. snprintf(debounce_value, sizeof(debounce_value), "%06u", time * unit);
  317. seq_printf(s, "%s (🕑 %sus)|", debounce_enable, debounce_value);
  318. } else {
  319. seq_puts(s, " |");
  320. }
  321. seq_printf(s, "0x%x\n", pin_reg);
  322. }
  323. }
  324. }
  325. #else
  326. #define amd_gpio_dbg_show NULL
  327. #endif
  328. static void amd_gpio_irq_enable(struct irq_data *d)
  329. {
  330. u32 pin_reg;
  331. unsigned long flags;
  332. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  333. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  334. gpiochip_enable_irq(gc, d->hwirq);
  335. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  336. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  337. pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
  338. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  339. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  340. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  341. }
  342. static void amd_gpio_irq_disable(struct irq_data *d)
  343. {
  344. u32 pin_reg;
  345. unsigned long flags;
  346. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  347. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  348. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  349. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  350. pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
  351. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  352. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  353. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  354. gpiochip_disable_irq(gc, d->hwirq);
  355. }
  356. static void amd_gpio_irq_mask(struct irq_data *d)
  357. {
  358. u32 pin_reg;
  359. unsigned long flags;
  360. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  361. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  362. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  363. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  364. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  365. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  366. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  367. }
  368. static void amd_gpio_irq_unmask(struct irq_data *d)
  369. {
  370. u32 pin_reg;
  371. unsigned long flags;
  372. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  373. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  374. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  375. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  376. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  377. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  378. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  379. }
  380. static int amd_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  381. {
  382. u32 pin_reg;
  383. unsigned long flags;
  384. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  385. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  386. u32 wake_mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3);
  387. int err;
  388. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  389. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  390. if (on)
  391. pin_reg |= wake_mask;
  392. else
  393. pin_reg &= ~wake_mask;
  394. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  395. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  396. if (on)
  397. err = enable_irq_wake(gpio_dev->irq);
  398. else
  399. err = disable_irq_wake(gpio_dev->irq);
  400. if (err)
  401. dev_err(&gpio_dev->pdev->dev, "failed to %s wake-up interrupt\n",
  402. on ? "enable" : "disable");
  403. return 0;
  404. }
  405. static void amd_gpio_irq_eoi(struct irq_data *d)
  406. {
  407. u32 reg;
  408. unsigned long flags;
  409. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  410. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  411. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  412. reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  413. reg |= EOI_MASK;
  414. writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
  415. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  416. }
  417. static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  418. {
  419. int ret = 0;
  420. u32 pin_reg, pin_reg_irq_en, mask;
  421. unsigned long flags;
  422. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  423. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  424. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  425. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  426. switch (type & IRQ_TYPE_SENSE_MASK) {
  427. case IRQ_TYPE_EDGE_RISING:
  428. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  429. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  430. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  431. irq_set_handler_locked(d, handle_edge_irq);
  432. break;
  433. case IRQ_TYPE_EDGE_FALLING:
  434. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  435. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  436. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  437. irq_set_handler_locked(d, handle_edge_irq);
  438. break;
  439. case IRQ_TYPE_EDGE_BOTH:
  440. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  441. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  442. pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
  443. irq_set_handler_locked(d, handle_edge_irq);
  444. break;
  445. case IRQ_TYPE_LEVEL_HIGH:
  446. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  447. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  448. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  449. irq_set_handler_locked(d, handle_level_irq);
  450. break;
  451. case IRQ_TYPE_LEVEL_LOW:
  452. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  453. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  454. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  455. irq_set_handler_locked(d, handle_level_irq);
  456. break;
  457. case IRQ_TYPE_NONE:
  458. break;
  459. default:
  460. dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
  461. ret = -EINVAL;
  462. }
  463. pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
  464. /*
  465. * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
  466. * debounce registers of any GPIO will block wake/interrupt status
  467. * generation for *all* GPIOs for a length of time that depends on
  468. * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
  469. * INTERRUPT_ENABLE bit will read as 0.
  470. *
  471. * We temporarily enable irq for the GPIO whose configuration is
  472. * changing, and then wait for it to read back as 1 to know when
  473. * debounce has settled and then disable the irq again.
  474. * We do this polling with the spinlock held to ensure other GPIO
  475. * access routines do not read an incorrect value for the irq enable
  476. * bit of other GPIOs. We keep the GPIO masked while polling to avoid
  477. * spurious irqs, and disable the irq again after polling.
  478. */
  479. mask = BIT(INTERRUPT_ENABLE_OFF);
  480. pin_reg_irq_en = pin_reg;
  481. pin_reg_irq_en |= mask;
  482. pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
  483. writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
  484. while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
  485. continue;
  486. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  487. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  488. return ret;
  489. }
  490. static void amd_irq_ack(struct irq_data *d)
  491. {
  492. /*
  493. * based on HW design,there is no need to ack HW
  494. * before handle current irq. But this routine is
  495. * necessary for handle_edge_irq
  496. */
  497. }
  498. static const struct irq_chip amd_gpio_irqchip = {
  499. .name = "amd_gpio",
  500. .irq_ack = amd_irq_ack,
  501. .irq_enable = amd_gpio_irq_enable,
  502. .irq_disable = amd_gpio_irq_disable,
  503. .irq_mask = amd_gpio_irq_mask,
  504. .irq_unmask = amd_gpio_irq_unmask,
  505. .irq_set_wake = amd_gpio_irq_set_wake,
  506. .irq_eoi = amd_gpio_irq_eoi,
  507. .irq_set_type = amd_gpio_irq_set_type,
  508. /*
  509. * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
  510. * also generates an IRQ. We need the IRQ so the irq_handler can clear
  511. * the wake event. Otherwise the wake event will never clear and
  512. * prevent the system from suspending.
  513. */
  514. .flags = IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | IRQCHIP_IMMUTABLE,
  515. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  516. };
  517. #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
  518. static bool do_amd_gpio_irq_handler(int irq, void *dev_id)
  519. {
  520. struct amd_gpio *gpio_dev = dev_id;
  521. struct gpio_chip *gc = &gpio_dev->gc;
  522. unsigned int i, irqnr;
  523. unsigned long flags;
  524. u32 __iomem *regs;
  525. bool ret = false;
  526. u32 regval;
  527. u64 status, mask;
  528. /* Read the wake status */
  529. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  530. status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
  531. status <<= 32;
  532. status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
  533. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  534. /* Bit 0-45 contain the relevant status bits */
  535. status &= (1ULL << 46) - 1;
  536. regs = gpio_dev->base;
  537. for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
  538. if (!(status & mask))
  539. continue;
  540. status &= ~mask;
  541. /* Each status bit covers four pins */
  542. for (i = 0; i < 4; i++) {
  543. regval = readl(regs + i);
  544. if (regval & PIN_IRQ_PENDING)
  545. dev_dbg(&gpio_dev->pdev->dev,
  546. "GPIO %d is active: 0x%x",
  547. irqnr + i, regval);
  548. /* caused wake on resume context for shared IRQ */
  549. if (irq < 0 && (regval & BIT(WAKE_STS_OFF)))
  550. return true;
  551. if (!(regval & PIN_IRQ_PENDING) ||
  552. !(regval & BIT(INTERRUPT_MASK_OFF)))
  553. continue;
  554. generic_handle_domain_irq_safe(gc->irq.domain, irqnr + i);
  555. /* Clear interrupt.
  556. * We must read the pin register again, in case the
  557. * value was changed while executing
  558. * generic_handle_domain_irq() above.
  559. * If the line is not an irq, disable it in order to
  560. * avoid a system hang caused by an interrupt storm.
  561. */
  562. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  563. regval = readl(regs + i);
  564. if (!gpiochip_line_is_irq(gc, irqnr + i)) {
  565. regval &= ~BIT(INTERRUPT_MASK_OFF);
  566. dev_dbg(&gpio_dev->pdev->dev,
  567. "Disabling spurious GPIO IRQ %d\n",
  568. irqnr + i);
  569. } else {
  570. ret = true;
  571. }
  572. writel(regval, regs + i);
  573. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  574. }
  575. }
  576. /* did not cause wake on resume context for shared IRQ */
  577. if (irq < 0)
  578. return false;
  579. /* Signal EOI to the GPIO unit */
  580. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  581. regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  582. regval |= EOI_MASK;
  583. writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
  584. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  585. return ret;
  586. }
  587. static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
  588. {
  589. return IRQ_RETVAL(do_amd_gpio_irq_handler(irq, dev_id));
  590. }
  591. static bool __maybe_unused amd_gpio_check_wake(void *dev_id)
  592. {
  593. return do_amd_gpio_irq_handler(-1, dev_id);
  594. }
  595. static int amd_get_groups_count(struct pinctrl_dev *pctldev)
  596. {
  597. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  598. return gpio_dev->ngroups;
  599. }
  600. static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
  601. unsigned group)
  602. {
  603. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  604. return gpio_dev->groups[group].name;
  605. }
  606. static int amd_get_group_pins(struct pinctrl_dev *pctldev,
  607. unsigned group,
  608. const unsigned **pins,
  609. unsigned *num_pins)
  610. {
  611. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  612. *pins = gpio_dev->groups[group].pins;
  613. *num_pins = gpio_dev->groups[group].npins;
  614. return 0;
  615. }
  616. static const struct pinctrl_ops amd_pinctrl_ops = {
  617. .get_groups_count = amd_get_groups_count,
  618. .get_group_name = amd_get_group_name,
  619. .get_group_pins = amd_get_group_pins,
  620. #ifdef CONFIG_OF
  621. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  622. .dt_free_map = pinctrl_utils_free_map,
  623. #endif
  624. };
  625. static int amd_pinconf_get(struct pinctrl_dev *pctldev,
  626. unsigned int pin,
  627. unsigned long *config)
  628. {
  629. u32 pin_reg;
  630. unsigned arg;
  631. unsigned long flags;
  632. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  633. enum pin_config_param param = pinconf_to_config_param(*config);
  634. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  635. pin_reg = readl(gpio_dev->base + pin*4);
  636. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  637. switch (param) {
  638. case PIN_CONFIG_INPUT_DEBOUNCE:
  639. arg = pin_reg & DB_TMR_OUT_MASK;
  640. break;
  641. case PIN_CONFIG_BIAS_PULL_DOWN:
  642. arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
  643. break;
  644. case PIN_CONFIG_BIAS_PULL_UP:
  645. arg = (pin_reg >> PULL_UP_ENABLE_OFF) & BIT(0);
  646. break;
  647. case PIN_CONFIG_DRIVE_STRENGTH:
  648. arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
  649. break;
  650. default:
  651. dev_dbg(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
  652. param);
  653. return -ENOTSUPP;
  654. }
  655. *config = pinconf_to_config_packed(param, arg);
  656. return 0;
  657. }
  658. static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  659. unsigned long *configs, unsigned int num_configs)
  660. {
  661. int i;
  662. u32 arg;
  663. int ret = 0;
  664. u32 pin_reg;
  665. unsigned long flags;
  666. enum pin_config_param param;
  667. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  668. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  669. for (i = 0; i < num_configs; i++) {
  670. param = pinconf_to_config_param(configs[i]);
  671. arg = pinconf_to_config_argument(configs[i]);
  672. pin_reg = readl(gpio_dev->base + pin*4);
  673. switch (param) {
  674. case PIN_CONFIG_INPUT_DEBOUNCE:
  675. ret = amd_gpio_set_debounce(gpio_dev, pin, arg);
  676. goto out_unlock;
  677. case PIN_CONFIG_BIAS_PULL_DOWN:
  678. pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
  679. pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
  680. break;
  681. case PIN_CONFIG_BIAS_PULL_UP:
  682. pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
  683. pin_reg |= (arg & BIT(0)) << PULL_UP_ENABLE_OFF;
  684. break;
  685. case PIN_CONFIG_DRIVE_STRENGTH:
  686. pin_reg &= ~(DRV_STRENGTH_SEL_MASK
  687. << DRV_STRENGTH_SEL_OFF);
  688. pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
  689. << DRV_STRENGTH_SEL_OFF;
  690. break;
  691. default:
  692. dev_dbg(&gpio_dev->pdev->dev,
  693. "Invalid config param %04x\n", param);
  694. ret = -ENOTSUPP;
  695. }
  696. writel(pin_reg, gpio_dev->base + pin*4);
  697. }
  698. out_unlock:
  699. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  700. return ret;
  701. }
  702. static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
  703. unsigned int group,
  704. unsigned long *config)
  705. {
  706. const unsigned *pins;
  707. unsigned npins;
  708. int ret;
  709. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  710. if (ret)
  711. return ret;
  712. if (amd_pinconf_get(pctldev, pins[0], config))
  713. return -ENOTSUPP;
  714. return 0;
  715. }
  716. static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
  717. unsigned group, unsigned long *configs,
  718. unsigned num_configs)
  719. {
  720. const unsigned *pins;
  721. unsigned npins;
  722. int i, ret;
  723. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  724. if (ret)
  725. return ret;
  726. for (i = 0; i < npins; i++) {
  727. if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
  728. return -ENOTSUPP;
  729. }
  730. return 0;
  731. }
  732. static int amd_gpio_set_config(struct gpio_chip *gc, unsigned int pin,
  733. unsigned long config)
  734. {
  735. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  736. return amd_pinconf_set(gpio_dev->pctrl, pin, &config, 1);
  737. }
  738. static const struct pinconf_ops amd_pinconf_ops = {
  739. .pin_config_get = amd_pinconf_get,
  740. .pin_config_set = amd_pinconf_set,
  741. .pin_config_group_get = amd_pinconf_group_get,
  742. .pin_config_group_set = amd_pinconf_group_set,
  743. };
  744. static void amd_gpio_irq_init(struct amd_gpio *gpio_dev)
  745. {
  746. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  747. unsigned long flags;
  748. u32 pin_reg, mask;
  749. int i;
  750. mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) |
  751. BIT(WAKE_CNTRL_OFF_S4);
  752. for (i = 0; i < desc->npins; i++) {
  753. int pin = desc->pins[i].number;
  754. const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
  755. if (!pd)
  756. continue;
  757. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  758. pin_reg = readl(gpio_dev->base + pin * 4);
  759. pin_reg &= ~mask;
  760. writel(pin_reg, gpio_dev->base + pin * 4);
  761. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  762. }
  763. }
  764. #ifdef CONFIG_PM_SLEEP
  765. static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
  766. {
  767. const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
  768. if (!pd)
  769. return false;
  770. /*
  771. * Only restore the pin if it is actually in use by the kernel (or
  772. * by userspace).
  773. */
  774. if (pd->mux_owner || pd->gpio_owner ||
  775. gpiochip_line_is_irq(&gpio_dev->gc, pin))
  776. return true;
  777. return false;
  778. }
  779. static int amd_gpio_suspend(struct device *dev)
  780. {
  781. struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
  782. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  783. unsigned long flags;
  784. int i;
  785. for (i = 0; i < desc->npins; i++) {
  786. int pin = desc->pins[i].number;
  787. if (!amd_gpio_should_save(gpio_dev, pin))
  788. continue;
  789. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  790. gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin * 4) & ~PIN_IRQ_PENDING;
  791. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  792. }
  793. return 0;
  794. }
  795. static int amd_gpio_resume(struct device *dev)
  796. {
  797. struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
  798. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  799. unsigned long flags;
  800. int i;
  801. for (i = 0; i < desc->npins; i++) {
  802. int pin = desc->pins[i].number;
  803. if (!amd_gpio_should_save(gpio_dev, pin))
  804. continue;
  805. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  806. gpio_dev->saved_regs[i] |= readl(gpio_dev->base + pin * 4) & PIN_IRQ_PENDING;
  807. writel(gpio_dev->saved_regs[i], gpio_dev->base + pin * 4);
  808. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  809. }
  810. return 0;
  811. }
  812. static const struct dev_pm_ops amd_gpio_pm_ops = {
  813. SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
  814. amd_gpio_resume)
  815. };
  816. #endif
  817. static int amd_get_functions_count(struct pinctrl_dev *pctldev)
  818. {
  819. return ARRAY_SIZE(pmx_functions);
  820. }
  821. static const char *amd_get_fname(struct pinctrl_dev *pctrldev, unsigned int selector)
  822. {
  823. return pmx_functions[selector].name;
  824. }
  825. static int amd_get_groups(struct pinctrl_dev *pctrldev, unsigned int selector,
  826. const char * const **groups,
  827. unsigned int * const num_groups)
  828. {
  829. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
  830. if (!gpio_dev->iomux_base) {
  831. dev_err(&gpio_dev->pdev->dev, "iomux function %d group not supported\n", selector);
  832. return -EINVAL;
  833. }
  834. *groups = pmx_functions[selector].groups;
  835. *num_groups = pmx_functions[selector].ngroups;
  836. return 0;
  837. }
  838. static int amd_set_mux(struct pinctrl_dev *pctrldev, unsigned int function, unsigned int group)
  839. {
  840. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
  841. struct device *dev = &gpio_dev->pdev->dev;
  842. struct pin_desc *pd;
  843. int ind, index;
  844. if (!gpio_dev->iomux_base)
  845. return -EINVAL;
  846. for (index = 0; index < NSELECTS; index++) {
  847. if (strcmp(gpio_dev->groups[group].name, pmx_functions[function].groups[index]))
  848. continue;
  849. if (readb(gpio_dev->iomux_base + pmx_functions[function].index) ==
  850. FUNCTION_INVALID) {
  851. dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
  852. pmx_functions[function].index);
  853. return -EINVAL;
  854. }
  855. writeb(index, gpio_dev->iomux_base + pmx_functions[function].index);
  856. if (index != (readb(gpio_dev->iomux_base + pmx_functions[function].index) &
  857. FUNCTION_MASK)) {
  858. dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
  859. pmx_functions[function].index);
  860. return -EINVAL;
  861. }
  862. for (ind = 0; ind < gpio_dev->groups[group].npins; ind++) {
  863. if (strncmp(gpio_dev->groups[group].name, "IMX_F", strlen("IMX_F")))
  864. continue;
  865. pd = pin_desc_get(gpio_dev->pctrl, gpio_dev->groups[group].pins[ind]);
  866. pd->mux_owner = gpio_dev->groups[group].name;
  867. }
  868. break;
  869. }
  870. return 0;
  871. }
  872. static const struct pinmux_ops amd_pmxops = {
  873. .get_functions_count = amd_get_functions_count,
  874. .get_function_name = amd_get_fname,
  875. .get_function_groups = amd_get_groups,
  876. .set_mux = amd_set_mux,
  877. };
  878. static struct pinctrl_desc amd_pinctrl_desc = {
  879. .pins = kerncz_pins,
  880. .npins = ARRAY_SIZE(kerncz_pins),
  881. .pctlops = &amd_pinctrl_ops,
  882. .pmxops = &amd_pmxops,
  883. .confops = &amd_pinconf_ops,
  884. .owner = THIS_MODULE,
  885. };
  886. static void amd_get_iomux_res(struct amd_gpio *gpio_dev)
  887. {
  888. struct pinctrl_desc *desc = &amd_pinctrl_desc;
  889. struct device *dev = &gpio_dev->pdev->dev;
  890. int index;
  891. index = device_property_match_string(dev, "pinctrl-resource-names", "iomux");
  892. if (index < 0) {
  893. dev_dbg(dev, "iomux not supported\n");
  894. goto out_no_pinmux;
  895. }
  896. gpio_dev->iomux_base = devm_platform_ioremap_resource(gpio_dev->pdev, index);
  897. if (IS_ERR(gpio_dev->iomux_base)) {
  898. dev_dbg(dev, "iomux not supported %d io resource\n", index);
  899. goto out_no_pinmux;
  900. }
  901. return;
  902. out_no_pinmux:
  903. desc->pmxops = NULL;
  904. }
  905. static int amd_gpio_probe(struct platform_device *pdev)
  906. {
  907. int ret = 0;
  908. struct resource *res;
  909. struct amd_gpio *gpio_dev;
  910. struct gpio_irq_chip *girq;
  911. gpio_dev = devm_kzalloc(&pdev->dev,
  912. sizeof(struct amd_gpio), GFP_KERNEL);
  913. if (!gpio_dev)
  914. return -ENOMEM;
  915. raw_spin_lock_init(&gpio_dev->lock);
  916. gpio_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  917. if (IS_ERR(gpio_dev->base)) {
  918. dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
  919. return PTR_ERR(gpio_dev->base);
  920. }
  921. gpio_dev->irq = platform_get_irq(pdev, 0);
  922. if (gpio_dev->irq < 0)
  923. return gpio_dev->irq;
  924. #ifdef CONFIG_PM_SLEEP
  925. gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
  926. sizeof(*gpio_dev->saved_regs),
  927. GFP_KERNEL);
  928. if (!gpio_dev->saved_regs)
  929. return -ENOMEM;
  930. #endif
  931. gpio_dev->pdev = pdev;
  932. gpio_dev->gc.get_direction = amd_gpio_get_direction;
  933. gpio_dev->gc.direction_input = amd_gpio_direction_input;
  934. gpio_dev->gc.direction_output = amd_gpio_direction_output;
  935. gpio_dev->gc.get = amd_gpio_get_value;
  936. gpio_dev->gc.set = amd_gpio_set_value;
  937. gpio_dev->gc.set_config = amd_gpio_set_config;
  938. gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
  939. gpio_dev->gc.base = -1;
  940. gpio_dev->gc.label = pdev->name;
  941. gpio_dev->gc.owner = THIS_MODULE;
  942. gpio_dev->gc.parent = &pdev->dev;
  943. gpio_dev->gc.ngpio = resource_size(res) / 4;
  944. gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
  945. gpio_dev->groups = kerncz_groups;
  946. gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
  947. amd_pinctrl_desc.name = dev_name(&pdev->dev);
  948. amd_get_iomux_res(gpio_dev);
  949. gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
  950. gpio_dev);
  951. if (IS_ERR(gpio_dev->pctrl)) {
  952. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  953. return PTR_ERR(gpio_dev->pctrl);
  954. }
  955. /* Disable and mask interrupts */
  956. amd_gpio_irq_init(gpio_dev);
  957. girq = &gpio_dev->gc.irq;
  958. gpio_irq_chip_set_chip(girq, &amd_gpio_irqchip);
  959. /* This will let us handle the parent IRQ in the driver */
  960. girq->parent_handler = NULL;
  961. girq->num_parents = 0;
  962. girq->parents = NULL;
  963. girq->default_type = IRQ_TYPE_NONE;
  964. girq->handler = handle_simple_irq;
  965. ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
  966. if (ret)
  967. return ret;
  968. ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
  969. 0, 0, gpio_dev->gc.ngpio);
  970. if (ret) {
  971. dev_err(&pdev->dev, "Failed to add pin range\n");
  972. goto out2;
  973. }
  974. ret = devm_request_irq(&pdev->dev, gpio_dev->irq, amd_gpio_irq_handler,
  975. IRQF_SHARED, KBUILD_MODNAME, gpio_dev);
  976. if (ret)
  977. goto out2;
  978. platform_set_drvdata(pdev, gpio_dev);
  979. acpi_register_wakeup_handler(gpio_dev->irq, amd_gpio_check_wake, gpio_dev);
  980. dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
  981. return ret;
  982. out2:
  983. gpiochip_remove(&gpio_dev->gc);
  984. return ret;
  985. }
  986. static int amd_gpio_remove(struct platform_device *pdev)
  987. {
  988. struct amd_gpio *gpio_dev;
  989. gpio_dev = platform_get_drvdata(pdev);
  990. gpiochip_remove(&gpio_dev->gc);
  991. acpi_unregister_wakeup_handler(amd_gpio_check_wake, gpio_dev);
  992. return 0;
  993. }
  994. #ifdef CONFIG_ACPI
  995. static const struct acpi_device_id amd_gpio_acpi_match[] = {
  996. { "AMD0030", 0 },
  997. { "AMDI0030", 0},
  998. { "AMDI0031", 0},
  999. { },
  1000. };
  1001. MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
  1002. #endif
  1003. static struct platform_driver amd_gpio_driver = {
  1004. .driver = {
  1005. .name = "amd_gpio",
  1006. .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
  1007. #ifdef CONFIG_PM_SLEEP
  1008. .pm = &amd_gpio_pm_ops,
  1009. #endif
  1010. },
  1011. .probe = amd_gpio_probe,
  1012. .remove = amd_gpio_remove,
  1013. };
  1014. module_platform_driver(amd_gpio_driver);
  1015. MODULE_LICENSE("GPL v2");
  1016. MODULE_AUTHOR("Ken Xue <[email protected]>, Jeff Wu <[email protected]>");
  1017. MODULE_DESCRIPTION("AMD GPIO pinctrl driver");