pinctrl-amd.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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/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 <linux/suspend.h>
  33. #include "core.h"
  34. #include "pinctrl-utils.h"
  35. #include "pinctrl-amd.h"
  36. static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
  37. {
  38. unsigned long flags;
  39. u32 pin_reg;
  40. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  41. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  42. pin_reg = readl(gpio_dev->base + offset * 4);
  43. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  44. if (pin_reg & BIT(OUTPUT_ENABLE_OFF))
  45. return GPIO_LINE_DIRECTION_OUT;
  46. return GPIO_LINE_DIRECTION_IN;
  47. }
  48. static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  49. {
  50. unsigned long flags;
  51. u32 pin_reg;
  52. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  53. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  54. pin_reg = readl(gpio_dev->base + offset * 4);
  55. pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
  56. writel(pin_reg, gpio_dev->base + offset * 4);
  57. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  58. return 0;
  59. }
  60. static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  61. int value)
  62. {
  63. u32 pin_reg;
  64. unsigned long flags;
  65. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  66. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  67. pin_reg = readl(gpio_dev->base + offset * 4);
  68. pin_reg |= BIT(OUTPUT_ENABLE_OFF);
  69. if (value)
  70. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  71. else
  72. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  73. writel(pin_reg, gpio_dev->base + offset * 4);
  74. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  75. return 0;
  76. }
  77. static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
  78. {
  79. u32 pin_reg;
  80. unsigned long flags;
  81. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  82. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  83. pin_reg = readl(gpio_dev->base + offset * 4);
  84. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  85. return !!(pin_reg & BIT(PIN_STS_OFF));
  86. }
  87. static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
  88. {
  89. u32 pin_reg;
  90. unsigned long flags;
  91. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  92. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  93. pin_reg = readl(gpio_dev->base + offset * 4);
  94. if (value)
  95. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  96. else
  97. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  98. writel(pin_reg, gpio_dev->base + offset * 4);
  99. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  100. }
  101. static int amd_gpio_set_debounce(struct amd_gpio *gpio_dev, unsigned int offset,
  102. unsigned int debounce)
  103. {
  104. u32 time;
  105. u32 pin_reg;
  106. int ret = 0;
  107. /* Use special handling for Pin0 debounce */
  108. if (offset == 0) {
  109. pin_reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  110. if (pin_reg & INTERNAL_GPIO0_DEBOUNCE)
  111. debounce = 0;
  112. }
  113. pin_reg = readl(gpio_dev->base + offset * 4);
  114. if (debounce) {
  115. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  116. pin_reg &= ~DB_TMR_OUT_MASK;
  117. /*
  118. Debounce Debounce Timer Max
  119. TmrLarge TmrOutUnit Unit Debounce
  120. Time
  121. 0 0 61 usec (2 RtcClk) 976 usec
  122. 0 1 244 usec (8 RtcClk) 3.9 msec
  123. 1 0 15.6 msec (512 RtcClk) 250 msec
  124. 1 1 62.5 msec (2048 RtcClk) 1 sec
  125. */
  126. if (debounce < 61) {
  127. pin_reg |= 1;
  128. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  129. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  130. } else if (debounce < 976) {
  131. time = debounce / 61;
  132. pin_reg |= time & DB_TMR_OUT_MASK;
  133. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  134. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  135. } else if (debounce < 3900) {
  136. time = debounce / 244;
  137. pin_reg |= time & DB_TMR_OUT_MASK;
  138. pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
  139. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  140. } else if (debounce < 250000) {
  141. time = debounce / 15625;
  142. pin_reg |= time & DB_TMR_OUT_MASK;
  143. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  144. pin_reg |= BIT(DB_TMR_LARGE_OFF);
  145. } else if (debounce < 1000000) {
  146. time = debounce / 62500;
  147. pin_reg |= time & DB_TMR_OUT_MASK;
  148. pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
  149. pin_reg |= BIT(DB_TMR_LARGE_OFF);
  150. } else {
  151. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  152. ret = -EINVAL;
  153. }
  154. } else {
  155. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  156. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  157. pin_reg &= ~DB_TMR_OUT_MASK;
  158. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  159. }
  160. writel(pin_reg, gpio_dev->base + offset * 4);
  161. return ret;
  162. }
  163. #ifdef CONFIG_DEBUG_FS
  164. static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  165. {
  166. u32 pin_reg;
  167. u32 db_cntrl;
  168. unsigned long flags;
  169. unsigned int bank, i, pin_num;
  170. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  171. bool tmr_out_unit;
  172. bool tmr_large;
  173. char *level_trig;
  174. char *active_level;
  175. char *interrupt_mask;
  176. char *wake_cntrl0;
  177. char *wake_cntrl1;
  178. char *wake_cntrl2;
  179. char *pin_sts;
  180. char *interrupt_sts;
  181. char *wake_sts;
  182. char *orientation;
  183. char debounce_value[40];
  184. char *debounce_enable;
  185. char *wake_cntrlz;
  186. seq_printf(s, "WAKE_INT_MASTER_REG: 0x%08x\n", readl(gpio_dev->base + WAKE_INT_MASTER_REG));
  187. for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
  188. unsigned int time = 0;
  189. unsigned int unit = 0;
  190. switch (bank) {
  191. case 0:
  192. i = 0;
  193. pin_num = AMD_GPIO_PINS_BANK0;
  194. break;
  195. case 1:
  196. i = 64;
  197. pin_num = AMD_GPIO_PINS_BANK1 + i;
  198. break;
  199. case 2:
  200. i = 128;
  201. pin_num = AMD_GPIO_PINS_BANK2 + i;
  202. break;
  203. case 3:
  204. i = 192;
  205. pin_num = AMD_GPIO_PINS_BANK3 + i;
  206. break;
  207. default:
  208. /* Illegal bank number, ignore */
  209. continue;
  210. }
  211. seq_printf(s, "GPIO bank%d\n", bank);
  212. seq_puts(s, "gpio\t int|active|trigger|S0i3| S3|S4/S5| Z|wake|pull| orient| debounce|reg\n");
  213. for (; i < pin_num; i++) {
  214. seq_printf(s, "#%d\t", i);
  215. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  216. pin_reg = readl(gpio_dev->base + i * 4);
  217. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  218. if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
  219. u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
  220. ACTIVE_LEVEL_MASK;
  221. if (level == ACTIVE_LEVEL_HIGH)
  222. active_level = "↑";
  223. else if (level == ACTIVE_LEVEL_LOW)
  224. active_level = "↓";
  225. else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
  226. level == ACTIVE_LEVEL_BOTH)
  227. active_level = "b";
  228. else
  229. active_level = "?";
  230. if (pin_reg & BIT(LEVEL_TRIG_OFF))
  231. level_trig = "level";
  232. else
  233. level_trig = " edge";
  234. if (pin_reg & BIT(INTERRUPT_MASK_OFF))
  235. interrupt_mask = "😛";
  236. else
  237. interrupt_mask = "😷";
  238. if (pin_reg & BIT(INTERRUPT_STS_OFF))
  239. interrupt_sts = "🔥";
  240. else
  241. interrupt_sts = " ";
  242. seq_printf(s, "%s %s| %s| %s|",
  243. interrupt_sts,
  244. interrupt_mask,
  245. active_level,
  246. level_trig);
  247. } else
  248. seq_puts(s, " ∅| | |");
  249. if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
  250. wake_cntrl0 = "⏰";
  251. else
  252. wake_cntrl0 = " ";
  253. seq_printf(s, " %s| ", wake_cntrl0);
  254. if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
  255. wake_cntrl1 = "⏰";
  256. else
  257. wake_cntrl1 = " ";
  258. seq_printf(s, "%s|", wake_cntrl1);
  259. if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
  260. wake_cntrl2 = "⏰";
  261. else
  262. wake_cntrl2 = " ";
  263. seq_printf(s, " %s|", wake_cntrl2);
  264. if (pin_reg & BIT(WAKECNTRL_Z_OFF))
  265. wake_cntrlz = "⏰";
  266. else
  267. wake_cntrlz = " ";
  268. seq_printf(s, "%s|", wake_cntrlz);
  269. if (pin_reg & BIT(WAKE_STS_OFF))
  270. wake_sts = "🔥";
  271. else
  272. wake_sts = " ";
  273. seq_printf(s, " %s|", wake_sts);
  274. if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
  275. seq_puts(s, " ↑ |");
  276. } else if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF)) {
  277. seq_puts(s, " ↓ |");
  278. } else {
  279. seq_puts(s, " |");
  280. }
  281. if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
  282. pin_sts = "output";
  283. if (pin_reg & BIT(OUTPUT_VALUE_OFF))
  284. orientation = "↑";
  285. else
  286. orientation = "↓";
  287. } else {
  288. pin_sts = "input ";
  289. if (pin_reg & BIT(PIN_STS_OFF))
  290. orientation = "↑";
  291. else
  292. orientation = "↓";
  293. }
  294. seq_printf(s, "%s %s|", pin_sts, orientation);
  295. db_cntrl = (DB_CNTRl_MASK << DB_CNTRL_OFF) & pin_reg;
  296. if (db_cntrl) {
  297. tmr_out_unit = pin_reg & BIT(DB_TMR_OUT_UNIT_OFF);
  298. tmr_large = pin_reg & BIT(DB_TMR_LARGE_OFF);
  299. time = pin_reg & DB_TMR_OUT_MASK;
  300. if (tmr_large) {
  301. if (tmr_out_unit)
  302. unit = 62500;
  303. else
  304. unit = 15625;
  305. } else {
  306. if (tmr_out_unit)
  307. unit = 244;
  308. else
  309. unit = 61;
  310. }
  311. if ((DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF) == db_cntrl)
  312. debounce_enable = "b";
  313. else if ((DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF) == db_cntrl)
  314. debounce_enable = "↓";
  315. else
  316. debounce_enable = "↑";
  317. snprintf(debounce_value, sizeof(debounce_value), "%06u", time * unit);
  318. seq_printf(s, "%s (🕑 %sus)|", debounce_enable, debounce_value);
  319. } else {
  320. seq_puts(s, " |");
  321. }
  322. seq_printf(s, "0x%x\n", pin_reg);
  323. }
  324. }
  325. }
  326. #else
  327. #define amd_gpio_dbg_show NULL
  328. #endif
  329. static void amd_gpio_irq_enable(struct irq_data *d)
  330. {
  331. u32 pin_reg;
  332. unsigned long flags;
  333. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  334. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  335. gpiochip_enable_irq(gc, d->hwirq);
  336. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  337. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  338. pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
  339. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  340. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  341. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  342. }
  343. static void amd_gpio_irq_disable(struct irq_data *d)
  344. {
  345. u32 pin_reg;
  346. unsigned long flags;
  347. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  348. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  349. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  350. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  351. pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
  352. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  353. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  354. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  355. gpiochip_disable_irq(gc, d->hwirq);
  356. }
  357. static void amd_gpio_irq_mask(struct irq_data *d)
  358. {
  359. u32 pin_reg;
  360. unsigned long flags;
  361. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  362. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  363. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  364. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  365. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  366. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  367. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  368. }
  369. static void amd_gpio_irq_unmask(struct irq_data *d)
  370. {
  371. u32 pin_reg;
  372. unsigned long flags;
  373. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  374. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  375. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  376. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  377. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  378. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  379. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  380. }
  381. static int amd_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  382. {
  383. u32 pin_reg;
  384. unsigned long flags;
  385. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  386. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  387. u32 wake_mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3);
  388. int err;
  389. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  390. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  391. if (on)
  392. pin_reg |= wake_mask;
  393. else
  394. pin_reg &= ~wake_mask;
  395. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  396. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  397. if (on)
  398. err = enable_irq_wake(gpio_dev->irq);
  399. else
  400. err = disable_irq_wake(gpio_dev->irq);
  401. if (err)
  402. dev_err(&gpio_dev->pdev->dev, "failed to %s wake-up interrupt\n",
  403. on ? "enable" : "disable");
  404. return 0;
  405. }
  406. static void amd_gpio_irq_eoi(struct irq_data *d)
  407. {
  408. u32 reg;
  409. unsigned long flags;
  410. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  411. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  412. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  413. reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  414. reg |= EOI_MASK;
  415. writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
  416. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  417. }
  418. static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  419. {
  420. int ret = 0;
  421. u32 pin_reg, pin_reg_irq_en, mask;
  422. unsigned long flags;
  423. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  424. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  425. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  426. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  427. switch (type & IRQ_TYPE_SENSE_MASK) {
  428. case IRQ_TYPE_EDGE_RISING:
  429. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  430. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  431. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  432. irq_set_handler_locked(d, handle_edge_irq);
  433. break;
  434. case IRQ_TYPE_EDGE_FALLING:
  435. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  436. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  437. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  438. irq_set_handler_locked(d, handle_edge_irq);
  439. break;
  440. case IRQ_TYPE_EDGE_BOTH:
  441. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  442. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  443. pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
  444. irq_set_handler_locked(d, handle_edge_irq);
  445. break;
  446. case IRQ_TYPE_LEVEL_HIGH:
  447. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  448. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  449. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  450. irq_set_handler_locked(d, handle_level_irq);
  451. break;
  452. case IRQ_TYPE_LEVEL_LOW:
  453. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  454. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  455. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  456. irq_set_handler_locked(d, handle_level_irq);
  457. break;
  458. case IRQ_TYPE_NONE:
  459. break;
  460. default:
  461. dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
  462. ret = -EINVAL;
  463. }
  464. pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
  465. /*
  466. * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
  467. * debounce registers of any GPIO will block wake/interrupt status
  468. * generation for *all* GPIOs for a length of time that depends on
  469. * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
  470. * INTERRUPT_ENABLE bit will read as 0.
  471. *
  472. * We temporarily enable irq for the GPIO whose configuration is
  473. * changing, and then wait for it to read back as 1 to know when
  474. * debounce has settled and then disable the irq again.
  475. * We do this polling with the spinlock held to ensure other GPIO
  476. * access routines do not read an incorrect value for the irq enable
  477. * bit of other GPIOs. We keep the GPIO masked while polling to avoid
  478. * spurious irqs, and disable the irq again after polling.
  479. */
  480. mask = BIT(INTERRUPT_ENABLE_OFF);
  481. pin_reg_irq_en = pin_reg;
  482. pin_reg_irq_en |= mask;
  483. pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
  484. writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
  485. while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
  486. continue;
  487. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  488. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  489. return ret;
  490. }
  491. static void amd_irq_ack(struct irq_data *d)
  492. {
  493. /*
  494. * based on HW design,there is no need to ack HW
  495. * before handle current irq. But this routine is
  496. * necessary for handle_edge_irq
  497. */
  498. }
  499. static const struct irq_chip amd_gpio_irqchip = {
  500. .name = "amd_gpio",
  501. .irq_ack = amd_irq_ack,
  502. .irq_enable = amd_gpio_irq_enable,
  503. .irq_disable = amd_gpio_irq_disable,
  504. .irq_mask = amd_gpio_irq_mask,
  505. .irq_unmask = amd_gpio_irq_unmask,
  506. .irq_set_wake = amd_gpio_irq_set_wake,
  507. .irq_eoi = amd_gpio_irq_eoi,
  508. .irq_set_type = amd_gpio_irq_set_type,
  509. /*
  510. * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
  511. * also generates an IRQ. We need the IRQ so the irq_handler can clear
  512. * the wake event. Otherwise the wake event will never clear and
  513. * prevent the system from suspending.
  514. */
  515. .flags = IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | IRQCHIP_IMMUTABLE,
  516. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  517. };
  518. #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
  519. static bool do_amd_gpio_irq_handler(int irq, void *dev_id)
  520. {
  521. struct amd_gpio *gpio_dev = dev_id;
  522. struct gpio_chip *gc = &gpio_dev->gc;
  523. unsigned int i, irqnr;
  524. unsigned long flags;
  525. u32 __iomem *regs;
  526. bool ret = false;
  527. u32 regval;
  528. u64 status, mask;
  529. /* Read the wake status */
  530. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  531. status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
  532. status <<= 32;
  533. status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
  534. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  535. /* Bit 0-45 contain the relevant status bits */
  536. status &= (1ULL << 46) - 1;
  537. regs = gpio_dev->base;
  538. for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
  539. if (!(status & mask))
  540. continue;
  541. status &= ~mask;
  542. /* Each status bit covers four pins */
  543. for (i = 0; i < 4; i++) {
  544. regval = readl(regs + i);
  545. if (regval & PIN_IRQ_PENDING)
  546. pm_pr_dbg("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_hibernate_common(struct device *dev, bool is_suspend)
  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. u32 wake_mask = is_suspend ? WAKE_SOURCE_SUSPEND : WAKE_SOURCE_HIBERNATE;
  786. for (i = 0; i < desc->npins; i++) {
  787. int pin = desc->pins[i].number;
  788. if (!amd_gpio_should_save(gpio_dev, pin))
  789. continue;
  790. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  791. gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin * 4) & ~PIN_IRQ_PENDING;
  792. /* mask any interrupts not intended to be a wake source */
  793. if (!(gpio_dev->saved_regs[i] & wake_mask)) {
  794. writel(gpio_dev->saved_regs[i] & ~BIT(INTERRUPT_MASK_OFF),
  795. gpio_dev->base + pin * 4);
  796. pm_pr_dbg("Disabling GPIO #%d interrupt for %s.\n",
  797. pin, is_suspend ? "suspend" : "hibernate");
  798. }
  799. /*
  800. * debounce enabled over suspend has shown issues with a GPIO
  801. * being unable to wake the system, as we're only interested in
  802. * the actual wakeup event, clear it.
  803. */
  804. if (gpio_dev->saved_regs[i] & (DB_CNTRl_MASK << DB_CNTRL_OFF)) {
  805. amd_gpio_set_debounce(gpio_dev, pin, 0);
  806. pm_pr_dbg("Clearing debounce for GPIO #%d during %s.\n",
  807. pin, is_suspend ? "suspend" : "hibernate");
  808. }
  809. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  810. }
  811. return 0;
  812. }
  813. static int amd_gpio_suspend(struct device *dev)
  814. {
  815. return amd_gpio_suspend_hibernate_common(dev, true);
  816. }
  817. static int amd_gpio_hibernate(struct device *dev)
  818. {
  819. return amd_gpio_suspend_hibernate_common(dev, false);
  820. }
  821. static int amd_gpio_resume(struct device *dev)
  822. {
  823. struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
  824. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  825. unsigned long flags;
  826. int i;
  827. for (i = 0; i < desc->npins; i++) {
  828. int pin = desc->pins[i].number;
  829. if (!amd_gpio_should_save(gpio_dev, pin))
  830. continue;
  831. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  832. gpio_dev->saved_regs[i] |= readl(gpio_dev->base + pin * 4) & PIN_IRQ_PENDING;
  833. writel(gpio_dev->saved_regs[i], gpio_dev->base + pin * 4);
  834. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  835. }
  836. return 0;
  837. }
  838. static const struct dev_pm_ops amd_gpio_pm_ops = {
  839. .suspend_late = amd_gpio_suspend,
  840. .resume_early = amd_gpio_resume,
  841. .freeze_late = amd_gpio_hibernate,
  842. .thaw_early = amd_gpio_resume,
  843. .poweroff_late = amd_gpio_hibernate,
  844. .restore_early = amd_gpio_resume,
  845. };
  846. #endif
  847. static int amd_get_functions_count(struct pinctrl_dev *pctldev)
  848. {
  849. return ARRAY_SIZE(pmx_functions);
  850. }
  851. static const char *amd_get_fname(struct pinctrl_dev *pctrldev, unsigned int selector)
  852. {
  853. return pmx_functions[selector].name;
  854. }
  855. static int amd_get_groups(struct pinctrl_dev *pctrldev, unsigned int selector,
  856. const char * const **groups,
  857. unsigned int * const num_groups)
  858. {
  859. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
  860. if (!gpio_dev->iomux_base) {
  861. dev_err(&gpio_dev->pdev->dev, "iomux function %d group not supported\n", selector);
  862. return -EINVAL;
  863. }
  864. *groups = pmx_functions[selector].groups;
  865. *num_groups = pmx_functions[selector].ngroups;
  866. return 0;
  867. }
  868. static int amd_set_mux(struct pinctrl_dev *pctrldev, unsigned int function, unsigned int group)
  869. {
  870. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
  871. struct device *dev = &gpio_dev->pdev->dev;
  872. struct pin_desc *pd;
  873. int ind, index;
  874. if (!gpio_dev->iomux_base)
  875. return -EINVAL;
  876. for (index = 0; index < NSELECTS; index++) {
  877. if (strcmp(gpio_dev->groups[group].name, pmx_functions[function].groups[index]))
  878. continue;
  879. if (readb(gpio_dev->iomux_base + pmx_functions[function].index) ==
  880. FUNCTION_INVALID) {
  881. dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
  882. pmx_functions[function].index);
  883. return -EINVAL;
  884. }
  885. writeb(index, gpio_dev->iomux_base + pmx_functions[function].index);
  886. if (index != (readb(gpio_dev->iomux_base + pmx_functions[function].index) &
  887. FUNCTION_MASK)) {
  888. dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
  889. pmx_functions[function].index);
  890. return -EINVAL;
  891. }
  892. for (ind = 0; ind < gpio_dev->groups[group].npins; ind++) {
  893. if (strncmp(gpio_dev->groups[group].name, "IMX_F", strlen("IMX_F")))
  894. continue;
  895. pd = pin_desc_get(gpio_dev->pctrl, gpio_dev->groups[group].pins[ind]);
  896. pd->mux_owner = gpio_dev->groups[group].name;
  897. }
  898. break;
  899. }
  900. return 0;
  901. }
  902. static const struct pinmux_ops amd_pmxops = {
  903. .get_functions_count = amd_get_functions_count,
  904. .get_function_name = amd_get_fname,
  905. .get_function_groups = amd_get_groups,
  906. .set_mux = amd_set_mux,
  907. };
  908. static struct pinctrl_desc amd_pinctrl_desc = {
  909. .pins = kerncz_pins,
  910. .npins = ARRAY_SIZE(kerncz_pins),
  911. .pctlops = &amd_pinctrl_ops,
  912. .pmxops = &amd_pmxops,
  913. .confops = &amd_pinconf_ops,
  914. .owner = THIS_MODULE,
  915. };
  916. static void amd_get_iomux_res(struct amd_gpio *gpio_dev)
  917. {
  918. struct pinctrl_desc *desc = &amd_pinctrl_desc;
  919. struct device *dev = &gpio_dev->pdev->dev;
  920. int index;
  921. index = device_property_match_string(dev, "pinctrl-resource-names", "iomux");
  922. if (index < 0) {
  923. dev_dbg(dev, "iomux not supported\n");
  924. goto out_no_pinmux;
  925. }
  926. gpio_dev->iomux_base = devm_platform_ioremap_resource(gpio_dev->pdev, index);
  927. if (IS_ERR(gpio_dev->iomux_base)) {
  928. dev_dbg(dev, "iomux not supported %d io resource\n", index);
  929. goto out_no_pinmux;
  930. }
  931. return;
  932. out_no_pinmux:
  933. desc->pmxops = NULL;
  934. }
  935. static int amd_gpio_probe(struct platform_device *pdev)
  936. {
  937. int ret = 0;
  938. struct resource *res;
  939. struct amd_gpio *gpio_dev;
  940. struct gpio_irq_chip *girq;
  941. gpio_dev = devm_kzalloc(&pdev->dev,
  942. sizeof(struct amd_gpio), GFP_KERNEL);
  943. if (!gpio_dev)
  944. return -ENOMEM;
  945. raw_spin_lock_init(&gpio_dev->lock);
  946. gpio_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  947. if (IS_ERR(gpio_dev->base)) {
  948. dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
  949. return PTR_ERR(gpio_dev->base);
  950. }
  951. gpio_dev->irq = platform_get_irq(pdev, 0);
  952. if (gpio_dev->irq < 0)
  953. return gpio_dev->irq;
  954. #ifdef CONFIG_PM_SLEEP
  955. gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
  956. sizeof(*gpio_dev->saved_regs),
  957. GFP_KERNEL);
  958. if (!gpio_dev->saved_regs)
  959. return -ENOMEM;
  960. #endif
  961. gpio_dev->pdev = pdev;
  962. gpio_dev->gc.get_direction = amd_gpio_get_direction;
  963. gpio_dev->gc.direction_input = amd_gpio_direction_input;
  964. gpio_dev->gc.direction_output = amd_gpio_direction_output;
  965. gpio_dev->gc.get = amd_gpio_get_value;
  966. gpio_dev->gc.set = amd_gpio_set_value;
  967. gpio_dev->gc.set_config = amd_gpio_set_config;
  968. gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
  969. gpio_dev->gc.base = -1;
  970. gpio_dev->gc.label = pdev->name;
  971. gpio_dev->gc.owner = THIS_MODULE;
  972. gpio_dev->gc.parent = &pdev->dev;
  973. gpio_dev->gc.ngpio = resource_size(res) / 4;
  974. gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
  975. gpio_dev->groups = kerncz_groups;
  976. gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
  977. amd_pinctrl_desc.name = dev_name(&pdev->dev);
  978. amd_get_iomux_res(gpio_dev);
  979. gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
  980. gpio_dev);
  981. if (IS_ERR(gpio_dev->pctrl)) {
  982. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  983. return PTR_ERR(gpio_dev->pctrl);
  984. }
  985. /* Disable and mask interrupts */
  986. amd_gpio_irq_init(gpio_dev);
  987. girq = &gpio_dev->gc.irq;
  988. gpio_irq_chip_set_chip(girq, &amd_gpio_irqchip);
  989. /* This will let us handle the parent IRQ in the driver */
  990. girq->parent_handler = NULL;
  991. girq->num_parents = 0;
  992. girq->parents = NULL;
  993. girq->default_type = IRQ_TYPE_NONE;
  994. girq->handler = handle_simple_irq;
  995. ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
  996. if (ret)
  997. return ret;
  998. ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
  999. 0, 0, gpio_dev->gc.ngpio);
  1000. if (ret) {
  1001. dev_err(&pdev->dev, "Failed to add pin range\n");
  1002. goto out2;
  1003. }
  1004. ret = devm_request_irq(&pdev->dev, gpio_dev->irq, amd_gpio_irq_handler,
  1005. IRQF_SHARED | IRQF_COND_ONESHOT, KBUILD_MODNAME, gpio_dev);
  1006. if (ret)
  1007. goto out2;
  1008. platform_set_drvdata(pdev, gpio_dev);
  1009. acpi_register_wakeup_handler(gpio_dev->irq, amd_gpio_check_wake, gpio_dev);
  1010. dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
  1011. return ret;
  1012. out2:
  1013. gpiochip_remove(&gpio_dev->gc);
  1014. return ret;
  1015. }
  1016. static void amd_gpio_remove(struct platform_device *pdev)
  1017. {
  1018. struct amd_gpio *gpio_dev;
  1019. gpio_dev = platform_get_drvdata(pdev);
  1020. gpiochip_remove(&gpio_dev->gc);
  1021. acpi_unregister_wakeup_handler(amd_gpio_check_wake, gpio_dev);
  1022. }
  1023. #ifdef CONFIG_ACPI
  1024. static const struct acpi_device_id amd_gpio_acpi_match[] = {
  1025. { "AMD0030", 0 },
  1026. { "AMDI0030", 0},
  1027. { "AMDI0031", 0},
  1028. { },
  1029. };
  1030. MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
  1031. #endif
  1032. static struct platform_driver amd_gpio_driver = {
  1033. .driver = {
  1034. .name = "amd_gpio",
  1035. .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
  1036. #ifdef CONFIG_PM_SLEEP
  1037. .pm = &amd_gpio_pm_ops,
  1038. #endif
  1039. },
  1040. .probe = amd_gpio_probe,
  1041. .remove_new = amd_gpio_remove,
  1042. };
  1043. module_platform_driver(amd_gpio_driver);
  1044. MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
  1045. MODULE_DESCRIPTION("AMD GPIO pinctrl driver");