gpio-adp5520.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 __devinit adp5520_gpio_probe(struct platform_device *pdev)
  72. {
  73. struct adp5520_gpio_platform_data *pdata = pdev->dev.platform_data;
  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 = kzalloc(sizeof(*dev), GFP_KERNEL);
  87. if (dev == NULL) {
  88. dev_err(&pdev->dev, "failed to alloc memory\n");
  89. return -ENOMEM;
  90. }
  91. dev->master = pdev->dev.parent;
  92. for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
  93. if (pdata->gpio_en_mask & (1 << i))
  94. dev->lut[gpios++] = 1 << i;
  95. if (gpios < 1) {
  96. ret = -EINVAL;
  97. goto err;
  98. }
  99. gc = &dev->gpio_chip;
  100. gc->direction_input = adp5520_gpio_direction_input;
  101. gc->direction_output = adp5520_gpio_direction_output;
  102. gc->get = adp5520_gpio_get_value;
  103. gc->set = adp5520_gpio_set_value;
  104. gc->can_sleep = 1;
  105. gc->base = pdata->gpio_start;
  106. gc->ngpio = gpios;
  107. gc->label = pdev->name;
  108. gc->owner = THIS_MODULE;
  109. ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
  110. pdata->gpio_en_mask);
  111. if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
  112. ctl_mask |= ADP5520_C3_MODE;
  113. if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
  114. ctl_mask |= ADP5520_R3_MODE;
  115. if (ctl_mask)
  116. ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
  117. ctl_mask);
  118. ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
  119. pdata->gpio_pullup_mask);
  120. if (ret) {
  121. dev_err(&pdev->dev, "failed to write\n");
  122. goto err;
  123. }
  124. ret = gpiochip_add(&dev->gpio_chip);
  125. if (ret)
  126. goto err;
  127. platform_set_drvdata(pdev, dev);
  128. return 0;
  129. err:
  130. kfree(dev);
  131. return ret;
  132. }
  133. static int __devexit adp5520_gpio_remove(struct platform_device *pdev)
  134. {
  135. struct adp5520_gpio *dev;
  136. int ret;
  137. dev = platform_get_drvdata(pdev);
  138. ret = gpiochip_remove(&dev->gpio_chip);
  139. if (ret) {
  140. dev_err(&pdev->dev, "%s failed, %d\n",
  141. "gpiochip_remove()", ret);
  142. return ret;
  143. }
  144. kfree(dev);
  145. return 0;
  146. }
  147. static struct platform_driver adp5520_gpio_driver = {
  148. .driver = {
  149. .name = "adp5520-gpio",
  150. .owner = THIS_MODULE,
  151. },
  152. .probe = adp5520_gpio_probe,
  153. .remove = __devexit_p(adp5520_gpio_remove),
  154. };
  155. module_platform_driver(adp5520_gpio_driver);
  156. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  157. MODULE_DESCRIPTION("GPIO ADP5520 Driver");
  158. MODULE_LICENSE("GPL");
  159. MODULE_ALIAS("platform:adp5520-gpio");