gpio-lynxpoint.c 12 KB

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