pinctrl-amd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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/driver.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 / 15600;
  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;
  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;
  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. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  387. irq_set_handler_locked(d, handle_edge_irq);
  388. break;
  389. case IRQ_TYPE_EDGE_FALLING:
  390. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  391. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  392. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  393. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  394. irq_set_handler_locked(d, handle_edge_irq);
  395. break;
  396. case IRQ_TYPE_EDGE_BOTH:
  397. pin_reg &= ~BIT(LEVEL_TRIG_OFF);
  398. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  399. pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
  400. pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
  401. irq_set_handler_locked(d, handle_edge_irq);
  402. break;
  403. case IRQ_TYPE_LEVEL_HIGH:
  404. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  405. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  406. pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
  407. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  408. pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
  409. irq_set_handler_locked(d, handle_level_irq);
  410. break;
  411. case IRQ_TYPE_LEVEL_LOW:
  412. pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
  413. pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
  414. pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
  415. pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
  416. pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
  417. irq_set_handler_locked(d, handle_level_irq);
  418. break;
  419. case IRQ_TYPE_NONE:
  420. break;
  421. default:
  422. dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
  423. ret = -EINVAL;
  424. }
  425. pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
  426. /*
  427. * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
  428. * debounce registers of any GPIO will block wake/interrupt status
  429. * generation for *all* GPIOs for a lenght of time that depends on
  430. * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
  431. * INTERRUPT_ENABLE bit will read as 0.
  432. *
  433. * We temporarily enable irq for the GPIO whose configuration is
  434. * changing, and then wait for it to read back as 1 to know when
  435. * debounce has settled and then disable the irq again.
  436. * We do this polling with the spinlock held to ensure other GPIO
  437. * access routines do not read an incorrect value for the irq enable
  438. * bit of other GPIOs. We keep the GPIO masked while polling to avoid
  439. * spurious irqs, and disable the irq again after polling.
  440. */
  441. mask = BIT(INTERRUPT_ENABLE_OFF);
  442. pin_reg_irq_en = pin_reg;
  443. pin_reg_irq_en |= mask;
  444. pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
  445. writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
  446. while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
  447. continue;
  448. writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
  449. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  450. return ret;
  451. }
  452. static void amd_irq_ack(struct irq_data *d)
  453. {
  454. /*
  455. * based on HW design,there is no need to ack HW
  456. * before handle current irq. But this routine is
  457. * necessary for handle_edge_irq
  458. */
  459. }
  460. static struct irq_chip amd_gpio_irqchip = {
  461. .name = "amd_gpio",
  462. .irq_ack = amd_irq_ack,
  463. .irq_enable = amd_gpio_irq_enable,
  464. .irq_disable = amd_gpio_irq_disable,
  465. .irq_mask = amd_gpio_irq_mask,
  466. .irq_unmask = amd_gpio_irq_unmask,
  467. .irq_eoi = amd_gpio_irq_eoi,
  468. .irq_set_type = amd_gpio_irq_set_type,
  469. .flags = IRQCHIP_SKIP_SET_WAKE,
  470. };
  471. #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
  472. static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
  473. {
  474. struct amd_gpio *gpio_dev = dev_id;
  475. struct gpio_chip *gc = &gpio_dev->gc;
  476. irqreturn_t ret = IRQ_NONE;
  477. unsigned int i, irqnr;
  478. unsigned long flags;
  479. u32 *regs, regval;
  480. u64 status, mask;
  481. /* Read the wake status */
  482. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  483. status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
  484. status <<= 32;
  485. status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
  486. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  487. /* Bit 0-45 contain the relevant status bits */
  488. status &= (1ULL << 46) - 1;
  489. regs = gpio_dev->base;
  490. for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
  491. if (!(status & mask))
  492. continue;
  493. status &= ~mask;
  494. /* Each status bit covers four pins */
  495. for (i = 0; i < 4; i++) {
  496. regval = readl(regs + i);
  497. if (!(regval & PIN_IRQ_PENDING) ||
  498. !(regval & BIT(INTERRUPT_MASK_OFF)))
  499. continue;
  500. irq = irq_find_mapping(gc->irq.domain, irqnr + i);
  501. generic_handle_irq(irq);
  502. /* Clear interrupt.
  503. * We must read the pin register again, in case the
  504. * value was changed while executing
  505. * generic_handle_irq() above.
  506. */
  507. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  508. regval = readl(regs + i);
  509. writel(regval, regs + i);
  510. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  511. ret = IRQ_HANDLED;
  512. }
  513. }
  514. /* Signal EOI to the GPIO unit */
  515. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  516. regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
  517. regval |= EOI_MASK;
  518. writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
  519. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  520. return ret;
  521. }
  522. static int amd_get_groups_count(struct pinctrl_dev *pctldev)
  523. {
  524. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  525. return gpio_dev->ngroups;
  526. }
  527. static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
  528. unsigned group)
  529. {
  530. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  531. return gpio_dev->groups[group].name;
  532. }
  533. static int amd_get_group_pins(struct pinctrl_dev *pctldev,
  534. unsigned group,
  535. const unsigned **pins,
  536. unsigned *num_pins)
  537. {
  538. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  539. *pins = gpio_dev->groups[group].pins;
  540. *num_pins = gpio_dev->groups[group].npins;
  541. return 0;
  542. }
  543. static const struct pinctrl_ops amd_pinctrl_ops = {
  544. .get_groups_count = amd_get_groups_count,
  545. .get_group_name = amd_get_group_name,
  546. .get_group_pins = amd_get_group_pins,
  547. #ifdef CONFIG_OF
  548. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  549. .dt_free_map = pinctrl_utils_free_map,
  550. #endif
  551. };
  552. static int amd_pinconf_get(struct pinctrl_dev *pctldev,
  553. unsigned int pin,
  554. unsigned long *config)
  555. {
  556. u32 pin_reg;
  557. unsigned arg;
  558. unsigned long flags;
  559. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  560. enum pin_config_param param = pinconf_to_config_param(*config);
  561. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  562. pin_reg = readl(gpio_dev->base + pin*4);
  563. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  564. switch (param) {
  565. case PIN_CONFIG_INPUT_DEBOUNCE:
  566. arg = pin_reg & DB_TMR_OUT_MASK;
  567. break;
  568. case PIN_CONFIG_BIAS_PULL_DOWN:
  569. arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
  570. break;
  571. case PIN_CONFIG_BIAS_PULL_UP:
  572. arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
  573. break;
  574. case PIN_CONFIG_DRIVE_STRENGTH:
  575. arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
  576. break;
  577. default:
  578. dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
  579. param);
  580. return -ENOTSUPP;
  581. }
  582. *config = pinconf_to_config_packed(param, arg);
  583. return 0;
  584. }
  585. static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  586. unsigned long *configs, unsigned num_configs)
  587. {
  588. int i;
  589. u32 arg;
  590. int ret = 0;
  591. u32 pin_reg;
  592. unsigned long flags;
  593. enum pin_config_param param;
  594. struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
  595. raw_spin_lock_irqsave(&gpio_dev->lock, flags);
  596. for (i = 0; i < num_configs; i++) {
  597. param = pinconf_to_config_param(configs[i]);
  598. arg = pinconf_to_config_argument(configs[i]);
  599. pin_reg = readl(gpio_dev->base + pin*4);
  600. switch (param) {
  601. case PIN_CONFIG_INPUT_DEBOUNCE:
  602. pin_reg &= ~DB_TMR_OUT_MASK;
  603. pin_reg |= arg & DB_TMR_OUT_MASK;
  604. break;
  605. case PIN_CONFIG_BIAS_PULL_DOWN:
  606. pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
  607. pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
  608. break;
  609. case PIN_CONFIG_BIAS_PULL_UP:
  610. pin_reg &= ~BIT(PULL_UP_SEL_OFF);
  611. pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
  612. pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
  613. pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
  614. break;
  615. case PIN_CONFIG_DRIVE_STRENGTH:
  616. pin_reg &= ~(DRV_STRENGTH_SEL_MASK
  617. << DRV_STRENGTH_SEL_OFF);
  618. pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
  619. << DRV_STRENGTH_SEL_OFF;
  620. break;
  621. default:
  622. dev_err(&gpio_dev->pdev->dev,
  623. "Invalid config param %04x\n", param);
  624. ret = -ENOTSUPP;
  625. }
  626. writel(pin_reg, gpio_dev->base + pin*4);
  627. }
  628. raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
  629. return ret;
  630. }
  631. static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
  632. unsigned int group,
  633. unsigned long *config)
  634. {
  635. const unsigned *pins;
  636. unsigned npins;
  637. int ret;
  638. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  639. if (ret)
  640. return ret;
  641. if (amd_pinconf_get(pctldev, pins[0], config))
  642. return -ENOTSUPP;
  643. return 0;
  644. }
  645. static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
  646. unsigned group, unsigned long *configs,
  647. unsigned num_configs)
  648. {
  649. const unsigned *pins;
  650. unsigned npins;
  651. int i, ret;
  652. ret = amd_get_group_pins(pctldev, group, &pins, &npins);
  653. if (ret)
  654. return ret;
  655. for (i = 0; i < npins; i++) {
  656. if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
  657. return -ENOTSUPP;
  658. }
  659. return 0;
  660. }
  661. static const struct pinconf_ops amd_pinconf_ops = {
  662. .pin_config_get = amd_pinconf_get,
  663. .pin_config_set = amd_pinconf_set,
  664. .pin_config_group_get = amd_pinconf_group_get,
  665. .pin_config_group_set = amd_pinconf_group_set,
  666. };
  667. #ifdef CONFIG_PM_SLEEP
  668. static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
  669. {
  670. const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
  671. if (!pd)
  672. return false;
  673. /*
  674. * Only restore the pin if it is actually in use by the kernel (or
  675. * by userspace).
  676. */
  677. if (pd->mux_owner || pd->gpio_owner ||
  678. gpiochip_line_is_irq(&gpio_dev->gc, pin))
  679. return true;
  680. return false;
  681. }
  682. static int amd_gpio_suspend(struct device *dev)
  683. {
  684. struct platform_device *pdev = to_platform_device(dev);
  685. struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
  686. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  687. int i;
  688. for (i = 0; i < desc->npins; i++) {
  689. int pin = desc->pins[i].number;
  690. if (!amd_gpio_should_save(gpio_dev, pin))
  691. continue;
  692. gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin*4);
  693. }
  694. return 0;
  695. }
  696. static int amd_gpio_resume(struct device *dev)
  697. {
  698. struct platform_device *pdev = to_platform_device(dev);
  699. struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
  700. struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
  701. int i;
  702. for (i = 0; i < desc->npins; i++) {
  703. int pin = desc->pins[i].number;
  704. if (!amd_gpio_should_save(gpio_dev, pin))
  705. continue;
  706. writel(gpio_dev->saved_regs[i], gpio_dev->base + pin*4);
  707. }
  708. return 0;
  709. }
  710. static const struct dev_pm_ops amd_gpio_pm_ops = {
  711. SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
  712. amd_gpio_resume)
  713. };
  714. #endif
  715. static struct pinctrl_desc amd_pinctrl_desc = {
  716. .pins = kerncz_pins,
  717. .npins = ARRAY_SIZE(kerncz_pins),
  718. .pctlops = &amd_pinctrl_ops,
  719. .confops = &amd_pinconf_ops,
  720. .owner = THIS_MODULE,
  721. };
  722. static int amd_gpio_probe(struct platform_device *pdev)
  723. {
  724. int ret = 0;
  725. int irq_base;
  726. struct resource *res;
  727. struct amd_gpio *gpio_dev;
  728. gpio_dev = devm_kzalloc(&pdev->dev,
  729. sizeof(struct amd_gpio), GFP_KERNEL);
  730. if (!gpio_dev)
  731. return -ENOMEM;
  732. raw_spin_lock_init(&gpio_dev->lock);
  733. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  734. if (!res) {
  735. dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
  736. return -EINVAL;
  737. }
  738. gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
  739. resource_size(res));
  740. if (!gpio_dev->base)
  741. return -ENOMEM;
  742. irq_base = platform_get_irq(pdev, 0);
  743. if (irq_base < 0) {
  744. dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base);
  745. return irq_base;
  746. }
  747. #ifdef CONFIG_PM_SLEEP
  748. gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
  749. sizeof(*gpio_dev->saved_regs),
  750. GFP_KERNEL);
  751. if (!gpio_dev->saved_regs)
  752. return -ENOMEM;
  753. #endif
  754. gpio_dev->pdev = pdev;
  755. gpio_dev->gc.get_direction = amd_gpio_get_direction;
  756. gpio_dev->gc.direction_input = amd_gpio_direction_input;
  757. gpio_dev->gc.direction_output = amd_gpio_direction_output;
  758. gpio_dev->gc.get = amd_gpio_get_value;
  759. gpio_dev->gc.set = amd_gpio_set_value;
  760. gpio_dev->gc.set_config = amd_gpio_set_config;
  761. gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
  762. gpio_dev->gc.base = -1;
  763. gpio_dev->gc.label = pdev->name;
  764. gpio_dev->gc.owner = THIS_MODULE;
  765. gpio_dev->gc.parent = &pdev->dev;
  766. gpio_dev->gc.ngpio = resource_size(res) / 4;
  767. #if defined(CONFIG_OF_GPIO)
  768. gpio_dev->gc.of_node = pdev->dev.of_node;
  769. #endif
  770. gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
  771. gpio_dev->groups = kerncz_groups;
  772. gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
  773. amd_pinctrl_desc.name = dev_name(&pdev->dev);
  774. gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
  775. gpio_dev);
  776. if (IS_ERR(gpio_dev->pctrl)) {
  777. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  778. return PTR_ERR(gpio_dev->pctrl);
  779. }
  780. ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
  781. if (ret)
  782. return ret;
  783. ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
  784. 0, 0, gpio_dev->gc.ngpio);
  785. if (ret) {
  786. dev_err(&pdev->dev, "Failed to add pin range\n");
  787. goto out2;
  788. }
  789. ret = gpiochip_irqchip_add(&gpio_dev->gc,
  790. &amd_gpio_irqchip,
  791. 0,
  792. handle_simple_irq,
  793. IRQ_TYPE_NONE);
  794. if (ret) {
  795. dev_err(&pdev->dev, "could not add irqchip\n");
  796. ret = -ENODEV;
  797. goto out2;
  798. }
  799. ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0,
  800. KBUILD_MODNAME, gpio_dev);
  801. if (ret)
  802. goto out2;
  803. platform_set_drvdata(pdev, gpio_dev);
  804. dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
  805. return ret;
  806. out2:
  807. gpiochip_remove(&gpio_dev->gc);
  808. return ret;
  809. }
  810. static int amd_gpio_remove(struct platform_device *pdev)
  811. {
  812. struct amd_gpio *gpio_dev;
  813. gpio_dev = platform_get_drvdata(pdev);
  814. gpiochip_remove(&gpio_dev->gc);
  815. return 0;
  816. }
  817. static const struct acpi_device_id amd_gpio_acpi_match[] = {
  818. { "AMD0030", 0 },
  819. { "AMDI0030", 0},
  820. { },
  821. };
  822. MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
  823. static struct platform_driver amd_gpio_driver = {
  824. .driver = {
  825. .name = "amd_gpio",
  826. .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
  827. #ifdef CONFIG_PM_SLEEP
  828. .pm = &amd_gpio_pm_ops,
  829. #endif
  830. },
  831. .probe = amd_gpio_probe,
  832. .remove = amd_gpio_remove,
  833. };
  834. module_platform_driver(amd_gpio_driver);
  835. MODULE_LICENSE("GPL v2");
  836. MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
  837. MODULE_DESCRIPTION("AMD GPIO pinctrl driver");