core.c 55.6 KB
Newer Older
1 2 3
/*
 * Core driver for the pin control subsystem
 *
4
 * Copyright (C) 2011-2012 ST-Ericsson SA
5 6 7 8 9
 * Written on behalf of Linaro for ST-Ericsson
 * Based on bits of regulator core, gpio core and clk core
 *
 * Author: Linus Walleij <linus.walleij@linaro.org>
 *
10 11
 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
 *
12 13 14 15 16
 * License terms: GNU General Public License (GPL) version 2
 */
#define pr_fmt(fmt) "pinctrl core: " fmt

#include <linux/kernel.h>
17
#include <linux/kref.h>
18
#include <linux/export.h>
19 20 21 22 23 24 25 26
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
27
#include <linux/pinctrl/consumer.h>
28 29
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/machine.h>
30 31

#ifdef CONFIG_GPIOLIB
32
#include <asm-generic/gpio.h>
33 34
#endif

35
#include "core.h"
36
#include "devicetree.h"
37
#include "pinmux.h"
38
#include "pinconf.h"
39

40

41 42
static bool pinctrl_dummy_state;

43
/* Mutex taken to protect pinctrl_list */
44
static DEFINE_MUTEX(pinctrl_list_mutex);
45 46 47 48 49

/* Mutex taken to protect pinctrl_maps */
DEFINE_MUTEX(pinctrl_maps_mutex);

/* Mutex taken to protect pinctrldev_list */
50
static DEFINE_MUTEX(pinctrldev_list_mutex);
51 52

/* Global list of pin control devices (struct pinctrl_dev) */
53
static LIST_HEAD(pinctrldev_list);
54

55
/* List of pin controller handles (struct pinctrl) */
56 57
static LIST_HEAD(pinctrl_list);

58
/* List of pinctrl maps (struct pinctrl_maps) */
59
LIST_HEAD(pinctrl_maps);
60

61

62 63 64 65 66 67 68 69 70 71 72 73 74
/**
 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
 *
 * Usually this function is called by platforms without pinctrl driver support
 * but run with some shared drivers using pinctrl APIs.
 * After calling this function, the pinctrl core will return successfully
 * with creating a dummy state for the driver to keep going smoothly.
 */
void pinctrl_provide_dummies(void)
{
	pinctrl_dummy_state = true;
}

75 76 77 78 79 80 81
const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
{
	/* We're not allowed to register devices without name */
	return pctldev->desc->name;
}
EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);

82 83 84 85 86 87
const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
{
	return dev_name(pctldev->dev);
}
EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);

88 89 90 91 92 93 94
void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
{
	return pctldev->driver_data;
}
EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);

/**
95 96
 * get_pinctrl_dev_from_devname() - look up pin controller device
 * @devname: the name of a device instance, as returned by dev_name()
97 98 99 100
 *
 * Looks up a pin control device matching a certain device name or pure device
 * pointer, the pure device pointer will take precedence.
 */
