gpio-adp5520.c 4.2 KB

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