1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Core driver for the pin control subsystem
4 *
5 * Copyright (C) 2011-2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 */
13 #define pr_fmt(fmt) "pinctrl core: " fmt
14
15 #include <linux/array_size.h>
16 #include <linux/cleanup.h>
17 #include <linux/debugfs.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/export.h>
21 #include <linux/init.h>
22 #include <linux/kref.h>
23 #include <linux/list.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26
27 #include <linux/gpio.h>
28 #include <linux/gpio/driver.h>
29
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/pinctrl/devinfo.h>
32 #include <linux/pinctrl/machine.h>
33 #include <linux/pinctrl/pinctrl.h>
34
35 #include "core.h"
36 #include "devicetree.h"
37 #include "pinconf.h"
38 #include "pinmux.h"
39
40 static bool pinctrl_dummy_state;
41
42 /* Mutex taken to protect pinctrl_list */
43 static DEFINE_MUTEX(pinctrl_list_mutex);
44
45 /* Mutex taken to protect pinctrl_maps */
46 DEFINE_MUTEX(pinctrl_maps_mutex);
47
48 /* Mutex taken to protect pinctrldev_list */
49 static DEFINE_MUTEX(pinctrldev_list_mutex);
50
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 static LIST_HEAD(pinctrldev_list);
53
54 /* List of pin controller handles (struct pinctrl) */
55 static LIST_HEAD(pinctrl_list);
56
57 /* List of pinctrl maps (struct pinctrl_maps) */
58 LIST_HEAD(pinctrl_maps);
59
60
61 /**
62 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
63 *
64 * Usually this function is called by platforms without pinctrl driver support
65 * but run with some shared drivers using pinctrl APIs.
66 * After calling this function, the pinctrl core will return successfully
67 * with creating a dummy state for the driver to keep going smoothly.
68 */
pinctrl_provide_dummies(void)69 void pinctrl_provide_dummies(void)
70 {
71 pinctrl_dummy_state = true;
72 }
73 EXPORT_SYMBOL_GPL(pinctrl_provide_dummies);
74
pinctrl_dev_get_name(struct pinctrl_dev * pctldev)75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76 {
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79 }
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
pinctrl_dev_get_devname(struct pinctrl_dev * pctldev)82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83 {
84 return dev_name(pctldev->dev);
85 }
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
pinctrl_dev_get_drvdata(struct pinctrl_dev * pctldev)88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89 {
90 return pctldev->driver_data;
91 }
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94 /**
95 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
97 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
get_pinctrl_dev_from_devname(const char * devname)101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
102 {
103 struct pinctrl_dev *pctldev;
104
105 if (!devname)
106 return NULL;
107
108 mutex_lock(&pinctrldev_list_mutex);
109
110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
111 if (!strcmp(dev_name(pctldev->dev), devname)) {
112 /* Matched on device name */
113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
115 }
116 }
117
118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
121 }
122
get_pinctrl_dev_from_of_node(struct device_node * np)123 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124 {
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (device_match_of_node(pctldev->dev, np)) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
135 mutex_unlock(&pinctrldev_list_mutex);
136
137 return NULL;
138 }
139
140 /**
141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
pin_get_from_name(struct pinctrl_dev * pctldev,const char * name)145 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146 {
147 unsigned int i, pin;
148
149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
151 struct pin_desc *desc;
152
153 pin = pctldev->desc->pins[i].number;
154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
156 if (desc && !strcmp(name, desc->name))
157 return pin;
158 }
159
160 return -EINVAL;
161 }
162
163 /**
164 * pin_get_name() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @pin: pin number/id to look up
167 */
pin_get_name(struct pinctrl_dev * pctldev,const unsigned int pin)168 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin)
169 {
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
173 if (!desc) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180 }
181 EXPORT_SYMBOL_GPL(pin_get_name);
182
183 /* Deletes a range of pin descriptors */
pinctrl_free_pindescs(struct pinctrl_dev * pctldev,const struct pinctrl_pin_desc * pins,unsigned int num_pins)184 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
185 const struct pinctrl_pin_desc *pins,
186 unsigned int num_pins)
187 {
188 int i;
189
190 for (i = 0; i < num_pins; i++) {
191 struct pin_desc *pindesc;
192
193 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
194 pins[i].number);
195 if (pindesc) {
196 radix_tree_delete(&pctldev->pin_desc_tree,
197 pins[i].number);
198 if (pindesc->dynamic_name)
199 kfree(pindesc->name);
200 }
201 kfree(pindesc);
202 }
203 }
204
pinctrl_register_one_pin(struct pinctrl_dev * pctldev,const struct pinctrl_pin_desc * pin)205 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
206 const struct pinctrl_pin_desc *pin)
207 {
208 struct pin_desc *pindesc;
209 int error;
210
211 pindesc = pin_desc_get(pctldev, pin->number);
212 if (pindesc) {
213 dev_err(pctldev->dev, "pin %d already registered\n",
214 pin->number);
215 return -EINVAL;
216 }
217
218 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
219 if (!pindesc)
220 return -ENOMEM;
221
222 /* Set owner */
223 pindesc->pctldev = pctldev;
224 #ifdef CONFIG_PINMUX
225 mutex_init(&pindesc->mux_lock);
226 #endif
227
228 /* Copy basic pin info */
229 if (pin->name) {
230 pindesc->name = pin->name;
231 } else {
232 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
233 if (!pindesc->name) {
234 error = -ENOMEM;
235 goto failed;
236 }
237 pindesc->dynamic_name = true;
238 }
239
240 pindesc->drv_data = pin->drv_data;
241
242 error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
243 if (error)
244 goto failed;
245
246 pr_debug("registered pin %d (%s) on %s\n",
247 pin->number, pindesc->name, pctldev->desc->name);
248 return 0;
249
250 failed:
251 kfree(pindesc);
252 return error;
253 }
254
pinctrl_register_pins(struct pinctrl_dev * pctldev,const struct pinctrl_pin_desc * pins,unsigned int num_descs)255 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
256 const struct pinctrl_pin_desc *pins,
257 unsigned int num_descs)
258 {
259 unsigned int i;
260 int ret = 0;
261
262 for (i = 0; i < num_descs; i++) {
263 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
264 if (ret)
265 return ret;
266 }
267
268 return 0;
269 }
270
271 /**
272 * gpio_to_pin() - GPIO range GPIO number to pin number translation
273 * @range: GPIO range used for the translation
274 * @gc: GPIO chip structure from the GPIO subsystem
275 * @offset: hardware offset of the GPIO relative to the controller
276 *
277 * Finds the pin number for a given GPIO using the specified GPIO range
278 * as a base for translation. The distinction between linear GPIO ranges
279 * and pin list based GPIO ranges is managed correctly by this function.
280 *
281 * This function assumes the gpio is part of the specified GPIO range, use
282 * only after making sure this is the case (e.g. by calling it on the
283 * result of successful pinctrl_get_device_gpio_range calls)!
284 */
gpio_to_pin(struct pinctrl_gpio_range * range,struct gpio_chip * gc,unsigned int offset)285 static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
286 struct gpio_chip *gc, unsigned int offset)
287 {
288 unsigned int pin = gc->base + offset - range->base;
289 if (range->pins)
290 return range->pins[pin];
291 else
292 return range->pin_base + pin;
293 }
294
295 /**
296 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
297 * @pctldev: pin controller device to check
298 * @gc: GPIO chip structure from the GPIO subsystem
299 * @offset: hardware offset of the GPIO relative to the controller
300 *
301 * Tries to match a GPIO pin number to the ranges handled by a certain pin
302 * controller, return the range or NULL
303 */
304 static struct pinctrl_gpio_range *
pinctrl_match_gpio_range(struct pinctrl_dev * pctldev,struct gpio_chip * gc,unsigned int offset)305 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc,
306 unsigned int offset)
307 {
308 struct pinctrl_gpio_range *range;
309
310 mutex_lock(&pctldev->mutex);
311 /* Loop over the ranges */
312 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
313 /* Check if we're in the valid range */
314 if ((gc->base + offset) >= range->base &&
315 (gc->base + offset) < range->base + range->npins) {
316 mutex_unlock(&pctldev->mutex);
317 return range;
318 }
319 }
320 mutex_unlock(&pctldev->mutex);
321 return NULL;
322 }
323
324 /**
325 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
326 * the same GPIO chip are in range
327 * @gc: GPIO chip structure from the GPIO subsystem
328 * @offset: hardware offset of the GPIO relative to the controller
329 *
330 * This function is complement of pinctrl_match_gpio_range(). If the return
331 * value of pinctrl_match_gpio_range() is NULL, this function could be used
332 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
333 * of the same GPIO chip don't have back-end pinctrl interface.
334 * If the return value is true, it means that pinctrl device is ready & the
335 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
336 * is false, it means that pinctrl device may not be ready.
337 */
338 #ifdef CONFIG_GPIOLIB
pinctrl_ready_for_gpio_range(struct gpio_chip * gc,unsigned int offset)339 static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc,
340 unsigned int offset)
341 {
342 struct pinctrl_dev *pctldev;
343 struct pinctrl_gpio_range *range = NULL;
344
345 mutex_lock(&pinctrldev_list_mutex);
346
347 /* Loop over the pin controllers */
348 list_for_each_entry(pctldev, &pinctrldev_list, node) {
349 /* Loop over the ranges */
350 mutex_lock(&pctldev->mutex);
351 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
352 /* Check if any gpio range overlapped with gpio chip */
353 if (range->base + range->npins - 1 < gc->base ||
354 range->base > gc->base + gc->ngpio - 1)
355 continue;
356 mutex_unlock(&pctldev->mutex);
357 mutex_unlock(&pinctrldev_list_mutex);
358 return true;
359 }
360 mutex_unlock(&pctldev->mutex);
361 }
362
363 mutex_unlock(&pinctrldev_list_mutex);
364
365 return false;
366 }
367 #else
368 static inline bool
pinctrl_ready_for_gpio_range(struct gpio_chip * gc,unsigned int offset)369 pinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsigned int offset)
370 {
371 return true;
372 }
373 #endif
374
375 /**
376 * pinctrl_get_device_gpio_range() - find device for GPIO range
377 * @gc: GPIO chip structure from the GPIO subsystem
378 * @offset: hardware offset of the GPIO relative to the controller
379 * @outdev: the pin control device if found
380 * @outrange: the GPIO range if found
381 *
382 * Find the pin controller handling a certain GPIO pin from the pinspace of
383 * the GPIO subsystem, return the device and the matching GPIO range. Returns
384 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
385 * may still have not been registered.
386 */
pinctrl_get_device_gpio_range(struct gpio_chip * gc,unsigned int offset,struct pinctrl_dev ** outdev,struct pinctrl_gpio_range ** outrange)387 static int pinctrl_get_device_gpio_range(struct gpio_chip *gc,
388 unsigned int offset,
389 struct pinctrl_dev **outdev,
390 struct pinctrl_gpio_range **outrange)
391 {
392 struct pinctrl_dev *pctldev;
393
394 mutex_lock(&pinctrldev_list_mutex);
395
396 /* Loop over the pin controllers */
397 list_for_each_entry(pctldev, &pinctrldev_list, node) {
398 struct pinctrl_gpio_range *range;
399
400 range = pinctrl_match_gpio_range(pctldev, gc, offset);
401 if (range) {
402 *outdev = pctldev;
403 *outrange = range;
404 mutex_unlock(&pinctrldev_list_mutex);
405 return 0;
406 }
407 }
408
409 mutex_unlock(&pinctrldev_list_mutex);
410
411 return -EPROBE_DEFER;
412 }
413
414 /**
415 * pinctrl_add_gpio_range() - register a GPIO range for a controller
416 * @pctldev: pin controller device to add the range to
417 * @range: the GPIO range to add
418 *
419 * DEPRECATED: Don't use this function in new code. See section 2 of
420 * Documentation/devicetree/bindings/gpio/gpio.txt on how to bind pinctrl and
421 * gpio drivers.
422 *
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
425 */
pinctrl_add_gpio_range(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range)426 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range)
428 {
429 mutex_lock(&pctldev->mutex);
430 list_add_tail(&range->node, &pctldev->gpio_ranges);
431 mutex_unlock(&pctldev->mutex);
432 }
433 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
434
pinctrl_add_gpio_ranges(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * ranges,unsigned int nranges)435 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *ranges,
437 unsigned int nranges)
438 {
439 int i;
440
441 for (i = 0; i < nranges; i++)
442 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443 }
444 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445
pinctrl_find_and_add_gpio_range(const char * devname,struct pinctrl_gpio_range * range)446 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
447 struct pinctrl_gpio_range *range)
448 {
449 struct pinctrl_dev *pctldev;
450
451 pctldev = get_pinctrl_dev_from_devname(devname);
452
453 /*
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
457 */
458 if (!pctldev)
459 return ERR_PTR(-EPROBE_DEFER);
460
461 pinctrl_add_gpio_range(pctldev, range);
462
463 return pctldev;
464 }
465 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
466
pinctrl_get_group_pins(struct pinctrl_dev * pctldev,const char * pin_group,const unsigned int ** pins,unsigned int * num_pins)467 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 const unsigned int **pins, unsigned int *num_pins)
469 {
470 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 int gs;
472
473 if (!pctlops->get_group_pins)
474 return -EINVAL;
475
476 gs = pinctrl_get_group_selector(pctldev, pin_group);
477 if (gs < 0)
478 return gs;
479
480 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481 }
482 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483
484 struct pinctrl_gpio_range *
pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev * pctldev,unsigned int pin)485 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487 {
488 struct pinctrl_gpio_range *range;
489
490 /* Loop over the ranges */
491 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
492 /* Check if we're in the valid range */
493 if (range->pins) {
494 int a;
495 for (a = 0; a < range->npins; a++) {
496 if (range->pins[a] == pin)
497 return range;
498 }
499 } else if (pin >= range->pin_base &&
500 pin < range->pin_base + range->npins)
501 return range;
502 }
503
504 return NULL;
505 }
506 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
507
508 /**
509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
512 */
513 struct pinctrl_gpio_range *
pinctrl_find_gpio_range_from_pin(struct pinctrl_dev * pctldev,unsigned int pin)514 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
515 unsigned int pin)
516 {
517 struct pinctrl_gpio_range *range;
518
519 mutex_lock(&pctldev->mutex);
520 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
521 mutex_unlock(&pctldev->mutex);
522
523 return range;
524 }
525 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
526
527 /**
528 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
531 */
pinctrl_remove_gpio_range(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range)532 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
533 struct pinctrl_gpio_range *range)
534 {
535 mutex_lock(&pctldev->mutex);
536 list_del(&range->node);
537 mutex_unlock(&pctldev->mutex);
538 }
539 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540
541 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
542
543 /**
544 * pinctrl_generic_get_group_count() - returns the number of pin groups
545 * @pctldev: pin controller device
546 */
pinctrl_generic_get_group_count(struct pinctrl_dev * pctldev)547 int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
548 {
549 return pctldev->num_groups;
550 }
551 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
552
553 /**
554 * pinctrl_generic_get_group_name() - returns the name of a pin group
555 * @pctldev: pin controller device
556 * @selector: group number
557 */
pinctrl_generic_get_group_name(struct pinctrl_dev * pctldev,unsigned int selector)558 const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
559 unsigned int selector)
560 {
561 struct group_desc *group;
562
563 group = radix_tree_lookup(&pctldev->pin_group_tree,
564 selector);
565 if (!group)
566 return NULL;
567
568 return group->grp.name;
569 }
570 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
571
572 /**
573 * pinctrl_generic_get_group_pins() - gets the pin group pins
574 * @pctldev: pin controller device
575 * @selector: group number
576 * @pins: pins in the group
577 * @num_pins: number of pins in the group
578 */
pinctrl_generic_get_group_pins(struct pinctrl_dev * pctldev,unsigned int selector,const unsigned int ** pins,unsigned int * num_pins)579 int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
580 unsigned int selector,
581 const unsigned int **pins,
582 unsigned int *num_pins)
583 {
584 struct group_desc *group;
585
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group) {
589 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
590 __func__, selector);
591 return -EINVAL;
592 }
593
594 *pins = group->grp.pins;
595 *num_pins = group->grp.npins;
596
597 return 0;
598 }
599 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
600
601 /**
602 * pinctrl_generic_get_group() - returns a pin group based on the number
603 * @pctldev: pin controller device
604 * @selector: group number
605 */
pinctrl_generic_get_group(struct pinctrl_dev * pctldev,unsigned int selector)606 struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
607 unsigned int selector)
608 {
609 struct group_desc *group;
610
611 group = radix_tree_lookup(&pctldev->pin_group_tree,
612 selector);
613 if (!group)
614 return NULL;
615
616 return group;
617 }
618 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
619
pinctrl_generic_group_name_to_selector(struct pinctrl_dev * pctldev,const char * function)620 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
621 const char *function)
622 {
623 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
624 int ngroups = ops->get_groups_count(pctldev);
625 int selector = 0;
626
627 /* See if this pctldev has this group */
628 while (selector < ngroups) {
629 const char *gname = ops->get_group_name(pctldev, selector);
630
631 if (gname && !strcmp(function, gname))
632 return selector;
633
634 selector++;
635 }
636
637 return -EINVAL;
638 }
639
640 /**
641 * pinctrl_generic_add_group() - adds a new pin group
642 * @pctldev: pin controller device
643 * @name: name of the pin group
644 * @pins: pins in the pin group
645 * @num_pins: number of pins in the pin group
646 * @data: pin controller driver specific data
647 *
648 * Note that the caller must take care of locking.
649 */
pinctrl_generic_add_group(struct pinctrl_dev * pctldev,const char * name,const unsigned int * pins,int num_pins,void * data)650 int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
651 const unsigned int *pins, int num_pins, void *data)
652 {
653 struct group_desc *group;
654 int selector, error;
655
656 if (!name)
657 return -EINVAL;
658
659 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
660 if (selector >= 0)
661 return selector;
662
663 selector = pctldev->num_groups;
664
665 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
666 if (!group)
667 return -ENOMEM;
668
669 *group = PINCTRL_GROUP_DESC(name, pins, num_pins, data);
670
671 error = radix_tree_insert(&pctldev->pin_group_tree, selector, group);
672 if (error)
673 return error;
674
675 pctldev->num_groups++;
676
677 return selector;
678 }
679 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
680
681 /**
682 * pinctrl_generic_remove_group() - removes a numbered pin group
683 * @pctldev: pin controller device
684 * @selector: group number
685 *
686 * Note that the caller must take care of locking.
687 */
pinctrl_generic_remove_group(struct pinctrl_dev * pctldev,unsigned int selector)688 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
689 unsigned int selector)
690 {
691 struct group_desc *group;
692
693 group = radix_tree_lookup(&pctldev->pin_group_tree,
694 selector);
695 if (!group)
696 return -ENOENT;
697
698 radix_tree_delete(&pctldev->pin_group_tree, selector);
699 devm_kfree(pctldev->dev, group);
700
701 pctldev->num_groups--;
702
703 return 0;
704 }
705 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
706
707 /**
708 * pinctrl_generic_free_groups() - removes all pin groups
709 * @pctldev: pin controller device
710 *
711 * Note that the caller must take care of locking. The pinctrl groups
712 * are allocated with devm_kzalloc() so no need to free them here.
713 */
pinctrl_generic_free_groups(struct pinctrl_dev * pctldev)714 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
715 {
716 struct radix_tree_iter iter;
717 void __rcu **slot;
718
719 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
720 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
721
722 pctldev->num_groups = 0;
723 }
724
725 #else
pinctrl_generic_free_groups(struct pinctrl_dev * pctldev)726 static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
727 {
728 }
729 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
730
731 /**
732 * pinctrl_get_group_selector() - returns the group selector for a group
733 * @pctldev: the pin controller handling the group
734 * @pin_group: the pin group to look up
735 */
pinctrl_get_group_selector(struct pinctrl_dev * pctldev,const char * pin_group)736 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
737 const char *pin_group)
738 {
739 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
740 unsigned int ngroups = pctlops->get_groups_count(pctldev);
741 unsigned int group_selector = 0;
742
743 while (group_selector < ngroups) {
744 const char *gname = pctlops->get_group_name(pctldev,
745 group_selector);
746 if (gname && !strcmp(gname, pin_group)) {
747 dev_dbg(pctldev->dev,
748 "found group selector %u for %s\n",
749 group_selector,
750 pin_group);
751 return group_selector;
752 }
753
754 group_selector++;
755 }
756
757 dev_err(pctldev->dev, "does not have pin group %s\n",
758 pin_group);
759
760 return -EINVAL;
761 }
762
pinctrl_gpio_can_use_line(struct gpio_chip * gc,unsigned int offset)763 bool pinctrl_gpio_can_use_line(struct gpio_chip *gc, unsigned int offset)
764 {
765 struct pinctrl_dev *pctldev;
766 struct pinctrl_gpio_range *range;
767 bool result;
768 int pin;
769
770 /*
771 * Try to obtain GPIO range, if it fails
772 * we're probably dealing with GPIO driver
773 * without a backing pin controller - bail out.
774 */
775 if (pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range))
776 return true;
777
778 mutex_lock(&pctldev->mutex);
779
780 /* Convert to the pin controllers number space */
781 pin = gpio_to_pin(range, gc, offset);
782
783 result = pinmux_can_be_used_for_gpio(pctldev, pin);
784
785 mutex_unlock(&pctldev->mutex);
786
787 return result;
788 }
789 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
790
791 /**
792 * pinctrl_gpio_request() - request a single pin to be used as GPIO
793 * @gc: GPIO chip structure from the GPIO subsystem
794 * @offset: hardware offset of the GPIO relative to the controller
795 *
796 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
797 * as part of their gpio_request() semantics, platforms and individual drivers
798 * shall *NOT* request GPIO pins to be muxed in.
799 */
pinctrl_gpio_request(struct gpio_chip * gc,unsigned int offset)800 int pinctrl_gpio_request(struct gpio_chip *gc, unsigned int offset)
801 {
802 struct pinctrl_gpio_range *range;
803 struct pinctrl_dev *pctldev;
804 int ret, pin;
805
806 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
807 if (ret) {
808 if (pinctrl_ready_for_gpio_range(gc, offset))
809 ret = 0;
810 return ret;
811 }
812
813 mutex_lock(&pctldev->mutex);
814
815 /* Convert to the pin controllers number space */
816 pin = gpio_to_pin(range, gc, offset);
817
818 ret = pinmux_request_gpio(pctldev, range, pin, gc->base + offset);
819
820 mutex_unlock(&pctldev->mutex);
821
822 return ret;
823 }
824 EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
825
826 /**
827 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
828 * @gc: GPIO chip structure from the GPIO subsystem
829 * @offset: hardware offset of the GPIO relative to the controller
830 *
831 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
832 * as part of their gpio_request() semantics, platforms and individual drivers
833 * shall *NOT* request GPIO pins to be muxed in.
834 */
pinctrl_gpio_free(struct gpio_chip * gc,unsigned int offset)835 void pinctrl_gpio_free(struct gpio_chip *gc, unsigned int offset)
836 {
837 struct pinctrl_gpio_range *range;
838 struct pinctrl_dev *pctldev;
839 int ret, pin;
840
841 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
842 if (ret)
843 return;
844
845 mutex_lock(&pctldev->mutex);
846
847 /* Convert to the pin controllers number space */
848 pin = gpio_to_pin(range, gc, offset);
849
850 pinmux_free_gpio(pctldev, pin, range);
851
852 mutex_unlock(&pctldev->mutex);
853 }
854 EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
855
pinctrl_gpio_direction(struct gpio_chip * gc,unsigned int offset,bool input)856 static int pinctrl_gpio_direction(struct gpio_chip *gc, unsigned int offset,
857 bool input)
858 {
859 struct pinctrl_dev *pctldev;
860 struct pinctrl_gpio_range *range;
861 int ret;
862 int pin;
863
864 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
865 if (ret) {
866 return ret;
867 }
868
869 mutex_lock(&pctldev->mutex);
870
871 /* Convert to the pin controllers number space */
872 pin = gpio_to_pin(range, gc, offset);
873 ret = pinmux_gpio_direction(pctldev, range, pin, input);
874
875 mutex_unlock(&pctldev->mutex);
876
877 return ret;
878 }
879
880 /**
881 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
882 * @gc: GPIO chip structure from the GPIO subsystem
883 * @offset: hardware offset of the GPIO relative to the controller
884 *
885 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
886 * as part of their gpio_direction_input() semantics, platforms and individual
887 * drivers shall *NOT* touch pin control GPIO calls.
888 */
pinctrl_gpio_direction_input(struct gpio_chip * gc,unsigned int offset)889 int pinctrl_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
890 {
891 return pinctrl_gpio_direction(gc, offset, true);
892 }
893 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
894
895 /**
896 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
897 * @gc: GPIO chip structure from the GPIO subsystem
898 * @offset: hardware offset of the GPIO relative to the controller
899 *
900 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
901 * as part of their gpio_direction_output() semantics, platforms and individual
902 * drivers shall *NOT* touch pin control GPIO calls.
903 */
pinctrl_gpio_direction_output(struct gpio_chip * gc,unsigned int offset)904 int pinctrl_gpio_direction_output(struct gpio_chip *gc, unsigned int offset)
905 {
906 return pinctrl_gpio_direction(gc, offset, false);
907 }
908 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
909
910 /**
911 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
912 * @gc: GPIO chip structure from the GPIO subsystem
913 * @offset: hardware offset of the GPIO relative to the controller
914 * @config: the configuration to apply to the GPIO
915 *
916 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
917 * they need to call the underlying pin controller to change GPIO config
918 * (for example set debounce time).
919 */
pinctrl_gpio_set_config(struct gpio_chip * gc,unsigned int offset,unsigned long config)920 int pinctrl_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
921 unsigned long config)
922 {
923 unsigned long configs[] = { config };
924 struct pinctrl_gpio_range *range;
925 struct pinctrl_dev *pctldev;
926 int ret, pin;
927
928 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
929 if (ret)
930 return ret;
931
932 mutex_lock(&pctldev->mutex);
933 pin = gpio_to_pin(range, gc, offset);
934 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
935 mutex_unlock(&pctldev->mutex);
936
937 return ret;
938 }
939 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
940
find_state(struct pinctrl * p,const char * name)941 static struct pinctrl_state *find_state(struct pinctrl *p,
942 const char *name)
943 {
944 struct pinctrl_state *state;
945
946 list_for_each_entry(state, &p->states, node)
947 if (!strcmp(state->name, name))
948 return state;
949
950 return NULL;
951 }
952
create_state(struct pinctrl * p,const char * name)953 static struct pinctrl_state *create_state(struct pinctrl *p,
954 const char *name)
955 {
956 struct pinctrl_state *state;
957
958 state = kzalloc(sizeof(*state), GFP_KERNEL);
959 if (!state)
960 return ERR_PTR(-ENOMEM);
961
962 state->name = name;
963 INIT_LIST_HEAD(&state->settings);
964
965 list_add_tail(&state->node, &p->states);
966
967 return state;
968 }
969
add_setting(struct pinctrl * p,struct pinctrl_dev * pctldev,const struct pinctrl_map * map)970 static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
971 const struct pinctrl_map *map)
972 {
973 struct pinctrl_state *state;
974 struct pinctrl_setting *setting;
975 int ret;
976
977 state = find_state(p, map->name);
978 if (!state)
979 state = create_state(p, map->name);
980 if (IS_ERR(state))
981 return PTR_ERR(state);
982
983 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
984 return 0;
985
986 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
987 if (!setting)
988 return -ENOMEM;
989
990 setting->type = map->type;
991
992 if (pctldev)
993 setting->pctldev = pctldev;
994 else
995 setting->pctldev =
996 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
997 if (!setting->pctldev) {
998 kfree(setting);
999 /* Do not defer probing of hogs (circular loop) */
1000 if (!strcmp(map->ctrl_dev_name, map->dev_name))
1001 return -ENODEV;
1002 /*
1003 * OK let us guess that the driver is not there yet, and
1004 * let's defer obtaining this pinctrl handle to later...
1005 */
1006 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
1007 map->ctrl_dev_name);
1008 return -EPROBE_DEFER;
1009 }
1010
1011 setting->dev_name = map->dev_name;
1012
1013 switch (map->type) {
1014 case PIN_MAP_TYPE_MUX_GROUP:
1015 ret = pinmux_map_to_setting(map, setting);
1016 break;
1017 case PIN_MAP_TYPE_CONFIGS_PIN:
1018 case PIN_MAP_TYPE_CONFIGS_GROUP:
1019 ret = pinconf_map_to_setting(map, setting);
1020 break;
1021 default:
1022 ret = -EINVAL;
1023 break;
1024 }
1025 if (ret < 0) {
1026 kfree(setting);
1027 return ret;
1028 }
1029
1030 list_add_tail(&setting->node, &state->settings);
1031
1032 return 0;
1033 }
1034
find_pinctrl(struct device * dev)1035 static struct pinctrl *find_pinctrl(struct device *dev)
1036 {
1037 struct pinctrl *p;
1038
1039 mutex_lock(&pinctrl_list_mutex);
1040 list_for_each_entry(p, &pinctrl_list, node)
1041 if (p->dev == dev) {
1042 mutex_unlock(&pinctrl_list_mutex);
1043 return p;
1044 }
1045
1046 mutex_unlock(&pinctrl_list_mutex);
1047 return NULL;
1048 }
1049
1050 static void pinctrl_free(struct pinctrl *p, bool inlist);
1051
create_pinctrl(struct device * dev,struct pinctrl_dev * pctldev)1052 static struct pinctrl *create_pinctrl(struct device *dev,
1053 struct pinctrl_dev *pctldev)
1054 {
1055 struct pinctrl *p;
1056 const char *devname;
1057 struct pinctrl_maps *maps_node;
1058 const struct pinctrl_map *map;
1059 int ret;
1060
1061 /*
1062 * create the state cookie holder struct pinctrl for each
1063 * mapping, this is what consumers will get when requesting
1064 * a pin control handle with pinctrl_get()
1065 */
1066 p = kzalloc(sizeof(*p), GFP_KERNEL);
1067 if (!p)
1068 return ERR_PTR(-ENOMEM);
1069 p->dev = dev;
1070 INIT_LIST_HEAD(&p->states);
1071 INIT_LIST_HEAD(&p->dt_maps);
1072
1073 ret = pinctrl_dt_to_map(p, pctldev);
1074 if (ret < 0) {
1075 kfree(p);
1076 return ERR_PTR(ret);
1077 }
1078
1079 devname = dev_name(dev);
1080
1081 mutex_lock(&pinctrl_maps_mutex);
1082 /* Iterate over the pin control maps to locate the right ones */
1083 for_each_pin_map(maps_node, map) {
1084 /* Map must be for this device */
1085 if (strcmp(map->dev_name, devname))
1086 continue;
1087 /*
1088 * If pctldev is not null, we are claiming hog for it,
1089 * that means, setting that is served by pctldev by itself.
1090 *
1091 * Thus we must skip map that is for this device but is served
1092 * by other device.
1093 */
1094 if (pctldev &&
1095 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1096 continue;
1097
1098 ret = add_setting(p, pctldev, map);
1099 /*
1100 * At this point the adding of a setting may:
1101 *
1102 * - Defer, if the pinctrl device is not yet available
1103 * - Fail, if the pinctrl device is not yet available,
1104 * AND the setting is a hog. We cannot defer that, since
1105 * the hog will kick in immediately after the device
1106 * is registered.
1107 *
1108 * If the error returned was not -EPROBE_DEFER then we
1109 * accumulate the errors to see if we end up with
1110 * an -EPROBE_DEFER later, as that is the worst case.
1111 */
1112 if (ret == -EPROBE_DEFER) {
1113 mutex_unlock(&pinctrl_maps_mutex);
1114 pinctrl_free(p, false);
1115 return ERR_PTR(ret);
1116 }
1117 }
1118 mutex_unlock(&pinctrl_maps_mutex);
1119
1120 if (ret < 0) {
1121 /* If some other error than deferral occurred, return here */
1122 pinctrl_free(p, false);
1123 return ERR_PTR(ret);
1124 }
1125
1126 kref_init(&p->users);
1127
1128 /* Add the pinctrl handle to the global list */
1129 mutex_lock(&pinctrl_list_mutex);
1130 list_add_tail(&p->node, &pinctrl_list);
1131 mutex_unlock(&pinctrl_list_mutex);
1132
1133 return p;
1134 }
1135
1136 /**
1137 * pinctrl_get() - retrieves the pinctrl handle for a device
1138 * @dev: the device to obtain the handle for
1139 */
pinctrl_get(struct device * dev)1140 struct pinctrl *pinctrl_get(struct device *dev)
1141 {
1142 struct pinctrl *p;
1143
1144 if (WARN_ON(!dev))
1145 return ERR_PTR(-EINVAL);
1146
1147 /*
1148 * See if somebody else (such as the device core) has already
1149 * obtained a handle to the pinctrl for this device. In that case,
1150 * return another pointer to it.
1151 */
1152 p = find_pinctrl(dev);
1153 if (p) {
1154 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1155 kref_get(&p->users);
1156 return p;
1157 }
1158
1159 return create_pinctrl(dev, NULL);
1160 }
1161 EXPORT_SYMBOL_GPL(pinctrl_get);
1162
pinctrl_free_setting(bool disable_setting,struct pinctrl_setting * setting)1163 static void pinctrl_free_setting(bool disable_setting,
1164 struct pinctrl_setting *setting)
1165 {
1166 switch (setting->type) {
1167 case PIN_MAP_TYPE_MUX_GROUP:
1168 if (disable_setting)
1169 pinmux_disable_setting(setting);
1170 pinmux_free_setting(setting);
1171 break;
1172 case PIN_MAP_TYPE_CONFIGS_PIN:
1173 case PIN_MAP_TYPE_CONFIGS_GROUP:
1174 pinconf_free_setting(setting);
1175 break;
1176 default:
1177 break;
1178 }
1179 }
1180
pinctrl_free(struct pinctrl * p,bool inlist)1181 static void pinctrl_free(struct pinctrl *p, bool inlist)
1182 {
1183 struct pinctrl_state *state, *n1;
1184 struct pinctrl_setting *setting, *n2;
1185
1186 mutex_lock(&pinctrl_list_mutex);
1187 list_for_each_entry_safe(state, n1, &p->states, node) {
1188 list_for_each_entry_safe(setting, n2, &state->settings, node) {
1189 pinctrl_free_setting(state == p->state, setting);
1190 list_del(&setting->node);
1191 kfree(setting);
1192 }
1193 list_del(&state->node);
1194 kfree(state);
1195 }
1196
1197 pinctrl_dt_free_maps(p);
1198
1199 if (inlist)
1200 list_del(&p->node);
1201 kfree(p);
1202 mutex_unlock(&pinctrl_list_mutex);
1203 }
1204
1205 /**
1206 * pinctrl_release() - release the pinctrl handle
1207 * @kref: the kref in the pinctrl being released
1208 */
pinctrl_release(struct kref * kref)1209 static void pinctrl_release(struct kref *kref)
1210 {
1211 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1212
1213 pinctrl_free(p, true);
1214 }
1215
1216 /**
1217 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1218 * @p: the pinctrl handle to release
1219 */
pinctrl_put(struct pinctrl * p)1220 void pinctrl_put(struct pinctrl *p)
1221 {
1222 kref_put(&p->users, pinctrl_release);
1223 }
1224 EXPORT_SYMBOL_GPL(pinctrl_put);
1225
1226 /**
1227 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1228 * @p: the pinctrl handle to retrieve the state from
1229 * @name: the state name to retrieve
1230 */
pinctrl_lookup_state(struct pinctrl * p,const char * name)1231 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1232 const char *name)
1233 {
1234 struct pinctrl_state *state;
1235
1236 state = find_state(p, name);
1237 if (!state) {
1238 if (pinctrl_dummy_state) {
1239 /* create dummy state */
1240 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1241 name);
1242 state = create_state(p, name);
1243 } else
1244 state = ERR_PTR(-ENODEV);
1245 }
1246
1247 return state;
1248 }
1249 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1250
pinctrl_link_add(struct pinctrl_dev * pctldev,struct device * consumer)1251 static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1252 struct device *consumer)
1253 {
1254 if (pctldev->desc->link_consumers)
1255 device_link_add(consumer, pctldev->dev,
1256 DL_FLAG_PM_RUNTIME |
1257 DL_FLAG_AUTOREMOVE_CONSUMER);
1258 }
1259
pinctrl_cond_disable_mux_setting(struct pinctrl_state * state,struct pinctrl_setting * target_setting)1260 static void pinctrl_cond_disable_mux_setting(struct pinctrl_state *state,
1261 struct pinctrl_setting *target_setting)
1262 {
1263 struct pinctrl_setting *setting;
1264
1265 list_for_each_entry(setting, &state->settings, node) {
1266 if (target_setting && (&setting->node == &target_setting->node))
1267 break;
1268
1269 if (setting->type == PIN_MAP_TYPE_MUX_GROUP)
1270 pinmux_disable_setting(setting);
1271 }
1272 }
1273
1274 /**
1275 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1276 * @p: the pinctrl handle for the device that requests configuration
1277 * @state: the state handle to select/activate/program
1278 */
pinctrl_commit_state(struct pinctrl * p,struct pinctrl_state * state)1279 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
1280 {
1281 struct pinctrl_setting *setting;
1282 struct pinctrl_state *old_state = READ_ONCE(p->state);
1283 int ret;
1284
1285 if (old_state) {
1286 /*
1287 * For each pinmux setting in the old state, forget SW's record
1288 * of mux owner for that pingroup. Any pingroups which are
1289 * still owned by the new state will be re-acquired by the call
1290 * to pinmux_enable_setting() in the loop below.
1291 */
1292 pinctrl_cond_disable_mux_setting(old_state, NULL);
1293 }
1294
1295 p->state = NULL;
1296
1297 /* Apply all the settings for the new state - pinmux first */
1298 list_for_each_entry(setting, &state->settings, node) {
1299 switch (setting->type) {
1300 case PIN_MAP_TYPE_MUX_GROUP:
1301 ret = pinmux_enable_setting(setting);
1302 break;
1303 case PIN_MAP_TYPE_CONFIGS_PIN:
1304 case PIN_MAP_TYPE_CONFIGS_GROUP:
1305 ret = 0;
1306 break;
1307 default:
1308 ret = -EINVAL;
1309 break;
1310 }
1311
1312 if (ret < 0)
1313 goto unapply_new_state;
1314
1315 /* Do not link hogs (circular dependency) */
1316 if (p != setting->pctldev->p)
1317 pinctrl_link_add(setting->pctldev, p->dev);
1318 }
1319
1320 /* Apply all the settings for the new state - pinconf after */
1321 list_for_each_entry(setting, &state->settings, node) {
1322 switch (setting->type) {
1323 case PIN_MAP_TYPE_MUX_GROUP:
1324 ret = 0;
1325 break;
1326 case PIN_MAP_TYPE_CONFIGS_PIN:
1327 case PIN_MAP_TYPE_CONFIGS_GROUP:
1328 ret = pinconf_apply_setting(setting);
1329 break;
1330 default:
1331 ret = -EINVAL;
1332 break;
1333 }
1334
1335 if (ret < 0) {
1336 goto unapply_mux_setting;
1337 }
1338
1339 /* Do not link hogs (circular dependency) */
1340 if (p != setting->pctldev->p)
1341 pinctrl_link_add(setting->pctldev, p->dev);
1342 }
1343
1344 p->state = state;
1345
1346 return 0;
1347
1348 unapply_mux_setting:
1349 pinctrl_cond_disable_mux_setting(state, NULL);
1350 goto restore_old_state;
1351
1352 unapply_new_state:
1353 dev_err(p->dev, "Error applying setting, reverse things back\n");
1354
1355 /*
1356 * All we can do here is pinmux_disable_setting.
1357 * That means that some pins are muxed differently now
1358 * than they were before applying the setting (We can't
1359 * "unmux a pin"!), but it's not a big deal since the pins
1360 * are free to be muxed by another apply_setting.
1361 */
1362 pinctrl_cond_disable_mux_setting(state, setting);
1363
1364 restore_old_state:
1365 /* There's no infinite recursive loop here because p->state is NULL */
1366 if (old_state)
1367 pinctrl_select_state(p, old_state);
1368
1369 return ret;
1370 }
1371
1372 /**
1373 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1374 * @p: the pinctrl handle for the device that requests configuration
1375 * @state: the state handle to select/activate/program
1376 */
pinctrl_select_state(struct pinctrl * p,struct pinctrl_state * state)1377 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1378 {
1379 if (p->state == state)
1380 return 0;
1381
1382 return pinctrl_commit_state(p, state);
1383 }
1384 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1385
devm_pinctrl_release(struct device * dev,void * res)1386 static void devm_pinctrl_release(struct device *dev, void *res)
1387 {
1388 pinctrl_put(*(struct pinctrl **)res);
1389 }
1390
1391 /**
1392 * devm_pinctrl_get() - Resource managed pinctrl_get()
1393 * @dev: the device to obtain the handle for
1394 *
1395 * If there is a need to explicitly destroy the returned struct pinctrl,
1396 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1397 */
devm_pinctrl_get(struct device * dev)1398 struct pinctrl *devm_pinctrl_get(struct device *dev)
1399 {
1400 struct pinctrl **ptr, *p;
1401
1402 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1403 if (!ptr)
1404 return ERR_PTR(-ENOMEM);
1405
1406 p = pinctrl_get(dev);
1407 if (!IS_ERR(p)) {
1408 *ptr = p;
1409 devres_add(dev, ptr);
1410 } else {
1411 devres_free(ptr);
1412 }
1413
1414 return p;
1415 }
1416 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1417
devm_pinctrl_match(struct device * dev,void * res,void * data)1418 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1419 {
1420 struct pinctrl **p = res;
1421
1422 return *p == data;
1423 }
1424
1425 /**
1426 * devm_pinctrl_put() - Resource managed pinctrl_put()
1427 * @p: the pinctrl handle to release
1428 *
1429 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1430 * this function will not need to be called and the resource management
1431 * code will ensure that the resource is freed.
1432 */
devm_pinctrl_put(struct pinctrl * p)1433 void devm_pinctrl_put(struct pinctrl *p)
1434 {
1435 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1436 devm_pinctrl_match, p));
1437 }
1438 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1439
1440 /**
1441 * pinctrl_register_mappings() - register a set of pin controller mappings
1442 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1443 * keeps a reference to the passed in maps, so they should _not_ be
1444 * marked with __initdata.
1445 * @num_maps: the number of maps in the mapping table
1446 */
pinctrl_register_mappings(const struct pinctrl_map * maps,unsigned int num_maps)1447 int pinctrl_register_mappings(const struct pinctrl_map *maps,
1448 unsigned int num_maps)
1449 {
1450 int i, ret;
1451 struct pinctrl_maps *maps_node;
1452
1453 pr_debug("add %u pinctrl maps\n", num_maps);
1454
1455 /* First sanity check the new mapping */
1456 for (i = 0; i < num_maps; i++) {
1457 if (!maps[i].dev_name) {
1458 pr_err("failed to register map %s (%d): no device given\n",
1459 maps[i].name, i);
1460 return -EINVAL;
1461 }
1462
1463 if (!maps[i].name) {
1464 pr_err("failed to register map %d: no map name given\n",
1465 i);
1466 return -EINVAL;
1467 }
1468
1469 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1470 !maps[i].ctrl_dev_name) {
1471 pr_err("failed to register map %s (%d): no pin control device given\n",
1472 maps[i].name, i);
1473 return -EINVAL;
1474 }
1475
1476 switch (maps[i].type) {
1477 case PIN_MAP_TYPE_DUMMY_STATE:
1478 break;
1479 case PIN_MAP_TYPE_MUX_GROUP:
1480 ret = pinmux_validate_map(&maps[i], i);
1481 if (ret < 0)
1482 return ret;
1483 break;
1484 case PIN_MAP_TYPE_CONFIGS_PIN:
1485 case PIN_MAP_TYPE_CONFIGS_GROUP:
1486 ret = pinconf_validate_map(&maps[i], i);
1487 if (ret < 0)
1488 return ret;
1489 break;
1490 default:
1491 pr_err("failed to register map %s (%d): invalid type given\n",
1492 maps[i].name, i);
1493 return -EINVAL;
1494 }
1495 }
1496
1497 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1498 if (!maps_node)
1499 return -ENOMEM;
1500
1501 maps_node->maps = maps;
1502 maps_node->num_maps = num_maps;
1503
1504 mutex_lock(&pinctrl_maps_mutex);
1505 list_add_tail(&maps_node->node, &pinctrl_maps);
1506 mutex_unlock(&pinctrl_maps_mutex);
1507
1508 return 0;
1509 }
1510 EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
1511
1512 /**
1513 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1514 * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
1515 * when registering the mappings.
1516 */
pinctrl_unregister_mappings(const struct pinctrl_map * map)1517 void pinctrl_unregister_mappings(const struct pinctrl_map *map)
1518 {
1519 struct pinctrl_maps *maps_node;
1520
1521 mutex_lock(&pinctrl_maps_mutex);
1522 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1523 if (maps_node->maps == map) {
1524 list_del(&maps_node->node);
1525 kfree(maps_node);
1526 mutex_unlock(&pinctrl_maps_mutex);
1527 return;
1528 }
1529 }
1530 mutex_unlock(&pinctrl_maps_mutex);
1531 }
1532 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
1533
devm_pinctrl_unregister_mappings(void * maps)1534 static void devm_pinctrl_unregister_mappings(void *maps)
1535 {
1536 pinctrl_unregister_mappings(maps);
1537 }
1538
1539 /**
1540 * devm_pinctrl_register_mappings() - Resource managed pinctrl_register_mappings()
1541 * @dev: device for which mappings are registered
1542 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1543 * keeps a reference to the passed in maps, so they should _not_ be
1544 * marked with __initdata.
1545 * @num_maps: the number of maps in the mapping table
1546 *
1547 * Returns: 0 on success, or negative errno on failure.
1548 */
devm_pinctrl_register_mappings(struct device * dev,const struct pinctrl_map * maps,unsigned int num_maps)1549 int devm_pinctrl_register_mappings(struct device *dev,
1550 const struct pinctrl_map *maps,
1551 unsigned int num_maps)
1552 {
1553 int ret;
1554
1555 ret = pinctrl_register_mappings(maps, num_maps);
1556 if (ret)
1557 return ret;
1558
1559 return devm_add_action_or_reset(dev, devm_pinctrl_unregister_mappings, (void *)maps);
1560 }
1561 EXPORT_SYMBOL_GPL(devm_pinctrl_register_mappings);
1562
1563 /**
1564 * pinctrl_force_sleep() - turn a given controller device into sleep state
1565 * @pctldev: pin controller device
1566 */
pinctrl_force_sleep(struct pinctrl_dev * pctldev)1567 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1568 {
1569 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1570 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
1571 return 0;
1572 }
1573 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1574
1575 /**
1576 * pinctrl_force_default() - turn a given controller device into default state
1577 * @pctldev: pin controller device
1578 */
pinctrl_force_default(struct pinctrl_dev * pctldev)1579 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1580 {
1581 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1582 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
1583 return 0;
1584 }
1585 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1586
1587 /**
1588 * pinctrl_init_done() - tell pinctrl probe is done
1589 *
1590 * We'll use this time to switch the pins from "init" to "default" unless the
1591 * driver selected some other state.
1592 *
1593 * @dev: device to that's done probing
1594 */
pinctrl_init_done(struct device * dev)1595 int pinctrl_init_done(struct device *dev)
1596 {
1597 struct dev_pin_info *pins = dev->pins;
1598 int ret;
1599
1600 if (!pins)
1601 return 0;
1602
1603 if (IS_ERR(pins->init_state))
1604 return 0; /* No such state */
1605
1606 if (pins->p->state != pins->init_state)
1607 return 0; /* Not at init anyway */
1608
1609 if (IS_ERR(pins->default_state))
1610 return 0; /* No default state */
1611
1612 ret = pinctrl_select_state(pins->p, pins->default_state);
1613 if (ret)
1614 dev_err(dev, "failed to activate default pinctrl state\n");
1615
1616 return ret;
1617 }
1618
pinctrl_select_bound_state(struct device * dev,struct pinctrl_state * state)1619 static int pinctrl_select_bound_state(struct device *dev,
1620 struct pinctrl_state *state)
1621 {
1622 struct dev_pin_info *pins = dev->pins;
1623 int ret;
1624
1625 if (IS_ERR(state))
1626 return 0; /* No such state */
1627 ret = pinctrl_select_state(pins->p, state);
1628 if (ret)
1629 dev_err(dev, "failed to activate pinctrl state %s\n",
1630 state->name);
1631 return ret;
1632 }
1633
1634 /**
1635 * pinctrl_select_default_state() - select default pinctrl state
1636 * @dev: device to select default state for
1637 */
pinctrl_select_default_state(struct device * dev)1638 int pinctrl_select_default_state(struct device *dev)
1639 {
1640 if (!dev->pins)
1641 return 0;
1642
1643 return pinctrl_select_bound_state(dev, dev->pins->default_state);
1644 }
1645 EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1646
1647 #ifdef CONFIG_PM
1648
1649 /**
1650 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1651 * @dev: device to select default state for
1652 */
pinctrl_pm_select_default_state(struct device * dev)1653 int pinctrl_pm_select_default_state(struct device *dev)
1654 {
1655 return pinctrl_select_default_state(dev);
1656 }
1657 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1658
1659 /**
1660 * pinctrl_pm_select_init_state() - select init pinctrl state for PM
1661 * @dev: device to select init state for
1662 */
pinctrl_pm_select_init_state(struct device * dev)1663 int pinctrl_pm_select_init_state(struct device *dev)
1664 {
1665 if (!dev->pins)
1666 return 0;
1667
1668 return pinctrl_select_bound_state(dev, dev->pins->init_state);
1669 }
1670 EXPORT_SYMBOL_GPL(pinctrl_pm_select_init_state);
1671
1672 /**
1673 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1674 * @dev: device to select sleep state for
1675 */
pinctrl_pm_select_sleep_state(struct device * dev)1676 int pinctrl_pm_select_sleep_state(struct device *dev)
1677 {
1678 if (!dev->pins)
1679 return 0;
1680
1681 return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
1682 }
1683 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1684
1685 /**
1686 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1687 * @dev: device to select idle state for
1688 */
pinctrl_pm_select_idle_state(struct device * dev)1689 int pinctrl_pm_select_idle_state(struct device *dev)
1690 {
1691 if (!dev->pins)
1692 return 0;
1693
1694 return pinctrl_select_bound_state(dev, dev->pins->idle_state);
1695 }
1696 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1697 #endif
1698
1699 #ifdef CONFIG_DEBUG_FS
1700
pinctrl_pins_show(struct seq_file * s,void * what)1701 static int pinctrl_pins_show(struct seq_file *s, void *what)
1702 {
1703 struct pinctrl_dev *pctldev = s->private;
1704 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1705 unsigned int i, pin;
1706 #ifdef CONFIG_GPIOLIB
1707 struct gpio_device *gdev = NULL;
1708 struct pinctrl_gpio_range *range;
1709 int gpio_num;
1710 #endif
1711
1712 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1713
1714 mutex_lock(&pctldev->mutex);
1715
1716 /* The pin number can be retrived from the pin controller descriptor */
1717 for (i = 0; i < pctldev->desc->npins; i++) {
1718 struct pin_desc *desc;
1719
1720 pin = pctldev->desc->pins[i].number;
1721 desc = pin_desc_get(pctldev, pin);
1722 /* Pin space may be sparse */
1723 if (!desc)
1724 continue;
1725
1726 seq_printf(s, "pin %d (%s) ", pin, desc->name);
1727
1728 #ifdef CONFIG_GPIOLIB
1729 gdev = NULL;
1730 gpio_num = -1;
1731 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1732 if (range->pins != NULL) {
1733 for (int i = 0; i < range->npins; ++i) {
1734 if (range->pins[i] == pin) {
1735 gpio_num = range->base + i;
1736 break;
1737 }
1738 }
1739 } else if ((pin >= range->pin_base) &&
1740 (pin < (range->pin_base + range->npins))) {
1741 gpio_num =
1742 range->base + (pin - range->pin_base);
1743 }
1744 if (gpio_num != -1)
1745 break;
1746 }
1747 if (gpio_num >= 0)
1748 /*
1749 * FIXME: gpio_num comes from the global GPIO numberspace.
1750 * we need to get rid of the range->base eventually and
1751 * get the descriptor directly from the gpio_chip.
1752 */
1753 gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num));
1754 if (gdev)
1755 seq_printf(s, "%u:%s ",
1756 gpio_num - gpio_device_get_base(gdev),
1757 gpio_device_get_label(gdev));
1758 else
1759 seq_puts(s, "0:? ");
1760 #endif
1761
1762 /* Driver-specific info per pin */
1763 if (ops->pin_dbg_show)
1764 ops->pin_dbg_show(pctldev, s, pin);
1765
1766 seq_puts(s, "\n");
1767 }
1768
1769 mutex_unlock(&pctldev->mutex);
1770
1771 return 0;
1772 }
1773 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
1774
pinctrl_groups_show(struct seq_file * s,void * what)1775 static int pinctrl_groups_show(struct seq_file *s, void *what)
1776 {
1777 struct pinctrl_dev *pctldev = s->private;
1778 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1779 unsigned int ngroups, selector = 0;
1780
1781 mutex_lock(&pctldev->mutex);
1782
1783 ngroups = ops->get_groups_count(pctldev);
1784
1785 seq_puts(s, "registered pin groups:\n");
1786 while (selector < ngroups) {
1787 const unsigned int *pins = NULL;
1788 unsigned int num_pins = 0;
1789 const char *gname = ops->get_group_name(pctldev, selector);
1790 const char *pname;
1791 int ret = 0;
1792 int i;
1793
1794 if (ops->get_group_pins)
1795 ret = ops->get_group_pins(pctldev, selector,
1796 &pins, &num_pins);
1797 if (ret)
1798 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1799 gname);
1800 else {
1801 seq_printf(s, "group: %s\n", gname);
1802 for (i = 0; i < num_pins; i++) {
1803 pname = pin_get_name(pctldev, pins[i]);
1804 if (WARN_ON(!pname)) {
1805 mutex_unlock(&pctldev->mutex);
1806 return -EINVAL;
1807 }
1808 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1809 }
1810 seq_puts(s, "\n");
1811 }
1812 selector++;
1813 }
1814
1815 mutex_unlock(&pctldev->mutex);
1816
1817 return 0;
1818 }
1819 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
1820
pinctrl_gpioranges_show(struct seq_file * s,void * what)1821 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1822 {
1823 struct pinctrl_dev *pctldev = s->private;
1824 struct pinctrl_gpio_range *range;
1825
1826 seq_puts(s, "GPIO ranges handled:\n");
1827
1828 mutex_lock(&pctldev->mutex);
1829
1830 /* Loop over the ranges */
1831 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1832 if (range->pins) {
1833 int a;
1834 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1835 range->id, range->name,
1836 range->base, (range->base + range->npins - 1));
1837 for (a = 0; a < range->npins - 1; a++)
1838 seq_printf(s, "%u, ", range->pins[a]);
1839 seq_printf(s, "%u}\n", range->pins[a]);
1840 }
1841 else
1842 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1843 range->id, range->name,
1844 range->base, (range->base + range->npins - 1),
1845 range->pin_base,
1846 (range->pin_base + range->npins - 1));
1847 }
1848
1849 mutex_unlock(&pctldev->mutex);
1850
1851 return 0;
1852 }
1853 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
1854
pinctrl_devices_show(struct seq_file * s,void * what)1855 static int pinctrl_devices_show(struct seq_file *s, void *what)
1856 {
1857 struct pinctrl_dev *pctldev;
1858
1859 seq_puts(s, "name [pinmux] [pinconf]\n");
1860
1861 mutex_lock(&pinctrldev_list_mutex);
1862
1863 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1864 seq_printf(s, "%s ", pctldev->desc->name);
1865 if (pctldev->desc->pmxops)
1866 seq_puts(s, "yes ");
1867 else
1868 seq_puts(s, "no ");
1869 if (pctldev->desc->confops)
1870 seq_puts(s, "yes");
1871 else
1872 seq_puts(s, "no");
1873 seq_puts(s, "\n");
1874 }
1875
1876 mutex_unlock(&pinctrldev_list_mutex);
1877
1878 return 0;
1879 }
1880 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
1881
map_type(enum pinctrl_map_type type)1882 static inline const char *map_type(enum pinctrl_map_type type)
1883 {
1884 static const char * const names[] = {
1885 "INVALID",
1886 "DUMMY_STATE",
1887 "MUX_GROUP",
1888 "CONFIGS_PIN",
1889 "CONFIGS_GROUP",
1890 };
1891
1892 if (type >= ARRAY_SIZE(names))
1893 return "UNKNOWN";
1894
1895 return names[type];
1896 }
1897
pinctrl_maps_show(struct seq_file * s,void * what)1898 static int pinctrl_maps_show(struct seq_file *s, void *what)
1899 {
1900 struct pinctrl_maps *maps_node;
1901 const struct pinctrl_map *map;
1902
1903 seq_puts(s, "Pinctrl maps:\n");
1904
1905 mutex_lock(&pinctrl_maps_mutex);
1906 for_each_pin_map(maps_node, map) {
1907 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1908 map->dev_name, map->name, map_type(map->type),
1909 map->type);
1910
1911 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1912 seq_printf(s, "controlling device %s\n",
1913 map->ctrl_dev_name);
1914
1915 switch (map->type) {
1916 case PIN_MAP_TYPE_MUX_GROUP:
1917 pinmux_show_map(s, map);
1918 break;
1919 case PIN_MAP_TYPE_CONFIGS_PIN:
1920 case PIN_MAP_TYPE_CONFIGS_GROUP:
1921 pinconf_show_map(s, map);
1922 break;
1923 default:
1924 break;
1925 }
1926
1927 seq_putc(s, '\n');
1928 }
1929 mutex_unlock(&pinctrl_maps_mutex);
1930
1931 return 0;
1932 }
1933 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
1934
pinctrl_show(struct seq_file * s,void * what)1935 static int pinctrl_show(struct seq_file *s, void *what)
1936 {
1937 struct pinctrl *p;
1938 struct pinctrl_state *state;
1939 struct pinctrl_setting *setting;
1940
1941 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1942
1943 mutex_lock(&pinctrl_list_mutex);
1944
1945 list_for_each_entry(p, &pinctrl_list, node) {
1946 seq_printf(s, "device: %s current state: %s\n",
1947 dev_name(p->dev),
1948 p->state ? p->state->name : "none");
1949
1950 list_for_each_entry(state, &p->states, node) {
1951 seq_printf(s, " state: %s\n", state->name);
1952
1953 list_for_each_entry(setting, &state->settings, node) {
1954 struct pinctrl_dev *pctldev = setting->pctldev;
1955
1956 seq_printf(s, " type: %s controller %s ",
1957 map_type(setting->type),
1958 pinctrl_dev_get_name(pctldev));
1959
1960 switch (setting->type) {
1961 case PIN_MAP_TYPE_MUX_GROUP:
1962 pinmux_show_setting(s, setting);
1963 break;
1964 case PIN_MAP_TYPE_CONFIGS_PIN:
1965 case PIN_MAP_TYPE_CONFIGS_GROUP:
1966 pinconf_show_setting(s, setting);
1967 break;
1968 default:
1969 break;
1970 }
1971 }
1972 }
1973 }
1974
1975 mutex_unlock(&pinctrl_list_mutex);
1976
1977 return 0;
1978 }
1979 DEFINE_SHOW_ATTRIBUTE(pinctrl);
1980
1981 static struct dentry *debugfs_root;
1982
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)1983 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1984 {
1985 struct dentry *device_root;
1986 const char *debugfs_name;
1987
1988 if (pctldev->desc->name &&
1989 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1990 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1991 "%s-%s", dev_name(pctldev->dev),
1992 pctldev->desc->name);
1993 if (!debugfs_name) {
1994 pr_warn("failed to determine debugfs dir name for %s\n",
1995 dev_name(pctldev->dev));
1996 return;
1997 }
1998 } else {
1999 debugfs_name = dev_name(pctldev->dev);
2000 }
2001
2002 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
2003 pctldev->device_root = device_root;
2004
2005 if (IS_ERR(device_root) || !device_root) {
2006 pr_warn("failed to create debugfs directory for %s\n",
2007 dev_name(pctldev->dev));
2008 return;
2009 }
2010 debugfs_create_file("pins", 0444,
2011 device_root, pctldev, &pinctrl_pins_fops);
2012 debugfs_create_file("pingroups", 0444,
2013 device_root, pctldev, &pinctrl_groups_fops);
2014 debugfs_create_file("gpio-ranges", 0444,
2015 device_root, pctldev, &pinctrl_gpioranges_fops);
2016 if (pctldev->desc->pmxops)
2017 pinmux_init_device_debugfs(device_root, pctldev);
2018 if (pctldev->desc->confops)
2019 pinconf_init_device_debugfs(device_root, pctldev);
2020 }
2021
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)2022 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
2023 {
2024 debugfs_remove_recursive(pctldev->device_root);
2025 }
2026
pinctrl_init_debugfs(void)2027 static void pinctrl_init_debugfs(void)
2028 {
2029 debugfs_root = debugfs_create_dir("pinctrl", NULL);
2030 if (IS_ERR(debugfs_root)) {
2031 pr_warn("failed to create debugfs directory\n");
2032 debugfs_root = NULL;
2033 return;
2034 }
2035
2036 debugfs_create_file("pinctrl-devices", 0444,
2037 debugfs_root, NULL, &pinctrl_devices_fops);
2038 debugfs_create_file("pinctrl-maps", 0444,
2039 debugfs_root, NULL, &pinctrl_maps_fops);
2040 debugfs_create_file("pinctrl-handles", 0444,
2041 debugfs_root, NULL, &pinctrl_fops);
2042 }
2043
2044 #else /* CONFIG_DEBUG_FS */
2045
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)2046 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
2047 {
2048 }
2049
pinctrl_init_debugfs(void)2050 static void pinctrl_init_debugfs(void)
2051 {
2052 }
2053
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)2054 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
2055 {
2056 }
2057
2058 #endif
2059
pinctrl_check_ops(struct pinctrl_dev * pctldev)2060 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
2061 {
2062 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
2063
2064 if (!ops ||
2065 !ops->get_groups_count ||
2066 !ops->get_group_name)
2067 return -EINVAL;
2068
2069 return 0;
2070 }
2071
2072 /**
2073 * pinctrl_init_controller() - init a pin controller device
2074 * @pctldesc: descriptor for this pin controller
2075 * @dev: parent device for this pin controller
2076 * @driver_data: private pin controller data for this pin controller
2077 */
2078 static struct pinctrl_dev *
pinctrl_init_controller(const struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data)2079 pinctrl_init_controller(const struct pinctrl_desc *pctldesc, struct device *dev,
2080 void *driver_data)
2081 {
2082 struct pinctrl_dev *pctldev;
2083 int ret;
2084
2085 if (!pctldesc)
2086 return ERR_PTR(-EINVAL);
2087 if (!pctldesc->name)
2088 return ERR_PTR(-EINVAL);
2089
2090 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
2091 if (!pctldev)
2092 return ERR_PTR(-ENOMEM);
2093
2094 /* Initialize pin control device struct */
2095 pctldev->owner = pctldesc->owner;
2096 pctldev->desc = pctldesc;
2097 pctldev->driver_data = driver_data;
2098 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
2099 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
2100 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
2101 #endif
2102 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2103 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
2104 #endif
2105 INIT_LIST_HEAD(&pctldev->gpio_ranges);
2106 INIT_LIST_HEAD(&pctldev->node);
2107 pctldev->dev = dev;
2108 mutex_init(&pctldev->mutex);
2109
2110 /* check core ops for sanity */
2111 ret = pinctrl_check_ops(pctldev);
2112 if (ret) {
2113 dev_err(dev, "pinctrl ops lacks necessary functions\n");
2114 goto out_err;
2115 }
2116
2117 /* If we're implementing pinmuxing, check the ops for sanity */
2118 if (pctldesc->pmxops) {
2119 ret = pinmux_check_ops(pctldev);
2120 if (ret)
2121 goto out_err;
2122 }
2123
2124 /* If we're implementing pinconfig, check the ops for sanity */
2125 if (pctldesc->confops) {
2126 ret = pinconf_check_ops(pctldev);
2127 if (ret)
2128 goto out_err;
2129 }
2130
2131 /* Register all the pins */
2132 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
2133 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2134 if (ret) {
2135 dev_err(dev, "error during pin registration\n");
2136 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2137 pctldesc->npins);
2138 goto out_err;
2139 }
2140
2141 return pctldev;
2142
2143 out_err:
2144 mutex_destroy(&pctldev->mutex);
2145 kfree(pctldev);
2146 return ERR_PTR(ret);
2147 }
2148
pinctrl_uninit_controller(struct pinctrl_dev * pctldev,const struct pinctrl_desc * pctldesc)2149 static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev,
2150 const struct pinctrl_desc *pctldesc)
2151 {
2152 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2153 pctldesc->npins);
2154 mutex_destroy(&pctldev->mutex);
2155 kfree(pctldev);
2156 }
2157
pinctrl_claim_hogs(struct pinctrl_dev * pctldev)2158 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
2159 {
2160 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2161 if (PTR_ERR(pctldev->p) == -ENODEV) {
2162 dev_dbg(pctldev->dev, "no hogs found\n");
2163
2164 return 0;
2165 }
2166
2167 if (IS_ERR(pctldev->p)) {
2168 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2169 PTR_ERR(pctldev->p));
2170
2171 return PTR_ERR(pctldev->p);
2172 }
2173
2174 pctldev->hog_default =
2175 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2176 if (IS_ERR(pctldev->hog_default)) {
2177 dev_dbg(pctldev->dev,
2178 "failed to lookup the default state\n");
2179 } else {
2180 if (pinctrl_select_state(pctldev->p,
2181 pctldev->hog_default))
2182 dev_err(pctldev->dev,
2183 "failed to select default state\n");
2184 }
2185
2186 pctldev->hog_sleep =
2187 pinctrl_lookup_state(pctldev->p,
2188 PINCTRL_STATE_SLEEP);
2189 if (IS_ERR(pctldev->hog_sleep))
2190 dev_dbg(pctldev->dev,
2191 "failed to lookup the sleep state\n");
2192
2193 return 0;
2194 }
2195
pinctrl_enable(struct pinctrl_dev * pctldev)2196 int pinctrl_enable(struct pinctrl_dev *pctldev)
2197 {
2198 int error;
2199
2200 error = pinctrl_claim_hogs(pctldev);
2201 if (error) {
2202 dev_err(pctldev->dev, "could not claim hogs: %i\n", error);
2203 return error;
2204 }
2205
2206 mutex_lock(&pinctrldev_list_mutex);
2207 list_add_tail(&pctldev->node, &pinctrldev_list);
2208 mutex_unlock(&pinctrldev_list_mutex);
2209
2210 pinctrl_init_device_debugfs(pctldev);
2211
2212 return 0;
2213 }
2214 EXPORT_SYMBOL_GPL(pinctrl_enable);
2215
2216 /**
2217 * pinctrl_register() - register a pin controller device
2218 * @pctldesc: descriptor for this pin controller
2219 * @dev: parent device for this pin controller
2220 * @driver_data: private pin controller data for this pin controller
2221 *
2222 * Note that pinctrl_register() is known to have problems as the pin
2223 * controller driver functions are called before the driver has a
2224 * struct pinctrl_dev handle. To avoid issues later on, please use the
2225 * new pinctrl_register_and_init() below instead.
2226 */
pinctrl_register(const struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data)2227 struct pinctrl_dev *pinctrl_register(const struct pinctrl_desc *pctldesc,
2228 struct device *dev, void *driver_data)
2229 {
2230 struct pinctrl_dev *pctldev;
2231 int error;
2232
2233 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2234 if (IS_ERR(pctldev))
2235 return pctldev;
2236
2237 error = pinctrl_enable(pctldev);
2238 if (error) {
2239 pinctrl_uninit_controller(pctldev, pctldesc);
2240 return ERR_PTR(error);
2241 }
2242
2243 return pctldev;
2244 }
2245 EXPORT_SYMBOL_GPL(pinctrl_register);
2246
2247 /**
2248 * pinctrl_register_and_init() - register and init pin controller device
2249 * @pctldesc: descriptor for this pin controller
2250 * @dev: parent device for this pin controller
2251 * @driver_data: private pin controller data for this pin controller
2252 * @pctldev: pin controller device
2253 *
2254 * Note that pinctrl_enable() still needs to be manually called after
2255 * this once the driver is ready.
2256 */
pinctrl_register_and_init(const struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data,struct pinctrl_dev ** pctldev)2257 int pinctrl_register_and_init(const struct pinctrl_desc *pctldesc,
2258 struct device *dev, void *driver_data,
2259 struct pinctrl_dev **pctldev)
2260 {
2261 struct pinctrl_dev *p;
2262
2263 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2264 if (IS_ERR(p))
2265 return PTR_ERR(p);
2266
2267 /*
2268 * We have pinctrl_start() call functions in the pin controller
2269 * driver with create_pinctrl() for at least dt_node_to_map(). So
2270 * let's make sure pctldev is properly initialized for the
2271 * pin controller driver before we do anything.
2272 */
2273 *pctldev = p;
2274
2275 return 0;
2276 }
2277 EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2278
2279 /**
2280 * pinctrl_unregister() - unregister pinmux
2281 * @pctldev: pin controller to unregister
2282 *
2283 * Called by pinmux drivers to unregister a pinmux.
2284 */
pinctrl_unregister(struct pinctrl_dev * pctldev)2285 void pinctrl_unregister(struct pinctrl_dev *pctldev)
2286 {
2287 struct pinctrl_gpio_range *range, *n;
2288
2289 if (!pctldev)
2290 return;
2291
2292 mutex_lock(&pctldev->mutex);
2293 pinctrl_remove_device_debugfs(pctldev);
2294 mutex_unlock(&pctldev->mutex);
2295
2296 if (!IS_ERR_OR_NULL(pctldev->p))
2297 pinctrl_put(pctldev->p);
2298
2299 mutex_lock(&pinctrldev_list_mutex);
2300 mutex_lock(&pctldev->mutex);
2301 /* TODO: check that no pinmuxes are still active? */
2302 list_del(&pctldev->node);
2303 pinmux_generic_free_functions(pctldev);
2304 pinctrl_generic_free_groups(pctldev);
2305 /* Destroy descriptor tree */
2306 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2307 pctldev->desc->npins);
2308 /* remove gpio ranges map */
2309 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2310 list_del(&range->node);
2311
2312 mutex_unlock(&pctldev->mutex);
2313 mutex_destroy(&pctldev->mutex);
2314 kfree(pctldev);
2315 mutex_unlock(&pinctrldev_list_mutex);
2316 }
2317 EXPORT_SYMBOL_GPL(pinctrl_unregister);
2318
devm_pinctrl_dev_release(struct device * dev,void * res)2319 static void devm_pinctrl_dev_release(struct device *dev, void *res)
2320 {
2321 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2322
2323 pinctrl_unregister(pctldev);
2324 }
2325
devm_pinctrl_dev_match(struct device * dev,void * res,void * data)2326 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2327 {
2328 struct pctldev **r = res;
2329
2330 if (WARN_ON(!r || !*r))
2331 return 0;
2332
2333 return *r == data;
2334 }
2335
2336 /**
2337 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2338 * @dev: parent device for this pin controller
2339 * @pctldesc: descriptor for this pin controller
2340 * @driver_data: private pin controller data for this pin controller
2341 *
2342 * Returns an error pointer if pincontrol register failed. Otherwise
2343 * it returns valid pinctrl handle.
2344 *
2345 * The pinctrl device will be automatically released when the device is unbound.
2346 */
devm_pinctrl_register(struct device * dev,const struct pinctrl_desc * pctldesc,void * driver_data)2347 struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2348 const struct pinctrl_desc *pctldesc,
2349 void *driver_data)
2350 {
2351 struct pinctrl_dev **ptr, *pctldev;
2352
2353 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2354 if (!ptr)
2355 return ERR_PTR(-ENOMEM);
2356
2357 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2358 if (IS_ERR(pctldev)) {
2359 devres_free(ptr);
2360 return pctldev;
2361 }
2362
2363 *ptr = pctldev;
2364 devres_add(dev, ptr);
2365
2366 return pctldev;
2367 }
2368 EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2369
2370 /**
2371 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2372 * @dev: parent device for this pin controller
2373 * @pctldesc: descriptor for this pin controller
2374 * @driver_data: private pin controller data for this pin controller
2375 * @pctldev: pin controller device
2376 *
2377 * Returns zero on success or an error number on failure.
2378 *
2379 * The pinctrl device will be automatically released when the device is unbound.
2380 */
devm_pinctrl_register_and_init(struct device * dev,const struct pinctrl_desc * pctldesc,void * driver_data,struct pinctrl_dev ** pctldev)2381 int devm_pinctrl_register_and_init(struct device *dev,
2382 const struct pinctrl_desc *pctldesc,
2383 void *driver_data,
2384 struct pinctrl_dev **pctldev)
2385 {
2386 struct pinctrl_dev **ptr;
2387 int error;
2388
2389 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2390 if (!ptr)
2391 return -ENOMEM;
2392
2393 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2394 if (error) {
2395 devres_free(ptr);
2396 return error;
2397 }
2398
2399 *ptr = *pctldev;
2400 devres_add(dev, ptr);
2401
2402 return 0;
2403 }
2404 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2405
2406 /**
2407 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2408 * @dev: device for which resource was allocated
2409 * @pctldev: the pinctrl device to unregister.
2410 */
devm_pinctrl_unregister(struct device * dev,struct pinctrl_dev * pctldev)2411 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2412 {
2413 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2414 devm_pinctrl_dev_match, pctldev));
2415 }
2416 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2417
pinctrl_init(void)2418 static int __init pinctrl_init(void)
2419 {
2420 pr_debug("initialized pinctrl subsystem\n");
2421 pinctrl_init_debugfs();
2422 return 0;
2423 }
2424
2425 /* init early since many drivers really need to initialized pinmux early */
2426 core_initcall(pinctrl_init);
2427