gpio-crystalcove.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 struct crystalcove_gpio *to_cg(struct gpio_chip *gc)
  77. {
  78. return container_of(gc, struct crystalcove_gpio, chip);
  79. }
  80. static inline int to_reg(int gpio, enum ctrl_register reg_type)
  81. {
  82. int reg;
  83. if (gpio == 94)
  84. return GPIOPANELCTL;
  85. if (reg_type == CTRL_IN) {
  86. if (gpio < 8)
  87. reg = GPIO0P0CTLI;
  88. else
  89. reg = GPIO1P0CTLI;
  90. } else {
  91. if (gpio < 8)
  92. reg = GPIO0P0CTLO;
  93. else
  94. reg = GPIO1P0CTLO;
  95. }
  96. return reg + gpio % 8;
  97. }
  98. static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
  99. int gpio)
  100. {
  101. u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
  102. int mask = BIT(gpio % 8);
  103. if (cg->set_irq_mask)
  104. regmap_update_bits(cg->regmap, mirqs0, mask, mask);
  105. else
  106. regmap_update_bits(cg->regmap, mirqs0, mask, 0);
  107. }
  108. static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
  109. {
  110. int reg = to_reg(gpio, CTRL_IN);
  111. regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
  112. }
  113. static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
  114. {
  115. struct crystalcove_gpio *cg = to_cg(chip);
  116. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  117. return 0;
  118. return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
  119. CTLO_INPUT_SET);
  120. }
  121. static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
  122. int value)
  123. {
  124. struct crystalcove_gpio *cg = to_cg(chip);
  125. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  126. return 0;
  127. return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
  128. CTLO_OUTPUT_SET | value);
  129. }
  130. static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
  131. {
  132. struct crystalcove_gpio *cg = to_cg(chip);
  133. int ret;
  134. unsigned int val;
  135. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  136. return 0;
  137. ret = regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &val);
  138. if (ret)
  139. return ret;
  140. return val & 0x1;
  141. }
  142. static void crystalcove_gpio_set(struct gpio_chip *chip,
  143. unsigned gpio, int value)
  144. {
  145. struct crystalcove_gpio *cg = to_cg(chip);
  146. if (gpio > CRYSTALCOVE_VGPIO_NUM)
  147. return;
  148. if (value)
  149. regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 1);
  150. else
  151. regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 0);
  152. }
  153. static int crystalcove_irq_type(struct irq_data *data, unsigned type)
  154. {
  155. struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
  156. switch (type) {
  157. case IRQ_TYPE_NONE:
  158. cg->intcnt_value = CTLI_INTCNT_DIS;
  159. break;
  160. case IRQ_TYPE_EDGE_BOTH:
  161. cg->intcnt_value = CTLI_INTCNT_BE;
  162. break;
  163. case IRQ_TYPE_EDGE_RISING:
  164. cg->intcnt_value = CTLI_INTCNT_PE;
  165. break;
  166. case IRQ_TYPE_EDGE_FALLING:
  167. cg->intcnt_value = CTLI_INTCNT_NE;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. cg->update |= UPDATE_IRQ_TYPE;
  173. return 0;
  174. }
  175. static void crystalcove_bus_lock(struct irq_data *data)
  176. {
  177. struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
  178. mutex_lock(&cg->buslock);
  179. }
  180. static void crystalcove_bus_sync_unlock(struct irq_data *data)
  181. {
  182. struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
  183. int gpio = data->hwirq;
  184. if (cg->update & UPDATE_IRQ_TYPE)
  185. crystalcove_update_irq_ctrl(cg, gpio);
  186. if (cg->update & UPDATE_IRQ_MASK)
  187. crystalcove_update_irq_mask(cg, gpio);
  188. cg->update = 0;
  189. mutex_unlock(&cg->buslock);
  190. }
  191. static void crystalcove_irq_unmask(struct irq_data *data)
  192. {
  193. struct crystalcove_gpio *cg = to_cg(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 = to_cg(irq_data_get_irq_chip_data(data));
  200. cg->set_irq_mask = true;
  201. cg->update |= UPDATE_IRQ_MASK;
  202. }
  203. static struct irq_chip crystalcove_irqchip = {
  204. .name = "Crystal Cove",
  205. .irq_mask = crystalcove_irq_mask,
  206. .irq_unmask = crystalcove_irq_unmask,
  207. .irq_set_type = crystalcove_irq_type,
  208. .irq_bus_lock = crystalcove_bus_lock,
  209. .irq_bus_sync_unlock = crystalcove_bus_sync_unlock,
  210. .flags = IRQCHIP_SKIP_SET_WAKE,
  211. };
  212. static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
  213. {
  214. struct crystalcove_gpio *cg = data;
  215. unsigned int p0, p1;
  216. int pending;
  217. int gpio;
  218. unsigned int virq;
  219. if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
  220. regmap_read(cg->regmap, GPIO1IRQ, &p1))
  221. return IRQ_NONE;
  222. regmap_write(cg->regmap, GPIO0IRQ, p0);
  223. regmap_write(cg->regmap, GPIO1IRQ, p1);
  224. pending = p0 | p1 << 8;
  225. for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
  226. if (pending & BIT(gpio)) {
  227. virq = irq_find_mapping(cg->chip.irqdomain, gpio);
  228. handle_nested_irq(virq);
  229. }
  230. }
  231. return IRQ_HANDLED;
  232. }
  233. static void crystalcove_gpio_dbg_show(struct seq_file *s,
  234. struct gpio_chip *chip)
  235. {
  236. struct crystalcove_gpio *cg = to_cg(chip);
  237. int gpio, offset;
  238. unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
  239. for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
  240. regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
  241. regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
  242. regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
  243. &mirqs0);
  244. regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
  245. &mirqsx);
  246. regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
  247. &irq);
  248. offset = gpio % 8;
  249. seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s\n",
  250. gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
  251. ctli & 0x1 ? "hi" : "lo",
  252. ctli & CTLI_INTCNT_NE ? "fall" : " ",
  253. ctli & CTLI_INTCNT_PE ? "rise" : " ",
  254. ctlo,
  255. mirqs0 & BIT(offset) ? "s0 mask " : "s0 unmask",
  256. mirqsx & BIT(offset) ? "sx mask " : "sx unmask",
  257. irq & BIT(offset) ? "pending" : " ");
  258. }
  259. }
  260. static int crystalcove_gpio_probe(struct platform_device *pdev)
  261. {
  262. int irq = platform_get_irq(pdev, 0);
  263. struct crystalcove_gpio *cg;
  264. int retval;
  265. struct device *dev = pdev->dev.parent;
  266. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  267. if (irq < 0)
  268. return irq;
  269. cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
  270. if (!cg)
  271. return -ENOMEM;
  272. platform_set_drvdata(pdev, cg);
  273. mutex_init(&cg->buslock);
  274. cg->chip.label = KBUILD_MODNAME;
  275. cg->chip.direction_input = crystalcove_gpio_dir_in;
  276. cg->chip.direction_output = crystalcove_gpio_dir_out;
  277. cg->chip.get = crystalcove_gpio_get;
  278. cg->chip.set = crystalcove_gpio_set;
  279. cg->chip.base = -1;
  280. cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
  281. cg->chip.can_sleep = true;
  282. cg->chip.dev = dev;
  283. cg->chip.dbg_show = crystalcove_gpio_dbg_show;
  284. cg->regmap = pmic->regmap;
  285. retval = gpiochip_add(&cg->chip);
  286. if (retval) {
  287. dev_warn(&pdev->dev, "add gpio chip error: %d\n", retval);
  288. return retval;
  289. }
  290. gpiochip_irqchip_add(&cg->chip, &crystalcove_irqchip, 0,
  291. handle_simple_irq, IRQ_TYPE_NONE);
  292. retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
  293. IRQF_ONESHOT, KBUILD_MODNAME, cg);
  294. if (retval) {
  295. dev_warn(&pdev->dev, "request irq failed: %d\n", retval);
  296. goto out_remove_gpio;
  297. }
  298. return 0;
  299. out_remove_gpio:
  300. gpiochip_remove(&cg->chip);
  301. return retval;
  302. }
  303. static int crystalcove_gpio_remove(struct platform_device *pdev)
  304. {
  305. struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
  306. int irq = platform_get_irq(pdev, 0);
  307. gpiochip_remove(&cg->chip);
  308. if (irq >= 0)
  309. free_irq(irq, cg);
  310. return 0;
  311. }
  312. static struct platform_driver crystalcove_gpio_driver = {
  313. .probe = crystalcove_gpio_probe,
  314. .remove = crystalcove_gpio_remove,
  315. .driver = {
  316. .name = "crystal_cove_gpio",
  317. },
  318. };
  319. module_platform_driver(crystalcove_gpio_driver);
  320. MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
  321. MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
  322. MODULE_LICENSE("GPL v2");