reset-gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/auxiliary_bus.h>
  3. #include <linux/gpio/consumer.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/reset-controller.h>
  8. struct reset_gpio_priv {
  9. struct reset_controller_dev rc;
  10. struct gpio_desc *reset;
  11. };
  12. static inline struct reset_gpio_priv
  13. *rc_to_reset_gpio(struct reset_controller_dev *rc)
  14. {
  15. return container_of(rc, struct reset_gpio_priv, rc);
  16. }
  17. static int reset_gpio_assert(struct reset_controller_dev *rc, unsigned long id)
  18. {
  19. struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
  20. return gpiod_set_value_cansleep(priv->reset, 1);
  21. }
  22. static int reset_gpio_deassert(struct reset_controller_dev *rc,
  23. unsigned long id)
  24. {
  25. struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
  26. return gpiod_set_value_cansleep(priv->reset, 0);
  27. }
  28. static int reset_gpio_status(struct reset_controller_dev *rc, unsigned long id)
  29. {
  30. struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
  31. return gpiod_get_value_cansleep(priv->reset);
  32. }
  33. static const struct reset_control_ops reset_gpio_ops = {
  34. .assert = reset_gpio_assert,
  35. .deassert = reset_gpio_deassert,
  36. .status = reset_gpio_status,
  37. };
  38. static int reset_gpio_of_xlate(struct reset_controller_dev *rcdev,
  39. const struct of_phandle_args *reset_spec)
  40. {
  41. return reset_spec->args[0];
  42. }
  43. static void reset_gpio_of_node_put(void *data)
  44. {
  45. of_node_put(data);
  46. }
  47. static int reset_gpio_probe(struct auxiliary_device *adev,
  48. const struct auxiliary_device_id *id)
  49. {
  50. struct device *dev = &adev->dev;
  51. struct of_phandle_args *platdata = dev_get_platdata(dev);
  52. struct reset_gpio_priv *priv;
  53. int ret;
  54. if (!platdata)
  55. return -EINVAL;
  56. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  57. if (!priv)
  58. return -ENOMEM;
  59. auxiliary_set_drvdata(adev, &priv->rc);
  60. priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  61. if (IS_ERR(priv->reset))
  62. return dev_err_probe(dev, PTR_ERR(priv->reset),
  63. "Could not get reset gpios\n");
  64. priv->rc.ops = &reset_gpio_ops;
  65. priv->rc.owner = THIS_MODULE;
  66. priv->rc.dev = dev;
  67. priv->rc.of_args = platdata;
  68. ret = devm_add_action_or_reset(dev, reset_gpio_of_node_put,
  69. priv->rc.of_node);
  70. if (ret)
  71. return ret;
  72. /* Cells to match GPIO specifier, but it's not really used */
  73. priv->rc.of_reset_n_cells = 2;
  74. priv->rc.of_xlate = reset_gpio_of_xlate;
  75. priv->rc.nr_resets = 1;
  76. return devm_reset_controller_register(dev, &priv->rc);
  77. }
  78. static const struct auxiliary_device_id reset_gpio_ids[] = {
  79. { .name = "reset.gpio" },
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(auxiliary, reset_gpio_ids);
  83. static struct auxiliary_driver reset_gpio_driver = {
  84. .probe = reset_gpio_probe,
  85. .id_table = reset_gpio_ids,
  86. .driver = {
  87. .name = "reset-gpio",
  88. .suppress_bind_attrs = true,
  89. },
  90. };
  91. module_auxiliary_driver(reset_gpio_driver);
  92. MODULE_AUTHOR("Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>");
  93. MODULE_DESCRIPTION("Generic GPIO reset driver");
  94. MODULE_LICENSE("GPL");