1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Multiplexer subsystem
4 *
5 * Based on the linux multiplexer framework
6 *
7 * Copyright (C) 2017 Axentia Technologies AB
8 * Author: Peter Rosin <peda@axentia.se>
9 *
10 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
11 * Jean-Jacques Hiblot <jjhiblot@ti.com>
12 */
13
14 #define LOG_CATEGORY UCLASS_MUX
15
16 #include <common.h>
17 #include <dm.h>
18 #include <mux-internal.h>
19 #include <dm/device-internal.h>
20 #include <dm/device_compat.h>
21 #include <dm/devres.h>
22 #include <dt-bindings/mux/mux.h>
23 #include <linux/bug.h>
24
25 /*
26 * The idle-as-is "state" is not an actual state that may be selected, it
27 * only implies that the state should not be changed. So, use that state
28 * as indication that the cached state of the multiplexer is unknown.
29 */
30 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
31
32 /**
33 * mux_control_ops() - Get the mux_control ops.
34 * @dev: The client device.
35 *
36 * Return: A pointer to the 'mux_control_ops' of the device.
37 */
mux_dev_ops(struct udevice * dev)38 static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev)
39 {
40 return (const struct mux_control_ops *)dev->driver->ops;
41 }
42
43 /**
44 * mux_control_set() - Set the state of the given mux controller.
45 * @mux: A multiplexer control
46 * @state: The new requested state.
47 *
48 * Return: 0 if OK, or a negative error code.
49 */
mux_control_set(struct mux_control * mux,int state)50 static int mux_control_set(struct mux_control *mux, int state)
51 {
52 int ret = mux_dev_ops(mux->dev)->set(mux, state);
53
54 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
55
56 return ret;
57 }
58
mux_control_states(struct mux_control * mux)59 unsigned int mux_control_states(struct mux_control *mux)
60 {
61 return mux->states;
62 }
63
64 /**
65 * __mux_control_select() - Select the given multiplexer state.
66 * @mux: The mux-control to request a change of state from.
67 * @state: The new requested state.
68 *
69 * Try to set the mux to the requested state. If not, try to revert if
70 * appropriate.
71 */
__mux_control_select(struct mux_control * mux,int state)72 static int __mux_control_select(struct mux_control *mux, int state)
73 {
74 int ret;
75
76 if (WARN_ON(state < 0 || state >= mux->states))
77 return -EINVAL;
78
79 if (mux->cached_state == state)
80 return 0;
81
82 ret = mux_control_set(mux, state);
83 if (ret >= 0)
84 return 0;
85
86 /* The mux update failed, try to revert if appropriate... */
87 if (mux->idle_state != MUX_IDLE_AS_IS)
88 mux_control_set(mux, mux->idle_state);
89
90 return ret;
91 }
92
mux_control_select(struct mux_control * mux,unsigned int state)93 int mux_control_select(struct mux_control *mux, unsigned int state)
94 {
95 int ret;
96
97 if (mux->in_use)
98 return -EBUSY;
99
100 ret = __mux_control_select(mux, state);
101
102 if (ret < 0)
103 return ret;
104
105 mux->in_use = true;
106
107 return 0;
108 }
109
mux_control_deselect(struct mux_control * mux)110 int mux_control_deselect(struct mux_control *mux)
111 {
112 int ret = 0;
113
114 if (mux->idle_state != MUX_IDLE_AS_IS &&
115 mux->idle_state != mux->cached_state)
116 ret = mux_control_set(mux, mux->idle_state);
117
118 mux->in_use = false;
119
120 return ret;
121 }
122
mux_of_xlate_default(struct mux_chip * mux_chip,struct ofnode_phandle_args * args,struct mux_control ** muxp)123 static int mux_of_xlate_default(struct mux_chip *mux_chip,
124 struct ofnode_phandle_args *args,
125 struct mux_control **muxp)
126 {
127 struct mux_control *mux;
128 int id;
129
130 log_debug("%s(muxp=%p)\n", __func__, muxp);
131
132 if (args->args_count > 1) {
133 debug("Invaild args_count: %d\n", args->args_count);
134 return -EINVAL;
135 }
136
137 if (args->args_count)
138 id = args->args[0];
139 else
140 id = 0;
141
142 if (id >= mux_chip->controllers) {
143 pr_err("bad mux controller %u specified in %s\n",
144 id, ofnode_get_name(args->node));
145 return -ERANGE;
146 }
147
148 mux = &mux_chip->mux[id];
149 mux->id = id;
150 *muxp = mux;
151 return 0;
152 }
153
154 /**
155 * mux_get_by_indexed_prop() - Get a mux control by integer index
156 * @dev: The client device.
157 * @prop_name: Name of the device tree property.
158 * @index: The index of the mux to get
159 * @mux: A pointer to the 'mux_control' struct to initialize.
160 *
161 * Return: 0 of OK, -errno otherwise.
162 */
mux_get_by_indexed_prop(struct udevice * dev,const char * prop_name,int index,struct mux_control ** mux)163 static int mux_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
164 int index, struct mux_control **mux)
165 {
166 int ret;
167 struct ofnode_phandle_args args;
168 struct udevice *dev_mux;
169 const struct mux_control_ops *ops;
170 struct mux_chip *mux_chip;
171
172 log_debug("%s(dev=%p, index=%d, mux=%p)\n", __func__, dev, index, mux);
173
174 ret = dev_read_phandle_with_args(dev, prop_name, "#mux-control-cells",
175 0, index, &args);
176 if (ret) {
177 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
178 __func__, ret);
179 return ret;
180 }
181
182 ret = uclass_get_device_by_ofnode(UCLASS_MUX, args.node, &dev_mux);
183 if (ret) {
184 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
185 __func__, ret);
186 return ret;
187 }
188
189 mux_chip = dev_get_uclass_priv(dev_mux);
190
191 ops = mux_dev_ops(dev_mux);
192 if (ops->of_xlate)
193 ret = ops->of_xlate(mux_chip, &args, mux);
194 else
195 ret = mux_of_xlate_default(mux_chip, &args, mux);
196 if (ret) {
197 debug("of_xlate() failed: %d\n", ret);
198 return ret;
199 }
200 (*mux)->dev = dev_mux;
201
202 return 0;
203 }
204
mux_get_by_index(struct udevice * dev,int index,struct mux_control ** mux)205 int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux)
206 {
207 return mux_get_by_indexed_prop(dev, "mux-controls", index, mux);
208 }
209
mux_control_get(struct udevice * dev,const char * name,struct mux_control ** mux)210 int mux_control_get(struct udevice *dev, const char *name,
211 struct mux_control **mux)
212 {
213 int index;
214
215 debug("%s(dev=%p, name=%s, mux=%p)\n", __func__, dev, name, mux);
216
217 index = dev_read_stringlist_search(dev, "mux-control-names", name);
218 if (index < 0) {
219 debug("fdt_stringlist_search() failed: %d\n", index);
220 return index;
221 }
222
223 return mux_get_by_index(dev, index, mux);
224 }
225
mux_control_put(struct mux_control * mux)226 void mux_control_put(struct mux_control *mux)
227 {
228 mux_control_deselect(mux);
229 }
230
231 /**
232 * devm_mux_control_release() - Release the given managed mux.
233 * @dev: The client device.
234 * @res: Pointer to the mux to be released.
235 *
236 * This function is called by devres to release the mux. It reverses the
237 * effects of mux_control_get().
238 */
devm_mux_control_release(struct udevice * dev,void * res)239 static void devm_mux_control_release(struct udevice *dev, void *res)
240 {
241 mux_control_put(*(struct mux_control **)res);
242 }
243
devm_mux_control_get(struct udevice * dev,const char * id)244 struct mux_control *devm_mux_control_get(struct udevice *dev, const char *id)
245 {
246 int rc;
247 struct mux_control **mux;
248
249 mux = devres_alloc(devm_mux_control_release,
250 sizeof(struct mux_control *), __GFP_ZERO);
251 if (unlikely(!mux))
252 return ERR_PTR(-ENOMEM);
253
254 rc = mux_control_get(dev, id, mux);
255 if (rc)
256 return ERR_PTR(rc);
257
258 devres_add(dev, mux);
259 return *mux;
260 }
261
mux_alloc_controllers(struct udevice * dev,unsigned int controllers)262 int mux_alloc_controllers(struct udevice *dev, unsigned int controllers)
263 {
264 int i;
265 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
266
267 mux_chip->mux = devm_kmalloc(dev,
268 sizeof(struct mux_control) * controllers,
269 __GFP_ZERO);
270 if (!mux_chip->mux)
271 return -ENOMEM;
272
273 mux_chip->controllers = controllers;
274
275 for (i = 0; i < mux_chip->controllers; ++i) {
276 struct mux_control *mux = &mux_chip->mux[i];
277
278 mux->dev = dev;
279 mux->cached_state = MUX_CACHE_UNKNOWN;
280 mux->idle_state = MUX_IDLE_AS_IS;
281 mux->in_use = false;
282 mux->id = i;
283 }
284
285 return 0;
286 }
287
mux_uclass_post_probe(struct udevice * dev)288 static int mux_uclass_post_probe(struct udevice *dev)
289 {
290 int i, ret;
291 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
292
293 /* Set all mux controllers to their idle state. */
294 for (i = 0; i < mux_chip->controllers; ++i) {
295 struct mux_control *mux = &mux_chip->mux[i];
296
297 if (mux->idle_state == mux->cached_state)
298 continue;
299
300 ret = mux_control_set(mux, mux->idle_state);
301 if (ret < 0) {
302 dev_err(dev, "unable to set idle state\n");
303 return ret;
304 }
305 }
306 return 0;
307 }
308
dm_mux_init(void)309 int dm_mux_init(void)
310 {
311 struct uclass *uc;
312 struct udevice *dev;
313 int ret;
314
315 ret = uclass_get(UCLASS_MUX, &uc);
316 if (ret < 0) {
317 log_debug("unable to get MUX uclass\n");
318 return ret;
319 }
320 uclass_foreach_dev(dev, uc) {
321 if (dev_read_bool(dev, "u-boot,mux-autoprobe")) {
322 ret = device_probe(dev);
323 if (ret)
324 log_debug("unable to probe device %s\n",
325 dev->name);
326 }
327 }
328
329 return 0;
330 }
331
332 UCLASS_DRIVER(mux) = {
333 .id = UCLASS_MUX,
334 .name = "mux",
335 .post_probe = mux_uclass_post_probe,
336 .per_device_auto = sizeof(struct mux_chip),
337 };
338