adp5520-gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, GPIO_OUT, &reg_val);
  31. else
  32. adp5520_read(dev->master, 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, GPIO_OUT, dev->lut[off]);
  42. else
  43. adp5520_clr_bits(dev->master, 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, GPIO_CFG_2, dev->lut[off]);
  51. }
  52. static int adp5520_gpio_direction_output(struct gpio_chip *chip,
  53. unsigned off, int val)
  54. {
  55. struct adp5520_gpio *dev;
  56. int ret = 0;
  57. dev = container_of(chip, struct adp5520_gpio, gpio_chip);
  58. set_bit(off, &dev->output);
  59. if (val)
  60. ret |= adp5520_set_bits(dev->master, GPIO_OUT, dev->lut[off]);
  61. else
  62. ret |= adp5520_clr_bits(dev->master, GPIO_OUT, dev->lut[off]);
  63. ret |= adp5520_set_bits(dev->master, GPIO_CFG_2, dev->lut[off]);
  64. return ret;
  65. }
  66. static int __devinit adp5520_gpio_probe(struct platform_device *pdev)
  67. {
  68. struct adp5520_gpio_platfrom_data *pdata = pdev->dev.platform_data;
  69. struct adp5520_gpio *dev;
  70. struct gpio_chip *gc;
  71. int ret, i, gpios;
  72. unsigned char ctl_mask = 0;
  73. if (pdata == NULL) {
  74. dev_err(&pdev->dev, "missing platform data\n");
  75. return -ENODEV;
  76. }
  77. if (pdev->id != ID_ADP5520) {
  78. dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
  79. return -ENODEV;
  80. }
  81. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  82. if (dev == NULL) {
  83. dev_err(&pdev->dev, "failed to alloc memory\n");
  84. return -ENOMEM;
  85. }
  86. dev->master = pdev->dev.parent;
  87. for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
  88. if (pdata->gpio_en_mask & (1 << i))
  89. dev->lut[gpios++] = 1 << i;
  90. if (gpios < 1) {
  91. ret = -EINVAL;
  92. goto err;
  93. }
  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 = 1;
  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, GPIO_CFG_1,
  105. pdata->gpio_en_mask);
  106. if (pdata->gpio_en_mask & GPIO_C3)
  107. ctl_mask |= C3_MODE;
  108. if (pdata->gpio_en_mask & GPIO_R3)
  109. ctl_mask |= R3_MODE;
  110. if (ctl_mask)
  111. ret = adp5520_set_bits(dev->master, LED_CONTROL,
  112. ctl_mask);
  113. ret |= adp5520_set_bits(dev->master, GPIO_PULLUP,
  114. pdata->gpio_pullup_mask);
  115. if (ret) {
  116. dev_err(&pdev->dev, "failed to write\n");
  117. goto err;
  118. }
  119. ret = gpiochip_add(&dev->gpio_chip);
  120. if (ret)
  121. goto err;
  122. platform_set_drvdata(pdev, dev);
  123. return 0;
  124. err:
  125. kfree(dev);
  126. return ret;
  127. }
  128. static int __devexit adp5520_gpio_remove(struct platform_device *pdev)
  129. {
  130. struct adp5520_gpio *dev;
  131. int ret;
  132. dev = platform_get_drvdata(pdev);
  133. ret = gpiochip_remove(&dev->gpio_chip);
  134. if (ret) {
  135. dev_err(&pdev->dev, "%s failed, %d\n",
  136. "gpiochip_remove()", ret);
  137. return ret;
  138. }
  139. kfree(dev);
  140. return 0;
  141. }
  142. static struct platform_driver adp5520_gpio_driver = {
  143. .driver = {
  144. .name = "adp5520-gpio",
  145. .owner = THIS_MODULE,
  146. },
  147. .probe = adp5520_gpio_probe,
  148. .remove = __devexit_p(adp5520_gpio_remove),
  149. };
  150. static int __init adp5520_gpio_init(void)
  151. {
  152. return platform_driver_register(&adp5520_gpio_driver);
  153. }
  154. module_init(adp5520_gpio_init);
  155. static void __exit adp5520_gpio_exit(void)
  156. {
  157. platform_driver_unregister(&adp5520_gpio_driver);
  158. }
  159. module_exit(adp5520_gpio_exit);
  160. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  161. MODULE_DESCRIPTION("GPIO ADP5520 Driver");
  162. MODULE_LICENSE("GPL");
  163. MODULE_ALIAS("platform:adp5520-gpio");