intel_pmic_gpio.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Moorestown PMIC GPIO (access through IPC) 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 PMIC chip
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/stddef.h>
  25. #include <linux/slab.h>
  26. #include <linux/ioport.h>
  27. #include <linux/init.h>
  28. #include <linux/io.h>
  29. #include <linux/gpio.h>
  30. #include <asm/intel_scu_ipc.h>
  31. #include <linux/device.h>
  32. #include <linux/intel_pmic_gpio.h>
  33. #include <linux/platform_device.h>
  34. #define DRIVER_NAME "pmic_gpio"
  35. /* register offset that IPC driver should use
  36. * 8 GPIO + 8 GPOSW (6 controllable) + 8GPO
  37. */
  38. enum pmic_gpio_register {
  39. GPIO0 = 0xE0,
  40. GPIO7 = 0xE7,
  41. GPIOINT = 0xE8,
  42. GPOSWCTL0 = 0xEC,
  43. GPOSWCTL5 = 0xF1,
  44. GPO = 0xF4,
  45. };
  46. /* bits definition for GPIO & GPOSW */
  47. #define GPIO_DRV 0x01
  48. #define GPIO_DIR 0x02
  49. #define GPIO_DIN 0x04
  50. #define GPIO_DOU 0x08
  51. #define GPIO_INTCTL 0x30
  52. #define GPIO_DBC 0xc0
  53. #define GPOSW_DRV 0x01
  54. #define GPOSW_DOU 0x08
  55. #define GPOSW_RDRV 0x30
  56. #define NUM_GPIO 24
  57. struct pmic_gpio_irq {
  58. spinlock_t lock;
  59. u32 trigger[NUM_GPIO];
  60. u32 dirty;
  61. struct work_struct work;
  62. };
  63. struct pmic_gpio {
  64. struct gpio_chip chip;
  65. struct pmic_gpio_irq irqtypes;
  66. void *gpiointr;
  67. int irq;
  68. unsigned irq_base;
  69. };
  70. static void pmic_program_irqtype(int gpio, int type)
  71. {
  72. if (type & IRQ_TYPE_EDGE_RISING)
  73. intel_scu_ipc_update_register(GPIO0 + gpio, 0x20, 0x20);
  74. else
  75. intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x20);
  76. if (type & IRQ_TYPE_EDGE_FALLING)
  77. intel_scu_ipc_update_register(GPIO0 + gpio, 0x10, 0x10);
  78. else
  79. intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10);
  80. };
  81. static void pmic_irqtype_work(struct work_struct *work)
  82. {
  83. struct pmic_gpio_irq *t =
  84. container_of(work, struct pmic_gpio_irq, work);
  85. unsigned long flags;
  86. int i;
  87. u16 type;
  88. spin_lock_irqsave(&t->lock, flags);
  89. /* As we drop the lock, we may need multiple scans if we race the
  90. pmic_irq_type function */
  91. while (t->dirty) {
  92. /*
  93. * For each pin that has the dirty bit set send an IPC
  94. * message to configure the hardware via the PMIC
  95. */
  96. for (i = 0; i < NUM_GPIO; i++) {
  97. if (!(t->dirty & (1 << i)))
  98. continue;
  99. t->dirty &= ~(1 << i);
  100. /* We can't trust the array entry or dirty
  101. once the lock is dropped */
  102. type = t->trigger[i];
  103. spin_unlock_irqrestore(&t->lock, flags);
  104. pmic_program_irqtype(i, type);
  105. spin_lock_irqsave(&t->lock, flags);
  106. }
  107. }
  108. spin_unlock_irqrestore(&t->lock, flags);
  109. }
  110. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  111. {
  112. if (offset > 8) {
  113. printk(KERN_ERR
  114. "%s: only pin 0-7 support input\n", __func__);
  115. return -1;/* we only have 8 GPIO can use as input */
  116. }
  117. return intel_scu_ipc_update_register(GPIO0 + offset,
  118. GPIO_DIR, GPIO_DIR);
  119. }
  120. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  121. unsigned offset, int value)
  122. {
  123. int rc = 0;
  124. if (offset < 8)/* it is GPIO */
  125. rc = intel_scu_ipc_update_register(GPIO0 + offset,
  126. GPIO_DRV | (value ? GPIO_DOU : 0),
  127. GPIO_DRV | GPIO_DOU | GPIO_DIR);
  128. else if (offset < 16)/* it is GPOSW */
  129. rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  130. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  131. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  132. else if (offset > 15 && offset < 24)/* it is GPO */
  133. rc = intel_scu_ipc_update_register(GPO,
  134. value ? 1 << (offset - 16) : 0,
  135. 1 << (offset - 16));
  136. else {
  137. printk(KERN_ERR
  138. "%s: invalid PMIC GPIO pin %d!\n", __func__, offset);
  139. WARN_ON(1);
  140. }
  141. return rc;
  142. }
  143. static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset)
  144. {
  145. u8 r;
  146. int ret;
  147. /* we only have 8 GPIO pins we can use as input */
  148. if (offset > 8)
  149. return -EOPNOTSUPP;
  150. ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r);
  151. if (ret < 0)
  152. return ret;
  153. return r & GPIO_DIN;
  154. }
  155. static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  156. {
  157. if (offset < 8)/* it is GPIO */
  158. intel_scu_ipc_update_register(GPIO0 + offset,
  159. GPIO_DRV | (value ? GPIO_DOU : 0),
  160. GPIO_DRV | GPIO_DOU);
  161. else if (offset < 16)/* it is GPOSW */
  162. intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  163. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  164. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  165. else if (offset > 15 && offset < 24) /* it is GPO */
  166. intel_scu_ipc_update_register(GPO,
  167. value ? 1 << (offset - 16) : 0,
  168. 1 << (offset - 16));
  169. }
  170. static int pmic_irq_type(unsigned irq, unsigned type)
  171. {
  172. struct pmic_gpio *pg = get_irq_chip_data(irq);
  173. u32 gpio = irq - pg->irq_base;
  174. unsigned long flags;
  175. if (gpio >= pg->chip.ngpio)
  176. return -EINVAL;
  177. spin_lock_irqsave(&pg->irqtypes.lock, flags);
  178. pg->irqtypes.trigger[gpio] = type;
  179. pg->irqtypes.dirty |= (1 << gpio);
  180. spin_unlock_irqrestore(&pg->irqtypes.lock, flags);
  181. schedule_work(&pg->irqtypes.work);
  182. return 0;
  183. }
  184. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  185. {
  186. struct pmic_gpio *pg = container_of(chip, struct pmic_gpio, chip);
  187. return pg->irq_base + offset;
  188. }
  189. /* the gpiointr register is read-clear, so just do nothing. */
  190. static void pmic_irq_unmask(unsigned irq)
  191. {
  192. };
  193. static void pmic_irq_mask(unsigned irq)
  194. {
  195. };
  196. static struct irq_chip pmic_irqchip = {
  197. .name = "PMIC-GPIO",
  198. .mask = pmic_irq_mask,
  199. .unmask = pmic_irq_unmask,
  200. .set_type = pmic_irq_type,
  201. };
  202. static void pmic_irq_handler(unsigned irq, struct irq_desc *desc)
  203. {
  204. struct pmic_gpio *pg = (struct pmic_gpio *)get_irq_data(irq);
  205. u8 intsts = *((u8 *)pg->gpiointr + 4);
  206. int gpio;
  207. for (gpio = 0; gpio < 8; gpio++) {
  208. if (intsts & (1 << gpio)) {
  209. pr_debug("pmic pin %d triggered\n", gpio);
  210. generic_handle_irq(pg->irq_base + gpio);
  211. }
  212. }
  213. if (desc->chip->irq_eoi)
  214. desc->chip->irq_eoi(irq_get_irq_data(irq));
  215. else
  216. dev_warn(pg->chip.dev, "missing EOI handler for irq %d\n", irq);
  217. }
  218. static int __devinit platform_pmic_gpio_probe(struct platform_device *pdev)
  219. {
  220. struct device *dev = &pdev->dev;
  221. int irq = platform_get_irq(pdev, 0);
  222. struct intel_pmic_gpio_platform_data *pdata = dev->platform_data;
  223. struct pmic_gpio *pg;
  224. int retval;
  225. int i;
  226. if (irq < 0) {
  227. dev_dbg(dev, "no IRQ line\n");
  228. return -EINVAL;
  229. }
  230. if (!pdata || !pdata->gpio_base || !pdata->irq_base) {
  231. dev_dbg(dev, "incorrect or missing platform data\n");
  232. return -EINVAL;
  233. }
  234. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  235. if (!pg)
  236. return -ENOMEM;
  237. dev_set_drvdata(dev, pg);
  238. pg->irq = irq;
  239. /* setting up SRAM mapping for GPIOINT register */
  240. pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8);
  241. if (!pg->gpiointr) {
  242. printk(KERN_ERR "%s: Can not map GPIOINT.\n", __func__);
  243. retval = -EINVAL;
  244. goto err2;
  245. }
  246. pg->irq_base = pdata->irq_base;
  247. pg->chip.label = "intel_pmic";
  248. pg->chip.direction_input = pmic_gpio_direction_input;
  249. pg->chip.direction_output = pmic_gpio_direction_output;
  250. pg->chip.get = pmic_gpio_get;
  251. pg->chip.set = pmic_gpio_set;
  252. pg->chip.to_irq = pmic_gpio_to_irq;
  253. pg->chip.base = pdata->gpio_base;
  254. pg->chip.ngpio = NUM_GPIO;
  255. pg->chip.can_sleep = 1;
  256. pg->chip.dev = dev;
  257. INIT_WORK(&pg->irqtypes.work, pmic_irqtype_work);
  258. spin_lock_init(&pg->irqtypes.lock);
  259. pg->chip.dev = dev;
  260. retval = gpiochip_add(&pg->chip);
  261. if (retval) {
  262. printk(KERN_ERR "%s: Can not add pmic gpio chip.\n", __func__);
  263. goto err;
  264. }
  265. set_irq_data(pg->irq, pg);
  266. set_irq_chained_handler(pg->irq, pmic_irq_handler);
  267. for (i = 0; i < 8; i++) {
  268. set_irq_chip_and_handler_name(i + pg->irq_base, &pmic_irqchip,
  269. handle_simple_irq, "demux");
  270. set_irq_chip_data(i + pg->irq_base, pg);
  271. }
  272. return 0;
  273. err:
  274. iounmap(pg->gpiointr);
  275. err2:
  276. kfree(pg);
  277. return retval;
  278. }
  279. /* at the same time, register a platform driver
  280. * this supports the sfi 0.81 fw */
  281. static struct platform_driver platform_pmic_gpio_driver = {
  282. .driver = {
  283. .name = DRIVER_NAME,
  284. .owner = THIS_MODULE,
  285. },
  286. .probe = platform_pmic_gpio_probe,
  287. };
  288. static int __init platform_pmic_gpio_init(void)
  289. {
  290. return platform_driver_register(&platform_pmic_gpio_driver);
  291. }
  292. subsys_initcall(platform_pmic_gpio_init);
  293. MODULE_AUTHOR("Alek Du <alek.du@intel.com>");
  294. MODULE_DESCRIPTION("Intel Moorestown PMIC GPIO driver");
  295. MODULE_LICENSE("GPL v2");