gpio-wcove.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Intel Whiskey Cove PMIC GPIO Driver
  3. *
  4. * This driver is written based on gpio-crystalcove.c
  5. *
  6. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/mfd/intel_soc_pmic.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/seq_file.h>
  25. /*
  26. * Whiskey Cove PMIC has 13 physical GPIO pins divided into 3 banks:
  27. * Bank 0: Pin 0 - 6
  28. * Bank 1: Pin 7 - 10
  29. * Bank 2: Pin 11 -12
  30. * Each pin has one output control register and one input control register.
  31. */
  32. #define BANK0_NR_PINS 7
  33. #define BANK1_NR_PINS 4
  34. #define BANK2_NR_PINS 2
  35. #define WCOVE_GPIO_NUM (BANK0_NR_PINS + BANK1_NR_PINS + BANK2_NR_PINS)
  36. #define WCOVE_VGPIO_NUM 94
  37. /* GPIO output control registers (one per pin): 0x4e44 - 0x4e50 */
  38. #define GPIO_OUT_CTRL_BASE 0x4e44
  39. /* GPIO input control registers (one per pin): 0x4e51 - 0x4e5d */
  40. #define GPIO_IN_CTRL_BASE 0x4e51
  41. /*
  42. * GPIO interrupts are organized in two groups:
  43. * Group 0: Bank 0 pins (Pin 0 - 6)
  44. * Group 1: Bank 1 and Bank 2 pins (Pin 7 - 12)
  45. * Each group has two registers (one bit per pin): status and mask.
  46. */
  47. #define GROUP0_NR_IRQS 7
  48. #define GROUP1_NR_IRQS 6
  49. #define IRQ_MASK_BASE 0x4e19
  50. #define IRQ_STATUS_BASE 0x4e0b
  51. #define UPDATE_IRQ_TYPE BIT(0)
  52. #define UPDATE_IRQ_MASK BIT(1)
  53. #define CTLI_INTCNT_DIS (0 << 1)
  54. #define CTLI_INTCNT_NE (1 << 1)
  55. #define CTLI_INTCNT_PE (2 << 1)
  56. #define CTLI_INTCNT_BE (3 << 1)
  57. #define CTLO_DIR_IN (0 << 5)
  58. #define CTLO_DIR_OUT (1 << 5)
  59. #define CTLO_DRV_MASK (1 << 4)
  60. #define CTLO_DRV_OD (0 << 4)
  61. #define CTLO_DRV_CMOS (1 << 4)
  62. #define CTLO_DRV_REN (1 << 3)
  63. #define CTLO_RVAL_2KDOWN (0 << 1)
  64. #define CTLO_RVAL_2KUP (1 << 1)
  65. #define CTLO_RVAL_50KDOWN (2 << 1)
  66. #define CTLO_RVAL_50KUP (3 << 1)
  67. #define CTLO_INPUT_SET (CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
  68. #define CTLO_OUTPUT_SET (CTLO_DIR_OUT | CTLO_INPUT_SET)
  69. enum ctrl_register {
  70. CTRL_IN,
  71. CTRL_OUT,
  72. };
  73. /*
  74. * struct wcove_gpio - Whiskey Cove GPIO controller
  75. * @buslock: for bus lock/sync and unlock.
  76. * @chip: the abstract gpio_chip structure.
  77. * @dev: the gpio device
  78. * @regmap: the regmap from the parent device.
  79. * @regmap_irq_chip: the regmap of the gpio irq chip.
  80. * @update: pending IRQ setting update, to be written to the chip upon unlock.
  81. * @intcnt: the Interrupt Detect value to be written.
  82. * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
  83. */
  84. struct wcove_gpio {
  85. struct mutex buslock;
  86. struct gpio_chip chip;
  87. struct device *dev;
  88. struct regmap *regmap;
  89. struct regmap_irq_chip_data *regmap_irq_chip;
  90. int update;
  91. int intcnt;
  92. bool set_irq_mask;
  93. };
  94. static inline unsigned int to_reg(int gpio, enum ctrl_register reg_type)
  95. {
  96. unsigned int reg;
  97. int bank;
  98. if (gpio < BANK0_NR_PINS)
  99. bank = 0;
  100. else if (gpio < BANK0_NR_PINS + BANK1_NR_PINS)
  101. bank = 1;
  102. else
  103. bank = 2;
  104. if (reg_type == CTRL_IN)
  105. reg = GPIO_IN_CTRL_BASE + bank;
  106. else
  107. reg = GPIO_OUT_CTRL_BASE + bank;
  108. return reg;
  109. }
  110. static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio)
  111. {
  112. unsigned int reg, mask;
  113. if (gpio < GROUP0_NR_IRQS) {
  114. reg = IRQ_MASK_BASE;
  115. mask = BIT(gpio % GROUP0_NR_IRQS);
  116. } else {
  117. reg = IRQ_MASK_BASE + 1;
  118. mask = BIT((gpio - GROUP0_NR_IRQS) % GROUP1_NR_IRQS);
  119. }
  120. if (wg->set_irq_mask)
  121. regmap_update_bits(wg->regmap, reg, mask, mask);
  122. else
  123. regmap_update_bits(wg->regmap, reg, mask, 0);
  124. }
  125. static void wcove_update_irq_ctrl(struct wcove_gpio *wg, int gpio)
  126. {
  127. unsigned int reg = to_reg(gpio, CTRL_IN);
  128. regmap_update_bits(wg->regmap, reg, CTLI_INTCNT_BE, wg->intcnt);
  129. }
  130. static int wcove_gpio_dir_in(struct gpio_chip *chip, unsigned int gpio)
  131. {
  132. struct wcove_gpio *wg = gpiochip_get_data(chip);
  133. return regmap_write(wg->regmap, to_reg(gpio, CTRL_OUT),
  134. CTLO_INPUT_SET);
  135. }
  136. static int wcove_gpio_dir_out(struct gpio_chip *chip, unsigned int gpio,
  137. int value)
  138. {
  139. struct wcove_gpio *wg = gpiochip_get_data(chip);
  140. return regmap_write(wg->regmap, to_reg(gpio, CTRL_OUT),
  141. CTLO_OUTPUT_SET | value);
  142. }
  143. static int wcove_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
  144. {
  145. struct wcove_gpio *wg = gpiochip_get_data(chip);
  146. unsigned int val;
  147. int ret;
  148. ret = regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &val);
  149. if (ret)
  150. return ret;
  151. return !(val & CTLO_DIR_OUT);
  152. }
  153. static int wcove_gpio_get(struct gpio_chip *chip, unsigned int gpio)
  154. {
  155. struct wcove_gpio *wg = gpiochip_get_data(chip);
  156. unsigned int val;
  157. int ret;
  158. ret = regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &val);
  159. if (ret)
  160. return ret;
  161. return val & 0x1;
  162. }
  163. static void wcove_gpio_set(struct gpio_chip *chip,
  164. unsigned int gpio, int value)
  165. {
  166. struct wcove_gpio *wg = gpiochip_get_data(chip);
  167. if (value)
  168. regmap_update_bits(wg->regmap, to_reg(gpio, CTRL_OUT), 1, 1);
  169. else
  170. regmap_update_bits(wg->regmap, to_reg(gpio, CTRL_OUT), 1, 0);
  171. }
  172. static int wcove_gpio_set_config(struct gpio_chip *chip, unsigned int gpio,
  173. unsigned long config)
  174. {
  175. struct wcove_gpio *wg = gpiochip_get_data(chip);
  176. switch (pinconf_to_config_param(config)) {
  177. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  178. return regmap_update_bits(wg->regmap, to_reg(gpio, CTRL_OUT),
  179. CTLO_DRV_MASK, CTLO_DRV_OD);
  180. case PIN_CONFIG_DRIVE_PUSH_PULL:
  181. return regmap_update_bits(wg->regmap, to_reg(gpio, CTRL_OUT),
  182. CTLO_DRV_MASK, CTLO_DRV_CMOS);
  183. default:
  184. break;
  185. }
  186. return -ENOTSUPP;
  187. }
  188. static int wcove_irq_type(struct irq_data *data, unsigned int type)
  189. {
  190. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  191. struct wcove_gpio *wg = gpiochip_get_data(chip);
  192. switch (type) {
  193. case IRQ_TYPE_NONE:
  194. wg->intcnt = CTLI_INTCNT_DIS;
  195. break;
  196. case IRQ_TYPE_EDGE_BOTH:
  197. wg->intcnt = CTLI_INTCNT_BE;
  198. break;
  199. case IRQ_TYPE_EDGE_RISING:
  200. wg->intcnt = CTLI_INTCNT_PE;
  201. break;
  202. case IRQ_TYPE_EDGE_FALLING:
  203. wg->intcnt = CTLI_INTCNT_NE;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. wg->update |= UPDATE_IRQ_TYPE;
  209. return 0;
  210. }
  211. static void wcove_bus_lock(struct irq_data *data)
  212. {
  213. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  214. struct wcove_gpio *wg = gpiochip_get_data(chip);
  215. mutex_lock(&wg->buslock);
  216. }
  217. static void wcove_bus_sync_unlock(struct irq_data *data)
  218. {
  219. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  220. struct wcove_gpio *wg = gpiochip_get_data(chip);
  221. int gpio = data->hwirq;
  222. if (wg->update & UPDATE_IRQ_TYPE)
  223. wcove_update_irq_ctrl(wg, gpio);
  224. if (wg->update & UPDATE_IRQ_MASK)
  225. wcove_update_irq_mask(wg, gpio);
  226. wg->update = 0;
  227. mutex_unlock(&wg->buslock);
  228. }
  229. static void wcove_irq_unmask(struct irq_data *data)
  230. {
  231. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  232. struct wcove_gpio *wg = gpiochip_get_data(chip);
  233. wg->set_irq_mask = false;
  234. wg->update |= UPDATE_IRQ_MASK;
  235. }
  236. static void wcove_irq_mask(struct irq_data *data)
  237. {
  238. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  239. struct wcove_gpio *wg = gpiochip_get_data(chip);
  240. wg->set_irq_mask = true;
  241. wg->update |= UPDATE_IRQ_MASK;
  242. }
  243. static struct irq_chip wcove_irqchip = {
  244. .name = "Whiskey Cove",
  245. .irq_mask = wcove_irq_mask,
  246. .irq_unmask = wcove_irq_unmask,
  247. .irq_set_type = wcove_irq_type,
  248. .irq_bus_lock = wcove_bus_lock,
  249. .irq_bus_sync_unlock = wcove_bus_sync_unlock,
  250. };
  251. static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
  252. {
  253. struct wcove_gpio *wg = (struct wcove_gpio *)data;
  254. unsigned int pending, virq, gpio, mask, offset;
  255. u8 p[2];
  256. if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
  257. dev_err(wg->dev, "Failed to read irq status register\n");
  258. return IRQ_NONE;
  259. }
  260. pending = p[0] | (p[1] << 8);
  261. if (!pending)
  262. return IRQ_NONE;
  263. /* Iterate until no interrupt is pending */
  264. while (pending) {
  265. /* One iteration is for all pending bits */
  266. for_each_set_bit(gpio, (const unsigned long *)&pending,
  267. GROUP0_NR_IRQS) {
  268. offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0;
  269. mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) :
  270. BIT(gpio);
  271. virq = irq_find_mapping(wg->chip.irqdomain, gpio);
  272. handle_nested_irq(virq);
  273. regmap_update_bits(wg->regmap, IRQ_STATUS_BASE + offset,
  274. mask, mask);
  275. }
  276. /* Next iteration */
  277. if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
  278. dev_err(wg->dev, "Failed to read irq status\n");
  279. break;
  280. }
  281. pending = p[0] | (p[1] << 8);
  282. }
  283. return IRQ_HANDLED;
  284. }
  285. static void wcove_gpio_dbg_show(struct seq_file *s,
  286. struct gpio_chip *chip)
  287. {
  288. unsigned int ctlo, ctli, irq_mask, irq_status;
  289. struct wcove_gpio *wg = gpiochip_get_data(chip);
  290. int gpio, offset, group, ret = 0;
  291. for (gpio = 0; gpio < WCOVE_GPIO_NUM; gpio++) {
  292. group = gpio < GROUP0_NR_IRQS ? 0 : 1;
  293. ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
  294. ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &ctli);
  295. ret += regmap_read(wg->regmap, IRQ_MASK_BASE + group,
  296. &irq_mask);
  297. ret += regmap_read(wg->regmap, IRQ_STATUS_BASE + group,
  298. &irq_status);
  299. if (ret) {
  300. pr_err("Failed to read registers: ctrl out/in or irq status/mask\n");
  301. break;
  302. }
  303. offset = gpio % 8;
  304. seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s\n",
  305. gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
  306. ctli & 0x1 ? "hi" : "lo",
  307. ctli & CTLI_INTCNT_NE ? "fall" : " ",
  308. ctli & CTLI_INTCNT_PE ? "rise" : " ",
  309. ctlo,
  310. irq_mask & BIT(offset) ? "mask " : "unmask",
  311. irq_status & BIT(offset) ? "pending" : " ");
  312. }
  313. }
  314. static int wcove_gpio_probe(struct platform_device *pdev)
  315. {
  316. struct intel_soc_pmic *pmic;
  317. struct wcove_gpio *wg;
  318. int virq, ret, irq;
  319. struct device *dev;
  320. /*
  321. * This gpio platform device is created by a mfd device (see
  322. * drivers/mfd/intel_soc_pmic_bxtwc.c for details). Information
  323. * shared by all sub-devices created by the mfd device, the regmap
  324. * pointer for instance, is stored as driver data of the mfd device
  325. * driver.
  326. */
  327. pmic = dev_get_drvdata(pdev->dev.parent);
  328. if (!pmic)
  329. return -ENODEV;
  330. irq = platform_get_irq(pdev, 0);
  331. if (irq < 0)
  332. return irq;
  333. dev = &pdev->dev;
  334. wg = devm_kzalloc(dev, sizeof(*wg), GFP_KERNEL);
  335. if (!wg)
  336. return -ENOMEM;
  337. wg->regmap_irq_chip = pmic->irq_chip_data_level2;
  338. platform_set_drvdata(pdev, wg);
  339. mutex_init(&wg->buslock);
  340. wg->chip.label = KBUILD_MODNAME;
  341. wg->chip.direction_input = wcove_gpio_dir_in;
  342. wg->chip.direction_output = wcove_gpio_dir_out;
  343. wg->chip.get_direction = wcove_gpio_get_direction;
  344. wg->chip.get = wcove_gpio_get;
  345. wg->chip.set = wcove_gpio_set;
  346. wg->chip.set_config = wcove_gpio_set_config,
  347. wg->chip.base = -1;
  348. wg->chip.ngpio = WCOVE_VGPIO_NUM;
  349. wg->chip.can_sleep = true;
  350. wg->chip.parent = pdev->dev.parent;
  351. wg->chip.dbg_show = wcove_gpio_dbg_show;
  352. wg->dev = dev;
  353. wg->regmap = pmic->regmap;
  354. ret = devm_gpiochip_add_data(dev, &wg->chip, wg);
  355. if (ret) {
  356. dev_err(dev, "Failed to add gpiochip: %d\n", ret);
  357. return ret;
  358. }
  359. ret = gpiochip_irqchip_add_nested(&wg->chip, &wcove_irqchip, 0,
  360. handle_simple_irq, IRQ_TYPE_NONE);
  361. if (ret) {
  362. dev_err(dev, "Failed to add irqchip: %d\n", ret);
  363. return ret;
  364. }
  365. virq = regmap_irq_get_virq(wg->regmap_irq_chip, irq);
  366. if (virq < 0) {
  367. dev_err(dev, "Failed to get virq by irq %d\n", irq);
  368. return virq;
  369. }
  370. ret = devm_request_threaded_irq(dev, virq, NULL,
  371. wcove_gpio_irq_handler, IRQF_ONESHOT, pdev->name, wg);
  372. if (ret) {
  373. dev_err(dev, "Failed to request irq %d\n", virq);
  374. return ret;
  375. }
  376. gpiochip_set_nested_irqchip(&wg->chip, &wcove_irqchip, virq);
  377. return 0;
  378. }
  379. /*
  380. * Whiskey Cove PMIC itself is a analog device(but with digital control
  381. * interface) providing power management support for other devices in
  382. * the accompanied SoC, so we have no .pm for Whiskey Cove GPIO driver.
  383. */
  384. static struct platform_driver wcove_gpio_driver = {
  385. .driver = {
  386. .name = "bxt_wcove_gpio",
  387. },
  388. .probe = wcove_gpio_probe,
  389. };
  390. module_platform_driver(wcove_gpio_driver);
  391. MODULE_AUTHOR("Ajay Thomas <ajay.thomas.david.rajamanickam@intel.com>");
  392. MODULE_AUTHOR("Bin Gao <bin.gao@intel.com>");
  393. MODULE_DESCRIPTION("Intel Whiskey Cove GPIO Driver");
  394. MODULE_LICENSE("GPL v2");
  395. MODULE_ALIAS("platform:bxt_wcove_gpio");