pinctrl-amd.c 26 KB

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