gpio-bd9571mwv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM BD9571MWV-M and BD9574MWF-M GPIO driver
  4. *
  5. * Copyright (C) 2017 Marek Vasut <[email protected]>
  6. *
  7. * Based on the TPS65086 driver
  8. *
  9. * NOTE: Interrupts are not supported yet.
  10. */
  11. #include <linux/gpio/driver.h>
  12. #include <linux/mfd/rohm-generic.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mfd/bd9571mwv.h>
  16. struct bd9571mwv_gpio {
  17. struct regmap *regmap;
  18. struct gpio_chip chip;
  19. };
  20. static int bd9571mwv_gpio_get_direction(struct gpio_chip *chip,
  21. unsigned int offset)
  22. {
  23. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  24. int ret, val;
  25. ret = regmap_read(gpio->regmap, BD9571MWV_GPIO_DIR, &val);
  26. if (ret < 0)
  27. return ret;
  28. if (val & BIT(offset))
  29. return GPIO_LINE_DIRECTION_IN;
  30. return GPIO_LINE_DIRECTION_OUT;
  31. }
  32. static int bd9571mwv_gpio_direction_input(struct gpio_chip *chip,
  33. unsigned int offset)
  34. {
  35. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  36. regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_DIR, BIT(offset), 0);
  37. return 0;
  38. }
  39. static int bd9571mwv_gpio_direction_output(struct gpio_chip *chip,
  40. unsigned int offset, int value)
  41. {
  42. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  43. /* Set the initial value */
  44. regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_OUT,
  45. BIT(offset), value ? BIT(offset) : 0);
  46. regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_DIR,
  47. BIT(offset), BIT(offset));
  48. return 0;
  49. }
  50. static int bd9571mwv_gpio_get(struct gpio_chip *chip, unsigned int offset)
  51. {
  52. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  53. int ret, val;
  54. ret = regmap_read(gpio->regmap, BD9571MWV_GPIO_IN, &val);
  55. if (ret < 0)
  56. return ret;
  57. return val & BIT(offset);
  58. }
  59. static void bd9571mwv_gpio_set(struct gpio_chip *chip, unsigned int offset,
  60. int value)
  61. {
  62. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  63. regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_OUT,
  64. BIT(offset), value ? BIT(offset) : 0);
  65. }
  66. static const struct gpio_chip template_chip = {
  67. .label = "bd9571mwv-gpio",
  68. .owner = THIS_MODULE,
  69. .get_direction = bd9571mwv_gpio_get_direction,
  70. .direction_input = bd9571mwv_gpio_direction_input,
  71. .direction_output = bd9571mwv_gpio_direction_output,
  72. .get = bd9571mwv_gpio_get,
  73. .set = bd9571mwv_gpio_set,
  74. .base = -1,
  75. .ngpio = 2,
  76. .can_sleep = true,
  77. };
  78. static int bd9571mwv_gpio_probe(struct platform_device *pdev)
  79. {
  80. struct bd9571mwv_gpio *gpio;
  81. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  82. if (!gpio)
  83. return -ENOMEM;
  84. gpio->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  85. gpio->chip = template_chip;
  86. gpio->chip.parent = pdev->dev.parent;
  87. return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  88. }
  89. static const struct platform_device_id bd9571mwv_gpio_id_table[] = {
  90. { "bd9571mwv-gpio", ROHM_CHIP_TYPE_BD9571 },
  91. { "bd9574mwf-gpio", ROHM_CHIP_TYPE_BD9574 },
  92. { /* sentinel */ }
  93. };
  94. MODULE_DEVICE_TABLE(platform, bd9571mwv_gpio_id_table);
  95. static struct platform_driver bd9571mwv_gpio_driver = {
  96. .driver = {
  97. .name = "bd9571mwv-gpio",
  98. },
  99. .probe = bd9571mwv_gpio_probe,
  100. .id_table = bd9571mwv_gpio_id_table,
  101. };
  102. module_platform_driver(bd9571mwv_gpio_driver);
  103. MODULE_AUTHOR("Marek Vasut <[email protected]>");
  104. MODULE_DESCRIPTION("BD9571MWV GPIO driver");
  105. MODULE_LICENSE("GPL v2");