gpio-lynxpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * GPIO controller driver for Intel Lynxpoint PCH chipset>
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/bitops.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/gpio/driver.h>
  28. #include <linux/slab.h>
  29. #include <linux/acpi.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/io.h>
  33. /* LynxPoint chipset has support for 94 gpio pins */
  34. #define LP_NUM_GPIO 94
  35. /* Bitmapped register offsets */
  36. #define LP_ACPI_OWNED 0x00 /* Bitmap, set by bios, 0: pin reserved for ACPI */
  37. #define LP_GC 0x7C /* set APIC IRQ to IRQ14 or IRQ15 for all pins */
  38. #define LP_INT_STAT 0x80
  39. #define LP_INT_ENABLE 0x90
  40. /* Each pin has two 32 bit config registers, starting at 0x100 */
  41. #define LP_CONFIG1 0x100
  42. #define LP_CONFIG2 0x104
  43. /* LP_CONFIG1 reg bits */
  44. #define OUT_LVL_BIT BIT(31)
  45. #define IN_LVL_BIT BIT(30)
  46. #define TRIG_SEL_BIT BIT(4) /* 0: Edge, 1: Level */
  47. #define INT_INV_BIT BIT(3) /* Invert interrupt triggering */
  48. #define DIR_BIT BIT(2) /* 0: Output, 1: Input */
  49. #define USE_SEL_BIT BIT(0) /* 0: Native, 1: GPIO */
  50. /* LP_CONFIG2 reg bits */
  51. #define GPINDIS_BIT BIT(2) /* disable input sensing */
  52. #define GPIWP_BIT (BIT(0) | BIT(1)) /* weak pull options */
  53. struct lp_gpio {
  54. struct gpio_chip chip;
  55. struct platform_device *pdev;
  56. spinlock_t lock;
  57. unsigned long reg_base;
  58. };
  59. /*
  60. * Lynxpoint gpios are controlled through both bitmapped registers and
  61. * per gpio specific registers. The bitmapped registers are in chunks of
  62. * 3 x 32bit registers to cover all 94 gpios
  63. *
  64. * per gpio specific registers consist of two 32bit registers per gpio
  65. * (LP_CONFIG1 and LP_CONFIG2), with 94 gpios there's a total of
  66. * 188 config registers.
  67. *
  68. * A simplified view of the register layout look like this:
  69. *
  70. * LP_ACPI_OWNED[31:0] gpio ownerships for gpios 0-31 (bitmapped registers)
  71. * LP_ACPI_OWNED[63:32] gpio ownerships for gpios 32-63
  72. * LP_ACPI_OWNED[94:64] gpio ownerships for gpios 63-94
  73. * ...
  74. * LP_INT_ENABLE[31:0] ...
  75. * LP_INT_ENABLE[63:31] ...
  76. * LP_INT_ENABLE[94:64] ...
  77. * LP0_CONFIG1 (gpio 0) config1 reg for gpio 0 (per gpio registers)
  78. * LP0_CONFIG2 (gpio 0) config2 reg for gpio 0
  79. * LP1_CONFIG1 (gpio 1) config1 reg for gpio 1
  80. * LP1_CONFIG2 (gpio 1) config2 reg for gpio 1
  81. * LP2_CONFIG1 (gpio 2) ...
  82. * LP2_CONFIG2 (gpio 2) ...
  83. * ...
  84. * LP94_CONFIG1 (gpio 94) ...
  85. * LP94_CONFIG2 (gpio 94) ...
  86. */
  87. static unsigned long lp_gpio_reg(struct gpio_chip *chip, unsigned offset,
  88. int reg)
  89. {
  90. struct lp_gpio *lg = gpiochip_get_data(chip);
  91. int reg_offset;
  92. if (reg == LP_CONFIG1 || reg == LP_CONFIG2)
  93. /* per gpio specific config registers */
  94. reg_offset = offset * 8;
  95. else
  96. /* bitmapped registers */
  97. reg_offset = (offset / 32) * 4;
  98. return lg->reg_base + reg + reg_offset;
  99. }
  100. static int lp_gpio_request(struct gpio_chip *chip, unsigned offset)
  101. {
  102. struct lp_gpio *lg = gpiochip_get_data(chip);
  103. unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  104. unsigned long conf2 = lp_gpio_reg(chip, offset, LP_CONFIG2);
  105. unsigned long acpi_use = lp_gpio_reg(chip, offset, LP_ACPI_OWNED);
  106. pm_runtime_get(&lg->pdev->dev); /* should we put if failed */
  107. /* Fail if BIOS reserved pin for ACPI use */
  108. if (!(inl(acpi_use) & BIT(offset % 32))) {
  109. dev_err(&lg->pdev->dev, "gpio %d reserved for ACPI\n", offset);
  110. return -EBUSY;
  111. }
  112. /* Fail if pin is in alternate function mode (not GPIO mode) */
  113. if (!(inl(reg) & USE_SEL_BIT))
  114. return -ENODEV;
  115. /* enable input sensing */
  116. outl(inl(conf2) & ~GPINDIS_BIT, conf2);
  117. return 0;
  118. }
  119. static void lp_gpio_free(struct gpio_chip *chip, unsigned offset)
  120. {
  121. struct lp_gpio *lg = gpiochip_get_data(chip);
  122. unsigned long conf2 = lp_gpio_reg(chip, offset, LP_CONFIG2);
  123. /* disable input sensing */
  124. outl(inl(conf2) | GPINDIS_BIT, conf2);
  125. pm_runtime_put(&lg->pdev->dev);
  126. }
  127. static int lp_irq_type(struct irq_data *d, unsigned type)
  128. {
  129. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  130. struct lp_gpio *lg = gpiochip_get_data(gc);
  131. u32 hwirq = irqd_to_hwirq(d);
  132. unsigned long flags;
  133. u32 value;
  134. unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_CONFIG1);
  135. if (hwirq >= lg->chip.ngpio)
  136. return -EINVAL;
  137. spin_lock_irqsave(&lg->lock, flags);
  138. value = inl(reg);
  139. /* set both TRIG_SEL and INV bits to 0 for rising edge */
  140. if (type & IRQ_TYPE_EDGE_RISING)
  141. value &= ~(TRIG_SEL_BIT | INT_INV_BIT);
  142. /* TRIG_SEL bit 0, INV bit 1 for falling edge */
  143. if (type & IRQ_TYPE_EDGE_FALLING)
  144. value = (value | INT_INV_BIT) & ~TRIG_SEL_BIT;
  145. /* TRIG_SEL bit 1, INV bit 0 for level low */
  146. if (type & IRQ_TYPE_LEVEL_LOW)
  147. value = (value | TRIG_SEL_BIT) & ~INT_INV_BIT;
  148. /* TRIG_SEL bit 1, INV bit 1 for level high */
  149. if (type & IRQ_TYPE_LEVEL_HIGH)
  150. value |= TRIG_SEL_BIT | INT_INV_BIT;
  151. outl(value, reg);
  152. spin_unlock_irqrestore(&lg->lock, flags);
  153. return 0;
  154. }
  155. static int lp_gpio_get(struct gpio_chip *chip, unsigned offset)
  156. {
  157. unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  158. return !!(inl(reg) & IN_LVL_BIT);
  159. }
  160. static void lp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  161. {
  162. struct lp_gpio *lg = gpiochip_get_data(chip);
  163. unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  164. unsigned long flags;
  165. spin_lock_irqsave(&lg->lock, flags);
  166. if (value)
  167. outl(inl(reg) | OUT_LVL_BIT, reg);
  168. else
  169. outl(inl(reg) & ~OUT_LVL_BIT, reg);
  170. spin_unlock_irqrestore(&lg->lock, flags);
  171. }
  172. static int lp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  173. {
  174. struct lp_gpio *lg = gpiochip_get_data(chip);
  175. unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  176. unsigned long flags;
  177. spin_lock_irqsave(&lg->lock, flags);
  178. outl(inl(reg) | DIR_BIT, reg);
  179. spin_unlock_irqrestore(&lg->lock, flags);
  180. return 0;
  181. }
  182. static int lp_gpio_direction_output(struct gpio_chip *chip,
  183. unsigned offset, int value)
  184. {
  185. struct lp_gpio *lg = gpiochip_get_data(chip);
  186. unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  187. unsigned long flags;
  188. lp_gpio_set(chip, offset, value);
  189. spin_lock_irqsave(&lg->lock, flags);
  190. outl(inl(reg) & ~DIR_BIT, reg);
  191. spin_unlock_irqrestore(&lg->lock, flags);
  192. return 0;
  193. }
  194. static void lp_gpio_irq_handler(struct irq_desc *desc)
  195. {
  196. struct irq_data *data = irq_desc_get_irq_data(desc);
  197. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  198. struct lp_gpio *lg = gpiochip_get_data(gc);
  199. struct irq_chip *chip = irq_data_get_irq_chip(data);
  200. u32 base, pin, mask;
  201. unsigned long reg, ena, pending;
  202. /* check from GPIO controller which pin triggered the interrupt */
  203. for (base = 0; base < lg->chip.ngpio; base += 32) {
  204. reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
  205. ena = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
  206. while ((pending = (inl(reg) & inl(ena)))) {
  207. unsigned irq;
  208. pin = __ffs(pending);
  209. mask = BIT(pin);
  210. /* Clear before handling so we don't lose an edge */
  211. outl(mask, reg);
  212. irq = irq_find_mapping(lg->chip.irq.domain, base + pin);
  213. generic_handle_irq(irq);
  214. }
  215. }
  216. chip->irq_eoi(data);
  217. }
  218. static void lp_irq_unmask(struct irq_data *d)
  219. {
  220. }
  221. static void lp_irq_mask(struct irq_data *d)
  222. {
  223. }
  224. static void lp_irq_enable(struct irq_data *d)
  225. {
  226. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  227. struct lp_gpio *lg = gpiochip_get_data(gc);
  228. u32 hwirq = irqd_to_hwirq(d);
  229. unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
  230. unsigned long flags;
  231. spin_lock_irqsave(&lg->lock, flags);
  232. outl(inl(reg) | BIT(hwirq % 32), reg);
  233. spin_unlock_irqrestore(&lg->lock, flags);
  234. }
  235. static void lp_irq_disable(struct irq_data *d)
  236. {
  237. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  238. struct lp_gpio *lg = gpiochip_get_data(gc);
  239. u32 hwirq = irqd_to_hwirq(d);
  240. unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
  241. unsigned long flags;
  242. spin_lock_irqsave(&lg->lock, flags);
  243. outl(inl(reg) & ~BIT(hwirq % 32), reg);
  244. spin_unlock_irqrestore(&lg->lock, flags);
  245. }
  246. static struct irq_chip lp_irqchip = {
  247. .name = "LP-GPIO",
  248. .irq_mask = lp_irq_mask,
  249. .irq_unmask = lp_irq_unmask,
  250. .irq_enable = lp_irq_enable,
  251. .irq_disable = lp_irq_disable,
  252. .irq_set_type = lp_irq_type,
  253. .flags = IRQCHIP_SKIP_SET_WAKE,
  254. };
  255. static void lp_gpio_irq_init_hw(struct lp_gpio *lg)
  256. {
  257. unsigned long reg;
  258. unsigned base;
  259. for (base = 0; base < lg->chip.ngpio; base += 32) {
  260. /* disable gpio pin interrupts */
  261. reg = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
  262. outl(0, reg);
  263. /* Clear interrupt status register */
  264. reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
  265. outl(0xffffffff, reg);
  266. }
  267. }
  268. static int lp_gpio_probe(struct platform_device *pdev)
  269. {
  270. struct lp_gpio *lg;
  271. struct gpio_chip *gc;
  272. struct resource *io_rc, *irq_rc;
  273. struct device *dev = &pdev->dev;
  274. unsigned long reg_len;
  275. int ret = -ENODEV;
  276. lg = devm_kzalloc(dev, sizeof(struct lp_gpio), GFP_KERNEL);
  277. if (!lg)
  278. return -ENOMEM;
  279. lg->pdev = pdev;
  280. platform_set_drvdata(pdev, lg);
  281. io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
  282. irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  283. if (!io_rc) {
  284. dev_err(dev, "missing IO resources\n");
  285. return -EINVAL;
  286. }
  287. lg->reg_base = io_rc->start;
  288. reg_len = resource_size(io_rc);
  289. if (!devm_request_region(dev, lg->reg_base, reg_len, "lp-gpio")) {
  290. dev_err(dev, "failed requesting IO region 0x%x\n",
  291. (unsigned int)lg->reg_base);
  292. return -EBUSY;
  293. }
  294. spin_lock_init(&lg->lock);
  295. gc = &lg->chip;
  296. gc->label = dev_name(dev);
  297. gc->owner = THIS_MODULE;
  298. gc->request = lp_gpio_request;
  299. gc->free = lp_gpio_free;
  300. gc->direction_input = lp_gpio_direction_input;
  301. gc->direction_output = lp_gpio_direction_output;
  302. gc->get = lp_gpio_get;
  303. gc->set = lp_gpio_set;
  304. gc->base = -1;
  305. gc->ngpio = LP_NUM_GPIO;
  306. gc->can_sleep = false;
  307. gc->parent = dev;
  308. ret = devm_gpiochip_add_data(dev, gc, lg);
  309. if (ret) {
  310. dev_err(dev, "failed adding lp-gpio chip\n");
  311. return ret;
  312. }
  313. /* set up interrupts */
  314. if (irq_rc && irq_rc->start) {
  315. lp_gpio_irq_init_hw(lg);
  316. ret = gpiochip_irqchip_add(gc, &lp_irqchip, 0,
  317. handle_simple_irq, IRQ_TYPE_NONE);
  318. if (ret) {
  319. dev_err(dev, "failed to add irqchip\n");
  320. return ret;
  321. }
  322. gpiochip_set_chained_irqchip(gc, &lp_irqchip,
  323. (unsigned)irq_rc->start,
  324. lp_gpio_irq_handler);
  325. }
  326. pm_runtime_enable(dev);
  327. return 0;
  328. }
  329. static int lp_gpio_runtime_suspend(struct device *dev)
  330. {
  331. return 0;
  332. }
  333. static int lp_gpio_runtime_resume(struct device *dev)
  334. {
  335. return 0;
  336. }
  337. static int lp_gpio_resume(struct device *dev)
  338. {
  339. struct platform_device *pdev = to_platform_device(dev);
  340. struct lp_gpio *lg = platform_get_drvdata(pdev);
  341. unsigned long reg;
  342. int i;
  343. /* on some hardware suspend clears input sensing, re-enable it here */
  344. for (i = 0; i < lg->chip.ngpio; i++) {
  345. if (gpiochip_is_requested(&lg->chip, i) != NULL) {
  346. reg = lp_gpio_reg(&lg->chip, i, LP_CONFIG2);
  347. outl(inl(reg) & ~GPINDIS_BIT, reg);
  348. }
  349. }
  350. return 0;
  351. }
  352. static const struct dev_pm_ops lp_gpio_pm_ops = {
  353. .runtime_suspend = lp_gpio_runtime_suspend,
  354. .runtime_resume = lp_gpio_runtime_resume,
  355. .resume = lp_gpio_resume,
  356. };
  357. static const struct acpi_device_id lynxpoint_gpio_acpi_match[] = {
  358. { "INT33C7", 0 },
  359. { "INT3437", 0 },
  360. { }
  361. };
  362. MODULE_DEVICE_TABLE(acpi, lynxpoint_gpio_acpi_match);
  363. static int lp_gpio_remove(struct platform_device *pdev)
  364. {
  365. pm_runtime_disable(&pdev->dev);
  366. return 0;
  367. }
  368. static struct platform_driver lp_gpio_driver = {
  369. .probe = lp_gpio_probe,
  370. .remove = lp_gpio_remove,
  371. .driver = {
  372. .name = "lp_gpio",
  373. .pm = &lp_gpio_pm_ops,
  374. .acpi_match_table = ACPI_PTR(lynxpoint_gpio_acpi_match),
  375. },
  376. };
  377. static int __init lp_gpio_init(void)
  378. {
  379. return platform_driver_register(&lp_gpio_driver);
  380. }
  381. static void __exit lp_gpio_exit(void)
  382. {
  383. platform_driver_unregister(&lp_gpio_driver);
  384. }
  385. subsys_initcall(lp_gpio_init);
  386. module_exit(lp_gpio_exit);
  387. MODULE_AUTHOR("Mathias Nyman (Intel)");
  388. MODULE_DESCRIPTION("GPIO interface for Intel Lynxpoint");
  389. MODULE_LICENSE("GPL");
  390. MODULE_ALIAS("platform:lp_gpio");