gpio-max77620.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * MAXIM MAX77620 GPIO driver
  3. *
  4. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. */
  10. #include <linux/gpio/driver.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mfd/max77620.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #define GPIO_REG_ADDR(offset) (MAX77620_REG_GPIO0 + offset)
  17. struct max77620_gpio {
  18. struct gpio_chip gpio_chip;
  19. struct regmap *rmap;
  20. struct device *dev;
  21. int gpio_irq;
  22. int irq_base;
  23. int gpio_base;
  24. };
  25. static const struct regmap_irq max77620_gpio_irqs[] = {
  26. [0] = {
  27. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE0,
  28. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  29. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  30. .reg_offset = 0,
  31. .type_reg_offset = 0,
  32. },
  33. [1] = {
  34. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE1,
  35. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  36. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  37. .reg_offset = 0,
  38. .type_reg_offset = 1,
  39. },
  40. [2] = {
  41. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE2,
  42. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  43. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  44. .reg_offset = 0,
  45. .type_reg_offset = 2,
  46. },
  47. [3] = {
  48. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE3,
  49. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  50. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  51. .reg_offset = 0,
  52. .type_reg_offset = 3,
  53. },
  54. [4] = {
  55. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE4,
  56. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  57. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  58. .reg_offset = 0,
  59. .type_reg_offset = 4,
  60. },
  61. [5] = {
  62. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE5,
  63. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  64. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  65. .reg_offset = 0,
  66. .type_reg_offset = 5,
  67. },
  68. [6] = {
  69. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE6,
  70. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  71. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  72. .reg_offset = 0,
  73. .type_reg_offset = 6,
  74. },
  75. [7] = {
  76. .mask = MAX77620_IRQ_LVL2_GPIO_EDGE7,
  77. .type_rising_mask = MAX77620_CNFG_GPIO_INT_RISING,
  78. .type_falling_mask = MAX77620_CNFG_GPIO_INT_FALLING,
  79. .reg_offset = 0,
  80. .type_reg_offset = 7,
  81. },
  82. };
  83. static struct regmap_irq_chip max77620_gpio_irq_chip = {
  84. .name = "max77620-gpio",
  85. .irqs = max77620_gpio_irqs,
  86. .num_irqs = ARRAY_SIZE(max77620_gpio_irqs),
  87. .num_regs = 1,
  88. .num_type_reg = 8,
  89. .irq_reg_stride = 1,
  90. .type_reg_stride = 1,
  91. .status_base = MAX77620_REG_IRQ_LVL2_GPIO,
  92. .type_base = MAX77620_REG_GPIO0,
  93. };
  94. static int max77620_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
  95. {
  96. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  97. int ret;
  98. ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  99. MAX77620_CNFG_GPIO_DIR_MASK,
  100. MAX77620_CNFG_GPIO_DIR_INPUT);
  101. if (ret < 0)
  102. dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
  103. return ret;
  104. }
  105. static int max77620_gpio_get(struct gpio_chip *gc, unsigned int offset)
  106. {
  107. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  108. unsigned int val;
  109. int ret;
  110. ret = regmap_read(mgpio->rmap, GPIO_REG_ADDR(offset), &val);
  111. if (ret < 0) {
  112. dev_err(mgpio->dev, "CNFG_GPIOx read failed: %d\n", ret);
  113. return ret;
  114. }
  115. if (val & MAX77620_CNFG_GPIO_DIR_MASK)
  116. return !!(val & MAX77620_CNFG_GPIO_INPUT_VAL_MASK);
  117. else
  118. return !!(val & MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK);
  119. }
  120. static int max77620_gpio_dir_output(struct gpio_chip *gc, unsigned int offset,
  121. int value)
  122. {
  123. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  124. u8 val;
  125. int ret;
  126. val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
  127. MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
  128. ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  129. MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
  130. if (ret < 0) {
  131. dev_err(mgpio->dev, "CNFG_GPIOx val update failed: %d\n", ret);
  132. return ret;
  133. }
  134. ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  135. MAX77620_CNFG_GPIO_DIR_MASK,
  136. MAX77620_CNFG_GPIO_DIR_OUTPUT);
  137. if (ret < 0)
  138. dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
  139. return ret;
  140. }
  141. static int max77620_gpio_set_debounce(struct gpio_chip *gc,
  142. unsigned int offset,
  143. unsigned int debounce)
  144. {
  145. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  146. u8 val;
  147. int ret;
  148. switch (debounce) {
  149. case 0:
  150. val = MAX77620_CNFG_GPIO_DBNC_None;
  151. break;
  152. case 1 ... 8:
  153. val = MAX77620_CNFG_GPIO_DBNC_8ms;
  154. break;
  155. case 9 ... 16:
  156. val = MAX77620_CNFG_GPIO_DBNC_16ms;
  157. break;
  158. case 17 ... 32:
  159. val = MAX77620_CNFG_GPIO_DBNC_32ms;
  160. break;
  161. default:
  162. dev_err(mgpio->dev, "Illegal value %u\n", debounce);
  163. return -EINVAL;
  164. }
  165. ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  166. MAX77620_CNFG_GPIO_DBNC_MASK, val);
  167. if (ret < 0)
  168. dev_err(mgpio->dev, "CNFG_GPIOx_DBNC update failed: %d\n", ret);
  169. return ret;
  170. }
  171. static void max77620_gpio_set(struct gpio_chip *gc, unsigned int offset,
  172. int value)
  173. {
  174. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  175. u8 val;
  176. int ret;
  177. val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
  178. MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
  179. ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  180. MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
  181. if (ret < 0)
  182. dev_err(mgpio->dev, "CNFG_GPIO_OUT update failed: %d\n", ret);
  183. }
  184. static int max77620_gpio_set_single_ended(struct gpio_chip *gc,
  185. unsigned int offset,
  186. enum single_ended_mode mode)
  187. {
  188. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  189. switch (mode) {
  190. case LINE_MODE_OPEN_DRAIN:
  191. return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  192. MAX77620_CNFG_GPIO_DRV_MASK,
  193. MAX77620_CNFG_GPIO_DRV_OPENDRAIN);
  194. case LINE_MODE_PUSH_PULL:
  195. return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
  196. MAX77620_CNFG_GPIO_DRV_MASK,
  197. MAX77620_CNFG_GPIO_DRV_PUSHPULL);
  198. default:
  199. break;
  200. }
  201. return -ENOTSUPP;
  202. }
  203. static int max77620_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
  204. {
  205. struct max77620_gpio *mgpio = gpiochip_get_data(gc);
  206. struct max77620_chip *chip = dev_get_drvdata(mgpio->dev->parent);
  207. return regmap_irq_get_virq(chip->gpio_irq_data, offset);
  208. }
  209. static int max77620_gpio_probe(struct platform_device *pdev)
  210. {
  211. struct max77620_chip *chip = dev_get_drvdata(pdev->dev.parent);
  212. struct max77620_gpio *mgpio;
  213. int gpio_irq;
  214. int ret;
  215. gpio_irq = platform_get_irq(pdev, 0);
  216. if (gpio_irq <= 0) {
  217. dev_err(&pdev->dev, "GPIO irq not available %d\n", gpio_irq);
  218. return -ENODEV;
  219. }
  220. mgpio = devm_kzalloc(&pdev->dev, sizeof(*mgpio), GFP_KERNEL);
  221. if (!mgpio)
  222. return -ENOMEM;
  223. mgpio->rmap = chip->rmap;
  224. mgpio->dev = &pdev->dev;
  225. mgpio->gpio_irq = gpio_irq;
  226. mgpio->gpio_chip.label = pdev->name;
  227. mgpio->gpio_chip.parent = &pdev->dev;
  228. mgpio->gpio_chip.direction_input = max77620_gpio_dir_input;
  229. mgpio->gpio_chip.get = max77620_gpio_get;
  230. mgpio->gpio_chip.direction_output = max77620_gpio_dir_output;
  231. mgpio->gpio_chip.set_debounce = max77620_gpio_set_debounce;
  232. mgpio->gpio_chip.set = max77620_gpio_set;
  233. mgpio->gpio_chip.set_single_ended = max77620_gpio_set_single_ended;
  234. mgpio->gpio_chip.to_irq = max77620_gpio_to_irq;
  235. mgpio->gpio_chip.ngpio = MAX77620_GPIO_NR;
  236. mgpio->gpio_chip.can_sleep = 1;
  237. mgpio->gpio_chip.base = -1;
  238. mgpio->irq_base = -1;
  239. #ifdef CONFIG_OF_GPIO
  240. mgpio->gpio_chip.of_node = pdev->dev.parent->of_node;
  241. #endif
  242. platform_set_drvdata(pdev, mgpio);
  243. ret = devm_gpiochip_add_data(&pdev->dev, &mgpio->gpio_chip, mgpio);
  244. if (ret < 0) {
  245. dev_err(&pdev->dev, "gpio_init: Failed to add max77620_gpio\n");
  246. return ret;
  247. }
  248. mgpio->gpio_base = mgpio->gpio_chip.base;
  249. ret = devm_regmap_add_irq_chip(&pdev->dev, chip->rmap, mgpio->gpio_irq,
  250. IRQF_ONESHOT, mgpio->irq_base,
  251. &max77620_gpio_irq_chip,
  252. &chip->gpio_irq_data);
  253. if (ret < 0) {
  254. dev_err(&pdev->dev, "Failed to add gpio irq_chip %d\n", ret);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. static const struct platform_device_id max77620_gpio_devtype[] = {
  260. { .name = "max77620-gpio", },
  261. {},
  262. };
  263. MODULE_DEVICE_TABLE(platform, max77620_gpio_devtype);
  264. static struct platform_driver max77620_gpio_driver = {
  265. .driver.name = "max77620-gpio",
  266. .probe = max77620_gpio_probe,
  267. .id_table = max77620_gpio_devtype,
  268. };
  269. module_platform_driver(max77620_gpio_driver);
  270. MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
  271. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  272. MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
  273. MODULE_ALIAS("platform:max77620-gpio");
  274. MODULE_LICENSE("GPL v2");