reset-ti-syscon.c 7.2 KB

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