gpio-altera-a10sr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright Intel Corporation (C) 2014-2016. All Rights Reserved
  4. *
  5. * GPIO driver for Altera Arria10 MAX5 System Resource Chip
  6. *
  7. * Adapted from gpio-tps65910.c
  8. */
  9. #include <linux/gpio/driver.h>
  10. #include <linux/mfd/altera-a10sr.h>
  11. #include <linux/module.h>
  12. /**
  13. * struct altr_a10sr_gpio - Altera Max5 GPIO device private data structure
  14. * @gp: : instance of the gpio_chip
  15. * @regmap: the regmap from the parent device.
  16. */
  17. struct altr_a10sr_gpio {
  18. struct gpio_chip gp;
  19. struct regmap *regmap;
  20. };
  21. static int altr_a10sr_gpio_get(struct gpio_chip *chip, unsigned int offset)
  22. {
  23. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  24. int ret, val;
  25. ret = regmap_read(gpio->regmap, ALTR_A10SR_PBDSW_REG, &val);
  26. if (ret < 0)
  27. return ret;
  28. return !!(val & BIT(offset - ALTR_A10SR_LED_VALID_SHIFT));
  29. }
  30. static void altr_a10sr_gpio_set(struct gpio_chip *chip, unsigned int offset,
  31. int value)
  32. {
  33. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  34. regmap_update_bits(gpio->regmap, ALTR_A10SR_LED_REG,
  35. BIT(ALTR_A10SR_LED_VALID_SHIFT + offset),
  36. value ? BIT(ALTR_A10SR_LED_VALID_SHIFT + offset)
  37. : 0);
  38. }
  39. static int altr_a10sr_gpio_direction_input(struct gpio_chip *gc,
  40. unsigned int nr)
  41. {
  42. if (nr < (ALTR_A10SR_IN_VALID_RANGE_LO - ALTR_A10SR_LED_VALID_SHIFT))
  43. return -EINVAL;
  44. return 0;
  45. }
  46. static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
  47. unsigned int nr, int value)
  48. {
  49. if (nr > (ALTR_A10SR_OUT_VALID_RANGE_HI - ALTR_A10SR_LED_VALID_SHIFT))
  50. return -EINVAL;
  51. altr_a10sr_gpio_set(gc, nr, value);
  52. return 0;
  53. }
  54. static const struct gpio_chip altr_a10sr_gc = {
  55. .label = "altr_a10sr_gpio",
  56. .owner = THIS_MODULE,
  57. .get = altr_a10sr_gpio_get,
  58. .set = altr_a10sr_gpio_set,
  59. .direction_input = altr_a10sr_gpio_direction_input,
  60. .direction_output = altr_a10sr_gpio_direction_output,
  61. .can_sleep = true,
  62. .ngpio = 12,
  63. .base = -1,
  64. };
  65. static int altr_a10sr_gpio_probe(struct platform_device *pdev)
  66. {
  67. struct altr_a10sr_gpio *gpio;
  68. int ret;
  69. struct altr_a10sr *a10sr = dev_get_drvdata(pdev->dev.parent);
  70. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  71. if (!gpio)
  72. return -ENOMEM;
  73. gpio->regmap = a10sr->regmap;
  74. gpio->gp = altr_a10sr_gc;
  75. gpio->gp.parent = pdev->dev.parent;
  76. gpio->gp.of_node = pdev->dev.of_node;
  77. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
  78. if (ret < 0) {
  79. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  80. return ret;
  81. }
  82. platform_set_drvdata(pdev, gpio);
  83. return 0;
  84. }
  85. static const struct of_device_id altr_a10sr_gpio_of_match[] = {
  86. { .compatible = "altr,a10sr-gpio" },
  87. { },
  88. };
  89. MODULE_DEVICE_TABLE(of, altr_a10sr_gpio_of_match);
  90. static struct platform_driver altr_a10sr_gpio_driver = {
  91. .probe = altr_a10sr_gpio_probe,
  92. .driver = {
  93. .name = "altr_a10sr_gpio",
  94. .of_match_table = of_match_ptr(altr_a10sr_gpio_of_match),
  95. },
  96. };
  97. module_platform_driver(altr_a10sr_gpio_driver);
  98. MODULE_LICENSE("GPL v2");
  99. MODULE_AUTHOR("Thor Thayer <tthayer@opensource.altera.com>");
  100. MODULE_DESCRIPTION("Altera Arria10 System Resource Chip GPIO");