PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpio/gpio-max77620.c

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