intel_pmic_gpio.c 8.1 KB

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