gpio-sch.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GPIO interface for Intel Poulsbo SCH
  4. *
  5. * Copyright (c) 2010 CompuLab Ltd
  6. * Author: Denis Turischev <denis@compulab.co.il>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitops.h>
  10. #include <linux/errno.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pci_ids.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #define GEN 0x00
  20. #define GIO 0x04
  21. #define GLV 0x08
  22. #define GTPE 0x0c
  23. #define GTNE 0x10
  24. #define GGPE 0x14
  25. #define GSMI 0x18
  26. #define GTS 0x1c
  27. #define CORE_BANK_OFFSET 0x00
  28. #define RESUME_BANK_OFFSET 0x20
  29. /*
  30. * iLB datasheet describes GPE0BLK registers, in particular GPE0E.GPIO bit.
  31. * Document Number: 328195-001
  32. */
  33. #define GPE0E_GPIO 14
  34. struct sch_gpio {
  35. struct gpio_chip chip;
  36. void __iomem *regs;
  37. spinlock_t lock;
  38. unsigned short resume_base;
  39. /* GPE handling */
  40. u32 gpe;
  41. acpi_gpe_handler gpe_handler;
  42. };
  43. static unsigned int sch_gpio_offset(struct sch_gpio *sch, unsigned int gpio,
  44. unsigned int reg)
  45. {
  46. unsigned int base = CORE_BANK_OFFSET;
  47. if (gpio >= sch->resume_base) {
  48. gpio -= sch->resume_base;
  49. base = RESUME_BANK_OFFSET;
  50. }
  51. return base + reg + gpio / 8;
  52. }
  53. static unsigned int sch_gpio_bit(struct sch_gpio *sch, unsigned int gpio)
  54. {
  55. if (gpio >= sch->resume_base)
  56. gpio -= sch->resume_base;
  57. return gpio % 8;
  58. }
  59. static int sch_gpio_reg_get(struct sch_gpio *sch, unsigned int gpio, unsigned int reg)
  60. {
  61. unsigned short offset, bit;
  62. u8 reg_val;
  63. offset = sch_gpio_offset(sch, gpio, reg);
  64. bit = sch_gpio_bit(sch, gpio);
  65. reg_val = !!(ioread8(sch->regs + offset) & BIT(bit));
  66. return reg_val;
  67. }
  68. static void sch_gpio_reg_set(struct sch_gpio *sch, unsigned int gpio, unsigned int reg,
  69. int val)
  70. {
  71. unsigned short offset, bit;
  72. u8 reg_val;
  73. offset = sch_gpio_offset(sch, gpio, reg);
  74. bit = sch_gpio_bit(sch, gpio);
  75. reg_val = ioread8(sch->regs + offset);
  76. if (val)
  77. reg_val |= BIT(bit);
  78. else
  79. reg_val &= ~BIT(bit);
  80. iowrite8(reg_val, sch->regs + offset);
  81. }
  82. static int sch_gpio_direction_in(struct gpio_chip *gc, unsigned int gpio_num)
  83. {
  84. struct sch_gpio *sch = gpiochip_get_data(gc);
  85. unsigned long flags;
  86. spin_lock_irqsave(&sch->lock, flags);
  87. sch_gpio_reg_set(sch, gpio_num, GIO, 1);
  88. spin_unlock_irqrestore(&sch->lock, flags);
  89. return 0;
  90. }
  91. static int sch_gpio_get(struct gpio_chip *gc, unsigned int gpio_num)
  92. {
  93. struct sch_gpio *sch = gpiochip_get_data(gc);
  94. return sch_gpio_reg_get(sch, gpio_num, GLV);
  95. }
  96. static void sch_gpio_set(struct gpio_chip *gc, unsigned int gpio_num, int val)
  97. {
  98. struct sch_gpio *sch = gpiochip_get_data(gc);
  99. unsigned long flags;
  100. spin_lock_irqsave(&sch->lock, flags);
  101. sch_gpio_reg_set(sch, gpio_num, GLV, val);
  102. spin_unlock_irqrestore(&sch->lock, flags);
  103. }
  104. static int sch_gpio_direction_out(struct gpio_chip *gc, unsigned int gpio_num,
  105. int val)
  106. {
  107. struct sch_gpio *sch = gpiochip_get_data(gc);
  108. unsigned long flags;
  109. spin_lock_irqsave(&sch->lock, flags);
  110. sch_gpio_reg_set(sch, gpio_num, GIO, 0);
  111. spin_unlock_irqrestore(&sch->lock, flags);
  112. /*
  113. * according to the datasheet, writing to the level register has no
  114. * effect when GPIO is programmed as input.
  115. * Actually the level register is read-only when configured as input.
  116. * Thus presetting the output level before switching to output is _NOT_ possible.
  117. * Hence we set the level after configuring the GPIO as output.
  118. * But we cannot prevent a short low pulse if direction is set to high
  119. * and an external pull-up is connected.
  120. */
  121. sch_gpio_set(gc, gpio_num, val);
  122. return 0;
  123. }
  124. static int sch_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio_num)
  125. {
  126. struct sch_gpio *sch = gpiochip_get_data(gc);
  127. if (sch_gpio_reg_get(sch, gpio_num, GIO))
  128. return GPIO_LINE_DIRECTION_IN;
  129. return GPIO_LINE_DIRECTION_OUT;
  130. }
  131. static const struct gpio_chip sch_gpio_chip = {
  132. .label = "sch_gpio",
  133. .owner = THIS_MODULE,
  134. .direction_input = sch_gpio_direction_in,
  135. .get = sch_gpio_get,
  136. .direction_output = sch_gpio_direction_out,
  137. .set = sch_gpio_set,
  138. .get_direction = sch_gpio_get_direction,
  139. };
  140. static int sch_irq_type(struct irq_data *d, unsigned int type)
  141. {
  142. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  143. struct sch_gpio *sch = gpiochip_get_data(gc);
  144. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  145. unsigned long flags;
  146. int rising, falling;
  147. switch (type & IRQ_TYPE_SENSE_MASK) {
  148. case IRQ_TYPE_EDGE_RISING:
  149. rising = 1;
  150. falling = 0;
  151. break;
  152. case IRQ_TYPE_EDGE_FALLING:
  153. rising = 0;
  154. falling = 1;
  155. break;
  156. case IRQ_TYPE_EDGE_BOTH:
  157. rising = 1;
  158. falling = 1;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. spin_lock_irqsave(&sch->lock, flags);
  164. sch_gpio_reg_set(sch, gpio_num, GTPE, rising);
  165. sch_gpio_reg_set(sch, gpio_num, GTNE, falling);
  166. irq_set_handler_locked(d, handle_edge_irq);
  167. spin_unlock_irqrestore(&sch->lock, flags);
  168. return 0;
  169. }
  170. static void sch_irq_ack(struct irq_data *d)
  171. {
  172. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  173. struct sch_gpio *sch = gpiochip_get_data(gc);
  174. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  175. unsigned long flags;
  176. spin_lock_irqsave(&sch->lock, flags);
  177. sch_gpio_reg_set(sch, gpio_num, GTS, 1);
  178. spin_unlock_irqrestore(&sch->lock, flags);
  179. }
  180. static void sch_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t gpio_num, int val)
  181. {
  182. struct sch_gpio *sch = gpiochip_get_data(gc);
  183. unsigned long flags;
  184. spin_lock_irqsave(&sch->lock, flags);
  185. sch_gpio_reg_set(sch, gpio_num, GGPE, val);
  186. spin_unlock_irqrestore(&sch->lock, flags);
  187. }
  188. static void sch_irq_mask(struct irq_data *d)
  189. {
  190. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  191. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  192. sch_irq_mask_unmask(gc, gpio_num, 0);
  193. gpiochip_disable_irq(gc, gpio_num);
  194. }
  195. static void sch_irq_unmask(struct irq_data *d)
  196. {
  197. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  198. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  199. gpiochip_enable_irq(gc, gpio_num);
  200. sch_irq_mask_unmask(gc, gpio_num, 1);
  201. }
  202. static const struct irq_chip sch_irqchip = {
  203. .name = "sch_gpio",
  204. .irq_ack = sch_irq_ack,
  205. .irq_mask = sch_irq_mask,
  206. .irq_unmask = sch_irq_unmask,
  207. .irq_set_type = sch_irq_type,
  208. .flags = IRQCHIP_IMMUTABLE,
  209. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  210. };
  211. static u32 sch_gpio_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
  212. {
  213. struct sch_gpio *sch = context;
  214. struct gpio_chip *gc = &sch->chip;
  215. unsigned long core_status, resume_status;
  216. unsigned long pending;
  217. unsigned long flags;
  218. int offset;
  219. u32 ret;
  220. spin_lock_irqsave(&sch->lock, flags);
  221. core_status = ioread32(sch->regs + CORE_BANK_OFFSET + GTS);
  222. resume_status = ioread32(sch->regs + RESUME_BANK_OFFSET + GTS);
  223. spin_unlock_irqrestore(&sch->lock, flags);
  224. pending = (resume_status << sch->resume_base) | core_status;
  225. for_each_set_bit(offset, &pending, sch->chip.ngpio)
  226. generic_handle_domain_irq(gc->irq.domain, offset);
  227. /* Set returning value depending on whether we handled an interrupt */
  228. ret = pending ? ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  229. /* Acknowledge GPE to ACPICA */
  230. ret |= ACPI_REENABLE_GPE;
  231. return ret;
  232. }
  233. static void sch_gpio_remove_gpe_handler(void *data)
  234. {
  235. struct sch_gpio *sch = data;
  236. acpi_disable_gpe(NULL, sch->gpe);
  237. acpi_remove_gpe_handler(NULL, sch->gpe, sch->gpe_handler);
  238. }
  239. static int sch_gpio_install_gpe_handler(struct sch_gpio *sch)
  240. {
  241. struct device *dev = sch->chip.parent;
  242. acpi_status status;
  243. status = acpi_install_gpe_handler(NULL, sch->gpe, ACPI_GPE_LEVEL_TRIGGERED,
  244. sch->gpe_handler, sch);
  245. if (ACPI_FAILURE(status)) {
  246. dev_err(dev, "Failed to install GPE handler for %u: %s\n",
  247. sch->gpe, acpi_format_exception(status));
  248. return -ENODEV;
  249. }
  250. status = acpi_enable_gpe(NULL, sch->gpe);
  251. if (ACPI_FAILURE(status)) {
  252. dev_err(dev, "Failed to enable GPE handler for %u: %s\n",
  253. sch->gpe, acpi_format_exception(status));
  254. acpi_remove_gpe_handler(NULL, sch->gpe, sch->gpe_handler);
  255. return -ENODEV;
  256. }
  257. return devm_add_action_or_reset(dev, sch_gpio_remove_gpe_handler, sch);
  258. }
  259. static int sch_gpio_probe(struct platform_device *pdev)
  260. {
  261. struct device *dev = &pdev->dev;
  262. struct gpio_irq_chip *girq;
  263. struct sch_gpio *sch;
  264. struct resource *res;
  265. void __iomem *regs;
  266. int ret;
  267. sch = devm_kzalloc(dev, sizeof(*sch), GFP_KERNEL);
  268. if (!sch)
  269. return -ENOMEM;
  270. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  271. if (!res)
  272. return -EBUSY;
  273. regs = devm_ioport_map(dev, res->start, resource_size(res));
  274. if (!regs)
  275. return -EBUSY;
  276. sch->regs = regs;
  277. spin_lock_init(&sch->lock);
  278. sch->chip = sch_gpio_chip;
  279. sch->chip.label = dev_name(dev);
  280. sch->chip.parent = dev;
  281. switch (pdev->id) {
  282. case PCI_DEVICE_ID_INTEL_SCH_LPC:
  283. sch->resume_base = 10;
  284. sch->chip.ngpio = 14;
  285. /*
  286. * GPIO[6:0] enabled by default
  287. * GPIO7 is configured by the CMC as SLPIOVR
  288. * Enable GPIO[9:8] core powered gpios explicitly
  289. */
  290. sch_gpio_reg_set(sch, 8, GEN, 1);
  291. sch_gpio_reg_set(sch, 9, GEN, 1);
  292. /*
  293. * SUS_GPIO[2:0] enabled by default
  294. * Enable SUS_GPIO3 resume powered gpio explicitly
  295. */
  296. sch_gpio_reg_set(sch, 13, GEN, 1);
  297. break;
  298. case PCI_DEVICE_ID_INTEL_ITC_LPC:
  299. sch->resume_base = 5;
  300. sch->chip.ngpio = 14;
  301. break;
  302. case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
  303. sch->resume_base = 21;
  304. sch->chip.ngpio = 30;
  305. break;
  306. case PCI_DEVICE_ID_INTEL_QUARK_X1000_ILB:
  307. sch->resume_base = 2;
  308. sch->chip.ngpio = 8;
  309. break;
  310. default:
  311. return -ENODEV;
  312. }
  313. girq = &sch->chip.irq;
  314. gpio_irq_chip_set_chip(girq, &sch_irqchip);
  315. girq->num_parents = 0;
  316. girq->parents = NULL;
  317. girq->parent_handler = NULL;
  318. girq->default_type = IRQ_TYPE_NONE;
  319. girq->handler = handle_bad_irq;
  320. /* GPE setup is optional */
  321. sch->gpe = GPE0E_GPIO;
  322. sch->gpe_handler = sch_gpio_gpe_handler;
  323. ret = sch_gpio_install_gpe_handler(sch);
  324. if (ret)
  325. dev_warn(dev, "Can't setup GPE, no IRQ support\n");
  326. return devm_gpiochip_add_data(dev, &sch->chip, sch);
  327. }
  328. static struct platform_driver sch_gpio_driver = {
  329. .driver = {
  330. .name = "sch_gpio",
  331. },
  332. .probe = sch_gpio_probe,
  333. };
  334. module_platform_driver(sch_gpio_driver);
  335. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  336. MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
  337. MODULE_LICENSE("GPL v2");
  338. MODULE_ALIAS("platform:sch_gpio");