1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
6 #include <linux/device.h>
7 #include <linux/list.h>
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/soc/qcom/smem_state.h>
13 static LIST_HEAD(smem_states);
14 static DEFINE_MUTEX(list_lock);
17 * struct qcom_smem_state - state context
18 * @refcount: refcount for the state
19 * @orphan: boolean indicator that this state has been unregistered
20 * @list: entry in smem_states list
21 * @of_node: of_node to use for matching the state in DT
22 * @priv: implementation private data
23 * @ops: ops for the state
25 struct qcom_smem_state {
29 struct list_head list;
30 struct device_node *of_node;
34 struct qcom_smem_state_ops ops;
38 * qcom_smem_state_update_bits() - update the masked bits in state with value
39 * @state: state handle acquired by calling qcom_smem_state_get()
40 * @mask: bit mask for the change
41 * @value: new value for the masked bits
43 * Returns 0 on success, otherwise negative errno.
45 int qcom_smem_state_update_bits(struct qcom_smem_state *state,
52 if (!state->ops.update_bits)
55 return state->ops.update_bits(state->priv, mask, value);
57 EXPORT_SYMBOL_GPL(qcom_smem_state_update_bits);
59 static struct qcom_smem_state *of_node_to_state(struct device_node *np)
61 struct qcom_smem_state *state;
63 mutex_lock(&list_lock);
65 list_for_each_entry(state, &smem_states, list) {
66 if (state->of_node == np) {
67 kref_get(&state->refcount);
71 state = ERR_PTR(-EPROBE_DEFER);
74 mutex_unlock(&list_lock);
80 * qcom_smem_state_get() - acquire handle to a state
81 * @dev: client device pointer
82 * @con_id: name of the state to lookup
83 * @bit: flags from the state reference, indicating which bit's affected
85 * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() must be
86 * called to release the returned state handle.
88 struct qcom_smem_state *qcom_smem_state_get(struct device *dev,
92 struct qcom_smem_state *state;
93 struct of_phandle_args args;
98 index = of_property_match_string(dev->of_node,
99 "qcom,smem-state-names",
102 dev_err(dev, "missing qcom,smem-state-names\n");
103 return ERR_PTR(index);
107 ret = of_parse_phandle_with_args(dev->of_node,
109 "#qcom,smem-state-cells",
113 dev_err(dev, "failed to parse qcom,smem-states property\n");
117 if (args.args_count != 1) {
118 dev_err(dev, "invalid #qcom,smem-state-cells\n");
119 return ERR_PTR(-EINVAL);
122 state = of_node_to_state(args.np);
129 of_node_put(args.np);
132 EXPORT_SYMBOL_GPL(qcom_smem_state_get);
134 static void qcom_smem_state_release(struct kref *ref)
136 struct qcom_smem_state *state = container_of(ref, struct qcom_smem_state, refcount);
138 list_del(&state->list);
139 of_node_put(state->of_node);
144 * qcom_smem_state_put() - release state handle
145 * @state: state handle to be released
147 void qcom_smem_state_put(struct qcom_smem_state *state)
149 mutex_lock(&list_lock);
150 kref_put(&state->refcount, qcom_smem_state_release);
151 mutex_unlock(&list_lock);
153 EXPORT_SYMBOL_GPL(qcom_smem_state_put);
155 static void devm_qcom_smem_state_release(struct device *dev, void *res)
157 qcom_smem_state_put(*(struct qcom_smem_state **)res);
161 * devm_qcom_smem_state_get() - acquire handle to a devres managed state
162 * @dev: client device pointer
163 * @con_id: name of the state to lookup
164 * @bit: flags from the state reference, indicating which bit's affected
166 * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() is called
167 * automatically when @dev is removed.
169 struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev,
173 struct qcom_smem_state **ptr, *state;
175 ptr = devres_alloc(devm_qcom_smem_state_release, sizeof(*ptr), GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
179 state = qcom_smem_state_get(dev, con_id, bit);
180 if (!IS_ERR(state)) {
182 devres_add(dev, ptr);
189 EXPORT_SYMBOL_GPL(devm_qcom_smem_state_get);
192 * qcom_smem_state_register() - register a new state
193 * @of_node: of_node used for matching client lookups
194 * @ops: implementation ops
195 * @priv: implementation specific private data
197 struct qcom_smem_state *qcom_smem_state_register(struct device_node *of_node,
198 const struct qcom_smem_state_ops *ops,
201 struct qcom_smem_state *state;
203 state = kzalloc(sizeof(*state), GFP_KERNEL);
205 return ERR_PTR(-ENOMEM);
207 kref_init(&state->refcount);
209 state->of_node = of_node_get(of_node);
213 mutex_lock(&list_lock);
214 list_add(&state->list, &smem_states);
215 mutex_unlock(&list_lock);
219 EXPORT_SYMBOL_GPL(qcom_smem_state_register);
222 * qcom_smem_state_unregister() - unregister a registered state
223 * @state: state handle to be unregistered
225 void qcom_smem_state_unregister(struct qcom_smem_state *state)
227 state->orphan = true;
228 qcom_smem_state_put(state);
230 EXPORT_SYMBOL_GPL(qcom_smem_state_unregister);