langwell_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* langwell_gpio.c Moorestown platform Langwell chip GPIO driver
  2. * Copyright (c) 2008 - 2009, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Supports:
  18. * Moorestown platform Langwell chip.
  19. * Medfield platform Penwell chip.
  20. * Whitney point.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/stddef.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/init.h>
  30. #include <linux/irq.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <linux/slab.h>
  34. /*
  35. * Langwell chip has 64 pins and thus there are 2 32bit registers to control
  36. * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
  37. * registers to control them, so we only define the order here instead of a
  38. * structure, to get a bit offset for a pin (use GPDR as an example):
  39. *
  40. * nreg = ngpio / 32;
  41. * reg = offset / 32;
  42. * bit = offset % 32;
  43. * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
  44. *
  45. * so the bit of reg_addr is to control pin offset's GPDR feature
  46. */
  47. enum GPIO_REG {
  48. GPLR = 0, /* pin level read-only */
  49. GPDR, /* pin direction */
  50. GPSR, /* pin set */
  51. GPCR, /* pin clear */
  52. GRER, /* rising edge detect */
  53. GFER, /* falling edge detect */
  54. GEDR, /* edge detect result */
  55. };
  56. struct lnw_gpio {
  57. struct gpio_chip chip;
  58. void *reg_base;
  59. spinlock_t lock;
  60. unsigned irq_base;
  61. };
  62. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  63. enum GPIO_REG reg_type)
  64. {
  65. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  66. unsigned nreg = chip->ngpio / 32;
  67. u8 reg = offset / 32;
  68. void __iomem *ptr;
  69. ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
  70. return ptr;
  71. }
  72. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  73. {
  74. void __iomem *gplr = gpio_reg(chip, offset, GPLR);
  75. return readl(gplr) & BIT(offset % 32);
  76. }
  77. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  78. {
  79. void __iomem *gpsr, *gpcr;
  80. if (value) {
  81. gpsr = gpio_reg(chip, offset, GPSR);
  82. writel(BIT(offset % 32), gpsr);
  83. } else {
  84. gpcr = gpio_reg(chip, offset, GPCR);
  85. writel(BIT(offset % 32), gpcr);
  86. }
  87. }
  88. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  89. {
  90. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  91. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  92. u32 value;
  93. unsigned long flags;
  94. spin_lock_irqsave(&lnw->lock, flags);
  95. value = readl(gpdr);
  96. value &= ~BIT(offset % 32);
  97. writel(value, gpdr);
  98. spin_unlock_irqrestore(&lnw->lock, flags);
  99. return 0;
  100. }
  101. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  102. unsigned offset, int value)
  103. {
  104. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  105. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  106. unsigned long flags;
  107. lnw_gpio_set(chip, offset, value);
  108. spin_lock_irqsave(&lnw->lock, flags);
  109. value = readl(gpdr);
  110. value |= BIT(offset % 32);;
  111. writel(value, gpdr);
  112. spin_unlock_irqrestore(&lnw->lock, flags);
  113. return 0;
  114. }
  115. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  118. return lnw->irq_base + offset;
  119. }
  120. static int lnw_irq_type(struct irq_data *d, unsigned type)
  121. {
  122. struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
  123. u32 gpio = d->irq - lnw->irq_base;
  124. unsigned long flags;
  125. u32 value;
  126. void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
  127. void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
  128. if (gpio >= lnw->chip.ngpio)
  129. return -EINVAL;
  130. spin_lock_irqsave(&lnw->lock, flags);
  131. if (type & IRQ_TYPE_EDGE_RISING)
  132. value = readl(grer) | BIT(gpio % 32);
  133. else
  134. value = readl(grer) & (~BIT(gpio % 32));
  135. writel(value, grer);
  136. if (type & IRQ_TYPE_EDGE_FALLING)
  137. value = readl(gfer) | BIT(gpio % 32);
  138. else
  139. value = readl(gfer) & (~BIT(gpio % 32));
  140. writel(value, gfer);
  141. spin_unlock_irqrestore(&lnw->lock, flags);
  142. return 0;
  143. }
  144. static void lnw_irq_unmask(struct irq_data *d)
  145. {
  146. }
  147. static void lnw_irq_mask(struct irq_data *d)
  148. {
  149. }
  150. static struct irq_chip lnw_irqchip = {
  151. .name = "LNW-GPIO",
  152. .irq_mask = lnw_irq_mask,
  153. .irq_unmask = lnw_irq_unmask,
  154. .irq_set_type = lnw_irq_type,
  155. };
  156. static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = { /* pin number */
  157. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
  158. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
  159. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
  160. { 0, }
  161. };
  162. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  163. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  164. {
  165. struct irq_data *data = irq_desc_get_irq_data(desc);
  166. struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
  167. struct irq_chip *chip = irq_data_get_irq_chip(data);
  168. u32 base, gpio, mask;
  169. unsigned long pending;
  170. void __iomem *gedr;
  171. /* check GPIO controller to check which pin triggered the interrupt */
  172. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  173. gedr = gpio_reg(&lnw->chip, base, GEDR);
  174. pending = readl(gedr);
  175. while (pending) {
  176. gpio = __ffs(pending) - 1;
  177. mask = BIT(gpio);
  178. pending &= ~mask;
  179. /* Clear before handling so we can't lose an edge */
  180. writel(mask, gedr);
  181. generic_handle_irq(lnw->irq_base + base + gpio);
  182. }
  183. }
  184. chip->irq_eoi(data);
  185. }
  186. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  187. const struct pci_device_id *id)
  188. {
  189. void *base;
  190. int i;
  191. resource_size_t start, len;
  192. struct lnw_gpio *lnw;
  193. u32 irq_base;
  194. u32 gpio_base;
  195. int retval = 0;
  196. retval = pci_enable_device(pdev);
  197. if (retval)
  198. goto done;
  199. retval = pci_request_regions(pdev, "langwell_gpio");
  200. if (retval) {
  201. dev_err(&pdev->dev, "error requesting resources\n");
  202. goto err2;
  203. }
  204. /* get the irq_base from bar1 */
  205. start = pci_resource_start(pdev, 1);
  206. len = pci_resource_len(pdev, 1);
  207. base = ioremap_nocache(start, len);
  208. if (!base) {
  209. dev_err(&pdev->dev, "error mapping bar1\n");
  210. goto err3;
  211. }
  212. irq_base = *(u32 *)base;
  213. gpio_base = *((u32 *)base + 1);
  214. /* release the IO mapping, since we already get the info from bar1 */
  215. iounmap(base);
  216. /* get the register base from bar0 */
  217. start = pci_resource_start(pdev, 0);
  218. len = pci_resource_len(pdev, 0);
  219. base = ioremap_nocache(start, len);
  220. if (!base) {
  221. dev_err(&pdev->dev, "error mapping bar0\n");
  222. retval = -EFAULT;
  223. goto err3;
  224. }
  225. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  226. if (!lnw) {
  227. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  228. retval = -ENOMEM;
  229. goto err4;
  230. }
  231. lnw->reg_base = base;
  232. lnw->irq_base = irq_base;
  233. lnw->chip.label = dev_name(&pdev->dev);
  234. lnw->chip.direction_input = lnw_gpio_direction_input;
  235. lnw->chip.direction_output = lnw_gpio_direction_output;
  236. lnw->chip.get = lnw_gpio_get;
  237. lnw->chip.set = lnw_gpio_set;
  238. lnw->chip.to_irq = lnw_gpio_to_irq;
  239. lnw->chip.base = gpio_base;
  240. lnw->chip.ngpio = id->driver_data;
  241. lnw->chip.can_sleep = 0;
  242. pci_set_drvdata(pdev, lnw);
  243. retval = gpiochip_add(&lnw->chip);
  244. if (retval) {
  245. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  246. goto err5;
  247. }
  248. irq_set_handler_data(pdev->irq, lnw);
  249. irq_set_chained_handler(pdev->irq, lnw_irq_handler);
  250. for (i = 0; i < lnw->chip.ngpio; i++) {
  251. irq_set_chip_and_handler_name(i + lnw->irq_base, &lnw_irqchip,
  252. handle_simple_irq, "demux");
  253. irq_set_chip_data(i + lnw->irq_base, lnw);
  254. }
  255. spin_lock_init(&lnw->lock);
  256. goto done;
  257. err5:
  258. kfree(lnw);
  259. err4:
  260. iounmap(base);
  261. err3:
  262. pci_release_regions(pdev);
  263. err2:
  264. pci_disable_device(pdev);
  265. done:
  266. return retval;
  267. }
  268. static struct pci_driver lnw_gpio_driver = {
  269. .name = "langwell_gpio",
  270. .id_table = lnw_gpio_ids,
  271. .probe = lnw_gpio_probe,
  272. };
  273. static int __devinit wp_gpio_probe(struct platform_device *pdev)
  274. {
  275. struct lnw_gpio *lnw;
  276. struct gpio_chip *gc;
  277. struct resource *rc;
  278. int retval = 0;
  279. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. if (!rc)
  281. return -EINVAL;
  282. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  283. if (!lnw) {
  284. dev_err(&pdev->dev,
  285. "can't allocate whitneypoint_gpio chip data\n");
  286. return -ENOMEM;
  287. }
  288. lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
  289. if (lnw->reg_base == NULL) {
  290. retval = -EINVAL;
  291. goto err_kmalloc;
  292. }
  293. spin_lock_init(&lnw->lock);
  294. gc = &lnw->chip;
  295. gc->label = dev_name(&pdev->dev);
  296. gc->owner = THIS_MODULE;
  297. gc->direction_input = lnw_gpio_direction_input;
  298. gc->direction_output = lnw_gpio_direction_output;
  299. gc->get = lnw_gpio_get;
  300. gc->set = lnw_gpio_set;
  301. gc->to_irq = NULL;
  302. gc->base = 0;
  303. gc->ngpio = 64;
  304. gc->can_sleep = 0;
  305. retval = gpiochip_add(gc);
  306. if (retval) {
  307. dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
  308. retval);
  309. goto err_ioremap;
  310. }
  311. platform_set_drvdata(pdev, lnw);
  312. return 0;
  313. err_ioremap:
  314. iounmap(lnw->reg_base);
  315. err_kmalloc:
  316. kfree(lnw);
  317. return retval;
  318. }
  319. static int __devexit wp_gpio_remove(struct platform_device *pdev)
  320. {
  321. struct lnw_gpio *lnw = platform_get_drvdata(pdev);
  322. int err;
  323. err = gpiochip_remove(&lnw->chip);
  324. if (err)
  325. dev_err(&pdev->dev, "failed to remove gpio_chip.\n");
  326. iounmap(lnw->reg_base);
  327. kfree(lnw);
  328. platform_set_drvdata(pdev, NULL);
  329. return 0;
  330. }
  331. static struct platform_driver wp_gpio_driver = {
  332. .probe = wp_gpio_probe,
  333. .remove = __devexit_p(wp_gpio_remove),
  334. .driver = {
  335. .name = "wp_gpio",
  336. .owner = THIS_MODULE,
  337. },
  338. };
  339. static int __init lnw_gpio_init(void)
  340. {
  341. int ret;
  342. ret = pci_register_driver(&lnw_gpio_driver);
  343. if (ret < 0)
  344. return ret;
  345. ret = platform_driver_register(&wp_gpio_driver);
  346. if (ret < 0)
  347. pci_unregister_driver(&lnw_gpio_driver);
  348. return ret;
  349. }
  350. device_initcall(lnw_gpio_init);