pinmux.c revision d4e3198736d9d64e4ba4d2b46ab75cbcf5d0a4e0
1/*
2 * Core driver for the pin muxing portions of the pin control subsystem
3 *
4 * Copyright (C) 2011-2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinmux core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
18#include <linux/slab.h>
19#include <linux/radix-tree.h>
20#include <linux/err.h>
21#include <linux/list.h>
22#include <linux/mutex.h>
23#include <linux/spinlock.h>
24#include <linux/string.h>
25#include <linux/sysfs.h>
26#include <linux/debugfs.h>
27#include <linux/seq_file.h>
28#include <linux/pinctrl/machine.h>
29#include <linux/pinctrl/pinmux.h>
30#include "core.h"
31#include "pinmux.h"
32
33/**
34 * struct pinmux_group - group list item for pinmux groups
35 * @node: pinmux group list node
36 * @func_selector: the function selector for the pinmux device handling
37 *	this pinmux
38 * @group_selector: the group selector for this group
39 */
40struct pinmux_group {
41	struct list_head node;
42	unsigned func_selector;
43	unsigned group_selector;
44};
45
46int pinmux_check_ops(struct pinctrl_dev *pctldev)
47{
48	const struct pinmux_ops *ops = pctldev->desc->pmxops;
49	unsigned selector = 0;
50
51	/* Check that we implement required operations */
52	if (!ops->list_functions ||
53	    !ops->get_function_name ||
54	    !ops->get_function_groups ||
55	    !ops->enable ||
56	    !ops->disable)
57		return -EINVAL;
58
59	/* Check that all functions registered have names */
60	while (ops->list_functions(pctldev, selector) >= 0) {
61		const char *fname = ops->get_function_name(pctldev,
62							   selector);
63		if (!fname) {
64			pr_err("pinmux ops has no name for function%u\n",
65				selector);
66			return -EINVAL;
67		}
68		selector++;
69	}
70
71	return 0;
72}
73
74/**
75 * pin_request() - request a single pin to be muxed in, typically for GPIO
76 * @pin: the pin number in the global pin space
77 * @owner: a representation of the owner of this pin; typically the device
78 *	name that controls its mux function, or the requested GPIO name
79 * @gpio_range: the range matching the GPIO pin if this is a request for a
80 *	single GPIO pin
81 */
82static int pin_request(struct pinctrl_dev *pctldev,
83		       int pin, const char *owner,
84		       struct pinctrl_gpio_range *gpio_range)
85{
86	struct pin_desc *desc;
87	const struct pinmux_ops *ops = pctldev->desc->pmxops;
88	int status = -EINVAL;
89
90	dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
91
92	desc = pin_desc_get(pctldev, pin);
93	if (desc == NULL) {
94		dev_err(pctldev->dev,
95			"pin is not registered so it cannot be requested\n");
96		goto out;
97	}
98
99	spin_lock(&desc->lock);
100	if (desc->owner && strcmp(desc->owner, owner)) {
101		spin_unlock(&desc->lock);
102		dev_err(pctldev->dev,
103			"pin already requested\n");
104		goto out;
105	}
106	desc->owner = owner;
107	spin_unlock(&desc->lock);
108
109	/* Let each pin increase references to this module */
110	if (!try_module_get(pctldev->owner)) {
111		dev_err(pctldev->dev,
112			"could not increase module refcount for pin %d\n",
113			pin);
114		status = -EINVAL;
115		goto out_free_pin;
116	}
117
118	/*
119	 * If there is no kind of request function for the pin we just assume
120	 * we got it by default and proceed.
121	 */
122	if (gpio_range && ops->gpio_request_enable)
123		/* This requests and enables a single GPIO pin */
124		status = ops->gpio_request_enable(pctldev, gpio_range, pin);
125	else if (ops->request)
126		status = ops->request(pctldev, pin);
127	else
128		status = 0;
129
130	if (status)
131		dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
132		       pctldev->desc->name, pin);
133out_free_pin:
134	if (status) {
135		spin_lock(&desc->lock);
136		desc->owner = NULL;
137		spin_unlock(&desc->lock);
138	}
139out:
140	if (status)
141		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
142		       pin, owner, status);
143
144	return status;
145}
146
147/**
148 * pin_free() - release a single muxed in pin so something else can be muxed
149 * @pctldev: pin controller device handling this pin
150 * @pin: the pin to free
151 * @gpio_range: the range matching the GPIO pin if this is a request for a
152 *	single GPIO pin
153 *
154 * This function returns a pointer to the previous owner. This is used
155 * for callers that dynamically allocate an owner name so it can be freed
156 * once the pin is free. This is done for GPIO request functions.
157 */
158static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
159			    struct pinctrl_gpio_range *gpio_range)
160{
161	const struct pinmux_ops *ops = pctldev->desc->pmxops;
162	struct pin_desc *desc;
163	const char *owner;
164
165	desc = pin_desc_get(pctldev, pin);
166	if (desc == NULL) {
167		dev_err(pctldev->dev,
168			"pin is not registered so it cannot be freed\n");
169		return NULL;
170	}
171
172	/*
173	 * If there is no kind of request function for the pin we just assume
174	 * we got it by default and proceed.
175	 */
176	if (gpio_range && ops->gpio_disable_free)
177		ops->gpio_disable_free(pctldev, gpio_range, pin);
178	else if (ops->free)
179		ops->free(pctldev, pin);
180
181	spin_lock(&desc->lock);
182	owner = desc->owner;
183	desc->owner = NULL;
184	spin_unlock(&desc->lock);
185	module_put(pctldev->owner);
186
187	return owner;
188}
189
190/**
191 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
192 * @pctldev: pin controller device affected
193 * @pin: the pin to mux in for GPIO
194 * @range: the applicable GPIO range
195 */
196int pinmux_request_gpio(struct pinctrl_dev *pctldev,
197			struct pinctrl_gpio_range *range,
198			unsigned pin, unsigned gpio)
199{
200	char gpiostr[16];
201	const char *owner;
202	int ret;
203
204	/* Conjure some name stating what chip and pin this is taken by */
205	snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
206
207	owner = kstrdup(gpiostr, GFP_KERNEL);
208	if (!owner)
209		return -EINVAL;
210
211	ret = pin_request(pctldev, pin, owner, range);
212	if (ret < 0)
213		kfree(owner);
214
215	return ret;
216}
217
218/**
219 * pinmux_free_gpio() - release a pin from GPIO muxing
220 * @pctldev: the pin controller device for the pin
221 * @pin: the affected currently GPIO-muxed in pin
222 * @range: applicable GPIO range
223 */
224void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
225		      struct pinctrl_gpio_range *range)
226{
227	const char *owner;
228
229	owner = pin_free(pctldev, pin, range);
230	kfree(owner);
231}
232
233/**
234 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
235 * @pctldev: the pin controller handling this pin
236 * @range: applicable GPIO range
237 * @pin: the affected GPIO pin in this controller
238 * @input: true if we set the pin as input, false for output
239 */
240int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
241			  struct pinctrl_gpio_range *range,
242			  unsigned pin, bool input)
243{
244	const struct pinmux_ops *ops;
245	int ret;
246
247	ops = pctldev->desc->pmxops;
248
249	if (ops->gpio_set_direction)
250		ret = ops->gpio_set_direction(pctldev, range, pin, input);
251	else
252		ret = 0;
253
254	return ret;
255}
256
257/**
258 * acquire_pins() - acquire all the pins for a certain function on a pinmux
259 * @pctldev: the device to take the pins on
260 * @owner: a representation of the owner of this pin; typically the device
261 *	name that controls its mux function
262 * @group_selector: the group selector containing the pins to acquire
263 */
264static int acquire_pins(struct pinctrl_dev *pctldev,
265			const char *owner,
266			unsigned group_selector)
267{
268	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
269	const unsigned *pins;
270	unsigned num_pins;
271	int ret;
272	int i;
273
274	ret = pctlops->get_group_pins(pctldev, group_selector,
275				      &pins, &num_pins);
276	if (ret)
277		return ret;
278
279	dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
280		num_pins, group_selector);
281
282	/* Try to allocate all pins in this group, one by one */
283	for (i = 0; i < num_pins; i++) {
284		ret = pin_request(pctldev, pins[i], owner, NULL);
285		if (ret) {
286			dev_err(pctldev->dev,
287				"could not get request pin %d on device %s - conflicting mux mappings?\n",
288				pins[i],
289				pinctrl_dev_get_name(pctldev));
290			/* On error release all taken pins */
291			i--; /* this pin just failed */
292			for (; i >= 0; i--)
293				pin_free(pctldev, pins[i], NULL);
294			return -ENODEV;
295		}
296	}
297	return 0;
298}
299
300/**
301 * release_pins() - release pins taken by earlier acquirement
302 * @pctldev: the device to free the pins on
303 * @group_selector: the group selector containing the pins to free
304 */
305static void release_pins(struct pinctrl_dev *pctldev,
306			 unsigned group_selector)
307{
308	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
309	const unsigned *pins;
310	unsigned num_pins;
311	int ret;
312	int i;
313
314	ret = pctlops->get_group_pins(pctldev, group_selector,
315				      &pins, &num_pins);
316	if (ret) {
317		dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
318			group_selector);
319		return;
320	}
321	for (i = 0; i < num_pins; i++)
322		pin_free(pctldev, pins[i], NULL);
323}
324
325/**
326 * pinmux_check_pin_group() - check function and pin group combo
327 * @pctldev: device to check the pin group vs function for
328 * @func_selector: the function selector to check the pin group for, we have
329 *	already looked this up in the calling function
330 * @pin_group: the pin group to match to the function
331 *
332 * This function will check that the pinmux driver can supply the
333 * selected pin group for a certain function, returns the group selector if
334 * the group and function selector will work fine together, else returns
335 * negative
336 */
337static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
338				  unsigned func_selector,
339				  const char *pin_group)
340{
341	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
342	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
343	int ret;
344
345	/*
346	 * If the driver does not support different pin groups for the
347	 * functions, we only support group 0, and assume this exists.
348	 */
349	if (!pctlops || !pctlops->list_groups)
350		return 0;
351
352	/*
353	 * Passing NULL (no specific group) will select the first and
354	 * hopefully only group of pins available for this function.
355	 */
356	if (!pin_group) {
357		char const * const *groups;
358		unsigned num_groups;
359
360		ret = pmxops->get_function_groups(pctldev, func_selector,
361						  &groups, &num_groups);
362		if (ret)
363			return ret;
364		if (num_groups < 1)
365			return -EINVAL;
366		ret = pinctrl_get_group_selector(pctldev, groups[0]);
367		if (ret < 0) {
368			dev_err(pctldev->dev,
369				"function %s wants group %s but the pin controller does not seem to have that group\n",
370				pmxops->get_function_name(pctldev, func_selector),
371				groups[0]);
372			return ret;
373		}
374
375		if (num_groups > 1)
376			dev_dbg(pctldev->dev,
377				"function %s support more than one group, default-selecting first group %s (%d)\n",
378				pmxops->get_function_name(pctldev, func_selector),
379				groups[0],
380				ret);
381
382		return ret;
383	}
384
385	dev_dbg(pctldev->dev,
386		"check if we have pin group %s on controller %s\n",
387		pin_group, pinctrl_dev_get_name(pctldev));
388
389	ret = pinctrl_get_group_selector(pctldev, pin_group);
390	if (ret < 0) {
391		dev_dbg(pctldev->dev,
392			"%s does not support pin group %s with function %s\n",
393			pinctrl_dev_get_name(pctldev),
394			pin_group,
395			pmxops->get_function_name(pctldev, func_selector));
396	}
397	return ret;
398}
399
400/**
401 * pinmux_search_function() - check pin control driver for a certain function
402 * @pctldev: device to check for function and position
403 * @map: function map containing the function and position to look for
404 * @func_selector: returns the applicable function selector if found
405 * @group_selector: returns the applicable group selector if found
406 *
407 * This will search the pinmux driver for an applicable
408 * function with a specific pin group, returns 0 if these can be mapped
409 * negative otherwise
410 */
411static int pinmux_search_function(struct pinctrl_dev *pctldev,
412				  struct pinctrl_map const *map,
413				  unsigned *func_selector,
414				  unsigned *group_selector)
415{
416	const struct pinmux_ops *ops = pctldev->desc->pmxops;
417	unsigned selector = 0;
418
419	/* See if this pctldev has this function */
420	while (ops->list_functions(pctldev, selector) >= 0) {
421		const char *fname = ops->get_function_name(pctldev,
422							   selector);
423		int ret;
424
425		if (!strcmp(map->function, fname)) {
426			/* Found the function, check pin group */
427			ret = pinmux_check_pin_group(pctldev, selector,
428						     map->group);
429			if (ret < 0)
430				return ret;
431
432			/* This function and group selector can be used */
433			*func_selector = selector;
434			*group_selector = ret;
435			return 0;
436
437		}
438		selector++;
439	}
440
441	pr_err("%s does not support function %s\n",
442	       pinctrl_dev_get_name(pctldev), map->function);
443	return -EINVAL;
444}
445
446/**
447 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
448 */
449static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
450				struct pinctrl *p,
451				struct device *dev,
452				const char *devname,
453				struct pinctrl_map const *map)
454{
455	unsigned func_selector;
456	unsigned group_selector;
457	struct pinmux_group *grp;
458	int ret;
459
460	/*
461	 * Note that we're not locking the pinmux mutex here, because
462	 * this is only called at pinmux initialization time when it
463	 * has not been added to any list and thus is not reachable
464	 * by anyone else.
465	 */
466
467	if (p->pctldev && p->pctldev != pctldev) {
468		dev_err(pctldev->dev,
469			"different pin control devices given for device %s, function %s\n",
470			devname, map->function);
471		return -EINVAL;
472	}
473	p->dev = dev;
474	p->pctldev = pctldev;
475
476	/* Now go into the driver and try to match a function and group */
477	ret = pinmux_search_function(pctldev, map, &func_selector,
478				     &group_selector);
479	if (ret < 0)
480		return ret;
481
482	/* Now add this group selector, we may have many of them */
483	grp = kmalloc(sizeof(*grp), GFP_KERNEL);
484	if (!grp)
485		return -ENOMEM;
486	grp->func_selector = func_selector;
487	grp->group_selector = group_selector;
488	ret = acquire_pins(pctldev, devname, group_selector);
489	if (ret) {
490		kfree(grp);
491		return ret;
492	}
493	list_add_tail(&grp->node, &p->groups);
494
495	return 0;
496}
497
498/**
499 * pinmux_apply_muxmap() - apply a certain mux mapping entry
500 */
501int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
502			struct pinctrl *p,
503			struct device *dev,
504			const char *devname,
505			struct pinctrl_map const *map)
506{
507	int ret;
508
509	ret = pinmux_enable_muxmap(pctldev, p, dev,
510				   devname, map);
511	if (ret) {
512		pinmux_put(p);
513		return ret;
514	}
515
516	return 0;
517}
518
519/**
520 * pinmux_put() - free up the pinmux portions of a pin controller handle
521 */
522void pinmux_put(struct pinctrl *p)
523{
524	struct list_head *node, *tmp;
525
526	list_for_each_safe(node, tmp, &p->groups) {
527		struct pinmux_group *grp =
528			list_entry(node, struct pinmux_group, node);
529		/* Release all pins taken by this group */
530		release_pins(p->pctldev, grp->group_selector);
531		list_del(node);
532		kfree(grp);
533	}
534}
535
536/**
537 * pinmux_enable() - enable the pinmux portion of a pin control handle
538 */
539int pinmux_enable(struct pinctrl *p)
540{
541	struct pinctrl_dev *pctldev = p->pctldev;
542	const struct pinmux_ops *ops = pctldev->desc->pmxops;
543	struct pinmux_group *grp;
544	int ret;
545
546	list_for_each_entry(grp, &p->groups, node) {
547		ret = ops->enable(pctldev, grp->func_selector,
548				  grp->group_selector);
549		if (ret)
550			/*
551			 * TODO: call disable() on all groups we called
552			 * enable() on to this point?
553			 */
554			return ret;
555	}
556	return 0;
557}
558
559/**
560 * pinmux_disable() - disable the pinmux portions of a pin control handle
561 */
562void pinmux_disable(struct pinctrl *p)
563{
564	struct pinctrl_dev *pctldev = p->pctldev;
565	const struct pinmux_ops *ops = pctldev->desc->pmxops;
566	struct pinmux_group *grp;
567
568	list_for_each_entry(grp, &p->groups, node) {
569		ops->disable(pctldev, grp->func_selector,
570			     grp->group_selector);
571	}
572}
573
574#ifdef CONFIG_DEBUG_FS
575
576/* Called from pincontrol core */
577static int pinmux_functions_show(struct seq_file *s, void *what)
578{
579	struct pinctrl_dev *pctldev = s->private;
580	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
581	unsigned func_selector = 0;
582
583	while (pmxops->list_functions(pctldev, func_selector) >= 0) {
584		const char *func = pmxops->get_function_name(pctldev,
585							  func_selector);
586		const char * const *groups;
587		unsigned num_groups;
588		int ret;
589		int i;
590
591		ret = pmxops->get_function_groups(pctldev, func_selector,
592						  &groups, &num_groups);
593		if (ret)
594			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
595				   func);
596
597		seq_printf(s, "function: %s, groups = [ ", func);
598		for (i = 0; i < num_groups; i++)
599			seq_printf(s, "%s ", groups[i]);
600		seq_puts(s, "]\n");
601
602		func_selector++;
603
604	}
605
606	return 0;
607}
608
609static int pinmux_pins_show(struct seq_file *s, void *what)
610{
611	struct pinctrl_dev *pctldev = s->private;
612	unsigned i, pin;
613
614	seq_puts(s, "Pinmux settings per pin\n");
615	seq_puts(s, "Format: pin (name): owner\n");
616
617	/* The pin number can be retrived from the pin controller descriptor */
618	for (i = 0; i < pctldev->desc->npins; i++) {
619		struct pin_desc *desc;
620		bool is_hog = false;
621
622		pin = pctldev->desc->pins[i].number;
623		desc = pin_desc_get(pctldev, pin);
624		/* Skip if we cannot search the pin */
625		if (desc == NULL)
626			continue;
627
628		if (desc->owner &&
629		    !strcmp(desc->owner, pinctrl_dev_get_name(pctldev)))
630			is_hog = true;
631
632		seq_printf(s, "pin %d (%s): %s%s\n", pin,
633			   desc->name ? desc->name : "unnamed",
634			   desc->owner ? desc->owner : "UNCLAIMED",
635			   is_hog ? " (HOG)" : "");
636	}
637
638	return 0;
639}
640
641void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
642{
643	struct pinctrl_dev *pctldev = p->pctldev;
644	const struct pinmux_ops *pmxops;
645	const struct pinctrl_ops *pctlops;
646	struct pinmux_group *grp;
647	const char *sep = "";
648
649	pmxops = pctldev->desc->pmxops;
650	pctlops = pctldev->desc->pctlops;
651
652	seq_printf(s, " groups: [");
653	list_for_each_entry(grp, &p->groups, node) {
654		seq_printf(s, "%s%s (%u)=%s (%u)",
655			   sep,
656			   pctlops->get_group_name(pctldev,
657						   grp->group_selector),
658			   grp->group_selector,
659			   pmxops->get_function_name(pctldev,
660						     grp->func_selector),
661			   grp->func_selector);
662		sep = ", ";
663	}
664	seq_printf(s, " ]");
665}
666
667static int pinmux_functions_open(struct inode *inode, struct file *file)
668{
669	return single_open(file, pinmux_functions_show, inode->i_private);
670}
671
672static int pinmux_pins_open(struct inode *inode, struct file *file)
673{
674	return single_open(file, pinmux_pins_show, inode->i_private);
675}
676
677static const struct file_operations pinmux_functions_ops = {
678	.open		= pinmux_functions_open,
679	.read		= seq_read,
680	.llseek		= seq_lseek,
681	.release	= single_release,
682};
683
684static const struct file_operations pinmux_pins_ops = {
685	.open		= pinmux_pins_open,
686	.read		= seq_read,
687	.llseek		= seq_lseek,
688	.release	= single_release,
689};
690
691void pinmux_init_device_debugfs(struct dentry *devroot,
692			 struct pinctrl_dev *pctldev)
693{
694	debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
695			    devroot, pctldev, &pinmux_functions_ops);
696	debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
697			    devroot, pctldev, &pinmux_pins_ops);
698}
699
700#endif /* CONFIG_DEBUG_FS */
701