smem_state.c 5.5 KB

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