smem_state.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. 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. /**
  131. * qcom_smem_state_register() - register a new state
  132. * @of_node: of_node used for matching client lookups
  133. * @ops: implementation ops
  134. * @priv: implementation specific private data
  135. */
  136. struct qcom_smem_state *qcom_smem_state_register(struct device_node *of_node,
  137. const struct qcom_smem_state_ops *ops,
  138. void *priv)
  139. {
  140. struct qcom_smem_state *state;
  141. state = kzalloc(sizeof(*state), GFP_KERNEL);
  142. if (!state)
  143. return ERR_PTR(-ENOMEM);
  144. kref_init(&state->refcount);
  145. state->of_node = of_node;
  146. state->ops = *ops;
  147. state->priv = priv;
  148. mutex_lock(&list_lock);
  149. list_add(&state->list, &smem_states);
  150. mutex_unlock(&list_lock);
  151. return state;
  152. }
  153. EXPORT_SYMBOL_GPL(qcom_smem_state_register);
  154. /**
  155. * qcom_smem_state_unregister() - unregister a registered state
  156. * @state: state handle to be unregistered
  157. */
  158. void qcom_smem_state_unregister(struct qcom_smem_state *state)
  159. {
  160. state->orphan = true;
  161. qcom_smem_state_put(state);
  162. }
  163. EXPORT_SYMBOL_GPL(qcom_smem_state_unregister);