smem_state.c 4.9 KB

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