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