101
struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
102 103 104
{
	struct pinctrl_dev *pctldev = NULL;

105 106 107
	if (!devname)
		return NULL;

108 109
	mutex_lock(&pinctrldev_list_mutex);

110
	list_for_each_entry(pctldev, &pinctrldev_list, node) {
111
		if (!strcmp(dev_name(pctldev->dev), devname)) {
112
			/* Matched on device name */
113 114
			mutex_unlock(&pinctrldev_list_mutex);
			return pctldev;
115 116 117
		}
	}

118 119 120
	mutex_unlock(&pinctrldev_list_mutex);

	return NULL;
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134
struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
{
	struct pinctrl_dev *pctldev;

	mutex_lock(&pinctrldev_list_mutex);

	list_for_each_entry(pctldev, &pinctrldev_list, node)
		if (pctldev->dev->of_node == np) {
			mutex_unlock(&pinctrldev_list_mutex);
			return pctldev;
		}

135
	mutex_unlock(&pinctrldev_list_mutex);
136 137 138 139

	return NULL;
}

140 141 142 143 144 145 146
/**
 * pin_get_from_name() - look up a pin number from a name
 * @pctldev: the pin control device to lookup the pin on
 * @name: the name of the pin to look up
 */
int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
{
147
	unsigned i, pin;
148

149 150
	/* The pin number can be retrived from the pin controller descriptor */
	for (i = 0; i < pctldev->desc->npins; i++) {
151 152
		struct pin_desc *desc;

153
		pin = pctldev->desc->pins[i].number;
154 155
		desc = pin_desc_get(pctldev, pin);
		/* Pin space may be sparse */
156
		if (desc && !strcmp(name, desc->name))
157 158 159 160 161 162
			return pin;
	}

	return -EINVAL;
}

163 164 165 166 167 168 169 170 171 172
/**
 * pin_get_name_from_id() - look up a pin name from a pin id
 * @pctldev: the pin control device to lookup the pin on
 * @name: the name of the pin to look up
 */
const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
{
	const struct pin_desc *desc;

	desc = pin_desc_get(pctldev, pin);
173
	if (!desc) {
174 175 176 177 178 179 180 181
		dev_err(pctldev->dev, "failed to get pin(%d) name\n",
			pin);
		return NULL;
	}

	return desc->name;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/**
 * pin_is_valid() - check if pin exists on controller
 * @pctldev: the pin control device to check the pin on
 * @pin: pin to check, use the local pin controller index number
 *
 * This tells us whether a certain pin exist on a certain pin controller or
 * not. Pin lists may be sparse, so some pins may not exist.
 */
bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
{
	struct pin_desc *pindesc;

	if (pin < 0)
		return false;

197
	mutex_lock(&pctldev->mutex);
198
	pindesc = pin_desc_get(pctldev, pin);
199
	mutex_unlock(&pctldev->mutex);
200

201
	return pindesc != NULL;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}
EXPORT_SYMBOL_GPL(pin_is_valid);

/* Deletes a range of pin descriptors */
static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
				  const struct pinctrl_pin_desc *pins,
				  unsigned num_pins)
{
	int i;

	for (i = 0; i < num_pins; i++) {
		struct pin_desc *pindesc;

		pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
					    pins[i].number);
217
		if (pindesc) {
218 219
			radix_tree_delete(&pctldev->pin_desc_tree,
					  pins[i].number);
220 221
			if (pindesc->dynamic_name)
				kfree(pindesc->name);
222 223 224 225 226 227
		}
		kfree(pindesc);
	}
}

static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228
				    const struct pinctrl_pin_desc *pin)
229 230 231
{
	struct pin_desc *pindesc;

232
	pindesc = pin_desc_get(pctldev, pin->number);
233
	if (pindesc) {
234 235
		dev_err(pctldev->dev, "pin %d already registered\n",
			pin->number);
236 237 238 239
		return -EINVAL;
	}

	pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
240
	if (!pindesc)
241
		return -ENOMEM;
242

243 244 245
	/* Set owner */
	pindesc->pctldev = pctldev;

246
	/* Copy basic pin info */
247 248
	if (pin->name) {
		pindesc->name = pin->name;
249
	} else {
250
		pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
251
		if (!pindesc->name) {
252
			kfree(pindesc);
253
			return -ENOMEM;
254
		}
255 256
		pindesc->dynamic_name = true;
	}
257

258 259 260
	pindesc->drv_data = pin->drv_data;

	radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
261
	pr_debug("registered pin %d (%s) on %s\n",
262
		 pin->number, pindesc->name, pctldev->desc->name);
263 264 265 266
	return 0;
}

static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
267
				 const struct pinctrl_pin_desc *pins,
268 269 270 271 272 273
				 unsigned num_descs)
{
	unsigned i;
	int ret = 0;

	for (i = 0; i < num_descs; i++) {
274
		ret = pinctrl_register_one_pin(pctldev, &pins[i]);
275 276 277 278 279 280 281
		if (ret)
			return ret;
	}

	return 0;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/**
 * gpio_to_pin() - GPIO range GPIO number to pin number translation
 * @range: GPIO range used for the translation
 * @gpio: gpio pin to translate to a pin number
 *
 * Finds the pin number for a given GPIO using the specified GPIO range
 * as a base for translation. The distinction between linear GPIO ranges
 * and pin list based GPIO ranges is managed correctly by this function.
 *
 * This function assumes the gpio is part of the specified GPIO range, use
 * only after making sure this is the case (e.g. by calling it on the
 * result of successful pinctrl_get_device_gpio_range calls)!
 */
static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
				unsigned int gpio)
{
	unsigned int offset = gpio - range->base;
	if (range->pins)
		return range->pins[offset];
	else
		return range->pin_base + offset;
}

305 306 307 308 309 310 311 312 313 314 315 316 317
/**
 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
 * @pctldev: pin controller device to check
 * @gpio: gpio pin to check taken from the global GPIO pin space
 *
 * Tries to match a GPIO pin number to the ranges handled by a certain pin
 * controller, return the range or NULL
 */
