adp5520-gpio.c 4.6 KB

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