gpio-altera-a10sr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright Intel Corporation (C) 2014-2016. All Rights Reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * GPIO driver for Altera Arria10 MAX5 System Resource Chip
  17. *
  18. * Adapted from gpio-tps65910.c
  19. */
  20. #include <linux/gpio/driver.h>
  21. #include <linux/mfd/altera-a10sr.h>
  22. #include <linux/module.h>
  23. /**
  24. * struct altr_a10sr_gpio - Altera Max5 GPIO device private data structure
  25. * @gp: : instance of the gpio_chip
  26. * @regmap: the regmap from the parent device.
  27. */
  28. struct altr_a10sr_gpio {
  29. struct gpio_chip gp;
  30. struct regmap *regmap;
  31. };
  32. static int altr_a10sr_gpio_get(struct gpio_chip *chip, unsigned int offset)
  33. {
  34. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  35. int ret, val;
  36. ret = regmap_read(gpio->regmap, ALTR_A10SR_PBDSW_REG, &val);
  37. if (ret < 0)
  38. return ret;
  39. return !!(val & BIT(offset - ALTR_A10SR_LED_VALID_SHIFT));
  40. }
  41. static void altr_a10sr_gpio_set(struct gpio_chip *chip, unsigned int offset,
  42. int value)
  43. {
  44. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  45. regmap_update_bits(gpio->regmap, ALTR_A10SR_LED_REG,
  46. BIT(ALTR_A10SR_LED_VALID_SHIFT + offset),
  47. value ? BIT(ALTR_A10SR_LED_VALID_SHIFT + offset)
  48. : 0);
  49. }
  50. static int altr_a10sr_gpio_direction_input(struct gpio_chip *gc,
  51. unsigned int nr)
  52. {
  53. if (nr >= (ALTR_A10SR_IN_VALID_RANGE_LO - ALTR_A10SR_LED_VALID_SHIFT))
  54. return 0;
  55. return -EINVAL;
  56. }
  57. static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
  58. unsigned int nr, int value)
  59. {
  60. if (nr <= (ALTR_A10SR_OUT_VALID_RANGE_HI - ALTR_A10SR_LED_VALID_SHIFT))
  61. return 0;
  62. return -EINVAL;
  63. }
  64. static struct gpio_chip altr_a10sr_gc = {
  65. .label = "altr_a10sr_gpio",
  66. .owner = THIS_MODULE,
  67. .get = altr_a10sr_gpio_get,
  68. .set = altr_a10sr_gpio_set,
  69. .direction_input = altr_a10sr_gpio_direction_input,
  70. .direction_output = altr_a10sr_gpio_direction_output,
  71. .can_sleep = true,
  72. .ngpio = 12,
  73. .base = -1,
  74. };
  75. static int altr_a10sr_gpio_probe(struct platform_device *pdev)
  76. {
  77. struct altr_a10sr_gpio *gpio;
  78. int ret;
  79. struct altr_a10sr *a10sr = dev_get_drvdata(pdev->dev.parent);
  80. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  81. if (!gpio)
  82. return -ENOMEM;
  83. gpio->regmap = a10sr->regmap;
  84. gpio->gp = altr_a10sr_gc;
  85. gpio->gp.of_node = pdev->dev.of_node;
  86. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
  87. if (ret < 0) {
  88. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  89. return ret;
  90. }
  91. platform_set_drvdata(pdev, gpio);
  92. return 0;
  93. }
  94. static const struct of_device_id altr_a10sr_gpio_of_match[] = {
  95. { .compatible = "altr,a10sr-gpio" },
  96. { },
  97. };
  98. MODULE_DEVICE_TABLE(of, altr_a10sr_gpio_of_match);
  99. static struct platform_driver altr_a10sr_gpio_driver = {
  100. .probe = altr_a10sr_gpio_probe,
  101. .driver = {
  102. .name = "altr_a10sr_gpio",
  103. .of_match_table = of_match_ptr(altr_a10sr_gpio_of_match),
  104. },
  105. };
  106. module_platform_driver(altr_a10sr_gpio_driver);
  107. MODULE_LICENSE("GPL v2");
  108. MODULE_AUTHOR("Thor Thayer <tthayer@opensource.altera.com>");
  109. MODULE_DESCRIPTION("Altera Arria10 System Resource Chip GPIO");