gpio-da9055.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GPIO Driver for Dialog DA9055 PMICs.
  4. *
  5. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  6. *
  7. * Author: David Dajun Chen <dchen@diasemi.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/mfd/da9055/core.h>
  13. #include <linux/mfd/da9055/reg.h>
  14. #include <linux/mfd/da9055/pdata.h>
  15. #define DA9055_VDD_IO 0x0
  16. #define DA9055_PUSH_PULL 0x3
  17. #define DA9055_ACT_LOW 0x0
  18. #define DA9055_GPI 0x1
  19. #define DA9055_PORT_MASK 0x3
  20. #define DA9055_PORT_SHIFT(offset) (4 * (offset % 2))
  21. #define DA9055_INPUT DA9055_GPI
  22. #define DA9055_OUTPUT DA9055_PUSH_PULL
  23. #define DA9055_IRQ_GPI0 3
  24. struct da9055_gpio {
  25. struct da9055 *da9055;
  26. struct gpio_chip gp;
  27. };
  28. static int da9055_gpio_get(struct gpio_chip *gc, unsigned offset)
  29. {
  30. struct da9055_gpio *gpio = gpiochip_get_data(gc);
  31. int gpio_direction = 0;
  32. int ret;
  33. /* Get GPIO direction */
  34. ret = da9055_reg_read(gpio->da9055, (offset >> 1) + DA9055_REG_GPIO0_1);
  35. if (ret < 0)
  36. return ret;
  37. gpio_direction = ret & (DA9055_PORT_MASK) << DA9055_PORT_SHIFT(offset);
  38. gpio_direction >>= DA9055_PORT_SHIFT(offset);
  39. switch (gpio_direction) {
  40. case DA9055_INPUT:
  41. ret = da9055_reg_read(gpio->da9055, DA9055_REG_STATUS_B);
  42. if (ret < 0)
  43. return ret;
  44. break;
  45. case DA9055_OUTPUT:
  46. ret = da9055_reg_read(gpio->da9055, DA9055_REG_GPIO_MODE0_2);
  47. if (ret < 0)
  48. return ret;
  49. }
  50. return ret & (1 << offset);
  51. }
  52. static void da9055_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  53. {
  54. struct da9055_gpio *gpio = gpiochip_get_data(gc);
  55. da9055_reg_update(gpio->da9055,
  56. DA9055_REG_GPIO_MODE0_2,
  57. 1 << offset,
  58. value << offset);
  59. }
  60. static int da9055_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  61. {
  62. struct da9055_gpio *gpio = gpiochip_get_data(gc);
  63. unsigned char reg_byte;
  64. reg_byte = (DA9055_ACT_LOW | DA9055_GPI)
  65. << DA9055_PORT_SHIFT(offset);
  66. return da9055_reg_update(gpio->da9055, (offset >> 1) +
  67. DA9055_REG_GPIO0_1,
  68. DA9055_PORT_MASK <<
  69. DA9055_PORT_SHIFT(offset),
  70. reg_byte);
  71. }
  72. static int da9055_gpio_direction_output(struct gpio_chip *gc,
  73. unsigned offset, int value)
  74. {
  75. struct da9055_gpio *gpio = gpiochip_get_data(gc);
  76. unsigned char reg_byte;
  77. int ret;
  78. reg_byte = (DA9055_VDD_IO | DA9055_PUSH_PULL)
  79. << DA9055_PORT_SHIFT(offset);
  80. ret = da9055_reg_update(gpio->da9055, (offset >> 1) +
  81. DA9055_REG_GPIO0_1,
  82. DA9055_PORT_MASK <<
  83. DA9055_PORT_SHIFT(offset),
  84. reg_byte);
  85. if (ret < 0)
  86. return ret;
  87. da9055_gpio_set(gc, offset, value);
  88. return 0;
  89. }
  90. static int da9055_gpio_to_irq(struct gpio_chip *gc, u32 offset)
  91. {
  92. struct da9055_gpio *gpio = gpiochip_get_data(gc);
  93. struct da9055 *da9055 = gpio->da9055;
  94. return regmap_irq_get_virq(da9055->irq_data,
  95. DA9055_IRQ_GPI0 + offset);
  96. }
  97. static const struct gpio_chip reference_gp = {
  98. .label = "da9055-gpio",
  99. .owner = THIS_MODULE,
  100. .get = da9055_gpio_get,
  101. .set = da9055_gpio_set,
  102. .direction_input = da9055_gpio_direction_input,
  103. .direction_output = da9055_gpio_direction_output,
  104. .to_irq = da9055_gpio_to_irq,
  105. .can_sleep = true,
  106. .ngpio = 3,
  107. .base = -1,
  108. };
  109. static int da9055_gpio_probe(struct platform_device *pdev)
  110. {
  111. struct da9055_gpio *gpio;
  112. struct da9055_pdata *pdata;
  113. int ret;
  114. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  115. if (!gpio)
  116. return -ENOMEM;
  117. gpio->da9055 = dev_get_drvdata(pdev->dev.parent);
  118. pdata = dev_get_platdata(gpio->da9055->dev);
  119. gpio->gp = reference_gp;
  120. if (pdata && pdata->gpio_base)
  121. gpio->gp.base = pdata->gpio_base;
  122. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
  123. if (ret < 0) {
  124. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  125. return ret;
  126. }
  127. platform_set_drvdata(pdev, gpio);
  128. return 0;
  129. }
  130. static struct platform_driver da9055_gpio_driver = {
  131. .probe = da9055_gpio_probe,
  132. .driver = {
  133. .name = "da9055-gpio",
  134. },
  135. };
  136. static int __init da9055_gpio_init(void)
  137. {
  138. return platform_driver_register(&da9055_gpio_driver);
  139. }
  140. subsys_initcall(da9055_gpio_init);
  141. static void __exit da9055_gpio_exit(void)
  142. {
  143. platform_driver_unregister(&da9055_gpio_driver);
  144. }
  145. module_exit(da9055_gpio_exit);
  146. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  147. MODULE_DESCRIPTION("DA9055 GPIO Device Driver");
  148. MODULE_LICENSE("GPL");
  149. MODULE_ALIAS("platform:da9055-gpio");