static struct pinctrl_gpio_range *
pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
{
	struct pinctrl_gpio_range *range = NULL;

318
	mutex_lock(&pctldev->mutex);
319 320 321 322 323
	/* Loop over the ranges */
	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
		/* Check if we're in the valid range */
		if (gpio >= range->base &&
		    gpio < range->base + range->npins) {
324
			mutex_unlock(&pctldev->mutex);
325 326 327
			return range;
		}
	}
328
	mutex_unlock(&pctldev->mutex);
329 330 331
	return NULL;
}

332 333 334 335 336 337 338 339 340 341 342 343 344
/**
 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
 * the same GPIO chip are in range
 * @gpio: gpio pin to check taken from the global GPIO pin space
 *
 * This function is complement of pinctrl_match_gpio_range(). If the return
 * value of pinctrl_match_gpio_range() is NULL, this function could be used
 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
 * of the same GPIO chip don't have back-end pinctrl interface.
 * If the return value is true, it means that pinctrl device is ready & the
 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
 * is false, it means that pinctrl device may not be ready.
 */
345
#ifdef CONFIG_GPIOLIB
346 347 348 349 350 351
static bool pinctrl_ready_for_gpio_range(unsigned gpio)
{
	struct pinctrl_dev *pctldev;
	struct pinctrl_gpio_range *range = NULL;
	struct gpio_chip *chip = gpio_to_chip(gpio);

352 353 354
	if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
		return false;

355 356
	mutex_lock(&pinctrldev_list_mutex);

357 358 359
	/* Loop over the pin controllers */
	list_for_each_entry(pctldev, &pinctrldev_list, node) {
		/* Loop over the ranges */
360
		mutex_lock(&pctldev->mutex);
361 362 363 364 365
		list_for_each_entry(range, &pctldev->gpio_ranges, node) {
			/* Check if any gpio range overlapped with gpio chip */
			if (range->base + range->npins - 1 < chip->base ||
			    range->base > chip->base + chip->ngpio - 1)
				continue;
366
			mutex_unlock(&pctldev->mutex);
367
			mutex_unlock(&pinctrldev_list_mutex);
368 369
			return true;
		}
370
		mutex_unlock(&pctldev->mutex);
371
	}
372 373 374

	mutex_unlock(&pinctrldev_list_mutex);

375 376
	return false;
}
377 378 379
#else
static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
#endif
380

381 382 383 384 385 386 387 388
/**
 * pinctrl_get_device_gpio_range() - find device for GPIO range
 * @gpio: the pin to locate the pin controller for
 * @outdev: the pin control device if found
 * @outrange: the GPIO range if found
 *
 * Find the pin controller handling a certain GPIO pin from the pinspace of
 * the GPIO subsystem, return the device and the matching GPIO range. Returns
389 390
 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
 * may still have not been registered.
391
 */
Stephen Warren's avatar
Stephen Warren committed
392 393 394
static int pinctrl_get_device_gpio_range(unsigned gpio,
					 struct pinctrl_dev **outdev,
					 struct pinctrl_gpio_range **outrange)
395 396 397
{
	struct pinctrl_dev *pctldev = NULL;

398 399
	mutex_lock(&pinctrldev_list_mutex);

400 401 402 403 404
	/* Loop over the pin controllers */
	list_for_each_entry(pctldev, &pinctrldev_list, node) {
		struct pinctrl_gpio_range *range;

		range = pinctrl_match_gpio_range(pctldev, gpio);
405
		if (range) {
406 407
			*outdev = pctldev;
			*outrange = range;
408
			mutex_unlock(&pinctrldev_list_mutex);
409 410 411 412
			return 0;
		}
	}

413 414
	mutex_unlock(&pinctrldev_list_mutex);

415
	return -EPROBE_DEFER;
416 417 418 419 420 421 422 423 424 425 426 427 428
}

/**
 * pinctrl_add_gpio_range() - register a GPIO range for a controller
 * @pctldev: pin controller device to add the range to
 * @range: the GPIO range to add
 *
 * This adds a range of GPIOs to be handled by a certain pin controller. Call
 * this to register handled ranges after registering your pin controller.
 */
void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
			    struct pinctrl_gpio_range *range)
{
429
	mutex_lock(&pctldev->mutex);
430
	list_add_tail(&range->node, &pctldev->gpio_ranges);
431
	mutex_unlock(&pctldev->mutex);
432
}
Stephen Warren's avatar
Stephen Warren committed
433
EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
434

435 436 437 438 439 440 441 442 443 444 445
void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
			     struct pinctrl_gpio_range *ranges,
			     unsigned nranges)
{
	int i;

	for (i = 0; i < nranges; i++)
		pinctrl_add_gpio_range(pctldev, &ranges[i]);
}
EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);

