gpio-adp5520.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GPIO driver for Analog Devices ADP5520 MFD PMICs
  4. *
  5. * Copyright 2009 Analog Devices Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mfd/adp5520.h>
  13. #include <linux/gpio/driver.h>
  14. struct adp5520_gpio {
  15. struct device *master;
  16. struct gpio_chip gpio_chip;
  17. unsigned char lut[ADP5520_MAXGPIOS];
  18. unsigned long output;
  19. };
  20. static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
  21. {
  22. struct adp5520_gpio *dev;
  23. uint8_t reg_val;
  24. dev = gpiochip_get_data(chip);
  25. /*
  26. * There are dedicated registers for GPIO IN/OUT.
  27. * Make sure we return the right value, even when configured as output
  28. */
  29. if (test_bit(off, &dev->output))
  30. adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
  31. else
  32. adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
  33. return !!(reg_val & dev->lut[off]);
  34. }
  35. static void adp5520_gpio_set_value(struct gpio_chip *chip,
  36. unsigned off, int val)
  37. {
  38. struct adp5520_gpio *dev;
  39. dev = gpiochip_get_data(chip);
  40. if (val)
  41. adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
  42. else
  43. adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
  44. }
  45. static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  46. {
  47. struct adp5520_gpio *dev;
  48. dev = gpiochip_get_data(chip);
  49. clear_bit(off, &dev->output);
  50. return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
  51. dev->lut[off]);
  52. }
  53. static int adp5520_gpio_direction_output(struct gpio_chip *chip,
  54. unsigned off, int val)
  55. {
  56. struct adp5520_gpio *dev;
  57. int ret = 0;
  58. dev = gpiochip_get_data(chip);
  59. set_bit(off, &dev->output);
  60. if (val)
  61. ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
  62. dev->lut[off]);
  63. else
  64. ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
  65. dev->lut[off]);
  66. ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
  67. dev->lut[off]);
  68. return ret;
  69. }
  70. static int adp5520_gpio_probe(struct platform_device *pdev)
  71. {
  72. struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  73. struct adp5520_gpio *dev;
  74. struct gpio_chip *gc;
  75. int ret, i, gpios;
  76. unsigned char ctl_mask = 0;
  77. if (pdata == NULL) {
  78. dev_err(&pdev->dev, "missing platform data\n");
  79. return -ENODEV;
  80. }
  81. if (pdev->id != ID_ADP5520) {
  82. dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
  83. return -ENODEV;
  84. }
  85. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  86. if (dev == NULL)
  87. return -ENOMEM;
  88. dev->master = pdev->dev.parent;
  89. for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
  90. if (pdata->gpio_en_mask & (1 << i))
  91. dev->lut[gpios++] = 1 << i;
  92. if (gpios < 1)
  93. return -EINVAL;
  94. gc = &dev->gpio_chip;
  95. gc->direction_input = adp5520_gpio_direction_input;
  96. gc->direction_output = adp5520_gpio_direction_output;
  97. gc->get = adp5520_gpio_get_value;
  98. gc->set = adp5520_gpio_set_value;
  99. gc->can_sleep = true;
  100. gc->base = pdata->gpio_start;
  101. gc->ngpio = gpios;
  102. gc->label = pdev->name;
  103. gc->owner = THIS_MODULE;
  104. ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
  105. pdata->gpio_en_mask);
  106. if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
  107. ctl_mask |= ADP5520_C3_MODE;
  108. if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
  109. ctl_mask |= ADP5520_R3_MODE;
  110. if (ctl_mask)
  111. ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
  112. ctl_mask);
  113. ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
  114. pdata->gpio_pullup_mask);
  115. if (ret) {
  116. dev_err(&pdev->dev, "failed to write\n");
  117. return ret;
  118. }
  119. return devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
  120. }
  121. static struct platform_driver adp5520_gpio_driver = {
  122. .driver = {
  123. .name = "adp5520-gpio",
  124. },
  125. .probe = adp5520_gpio_probe,
  126. };
  127. module_platform_driver(adp5520_gpio_driver);
  128. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  129. MODULE_DESCRIPTION("GPIO ADP5520 Driver");
  130. MODULE_LICENSE("GPL");
  131. MODULE_ALIAS("platform:adp5520-gpio");