gpio-altera-a10sr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <linux/property.h>
  13. /**
  14. * struct altr_a10sr_gpio - Altera Max5 GPIO device private data structure
  15. * @gp: : instance of the gpio_chip
  16. * @regmap: the regmap from the parent device.
  17. */
  18. struct altr_a10sr_gpio {
  19. struct gpio_chip gp;
  20. struct regmap *regmap;
  21. };
  22. static int altr_a10sr_gpio_get(struct gpio_chip *chip, unsigned int offset)
  23. {
  24. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  25. int ret, val;
  26. ret = regmap_read(gpio->regmap, ALTR_A10SR_PBDSW_REG, &val);
  27. if (ret < 0)
  28. return ret;
  29. return !!(val & BIT(offset - ALTR_A10SR_LED_VALID_SHIFT));
  30. }
  31. static void altr_a10sr_gpio_set(struct gpio_chip *chip, unsigned int offset,
  32. int value)
  33. {
  34. struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
  35. regmap_update_bits(gpio->regmap, ALTR_A10SR_LED_REG,
  36. BIT(ALTR_A10SR_LED_VALID_SHIFT + offset),
  37. value ? BIT(ALTR_A10SR_LED_VALID_SHIFT + offset)
  38. : 0);
  39. }
  40. static int altr_a10sr_gpio_direction_input(struct gpio_chip *gc,
  41. unsigned int nr)
  42. {
  43. if (nr < (ALTR_A10SR_IN_VALID_RANGE_LO - ALTR_A10SR_LED_VALID_SHIFT))
  44. return -EINVAL;
  45. return 0;
  46. }
  47. static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
  48. unsigned int nr, int value)
  49. {
  50. if (nr > (ALTR_A10SR_OUT_VALID_RANGE_HI - ALTR_A10SR_LED_VALID_SHIFT))
  51. return -EINVAL;
  52. altr_a10sr_gpio_set(gc, nr, value);
  53. return 0;
  54. }
  55. static const struct gpio_chip altr_a10sr_gc = {
  56. .label = "altr_a10sr_gpio",
  57. .owner = THIS_MODULE,
  58. .get = altr_a10sr_gpio_get,
  59. .set = altr_a10sr_gpio_set,
  60. .direction_input = altr_a10sr_gpio_direction_input,
  61. .direction_output = altr_a10sr_gpio_direction_output,
  62. .can_sleep = true,
  63. .ngpio = 12,
  64. .base = -1,
  65. };
  66. static int altr_a10sr_gpio_probe(struct platform_device *pdev)
  67. {
  68. struct altr_a10sr_gpio *gpio;
  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.fwnode = dev_fwnode(&pdev->dev);
  77. return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
  78. }
  79. static const struct of_device_id altr_a10sr_gpio_of_match[] = {
  80. { .compatible = "altr,a10sr-gpio" },
  81. { },
  82. };
  83. MODULE_DEVICE_TABLE(of, altr_a10sr_gpio_of_match);
  84. static struct platform_driver altr_a10sr_gpio_driver = {
  85. .probe = altr_a10sr_gpio_probe,
  86. .driver = {
  87. .name = "altr_a10sr_gpio",
  88. .of_match_table = of_match_ptr(altr_a10sr_gpio_of_match),
  89. },
  90. };
  91. module_platform_driver(altr_a10sr_gpio_driver);
  92. MODULE_LICENSE("GPL v2");
  93. MODULE_AUTHOR("Thor Thayer <[email protected]>");
  94. MODULE_DESCRIPTION("Altera Arria10 System Resource Chip GPIO");