446
struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
447 448
		struct pinctrl_gpio_range *range)
{
449 450 451
	struct pinctrl_dev *pctldev;

	pctldev = get_pinctrl_dev_from_devname(devname);
452

453 454 455 456 457
	/*
	 * If we can't find this device, let's assume that is because
	 * it has not probed yet, so the driver trying to register this
	 * range need to defer probing.
	 */
458
	if (!pctldev) {
459
		return ERR_PTR(-EPROBE_DEFER);
460
	}
461
	pinctrl_add_gpio_range(pctldev, range);
462

463 464
	return pctldev;
}
465
EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
466

467 468 469 470 471 472
int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
				const unsigned **pins, unsigned *num_pins)
{
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	int gs;

473 474 475
	if (!pctlops->get_group_pins)
		return -EINVAL;

476 477 478 479 480 481 482 483
	gs = pinctrl_get_group_selector(pctldev, pin_group);
	if (gs < 0)
		return gs;

	return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
}
EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);

484
struct pinctrl_gpio_range *
485 486
pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
					unsigned int pin)
487
{
488
	struct pinctrl_gpio_range *range;
489 490 491 492

	/* Loop over the ranges */
	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
		/* Check if we're in the valid range */
493 494 495 496
		if (range->pins) {
			int a;
			for (a = 0; a < range->npins; a++) {
				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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

	return NULL;
}
EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);

/**
 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
 * @pctldev: the pin controller device to look in
 * @pin: a controller-local number to find the range for
 */
struct pinctrl_gpio_range *
pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
				 unsigned int pin)
{
	struct pinctrl_gpio_range *range;

	mutex_lock(&pctldev->mutex);
	range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
521
	mutex_unlock(&pctldev->mutex);
522

523
	return range;
524 525 526
}
EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);

527
/**
528
 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
529 530 531 532 533 534
 * @pctldev: pin controller device to remove the range from
 * @range: the GPIO range to remove
 */
void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
			       struct pinctrl_gpio_range *range)
{
535
	mutex_lock(&pctldev->mutex);
536
	list_del(&range->node);
537
	mutex_unlock(&pctldev->mutex);
538 539 540
}
EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);

541
#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

/**
 * pinctrl_generic_get_group_count() - returns the number of pin groups
 * @pctldev: pin controller device
 */
int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
{
	return pctldev->num_groups;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);

/**
 * pinctrl_generic_get_group_name() - returns the name of a pin group
 * @pctldev: pin controller device
 * @selector: group number
 */
const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
					   unsigned int selector)
{
	struct group_desc *group;

	group = radix_tree_lookup(&pctldev->pin_group_tree,
				  selector);
	if (!group)
		return NULL;

	return group->name;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);

/**
 * pinctrl_generic_get_group_pins() - gets the pin group pins
 * @pctldev: pin controller device
 * @selector: group number
 * @pins: pins in the group
 * @num_pins: number of pins in the group
 */
int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
				   unsigned int selector,
				   const unsigned int **pins,
				   unsigned int *num_pins)
{
	struct group_desc *group;

	group = radix_tree_lookup(&pctldev->pin_group_tree,
				  selector);
	if (!group) {
		dev_err(pctldev->dev, "%s could not find pingroup%i\n",
			__func__, selector);
		return -EINVAL;
	}

	*pins = group->pins;
	*num_pins = group->num_pins;

	return 0;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);

/**
 * pinctrl_generic_get_group() - returns a pin group based on the number
 * @pctldev: pin controller device
 * @gselector: group number
 */
struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
					     unsigned int selector)
{
	struct group_desc *group;

	group = radix_tree_lookup(&pctldev->pin_group_tree,
				  selector);
	if (!group)
		return NULL;

	return group;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);

/**
 * pinctrl_generic_add_group() - adds a new pin group
 * @pctldev: pin controller device
 * @name: name of the pin group
 * @pins: pins in the pin group
 * @num_pins: number of pins in the pin group
 * @data: pin controller driver specific data
 *
 * Note that the caller must take care of locking.
 */
int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
			      int *pins, int num_pins, void *data)
{
	struct group_desc *group;

	group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
	if (!group)
		return -ENOMEM;

	group->name = name;
	group->pins = pins;
	group->num_pins = num_pins;
	group->data = data;

	radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
			  group);

	pctldev->num_groups++;

	return 0;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);

/**
 * pinctrl_generic_remove_group() - removes a numbered pin group
 * @pctldev: pin controller device
 * @selector: group number
 *
 * Note that the caller must take care of locking.
 */
