pinctrl-amd.c 34 KB

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