gpio-msic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Intel Medfield MSIC GPIO driver>
  3. * Copyright (c) 2011, Intel Corporation.
  4. *
  5. * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
  6. * Based on intel_pmic_gpio.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/gpio.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mfd/intel_msic.h>
  30. /* the offset for the mapping of global gpio pin to irq */
  31. #define MSIC_GPIO_IRQ_OFFSET 0x100
  32. #define MSIC_GPIO_DIR_IN 0
  33. #define MSIC_GPIO_DIR_OUT BIT(5)
  34. #define MSIC_GPIO_TRIG_FALL BIT(1)
  35. #define MSIC_GPIO_TRIG_RISE BIT(2)
  36. /* masks for msic gpio output GPIOxxxxCTLO registers */
  37. #define MSIC_GPIO_DIR_MASK BIT(5)
  38. #define MSIC_GPIO_DRV_MASK BIT(4)
  39. #define MSIC_GPIO_REN_MASK BIT(3)
  40. #define MSIC_GPIO_RVAL_MASK (BIT(2) | BIT(1))
  41. #define MSIC_GPIO_DOUT_MASK BIT(0)
  42. /* masks for msic gpio input GPIOxxxxCTLI registers */
  43. #define MSIC_GPIO_GLBYP_MASK BIT(5)
  44. #define MSIC_GPIO_DBNC_MASK (BIT(4) | BIT(3))
  45. #define MSIC_GPIO_INTCNT_MASK (BIT(2) | BIT(1))
  46. #define MSIC_GPIO_DIN_MASK BIT(0)
  47. #define MSIC_NUM_GPIO 24
  48. struct msic_gpio {
  49. struct platform_device *pdev;
  50. struct mutex buslock;
  51. struct gpio_chip chip;
  52. int irq;
  53. unsigned irq_base;
  54. unsigned long trig_change_mask;
  55. unsigned trig_type;
  56. };
  57. /*
  58. * MSIC has 24 gpios, 16 low voltage (1.2-1.8v) and 8 high voltage (3v).
  59. * Both the high and low voltage gpios are divided in two banks.
  60. * GPIOs are numbered with GPIO0LV0 as gpio_base in the following order:
  61. * GPIO0LV0..GPIO0LV7: low voltage, bank 0, gpio_base
  62. * GPIO1LV0..GPIO1LV7: low voltage, bank 1, gpio_base + 8
  63. * GPIO0HV0..GPIO0HV3: high voltage, bank 0, gpio_base + 16
  64. * GPIO1HV0..GPIO1HV3: high voltage, bank 1, gpio_base + 20
  65. */
  66. static int msic_gpio_to_ireg(unsigned offset)
  67. {
  68. if (offset >= MSIC_NUM_GPIO)
  69. return -EINVAL;
  70. if (offset < 8)
  71. return INTEL_MSIC_GPIO0LV0CTLI - offset;
  72. if (offset < 16)
  73. return INTEL_MSIC_GPIO1LV0CTLI - offset + 8;
  74. if (offset < 20)
  75. return INTEL_MSIC_GPIO0HV0CTLI - offset + 16;
  76. return INTEL_MSIC_GPIO1HV0CTLI - offset + 20;
  77. }
  78. static int msic_gpio_to_oreg(unsigned offset)
  79. {
  80. if (offset >= MSIC_NUM_GPIO)
  81. return -EINVAL;
  82. if (offset < 8)
  83. return INTEL_MSIC_GPIO0LV0CTLO - offset;
  84. if (offset < 16)
  85. return INTEL_MSIC_GPIO1LV0CTLO - offset + 8;
  86. if (offset < 20)
  87. return INTEL_MSIC_GPIO0HV0CTLO - offset + 16;
  88. return INTEL_MSIC_GPIO1HV0CTLO - offset + 20;
  89. }
  90. static int msic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  91. {
  92. int reg;
  93. reg = msic_gpio_to_oreg(offset);
  94. if (reg < 0)
  95. return reg;
  96. return intel_msic_reg_update(reg, MSIC_GPIO_DIR_IN, MSIC_GPIO_DIR_MASK);
  97. }
  98. static int msic_gpio_direction_output(struct gpio_chip *chip,
  99. unsigned offset, int value)
  100. {
  101. int reg;
  102. unsigned mask;
  103. value = (!!value) | MSIC_GPIO_DIR_OUT;
  104. mask = MSIC_GPIO_DIR_MASK | MSIC_GPIO_DOUT_MASK;
  105. reg = msic_gpio_to_oreg(offset);
  106. if (reg < 0)
  107. return reg;
  108. return intel_msic_reg_update(reg, value, mask);
  109. }
  110. static int msic_gpio_get(struct gpio_chip *chip, unsigned offset)
  111. {
  112. u8 r;
  113. int ret;
  114. int reg;
  115. reg = msic_gpio_to_ireg(offset);
  116. if (reg < 0)
  117. return reg;
  118. ret = intel_msic_reg_read(reg, &r);
  119. if (ret < 0)
  120. return ret;
  121. return r & MSIC_GPIO_DIN_MASK;
  122. }
  123. static void msic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  124. {
  125. int reg;
  126. reg = msic_gpio_to_oreg(offset);
  127. if (reg < 0)
  128. return;
  129. intel_msic_reg_update(reg, !!value , MSIC_GPIO_DOUT_MASK);
  130. }
  131. /*
  132. * This is called from genirq with mg->buslock locked and
  133. * irq_desc->lock held. We can not access the scu bus here, so we
  134. * store the change and update in the bus_sync_unlock() function below
  135. */
  136. static int msic_irq_type(struct irq_data *data, unsigned type)
  137. {
  138. struct msic_gpio *mg = irq_data_get_irq_chip_data(data);
  139. u32 gpio = data->irq - mg->irq_base;
  140. if (gpio >= mg->chip.ngpio)
  141. return -EINVAL;
  142. /* mark for which gpio the trigger changed, protected by buslock */
  143. mg->trig_change_mask |= (1 << gpio);
  144. mg->trig_type = type;
  145. return 0;
  146. }
  147. static int msic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  148. {
  149. struct msic_gpio *mg = container_of(chip, struct msic_gpio, chip);
  150. return mg->irq_base + offset;
  151. }
  152. static void msic_bus_lock(struct irq_data *data)
  153. {
  154. struct msic_gpio *mg = irq_data_get_irq_chip_data(data);
  155. mutex_lock(&mg->buslock);
  156. }
  157. static void msic_bus_sync_unlock(struct irq_data *data)
  158. {
  159. struct msic_gpio *mg = irq_data_get_irq_chip_data(data);
  160. int offset;
  161. int reg;
  162. u8 trig = 0;
  163. /* We can only get one change at a time as the buslock covers the
  164. entire transaction. The irq_desc->lock is dropped before we are
  165. called but that is fine */
  166. if (mg->trig_change_mask) {
  167. offset = __ffs(mg->trig_change_mask);
  168. reg = msic_gpio_to_ireg(offset);
  169. if (reg < 0)
  170. goto out;
  171. if (mg->trig_type & IRQ_TYPE_EDGE_RISING)
  172. trig |= MSIC_GPIO_TRIG_RISE;
  173. if (mg->trig_type & IRQ_TYPE_EDGE_FALLING)
  174. trig |= MSIC_GPIO_TRIG_FALL;
  175. intel_msic_reg_update(reg, trig, MSIC_GPIO_INTCNT_MASK);
  176. mg->trig_change_mask = 0;
  177. }
  178. out:
  179. mutex_unlock(&mg->buslock);
  180. }
  181. /* Firmware does all the masking and unmasking for us, no masking here. */
  182. static void msic_irq_unmask(struct irq_data *data) { }
  183. static void msic_irq_mask(struct irq_data *data) { }
  184. static struct irq_chip msic_irqchip = {
  185. .name = "MSIC-GPIO",
  186. .irq_mask = msic_irq_mask,
  187. .irq_unmask = msic_irq_unmask,
  188. .irq_set_type = msic_irq_type,
  189. .irq_bus_lock = msic_bus_lock,
  190. .irq_bus_sync_unlock = msic_bus_sync_unlock,
  191. };
  192. static void msic_gpio_irq_handler(struct irq_desc *desc)
  193. {
  194. struct irq_data *data = irq_desc_get_irq_data(desc);
  195. struct msic_gpio *mg = irq_data_get_irq_handler_data(data);
  196. struct irq_chip *chip = irq_data_get_irq_chip(data);
  197. struct intel_msic *msic = pdev_to_intel_msic(mg->pdev);
  198. int i;
  199. int bitnr;
  200. u8 pin;
  201. unsigned long pending = 0;
  202. for (i = 0; i < (mg->chip.ngpio / BITS_PER_BYTE); i++) {
  203. intel_msic_irq_read(msic, INTEL_MSIC_GPIO0LVIRQ + i, &pin);
  204. pending = pin;
  205. if (pending) {
  206. for_each_set_bit(bitnr, &pending, BITS_PER_BYTE)
  207. generic_handle_irq(mg->irq_base +
  208. (i * BITS_PER_BYTE) + bitnr);
  209. }
  210. }
  211. chip->irq_eoi(data);
  212. }
  213. static int platform_msic_gpio_probe(struct platform_device *pdev)
  214. {
  215. struct device *dev = &pdev->dev;
  216. struct intel_msic_gpio_pdata *pdata = dev_get_platdata(dev);
  217. struct msic_gpio *mg;
  218. int irq = platform_get_irq(pdev, 0);
  219. int retval;
  220. int i;
  221. if (irq < 0) {
  222. dev_err(dev, "no IRQ line: %d\n", irq);
  223. return irq;
  224. }
  225. if (!pdata || !pdata->gpio_base) {
  226. dev_err(dev, "incorrect or missing platform data\n");
  227. return -EINVAL;
  228. }
  229. mg = kzalloc(sizeof(*mg), GFP_KERNEL);
  230. if (!mg)
  231. return -ENOMEM;
  232. dev_set_drvdata(dev, mg);
  233. mg->pdev = pdev;
  234. mg->irq = irq;
  235. mg->irq_base = pdata->gpio_base + MSIC_GPIO_IRQ_OFFSET;
  236. mg->chip.label = "msic_gpio";
  237. mg->chip.direction_input = msic_gpio_direction_input;
  238. mg->chip.direction_output = msic_gpio_direction_output;
  239. mg->chip.get = msic_gpio_get;
  240. mg->chip.set = msic_gpio_set;
  241. mg->chip.to_irq = msic_gpio_to_irq;
  242. mg->chip.base = pdata->gpio_base;
  243. mg->chip.ngpio = MSIC_NUM_GPIO;
  244. mg->chip.can_sleep = true;
  245. mg->chip.dev = dev;
  246. mutex_init(&mg->buslock);
  247. retval = gpiochip_add(&mg->chip);
  248. if (retval) {
  249. dev_err(dev, "Adding MSIC gpio chip failed\n");
  250. goto err;
  251. }
  252. for (i = 0; i < mg->chip.ngpio; i++) {
  253. irq_set_chip_data(i + mg->irq_base, mg);
  254. irq_set_chip_and_handler(i + mg->irq_base,
  255. &msic_irqchip,
  256. handle_simple_irq);
  257. }
  258. irq_set_chained_handler_and_data(mg->irq, msic_gpio_irq_handler, mg);
  259. return 0;
  260. err:
  261. kfree(mg);
  262. return retval;
  263. }
  264. static struct platform_driver platform_msic_gpio_driver = {
  265. .driver = {
  266. .name = "msic_gpio",
  267. },
  268. .probe = platform_msic_gpio_probe,
  269. };
  270. static int __init platform_msic_gpio_init(void)
  271. {
  272. return platform_driver_register(&platform_msic_gpio_driver);
  273. }
  274. subsys_initcall(platform_msic_gpio_init);
  275. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
  276. MODULE_DESCRIPTION("Intel Medfield MSIC GPIO driver");
  277. MODULE_LICENSE("GPL v2");