intel_pmic_gpio.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 GPIO_UPDATE_TYPE 0x80000000
  57. #define NUM_GPIO 24
  58. struct pmic_gpio {
  59. struct mutex buslock;
  60. struct gpio_chip chip;
  61. void *gpiointr;
  62. int irq;
  63. unsigned irq_base;
  64. unsigned int update_type;
  65. u32 trigger_type;
  66. };
  67. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  68. {
  69. if (offset > 8) {
  70. printk(KERN_ERR
  71. "%s: only pin 0-7 support input\n", __func__);
  72. return -1;/* we only have 8 GPIO can use as input */
  73. }
  74. return intel_scu_ipc_update_register(GPIO0 + offset,
  75. GPIO_DIR, GPIO_DIR);
  76. }
  77. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  78. unsigned offset, int value)
  79. {
  80. int rc = 0;
  81. if (offset < 8)/* it is GPIO */
  82. rc = intel_scu_ipc_update_register(GPIO0 + offset,
  83. GPIO_DRV | (value ? GPIO_DOU : 0),
  84. GPIO_DRV | GPIO_DOU | GPIO_DIR);
  85. else if (offset < 16)/* it is GPOSW */
  86. rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  87. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  88. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  89. else if (offset > 15 && offset < 24)/* it is GPO */
  90. rc = intel_scu_ipc_update_register(GPO,
  91. value ? 1 << (offset - 16) : 0,
  92. 1 << (offset - 16));
  93. else {
  94. printk(KERN_ERR
  95. "%s: invalid PMIC GPIO pin %d!\n", __func__, offset);
  96. WARN_ON(1);
  97. }
  98. return rc;
  99. }
  100. static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset)
  101. {
  102. u8 r;
  103. int ret;
  104. /* we only have 8 GPIO pins we can use as input */
  105. if (offset > 8)
  106. return -EOPNOTSUPP;
  107. ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r);
  108. if (ret < 0)
  109. return ret;
  110. return r & GPIO_DIN;
  111. }
  112. static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  113. {
  114. if (offset < 8)/* it is GPIO */
  115. intel_scu_ipc_update_register(GPIO0 + offset,
  116. GPIO_DRV | (value ? GPIO_DOU : 0),
  117. GPIO_DRV | GPIO_DOU);
  118. else if (offset < 16)/* it is GPOSW */
  119. intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  120. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  121. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  122. else if (offset > 15 && offset < 24) /* it is GPO */
  123. intel_scu_ipc_update_register(GPO,
  124. value ? 1 << (offset - 16) : 0,
  125. 1 << (offset - 16));
  126. }
  127. /*
  128. * This is called from genirq with pg->buslock locked and
  129. * irq_desc->lock held. We can not access the scu bus here, so we
  130. * store the change and update in the bus_sync_unlock() function below
  131. */
  132. static int pmic_irq_type(struct irq_data *data, unsigned type)
  133. {
  134. struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
  135. u32 gpio = data->irq - pg->irq_base;
  136. if (gpio >= pg->chip.ngpio)
  137. return -EINVAL;
  138. pg->trigger_type = type;
  139. pg->update_type = gpio | GPIO_UPDATE_TYPE;
  140. return 0;
  141. }
  142. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  143. {
  144. struct pmic_gpio *pg = container_of(chip, struct pmic_gpio, chip);
  145. return pg->irq_base + offset;
  146. }
  147. /* the gpiointr register is read-clear, so just do nothing. */
  148. static void pmic_irq_unmask(struct irq_data *data) { }
  149. static void pmic_irq_mask(struct irq_data *data) { }
  150. static struct irq_chip pmic_irqchip = {
  151. .name = "PMIC-GPIO",
  152. .irq_mask = pmic_irq_mask,
  153. .irq_unmask = pmic_irq_unmask,
  154. .irq_set_type = pmic_irq_type,
  155. };
  156. static irqreturn_t pmic_irq_handler(int irq, void *data)
  157. {
  158. struct pmic_gpio *pg = data;
  159. u8 intsts = *((u8 *)pg->gpiointr + 4);
  160. int gpio;
  161. irqreturn_t ret = IRQ_NONE;
  162. for (gpio = 0; gpio < 8; gpio++) {
  163. if (intsts & (1 << gpio)) {
  164. pr_debug("pmic pin %d triggered\n", gpio);
  165. generic_handle_irq(pg->irq_base + gpio);
  166. ret = IRQ_HANDLED;
  167. }
  168. }
  169. return ret;
  170. }
  171. static int __devinit platform_pmic_gpio_probe(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. int irq = platform_get_irq(pdev, 0);
  175. struct intel_pmic_gpio_platform_data *pdata = dev->platform_data;
  176. struct pmic_gpio *pg;
  177. int retval;
  178. int i;
  179. if (irq < 0) {
  180. dev_dbg(dev, "no IRQ line\n");
  181. return -EINVAL;
  182. }
  183. if (!pdata || !pdata->gpio_base || !pdata->irq_base) {
  184. dev_dbg(dev, "incorrect or missing platform data\n");
  185. return -EINVAL;
  186. }
  187. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  188. if (!pg)
  189. return -ENOMEM;
  190. dev_set_drvdata(dev, pg);
  191. pg->irq = irq;
  192. /* setting up SRAM mapping for GPIOINT register */
  193. pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8);
  194. if (!pg->gpiointr) {
  195. printk(KERN_ERR "%s: Can not map GPIOINT.\n", __func__);
  196. retval = -EINVAL;
  197. goto err2;
  198. }
  199. pg->irq_base = pdata->irq_base;
  200. pg->chip.label = "intel_pmic";
  201. pg->chip.direction_input = pmic_gpio_direction_input;
  202. pg->chip.direction_output = pmic_gpio_direction_output;
  203. pg->chip.get = pmic_gpio_get;
  204. pg->chip.set = pmic_gpio_set;
  205. pg->chip.to_irq = pmic_gpio_to_irq;
  206. pg->chip.base = pdata->gpio_base;
  207. pg->chip.ngpio = NUM_GPIO;
  208. pg->chip.can_sleep = 1;
  209. pg->chip.dev = dev;
  210. mutex_init(&pg->buslock);
  211. pg->chip.dev = dev;
  212. retval = gpiochip_add(&pg->chip);
  213. if (retval) {
  214. printk(KERN_ERR "%s: Can not add pmic gpio chip.\n", __func__);
  215. goto err;
  216. }
  217. retval = request_irq(pg->irq, pmic_irq_handler, 0, "pmic", pg);
  218. if (retval) {
  219. printk(KERN_WARNING "pmic: Interrupt request failed\n");
  220. goto err;
  221. }
  222. for (i = 0; i < 8; i++) {
  223. irq_set_chip_and_handler_name(i + pg->irq_base,
  224. &pmic_irqchip,
  225. handle_simple_irq,
  226. "demux");
  227. irq_set_chip_data(i + pg->irq_base, pg);
  228. }
  229. return 0;
  230. err:
  231. iounmap(pg->gpiointr);
  232. err2:
  233. kfree(pg);
  234. return retval;
  235. }
  236. /* at the same time, register a platform driver
  237. * this supports the sfi 0.81 fw */
  238. static struct platform_driver platform_pmic_gpio_driver = {
  239. .driver = {
  240. .name = DRIVER_NAME,
  241. .owner = THIS_MODULE,
  242. },
  243. .probe = platform_pmic_gpio_probe,
  244. };
  245. static int __init platform_pmic_gpio_init(void)
  246. {
  247. return platform_driver_register(&platform_pmic_gpio_driver);
  248. }
  249. subsys_initcall(platform_pmic_gpio_init);
  250. MODULE_AUTHOR("Alek Du <alek.du@intel.com>");
  251. MODULE_DESCRIPTION("Intel Moorestown PMIC GPIO driver");
  252. MODULE_LICENSE("GPL v2");