int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
				 unsigned int selector)
{
	struct group_desc *group;

	group = radix_tree_lookup(&pctldev->pin_group_tree,
				  selector);
	if (!group)
		return -ENOENT;

	radix_tree_delete(&pctldev->pin_group_tree, selector);
	devm_kfree(pctldev->dev, group);

	pctldev->num_groups--;

	return 0;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);

/**
 * pinctrl_generic_free_groups() - removes all pin groups
 * @pctldev: pin controller device
 *
683 684
 * Note that the caller must take care of locking. The pinctrl groups
 * are allocated with devm_kzalloc() so no need to free them here.
685 686 687 688
 */
static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
{
	struct radix_tree_iter iter;
689
	void __rcu **slot;
690 691

	radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
692
		radix_tree_delete(&pctldev->pin_group_tree, iter.index);
693 694 695 696 697 698 699 700

	pctldev->num_groups = 0;
}

#else
static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
{
}
701
#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
702

703 704 705 706 707 708 709 710 711
/**
 * pinctrl_get_group_selector() - returns the group selector for a group
 * @pctldev: the pin controller handling the group
 * @pin_group: the pin group to look up
 */
int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
			       const char *pin_group)
{
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
712
	unsigned ngroups = pctlops->get_groups_count(pctldev);
713 714
	unsigned group_selector = 0;

715
	while (group_selector < ngroups) {
716 717 718
		const char *gname = pctlops->get_group_name(pctldev,
							    group_selector);
		if (!strcmp(gname, pin_group)) {
719
			dev_dbg(pctldev->dev,
720 721 722 723 724 725 726 727 728
				"found group selector %u for %s\n",
				group_selector,
				pin_group);
			return group_selector;
		}

		group_selector++;
	}

729
	dev_err(pctldev->dev, "does not have pin group %s\n",
730 731 732 733 734
		pin_group);

	return -EINVAL;
}

735
/**
736
 * pinctrl_gpio_request() - request a single pin to be used as GPIO
737 738 739 740 741 742
 * @gpio: the GPIO pin number from the GPIO subsystem number space
 *
 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
 * as part of their gpio_request() semantics, platforms and individual drivers
 * shall *NOT* request GPIO pins to be muxed in.
 */
743
int pinctrl_gpio_request(unsigned gpio)
744 745 746 747 748 749 750
{
	struct pinctrl_dev *pctldev;
	struct pinctrl_gpio_range *range;
	int ret;
	int pin;

	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
751
	if (ret) {
752 753
		if (pinctrl_ready_for_gpio_range(gpio))
			ret = 0;
754
		return ret;
755
	}
756

757 758
	mutex_lock(&pctldev->mutex);

759
	/* Convert to the pin controllers number space */
760
	pin = gpio_to_pin(range, gpio);
761

762 763
	ret = pinmux_request_gpio(pctldev, range, pin, gpio);

764 765
	mutex_unlock(&pctldev->mutex);

766
	return ret;
767
}
768
EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
769 770

/**
771
 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
772 773 774 775 776 777
 * @gpio: the GPIO pin number from the GPIO subsystem number space
 *
 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
 * as part of their gpio_free() semantics, platforms and individual drivers
 * shall *NOT* request GPIO pins to be muxed out.
 */
778
void pinctrl_gpio_free(unsigned gpio)
779 780 781 782 783 784 785
{
	struct pinctrl_dev *pctldev;
	struct pinctrl_gpio_range *range;
	int ret;
	int pin;

	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
786
	if (ret) {
787
		return;
788
	}
789
	mutex_lock(&pctldev->mutex);
790 791

	/* Convert to the pin controllers number space */
792
	pin = gpio_to_pin(range, gpio);
793

794 795
	pinmux_free_gpio(pctldev, pin, range);

796
	mutex_unlock(&pctldev->mutex);
797
}
798
EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
799 800 801 802 803 804 805 806 807

static int pinctrl_gpio_direction(unsigned gpio, bool input)
{
	struct pinctrl_dev *pctldev;
	struct pinctrl_gpio_range *range;
	int ret;
	int pin;

	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
808
	if (ret) {
809
		return ret;
810 811 812
	}

	mutex_lock(&pctldev->mutex);
813 814

	/* Convert to the pin controllers number space */
815
	pin = gpio_to_pin(range, gpio);
816 817 818
	ret = pinmux_gpio_direction(pctldev, range, pin, input);

	mutex_unlock(&pctldev->mutex);
819

820
	return ret;
821 822 823 824 825 826 827 828 829 830 831 832
}

