pinctrl: use dev_*() instead of pr_*(), add some msgs, minor cleanups
[deliverable/linux.git] / drivers / pinctrl / core.c
1 /*
2 * Core driver for the pin control subsystem
3 *
4 * Copyright (C) 2011-2012 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 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
12 * License terms: GNU General Public License (GPL) version 2
13 */
14 #define pr_fmt(fmt) "pinctrl core: " fmt
15
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/radix-tree.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
26 #include <linux/sysfs.h>
27 #include <linux/debugfs.h>
28 #include <linux/seq_file.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/machine.h>
31 #include "core.h"
32 #include "pinmux.h"
33 #include "pinconf.h"
34
35 /**
36 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
40 */
41 struct pinctrl_maps {
42 struct list_head node;
43 struct pinctrl_map const *maps;
44 unsigned num_maps;
45 };
46
47 /**
48 * struct pinctrl_hog - a list item to stash control hogs
49 * @node: pin control hog list node
50 * @map: map entry responsible for this hogging
51 * @pmx: the pin control hogged by this item
52 */
53 struct pinctrl_hog {
54 struct list_head node;
55 struct pinctrl_map const *map;
56 struct pinctrl *p;
57 };
58
59 /* Global list of pin control devices */
60 static DEFINE_MUTEX(pinctrldev_list_mutex);
61 static LIST_HEAD(pinctrldev_list);
62
63 /* List of pin controller handles */
64 static DEFINE_MUTEX(pinctrl_list_mutex);
65 static LIST_HEAD(pinctrl_list);
66
67 /* Global pinctrl maps */
68 static DEFINE_MUTEX(pinctrl_maps_mutex);
69 static LIST_HEAD(pinctrl_maps);
70
71 #define for_each_maps(_maps_node_, _i_, _map_) \
72 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
73 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
74 _i_ < _maps_node_->num_maps; \
75 i++, _map_ = &_maps_node_->maps[_i_])
76
77 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
78 {
79 /* We're not allowed to register devices without name */
80 return pctldev->desc->name;
81 }
82 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
83
84 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
85 {
86 return pctldev->driver_data;
87 }
88 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
89
90 /**
91 * get_pinctrl_dev_from_devname() - look up pin controller device
92 * @devname: the name of a device instance, as returned by dev_name()
93 *
94 * Looks up a pin control device matching a certain device name or pure device
95 * pointer, the pure device pointer will take precedence.
96 */
97 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
98 {
99 struct pinctrl_dev *pctldev = NULL;
100 bool found = false;
101
102 if (!devname)
103 return NULL;
104
105 mutex_lock(&pinctrldev_list_mutex);
106 list_for_each_entry(pctldev, &pinctrldev_list, node) {
107 if (!strcmp(dev_name(pctldev->dev), devname)) {
108 /* Matched on device name */
109 found = true;
110 break;
111 }
112 }
113 mutex_unlock(&pinctrldev_list_mutex);
114
115 return found ? pctldev : NULL;
116 }
117
118 struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
119 {
120 struct pin_desc *pindesc;
121 unsigned long flags;
122
123 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
124 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
125 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
126
127 return pindesc;
128 }
129
130 /**
131 * pin_get_from_name() - look up a pin number from a name
132 * @pctldev: the pin control device to lookup the pin on
133 * @name: the name of the pin to look up
134 */
135 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
136 {
137 unsigned i, pin;
138
139 /* The pin number can be retrived from the pin controller descriptor */
140 for (i = 0; i < pctldev->desc->npins; i++) {
141 struct pin_desc *desc;
142
143 pin = pctldev->desc->pins[i].number;
144 desc = pin_desc_get(pctldev, pin);
145 /* Pin space may be sparse */
146 if (desc == NULL)
147 continue;
148 if (desc->name && !strcmp(name, desc->name))
149 return pin;
150 }
151
152 return -EINVAL;
153 }
154
155 /**
156 * pin_is_valid() - check if pin exists on controller
157 * @pctldev: the pin control device to check the pin on
158 * @pin: pin to check, use the local pin controller index number
159 *
160 * This tells us whether a certain pin exist on a certain pin controller or
161 * not. Pin lists may be sparse, so some pins may not exist.
162 */
163 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
164 {
165 struct pin_desc *pindesc;
166
167 if (pin < 0)
168 return false;
169
170 pindesc = pin_desc_get(pctldev, pin);
171 if (pindesc == NULL)
172 return false;
173
174 return true;
175 }
176 EXPORT_SYMBOL_GPL(pin_is_valid);
177
178 /* Deletes a range of pin descriptors */
179 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
180 const struct pinctrl_pin_desc *pins,
181 unsigned num_pins)
182 {
183 int i;
184
185 spin_lock(&pctldev->pin_desc_tree_lock);
186 for (i = 0; i < num_pins; i++) {
187 struct pin_desc *pindesc;
188
189 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
190 pins[i].number);
191 if (pindesc != NULL) {
192 radix_tree_delete(&pctldev->pin_desc_tree,
193 pins[i].number);
194 if (pindesc->dynamic_name)
195 kfree(pindesc->name);
196 }
197 kfree(pindesc);
198 }
199 spin_unlock(&pctldev->pin_desc_tree_lock);
200 }
201
202 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
203 unsigned number, const char *name)
204 {
205 struct pin_desc *pindesc;
206
207 pindesc = pin_desc_get(pctldev, number);
208 if (pindesc != NULL) {
209 pr_err("pin %d already registered on %s\n", number,
210 pctldev->desc->name);
211 return -EINVAL;
212 }
213
214 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
215 if (pindesc == NULL) {
216 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
217 return -ENOMEM;
218 }
219
220 spin_lock_init(&pindesc->lock);
221
222 /* Set owner */
223 pindesc->pctldev = pctldev;
224
225 /* Copy basic pin info */
226 if (name) {
227 pindesc->name = name;
228 } else {
229 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
230 if (pindesc->name == NULL)
231 return -ENOMEM;
232 pindesc->dynamic_name = true;
233 }
234
235 spin_lock(&pctldev->pin_desc_tree_lock);
236 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
237 spin_unlock(&pctldev->pin_desc_tree_lock);
238 pr_debug("registered pin %d (%s) on %s\n",
239 number, pindesc->name, pctldev->desc->name);
240 return 0;
241 }
242
243 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
244 struct pinctrl_pin_desc const *pins,
245 unsigned num_descs)
246 {
247 unsigned i;
248 int ret = 0;
249
250 for (i = 0; i < num_descs; i++) {
251 ret = pinctrl_register_one_pin(pctldev,
252 pins[i].number, pins[i].name);
253 if (ret)
254 return ret;
255 }
256
257 return 0;
258 }
259
260 /**
261 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
262 * @pctldev: pin controller device to check
263 * @gpio: gpio pin to check taken from the global GPIO pin space
264 *
265 * Tries to match a GPIO pin number to the ranges handled by a certain pin
266 * controller, return the range or NULL
267 */
268 static struct pinctrl_gpio_range *
269 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
270 {
271 struct pinctrl_gpio_range *range = NULL;
272
273 /* Loop over the ranges */
274 mutex_lock(&pctldev->gpio_ranges_lock);
275 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
276 /* Check if we're in the valid range */
277 if (gpio >= range->base &&
278 gpio < range->base + range->npins) {
279 mutex_unlock(&pctldev->gpio_ranges_lock);
280 return range;
281 }
282 }
283 mutex_unlock(&pctldev->gpio_ranges_lock);
284
285 return NULL;
286 }
287
288 /**
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
293 *
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
296 * negative if the GPIO range could not be found in any device.
297 */
298 static int pinctrl_get_device_gpio_range(unsigned gpio,
299 struct pinctrl_dev **outdev,
300 struct pinctrl_gpio_range **outrange)
301 {
302 struct pinctrl_dev *pctldev = NULL;
303
304 /* Loop over the pin controllers */
305 mutex_lock(&pinctrldev_list_mutex);
306 list_for_each_entry(pctldev, &pinctrldev_list, node) {
307 struct pinctrl_gpio_range *range;
308
309 range = pinctrl_match_gpio_range(pctldev, gpio);
310 if (range != NULL) {
311 *outdev = pctldev;
312 *outrange = range;
313 mutex_unlock(&pinctrldev_list_mutex);
314 return 0;
315 }
316 }
317 mutex_unlock(&pinctrldev_list_mutex);
318
319 return -EINVAL;
320 }
321
322 /**
323 * pinctrl_add_gpio_range() - register a GPIO range for a controller
324 * @pctldev: pin controller device to add the range to
325 * @range: the GPIO range to add
326 *
327 * This adds a range of GPIOs to be handled by a certain pin controller. Call
328 * this to register handled ranges after registering your pin controller.
329 */
330 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
331 struct pinctrl_gpio_range *range)
332 {
333 mutex_lock(&pctldev->gpio_ranges_lock);
334 list_add_tail(&range->node, &pctldev->gpio_ranges);
335 mutex_unlock(&pctldev->gpio_ranges_lock);
336 }
337 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
338
339 /**
340 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
341 * @pctldev: pin controller device to remove the range from
342 * @range: the GPIO range to remove
343 */
344 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
345 struct pinctrl_gpio_range *range)
346 {
347 mutex_lock(&pctldev->gpio_ranges_lock);
348 list_del(&range->node);
349 mutex_unlock(&pctldev->gpio_ranges_lock);
350 }
351 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
352
353 /**
354 * pinctrl_get_group_selector() - returns the group selector for a group
355 * @pctldev: the pin controller handling the group
356 * @pin_group: the pin group to look up
357 */
358 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
359 const char *pin_group)
360 {
361 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
362 unsigned group_selector = 0;
363
364 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
365 const char *gname = pctlops->get_group_name(pctldev,
366 group_selector);
367 if (!strcmp(gname, pin_group)) {
368 dev_dbg(pctldev->dev,
369 "found group selector %u for %s\n",
370 group_selector,
371 pin_group);
372 return group_selector;
373 }
374
375 group_selector++;
376 }
377
378 dev_err(pctldev->dev, "does not have pin group %s\n",
379 pin_group);
380
381 return -EINVAL;
382 }
383
384 /**
385 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
386 * @gpio: the GPIO pin number from the GPIO subsystem number space
387 *
388 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
389 * as part of their gpio_request() semantics, platforms and individual drivers
390 * shall *NOT* request GPIO pins to be muxed in.
391 */
392 int pinctrl_request_gpio(unsigned gpio)
393 {
394 struct pinctrl_dev *pctldev;
395 struct pinctrl_gpio_range *range;
396 int ret;
397 int pin;
398
399 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
400 if (ret)
401 return -EINVAL;
402
403 /* Convert to the pin controllers number space */
404 pin = gpio - range->base + range->pin_base;
405
406 return pinmux_request_gpio(pctldev, range, pin, gpio);
407 }
408 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
409
410 /**
411 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
412 * @gpio: the GPIO pin number from the GPIO subsystem number space
413 *
414 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
415 * as part of their gpio_free() semantics, platforms and individual drivers
416 * shall *NOT* request GPIO pins to be muxed out.
417 */
418 void pinctrl_free_gpio(unsigned gpio)
419 {
420 struct pinctrl_dev *pctldev;
421 struct pinctrl_gpio_range *range;
422 int ret;
423 int pin;
424
425 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
426 if (ret)
427 return;
428
429 /* Convert to the pin controllers number space */
430 pin = gpio - range->base + range->pin_base;
431
432 return pinmux_free_gpio(pctldev, pin, range);
433 }
434 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
435
436 static int pinctrl_gpio_direction(unsigned gpio, bool input)
437 {
438 struct pinctrl_dev *pctldev;
439 struct pinctrl_gpio_range *range;
440 int ret;
441 int pin;
442
443 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
444 if (ret)
445 return ret;
446
447 /* Convert to the pin controllers number space */
448 pin = gpio - range->base + range->pin_base;
449
450 return pinmux_gpio_direction(pctldev, range, pin, input);
451 }
452
453 /**
454 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
455 * @gpio: the GPIO pin number from the GPIO subsystem number space
456 *
457 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
458 * as part of their gpio_direction_input() semantics, platforms and individual
459 * drivers shall *NOT* touch pin control GPIO calls.
460 */
461 int pinctrl_gpio_direction_input(unsigned gpio)
462 {
463 return pinctrl_gpio_direction(gpio, true);
464 }
465 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
466
467 /**
468 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
469 * @gpio: the GPIO pin number from the GPIO subsystem number space
470 *
471 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
472 * as part of their gpio_direction_output() semantics, platforms and individual
473 * drivers shall *NOT* touch pin control GPIO calls.
474 */
475 int pinctrl_gpio_direction_output(unsigned gpio)
476 {
477 return pinctrl_gpio_direction(gpio, false);
478 }
479 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
480
481 static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
482 {
483 struct pinctrl_dev *pctldev = NULL;
484 const char *devname;
485 struct pinctrl *p;
486 unsigned num_maps = 0;
487 int ret = -ENODEV;
488 struct pinctrl_maps *maps_node;
489 int i;
490 struct pinctrl_map const *map;
491
492 /* We must have a dev name */
493 if (WARN_ON(!dev))
494 return ERR_PTR(-EINVAL);
495
496 devname = dev_name(dev);
497
498 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name);
499
500 /*
501 * create the state cookie holder struct pinctrl for each
502 * mapping, this is what consumers will get when requesting
503 * a pin control handle with pinctrl_get()
504 */
505 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
506 if (p == NULL) {
507 dev_err(dev, "failed to alloc struct pinctrl\n");
508 return ERR_PTR(-ENOMEM);
509 }
510 mutex_init(&p->mutex);
511 pinmux_init_pinctrl_handle(p);
512
513 /* Iterate over the pin control maps to locate the right ones */
514 for_each_maps(maps_node, i, map) {
515 /*
516 * First, try to find the pctldev given in the map
517 */
518 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
519 if (!pctldev) {
520 dev_err(dev, "unknown pinctrl device %s in map entry",
521 map->ctrl_dev_name);
522 pinmux_put(p);
523 kfree(p);
524 /* Eventually, this should trigger deferred probe */
525 return ERR_PTR(-ENODEV);
526 }
527
528 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
529 dev_name(pctldev->dev), map->function);
530
531 /* Map must be for this device */
532 if (strcmp(map->dev_name, devname))
533 continue;
534
535 /*
536 * If we're looking for a specific named map, this must match,
537 * else we loop and look for the next.
538 */
539 if (name != NULL) {
540 if (map->name == NULL)
541 continue;
542 if (strcmp(map->name, name))
543 continue;
544 }
545
546 ret = pinmux_apply_muxmap(pctldev, p, dev, devname, map);
547 if (ret) {
548 kfree(p);
549 return ERR_PTR(ret);
550 }
551 num_maps++;
552 }
553
554 /*
555 * This may be perfectly legitimate. An IP block may get re-used
556 * across SoCs. Not all of those SoCs may need pinmux settings for the
557 * IP block, e.g. if one SoC dedicates pins to that function but
558 * another doesn't. The driver won't know this, and will always
559 * attempt to set up the pinmux. The mapping table defines whether any
560 * HW programming is actually needed.
561 */
562 if (!num_maps)
563 dev_info(dev, "zero maps found for mapping %s\n", name);
564
565 dev_dbg(dev, "found %u maps for device %s state %s\n",
566 num_maps, devname, name ? name : "(undefined)");
567
568 /* Add the pinmux to the global list */
569 mutex_lock(&pinctrl_list_mutex);
570 list_add_tail(&p->node, &pinctrl_list);
571 mutex_unlock(&pinctrl_list_mutex);
572
573 return p;
574 }
575
576 /**
577 * pinctrl_get() - retrieves the pin controller handle for a certain device
578 * @dev: the device to get the pin controller handle for
579 * @name: an optional specific control mapping name or NULL, the name is only
580 * needed if you want to have more than one mapping per device, or if you
581 * need an anonymous pin control (not tied to any specific device)
582 */
583 struct pinctrl *pinctrl_get(struct device *dev, const char *name)
584 {
585 struct pinctrl *p;
586
587 mutex_lock(&pinctrl_maps_mutex);
588 p = pinctrl_get_locked(dev, name);
589 mutex_unlock(&pinctrl_maps_mutex);
590
591 return p;
592 }
593 EXPORT_SYMBOL_GPL(pinctrl_get);
594
595 /**
596 * pinctrl_put() - release a previously claimed pin control handle
597 * @p: a pin control handle previously claimed by pinctrl_get()
598 */
599 void pinctrl_put(struct pinctrl *p)
600 {
601 if (p == NULL)
602 return;
603
604 mutex_lock(&p->mutex);
605 if (p->usecount)
606 pr_warn("releasing pin control handle with active users!\n");
607 /* Free the groups and all acquired pins */
608 pinmux_put(p);
609 mutex_unlock(&p->mutex);
610
611 /* Remove from list */
612 mutex_lock(&pinctrl_list_mutex);
613 list_del(&p->node);
614 mutex_unlock(&pinctrl_list_mutex);
615
616 kfree(p);
617 }
618 EXPORT_SYMBOL_GPL(pinctrl_put);
619
620 /**
621 * pinctrl_enable() - enable a certain pin controller setting
622 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
623 */
624 int pinctrl_enable(struct pinctrl *p)
625 {
626 int ret = 0;
627
628 if (p == NULL)
629 return -EINVAL;
630 mutex_lock(&p->mutex);
631 if (p->usecount++ == 0) {
632 ret = pinmux_enable(p);
633 if (ret)
634 p->usecount--;
635 }
636 mutex_unlock(&p->mutex);
637 return ret;
638 }
639 EXPORT_SYMBOL_GPL(pinctrl_enable);
640
641 /**
642 * pinctrl_disable() - disable a certain pin control setting
643 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
644 */
645 void pinctrl_disable(struct pinctrl *p)
646 {
647 if (p == NULL)
648 return;
649
650 mutex_lock(&p->mutex);
651 if (--p->usecount == 0) {
652 pinmux_disable(p);
653 }
654 mutex_unlock(&p->mutex);
655 }
656 EXPORT_SYMBOL_GPL(pinctrl_disable);
657
658 /**
659 * pinctrl_register_mappings() - register a set of pin controller mappings
660 * @maps: the pincontrol mappings table to register. This should probably be
661 * marked with __initdata so it can be discarded after boot. This
662 * function will perform a shallow copy for the mapping entries.
663 * @num_maps: the number of maps in the mapping table
664 */
665 int pinctrl_register_mappings(struct pinctrl_map const *maps,
666 unsigned num_maps)
667 {
668 int i;
669 struct pinctrl_maps *maps_node;
670
671 pr_debug("add %d pinmux maps\n", num_maps);
672
673 /* First sanity check the new mapping */
674 for (i = 0; i < num_maps; i++) {
675 if (!maps[i].name) {
676 pr_err("failed to register map %d: no map name given\n",
677 i);
678 return -EINVAL;
679 }
680
681 if (!maps[i].ctrl_dev_name) {
682 pr_err("failed to register map %s (%d): no pin control device given\n",
683 maps[i].name, i);
684 return -EINVAL;
685 }
686
687 if (!maps[i].function) {
688 pr_err("failed to register map %s (%d): no function ID given\n",
689 maps[i].name, i);
690 return -EINVAL;
691 }
692
693 if (!maps[i].dev_name) {
694 pr_err("failed to register map %s (%d): no device given\n",
695 maps[i].name, i);
696 return -EINVAL;
697 }
698 }
699
700 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
701 if (!maps_node) {
702 pr_err("failed to alloc struct pinctrl_maps\n");
703 return -ENOMEM;
704 }
705
706 maps_node->num_maps = num_maps;
707 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
708 if (!maps_node->maps) {
709 pr_err("failed to duplicate mapping table\n");
710 kfree(maps_node);
711 return -ENOMEM;
712 }
713
714 mutex_lock(&pinctrl_maps_mutex);
715 list_add_tail(&maps_node->node, &pinctrl_maps);
716 mutex_unlock(&pinctrl_maps_mutex);
717
718 return 0;
719 }
720
721 /* Hog a single map entry and add to the hoglist */
722 static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
723 struct pinctrl_map const *map)
724 {
725 struct pinctrl_hog *hog;
726 struct pinctrl *p;
727 int ret;
728
729 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
730 if (!hog) {
731 dev_err(pctldev->dev, "failed to alloc struct pinctrl_hog\n");
732 return -ENOMEM;
733 }
734
735 p = pinctrl_get_locked(pctldev->dev, map->name);
736 if (IS_ERR(p)) {
737 kfree(hog);
738 dev_err(pctldev->dev,
739 "could not get the %s pin control mapping for hogging\n",
740 map->name);
741 return PTR_ERR(p);
742 }
743
744 ret = pinctrl_enable(p);
745 if (ret) {
746 pinctrl_put(p);
747 kfree(hog);
748 dev_err(pctldev->dev,
749 "could not enable the %s pin control mapping for hogging\n",
750 map->name);
751 return ret;
752 }
753
754 hog->map = map;
755 hog->p = p;
756
757 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
758 map->function);
759 mutex_lock(&pctldev->pinctrl_hogs_lock);
760 list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
761 mutex_unlock(&pctldev->pinctrl_hogs_lock);
762
763 return 0;
764 }
765
766 /**
767 * pinctrl_hog_maps() - hog specific map entries on controller device
768 * @pctldev: the pin control device to hog entries on
769 *
770 * When the pin controllers are registered, there may be some specific pinmux
771 * map entries that need to be hogged, i.e. get+enabled until the system shuts
772 * down.
773 */
774 static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
775 {
776 struct device *dev = pctldev->dev;
777 const char *devname = dev_name(dev);
778 int ret;
779 struct pinctrl_maps *maps_node;
780 int i;
781 struct pinctrl_map const *map;
782
783 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
784 mutex_init(&pctldev->pinctrl_hogs_lock);
785
786 mutex_lock(&pinctrl_maps_mutex);
787 for_each_maps(maps_node, i, map) {
788 if (!strcmp(map->ctrl_dev_name, devname) &&
789 !strcmp(map->dev_name, devname)) {
790 /* OK time to hog! */
791 ret = pinctrl_hog_map(pctldev, map);
792 if (ret) {
793 mutex_unlock(&pinctrl_maps_mutex);
794 return ret;
795 }
796 }
797 }
798 mutex_unlock(&pinctrl_maps_mutex);
799
800 return 0;
801 }
802
803 /**
804 * pinctrl_unhog_maps() - unhog specific map entries on controller device
805 * @pctldev: the pin control device to unhog entries on
806 */
807 static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
808 {
809 struct list_head *node, *tmp;
810
811 mutex_lock(&pctldev->pinctrl_hogs_lock);
812 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
813 struct pinctrl_hog *hog =
814 list_entry(node, struct pinctrl_hog, node);
815 pinctrl_disable(hog->p);
816 pinctrl_put(hog->p);
817 list_del(node);
818 kfree(hog);
819 }
820 mutex_unlock(&pctldev->pinctrl_hogs_lock);
821 }
822
823 #ifdef CONFIG_DEBUG_FS
824
825 static int pinctrl_pins_show(struct seq_file *s, void *what)
826 {
827 struct pinctrl_dev *pctldev = s->private;
828 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
829 unsigned i, pin;
830
831 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
832
833 /* The pin number can be retrived from the pin controller descriptor */
834 for (i = 0; i < pctldev->desc->npins; i++) {
835 struct pin_desc *desc;
836
837 pin = pctldev->desc->pins[i].number;
838 desc = pin_desc_get(pctldev, pin);
839 /* Pin space may be sparse */
840 if (desc == NULL)
841 continue;
842
843 seq_printf(s, "pin %d (%s) ", pin,
844 desc->name ? desc->name : "unnamed");
845
846 /* Driver-specific info per pin */
847 if (ops->pin_dbg_show)
848 ops->pin_dbg_show(pctldev, s, pin);
849
850 seq_puts(s, "\n");
851 }
852
853 return 0;
854 }
855
856 static int pinctrl_groups_show(struct seq_file *s, void *what)
857 {
858 struct pinctrl_dev *pctldev = s->private;
859 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
860 unsigned selector = 0;
861
862 /* No grouping */
863 if (!ops)
864 return 0;
865
866 seq_puts(s, "registered pin groups:\n");
867 while (ops->list_groups(pctldev, selector) >= 0) {
868 const unsigned *pins;
869 unsigned num_pins;
870 const char *gname = ops->get_group_name(pctldev, selector);
871 int ret;
872 int i;
873
874 ret = ops->get_group_pins(pctldev, selector,
875 &pins, &num_pins);
876 if (ret)
877 seq_printf(s, "%s [ERROR GETTING PINS]\n",
878 gname);
879 else {
880 seq_printf(s, "group: %s, pins = [ ", gname);
881 for (i = 0; i < num_pins; i++)
882 seq_printf(s, "%d ", pins[i]);
883 seq_puts(s, "]\n");
884 }
885 selector++;
886 }
887
888
889 return 0;
890 }
891
892 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
893 {
894 struct pinctrl_dev *pctldev = s->private;
895 struct pinctrl_gpio_range *range = NULL;
896
897 seq_puts(s, "GPIO ranges handled:\n");
898
899 /* Loop over the ranges */
900 mutex_lock(&pctldev->gpio_ranges_lock);
901 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
902 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
903 range->id, range->name,
904 range->base, (range->base + range->npins - 1),
905 range->pin_base,
906 (range->pin_base + range->npins - 1));
907 }
908 mutex_unlock(&pctldev->gpio_ranges_lock);
909
910 return 0;
911 }
912
913 static int pinctrl_maps_show(struct seq_file *s, void *what)
914 {
915 struct pinctrl_maps *maps_node;
916 int i;
917 struct pinctrl_map const *map;
918
919 seq_puts(s, "Pinctrl maps:\n");
920
921 mutex_lock(&pinctrl_maps_mutex);
922 for_each_maps(maps_node, i, map) {
923 seq_printf(s, "%s:\n", map->name);
924 seq_printf(s, " device: %s\n", map->dev_name);
925 seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
926 seq_printf(s, " function: %s\n", map->function);
927 seq_printf(s, " group: %s\n", map->group ? map->group :
928 "(default)");
929 }
930 mutex_unlock(&pinctrl_maps_mutex);
931
932 return 0;
933 }
934
935 static int pinmux_hogs_show(struct seq_file *s, void *what)
936 {
937 struct pinctrl_dev *pctldev = s->private;
938 struct pinctrl_hog *hog;
939
940 seq_puts(s, "Pin control map hogs held by device\n");
941
942 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
943 seq_printf(s, "%s\n", hog->map->name);
944
945 return 0;
946 }
947
948 static int pinctrl_devices_show(struct seq_file *s, void *what)
949 {
950 struct pinctrl_dev *pctldev;
951
952 seq_puts(s, "name [pinmux] [pinconf]\n");
953 mutex_lock(&pinctrldev_list_mutex);
954 list_for_each_entry(pctldev, &pinctrldev_list, node) {
955 seq_printf(s, "%s ", pctldev->desc->name);
956 if (pctldev->desc->pmxops)
957 seq_puts(s, "yes ");
958 else
959 seq_puts(s, "no ");
960 if (pctldev->desc->confops)
961 seq_puts(s, "yes");
962 else
963 seq_puts(s, "no");
964 seq_puts(s, "\n");
965 }
966 mutex_unlock(&pinctrldev_list_mutex);
967
968 return 0;
969 }
970
971 static int pinctrl_show(struct seq_file *s, void *what)
972 {
973 struct pinctrl *p;
974
975 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
976 list_for_each_entry(p, &pinctrl_list, node) {
977 struct pinctrl_dev *pctldev = p->pctldev;
978
979 if (!pctldev) {
980 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
981 continue;
982 }
983
984 seq_printf(s, "device: %s",
985 pinctrl_dev_get_name(p->pctldev));
986
987 pinmux_dbg_show(s, p);
988
989 seq_printf(s, " users: %u map-> %s\n",
990 p->usecount,
991 p->dev ? dev_name(p->dev) : "(system)");
992 }
993
994 return 0;
995 }
996
997 static int pinctrl_pins_open(struct inode *inode, struct file *file)
998 {
999 return single_open(file, pinctrl_pins_show, inode->i_private);
1000 }
1001
1002 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1003 {
1004 return single_open(file, pinctrl_groups_show, inode->i_private);
1005 }
1006
1007 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1008 {
1009 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1010 }
1011
1012 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1013 {
1014 return single_open(file, pinctrl_maps_show, inode->i_private);
1015 }
1016
1017 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1018 {
1019 return single_open(file, pinmux_hogs_show, inode->i_private);
1020 }
1021
1022 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1023 {
1024 return single_open(file, pinctrl_devices_show, NULL);
1025 }
1026
1027 static int pinctrl_open(struct inode *inode, struct file *file)
1028 {
1029 return single_open(file, pinctrl_show, NULL);
1030 }
1031
1032 static const struct file_operations pinctrl_pins_ops = {
1033 .open = pinctrl_pins_open,
1034 .read = seq_read,
1035 .llseek = seq_lseek,
1036 .release = single_release,
1037 };
1038
1039 static const struct file_operations pinctrl_groups_ops = {
1040 .open = pinctrl_groups_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1044 };
1045
1046 static const struct file_operations pinctrl_gpioranges_ops = {
1047 .open = pinctrl_gpioranges_open,
1048 .read = seq_read,
1049 .llseek = seq_lseek,
1050 .release = single_release,
1051 };
1052
1053 static const struct file_operations pinctrl_maps_ops = {
1054 .open = pinctrl_maps_open,
1055 .read = seq_read,
1056 .llseek = seq_lseek,
1057 .release = single_release,
1058 };
1059
1060 static const struct file_operations pinmux_hogs_ops = {
1061 .open = pinmux_hogs_open,
1062 .read = seq_read,
1063 .llseek = seq_lseek,
1064 .release = single_release,
1065 };
1066
1067 static const struct file_operations pinctrl_devices_ops = {
1068 .open = pinctrl_devices_open,
1069 .read = seq_read,
1070 .llseek = seq_lseek,
1071 .release = single_release,
1072 };
1073
1074 static const struct file_operations pinctrl_ops = {
1075 .open = pinctrl_open,
1076 .read = seq_read,
1077 .llseek = seq_lseek,
1078 .release = single_release,
1079 };
1080
1081 static struct dentry *debugfs_root;
1082
1083 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1084 {
1085 struct dentry *device_root;
1086
1087 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1088 debugfs_root);
1089 pctldev->device_root = device_root;
1090
1091 if (IS_ERR(device_root) || !device_root) {
1092 pr_warn("failed to create debugfs directory for %s\n",
1093 dev_name(pctldev->dev));
1094 return;
1095 }
1096 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1097 device_root, pctldev, &pinctrl_pins_ops);
1098 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1099 device_root, pctldev, &pinctrl_groups_ops);
1100 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1101 device_root, pctldev, &pinctrl_gpioranges_ops);
1102 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1103 device_root, pctldev, &pinctrl_maps_ops);
1104 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1105 device_root, pctldev, &pinmux_hogs_ops);
1106 pinmux_init_device_debugfs(device_root, pctldev);
1107 pinconf_init_device_debugfs(device_root, pctldev);
1108 }
1109
1110 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1111 {
1112 debugfs_remove_recursive(pctldev->device_root);
1113 }
1114
1115 static void pinctrl_init_debugfs(void)
1116 {
1117 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1118 if (IS_ERR(debugfs_root) || !debugfs_root) {
1119 pr_warn("failed to create debugfs directory\n");
1120 debugfs_root = NULL;
1121 return;
1122 }
1123
1124 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1125 debugfs_root, NULL, &pinctrl_devices_ops);
1126 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1127 debugfs_root, NULL, &pinctrl_ops);
1128 }
1129
1130 #else /* CONFIG_DEBUG_FS */
1131
1132 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1133 {
1134 }
1135
1136 static void pinctrl_init_debugfs(void)
1137 {
1138 }
1139
1140 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1141 {
1142 }
1143
1144 #endif
1145
1146 /**
1147 * pinctrl_register() - register a pin controller device
1148 * @pctldesc: descriptor for this pin controller
1149 * @dev: parent device for this pin controller
1150 * @driver_data: private pin controller data for this pin controller
1151 */
1152 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1153 struct device *dev, void *driver_data)
1154 {
1155 struct pinctrl_dev *pctldev;
1156 int ret;
1157
1158 if (pctldesc == NULL)
1159 return NULL;
1160 if (pctldesc->name == NULL)
1161 return NULL;
1162
1163 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
1164 if (pctldev == NULL) {
1165 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1166 return NULL;
1167 }
1168
1169 /* Initialize pin control device struct */
1170 pctldev->owner = pctldesc->owner;
1171 pctldev->desc = pctldesc;
1172 pctldev->driver_data = driver_data;
1173 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1174 spin_lock_init(&pctldev->pin_desc_tree_lock);
1175 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1176 mutex_init(&pctldev->gpio_ranges_lock);
1177 pctldev->dev = dev;
1178
1179 /* If we're implementing pinmuxing, check the ops for sanity */
1180 if (pctldesc->pmxops) {
1181 ret = pinmux_check_ops(pctldev);
1182 if (ret) {
1183 pr_err("%s pinmux ops lacks necessary functions\n",
1184 pctldesc->name);
1185 goto out_err;
1186 }
1187 }
1188
1189 /* If we're implementing pinconfig, check the ops for sanity */
1190 if (pctldesc->confops) {
1191 ret = pinconf_check_ops(pctldev);
1192 if (ret) {
1193 pr_err("%s pin config ops lacks necessary functions\n",
1194 pctldesc->name);
1195 goto out_err;
1196 }
1197 }
1198
1199 /* Register all the pins */
1200 pr_debug("try to register %d pins on %s...\n",
1201 pctldesc->npins, pctldesc->name);
1202 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1203 if (ret) {
1204 pr_err("error during pin registration\n");
1205 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1206 pctldesc->npins);
1207 goto out_err;
1208 }
1209
1210 pinctrl_init_device_debugfs(pctldev);
1211 mutex_lock(&pinctrldev_list_mutex);
1212 list_add_tail(&pctldev->node, &pinctrldev_list);
1213 mutex_unlock(&pinctrldev_list_mutex);
1214 pinctrl_hog_maps(pctldev);
1215 return pctldev;
1216
1217 out_err:
1218 kfree(pctldev);
1219 return NULL;
1220 }
1221 EXPORT_SYMBOL_GPL(pinctrl_register);
1222
1223 /**
1224 * pinctrl_unregister() - unregister pinmux
1225 * @pctldev: pin controller to unregister
1226 *
1227 * Called by pinmux drivers to unregister a pinmux.
1228 */
1229 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1230 {
1231 if (pctldev == NULL)
1232 return;
1233
1234 pinctrl_remove_device_debugfs(pctldev);
1235 pinctrl_unhog_maps(pctldev);
1236 /* TODO: check that no pinmuxes are still active? */
1237 mutex_lock(&pinctrldev_list_mutex);
1238 list_del(&pctldev->node);
1239 mutex_unlock(&pinctrldev_list_mutex);
1240 /* Destroy descriptor tree */
1241 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1242 pctldev->desc->npins);
1243 kfree(pctldev);
1244 }
1245 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1246
1247 static int __init pinctrl_init(void)
1248 {
1249 pr_info("initialized pinctrl subsystem\n");
1250 pinctrl_init_debugfs();
1251 return 0;
1252 }
1253
1254 /* init early since many drivers really need to initialized pinmux early */
1255 core_initcall(pinctrl_init);
This page took 0.09654 seconds and 6 git commands to generate.