reset-tps380x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TI TPS380x Supply Voltage Supervisor and Reset Controller Driver
  4. *
  5. * Copyright (C) 2022 Pengutronix, Marco Felsch <[email protected]>
  6. *
  7. * Based on Simple Reset Controller Driver
  8. *
  9. * Copyright (C) 2017 Pengutronix, Philipp Zabel <[email protected]>
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <linux/reset-controller.h>
  18. struct tps380x_reset {
  19. struct reset_controller_dev rcdev;
  20. struct gpio_desc *reset_gpio;
  21. unsigned int reset_ms;
  22. };
  23. struct tps380x_reset_devdata {
  24. unsigned int min_reset_ms;
  25. unsigned int typ_reset_ms;
  26. unsigned int max_reset_ms;
  27. };
  28. static inline
  29. struct tps380x_reset *to_tps380x_reset(struct reset_controller_dev *rcdev)
  30. {
  31. return container_of(rcdev, struct tps380x_reset, rcdev);
  32. }
  33. static int
  34. tps380x_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
  35. {
  36. struct tps380x_reset *tps380x = to_tps380x_reset(rcdev);
  37. gpiod_set_value_cansleep(tps380x->reset_gpio, 1);
  38. return 0;
  39. }
  40. static int
  41. tps380x_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  42. {
  43. struct tps380x_reset *tps380x = to_tps380x_reset(rcdev);
  44. gpiod_set_value_cansleep(tps380x->reset_gpio, 0);
  45. msleep(tps380x->reset_ms);
  46. return 0;
  47. }
  48. static const struct reset_control_ops reset_tps380x_ops = {
  49. .assert = tps380x_reset_assert,
  50. .deassert = tps380x_reset_deassert,
  51. };
  52. static int tps380x_reset_of_xlate(struct reset_controller_dev *rcdev,
  53. const struct of_phandle_args *reset_spec)
  54. {
  55. /* No special handling needed, we have only one reset line per device */
  56. return 0;
  57. }
  58. static int tps380x_reset_probe(struct platform_device *pdev)
  59. {
  60. struct device *dev = &pdev->dev;
  61. const struct tps380x_reset_devdata *devdata;
  62. struct tps380x_reset *tps380x;
  63. devdata = device_get_match_data(dev);
  64. if (!devdata)
  65. return -EINVAL;
  66. tps380x = devm_kzalloc(dev, sizeof(*tps380x), GFP_KERNEL);
  67. if (!tps380x)
  68. return -ENOMEM;
  69. tps380x->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  70. if (IS_ERR(tps380x->reset_gpio))
  71. return dev_err_probe(dev, PTR_ERR(tps380x->reset_gpio),
  72. "Failed to get GPIO\n");
  73. tps380x->reset_ms = devdata->max_reset_ms;
  74. tps380x->rcdev.ops = &reset_tps380x_ops;
  75. tps380x->rcdev.owner = THIS_MODULE;
  76. tps380x->rcdev.dev = dev;
  77. tps380x->rcdev.of_node = dev->of_node;
  78. tps380x->rcdev.of_reset_n_cells = 0;
  79. tps380x->rcdev.of_xlate = tps380x_reset_of_xlate;
  80. tps380x->rcdev.nr_resets = 1;
  81. return devm_reset_controller_register(dev, &tps380x->rcdev);
  82. }
  83. static const struct tps380x_reset_devdata tps3801_reset_data = {
  84. .min_reset_ms = 120,
  85. .typ_reset_ms = 200,
  86. .max_reset_ms = 280,
  87. };
  88. static const struct of_device_id tps380x_reset_dt_ids[] = {
  89. { .compatible = "ti,tps3801", .data = &tps3801_reset_data },
  90. { /* sentinel */ },
  91. };
  92. MODULE_DEVICE_TABLE(of, tps380x_reset_dt_ids);
  93. static struct platform_driver tps380x_reset_driver = {
  94. .probe = tps380x_reset_probe,
  95. .driver = {
  96. .name = "tps380x-reset",
  97. .of_match_table = tps380x_reset_dt_ids,
  98. },
  99. };
  100. module_platform_driver(tps380x_reset_driver);
  101. MODULE_AUTHOR("Marco Felsch <[email protected]>");
  102. MODULE_DESCRIPTION("TI TPS380x Supply Voltage Supervisor and Reset Driver");
  103. MODULE_LICENSE("GPL v2");