gpio-wcove.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 GPIO_IRQ0_MASK GENMASK(6, 0)
  52. #define GPIO_IRQ1_MASK GENMASK(5, 0)
  53. #define UPDATE_IRQ_TYPE BIT(0)
  54. #define UPDATE_IRQ_MASK BIT(1)
  55. #define CTLI_INTCNT_DIS (0 << 1)
  56. #define CTLI_INTCNT_NE (1 << 1)
  57. #define CTLI_INTCNT_PE (2 << 1)
  58. #define CTLI_INTCNT_BE (3 << 1)
  59. #define CTLO_DIR_IN (0 << 5)
  60. #define CTLO_DIR_OUT (1 << 5)
  61. #define CTLO_DRV_MASK (1 << 4)
  62. #define CTLO_DRV_OD (0 << 4)
  63. #define CTLO_DRV_CMOS (1 << 4)
  64. #define CTLO_DRV_REN (1 << 3)
  65. #define CTLO_RVAL_2KDOWN (0 << 1)
  66. #define CTLO_RVAL_2KUP (1 << 1)
  67. #define CTLO_RVAL_50KDOWN (2 << 1)
  68. #define CTLO_RVAL_50KUP (3 << 1)
  69. #define CTLO_INPUT_SET (CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
  70. #define CTLO_OUTPUT_SET (CTLO_DIR_OUT | CTLO_INPUT_SET)
  71. enum ctrl_register {
  72. CTRL_IN,
  73. CTRL_OUT,
  74. };
  75. /*
  76. * struct wcove_gpio - Whiskey Cove GPIO controller
  77. * @buslock: for bus lock/sync and unlock.
  78. * @chip: the abstract gpio_chip structure.
  79. * @dev: the gpio device
  80. * @regmap: the regmap from the parent device.
  81. * @regmap_irq_chip: the regmap of the gpio irq chip.
  82. * @update: pending IRQ setting update, to be written to the chip upon unlock.
  83. * @intcnt: the Interrupt Detect value to be written.
  84. * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
  85. */
  86. struct wcove_gpio {
  87. struct mutex buslock;
  88. struct gpio_chip chip;
  89. struct device *dev;
  90. struct regmap *regmap;
  91. struct regmap_irq_chip_data *regmap_irq_chip;
  92. int update;
  93. int intcnt;
  94. bool set_irq_mask;
  95. };
  96. static inline unsigned int to_reg(int gpio, enum ctrl_register reg_type)
  97. {
  98. unsigned int reg;
  99. if (gpio >= WCOVE_GPIO_NUM)
  100. return -EOPNOTSUPP;
  101. if (reg_type == CTRL_IN)
  102. reg = GPIO_IN_CTRL_BASE + gpio;
  103. else
  104. reg = GPIO_OUT_CTRL_BASE + gpio;
  105. return reg;
  106. }
  107. static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio)
  108. {
  109. unsigned int reg, mask;
  110. if (gpio < GROUP0_NR_IRQS) {
  111. reg = IRQ_MASK_BASE;
  112. mask = BIT(gpio % GROUP0_NR_IRQS);
  113. } else {
  114. reg = IRQ_MASK_BASE + 1;
  115. mask = BIT((gpio - GROUP0_NR_IRQS) % GROUP1_NR_IRQS);
  116. }
  117. if (wg->set_irq_mask)
  118. regmap_update_bits(wg->regmap, reg, mask, mask);
  119. else
  120. regmap_update_bits(wg->regmap, reg, mask, 0);
  121. }
  122. static void wcove_update_irq_ctrl(struct wcove_gpio *wg, int gpio)
  123. {
  124. int reg = to_reg(gpio, CTRL_IN);
  125. if (reg < 0)
  126. return;
  127. regmap_update_bits(wg->regmap, reg, CTLI_INTCNT_BE, wg->intcnt);
  128. }
  129. static int wcove_gpio_dir_in(struct gpio_chip *chip, unsigned int gpio)
  130. {
  131. struct wcove_gpio *wg = gpiochip_get_data(chip);
  132. int reg = to_reg(gpio, CTRL_OUT);
  133. if (reg < 0)
  134. return 0;
  135. return regmap_write(wg->regmap, reg, CTLO_INPUT_SET);
  136. }
  137. static int wcove_gpio_dir_out(struct gpio_chip *chip, unsigned int gpio,
  138. int value)
  139. {
  140. struct wcove_gpio *wg = gpiochip_get_data(chip);
  141. int reg = to_reg(gpio, CTRL_OUT);
  142. if (reg < 0)
  143. return 0;
  144. return regmap_write(wg->regmap, reg, CTLO_OUTPUT_SET | value);
  145. }
  146. static int wcove_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
  147. {
  148. struct wcove_gpio *wg = gpiochip_get_data(chip);
  149. unsigned int val;
  150. int ret, reg = to_reg(gpio, CTRL_OUT);
  151. if (reg < 0)
  152. return 0;
  153. ret = regmap_read(wg->regmap, reg, &val);
  154. if (ret)
  155. return ret;
  156. return !(val & CTLO_DIR_OUT);
  157. }
  158. static int wcove_gpio_get(struct gpio_chip *chip, unsigned int gpio)
  159. {
  160. struct wcove_gpio *wg = gpiochip_get_data(chip);
  161. unsigned int val;
  162. int ret, reg = to_reg(gpio, CTRL_IN);
  163. if (reg < 0)
  164. return 0;
  165. ret = regmap_read(wg->regmap, reg, &val);
  166. if (ret)
  167. return ret;
  168. return val & 0x1;
  169. }
  170. static void wcove_gpio_set(struct gpio_chip *chip,
  171. unsigned int gpio, int value)
  172. {
  173. struct wcove_gpio *wg = gpiochip_get_data(chip);
  174. int reg = to_reg(gpio, CTRL_OUT);
  175. if (reg < 0)
  176. return;
  177. if (value)
  178. regmap_update_bits(wg->regmap, reg, 1, 1);
  179. else
  180. regmap_update_bits(wg->regmap, reg, 1, 0);
  181. }
  182. static int wcove_gpio_set_config(struct gpio_chip *chip, unsigned int gpio,
  183. unsigned long config)
  184. {
  185. struct wcove_gpio *wg = gpiochip_get_data(chip);
  186. int reg = to_reg(gpio, CTRL_OUT);
  187. if (reg < 0)
  188. return 0;
  189. switch (pinconf_to_config_param(config)) {
  190. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  191. return regmap_update_bits(wg->regmap, reg, CTLO_DRV_MASK,
  192. CTLO_DRV_OD);
  193. case PIN_CONFIG_DRIVE_PUSH_PULL:
  194. return regmap_update_bits(wg->regmap, reg, CTLO_DRV_MASK,
  195. CTLO_DRV_CMOS);
  196. default:
  197. break;
  198. }
  199. return -ENOTSUPP;
  200. }
  201. static int wcove_irq_type(struct irq_data *data, unsigned int type)
  202. {
  203. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  204. struct wcove_gpio *wg = gpiochip_get_data(chip);
  205. if (data->hwirq >= WCOVE_GPIO_NUM)
  206. return 0;
  207. switch (type) {
  208. case IRQ_TYPE_NONE:
  209. wg->intcnt = CTLI_INTCNT_DIS;
  210. break;
  211. case IRQ_TYPE_EDGE_BOTH:
  212. wg->intcnt = CTLI_INTCNT_BE;
  213. break;
  214. case IRQ_TYPE_EDGE_RISING:
  215. wg->intcnt = CTLI_INTCNT_PE;
  216. break;
  217. case IRQ_TYPE_EDGE_FALLING:
  218. wg->intcnt = CTLI_INTCNT_NE;
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. wg->update |= UPDATE_IRQ_TYPE;
  224. return 0;
  225. }
  226. static void wcove_bus_lock(struct irq_data *data)
  227. {
  228. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  229. struct wcove_gpio *wg = gpiochip_get_data(chip);
  230. mutex_lock(&wg->buslock);
  231. }
  232. static void wcove_bus_sync_unlock(struct irq_data *data)
  233. {
  234. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  235. struct wcove_gpio *wg = gpiochip_get_data(chip);
  236. int gpio = data->hwirq;
  237. if (wg->update & UPDATE_IRQ_TYPE)
  238. wcove_update_irq_ctrl(wg, gpio);
  239. if (wg->update & UPDATE_IRQ_MASK)
  240. wcove_update_irq_mask(wg, gpio);
  241. wg->update = 0;
  242. mutex_unlock(&wg->buslock);
  243. }
  244. static void wcove_irq_unmask(struct irq_data *data)
  245. {
  246. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  247. struct wcove_gpio *wg = gpiochip_get_data(chip);
  248. if (data->hwirq >= WCOVE_GPIO_NUM)
  249. return;
  250. wg->set_irq_mask = false;
  251. wg->update |= UPDATE_IRQ_MASK;
  252. }
  253. static void wcove_irq_mask(struct irq_data *data)
  254. {
  255. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  256. struct wcove_gpio *wg = gpiochip_get_data(chip);
  257. if (data->hwirq >= WCOVE_GPIO_NUM)
  258. return;
  259. wg->set_irq_mask = true;
  260. wg->update |= UPDATE_IRQ_MASK;
  261. }
  262. static struct irq_chip wcove_irqchip = {
  263. .name = "Whiskey Cove",
  264. .irq_mask = wcove_irq_mask,
  265. .irq_unmask = wcove_irq_unmask,
  266. .irq_set_type = wcove_irq_type,
  267. .irq_bus_lock = wcove_bus_lock,
  268. .irq_bus_sync_unlock = wcove_bus_sync_unlock,
  269. };
  270. static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
  271. {
  272. struct wcove_gpio *wg = (struct wcove_gpio *)data;
  273. unsigned int pending, virq, gpio, mask, offset;
  274. u8 p[2];
  275. if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
  276. dev_err(wg->dev, "Failed to read irq status register\n");
  277. return IRQ_NONE;
  278. }
  279. pending = (p[0] & GPIO_IRQ0_MASK) | ((p[1] & GPIO_IRQ1_MASK) << 7);
  280. if (!pending)
  281. return IRQ_NONE;
  282. /* Iterate until no interrupt is pending */
  283. while (pending) {
  284. /* One iteration is for all pending bits */
  285. for_each_set_bit(gpio, (const unsigned long *)&pending,
  286. WCOVE_GPIO_NUM) {
  287. offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0;
  288. mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) :
  289. BIT(gpio);
  290. virq = irq_find_mapping(wg->chip.irq.domain, gpio);
  291. handle_nested_irq(virq);
  292. regmap_update_bits(wg->regmap, IRQ_STATUS_BASE + offset,
  293. mask, mask);
  294. }
  295. /* Next iteration */
  296. if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
  297. dev_err(wg->dev, "Failed to read irq status\n");
  298. break;
  299. }
  300. pending = (p[0] & GPIO_IRQ0_MASK) | ((p[1] & GPIO_IRQ1_MASK) << 7);
  301. }
  302. return IRQ_HANDLED;
  303. }
  304. static void wcove_gpio_dbg_show(struct seq_file *s,
  305. struct gpio_chip *chip)
  306. {
  307. unsigned int ctlo, ctli, irq_mask, irq_status;
  308. struct wcove_gpio *wg = gpiochip_get_data(chip);
  309. int gpio, offset, group, ret = 0;
  310. for (gpio = 0; gpio < WCOVE_GPIO_NUM; gpio++) {
  311. group = gpio < GROUP0_NR_IRQS ? 0 : 1;
  312. ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
  313. ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &ctli);
  314. ret += regmap_read(wg->regmap, IRQ_MASK_BASE + group,
  315. &irq_mask);
  316. ret += regmap_read(wg->regmap, IRQ_STATUS_BASE + group,
  317. &irq_status);
  318. if (ret) {
  319. pr_err("Failed to read registers: ctrl out/in or irq status/mask\n");
  320. break;
  321. }
  322. offset = gpio % 8;
  323. seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s\n",
  324. gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
  325. ctli & 0x1 ? "hi" : "lo",
  326. ctli & CTLI_INTCNT_NE ? "fall" : " ",
  327. ctli & CTLI_INTCNT_PE ? "rise" : " ",
  328. ctlo,
  329. irq_mask & BIT(offset) ? "mask " : "unmask",
  330. irq_status & BIT(offset) ? "pending" : " ");
  331. }
  332. }
  333. static int wcove_gpio_probe(struct platform_device *pdev)
  334. {
  335. struct intel_soc_pmic *pmic;
  336. struct wcove_gpio *wg;
  337. int virq, ret, irq;
  338. struct device *dev;
  339. /*
  340. * This gpio platform device is created by a mfd device (see
  341. * drivers/mfd/intel_soc_pmic_bxtwc.c for details). Information
  342. * shared by all sub-devices created by the mfd device, the regmap
  343. * pointer for instance, is stored as driver data of the mfd device
  344. * driver.
  345. */
  346. pmic = dev_get_drvdata(pdev->dev.parent);
  347. if (!pmic)
  348. return -ENODEV;
  349. irq = platform_get_irq(pdev, 0);
  350. if (irq < 0)
  351. return irq;
  352. dev = &pdev->dev;
  353. wg = devm_kzalloc(dev, sizeof(*wg), GFP_KERNEL);
  354. if (!wg)
  355. return -ENOMEM;
  356. wg->regmap_irq_chip = pmic->irq_chip_data;
  357. platform_set_drvdata(pdev, wg);
  358. mutex_init(&wg->buslock);
  359. wg->chip.label = KBUILD_MODNAME;
  360. wg->chip.direction_input = wcove_gpio_dir_in;
  361. wg->chip.direction_output = wcove_gpio_dir_out;
  362. wg->chip.get_direction = wcove_gpio_get_direction;
  363. wg->chip.get = wcove_gpio_get;
  364. wg->chip.set = wcove_gpio_set;
  365. wg->chip.set_config = wcove_gpio_set_config,
  366. wg->chip.base = -1;
  367. wg->chip.ngpio = WCOVE_VGPIO_NUM;
  368. wg->chip.can_sleep = true;
  369. wg->chip.parent = pdev->dev.parent;
  370. wg->chip.dbg_show = wcove_gpio_dbg_show;
  371. wg->dev = dev;
  372. wg->regmap = pmic->regmap;
  373. ret = devm_gpiochip_add_data(dev, &wg->chip, wg);
  374. if (ret) {
  375. dev_err(dev, "Failed to add gpiochip: %d\n", ret);
  376. return ret;
  377. }
  378. ret = gpiochip_irqchip_add_nested(&wg->chip, &wcove_irqchip, 0,
  379. handle_simple_irq, IRQ_TYPE_NONE);
  380. if (ret) {
  381. dev_err(dev, "Failed to add irqchip: %d\n", ret);
  382. return ret;
  383. }
  384. virq = regmap_irq_get_virq(wg->regmap_irq_chip, irq);
  385. if (virq < 0) {
  386. dev_err(dev, "Failed to get virq by irq %d\n", irq);
  387. return virq;
  388. }
  389. ret = devm_request_threaded_irq(dev, virq, NULL,
  390. wcove_gpio_irq_handler, IRQF_ONESHOT, pdev->name, wg);
  391. if (ret) {
  392. dev_err(dev, "Failed to request irq %d\n", virq);
  393. return ret;
  394. }
  395. gpiochip_set_nested_irqchip(&wg->chip, &wcove_irqchip, virq);
  396. /* Enable GPIO0 interrupts */
  397. ret = regmap_update_bits(wg->regmap, IRQ_MASK_BASE, GPIO_IRQ0_MASK,
  398. 0x00);
  399. if (ret)
  400. return ret;
  401. /* Enable GPIO1 interrupts */
  402. ret = regmap_update_bits(wg->regmap, IRQ_MASK_BASE + 1, GPIO_IRQ1_MASK,
  403. 0x00);
  404. if (ret)
  405. return ret;
  406. return 0;
  407. }
  408. /*
  409. * Whiskey Cove PMIC itself is a analog device(but with digital control
  410. * interface) providing power management support for other devices in
  411. * the accompanied SoC, so we have no .pm for Whiskey Cove GPIO driver.
  412. */
  413. static struct platform_driver wcove_gpio_driver = {
  414. .driver = {
  415. .name = "bxt_wcove_gpio",
  416. },
  417. .probe = wcove_gpio_probe,
  418. };
  419. module_platform_driver(wcove_gpio_driver);
  420. MODULE_AUTHOR("Ajay Thomas <ajay.thomas.david.rajamanickam@intel.com>");
  421. MODULE_AUTHOR("Bin Gao <bin.gao@intel.com>");
  422. MODULE_DESCRIPTION("Intel Whiskey Cove GPIO Driver");
  423. MODULE_LICENSE("GPL v2");
  424. MODULE_ALIAS("platform:bxt_wcove_gpio");