gpio-bd9571mwv.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (val & BIT(offset))
  36. return GPIO_LINE_DIRECTION_IN;
  37. return GPIO_LINE_DIRECTION_OUT;
  38. }
  39. static int bd9571mwv_gpio_direction_input(struct gpio_chip *chip,
  40. unsigned int offset)
  41. {
  42. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  43. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_DIR,
  44. BIT(offset), 0);
  45. return 0;
  46. }
  47. static int bd9571mwv_gpio_direction_output(struct gpio_chip *chip,
  48. unsigned int offset, int value)
  49. {
  50. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  51. /* Set the initial value */
  52. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_OUT,
  53. BIT(offset), value ? BIT(offset) : 0);
  54. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_DIR,
  55. BIT(offset), BIT(offset));
  56. return 0;
  57. }
  58. static int bd9571mwv_gpio_get(struct gpio_chip *chip, unsigned int offset)
  59. {
  60. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  61. int ret, val;
  62. ret = regmap_read(gpio->bd->regmap, BD9571MWV_GPIO_IN, &val);
  63. if (ret < 0)
  64. return ret;
  65. return val & BIT(offset);
  66. }
  67. static void bd9571mwv_gpio_set(struct gpio_chip *chip, unsigned int offset,
  68. int value)
  69. {
  70. struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
  71. regmap_update_bits(gpio->bd->regmap, BD9571MWV_GPIO_OUT,
  72. BIT(offset), value ? BIT(offset) : 0);
  73. }
  74. static const struct gpio_chip template_chip = {
  75. .label = "bd9571mwv-gpio",
  76. .owner = THIS_MODULE,
  77. .get_direction = bd9571mwv_gpio_get_direction,
  78. .direction_input = bd9571mwv_gpio_direction_input,
  79. .direction_output = bd9571mwv_gpio_direction_output,
  80. .get = bd9571mwv_gpio_get,
  81. .set = bd9571mwv_gpio_set,
  82. .base = -1,
  83. .ngpio = 2,
  84. .can_sleep = true,
  85. };
  86. static int bd9571mwv_gpio_probe(struct platform_device *pdev)
  87. {
  88. struct bd9571mwv_gpio *gpio;
  89. int ret;
  90. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  91. if (!gpio)
  92. return -ENOMEM;
  93. platform_set_drvdata(pdev, gpio);
  94. gpio->bd = dev_get_drvdata(pdev->dev.parent);
  95. gpio->chip = template_chip;
  96. gpio->chip.parent = gpio->bd->dev;
  97. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  98. if (ret < 0) {
  99. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  100. return ret;
  101. }
  102. return 0;
  103. }
  104. static const struct platform_device_id bd9571mwv_gpio_id_table[] = {
  105. { "bd9571mwv-gpio", },
  106. { /* sentinel */ }
  107. };
  108. MODULE_DEVICE_TABLE(platform, bd9571mwv_gpio_id_table);
  109. static struct platform_driver bd9571mwv_gpio_driver = {
  110. .driver = {
  111. .name = "bd9571mwv-gpio",
  112. },
  113. .probe = bd9571mwv_gpio_probe,
  114. .id_table = bd9571mwv_gpio_id_table,
  115. };
  116. module_platform_driver(bd9571mwv_gpio_driver);
  117. MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
  118. MODULE_DESCRIPTION("BD9571MWV GPIO driver");
  119. MODULE_LICENSE("GPL v2");