intel_pmic_gpio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Moorestown PMIC GPIO (access through IPC) driver
  2. * Copyright (c) 2008 - 2009, Intel Corporation.
  3. *
  4. * Author: Alek Du <alek.du@intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Supports:
  20. * Moorestown platform PMIC chip
  21. */
  22. #define pr_fmt(fmt) "%s: " fmt, __func__
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/stddef.h>
  27. #include <linux/slab.h>
  28. #include <linux/ioport.h>
  29. #include <linux/init.h>
  30. #include <linux/io.h>
  31. #include <linux/gpio/driver.h>
  32. #include <asm/intel_scu_ipc.h>
  33. #include <linux/device.h>
  34. #include <linux/intel_pmic_gpio.h>
  35. #include <linux/platform_device.h>
  36. #define DRIVER_NAME "pmic_gpio"
  37. /* register offset that IPC driver should use
  38. * 8 GPIO + 8 GPOSW (6 controllable) + 8GPO
  39. */
  40. enum pmic_gpio_register {
  41. GPIO0 = 0xE0,
  42. GPIO7 = 0xE7,
  43. GPIOINT = 0xE8,
  44. GPOSWCTL0 = 0xEC,
  45. GPOSWCTL5 = 0xF1,
  46. GPO = 0xF4,
  47. };
  48. /* bits definition for GPIO & GPOSW */
  49. #define GPIO_DRV 0x01
  50. #define GPIO_DIR 0x02
  51. #define GPIO_DIN 0x04
  52. #define GPIO_DOU 0x08
  53. #define GPIO_INTCTL 0x30
  54. #define GPIO_DBC 0xc0
  55. #define GPOSW_DRV 0x01
  56. #define GPOSW_DOU 0x08
  57. #define GPOSW_RDRV 0x30
  58. #define GPIO_UPDATE_TYPE 0x80000000
  59. #define NUM_GPIO 24
  60. struct pmic_gpio {
  61. struct mutex buslock;
  62. struct gpio_chip chip;
  63. void *gpiointr;
  64. int irq;
  65. unsigned irq_base;
  66. unsigned int update_type;
  67. u32 trigger_type;
  68. };
  69. static void pmic_program_irqtype(int gpio, int type)
  70. {
  71. if (type & IRQ_TYPE_EDGE_RISING)
  72. intel_scu_ipc_update_register(GPIO0 + gpio, 0x20, 0x20);
  73. else
  74. intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x20);
  75. if (type & IRQ_TYPE_EDGE_FALLING)
  76. intel_scu_ipc_update_register(GPIO0 + gpio, 0x10, 0x10);
  77. else
  78. intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10);
  79. };
  80. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  81. {
  82. if (offset >= 8) {
  83. pr_err("only pin 0-7 support input\n");
  84. return -1;/* we only have 8 GPIO can use as input */
  85. }
  86. return intel_scu_ipc_update_register(GPIO0 + offset,
  87. GPIO_DIR, GPIO_DIR);
  88. }
  89. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  90. unsigned offset, int value)
  91. {
  92. int rc = 0;
  93. if (offset < 8)/* it is GPIO */
  94. rc = intel_scu_ipc_update_register(GPIO0 + offset,
  95. GPIO_DRV | (value ? GPIO_DOU : 0),
  96. GPIO_DRV | GPIO_DOU | GPIO_DIR);
  97. else if (offset < 16)/* it is GPOSW */
  98. rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  99. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  100. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  101. else if (offset > 15 && offset < 24)/* it is GPO */
  102. rc = intel_scu_ipc_update_register(GPO,
  103. value ? 1 << (offset - 16) : 0,
  104. 1 << (offset - 16));
  105. else {
  106. pr_err("invalid PMIC GPIO pin %d!\n", offset);
  107. WARN_ON(1);
  108. }
  109. return rc;
  110. }
  111. static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset)
  112. {
  113. u8 r;
  114. int ret;
  115. /* we only have 8 GPIO pins we can use as input */
  116. if (offset >= 8)
  117. return -EOPNOTSUPP;
  118. ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r);
  119. if (ret < 0)
  120. return ret;
  121. return r & GPIO_DIN;
  122. }
  123. static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  124. {
  125. if (offset < 8)/* it is GPIO */
  126. intel_scu_ipc_update_register(GPIO0 + offset,
  127. GPIO_DRV | (value ? GPIO_DOU : 0),
  128. GPIO_DRV | GPIO_DOU);
  129. else if (offset < 16)/* it is GPOSW */
  130. intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
  131. GPOSW_DRV | (value ? GPOSW_DOU : 0),
  132. GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
  133. else if (offset > 15 && offset < 24) /* it is GPO */
  134. intel_scu_ipc_update_register(GPO,
  135. value ? 1 << (offset - 16) : 0,
  136. 1 << (offset - 16));
  137. }
  138. /*
  139. * This is called from genirq with pg->buslock locked and
  140. * irq_desc->lock held. We can not access the scu bus here, so we
  141. * store the change and update in the bus_sync_unlock() function below
  142. */
  143. static int pmic_irq_type(struct irq_data *data, unsigned type)
  144. {
  145. struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
  146. u32 gpio = data->irq - pg->irq_base;
  147. if (gpio >= pg->chip.ngpio)
  148. return -EINVAL;
  149. pg->trigger_type = type;
  150. pg->update_type = gpio | GPIO_UPDATE_TYPE;
  151. return 0;
  152. }
  153. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  154. {
  155. struct pmic_gpio *pg = gpiochip_get_data(chip);
  156. return pg->irq_base + offset;
  157. }
  158. static void pmic_bus_lock(struct irq_data *data)
  159. {
  160. struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
  161. mutex_lock(&pg->buslock);
  162. }
  163. static void pmic_bus_sync_unlock(struct irq_data *data)
  164. {
  165. struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
  166. if (pg->update_type) {
  167. unsigned int gpio = pg->update_type & ~GPIO_UPDATE_TYPE;
  168. pmic_program_irqtype(gpio, pg->trigger_type);
  169. pg->update_type = 0;
  170. }
  171. mutex_unlock(&pg->buslock);
  172. }
  173. /* the gpiointr register is read-clear, so just do nothing. */
  174. static void pmic_irq_unmask(struct irq_data *data) { }
  175. static void pmic_irq_mask(struct irq_data *data) { }
  176. static struct irq_chip pmic_irqchip = {
  177. .name = "PMIC-GPIO",
  178. .irq_mask = pmic_irq_mask,
  179. .irq_unmask = pmic_irq_unmask,
  180. .irq_set_type = pmic_irq_type,
  181. .irq_bus_lock = pmic_bus_lock,
  182. .irq_bus_sync_unlock = pmic_bus_sync_unlock,
  183. };
  184. static irqreturn_t pmic_irq_handler(int irq, void *data)
  185. {
  186. struct pmic_gpio *pg = data;
  187. u8 intsts = *((u8 *)pg->gpiointr + 4);
  188. int gpio;
  189. irqreturn_t ret = IRQ_NONE;
  190. for (gpio = 0; gpio < 8; gpio++) {
  191. if (intsts & (1 << gpio)) {
  192. pr_debug("pmic pin %d triggered\n", gpio);
  193. generic_handle_irq(pg->irq_base + gpio);
  194. ret = IRQ_HANDLED;
  195. }
  196. }
  197. return ret;
  198. }
  199. static int platform_pmic_gpio_probe(struct platform_device *pdev)
  200. {
  201. struct device *dev = &pdev->dev;
  202. int irq = platform_get_irq(pdev, 0);
  203. struct intel_pmic_gpio_platform_data *pdata = dev->platform_data;
  204. struct pmic_gpio *pg;
  205. int retval;
  206. int i;
  207. if (irq < 0) {
  208. dev_dbg(dev, "no IRQ line\n");
  209. return -EINVAL;
  210. }
  211. if (!pdata || !pdata->gpio_base || !pdata->irq_base) {
  212. dev_dbg(dev, "incorrect or missing platform data\n");
  213. return -EINVAL;
  214. }
  215. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  216. if (!pg)
  217. return -ENOMEM;
  218. dev_set_drvdata(dev, pg);
  219. pg->irq = irq;
  220. /* setting up SRAM mapping for GPIOINT register */
  221. pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8);
  222. if (!pg->gpiointr) {
  223. pr_err("Can not map GPIOINT\n");
  224. retval = -EINVAL;
  225. goto err2;
  226. }
  227. pg->irq_base = pdata->irq_base;
  228. pg->chip.label = "intel_pmic";
  229. pg->chip.direction_input = pmic_gpio_direction_input;
  230. pg->chip.direction_output = pmic_gpio_direction_output;
  231. pg->chip.get = pmic_gpio_get;
  232. pg->chip.set = pmic_gpio_set;
  233. pg->chip.to_irq = pmic_gpio_to_irq;
  234. pg->chip.base = pdata->gpio_base;
  235. pg->chip.ngpio = NUM_GPIO;
  236. pg->chip.can_sleep = 1;
  237. pg->chip.parent = dev;
  238. mutex_init(&pg->buslock);
  239. pg->chip.parent = dev;
  240. retval = gpiochip_add_data(&pg->chip, pg);
  241. if (retval) {
  242. pr_err("Can not add pmic gpio chip\n");
  243. goto err;
  244. }
  245. retval = request_irq(pg->irq, pmic_irq_handler, 0, "pmic", pg);
  246. if (retval) {
  247. pr_warn("Interrupt request failed\n");
  248. goto fail_request_irq;
  249. }
  250. for (i = 0; i < 8; i++) {
  251. irq_set_chip_and_handler_name(i + pg->irq_base,
  252. &pmic_irqchip,
  253. handle_simple_irq,
  254. "demux");
  255. irq_set_chip_data(i + pg->irq_base, pg);
  256. }
  257. return 0;
  258. fail_request_irq:
  259. gpiochip_remove(&pg->chip);
  260. err:
  261. iounmap(pg->gpiointr);
  262. err2:
  263. kfree(pg);
  264. return retval;
  265. }
  266. /* at the same time, register a platform driver
  267. * this supports the sfi 0.81 fw */
  268. static struct platform_driver platform_pmic_gpio_driver = {
  269. .driver = {
  270. .name = DRIVER_NAME,
  271. },
  272. .probe = platform_pmic_gpio_probe,
  273. };
  274. static int __init platform_pmic_gpio_init(void)
  275. {
  276. return platform_driver_register(&platform_pmic_gpio_driver);
  277. }
  278. subsys_initcall(platform_pmic_gpio_init);