gpio-arizona.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  28. {
  29. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  30. struct arizona *arizona = arizona_gpio->arizona;
  31. return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  32. ARIZONA_GPN_DIR, ARIZONA_GPN_DIR);
  33. }
  34. static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
  35. {
  36. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  37. struct arizona *arizona = arizona_gpio->arizona;
  38. unsigned int val;
  39. int ret;
  40. ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
  41. if (ret < 0)
  42. return ret;
  43. if (val & ARIZONA_GPN_LVL)
  44. return 1;
  45. else
  46. return 0;
  47. }
  48. static int arizona_gpio_direction_out(struct gpio_chip *chip,
  49. unsigned offset, int value)
  50. {
  51. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  52. struct arizona *arizona = arizona_gpio->arizona;
  53. if (value)
  54. value = ARIZONA_GPN_LVL;
  55. return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  56. ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
  57. }
  58. static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  59. {
  60. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  61. struct arizona *arizona = arizona_gpio->arizona;
  62. if (value)
  63. value = ARIZONA_GPN_LVL;
  64. regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  65. ARIZONA_GPN_LVL, value);
  66. }
  67. static const struct gpio_chip template_chip = {
  68. .label = "arizona",
  69. .owner = THIS_MODULE,
  70. .direction_input = arizona_gpio_direction_in,
  71. .get = arizona_gpio_get,
  72. .direction_output = arizona_gpio_direction_out,
  73. .set = arizona_gpio_set,
  74. .can_sleep = true,
  75. };
  76. static int arizona_gpio_probe(struct platform_device *pdev)
  77. {
  78. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  79. struct arizona_pdata *pdata = dev_get_platdata(arizona->dev);
  80. struct arizona_gpio *arizona_gpio;
  81. int ret;
  82. arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
  83. GFP_KERNEL);
  84. if (!arizona_gpio)
  85. return -ENOMEM;
  86. arizona_gpio->arizona = arizona;
  87. arizona_gpio->gpio_chip = template_chip;
  88. arizona_gpio->gpio_chip.parent = &pdev->dev;
  89. #ifdef CONFIG_OF_GPIO
  90. arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
  91. #endif
  92. switch (arizona->type) {
  93. case WM5102:
  94. case WM5110:
  95. case WM8280:
  96. case WM8997:
  97. case WM8998:
  98. case WM1814:
  99. arizona_gpio->gpio_chip.ngpio = 5;
  100. break;
  101. case WM1831:
  102. case CS47L24:
  103. arizona_gpio->gpio_chip.ngpio = 2;
  104. break;
  105. default:
  106. dev_err(&pdev->dev, "Unknown chip variant %d\n",
  107. arizona->type);
  108. return -EINVAL;
  109. }
  110. if (pdata && pdata->gpio_base)
  111. arizona_gpio->gpio_chip.base = pdata->gpio_base;
  112. else
  113. arizona_gpio->gpio_chip.base = -1;
  114. ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
  115. arizona_gpio);
  116. if (ret < 0) {
  117. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  118. ret);
  119. goto err;
  120. }
  121. platform_set_drvdata(pdev, arizona_gpio);
  122. return ret;
  123. err:
  124. return ret;
  125. }
  126. static struct platform_driver arizona_gpio_driver = {
  127. .driver.name = "arizona-gpio",
  128. .probe = arizona_gpio_probe,
  129. };
  130. module_platform_driver(arizona_gpio_driver);
  131. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  132. MODULE_DESCRIPTION("GPIO interface for Arizona devices");
  133. MODULE_LICENSE("GPL");
  134. MODULE_ALIAS("platform:arizona-gpio");