gpio-crystalcove.c 9.4 KB

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