pinctrl-amd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * GPIO driver for AMD
  3. *
  4. * Copyright (c) 2014,2015 AMD Corporation.
  5. * Authors: Ken Xue <Ken.Xue@amd.com>
  6. * Wu, Jeff <Jeff.Wu@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/bug.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/compiler.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/log2.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mutex.h>
  26. #include <linux/acpi.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/list.h>
  30. #include <linux/bitops.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinconf-generic.h>
  33. #include "pinctrl-utils.h"
  34. #include "pinctrl-amd.h"
  35. static int amd_gpio_direction_input(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. pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
  43. writel(pin_reg, gpio_dev->base + offset * 4);
  44. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  45. return 0;
  46. }
  47. static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  48. int value)
  49. {
  50. u32 pin_reg;
  51. unsigned long flags;
  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. if (value)
  57. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  58. else
  59. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  60. writel(pin_reg, gpio_dev->base + offset * 4);
  61. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  62. return 0;
  63. }
  64. static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
  65. {
  66. u32 pin_reg;
  67. unsigned long flags;
  68. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  69. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  70. pin_reg = readl(gpio_dev->base + offset * 4);
  71. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  72. return !!(pin_reg & BIT(PIN_STS_OFF));
  73. }
  74. static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
  75. {
  76. u32 pin_reg;
  77. unsigned long flags;
  78. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  79. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  80. pin_reg = readl(gpio_dev->base + offset * 4);
  81. if (value)
  82. pin_reg |= BIT(OUTPUT_VALUE_OFF);
  83. else
  84. pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
  85. writel(pin_reg, gpio_dev->base + offset * 4);
  86. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  87. }
  88. static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
  89. unsigned debounce)
  90. {
  91. u32 time;
  92. u32 pin_reg;
  93. int ret = 0;
  94. unsigned long flags;
  95. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  96. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  97. pin_reg = readl(gpio_dev->base + offset * 4);
  98. if (debounce) {
  99. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  100. pin_reg &= ~DB_TMR_OUT_MASK;
  101. /*
  102. Debounce Debounce Timer Max
  103. TmrLarge TmrOutUnit Unit Debounce
  104. Time
  105. 0 0 61 usec (2 RtcClk) 976 usec
  106. 0 1 244 usec (8 RtcClk) 3.9 msec
  107. 1 0 15.6 msec (512 RtcClk) 250 msec
  108. 1 1 62.5 msec (2048 RtcClk) 1 sec
  109. */
  110. if (debounce < 61) {
  111. pin_reg |= 1;
  112. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  113. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  114. } else if (debounce < 976) {
  115. time = debounce / 61;
  116. pin_reg |= time & DB_TMR_OUT_MASK;
  117. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  118. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  119. } else if (debounce < 3900) {
  120. time = debounce / 244;
  121. pin_reg |= time & DB_TMR_OUT_MASK;
  122. pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
  123. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  124. } else if (debounce < 250000) {
  125. time = debounce / 15600;
  126. pin_reg |= time & DB_TMR_OUT_MASK;
  127. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  128. pin_reg |= BIT(DB_TMR_LARGE_OFF);
  129. } else if (debounce < 1000000) {
  130. time = debounce / 62500;
  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 {
  135. pin_reg &= ~DB_CNTRl_MASK;
  136. ret = -EINVAL;
  137. }
  138. } else {
  139. pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
  140. pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
  141. pin_reg &= ~DB_TMR_OUT_MASK;
  142. pin_reg &= ~DB_CNTRl_MASK;
  143. }
  144. writel(pin_reg, gpio_dev->base + offset * 4);
  145. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  146. return ret;
  147. }
  148. static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  149. unsigned long config)
  150. {
  151. u32 debounce;
  152. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  153. return -ENOTSUPP;
  154. debounce = pinconf_to_config_argument(config);
  155. return amd_gpio_set_debounce(gc, offset, debounce);
  156. }
  157. #ifdef CONFIG_DEBUG_FS
  158. static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  159. {
  160. u32 pin_reg;
  161. unsigned long flags;
  162. unsigned int bank, i, pin_num;
  163. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  164. char *level_trig;
  165. char *active_level;
  166. char *interrupt_enable;
  167. char *interrupt_mask;
  168. char *wake_cntrl0;
  169. char *wake_cntrl1;
  170. char *wake_cntrl2;
  171. char *pin_sts;
  172. char *pull_up_sel;
  173. char *pull_up_enable;
  174. char *pull_down_enable;
  175. char *output_value;
  176. char *output_enable;
  177. for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
  178. seq_printf(s, "GPIO bank%d\t", bank);
  179. switch (bank) {
  180. case 0:
  181. i = 0;
  182. pin_num = AMD_GPIO_PINS_BANK0;
  183. break;
  184. case 1:
  185. i = 64;
  186. pin_num = AMD_GPIO_PINS_BANK1 + i;
  187. break;
  188. case 2:
  189. i = 128;
  190. pin_num = AMD_GPIO_PINS_BANK2 + i;
  191. break;
  192. case 3:
  193. i = 192;
  194. pin_num = AMD_GPIO_PINS_BANK3 + i;
  195. break;
  196. default:
  197. /* Illegal bank number, ignore */
  198. continue;
  199. }
  200. for (; i < pin_num; i++) {
  201. seq_printf(s, "pin%d\t", i);
  202. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  203. pin_reg = readl(gpio_dev->base + i * 4);
  204. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  205. if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
  206. interrupt_enable = "interrupt is enabled|";
  207. if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
  208. !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
  209. active_level = "Active low|";
  210. else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
  211. !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
  212. active_level = "Active high|";
  213. else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
  214. pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
  215. active_level = "Active on both|";
  216. else
  217. active_level = "Unknown Active level|";
  218. if (pin_reg & BIT(LEVEL_TRIG_OFF))
  219. level_trig = "Level trigger|";
  220. else
  221. level_trig = "Edge trigger|";
  222. } else {
  223. interrupt_enable =
  224. "interrupt is disabled|";
  225. active_level = " ";
  226. level_trig = " ";
  227. }
  228. if (pin_reg & BIT(INTERRUPT_MASK_OFF))
  229. interrupt_mask =
  230. "interrupt is unmasked|";
  231. else
  232. interrupt_mask =
  233. "interrupt is masked|";
  234. if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
  235. wake_cntrl0 = "enable wakeup in S0i3 state|";
  236. else
  237. wake_cntrl0 = "disable wakeup in S0i3 state|";
  238. if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
  239. wake_cntrl1 = "enable wakeup in S3 state|";
  240. else
  241. wake_cntrl1 = "disable wakeup in S3 state|";
  242. if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
  243. wake_cntrl2 = "enable wakeup in S4/S5 state|";
  244. else
  245. wake_cntrl2 = "disable wakeup in S4/S5 state|";
  246. if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
  247. pull_up_enable = "pull-up is enabled|";
  248. if (pin_reg & BIT(PULL_UP_SEL_OFF))
  249. pull_up_sel = "8k pull-up|";
  250. else
  251. pull_up_sel = "4k pull-up|";
  252. } else {
  253. pull_up_enable = "pull-up is disabled|";
  254. pull_up_sel = " ";
  255. }
  256. if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
  257. pull_down_enable = "pull-down is enabled|";
  258. else
  259. pull_down_enable = "Pull-down is disabled|";
  260. if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
  261. pin_sts = " ";
  262. output_enable = "output is enabled|";
  263. if (pin_reg & BIT(OUTPUT_VALUE_OFF))
  264. output_value = "output is high|";
  265. else
  266. output_value = "output is low|";
  267. } else {
  268. output_enable = "output is disabled|";
  269. output_value = " ";
  270. if (pin_reg & BIT(PIN_STS_OFF))
  271. pin_sts = "input is high|";
  272. else
  273. pin_sts = "input is low|";
  274. }
  275. seq_printf(s, "%s %s %s %s %s %s\n"
  276. " %s %s %s %s %s %s %s 0x%x\n",
  277. level_trig, active_level, interrupt_enable,
  278. interrupt_mask, wake_cntrl0, wake_cntrl1,
  279. wake_cntrl2, pin_sts, pull_up_sel,
  280. pull_up_enable, pull_down_enable,
  281. output_value, output_enable, pin_reg);
  282. }
  283. }
  284. }
  285. #else
  286. #define amd_gpio_dbg_show NULL
  287. #endif
  288. static void amd_gpio_irq_enable(struct irq_data *d)
  289. {
  290. u32 pin_reg;
  291. unsigned long flags;
  292. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  293. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  294. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  295. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  296. pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
  297. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  298. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  299. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  300. }
  301. static void amd_gpio_irq_disable(struct irq_data *d)
  302. {
  303. u32 pin_reg;
  304. unsigned long flags;
  305. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  306. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  307. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  308. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  309. pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
  310. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  311. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  312. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  313. }
  314. static void amd_gpio_irq_mask(struct irq_data *d)
  315. {
  316. u32 pin_reg;
  317. unsigned long flags;
  318. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  319. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  320. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  321. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  322. pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
  323. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  324. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  325. }
  326. static void amd_gpio_irq_unmask(struct irq_data *d)
  327. {
  328. u32 pin_reg;
  329. unsigned long flags;
  330. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  331. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  332. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  333. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  334. pin_reg |= BIT(INTERRUPT_MASK_OFF);
  335. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  336. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  337. }
  338. static void amd_gpio_irq_eoi(struct irq_data *d)
  339. {
  340. u32 reg;
  341. unsigned long flags;
  342. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  343. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  344. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  345. reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  346. reg |= EOI_MASK;
  347. writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
  348. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  349. }
  350. static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  351. {
  352. int ret = 0;
  353. u32 pin_reg;
  354. unsigned long flags, irq_flags;
  355. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  356. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  357. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  358. pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
  359. /* Ignore the settings coming from the client and
  360. * read the values from the ACPI tables
  361. * while setting the trigger type
  362. */
  363. irq_flags = irq_get_trigger_type(d->irq);
  364. if (irq_flags != IRQ_TYPE_NONE)
  365. type = irq_flags;
  366. switch (type & IRQ_TYPE_SENSE_MASK) {
  367. case IRQ_TYPE_EDGE_RISING:
  368. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  369. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  370. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  371. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  372. irq_set_handler_locked(d, handle_edge_irq);
  373. break;
  374. case IRQ_TYPE_EDGE_FALLING:
  375. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  376. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  377. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  378. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  379. irq_set_handler_locked(d, handle_edge_irq);
  380. break;
  381. case IRQ_TYPE_EDGE_BOTH:
  382. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  383. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  384. pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
  385. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  386. irq_set_handler_locked(d, handle_edge_irq);
  387. break;
  388. case IRQ_TYPE_LEVEL_HIGH:
  389. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  390. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  391. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  392. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  393. pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
  394. irq_set_handler_locked(d, handle_level_irq);
  395. break;
  396. case IRQ_TYPE_LEVEL_LOW:
  397. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  398. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  399. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  400. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  401. pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
  402. irq_set_handler_locked(d, handle_level_irq);
  403. break;
  404. case IRQ_TYPE_NONE:
  405. break;
  406. default:
  407. dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
  408. ret = -EINVAL;
  409. }
  410. pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
  411. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  412. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  413. return ret;
  414. }
  415. static void amd_irq_ack(struct irq_data *d)
  416. {
  417. /*
  418. * based on HW design,there is no need to ack HW
  419. * before handle current irq. But this routine is
  420. * necessary for handle_edge_irq
  421. */
  422. }
  423. static struct irq_chip amd_gpio_irqchip = {
  424. .name = "amd_gpio",
  425. .irq_ack = amd_irq_ack,
  426. .irq_enable = amd_gpio_irq_enable,
  427. .irq_disable = amd_gpio_irq_disable,
  428. .irq_mask = amd_gpio_irq_mask,
  429. .irq_unmask = amd_gpio_irq_unmask,
  430. .irq_eoi = amd_gpio_irq_eoi,
  431. .irq_set_type = amd_gpio_irq_set_type,
  432. .flags = IRQCHIP_SKIP_SET_WAKE,
  433. };
  434. static void amd_gpio_irq_handler(struct irq_desc *desc)
  435. {
  436. u32 i;
  437. u32 off;
  438. u32 reg;
  439. u32 pin_reg;
  440. u64 reg64;
  441. int handled = 0;
  442. unsigned int irq;
  443. unsigned long flags;
  444. struct irq_chip *chip = irq_desc_get_chip(desc);
  445. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  446. struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
  447. chained_irq_enter(chip, desc);
  448. /*enable GPIO interrupt again*/
  449. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  450. reg = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
  451. reg64 = reg;
  452. reg64 = reg64 << 32;
  453. reg = readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
  454. reg64 |= reg;
  455. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  456. /*
  457. * first 46 bits indicates interrupt status.
  458. * one bit represents four interrupt sources.
  459. */
  460. for (off = 0; off < 46 ; off++) {
  461. if (reg64 & BIT(off)) {
  462. for (i = 0; i < 4; i++) {
  463. pin_reg = readl(gpio_dev->base +
  464. (off * 4 + i) * 4);
  465. if ((pin_reg & BIT(INTERRUPT_STS_OFF)) ||
  466. (pin_reg & BIT(WAKE_STS_OFF))) {
  467. irq = irq_find_mapping(gc->irqdomain,
  468. off * 4 + i);
  469. generic_handle_irq(irq);
  470. writel(pin_reg,
  471. gpio_dev->base
  472. + (off * 4 + i) * 4);
  473. handled++;
  474. }
  475. }
  476. }
  477. }
  478. if (handled == 0)
  479. handle_bad_irq(desc);
  480. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  481. reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  482. reg |= EOI_MASK;
  483. writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
  484. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  485. chained_irq_exit(chip, desc);
  486. }
  487. static int amd_get_groups_count(struct pinctrl_dev *pctldev)
  488. {
  489. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  490. return gpio_dev->ngroups;
  491. }
  492. static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
  493. unsigned group)
  494. {
  495. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  496. return gpio_dev->groups[group].name;
  497. }
  498. static int amd_get_group_pins(struct pinctrl_dev *pctldev,
  499. unsigned group,
  500. const unsigned **pins,
  501. unsigned *num_pins)
  502. {
  503. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  504. *pins = gpio_dev->groups[group].pins;
  505. *num_pins = gpio_dev->groups[group].npins;
  506. return 0;
  507. }
  508. static const struct pinctrl_ops amd_pinctrl_ops = {
  509. .get_groups_count = amd_get_groups_count,
  510. .get_group_name = amd_get_group_name,
  511. .get_group_pins = amd_get_group_pins,
  512. #ifdef CONFIG_OF
  513. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  514. .dt_free_map = pinctrl_utils_free_map,
  515. #endif
  516. };
  517. static int amd_pinconf_get(struct pinctrl_dev *pctldev,
  518. unsigned int pin,
  519. unsigned long *config)
  520. {
  521. u32 pin_reg;
  522. unsigned arg;
  523. unsigned long flags;
  524. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  525. enum pin_config_param param = pinconf_to_config_param(*config);
  526. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  527. pin_reg = readl(gpio_dev->base + pin*4);
  528. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  529. switch (param) {
  530. case PIN_CONFIG_INPUT_DEBOUNCE:
  531. arg = pin_reg & DB_TMR_OUT_MASK;
  532. break;
  533. case PIN_CONFIG_BIAS_PULL_DOWN:
  534. arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
  535. break;
  536. case PIN_CONFIG_BIAS_PULL_UP:
  537. arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
  538. break;
  539. case PIN_CONFIG_DRIVE_STRENGTH:
  540. arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
  541. break;
  542. default:
  543. dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
  544. param);
  545. return -ENOTSUPP;
  546. }
  547. *config = pinconf_to_config_packed(param, arg);
  548. return 0;
  549. }
  550. static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  551. unsigned long *configs, unsigned num_configs)
  552. {
  553. int i;
  554. u32 arg;
  555. int ret = 0;
  556. u32 pin_reg;
  557. unsigned long flags;
  558. enum pin_config_param param;
  559. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  560. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  561. for (i = 0; i < num_configs; i++) {
  562. param = pinconf_to_config_param(configs[i]);
  563. arg = pinconf_to_config_argument(configs[i]);
  564. pin_reg = readl(gpio_dev->base + pin*4);
  565. switch (param) {
  566. case PIN_CONFIG_INPUT_DEBOUNCE:
  567. pin_reg &= ~DB_TMR_OUT_MASK;
  568. pin_reg |= arg & DB_TMR_OUT_MASK;
  569. break;
  570. case PIN_CONFIG_BIAS_PULL_DOWN:
  571. pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
  572. pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
  573. break;
  574. case PIN_CONFIG_BIAS_PULL_UP:
  575. pin_reg &= ~BIT(PULL_UP_SEL_OFF);
  576. pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
  577. pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
  578. pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
  579. break;
  580. case PIN_CONFIG_DRIVE_STRENGTH:
  581. pin_reg &= ~(DRV_STRENGTH_SEL_MASK
  582. << DRV_STRENGTH_SEL_OFF);
  583. pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
  584. << DRV_STRENGTH_SEL_OFF;
  585. break;
  586. default:
  587. dev_err(&gpio_dev->pdev->dev,
  588. "Invalid config param %04x\n", param);
  589. ret = -ENOTSUPP;
  590. }
  591. writel(pin_reg, gpio_dev->base + pin*4);
  592. }
  593. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  594. return ret;
  595. }
  596. static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
  597. unsigned int group,
  598. unsigned long *config)
  599. {
  600. const unsigned *pins;
  601. unsigned npins;
  602. int ret;
  603. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  604. if (ret)
  605. return ret;
  606. if (amd_pinconf_get(pctldev, pins[0], config))
  607. return -ENOTSUPP;
  608. return 0;
  609. }
  610. static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
  611. unsigned group, unsigned long *configs,
  612. unsigned num_configs)
  613. {
  614. const unsigned *pins;
  615. unsigned npins;
  616. int i, ret;
  617. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  618. if (ret)
  619. return ret;
  620. for (i = 0; i < npins; i++) {
  621. if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
  622. return -ENOTSUPP;
  623. }
  624. return 0;
  625. }
  626. static const struct pinconf_ops amd_pinconf_ops = {
  627. .pin_config_get = amd_pinconf_get,
  628. .pin_config_set = amd_pinconf_set,
  629. .pin_config_group_get = amd_pinconf_group_get,
  630. .pin_config_group_set = amd_pinconf_group_set,
  631. };
  632. static struct pinctrl_desc amd_pinctrl_desc = {
  633. .pins = kerncz_pins,
  634. .npins = ARRAY_SIZE(kerncz_pins),
  635. .pctlops = &amd_pinctrl_ops,
  636. .confops = &amd_pinconf_ops,
  637. .owner = THIS_MODULE,
  638. };
  639. static int amd_gpio_probe(struct platform_device *pdev)
  640. {
  641. int ret = 0;
  642. int irq_base;
  643. struct resource *res;
  644. struct amd_gpio *gpio_dev;
  645. gpio_dev = devm_kzalloc(&pdev->dev,
  646. sizeof(struct amd_gpio), GFP_KERNEL);
  647. if (!gpio_dev)
  648. return -ENOMEM;
  649. raw_spin_lock_init(&gpio_dev->lock);
  650. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  651. if (!res) {
  652. dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
  653. return -EINVAL;
  654. }
  655. gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
  656. resource_size(res));
  657. if (!gpio_dev->base)
  658. return -ENOMEM;
  659. irq_base = platform_get_irq(pdev, 0);
  660. if (irq_base < 0) {
  661. dev_err(&pdev->dev, "Failed to get gpio IRQ.\n");
  662. return -EINVAL;
  663. }
  664. gpio_dev->pdev = pdev;
  665. gpio_dev->gc.direction_input = amd_gpio_direction_input;
  666. gpio_dev->gc.direction_output = amd_gpio_direction_output;
  667. gpio_dev->gc.get = amd_gpio_get_value;
  668. gpio_dev->gc.set = amd_gpio_set_value;
  669. gpio_dev->gc.set_config = amd_gpio_set_config;
  670. gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
  671. gpio_dev->gc.base = -1;
  672. gpio_dev->gc.label = pdev->name;
  673. gpio_dev->gc.owner = THIS_MODULE;
  674. gpio_dev->gc.parent = &pdev->dev;
  675. gpio_dev->gc.ngpio = resource_size(res) / 4;
  676. #if defined(CONFIG_OF_GPIO)
  677. gpio_dev->gc.of_node = pdev->dev.of_node;
  678. #endif
  679. gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
  680. gpio_dev->groups = kerncz_groups;
  681. gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
  682. amd_pinctrl_desc.name = dev_name(&pdev->dev);
  683. gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
  684. gpio_dev);
  685. if (IS_ERR(gpio_dev->pctrl)) {
  686. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  687. return PTR_ERR(gpio_dev->pctrl);
  688. }
  689. ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
  690. if (ret)
  691. return ret;
  692. ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
  693. 0, 0, gpio_dev->gc.ngpio);
  694. if (ret) {
  695. dev_err(&pdev->dev, "Failed to add pin range\n");
  696. goto out2;
  697. }
  698. ret = gpiochip_irqchip_add(&gpio_dev->gc,
  699. &amd_gpio_irqchip,
  700. 0,
  701. handle_simple_irq,
  702. IRQ_TYPE_NONE);
  703. if (ret) {
  704. dev_err(&pdev->dev, "could not add irqchip\n");
  705. ret = -ENODEV;
  706. goto out2;
  707. }
  708. gpiochip_set_chained_irqchip(&gpio_dev->gc,
  709. &amd_gpio_irqchip,
  710. irq_base,
  711. amd_gpio_irq_handler);
  712. platform_set_drvdata(pdev, gpio_dev);
  713. dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
  714. return ret;
  715. out2:
  716. gpiochip_remove(&gpio_dev->gc);
  717. return ret;
  718. }
  719. static int amd_gpio_remove(struct platform_device *pdev)
  720. {
  721. struct amd_gpio *gpio_dev;
  722. gpio_dev = platform_get_drvdata(pdev);
  723. gpiochip_remove(&gpio_dev->gc);
  724. return 0;
  725. }
  726. static const struct acpi_device_id amd_gpio_acpi_match[] = {
  727. { "AMD0030", 0 },
  728. { "AMDI0030", 0},
  729. { },
  730. };
  731. MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
  732. static struct platform_driver amd_gpio_driver = {
  733. .driver = {
  734. .name = "amd_gpio",
  735. .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
  736. },
  737. .probe = amd_gpio_probe,
  738. .remove = amd_gpio_remove,
  739. };
  740. module_platform_driver(amd_gpio_driver);
  741. MODULE_LICENSE("GPL v2");
  742. MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
  743. MODULE_DESCRIPTION("AMD GPIO pinctrl driver");