gpio-arizona.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * gpiolib support for Wolfson Arizona class devices
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/gpio.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/mfd/arizona/core.h>
  21. #include <linux/mfd/arizona/pdata.h>
  22. #include <linux/mfd/arizona/registers.h>
  23. struct arizona_gpio {
  24. struct arizona *arizona;
  25. struct gpio_chip gpio_chip;
  26. };
  27. static inline struct arizona_gpio *to_arizona_gpio(struct gpio_chip *chip)
  28. {
  29. return container_of(chip, struct arizona_gpio, gpio_chip);
  30. }
  31. static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  32. {
  33. struct arizona_gpio *arizona_gpio = to_arizona_gpio(chip);
  34. struct arizona *arizona = arizona_gpio->arizona;
  35. return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  36. ARIZONA_GPN_DIR, ARIZONA_GPN_DIR);
  37. }
  38. static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
  39. {
  40. struct arizona_gpio *arizona_gpio = to_arizona_gpio(chip);
  41. struct arizona *arizona = arizona_gpio->arizona;
  42. unsigned int val;
  43. int ret;
  44. ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
  45. if (ret < 0)
  46. return ret;
  47. if (val & ARIZONA_GPN_LVL)
  48. return 1;
  49. else
  50. return 0;
  51. }
  52. static int arizona_gpio_direction_out(struct gpio_chip *chip,
  53. unsigned offset, int value)
  54. {
  55. struct arizona_gpio *arizona_gpio = to_arizona_gpio(chip);
  56. struct arizona *arizona = arizona_gpio->arizona;
  57. if (value)
  58. value = ARIZONA_GPN_LVL;
  59. return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  60. ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
  61. }
  62. static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  63. {
  64. struct arizona_gpio *arizona_gpio = to_arizona_gpio(chip);
  65. struct arizona *arizona = arizona_gpio->arizona;
  66. if (value)
  67. value = ARIZONA_GPN_LVL;
  68. regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  69. ARIZONA_GPN_LVL, value);
  70. }
  71. static struct gpio_chip template_chip = {
  72. .label = "arizona",
  73. .owner = THIS_MODULE,
  74. .direction_input = arizona_gpio_direction_in,
  75. .get = arizona_gpio_get,
  76. .direction_output = arizona_gpio_direction_out,
  77. .set = arizona_gpio_set,
  78. .can_sleep = 1,
  79. };
  80. static int __devinit arizona_gpio_probe(struct platform_device *pdev)
  81. {
  82. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  83. struct arizona_pdata *pdata = arizona->dev->platform_data;
  84. struct arizona_gpio *arizona_gpio;
  85. int ret;
  86. arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
  87. GFP_KERNEL);
  88. if (arizona_gpio == NULL)
  89. return -ENOMEM;
  90. arizona_gpio->arizona = arizona;
  91. arizona_gpio->gpio_chip = template_chip;
  92. arizona_gpio->gpio_chip.dev = &pdev->dev;
  93. switch (arizona->type) {
  94. case WM5102:
  95. case WM5110:
  96. arizona_gpio->gpio_chip.ngpio = 5;
  97. break;
  98. default:
  99. dev_err(&pdev->dev, "Unknown chip variant %d\n",
  100. arizona->type);
  101. return -EINVAL;
  102. }
  103. if (pdata && pdata->gpio_base)
  104. arizona_gpio->gpio_chip.base = pdata->gpio_base;
  105. else
  106. arizona_gpio->gpio_chip.base = -1;
  107. ret = gpiochip_add(&arizona_gpio->gpio_chip);
  108. if (ret < 0) {
  109. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  110. ret);
  111. goto err;
  112. }
  113. platform_set_drvdata(pdev, arizona_gpio);
  114. return ret;
  115. err:
  116. return ret;
  117. }
  118. static int __devexit arizona_gpio_remove(struct platform_device *pdev)
  119. {
  120. struct arizona_gpio *arizona_gpio = platform_get_drvdata(pdev);
  121. return gpiochip_remove(&arizona_gpio->gpio_chip);
  122. }
  123. static struct platform_driver arizona_gpio_driver = {
  124. .driver.name = "arizona-gpio",
  125. .driver.owner = THIS_MODULE,
  126. .probe = arizona_gpio_probe,
  127. .remove = __devexit_p(arizona_gpio_remove),
  128. };
  129. module_platform_driver(arizona_gpio_driver);
  130. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  131. MODULE_DESCRIPTION("GPIO interface for Arizona devices");
  132. MODULE_LICENSE("GPL");
  133. MODULE_ALIAS("platform:arizona-gpio");