smem_state.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return ERR_PTR(-EINVAL);
  103. }
  104. state = of_node_to_state(args.np);
  105. if (IS_ERR(state))
  106. goto put;
  107. *bit = args.args[0];
  108. put:
  109. of_node_put(args.np);
  110. return state;
  111. }
  112. EXPORT_SYMBOL_GPL(qcom_smem_state_get);
  113. static void qcom_smem_state_release(struct kref *ref)
  114. {
  115. struct qcom_smem_state *state = container_of(ref, struct qcom_smem_state, refcount);
  116. list_del(&state->list);
  117. of_node_put(state->of_node);
  118. kfree(state);
  119. }
  120. /**
  121. * qcom_smem_state_put() - release state handle
  122. * @state: state handle to be released
  123. */
  124. void qcom_smem_state_put(struct qcom_smem_state *state)
  125. {
  126. mutex_lock(&list_lock);
  127. kref_put(&state->refcount, qcom_smem_state_release);
  128. mutex_unlock(&list_lock);
  129. }
  130. EXPORT_SYMBOL_GPL(qcom_smem_state_put);
  131. static void devm_qcom_smem_state_release(struct device *dev, void *res)
  132. {
  133. qcom_smem_state_put(*(struct qcom_smem_state **)res);
  134. }
  135. /**
  136. * devm_qcom_smem_state_get() - acquire handle to a devres managed state
  137. * @dev: client device pointer
  138. * @con_id: name of the state to lookup
  139. * @bit: flags from the state reference, indicating which bit's affected
  140. *
  141. * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() is called
  142. * automatically when @dev is removed.
  143. */
  144. struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev,
  145. const char *con_id,
  146. unsigned *bit)
  147. {
  148. struct qcom_smem_state **ptr, *state;
  149. ptr = devres_alloc(devm_qcom_smem_state_release, sizeof(*ptr), GFP_KERNEL);
  150. if (!ptr)
  151. return ERR_PTR(-ENOMEM);
  152. state = qcom_smem_state_get(dev, con_id, bit);
  153. if (!IS_ERR(state)) {
  154. *ptr = state;
  155. devres_add(dev, ptr);
  156. } else {
  157. devres_free(ptr);
  158. }
  159. return state;
  160. }
  161. EXPORT_SYMBOL_GPL(devm_qcom_smem_state_get);
  162. /**
  163. * qcom_smem_state_register() - register a new state
  164. * @of_node: of_node used for matching client lookups
  165. * @ops: implementation ops
  166. * @priv: implementation specific private data
  167. */
  168. struct qcom_smem_state *qcom_smem_state_register(struct device_node *of_node,
  169. const struct qcom_smem_state_ops *ops,
  170. void *priv)
  171. {
  172. struct qcom_smem_state *state;
  173. state = kzalloc(sizeof(*state), GFP_KERNEL);
  174. if (!state)
  175. return ERR_PTR(-ENOMEM);
  176. kref_init(&state->refcount);
  177. state->of_node = of_node_get(of_node);
  178. state->ops = *ops;
  179. state->priv = priv;
  180. mutex_lock(&list_lock);
  181. list_add(&state->list, &smem_states);
  182. mutex_unlock(&list_lock);
  183. return state;
  184. }
  185. EXPORT_SYMBOL_GPL(qcom_smem_state_register);
  186. /**
  187. * qcom_smem_state_unregister() - unregister a registered state
  188. * @state: state handle to be unregistered
  189. */
  190. void qcom_smem_state_unregister(struct qcom_smem_state *state)
  191. {
  192. state->orphan = true;
  193. qcom_smem_state_put(state);
  194. }
  195. EXPORT_SYMBOL_GPL(qcom_smem_state_unregister);