/**
 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
 * @gpio: the GPIO pin number from the GPIO subsystem number space
 *
 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
 * as part of their gpio_direction_input() semantics, platforms and individual
 * drivers shall *NOT* touch pin control GPIO calls.
 */
int pinctrl_gpio_direction_input(unsigned gpio)
{
833
	return pinctrl_gpio_direction(gpio, true);
834 835 836 837 838 839 840 841 842 843 844 845 846
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);

/**
 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
 * @gpio: the GPIO pin number from the GPIO subsystem number space
 *
 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
 * as part of their gpio_direction_output() semantics, platforms and individual
 * drivers shall *NOT* touch pin control GPIO calls.
 */
int pinctrl_gpio_direction_output(unsigned gpio)
{
847
	return pinctrl_gpio_direction(gpio, false);
848 849 850
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
/**
 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
 * @gpio: the GPIO pin number from the GPIO subsystem number space
 * @config: the configuration to apply to the GPIO
 *
 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
 * they need to call the underlying pin controller to change GPIO config
 * (for example set debounce time).
 */
int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
{
	unsigned long configs[] = { config };
	struct pinctrl_gpio_range *range;
	struct pinctrl_dev *pctldev;
	int ret, pin;

	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
	if (ret)
		return ret;

	mutex_lock(&pctldev->mutex);
	pin = gpio_to_pin(range, gpio);
	ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
	mutex_unlock(&pctldev->mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);

880 881
static struct pinctrl_state *find_state(struct pinctrl *p,
					const char *name)
882
{
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	struct pinctrl_state *state;

	list_for_each_entry(state, &p->states, node)
		if (!strcmp(state->name, name))
			return state;

	return NULL;
}

static struct pinctrl_state *create_state(struct pinctrl *p,
					  const char *name)
{
	struct pinctrl_state *state;

	state = kzalloc(sizeof(*state), GFP_KERNEL);
898
	if (!state)
899 900 901 902 903 904 905 906 907 908
		return ERR_PTR(-ENOMEM);

	state->name = name;
	INIT_LIST_HEAD(&state->settings);

	list_add_tail(&state->node, &p->states);

	return state;
}

909
static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
910
		       const struct pinctrl_map *map)
911 912
{
	struct pinctrl_state *state;
913
	struct pinctrl_setting *setting;
914
	int ret;
915

916 917 918 919 920
	state = find_state(p, map->name);
	if (!state)
		state = create_state(p, map->name);
	if (IS_ERR(state))
		return PTR_ERR(state);
921

922 923 924
	if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
		return 0;

925
	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
926
	if (!setting)
927
		return -ENOMEM;
928

929 930
	setting->type = map->type;

931 932 933 934 935
	if (pctldev)
		setting->pctldev = pctldev;
	else
		setting->pctldev =
			get_pinctrl_dev_from_devname(map->ctrl_dev_name);
936
	if (!setting->pctldev) {
937
		kfree(setting);
938 939 940
		/* Do not defer probing of hogs (circular loop) */
		if (!strcmp(map->ctrl_dev_name, map->dev_name))
			return -ENODEV;
941 942 943 944
		/*
		 * OK let us guess that the driver is not there yet, and
		 * let's defer obtaining this pinctrl handle to later...
		 */
945 946
		dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
			map->ctrl_dev_name);
947
		return -EPROBE_DEFER;
948 949
	}

950 951
	setting->dev_name = map->dev_name;

952 953 954 955 956 957 958 959 960 961 962 963
	switch (map->type) {
	case PIN_MAP_TYPE_MUX_GROUP:
		ret = pinmux_map_to_setting(map, setting);
		break;
	case PIN_MAP_TYPE_CONFIGS_PIN:
	case PIN_MAP_TYPE_CONFIGS_GROUP:
		ret = pinconf_map_to_setting(map, setting);
		break;
	default:
		ret = -EINVAL;
		break;
	}
964 965 966 967 968 969 970 971 972 973 974 975 976 977
	if (ret < 0) {
		kfree(setting);
		return ret;
	}

	list_add_tail(&setting->node, &state->settings);

	return 0;
}

static struct pinctrl *find_pinctrl(struct device *dev)
{
	struct pinctrl *p;

978
	mutex_lock(&pinctrl_list_mutex);
979
	list_for_each_entry(p, &pinctrl_list, node)
980 981
		if (p->dev == dev) {
			mutex_unlock(&pinctrl_list_mutex);
982
			return p;
983
		}
984

985
	mutex_unlock(&pinctrl_list_mutex);
986 987 988
	return NULL;
}

989
static void pinctrl_free(struct pinctrl *p, bool inlist);
990

