reset-ti-syscon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI SYSCON regmap reset driver
  4. *
  5. * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
  6. * Andrew F. Davis <[email protected]>
  7. * Suman Anna <[email protected]>
  8. */
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset-controller.h>
  15. #include <dt-bindings/reset/ti-syscon.h>
  16. /**
  17. * struct ti_syscon_reset_control - reset control structure
  18. * @assert_offset: reset assert control register offset from syscon base
  19. * @assert_bit: reset assert bit in the reset assert control register
  20. * @deassert_offset: reset deassert control register offset from syscon base
  21. * @deassert_bit: reset deassert bit in the reset deassert control register
  22. * @status_offset: reset status register offset from syscon base
  23. * @status_bit: reset status bit in the reset status register
  24. * @flags: reset flag indicating how the (de)assert and status are handled
  25. */
  26. struct ti_syscon_reset_control {
  27. unsigned int assert_offset;
  28. unsigned int assert_bit;
  29. unsigned int deassert_offset;
  30. unsigned int deassert_bit;
  31. unsigned int status_offset;
  32. unsigned int status_bit;
  33. u32 flags;
  34. };
  35. /**
  36. * struct ti_syscon_reset_data - reset controller information structure
  37. * @rcdev: reset controller entity
  38. * @regmap: regmap handle containing the memory-mapped reset registers
  39. * @controls: array of reset controls
  40. * @nr_controls: number of controls in control array
  41. */
  42. struct ti_syscon_reset_data {
  43. struct reset_controller_dev rcdev;
  44. struct regmap *regmap;
  45. struct ti_syscon_reset_control *controls;
  46. unsigned int nr_controls;
  47. };
  48. #define to_ti_syscon_reset_data(_rcdev) \
  49. container_of(_rcdev, struct ti_syscon_reset_data, rcdev)
  50. /**
  51. * ti_syscon_reset_assert() - assert device reset
  52. * @rcdev: reset controller entity
  53. * @id: ID of the reset to be asserted
  54. *
  55. * This function implements the reset driver op to assert a device's reset.
  56. * This asserts the reset in a manner prescribed by the reset flags.
  57. *
  58. * Return: 0 for successful request, else a corresponding error value
  59. */
  60. static int ti_syscon_reset_assert(struct reset_controller_dev *rcdev,
  61. unsigned long id)
  62. {
  63. struct ti_syscon_reset_data *data = to_ti_syscon_reset_data(rcdev);
  64. struct ti_syscon_reset_control *control;
  65. unsigned int mask, value;
  66. if (id >= data->nr_controls)
  67. return -EINVAL;
  68. control = &data->controls[id];
  69. if (control->flags & ASSERT_NONE)
  70. return -ENOTSUPP; /* assert not supported for this reset */
  71. mask = BIT(control->assert_bit);
  72. value = (control->flags & ASSERT_SET) ? mask : 0x0;
  73. return regmap_write_bits(data->regmap, control->assert_offset, mask, value);
  74. }
  75. /**
  76. * ti_syscon_reset_deassert() - deassert device reset
  77. * @rcdev: reset controller entity
  78. * @id: ID of reset to be deasserted
  79. *
  80. * This function implements the reset driver op to deassert a device's reset.
  81. * This deasserts the reset in a manner prescribed by the reset flags.
  82. *
  83. * Return: 0 for successful request, else a corresponding error value
  84. */
  85. static int ti_syscon_reset_deassert(struct reset_controller_dev *rcdev,
  86. unsigned long id)
  87. {
  88. struct ti_syscon_reset_data *data = to_ti_syscon_reset_data(rcdev);
  89. struct ti_syscon_reset_control *control;
  90. unsigned int mask, value;
  91. if (id >= data->nr_controls)
  92. return -EINVAL;
  93. control = &data->controls[id];
  94. if (control->flags & DEASSERT_NONE)
  95. return -ENOTSUPP; /* deassert not supported for this reset */
  96. mask = BIT(control->deassert_bit);
  97. value = (control->flags & DEASSERT_SET) ? mask : 0x0;
  98. return regmap_write_bits(data->regmap, control->deassert_offset, mask, value);
  99. }
  100. /**
  101. * ti_syscon_reset_status() - check device reset status
  102. * @rcdev: reset controller entity
  103. * @id: ID of the reset for which the status is being requested
  104. *
  105. * This function implements the reset driver op to return the status of a
  106. * device's reset.
  107. *
  108. * Return: 0 if reset is deasserted, true if reset is asserted, else a
  109. * corresponding error value
  110. */
  111. static int ti_syscon_reset_status(struct reset_controller_dev *rcdev,
  112. unsigned long id)
  113. {
  114. struct ti_syscon_reset_data *data = to_ti_syscon_reset_data(rcdev);
  115. struct ti_syscon_reset_control *control;
  116. unsigned int reset_state;
  117. int ret;
  118. if (id >= data->nr_controls)
  119. return -EINVAL;
  120. control = &data->controls[id];
  121. if (control->flags & STATUS_NONE)
  122. return -ENOTSUPP; /* status not supported for this reset */
  123. ret = regmap_read(data->regmap, control->status_offset, &reset_state);
  124. if (ret)
  125. return ret;
  126. return !(reset_state & BIT(control->status_bit)) ==
  127. !(control->flags & STATUS_SET);
  128. }
  129. static const struct reset_control_ops ti_syscon_reset_ops = {
  130. .assert = ti_syscon_reset_assert,
  131. .deassert = ti_syscon_reset_deassert,
  132. .status = ti_syscon_reset_status,
  133. };
  134. static int ti_syscon_reset_probe(struct platform_device *pdev)
  135. {
  136. struct device *dev = &pdev->dev;
  137. struct device_node *np = dev->of_node;
  138. struct ti_syscon_reset_data *data;
  139. struct regmap *regmap;
  140. const __be32 *list;
  141. struct ti_syscon_reset_control *controls;
  142. int size, nr_controls, i;
  143. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  144. if (!data)
  145. return -ENOMEM;
  146. regmap = syscon_node_to_regmap(np->parent);
  147. if (IS_ERR(regmap))
  148. return PTR_ERR(regmap);
  149. list = of_get_property(np, "ti,reset-bits", &size);
  150. if (!list || (size / sizeof(*list)) % 7 != 0) {
  151. dev_err(dev, "invalid DT reset description\n");
  152. return -EINVAL;
  153. }
  154. nr_controls = (size / sizeof(*list)) / 7;
  155. controls = devm_kcalloc(dev, nr_controls, sizeof(*controls),
  156. GFP_KERNEL);
  157. if (!controls)
  158. return -ENOMEM;
  159. for (i = 0; i < nr_controls; i++) {
  160. controls[i].assert_offset = be32_to_cpup(list++);
  161. controls[i].assert_bit = be32_to_cpup(list++);
  162. controls[i].deassert_offset = be32_to_cpup(list++);
  163. controls[i].deassert_bit = be32_to_cpup(list++);
  164. controls[i].status_offset = be32_to_cpup(list++);
  165. controls[i].status_bit = be32_to_cpup(list++);
  166. controls[i].flags = be32_to_cpup(list++);
  167. }
  168. data->rcdev.ops = &ti_syscon_reset_ops;
  169. data->rcdev.owner = THIS_MODULE;
  170. data->rcdev.of_node = np;
  171. data->rcdev.nr_resets = nr_controls;
  172. data->regmap = regmap;
  173. data->controls = controls;
  174. data->nr_controls = nr_controls;
  175. platform_set_drvdata(pdev, data);
  176. return devm_reset_controller_register(dev, &data->rcdev);
  177. }
  178. static const struct of_device_id ti_syscon_reset_of_match[] = {
  179. { .compatible = "ti,syscon-reset", },
  180. { /* sentinel */ },
  181. };
  182. MODULE_DEVICE_TABLE(of, ti_syscon_reset_of_match);
  183. static struct platform_driver ti_syscon_reset_driver = {
  184. .probe = ti_syscon_reset_probe,
  185. .driver = {
  186. .name = "ti-syscon-reset",
  187. .of_match_table = ti_syscon_reset_of_match,
  188. },
  189. };
  190. module_platform_driver(ti_syscon_reset_driver);
  191. MODULE_AUTHOR("Andrew F. Davis <[email protected]>");
  192. MODULE_AUTHOR("Suman Anna <[email protected]>");
  193. MODULE_DESCRIPTION("TI SYSCON Regmap Reset Driver");
  194. MODULE_LICENSE("GPL v2");