pinctrl-amd.c 24 KB

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