991 992
static struct pinctrl *create_pinctrl(struct device *dev,
				      struct pinctrl_dev *pctldev)
993 994 995 996 997
{
	struct pinctrl *p;
	const char *devname;
	struct pinctrl_maps *maps_node;
	int i;
998
	const struct pinctrl_map *map;
999
	int ret;
1000 1001 1002 1003 1004 1005

	/*
	 * create the state cookie holder struct pinctrl for each
	 * mapping, this is what consumers will get when requesting
	 * a pin control handle with pinctrl_get()
	 */
1006
	p = kzalloc(sizeof(*p), GFP_KERNEL);
1007
	if (!p)
1008
		return ERR_PTR(-ENOMEM);
1009
	p->dev = dev;
1010
	INIT_LIST_HEAD(&p->states);
1011 1012
	INIT_LIST_HEAD(&p->dt_maps);

1013
	ret = pinctrl_dt_to_map(p, pctldev);
1014 1015 1016 1017
	if (ret < 0) {
		kfree(p);
		return ERR_PTR(ret);
	}
1018 1019

	devname = dev_name(dev);
1020

1021
	mutex_lock(&pinctrl_maps_mutex);
1022
	/* Iterate over the pin control maps to locate the right ones */
1023
	for_each_maps(maps_node, i, map) {
1024 1025 1026
		/* Map must be for this device */
		if (strcmp(map->dev_name, devname))
			continue;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		/*
		 * If pctldev is not null, we are claiming hog for it,
		 * that means, setting that is served by pctldev by itself.
		 *
		 * Thus we must skip map that is for this device but is served
		 * by other device.
		 */
		if (pctldev &&
		    strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
			continue;
1037

1038
		ret = add_setting(p, pctldev, map);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		/*
		 * At this point the adding of a setting may:
		 *
		 * - Defer, if the pinctrl device is not yet available
		 * - Fail, if the pinctrl device is not yet available,
		 *   AND the setting is a hog. We cannot defer that, since
		 *   the hog will kick in immediately after the device
		 *   is registered.
		 *
		 * If the error returned was not -EPROBE_DEFER then we
		 * accumulate the errors to see if we end up with
		 * an -EPROBE_DEFER later, as that is the worst case.
		 */
		if (ret == -EPROBE_DEFER) {
1053 1054
			pinctrl_free(p, false);
			mutex_unlock(&pinctrl_maps_mutex);
1055
			return ERR_PTR(ret);
1056
		}
1057
	}
1058 1059
	mutex_unlock(&pinctrl_maps_mutex);

1060
	if (ret < 0) {
1061
		/* If some other error than deferral occurred, return here */
1062
		pinctrl_free(p, false);
1063 1064
		return ERR_PTR(ret);
	}
1065

1066 1067
	kref_init(&p->users);

Linus Walleij's avatar
Linus Walleij committed
1068
	/* Add the pinctrl handle to the global list */
1069
	mutex_lock(&pinctrl_list_mutex);
1070
	list_add_tail(&p->node, &pinctrl_list);
1071
	mutex_unlock(&pinctrl_list_mutex);
1072 1073

	return p;
1074
}
1075

1076 1077 1078 1079 1080
/**
 * pinctrl_get() - retrieves the pinctrl handle for a device
 * @dev: the device to obtain the handle for
 */
struct pinctrl *pinctrl_get(struct device *dev)
1081 1082
{
	struct pinctrl *p;
1083

1084 1085 1086
	if (WARN_ON(!dev))
		return ERR_PTR(-EINVAL);

1087 1088 1089 1090 1091
	/*
	 * See if somebody else (such as the device core) has already
	 * obtained a handle to the pinctrl for this device. In that case,
	 * return another pointer to it.
	 */
1092
	p = find_pinctrl(dev);
1093
	if (p) {
1094 1095 1096 1097
		dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
		kref_get(&p->users);
		return p;
	}
1098

1099
	return create_pinctrl(dev, NULL);
1100 1101 1102
}
EXPORT_SYMBOL_GPL(pinctrl_get);

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
static void pinctrl_free_setting(bool disable_setting,
				 struct pinctrl_setting *setting)
{
	switch (setting->type) {
	case PIN_MAP_TYPE_MUX_GROUP:
		if (disable_setting)
			pinmux_disable_setting(setting);
		pinmux_free_setting(setting);
		break;
	case PIN_MAP_TYPE_CONFIGS_PIN:
	case PIN_MAP_TYPE_CONFIGS_GROUP:
		pinconf_free_setting(setting);
		break;
	default:
		break;
	}
}

1121
static void pinctrl_free(struct pinctrl *p, bool inlist)
1122
{
1123 1124 1125
	struct pinctrl_state *state, *n1;
	struct pinctrl_setting *setting, *n2;

1126
	mutex_lock(&pinctrl_list_mutex);
1127 1128
	list_for_each_entry_safe(state, n1, &p->states, node) {
		list_for_each_entry_safe(setting, n2, &state->settings, node) {
1129
			pinctrl_free_setting(state == p->state, setting);
1130 1131 1132 1133 1134
			list_del(&setting->node);
			kfree(setting);
		}
		list_del(&state->node);
		kfree(state);
1135
	}
1136

1137 1138
	pinctrl_dt_free_maps(p);

1139 1140
	if (inlist)
		list_del(&p->node);
1141
	kfree(p);
1142
	mutex_unlock(&pinctrl_list_mutex);
1143 1144 1145
}

/**
1146 1147 1148
 * pinctrl_release() - release the pinctrl handle
 * @kref: the kref in the pinctrl being released
 */
1149
static void pinctrl_release(struct kref *kref)
1150 1151 1152
{
	struct pinctrl *p = container_of(kref, struct pinctrl, users);

1153
	pinctrl_free(p, true);
1154 1155 1156 1157
}

/**
 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1158
 * @p: the pinctrl handle to release
1159
 */
1160 1161
void pinctrl_put(struct pinctrl *p)
{
1162
	kref_put(&p->users, pinctrl_release);
1163 1164 1165
}
EXPORT_SYMBOL_GPL(pinctrl_put);

1166 1167 1168 1169 1170 1171 1172
/**
 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
 * @p: the pinctrl handle to retrieve the state from
 * @name: the state name to retrieve
 */
struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
						 const char *name)
