gpio-lynxpoint.c 12 KB

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