intel_pmic_gpio.c 8.4 KB

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