gpio-arizona.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = true,
  79. };
  80. static int arizona_gpio_probe(struct platform_device *pdev)
  81. {
  82. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  83. struct arizona_pdata *pdata = dev_get_platdata(arizona->dev);
  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)
  89. return -ENOMEM;
  90. arizona_gpio->arizona = arizona;
  91. arizona_gpio->gpio_chip = template_chip;
  92. arizona_gpio->gpio_chip.dev = &pdev->dev;
  93. #ifdef CONFIG_OF_GPIO
  94. arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
  95. #endif
  96. switch (arizona->type) {
  97. case WM5102:
  98. case WM5110:
  99. case WM8280:
  100. case WM8997:
  101. arizona_gpio->gpio_chip.ngpio = 5;
  102. break;
  103. default:
  104. dev_err(&pdev->dev, "Unknown chip variant %d\n",
  105. arizona->type);
  106. return -EINVAL;
  107. }
  108. if (pdata && pdata->gpio_base)
  109. arizona_gpio->gpio_chip.base = pdata->gpio_base;
  110. else
  111. arizona_gpio->gpio_chip.base = -1;
  112. ret = gpiochip_add(&arizona_gpio->gpio_chip);
  113. if (ret < 0) {
  114. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  115. ret);
  116. goto err;
  117. }
  118. platform_set_drvdata(pdev, arizona_gpio);
  119. return ret;
  120. err:
  121. return ret;
  122. }
  123. static int arizona_gpio_remove(struct platform_device *pdev)
  124. {
  125. struct arizona_gpio *arizona_gpio = platform_get_drvdata(pdev);
  126. gpiochip_remove(&arizona_gpio->gpio_chip);
  127. return 0;
  128. }
  129. static struct platform_driver arizona_gpio_driver = {
  130. .driver.name = "arizona-gpio",
  131. .probe = arizona_gpio_probe,
  132. .remove = arizona_gpio_remove,
  133. };
  134. module_platform_driver(arizona_gpio_driver);
  135. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  136. MODULE_DESCRIPTION("GPIO interface for Arizona devices");
  137. MODULE_LICENSE("GPL");
  138. MODULE_ALIAS("platform:arizona-gpio");