smem_state.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, Sony Mobile Communications Inc.
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/cleanup.h>
  7. #include <linux/device.h>
  8. #include <linux/list.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/slab.h>
  12. #include <linux/soc/qcom/smem_state.h>
  13. static LIST_HEAD(smem_states);
  14. static DEFINE_MUTEX(list_lock);
  15. /**
  16. * struct qcom_smem_state - state context
  17. * @refcount: refcount for the state
  18. * @orphan: boolean indicator that this state has been unregistered
  19. * @list: entry in smem_states list
  20. * @of_node: of_node to use for matching the state in DT
  21. * @priv: implementation private data
  22. * @ops: ops for the state
  23. */
  24. struct qcom_smem_state {
  25. struct kref refcount;
  26. bool orphan;
  27. struct list_head list;
  28. struct device_node *of_node;
  29. void *priv;
  30. struct qcom_smem_state_ops ops;
  31. };
  32. /**
  33. * qcom_smem_state_update_bits() - update the masked bits in state with value
  34. * @state: state handle acquired by calling qcom_smem_state_get()
  35. * @mask: bit mask for the change
  36. * @value: new value for the masked bits
  37. *
  38. * Returns 0 on success, otherwise negative errno.
  39. */
  40. int qcom_smem_state_update_bits(struct qcom_smem_state *state,
  41. u32 mask,
  42. u32 value)
  43. {
  44. if (state->orphan)
  45. return -ENXIO;
  46. if (!state->ops.update_bits)
  47. return -ENOTSUPP;
  48. return state->ops.update_bits(state->priv, mask, value);
  49. }
  50. EXPORT_SYMBOL_GPL(qcom_smem_state_update_bits);
  51. static struct qcom_smem_state *of_node_to_state(struct device_node *np)
  52. {
  53. struct qcom_smem_state *state;
  54. guard(mutex)(&list_lock);
  55. list_for_each_entry(state, &smem_states, list) {
  56. if (state->of_node == np) {
  57. kref_get(&state->refcount);
  58. return state;
  59. }
  60. }
  61. return ERR_PTR(-EPROBE_DEFER);
  62. }
  63. /**
  64. * qcom_smem_state_get() - acquire handle to a state
  65. * @dev: client device pointer
  66. * @con_id: name of the state to lookup
  67. * @bit: flags from the state reference, indicating which bit's affected
  68. *
  69. * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() must be
  70. * called to release the returned state handle.
  71. */
  72. struct qcom_smem_state *qcom_smem_state_get(struct device *dev,
  73. const char *con_id,
  74. unsigned *bit)
  75. {
  76. struct qcom_smem_state *state;
  77. struct of_phandle_args args;
  78. int index = 0;
  79. int ret;
  80. if (con_id) {
  81. index = of_property_match_string(dev->of_node,
  82. "qcom,smem-state-names",
  83. con_id);
  84. if (index < 0) {
  85. dev_err(dev, "missing qcom,smem-state-names\n");
  86. return ERR_PTR(index);
  87. }
  88. }
  89. ret = of_parse_phandle_with_args(dev->of_node,
  90. "qcom,smem-states",
  91. "#qcom,smem-state-cells",
  92. index,
  93. &args);
  94. if (ret) {
  95. dev_err(dev, "failed to parse qcom,smem-states property\n");
  96. return ERR_PTR(ret);
  97. }
  98. if (args.args_count != 1) {
  99. dev_err(dev, "invalid #qcom,smem-state-cells\n");
  100. state = ERR_PTR(-EINVAL);
  101. goto put;
  102. }
  103. state = of_node_to_state(args.np);
  104. if (IS_ERR(state))
  105. goto put;
  106. *bit = args.args[0];
  107. put:
  108. of_node_put(args.np);
  109. return state;
  110. }
  111. EXPORT_SYMBOL_GPL(qcom_smem_state_get);
  112. static void qcom_smem_state_release(struct kref *ref)
  113. {
  114. struct qcom_smem_state *state = container_of(ref, struct qcom_smem_state, refcount);
  115. list_del(&state->list);
  116. of_node_put(state->of_node);
  117. kfree(state);
  118. }
  119. /**
  120. * qcom_smem_state_put() - release state handle
  121. * @state: state handle to be released
  122. */
  123. void qcom_smem_state_put(struct qcom_smem_state *state)
  124. {
  125. mutex_lock(&list_lock);
  126. kref_put(&state->refcount, qcom_smem_state_release);
  127. mutex_unlock(&list_lock);
  128. }
  129. EXPORT_SYMBOL_GPL(qcom_smem_state_put);
  130. static void devm_qcom_smem_state_release(struct device *dev, void *res)
  131. {
  132. qcom_smem_state_put(*(struct qcom_smem_state **)res);
  133. }
  134. /**
  135. * devm_qcom_smem_state_get() - acquire handle to a devres managed state
  136. * @dev: client device pointer
  137. * @con_id: name of the state to lookup
  138. * @bit: flags from the state reference, indicating which bit's affected
  139. *
  140. * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() is called
  141. * automatically when @dev is removed.
  142. */
  143. struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev,
  144. const char *con_id,
  145. unsigned *bit)
  146. {
  147. struct qcom_smem_state **ptr, *state;
  148. ptr = devres_alloc(devm_qcom_smem_state_release, sizeof(*ptr), GFP_KERNEL);
  149. if (!ptr)
  150. return ERR_PTR(-ENOMEM);
  151. state = qcom_smem_state_get(dev, con_id, bit);
  152. if (!IS_ERR(state)) {
  153. *ptr = state;
  154. devres_add(dev, ptr);
  155. } else {
  156. devres_free(ptr);
  157. }
  158. return state;
  159. }
  160. EXPORT_SYMBOL_GPL(devm_qcom_smem_state_get);
  161. /**
  162. * qcom_smem_state_register() - register a new state
  163. * @of_node: of_node used for matching client lookups
  164. * @ops: implementation ops
  165. * @priv: implementation specific private data
  166. */
  167. struct qcom_smem_state *qcom_smem_state_register(struct device_node *of_node,
  168. const struct qcom_smem_state_ops *ops,
  169. void *priv)
  170. {
  171. struct qcom_smem_state *state;
  172. state = kzalloc_obj(*state);
  173. if (!state)
  174. return ERR_PTR(-ENOMEM);
  175. kref_init(&state->refcount);
  176. state->of_node = of_node_get(of_node);
  177. state->ops = *ops;
  178. state->priv = priv;
  179. mutex_lock(&list_lock);
  180. list_add(&state->list, &smem_states);
  181. mutex_unlock(&list_lock);
  182. return state;
  183. }
  184. EXPORT_SYMBOL_GPL(qcom_smem_state_register);
  185. /**
  186. * qcom_smem_state_unregister() - unregister a registered state
  187. * @state: state handle to be unregistered
  188. */
  189. void qcom_smem_state_unregister(struct qcom_smem_state *state)
  190. {
  191. state->orphan = true;
  192. qcom_smem_state_put(state);
  193. }
  194. EXPORT_SYMBOL_GPL(qcom_smem_state_unregister);