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