1173
{
1174
	struct pinctrl_state *state;
1175

1176
	state = find_state(p, name);
1177 1178 1179 1180 1181 1182
	if (!state) {
		if (pinctrl_dummy_state) {
			/* create dummy state */
			dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
				name);
			state = create_state(p, name);
1183 1184
		} else
			state = ERR_PTR(-ENODEV);
1185
	}
1186

1187
	return state;
1188
}
1189
EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1190 1191

/**
1192
 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1193 1194
 * @p: the pinctrl handle for the device that requests configuration
 * @state: the state handle to select/activate/program
1195
 */
1196
static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
1197
{
1198
	struct pinctrl_setting *setting, *setting2;
1199
	struct pinctrl_state *old_state = p->state;
1200
	int ret;
1201

1202 1203
	if (p->state) {
		/*
1204 1205 1206 1207
		 * For each pinmux setting in the old state, forget SW's record
		 * of mux owner for that pingroup. Any pingroups which are
		 * still owned by the new state will be re-acquired by the call
		 * to pinmux_enable_setting() in the loop below.
1208 1209
		 */
		list_for_each_entry(setting, &p->state->settings, node) {
1210 1211
			if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
				continue;
1212
			pinmux_disable_setting(setting);
1213 1214 1215
		}
	}

1216
	p->state = NULL;
1217 1218 1219

	/* Apply all the settings for the new state */
	list_for_each_entry(setting, &state->settings, node) {
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		switch (setting->type) {
		case PIN_MAP_TYPE_MUX_GROUP:
			ret = pinmux_enable_setting(setting);
			break;
		case PIN_MAP_TYPE_CONFIGS_PIN:
		case PIN_MAP_TYPE_CONFIGS_GROUP:
			ret = pinconf_apply_setting(setting);
			break;
		default:
			ret = -EINVAL;
			break;
		}
1232

1233
		if (ret < 0) {
1234
			goto unapply_new_state;
1235
		}
1236
	}
1237

1238 1239
	p->state = state;

1240
	return 0;
1241 1242

unapply_new_state:
1243
	dev_err(p->dev, "Error applying setting, reverse things back\n");
1244 1245 1246 1247

	list_for_each_entry(setting2, &state->settings, node) {
		if (&setting2->node == &setting->node)
			break;
1248 1249 1250 1251 1252 1253 1254 1255 1256
		/*
		 * All we can do here is pinmux_disable_setting.
		 * That means that some pins are muxed differently now
		 * than they were before applying the setting (We can't
		 * "unmux a pin"!), but it's not a big deal since the pins
		 * are free to be muxed by another apply_setting.
		 */
		if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
			pinmux_disable_setting(setting2);
1257
	}
1258

1259 1260
	/* There's no infinite recursive loop here because p->state is NULL */
	if (old_state)
1261
		pinctrl_select_state(p, old_state);
1262 1263

	return ret;
1264
}