pinctrl-amd.c 25 KB

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