gpio-crystalcove.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * gpio-crystalcove.c - Intel Crystal Cove GPIO Driver
  3. *
  4. * Copyright (C) 2012, 2014 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as 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. * Author: Yang, Bin <bin.yang@intel.com>
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/bitops.h>
  23. #include <linux/regmap.h>
  24. #include <linux/mfd/intel_soc_pmic.h>
  25. #define CRYSTALCOVE_GPIO_NUM 16
  26. #define CRYSTALCOVE_VGPIO_NUM 95
  27. #define UPDATE_IRQ_TYPE BIT(0)
  28. #define UPDATE_IRQ_MASK BIT(1)
  29. #define GPIO0IRQ 0x0b
  30. #define GPIO1IRQ 0x0c
  31. #define MGPIO0IRQS0 0x19
  32. #define MGPIO1IRQS0 0x1a
  33. #define MGPIO0IRQSX 0x1b
  34. #define MGPIO1IRQSX 0x1c
  35. #define GPIO0P0CTLO 0x2b
  36. #define GPIO0P0CTLI 0x33
  37. #define GPIO1P0CTLO 0x3b
  38. #define GPIO1P0CTLI 0x43
  39. #define GPIOPANELCTL 0x52
  40. #define CTLI_INTCNT_DIS (0)
  41. #define CTLI_INTCNT_NE (1 << 1)
  42. #define CTLI_INTCNT_PE (2 << 1)
  43. #define CTLI_INTCNT_BE (3 << 1)
  44. #define CTLO_DIR_IN (0)
  45. #define CTLO_DIR_OUT (1 << 5)
  46. #define CTLO_DRV_CMOS (0)
  47. #define CTLO_DRV_OD (1 << 4)
  48. #define CTLO_DRV_REN (1 << 3)
  49. #define CTLO_RVAL_2KDW (0)
  50. #define CTLO_RVAL_2KUP (1 << 1)
  51. #define CTLO_RVAL_50KDW (2 << 1)
  52. #define CTLO_RVAL_50KUP (3 << 1)
  53. #define CTLO_INPUT_SET (CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
  54. #define CTLO_OUTPUT_SET (CTLO_DIR_OUT | CTLO_INPUT_SET)
  55. enum ctrl_register {
  56. CTRL_IN,
  57. CTRL_OUT,
  58. };
  59. /**
  60. * struct crystalcove_gpio - Crystal Cove GPIO controller
  61. * @buslock: for bus lock/sync and unlock.
  62. * @chip: the abstract gpio_chip structure.
  63. * @regmap: the regmap from the parent device.
  64. * @update: pending IRQ setting update, to be written to the chip upon unlock.
  65. * @intcnt_value: the Interrupt Detect value to be written.
  66. * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
  67. */
  68. struct crystalcove_gpio {
  69. struct mutex buslock; /* irq_bus_lock */
  70. struct gpio_chip chip;
  71. struct regmap *regmap;
  72. int update;
  73. int intcnt_value;
  74. bool set_irq_mask;
  75. };
  76. static inline int to_reg(int gpio, enum ctrl_register reg_type)
  77. {
  78. int reg;
  79. if (gpio == 94)
  80. return GPIOPANELCTL;
  81. if (reg_type == CTRL_IN) {
  82. if (gpio < 8)
  83. reg = GPIO0P0CTLI;
  84. else
  85. reg = GPIO1P0CTLI;
  86. } else {
  87. if (gpio < 8)
  88. reg = GPIO0P0CTLO;
  89. else
  90. reg = GPIO1P0CTLO;
  91. }
  92. return reg + gpio % 8;
  93. }
  94. static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
  95. int gpio)
  96. {
  97. u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
  98. int mask = BIT(gpio % 8);
  99. if (cg->set_irq_mask)
  100. regmap_update_bits(cg->regmap, mirqs0, mask, mask);
  101. else
  102. regmap_update_bits(cg->regmap, mirqs0, mask, 0);
  103. }
  104. static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
  105. {
  106. int reg = to_reg(gpio, CTRL_IN);
  107. regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
  108. }
  109. static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
  110. {
  111. struct crystalcove_gpio *cg = gpiochip_get_data(chip);
  112. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  113. return 0;
  114. return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
  115. CTLO_INPUT_SET);
  116. }
  117. static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
  118. int value)
  119. {
  120. struct crystalcove_gpio *cg = gpiochip_get_data(chip);
  121. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  122. return 0;
  123. return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
  124. CTLO_OUTPUT_SET | value);
  125. }
  126. static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
  127. {
  128. struct crystalcove_gpio *cg = gpiochip_get_data(chip);
  129. int ret;
  130. unsigned int val;
  131. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  132. return 0;
  133. ret = regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &val);
  134. if (ret)
  135. return ret;
  136. return val & 0x1;
  137. }
  138. static void crystalcove_gpio_set(struct gpio_chip *chip,
  139. unsigned gpio, int value)
  140. {
  141. struct crystalcove_gpio *cg = gpiochip_get_data(chip);
  142. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  143. return;
  144. if (value)
  145. regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 1);
  146. else
  147. regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 0);
  148. }
  149. static int crystalcove_irq_type(struct irq_data *data, unsigned type)
  150. {
  151. struct crystalcove_gpio *cg =
  152. gpiochip_get_data(irq_data_get_irq_chip_data(data));
  153. switch (type) {
  154. case IRQ_TYPE_NONE:
  155. cg->intcnt_value = CTLI_INTCNT_DIS;
  156. break;
  157. case IRQ_TYPE_EDGE_BOTH:
  158. cg->intcnt_value = CTLI_INTCNT_BE;
  159. break;
  160. case IRQ_TYPE_EDGE_RISING:
  161. cg->intcnt_value = CTLI_INTCNT_PE;
  162. break;
  163. case IRQ_TYPE_EDGE_FALLING:
  164. cg->intcnt_value = CTLI_INTCNT_NE;
  165. break;
  166. default:
  167. return -EINVAL;
  168. }
  169. cg->update |= UPDATE_IRQ_TYPE;
  170. return 0;
  171. }
  172. static void crystalcove_bus_lock(struct irq_data *data)
  173. {
  174. struct crystalcove_gpio *cg =
  175. gpiochip_get_data(irq_data_get_irq_chip_data(data));
  176. mutex_lock(&cg->buslock);
  177. }
  178. static void crystalcove_bus_sync_unlock(struct irq_data *data)
  179. {
  180. struct crystalcove_gpio *cg =
  181. gpiochip_get_data(irq_data_get_irq_chip_data(data));
  182. int gpio = data->hwirq;
  183. if (cg->update & UPDATE_IRQ_TYPE)
  184. crystalcove_update_irq_ctrl(cg, gpio);
  185. if (cg->update & UPDATE_IRQ_MASK)
  186. crystalcove_update_irq_mask(cg, gpio);
  187. cg->update = 0;
  188. mutex_unlock(&cg->buslock);
  189. }
  190. static void crystalcove_irq_unmask(struct irq_data *data)
  191. {
  192. struct crystalcove_gpio *cg =
  193. gpiochip_get_data(irq_data_get_irq_chip_data(data));
  194. cg->set_irq_mask = false;
  195. cg->update |= UPDATE_IRQ_MASK;
  196. }
  197. static void crystalcove_irq_mask(struct irq_data *data)
  198. {
  199. struct crystalcove_gpio *cg =
  200. gpiochip_get_data(irq_data_get_irq_chip_data(data));
  201. cg->set_irq_mask = true;
  202. cg->update |= UPDATE_IRQ_MASK;
  203. }
  204. static struct irq_chip crystalcove_irqchip = {
  205. .name = "Crystal Cove",
  206. .irq_mask = crystalcove_irq_mask,
  207. .irq_unmask = crystalcove_irq_unmask,
  208. .irq_set_type = crystalcove_irq_type,
  209. .irq_bus_lock = crystalcove_bus_lock,
  210. .irq_bus_sync_unlock = crystalcove_bus_sync_unlock,
  211. .flags = IRQCHIP_SKIP_SET_WAKE,
  212. };
  213. static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
  214. {
  215. struct crystalcove_gpio *cg = data;
  216. unsigned int p0, p1;
  217. int pending;
  218. int gpio;
  219. unsigned int virq;
  220. if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
  221. regmap_read(cg->regmap, GPIO1IRQ, &p1))
  222. return IRQ_NONE;
  223. regmap_write(cg->regmap, GPIO0IRQ, p0);
  224. regmap_write(cg->regmap, GPIO1IRQ, p1);
  225. pending = p0 | p1 << 8;
  226. for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
  227. if (pending & BIT(gpio)) {
  228. virq = irq_find_mapping(cg->chip.irqdomain, gpio);
  229. handle_nested_irq(virq);
  230. }
  231. }
  232. return IRQ_HANDLED;
  233. }
  234. static void crystalcove_gpio_dbg_show(struct seq_file *s,
  235. struct gpio_chip *chip)
  236. {
  237. struct crystalcove_gpio *cg = gpiochip_get_data(chip);
  238. int gpio, offset;
  239. unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
  240. for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
  241. regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
  242. regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
  243. regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
  244. &mirqs0);
  245. regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
  246. &mirqsx);
  247. regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
  248. &irq);
  249. offset = gpio % 8;
  250. seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s\n",
  251. gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
  252. ctli & 0x1 ? "hi" : "lo",
  253. ctli & CTLI_INTCNT_NE ? "fall" : " ",
  254. ctli & CTLI_INTCNT_PE ? "rise" : " ",
  255. ctlo,
  256. mirqs0 & BIT(offset) ? "s0 mask " : "s0 unmask",
  257. mirqsx & BIT(offset) ? "sx mask " : "sx unmask",
  258. irq & BIT(offset) ? "pending" : " ");
  259. }
  260. }
  261. static int crystalcove_gpio_probe(struct platform_device *pdev)
  262. {
  263. int irq = platform_get_irq(pdev, 0);
  264. struct crystalcove_gpio *cg;
  265. int retval;
  266. struct device *dev = pdev->dev.parent;
  267. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  268. if (irq < 0)
  269. return irq;
  270. cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
  271. if (!cg)
  272. return -ENOMEM;
  273. platform_set_drvdata(pdev, cg);
  274. mutex_init(&cg->buslock);
  275. cg->chip.label = KBUILD_MODNAME;
  276. cg->chip.direction_input = crystalcove_gpio_dir_in;
  277. cg->chip.direction_output = crystalcove_gpio_dir_out;
  278. cg->chip.get = crystalcove_gpio_get;
  279. cg->chip.set = crystalcove_gpio_set;
  280. cg->chip.base = -1;
  281. cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
  282. cg->chip.can_sleep = true;
  283. cg->chip.parent = dev;
  284. cg->chip.dbg_show = crystalcove_gpio_dbg_show;
  285. cg->regmap = pmic->regmap;
  286. retval = devm_gpiochip_add_data(&pdev->dev, &cg->chip, cg);
  287. if (retval) {
  288. dev_warn(&pdev->dev, "add gpio chip error: %d\n", retval);
  289. return retval;
  290. }
  291. gpiochip_irqchip_add(&cg->chip, &crystalcove_irqchip, 0,
  292. handle_simple_irq, IRQ_TYPE_NONE);
  293. retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
  294. IRQF_ONESHOT, KBUILD_MODNAME, cg);
  295. if (retval) {
  296. dev_warn(&pdev->dev, "request irq failed: %d\n", retval);
  297. return retval;
  298. }
  299. return 0;
  300. }
  301. static int crystalcove_gpio_remove(struct platform_device *pdev)
  302. {
  303. struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
  304. int irq = platform_get_irq(pdev, 0);
  305. if (irq >= 0)
  306. free_irq(irq, cg);
  307. return 0;
  308. }
  309. static struct platform_driver crystalcove_gpio_driver = {
  310. .probe = crystalcove_gpio_probe,
  311. .remove = crystalcove_gpio_remove,
  312. .driver = {
  313. .name = "crystal_cove_gpio",
  314. },
  315. };
  316. module_platform_driver(crystalcove_gpio_driver);
  317. MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
  318. MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
  319. MODULE_LICENSE("GPL v2");