pinctrl-amd.c 24 KB

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