]>
Commit | Line | Data |
---|---|---|
2744e8af LW |
1 | /* |
2 | * Core driver for the pin muxing portions of the pin control subsystem | |
3 | * | |
4 | * Copyright (C) 2011 ST-Ericsson SA | |
5 | * Written on behalf of Linaro for ST-Ericsson | |
6 | * Based on bits of regulator core, gpio core and clk core | |
7 | * | |
8 | * Author: Linus Walleij <linus.walleij@linaro.org> | |
9 | * | |
10 | * License terms: GNU General Public License (GPL) version 2 | |
11 | */ | |
12 | #define pr_fmt(fmt) "pinmux core: " fmt | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/device.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/radix-tree.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/list.h> | |
22 | #include <linux/mutex.h> | |
23 | #include <linux/spinlock.h> | |
24 | #include <linux/sysfs.h> | |
25 | #include <linux/debugfs.h> | |
26 | #include <linux/seq_file.h> | |
27 | #include <linux/pinctrl/machine.h> | |
28 | #include <linux/pinctrl/pinmux.h> | |
29 | #include "core.h" | |
30 | ||
31 | /* List of pinmuxes */ | |
32 | static DEFINE_MUTEX(pinmux_list_mutex); | |
33 | static LIST_HEAD(pinmux_list); | |
34 | ||
2744e8af LW |
35 | /* Global pinmux maps, we allow one set only */ |
36 | static struct pinmux_map const *pinmux_maps; | |
37 | static unsigned pinmux_maps_num; | |
38 | ||
39 | /** | |
40 | * struct pinmux_group - group list item for pinmux groups | |
41 | * @node: pinmux group list node | |
42 | * @group_selector: the group selector for this group | |
43 | */ | |
44 | struct pinmux_group { | |
45 | struct list_head node; | |
46 | unsigned group_selector; | |
47 | }; | |
48 | ||
49 | /** | |
50 | * struct pinmux - per-device pinmux state holder | |
51 | * @node: global list node | |
52 | * @dev: the device using this pinmux | |
53 | * @usecount: the number of active users of this mux setting, used to keep | |
54 | * track of nested use cases | |
55 | * @pins: an array of discrete physical pins used in this mapping, taken | |
56 | * from the global pin enumeration space (copied from pinmux map) | |
57 | * @num_pins: the number of pins in this mapping array, i.e. the number of | |
58 | * elements in .pins so we can iterate over that array (copied from | |
59 | * pinmux map) | |
60 | * @pctldev: pin control device handling this pinmux | |
61 | * @func_selector: the function selector for the pinmux device handling | |
62 | * this pinmux | |
63 | * @groups: the group selectors for the pinmux device and | |
64 | * selector combination handling this pinmux, this is a list that | |
65 | * will be traversed on all pinmux operations such as | |
66 | * get/put/enable/disable | |
67 | * @mutex: a lock for the pinmux state holder | |
68 | */ | |
69 | struct pinmux { | |
70 | struct list_head node; | |
71 | struct device *dev; | |
72 | unsigned usecount; | |
73 | struct pinctrl_dev *pctldev; | |
74 | unsigned func_selector; | |
75 | struct list_head groups; | |
76 | struct mutex mutex; | |
77 | }; | |
78 | ||
79 | /** | |
80 | * struct pinmux_hog - a list item to stash mux hogs | |
81 | * @node: pinmux hog list node | |
82 | * @map: map entry responsible for this hogging | |
83 | * @pmx: the pinmux hogged by this item | |
84 | */ | |
85 | struct pinmux_hog { | |
86 | struct list_head node; | |
87 | struct pinmux_map const *map; | |
88 | struct pinmux *pmx; | |
89 | }; | |
90 | ||
91 | /** | |
92 | * pin_request() - request a single pin to be muxed in, typically for GPIO | |
93 | * @pin: the pin number in the global pin space | |
94 | * @function: a functional name to give to this pin, passed to the driver | |
95 | * so it knows what function to mux in, e.g. the string "gpioNN" | |
96 | * means that you want to mux in the pin for use as GPIO number NN | |
2744e8af LW |
97 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
98 | * single GPIO pin | |
99 | */ | |
100 | static int pin_request(struct pinctrl_dev *pctldev, | |
3712a3c4 | 101 | int pin, const char *function, |
2744e8af LW |
102 | struct pinctrl_gpio_range *gpio_range) |
103 | { | |
104 | struct pin_desc *desc; | |
105 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | |
106 | int status = -EINVAL; | |
107 | ||
108 | dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function); | |
109 | ||
2744e8af LW |
110 | desc = pin_desc_get(pctldev, pin); |
111 | if (desc == NULL) { | |
112 | dev_err(&pctldev->dev, | |
113 | "pin is not registered so it cannot be requested\n"); | |
114 | goto out; | |
115 | } | |
116 | ||
d2f6a1c6 MB |
117 | if (!function) { |
118 | dev_err(&pctldev->dev, "no function name given\n"); | |
119 | return -EINVAL; | |
120 | } | |
121 | ||
2744e8af | 122 | spin_lock(&desc->lock); |
5d2eaf80 | 123 | if (desc->mux_function) { |
2744e8af LW |
124 | spin_unlock(&desc->lock); |
125 | dev_err(&pctldev->dev, | |
126 | "pin already requested\n"); | |
127 | goto out; | |
128 | } | |
5d2eaf80 | 129 | desc->mux_function = function; |
2744e8af LW |
130 | spin_unlock(&desc->lock); |
131 | ||
132 | /* Let each pin increase references to this module */ | |
133 | if (!try_module_get(pctldev->owner)) { | |
134 | dev_err(&pctldev->dev, | |
135 | "could not increase module refcount for pin %d\n", | |
136 | pin); | |
137 | status = -EINVAL; | |
138 | goto out_free_pin; | |
139 | } | |
140 | ||
141 | /* | |
142 | * If there is no kind of request function for the pin we just assume | |
143 | * we got it by default and proceed. | |
144 | */ | |
3712a3c4 | 145 | if (gpio_range && ops->gpio_request_enable) |
2744e8af LW |
146 | /* This requests and enables a single GPIO pin */ |
147 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); | |
148 | else if (ops->request) | |
149 | status = ops->request(pctldev, pin); | |
150 | else | |
151 | status = 0; | |
152 | ||
153 | if (status) | |
154 | dev_err(&pctldev->dev, "->request on device %s failed " | |
155 | "for pin %d\n", | |
156 | pctldev->desc->name, pin); | |
157 | out_free_pin: | |
158 | if (status) { | |
159 | spin_lock(&desc->lock); | |
5d2eaf80 | 160 | desc->mux_function = NULL; |
2744e8af LW |
161 | spin_unlock(&desc->lock); |
162 | } | |
163 | out: | |
164 | if (status) | |
165 | dev_err(&pctldev->dev, "pin-%d (%s) status %d\n", | |
166 | pin, function ? : "?", status); | |
167 | ||
168 | return status; | |
169 | } | |
170 | ||
171 | /** | |
172 | * pin_free() - release a single muxed in pin so something else can be muxed | |
173 | * @pctldev: pin controller device handling this pin | |
174 | * @pin: the pin to free | |
3712a3c4 SW |
175 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
176 | * single GPIO pin | |
2744e8af | 177 | */ |
3712a3c4 SW |
178 | static const char *pin_free(struct pinctrl_dev *pctldev, int pin, |
179 | struct pinctrl_gpio_range *gpio_range) | |
2744e8af LW |
180 | { |
181 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | |
182 | struct pin_desc *desc; | |
3712a3c4 | 183 | const char *func; |
2744e8af LW |
184 | |
185 | desc = pin_desc_get(pctldev, pin); | |
186 | if (desc == NULL) { | |
187 | dev_err(&pctldev->dev, | |
188 | "pin is not registered so it cannot be freed\n"); | |
3712a3c4 | 189 | return NULL; |
2744e8af LW |
190 | } |
191 | ||
3712a3c4 SW |
192 | /* |
193 | * If there is no kind of request function for the pin we just assume | |
194 | * we got it by default and proceed. | |
195 | */ | |
196 | if (gpio_range && ops->gpio_disable_free) | |
197 | ops->gpio_disable_free(pctldev, gpio_range, pin); | |
198 | else if (ops->free) | |
2744e8af LW |
199 | ops->free(pctldev, pin); |
200 | ||
201 | spin_lock(&desc->lock); | |
3712a3c4 | 202 | func = desc->mux_function; |
5d2eaf80 | 203 | desc->mux_function = NULL; |
2744e8af LW |
204 | spin_unlock(&desc->lock); |
205 | module_put(pctldev->owner); | |
3712a3c4 SW |
206 | |
207 | return func; | |
2744e8af LW |
208 | } |
209 | ||
210 | /** | |
211 | * pinmux_request_gpio() - request a single pin to be muxed in as GPIO | |
212 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
213 | */ | |
214 | int pinmux_request_gpio(unsigned gpio) | |
215 | { | |
216 | char gpiostr[16]; | |
5d2eaf80 | 217 | const char *function; |
2744e8af LW |
218 | struct pinctrl_dev *pctldev; |
219 | struct pinctrl_gpio_range *range; | |
220 | int ret; | |
221 | int pin; | |
222 | ||
223 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); | |
224 | if (ret) | |
225 | return -EINVAL; | |
226 | ||
227 | /* Convert to the pin controllers number space */ | |
228 | pin = gpio - range->base; | |
229 | ||
230 | /* Conjure some name stating what chip and pin this is taken by */ | |
231 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); | |
232 | ||
5d2eaf80 SW |
233 | function = kstrdup(gpiostr, GFP_KERNEL); |
234 | if (!function) | |
235 | return -EINVAL; | |
236 | ||
3712a3c4 | 237 | ret = pin_request(pctldev, pin, function, range); |
5d2eaf80 SW |
238 | if (ret < 0) |
239 | kfree(function); | |
240 | ||
241 | return ret; | |
2744e8af LW |
242 | } |
243 | EXPORT_SYMBOL_GPL(pinmux_request_gpio); | |
244 | ||
245 | /** | |
246 | * pinmux_free_gpio() - free a single pin, currently used as GPIO | |
247 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
248 | */ | |
249 | void pinmux_free_gpio(unsigned gpio) | |
250 | { | |
251 | struct pinctrl_dev *pctldev; | |
252 | struct pinctrl_gpio_range *range; | |
253 | int ret; | |
254 | int pin; | |
3712a3c4 | 255 | const char *func; |
2744e8af LW |
256 | |
257 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); | |
258 | if (ret) | |
259 | return; | |
260 | ||
261 | /* Convert to the pin controllers number space */ | |
262 | pin = gpio - range->base; | |
263 | ||
3712a3c4 SW |
264 | func = pin_free(pctldev, pin, range); |
265 | kfree(func); | |
2744e8af LW |
266 | } |
267 | EXPORT_SYMBOL_GPL(pinmux_free_gpio); | |
268 | ||
269 | /** | |
270 | * pinmux_register_mappings() - register a set of pinmux mappings | |
271 | * @maps: the pinmux mappings table to register | |
272 | * @num_maps: the number of maps in the mapping table | |
273 | * | |
274 | * Only call this once during initialization of your machine, the function is | |
275 | * tagged as __init and won't be callable after init has completed. The map | |
276 | * passed into this function will be owned by the pinmux core and cannot be | |
277 | * free:d. | |
278 | */ | |
279 | int __init pinmux_register_mappings(struct pinmux_map const *maps, | |
280 | unsigned num_maps) | |
281 | { | |
282 | int i; | |
283 | ||
284 | if (pinmux_maps != NULL) { | |
285 | pr_err("pinmux mappings already registered, you can only " | |
286 | "register one set of maps\n"); | |
287 | return -EINVAL; | |
288 | } | |
289 | ||
290 | pr_debug("add %d pinmux maps\n", num_maps); | |
291 | for (i = 0; i < num_maps; i++) { | |
292 | /* Sanity check the mapping */ | |
293 | if (!maps[i].name) { | |
294 | pr_err("failed to register map %d: " | |
295 | "no map name given\n", i); | |
296 | return -EINVAL; | |
297 | } | |
298 | if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) { | |
299 | pr_err("failed to register map %s (%d): " | |
300 | "no pin control device given\n", | |
301 | maps[i].name, i); | |
302 | return -EINVAL; | |
303 | } | |
304 | if (!maps[i].function) { | |
305 | pr_err("failed to register map %s (%d): " | |
306 | "no function ID given\n", maps[i].name, i); | |
307 | return -EINVAL; | |
308 | } | |
309 | ||
310 | if (!maps[i].dev && !maps[i].dev_name) | |
311 | pr_debug("add system map %s function %s with no device\n", | |
312 | maps[i].name, | |
313 | maps[i].function); | |
314 | else | |
315 | pr_debug("register map %s, function %s\n", | |
316 | maps[i].name, | |
317 | maps[i].function); | |
318 | } | |
319 | ||
320 | pinmux_maps = maps; | |
321 | pinmux_maps_num = num_maps; | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | /** | |
327 | * acquire_pins() - acquire all the pins for a certain funcion on a pinmux | |
328 | * @pctldev: the device to take the pins on | |
329 | * @func_selector: the function selector to acquire the pins for | |
330 | * @group_selector: the group selector containing the pins to acquire | |
331 | */ | |
332 | static int acquire_pins(struct pinctrl_dev *pctldev, | |
333 | unsigned func_selector, | |
334 | unsigned group_selector) | |
335 | { | |
336 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | |
337 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | |
338 | const char *func = pmxops->get_function_name(pctldev, | |
339 | func_selector); | |
a5818a8b | 340 | const unsigned *pins; |
2744e8af LW |
341 | unsigned num_pins; |
342 | int ret; | |
343 | int i; | |
344 | ||
345 | ret = pctlops->get_group_pins(pctldev, group_selector, | |
346 | &pins, &num_pins); | |
347 | if (ret) | |
348 | return ret; | |
349 | ||
350 | dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n", | |
351 | num_pins, group_selector); | |
352 | ||
353 | /* Try to allocate all pins in this group, one by one */ | |
354 | for (i = 0; i < num_pins; i++) { | |
3712a3c4 | 355 | ret = pin_request(pctldev, pins[i], func, NULL); |
2744e8af LW |
356 | if (ret) { |
357 | dev_err(&pctldev->dev, | |
358 | "could not get pin %d for function %s " | |
359 | "on device %s - conflicting mux mappings?\n", | |
360 | pins[i], func ? : "(undefined)", | |
361 | pinctrl_dev_get_name(pctldev)); | |
362 | /* On error release all taken pins */ | |
363 | i--; /* this pin just failed */ | |
364 | for (; i >= 0; i--) | |
3712a3c4 | 365 | pin_free(pctldev, pins[i], NULL); |
2744e8af LW |
366 | return -ENODEV; |
367 | } | |
368 | } | |
369 | return 0; | |
370 | } | |
371 | ||
372 | /** | |
373 | * release_pins() - release pins taken by earlier acquirement | |
374 | * @pctldev: the device to free the pinx on | |
375 | * @group_selector: the group selector containing the pins to free | |
376 | */ | |
377 | static void release_pins(struct pinctrl_dev *pctldev, | |
378 | unsigned group_selector) | |
379 | { | |
380 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | |
a5818a8b | 381 | const unsigned *pins; |
2744e8af LW |
382 | unsigned num_pins; |
383 | int ret; | |
384 | int i; | |
385 | ||
386 | ret = pctlops->get_group_pins(pctldev, group_selector, | |
387 | &pins, &num_pins); | |
388 | if (ret) { | |
389 | dev_err(&pctldev->dev, "could not get pins to release for " | |
390 | "group selector %d\n", | |
391 | group_selector); | |
392 | return; | |
393 | } | |
394 | for (i = 0; i < num_pins; i++) | |
3712a3c4 | 395 | pin_free(pctldev, pins[i], NULL); |
2744e8af LW |
396 | } |
397 | ||
2744e8af LW |
398 | /** |
399 | * pinmux_check_pin_group() - check function and pin group combo | |
400 | * @pctldev: device to check the pin group vs function for | |
401 | * @func_selector: the function selector to check the pin group for, we have | |
402 | * already looked this up in the calling function | |
403 | * @pin_group: the pin group to match to the function | |
404 | * | |
405 | * This function will check that the pinmux driver can supply the | |
406 | * selected pin group for a certain function, returns the group selector if | |
407 | * the group and function selector will work fine together, else returns | |
408 | * negative | |
409 | */ | |
410 | static int pinmux_check_pin_group(struct pinctrl_dev *pctldev, | |
411 | unsigned func_selector, | |
412 | const char *pin_group) | |
413 | { | |
414 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | |
415 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | |
416 | int ret; | |
417 | ||
418 | /* | |
419 | * If the driver does not support different pin groups for the | |
420 | * functions, we only support group 0, and assume this exists. | |
421 | */ | |
422 | if (!pctlops || !pctlops->list_groups) | |
423 | return 0; | |
424 | ||
425 | /* | |
426 | * Passing NULL (no specific group) will select the first and | |
427 | * hopefully only group of pins available for this function. | |
428 | */ | |
429 | if (!pin_group) { | |
430 | char const * const *groups; | |
431 | unsigned num_groups; | |
432 | ||
433 | ret = pmxops->get_function_groups(pctldev, func_selector, | |
434 | &groups, &num_groups); | |
435 | if (ret) | |
436 | return ret; | |
437 | if (num_groups < 1) | |
438 | return -EINVAL; | |
7afde8ba | 439 | ret = pinctrl_get_group_selector(pctldev, groups[0]); |
2744e8af LW |
440 | if (ret < 0) { |
441 | dev_err(&pctldev->dev, | |
442 | "function %s wants group %s but the pin " | |
443 | "controller does not seem to have that group\n", | |
444 | pmxops->get_function_name(pctldev, func_selector), | |
445 | groups[0]); | |
446 | return ret; | |
447 | } | |
448 | ||
449 | if (num_groups > 1) | |
450 | dev_dbg(&pctldev->dev, | |
451 | "function %s support more than one group, " | |
452 | "default-selecting first group %s (%d)\n", | |
453 | pmxops->get_function_name(pctldev, func_selector), | |
454 | groups[0], | |
455 | ret); | |
456 | ||
457 | return ret; | |
458 | } | |
459 | ||
460 | dev_dbg(&pctldev->dev, | |
461 | "check if we have pin group %s on controller %s\n", | |
462 | pin_group, pinctrl_dev_get_name(pctldev)); | |
463 | ||
7afde8ba | 464 | ret = pinctrl_get_group_selector(pctldev, pin_group); |
2744e8af LW |
465 | if (ret < 0) { |
466 | dev_dbg(&pctldev->dev, | |
467 | "%s does not support pin group %s with function %s\n", | |
468 | pinctrl_dev_get_name(pctldev), | |
469 | pin_group, | |
470 | pmxops->get_function_name(pctldev, func_selector)); | |
471 | } | |
472 | return ret; | |
473 | } | |
474 | ||
475 | /** | |
476 | * pinmux_search_function() - check pin control driver for a certain function | |
477 | * @pctldev: device to check for function and position | |
478 | * @map: function map containing the function and position to look for | |
479 | * @func_selector: returns the applicable function selector if found | |
480 | * @group_selector: returns the applicable group selector if found | |
481 | * | |
482 | * This will search the pinmux driver for an applicable | |
483 | * function with a specific pin group, returns 0 if these can be mapped | |
484 | * negative otherwise | |
485 | */ | |
486 | static int pinmux_search_function(struct pinctrl_dev *pctldev, | |
487 | struct pinmux_map const *map, | |
488 | unsigned *func_selector, | |
489 | unsigned *group_selector) | |
490 | { | |
491 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | |
492 | unsigned selector = 0; | |
493 | ||
494 | /* See if this pctldev has this function */ | |
495 | while (ops->list_functions(pctldev, selector) >= 0) { | |
496 | const char *fname = ops->get_function_name(pctldev, | |
497 | selector); | |
498 | int ret; | |
499 | ||
500 | if (!strcmp(map->function, fname)) { | |
501 | /* Found the function, check pin group */ | |
502 | ret = pinmux_check_pin_group(pctldev, selector, | |
503 | map->group); | |
504 | if (ret < 0) | |
505 | return ret; | |
506 | ||
507 | /* This function and group selector can be used */ | |
508 | *func_selector = selector; | |
509 | *group_selector = ret; | |
510 | return 0; | |
511 | ||
512 | } | |
513 | selector++; | |
514 | } | |
515 | ||
516 | pr_err("%s does not support function %s\n", | |
517 | pinctrl_dev_get_name(pctldev), map->function); | |
518 | return -EINVAL; | |
519 | } | |
520 | ||
521 | /** | |
522 | * pinmux_enable_muxmap() - enable a map entry for a certain pinmux | |
523 | */ | |
524 | static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev, | |
525 | struct pinmux *pmx, | |
526 | struct device *dev, | |
527 | const char *devname, | |
528 | struct pinmux_map const *map) | |
529 | { | |
530 | unsigned func_selector; | |
531 | unsigned group_selector; | |
532 | struct pinmux_group *grp; | |
533 | int ret; | |
534 | ||
535 | /* | |
536 | * Note that we're not locking the pinmux mutex here, because | |
537 | * this is only called at pinmux initialization time when it | |
538 | * has not been added to any list and thus is not reachable | |
539 | * by anyone else. | |
540 | */ | |
541 | ||
542 | if (pmx->pctldev && pmx->pctldev != pctldev) { | |
543 | dev_err(&pctldev->dev, | |
544 | "different pin control devices given for device %s, " | |
545 | "function %s\n", | |
546 | devname, | |
547 | map->function); | |
548 | return -EINVAL; | |
549 | } | |
550 | pmx->dev = dev; | |
551 | pmx->pctldev = pctldev; | |
552 | ||
553 | /* Now go into the driver and try to match a function and group */ | |
554 | ret = pinmux_search_function(pctldev, map, &func_selector, | |
555 | &group_selector); | |
556 | if (ret < 0) | |
557 | return ret; | |
558 | ||
559 | /* | |
560 | * If the function selector is already set, it needs to be identical, | |
561 | * we support several groups with one function but not several | |
562 | * functions with one or several groups in the same pinmux. | |
563 | */ | |
564 | if (pmx->func_selector != UINT_MAX && | |
565 | pmx->func_selector != func_selector) { | |
566 | dev_err(&pctldev->dev, | |
567 | "dual function defines in the map for device %s\n", | |
568 | devname); | |
569 | return -EINVAL; | |
570 | } | |
571 | pmx->func_selector = func_selector; | |
572 | ||
573 | /* Now add this group selector, we may have many of them */ | |
574 | grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL); | |
575 | if (!grp) | |
576 | return -ENOMEM; | |
577 | grp->group_selector = group_selector; | |
578 | ret = acquire_pins(pctldev, func_selector, group_selector); | |
579 | if (ret) { | |
580 | kfree(grp); | |
581 | return ret; | |
582 | } | |
583 | list_add(&grp->node, &pmx->groups); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static void pinmux_free_groups(struct pinmux *pmx) | |
589 | { | |
590 | struct list_head *node, *tmp; | |
591 | ||
592 | list_for_each_safe(node, tmp, &pmx->groups) { | |
593 | struct pinmux_group *grp = | |
594 | list_entry(node, struct pinmux_group, node); | |
595 | /* Release all pins taken by this group */ | |
596 | release_pins(pmx->pctldev, grp->group_selector); | |
597 | list_del(node); | |
598 | kfree(grp); | |
599 | } | |
600 | } | |
601 | ||
602 | /** | |
603 | * pinmux_get() - retrieves the pinmux for a certain device | |
604 | * @dev: the device to get the pinmux for | |
605 | * @name: an optional specific mux mapping name or NULL, the name is only | |
606 | * needed if you want to have more than one mapping per device, or if you | |
607 | * need an anonymous pinmux (not tied to any specific device) | |
608 | */ | |
609 | struct pinmux *pinmux_get(struct device *dev, const char *name) | |
610 | { | |
611 | ||
612 | struct pinmux_map const *map = NULL; | |
613 | struct pinctrl_dev *pctldev = NULL; | |
614 | const char *devname = NULL; | |
615 | struct pinmux *pmx; | |
616 | bool found_map; | |
617 | unsigned num_maps = 0; | |
618 | int ret = -ENODEV; | |
619 | int i; | |
620 | ||
621 | /* We must have dev or ID or both */ | |
622 | if (!dev && !name) | |
623 | return ERR_PTR(-EINVAL); | |
624 | ||
625 | if (dev) | |
626 | devname = dev_name(dev); | |
627 | ||
628 | pr_debug("get mux %s for device %s\n", name, | |
629 | devname ? devname : "(none)"); | |
630 | ||
631 | /* | |
632 | * create the state cookie holder struct pinmux for each | |
633 | * mapping, this is what consumers will get when requesting | |
634 | * a pinmux handle with pinmux_get() | |
635 | */ | |
636 | pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL); | |
637 | if (pmx == NULL) | |
638 | return ERR_PTR(-ENOMEM); | |
639 | mutex_init(&pmx->mutex); | |
640 | pmx->func_selector = UINT_MAX; | |
641 | INIT_LIST_HEAD(&pmx->groups); | |
642 | ||
643 | /* Iterate over the pinmux maps to locate the right ones */ | |
644 | for (i = 0; i < pinmux_maps_num; i++) { | |
645 | map = &pinmux_maps[i]; | |
646 | found_map = false; | |
647 | ||
648 | /* | |
649 | * First, try to find the pctldev given in the map | |
650 | */ | |
651 | pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev, | |
652 | map->ctrl_dev_name); | |
653 | if (!pctldev) { | |
654 | const char *devname = NULL; | |
655 | ||
656 | if (map->ctrl_dev) | |
657 | devname = dev_name(map->ctrl_dev); | |
658 | else if (map->ctrl_dev_name) | |
659 | devname = map->ctrl_dev_name; | |
660 | ||
661 | pr_warning("could not find a pinctrl device for pinmux " | |
662 | "function %s, fishy, they shall all have one\n", | |
663 | map->function); | |
664 | pr_warning("given pinctrl device name: %s", | |
665 | devname ? devname : "UNDEFINED"); | |
666 | ||
667 | /* Continue to check the other mappings anyway... */ | |
668 | continue; | |
669 | } | |
670 | ||
671 | pr_debug("in map, found pctldev %s to handle function %s", | |
672 | dev_name(&pctldev->dev), map->function); | |
673 | ||
674 | ||
675 | /* | |
676 | * If we're looking for a specific named map, this must match, | |
677 | * else we loop and look for the next. | |
678 | */ | |
679 | if (name != NULL) { | |
680 | if (map->name == NULL) | |
681 | continue; | |
682 | if (strcmp(map->name, name)) | |
683 | continue; | |
684 | } | |
685 | ||
686 | /* | |
687 | * This is for the case where no device name is given, we | |
688 | * already know that the function name matches from above | |
689 | * code. | |
690 | */ | |
691 | if (!map->dev_name && (name != NULL)) | |
692 | found_map = true; | |
693 | ||
694 | /* If the mapping has a device set up it must match */ | |
695 | if (map->dev_name && | |
696 | (!devname || !strcmp(map->dev_name, devname))) | |
697 | /* MATCH! */ | |
698 | found_map = true; | |
699 | ||
700 | /* If this map is applicable, then apply it */ | |
701 | if (found_map) { | |
702 | ret = pinmux_enable_muxmap(pctldev, pmx, dev, | |
703 | devname, map); | |
704 | if (ret) { | |
705 | pinmux_free_groups(pmx); | |
706 | kfree(pmx); | |
707 | return ERR_PTR(ret); | |
708 | } | |
709 | num_maps++; | |
710 | } | |
711 | } | |
712 | ||
713 | ||
714 | /* We should have atleast one map, right */ | |
715 | if (!num_maps) { | |
716 | pr_err("could not find any mux maps for device %s, ID %s\n", | |
717 | devname ? devname : "(anonymous)", | |
718 | name ? name : "(undefined)"); | |
719 | kfree(pmx); | |
720 | return ERR_PTR(-EINVAL); | |
721 | } | |
722 | ||
723 | pr_debug("found %u mux maps for device %s, UD %s\n", | |
724 | num_maps, | |
725 | devname ? devname : "(anonymous)", | |
726 | name ? name : "(undefined)"); | |
727 | ||
728 | /* Add the pinmux to the global list */ | |
729 | mutex_lock(&pinmux_list_mutex); | |
730 | list_add(&pmx->node, &pinmux_list); | |
731 | mutex_unlock(&pinmux_list_mutex); | |
732 | ||
733 | return pmx; | |
734 | } | |
735 | EXPORT_SYMBOL_GPL(pinmux_get); | |
736 | ||
737 | /** | |
738 | * pinmux_put() - release a previously claimed pinmux | |
739 | * @pmx: a pinmux previously claimed by pinmux_get() | |
740 | */ | |
741 | void pinmux_put(struct pinmux *pmx) | |
742 | { | |
743 | if (pmx == NULL) | |
744 | return; | |
745 | ||
746 | mutex_lock(&pmx->mutex); | |
747 | if (pmx->usecount) | |
748 | pr_warn("releasing pinmux with active users!\n"); | |
749 | /* Free the groups and all acquired pins */ | |
750 | pinmux_free_groups(pmx); | |
751 | mutex_unlock(&pmx->mutex); | |
752 | ||
753 | /* Remove from list */ | |
754 | mutex_lock(&pinmux_list_mutex); | |
755 | list_del(&pmx->node); | |
756 | mutex_unlock(&pinmux_list_mutex); | |
757 | ||
758 | kfree(pmx); | |
759 | } | |
760 | EXPORT_SYMBOL_GPL(pinmux_put); | |
761 | ||
762 | /** | |
763 | * pinmux_enable() - enable a certain pinmux setting | |
764 | * @pmx: the pinmux to enable, previously claimed by pinmux_get() | |
765 | */ | |
766 | int pinmux_enable(struct pinmux *pmx) | |
767 | { | |
768 | int ret = 0; | |
769 | ||
770 | if (pmx == NULL) | |
771 | return -EINVAL; | |
772 | mutex_lock(&pmx->mutex); | |
773 | if (pmx->usecount++ == 0) { | |
774 | struct pinctrl_dev *pctldev = pmx->pctldev; | |
775 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | |
776 | struct pinmux_group *grp; | |
777 | ||
778 | list_for_each_entry(grp, &pmx->groups, node) { | |
779 | ret = ops->enable(pctldev, pmx->func_selector, | |
780 | grp->group_selector); | |
781 | if (ret) { | |
782 | /* | |
783 | * TODO: call disable() on all groups we called | |
784 | * enable() on to this point? | |
785 | */ | |
786 | pmx->usecount--; | |
787 | break; | |
788 | } | |
789 | } | |
790 | } | |
791 | mutex_unlock(&pmx->mutex); | |
792 | return ret; | |
793 | } | |
794 | EXPORT_SYMBOL_GPL(pinmux_enable); | |
795 | ||
796 | /** | |
797 | * pinmux_disable() - disable a certain pinmux setting | |
798 | * @pmx: the pinmux to disable, previously claimed by pinmux_get() | |
799 | */ | |
800 | void pinmux_disable(struct pinmux *pmx) | |
801 | { | |
802 | if (pmx == NULL) | |
803 | return; | |
804 | ||
805 | mutex_lock(&pmx->mutex); | |
806 | if (--pmx->usecount == 0) { | |
807 | struct pinctrl_dev *pctldev = pmx->pctldev; | |
808 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | |
809 | struct pinmux_group *grp; | |
810 | ||
811 | list_for_each_entry(grp, &pmx->groups, node) { | |
812 | ops->disable(pctldev, pmx->func_selector, | |
813 | grp->group_selector); | |
814 | } | |
815 | } | |
816 | mutex_unlock(&pmx->mutex); | |
817 | } | |
818 | EXPORT_SYMBOL_GPL(pinmux_disable); | |
819 | ||
820 | int pinmux_check_ops(const struct pinmux_ops *ops) | |
821 | { | |
822 | /* Check that we implement required operations */ | |
823 | if (!ops->list_functions || | |
824 | !ops->get_function_name || | |
825 | !ops->get_function_groups || | |
826 | !ops->enable || | |
827 | !ops->disable) | |
828 | return -EINVAL; | |
829 | ||
830 | return 0; | |
831 | } | |
832 | ||
833 | /* Hog a single map entry and add to the hoglist */ | |
834 | static int pinmux_hog_map(struct pinctrl_dev *pctldev, | |
835 | struct pinmux_map const *map) | |
836 | { | |
837 | struct pinmux_hog *hog; | |
838 | struct pinmux *pmx; | |
839 | int ret; | |
840 | ||
841 | if (map->dev || map->dev_name) { | |
842 | /* | |
843 | * TODO: the day we have device tree support, we can | |
844 | * traverse the device tree and hog to specific device nodes | |
845 | * without any problems, so then we can hog pinmuxes for | |
846 | * all devices that just want a static pin mux at this point. | |
847 | */ | |
848 | dev_err(&pctldev->dev, "map %s wants to hog a non-system " | |
849 | "pinmux, this is not going to work\n", map->name); | |
850 | return -EINVAL; | |
851 | } | |
852 | ||
853 | hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL); | |
854 | if (!hog) | |
855 | return -ENOMEM; | |
856 | ||
857 | pmx = pinmux_get(NULL, map->name); | |
858 | if (IS_ERR(pmx)) { | |
859 | kfree(hog); | |
860 | dev_err(&pctldev->dev, | |
861 | "could not get the %s pinmux mapping for hogging\n", | |
862 | map->name); | |
863 | return PTR_ERR(pmx); | |
864 | } | |
865 | ||
866 | ret = pinmux_enable(pmx); | |
867 | if (ret) { | |
868 | pinmux_put(pmx); | |
869 | kfree(hog); | |
870 | dev_err(&pctldev->dev, | |
871 | "could not enable the %s pinmux mapping for hogging\n", | |
872 | map->name); | |
873 | return ret; | |
874 | } | |
875 | ||
876 | hog->map = map; | |
877 | hog->pmx = pmx; | |
878 | ||
879 | dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name, | |
880 | map->function); | |
881 | mutex_lock(&pctldev->pinmux_hogs_lock); | |
882 | list_add(&hog->node, &pctldev->pinmux_hogs); | |
883 | mutex_unlock(&pctldev->pinmux_hogs_lock); | |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
888 | /** | |
889 | * pinmux_hog_maps() - hog specific map entries on controller device | |
890 | * @pctldev: the pin control device to hog entries on | |
891 | * | |
892 | * When the pin controllers are registered, there may be some specific pinmux | |
893 | * map entries that need to be hogged, i.e. get+enabled until the system shuts | |
894 | * down. | |
895 | */ | |
896 | int pinmux_hog_maps(struct pinctrl_dev *pctldev) | |
897 | { | |
898 | struct device *dev = &pctldev->dev; | |
899 | const char *devname = dev_name(dev); | |
900 | int ret; | |
901 | int i; | |
902 | ||
903 | INIT_LIST_HEAD(&pctldev->pinmux_hogs); | |
904 | mutex_init(&pctldev->pinmux_hogs_lock); | |
905 | ||
906 | for (i = 0; i < pinmux_maps_num; i++) { | |
907 | struct pinmux_map const *map = &pinmux_maps[i]; | |
908 | ||
909 | if (((map->ctrl_dev == dev) || | |
910 | !strcmp(map->ctrl_dev_name, devname)) && | |
911 | map->hog_on_boot) { | |
912 | /* OK time to hog! */ | |
913 | ret = pinmux_hog_map(pctldev, map); | |
914 | if (ret) | |
915 | return ret; | |
916 | } | |
917 | } | |
918 | return 0; | |
919 | } | |
920 | ||
921 | /** | |
922 | * pinmux_hog_maps() - unhog specific map entries on controller device | |
923 | * @pctldev: the pin control device to unhog entries on | |
924 | */ | |
925 | void pinmux_unhog_maps(struct pinctrl_dev *pctldev) | |
926 | { | |
927 | struct list_head *node, *tmp; | |
928 | ||
929 | mutex_lock(&pctldev->pinmux_hogs_lock); | |
930 | list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) { | |
931 | struct pinmux_hog *hog = | |
932 | list_entry(node, struct pinmux_hog, node); | |
933 | pinmux_disable(hog->pmx); | |
934 | pinmux_put(hog->pmx); | |
935 | list_del(node); | |
936 | kfree(hog); | |
937 | } | |
938 | mutex_unlock(&pctldev->pinmux_hogs_lock); | |
939 | } | |
940 | ||
941 | #ifdef CONFIG_DEBUG_FS | |
942 | ||
943 | /* Called from pincontrol core */ | |
944 | static int pinmux_functions_show(struct seq_file *s, void *what) | |
945 | { | |
946 | struct pinctrl_dev *pctldev = s->private; | |
947 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | |
948 | unsigned func_selector = 0; | |
949 | ||
950 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { | |
951 | const char *func = pmxops->get_function_name(pctldev, | |
952 | func_selector); | |
953 | const char * const *groups; | |
954 | unsigned num_groups; | |
955 | int ret; | |
956 | int i; | |
957 | ||
958 | ret = pmxops->get_function_groups(pctldev, func_selector, | |
959 | &groups, &num_groups); | |
960 | if (ret) | |
961 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", | |
962 | func); | |
963 | ||
964 | seq_printf(s, "function: %s, groups = [ ", func); | |
965 | for (i = 0; i < num_groups; i++) | |
966 | seq_printf(s, "%s ", groups[i]); | |
967 | seq_puts(s, "]\n"); | |
968 | ||
969 | func_selector++; | |
970 | ||
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | static int pinmux_pins_show(struct seq_file *s, void *what) | |
977 | { | |
978 | struct pinctrl_dev *pctldev = s->private; | |
979 | unsigned pin; | |
980 | ||
981 | seq_puts(s, "Pinmux settings per pin\n"); | |
982 | seq_puts(s, "Format: pin (name): pinmuxfunction\n"); | |
983 | ||
984 | /* The highest pin number need to be included in the loop, thus <= */ | |
985 | for (pin = 0; pin <= pctldev->desc->maxpin; pin++) { | |
986 | ||
987 | struct pin_desc *desc; | |
988 | ||
989 | desc = pin_desc_get(pctldev, pin); | |
990 | /* Pin space may be sparse */ | |
991 | if (desc == NULL) | |
992 | continue; | |
993 | ||
994 | seq_printf(s, "pin %d (%s): %s\n", pin, | |
995 | desc->name ? desc->name : "unnamed", | |
5d2eaf80 SW |
996 | desc->mux_function ? desc->mux_function |
997 | : "UNCLAIMED"); | |
2744e8af LW |
998 | } |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static int pinmux_hogs_show(struct seq_file *s, void *what) | |
1004 | { | |
1005 | struct pinctrl_dev *pctldev = s->private; | |
1006 | struct pinmux_hog *hog; | |
1007 | ||
1008 | seq_puts(s, "Pinmux map hogs held by device\n"); | |
1009 | ||
1010 | list_for_each_entry(hog, &pctldev->pinmux_hogs, node) | |
1011 | seq_printf(s, "%s\n", hog->map->name); | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int pinmux_show(struct seq_file *s, void *what) | |
1017 | { | |
1018 | struct pinmux *pmx; | |
1019 | ||
1020 | seq_puts(s, "Requested pinmuxes and their maps:\n"); | |
1021 | list_for_each_entry(pmx, &pinmux_list, node) { | |
1022 | struct pinctrl_dev *pctldev = pmx->pctldev; | |
1023 | const struct pinmux_ops *pmxops; | |
1024 | const struct pinctrl_ops *pctlops; | |
1025 | struct pinmux_group *grp; | |
1026 | ||
1027 | if (!pctldev) { | |
1028 | seq_puts(s, "NO PIN CONTROLLER DEVICE\n"); | |
1029 | continue; | |
1030 | } | |
1031 | ||
1032 | pmxops = pctldev->desc->pmxops; | |
1033 | pctlops = pctldev->desc->pctlops; | |
1034 | ||
1035 | seq_printf(s, "device: %s function: %s (%u),", | |
1036 | pinctrl_dev_get_name(pmx->pctldev), | |
1037 | pmxops->get_function_name(pctldev, pmx->func_selector), | |
1038 | pmx->func_selector); | |
1039 | ||
1040 | seq_printf(s, " groups: ["); | |
1041 | list_for_each_entry(grp, &pmx->groups, node) { | |
1042 | seq_printf(s, " %s (%u)", | |
1043 | pctlops->get_group_name(pctldev, grp->group_selector), | |
1044 | grp->group_selector); | |
1045 | } | |
1046 | seq_printf(s, " ]"); | |
1047 | ||
1048 | seq_printf(s, " users: %u map-> %s\n", | |
1049 | pmx->usecount, | |
1050 | pmx->dev ? dev_name(pmx->dev) : "(system)"); | |
1051 | } | |
1052 | ||
1053 | return 0; | |
1054 | } | |
1055 | ||
1056 | static int pinmux_maps_show(struct seq_file *s, void *what) | |
1057 | { | |
1058 | int i; | |
1059 | ||
1060 | seq_puts(s, "Pinmux maps:\n"); | |
1061 | ||
1062 | for (i = 0; i < pinmux_maps_num; i++) { | |
1063 | struct pinmux_map const *map = &pinmux_maps[i]; | |
1064 | ||
1065 | seq_printf(s, "%s:\n", map->name); | |
1066 | if (map->dev || map->dev_name) | |
1067 | seq_printf(s, " device: %s\n", | |
1068 | map->dev ? dev_name(map->dev) : | |
1069 | map->dev_name); | |
1070 | else | |
1071 | seq_printf(s, " SYSTEM MUX\n"); | |
1072 | seq_printf(s, " controlling device %s\n", | |
1073 | map->ctrl_dev ? dev_name(map->ctrl_dev) : | |
1074 | map->ctrl_dev_name); | |
1075 | seq_printf(s, " function: %s\n", map->function); | |
1076 | seq_printf(s, " group: %s\n", map->group ? map->group : | |
1077 | "(default)"); | |
1078 | } | |
1079 | return 0; | |
1080 | } | |
1081 | ||
1082 | static int pinmux_functions_open(struct inode *inode, struct file *file) | |
1083 | { | |
1084 | return single_open(file, pinmux_functions_show, inode->i_private); | |
1085 | } | |
1086 | ||
1087 | static int pinmux_pins_open(struct inode *inode, struct file *file) | |
1088 | { | |
1089 | return single_open(file, pinmux_pins_show, inode->i_private); | |
1090 | } | |
1091 | ||
1092 | static int pinmux_hogs_open(struct inode *inode, struct file *file) | |
1093 | { | |
1094 | return single_open(file, pinmux_hogs_show, inode->i_private); | |
1095 | } | |
1096 | ||
1097 | static int pinmux_open(struct inode *inode, struct file *file) | |
1098 | { | |
1099 | return single_open(file, pinmux_show, NULL); | |
1100 | } | |
1101 | ||
1102 | static int pinmux_maps_open(struct inode *inode, struct file *file) | |
1103 | { | |
1104 | return single_open(file, pinmux_maps_show, NULL); | |
1105 | } | |
1106 | ||
1107 | static const struct file_operations pinmux_functions_ops = { | |
1108 | .open = pinmux_functions_open, | |
1109 | .read = seq_read, | |
1110 | .llseek = seq_lseek, | |
1111 | .release = single_release, | |
1112 | }; | |
1113 | ||
1114 | static const struct file_operations pinmux_pins_ops = { | |
1115 | .open = pinmux_pins_open, | |
1116 | .read = seq_read, | |
1117 | .llseek = seq_lseek, | |
1118 | .release = single_release, | |
1119 | }; | |
1120 | ||
1121 | static const struct file_operations pinmux_hogs_ops = { | |
1122 | .open = pinmux_hogs_open, | |
1123 | .read = seq_read, | |
1124 | .llseek = seq_lseek, | |
1125 | .release = single_release, | |
1126 | }; | |
1127 | ||
1128 | static const struct file_operations pinmux_ops = { | |
1129 | .open = pinmux_open, | |
1130 | .read = seq_read, | |
1131 | .llseek = seq_lseek, | |
1132 | .release = single_release, | |
1133 | }; | |
1134 | ||
1135 | static const struct file_operations pinmux_maps_ops = { | |
1136 | .open = pinmux_maps_open, | |
1137 | .read = seq_read, | |
1138 | .llseek = seq_lseek, | |
1139 | .release = single_release, | |
1140 | }; | |
1141 | ||
1142 | void pinmux_init_device_debugfs(struct dentry *devroot, | |
1143 | struct pinctrl_dev *pctldev) | |
1144 | { | |
1145 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, | |
1146 | devroot, pctldev, &pinmux_functions_ops); | |
1147 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, | |
1148 | devroot, pctldev, &pinmux_pins_ops); | |
1149 | debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO, | |
1150 | devroot, pctldev, &pinmux_hogs_ops); | |
1151 | } | |
1152 | ||
1153 | void pinmux_init_debugfs(struct dentry *subsys_root) | |
1154 | { | |
1155 | debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO, | |
1156 | subsys_root, NULL, &pinmux_ops); | |
1157 | debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO, | |
1158 | subsys_root, NULL, &pinmux_maps_ops); | |
1159 | } | |
1160 | ||
1161 | #endif /* CONFIG_DEBUG_FS */ |