gpio-bd9571mwv.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * ROHM BD9571MWV-M GPIO driver
  3. *
  4. * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether expressed or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License version 2 for more details.
  14. *
  15. * Based on the TPS65086 driver
  16. *
  17. * NOTE: Interrupts are not supported yet.
  18. */
  19. #include <linux/gpio/driver.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/bd9571mwv.h>
  23. struct bd9571mwv_gpio {
  24. struct gpio_chip chip;
  25. struct bd9571mwv *bd;
  26. };
  27. static int bd9571mwv_gpio_get_direction(struct gpio_chip *chip,
  28. unsigned int offset)
  29. {
  30. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  31. int ret, val;
  32. ret = regmap_read(gpio->bd->regmap, BD9571MWV_GPIO_DIR, &val);
  33. if (ret < 0)
  34. return ret;
  35. return val & BIT(offset);
  36. }
  37. static int bd9571mwv_gpio_direction_input(struct gpio_chip *chip,
  38. unsigned int offset)
  39. {
  40. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  41. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_DIR,
  42. BIT(offset), 0);
  43. return 0;
  44. }
  45. static int bd9571mwv_gpio_direction_output(struct gpio_chip *chip,
  46. unsigned int offset, int value)
  47. {
  48. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  49. /* Set the initial value */
  50. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_OUT,
  51. BIT(offset), value ? BIT(offset) : 0);
  52. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_DIR,
  53. BIT(offset), BIT(offset));
  54. return 0;
  55. }
  56. static int bd9571mwv_gpio_get(struct gpio_chip *chip, unsigned int offset)
  57. {
  58. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  59. int ret, val;
  60. ret = regmap_read(gpio->bd->regmap, BD9571MWV_GPIO_IN, &val);
  61. if (ret < 0)
  62. return ret;
  63. return val & BIT(offset);
  64. }
  65. static void bd9571mwv_gpio_set(struct gpio_chip *chip, unsigned int offset,
  66. int value)
  67. {
  68. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  69. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_OUT,
  70. BIT(offset), value ? BIT(offset) : 0);
  71. }
  72. static const struct gpio_chip template_chip = {
  73. .label = "bd9571mwv-gpio",
  74. .owner = THIS_MODULE,
  75. .get_direction = bd9571mwv_gpio_get_direction,
  76. .direction_input = bd9571mwv_gpio_direction_input,
  77. .direction_output = bd9571mwv_gpio_direction_output,
  78. .get = bd9571mwv_gpio_get,
  79. .set = bd9571mwv_gpio_set,
  80. .base = -1,
  81. .ngpio = 2,
  82. .can_sleep = true,
  83. };
  84. static int bd9571mwv_gpio_probe(struct platform_device *pdev)
  85. {
  86. struct bd9571mwv_gpio *gpio;
  87. int ret;
  88. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  89. if (!gpio)
  90. return -ENOMEM;
  91. platform_set_drvdata(pdev, gpio);
  92. gpio->bd = dev_get_drvdata(pdev->dev.parent);
  93. gpio->chip = template_chip;
  94. gpio->chip.parent = gpio->bd->dev;
  95. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  96. if (ret < 0) {
  97. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  98. return ret;
  99. }
  100. return 0;
  101. }
  102. static const struct platform_device_id bd9571mwv_gpio_id_table[] = {
  103. { "bd9571mwv-gpio", },
  104. { /* sentinel */ }
  105. };
  106. MODULE_DEVICE_TABLE(platform, bd9571mwv_gpio_id_table);
  107. static struct platform_driver bd9571mwv_gpio_driver = {
  108. .driver = {
  109. .name = "bd9571mwv-gpio",
  110. },
  111. .probe = bd9571mwv_gpio_probe,
  112. .id_table = bd9571mwv_gpio_id_table,
  113. };
  114. module_platform_driver(bd9571mwv_gpio_driver);
  115. MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
  116. MODULE_DESCRIPTION("BD9571MWV GPIO driver");
  117. MODULE_LICENSE("GPL v2");