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 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
12 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinmux core: " fmt
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
21#include <linux/radix-tree.h>
22#include <linux/err.h>
23#include <linux/list.h>
24#include <linux/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
33int pinmux_check_ops(struct pinctrl_dev *pctldev)
34{
35	const struct pinmux_ops *ops = pctldev->desc->pmxops;
36	unsigned nfuncs;
37	unsigned selector = 0;
38
39	/* Check that we implement required operations */
40	if (!ops ||
41	    !ops->get_functions_count ||
42	    !ops->get_function_name ||
43	    !ops->get_function_groups ||
44	    !ops->set_mux) {
45		dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
46		return -EINVAL;
47	}
48	/* Check that all functions registered have names */
49	nfuncs = ops->get_functions_count(pctldev);
50	while (selector < nfuncs) {
51		const char *fname = ops->get_function_name(pctldev,
52							   selector);
53		if (!fname) {
54			dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
55				selector);
56			return -EINVAL;
57		}
58		selector++;
59	}
60
61	return 0;
62}
63
64int pinmux_validate_map(struct pinctrl_map const *map, int i)
65{
66	if (!map->data.mux.function) {
67		pr_err("failed to register map %s (%d): no function given\n",
68		       map->name, i);
69		return -EINVAL;
70	}
71
72	return 0;
73}
74
75/**
76 * pin_request() - request a single pin to be muxed in, typically for GPIO
77 * @pin: the pin number in the global pin space
78 * @owner: a representation of the owner of this pin; typically the device
79 *	name that controls its mux function, or the requested GPIO name
80 * @gpio_range: the range matching the GPIO pin if this is a request for a
81 *	single GPIO pin
82 */
83static int pin_request(struct pinctrl_dev *pctldev,
84		       int pin, const char *owner,
85		       struct pinctrl_gpio_range *gpio_range)
86{
87	struct pin_desc *desc;
88	const struct pinmux_ops *ops = pctldev->desc->pmxops;
89	int status = -EINVAL;
90
91	desc = pin_desc_get(pctldev, pin);
92	if (desc == NULL) {
93		dev_err(pctldev->dev,
94			"pin %d is not registered so it cannot be requested\n",
95			pin);
96		goto out;
97	}
98
99	dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
100		pin, desc->name, owner);
101
102	if (gpio_range) {
103		/* There's no need to support multiple GPIO requests */
104		if (desc->gpio_owner) {
105			dev_err(pctldev->dev,
106				"pin %s already requested by %s; cannot claim for %s\n",
107				desc->name, desc->gpio_owner, owner);
108			goto out;
109		}
110
111		desc->gpio_owner = owner;
112	} else {
113		if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
114			dev_err(pctldev->dev,
115				"pin %s already requested by %s; cannot claim for %s\n",
116				desc->name, desc->mux_owner, owner);
117			goto out;
118		}
119
120		desc->mux_usecount++;
121		if (desc->mux_usecount > 1)
122			return 0;
123
124		desc->mux_owner = owner;
125	}
126
127	/* Let each pin increase references to this module */
128	if (!try_module_get(pctldev->owner)) {
129		dev_err(pctldev->dev,
130			"could not increase module refcount for pin %d\n",
131			pin);
132		status = -EINVAL;
133		goto out_free_pin;
134	}
135
136	/*
137	 * If there is no kind of request function for the pin we just assume
138	 * we got it by default and proceed.
139	 */
140	if (gpio_range && ops->gpio_request_enable)
141		/* This requests and enables a single GPIO pin */
142		status = ops->gpio_request_enable(pctldev, gpio_range, pin);
143	else if (ops->request)
144		status = ops->request(pctldev, pin);
145	else
146		status = 0;
147
148	if (status) {
149		dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
150		module_put(pctldev->owner);
151	}
152
153out_free_pin:
154	if (status) {
155		if (gpio_range) {
156			desc->gpio_owner = NULL;
157		} else {
158			desc->mux_usecount--;
159			if (!desc->mux_usecount)
160				desc->mux_owner = NULL;
161		}
162	}
163out:
164	if (status)
165		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
166			pin, owner, status);
167
168	return status;
169}
170
171/**
172 * pin_free() - release a single muxed in pin so something else can be muxed
173 * @pctldev: pin controller device handling this pin
174 * @pin: the pin to free
175 * @gpio_range: the range matching the GPIO pin if this is a request for a
176 *	single GPIO pin
177 *
178 * This function returns a pointer to the previous owner. This is used
179 * for callers that dynamically allocate an owner name so it can be freed
180 * once the pin is free. This is done for GPIO request functions.
181 */
182static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
183			    struct pinctrl_gpio_range *gpio_range)
184{
185	const struct pinmux_ops *ops = pctldev->desc->pmxops;
186	struct pin_desc *desc;
187	const char *owner;
188
189	desc = pin_desc_get(pctldev, pin);
190	if (desc == NULL) {
191		dev_err(pctldev->dev,
192			"pin is not registered so it cannot be freed\n");
193		return NULL;
194	}
195
196	if (!gpio_range) {
197		/*
198		 * A pin should not be freed more times than allocated.
199		 */
200		if (WARN_ON(!desc->mux_usecount))
201			return NULL;
202		desc->mux_usecount--;
203		if (desc->mux_usecount)
204			return NULL;
205	}
206
207	/*
208	 * If there is no kind of request function for the pin we just assume
209	 * we got it by default and proceed.
210	 */
211	if (gpio_range && ops->gpio_disable_free)
212		ops->gpio_disable_free(pctldev, gpio_range, pin);
213	else if (ops->free)
214		ops->free(pctldev, pin);
215
216	if (gpio_range) {
217		owner = desc->gpio_owner;
218		desc->gpio_owner = NULL;
219	} else {
220		owner = desc->mux_owner;
221		desc->mux_owner = NULL;
222		desc->mux_setting = NULL;
223	}
224
225	module_put(pctldev->owner);
226
227	return owner;
228}
229
230/**
231 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
232 * @pctldev: pin controller device affected
233 * @pin: the pin to mux in for GPIO
234 * @range: the applicable GPIO range
235 */
236int pinmux_request_gpio(struct pinctrl_dev *pctldev,
237			struct pinctrl_gpio_range *range,
238			unsigned pin, unsigned gpio)
239{
240	const char *owner;
241	int ret;
242
243	/* Conjure some name stating what chip and pin this is taken by */
244	owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
245	if (!owner)
246		return -EINVAL;
247
248	ret = pin_request(pctldev, pin, owner, range);
249	if (ret < 0)
250		kfree(owner);
251
252	return ret;
253}
254
255/**
256 * pinmux_free_gpio() - release a pin from GPIO muxing
257 * @pctldev: the pin controller device for the pin
258 * @pin: the affected currently GPIO-muxed in pin
259 * @range: applicable GPIO range
260 */
261void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
262		      struct pinctrl_gpio_range *range)
263{
264	const char *owner;
265
266	owner = pin_free(pctldev, pin, range);
267	kfree(owner);
268}
269
270/**
271 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
272 * @pctldev: the pin controller handling this pin
273 * @range: applicable GPIO range
274 * @pin: the affected GPIO pin in this controller
275 * @input: true if we set the pin as input, false for output
276 */
277int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
278			  struct pinctrl_gpio_range *range,
279			  unsigned pin, bool input)
280{
281	const struct pinmux_ops *ops;
282	int ret;
283
284	ops = pctldev->desc->pmxops;
285
286	if (ops->gpio_set_direction)
287		ret = ops->gpio_set_direction(pctldev, range, pin, input);
288	else
289		ret = 0;
290
291	return ret;
292}
293
294static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
295					const char *function)
296{
297	const struct pinmux_ops *ops = pctldev->desc->pmxops;
298	unsigned nfuncs = ops->get_functions_count(pctldev);
299	unsigned selector = 0;
300
301	/* See if this pctldev has this function */
302	while (selector < nfuncs) {
303		const char *fname = ops->get_function_name(pctldev,
304							   selector);
305
306		if (!strcmp(function, fname))
307			return selector;
308
309		selector++;
310	}
311
312	pr_err("%s does not support function %s\n",
313	       pinctrl_dev_get_name(pctldev), function);
314	return -EINVAL;
315}
316
317int pinmux_map_to_setting(struct pinctrl_map const *map,
318			  struct pinctrl_setting *setting)
319{
320	struct pinctrl_dev *pctldev = setting->pctldev;
321	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
322	char const * const *groups;
323	unsigned num_groups;
324	int ret;
325	const char *group;
326	int i;
327
328	if (!pmxops) {
329		dev_err(pctldev->dev, "does not support mux function\n");
330		return -EINVAL;
331	}
332
333	ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
334	if (ret < 0) {
335		dev_err(pctldev->dev, "invalid function %s in map table\n",
336			map->data.mux.function);
337		return ret;
338	}
339	setting->data.mux.func = ret;
340
341	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
342					  &groups, &num_groups);
343	if (ret < 0) {
344		dev_err(pctldev->dev, "can't query groups for function %s\n",
345			map->data.mux.function);
346		return ret;
347	}
348	if (!num_groups) {
349		dev_err(pctldev->dev,
350			"function %s can't be selected on any group\n",
351			map->data.mux.function);
352		return -EINVAL;
353	}
354	if (map->data.mux.group) {
355		bool found = false;
356		group = map->data.mux.group;
357		for (i = 0; i < num_groups; i++) {
358			if (!strcmp(group, groups[i])) {
359				found = true;
360				break;
361			}
362		}
363		if (!found) {
364			dev_err(pctldev->dev,
365				"invalid group \"%s\" for function \"%s\"\n",
366				group, map->data.mux.function);
367			return -EINVAL;
368		}
369	} else {
370		group = groups[0];
371	}
372
373	ret = pinctrl_get_group_selector(pctldev, group);
374	if (ret < 0) {
375		dev_err(pctldev->dev, "invalid group %s in map table\n",
376			map->data.mux.group);
377		return ret;
378	}
379	setting->data.mux.group = ret;
380
381	return 0;
382}
383
384void pinmux_free_setting(struct pinctrl_setting const *setting)
385{
386	/* This function is currently unused */
387}
388
389int pinmux_enable_setting(struct pinctrl_setting const *setting)
390{
391	struct pinctrl_dev *pctldev = setting->pctldev;
392	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
393	const struct pinmux_ops *ops = pctldev->desc->pmxops;
394	int ret = 0;
395	const unsigned *pins = NULL;
396	unsigned num_pins = 0;
397	int i;
398	struct pin_desc *desc;
399
400	if (pctlops->get_group_pins)
401		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
402					      &pins, &num_pins);
403
404	if (ret) {
405		const char *gname;
406
407		/* errors only affect debug data, so just warn */
408		gname = pctlops->get_group_name(pctldev,
409						setting->data.mux.group);
410		dev_warn(pctldev->dev,
411			 "could not get pins for group %s\n",
412			 gname);
413		num_pins = 0;
414	}
415
416	/* Try to allocate all pins in this group, one by one */
417	for (i = 0; i < num_pins; i++) {
418		ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
419		if (ret) {
420			const char *gname;
421			const char *pname;
422
423			desc = pin_desc_get(pctldev, pins[i]);
424			pname = desc ? desc->name : "non-existing";
425			gname = pctlops->get_group_name(pctldev,
426						setting->data.mux.group);
427			dev_err(pctldev->dev,
428				"could not request pin %d (%s) from group %s "
429				" on device %s\n",
430				pins[i], pname, gname,
431				pinctrl_dev_get_name(pctldev));
432			goto err_pin_request;
433		}
434	}
435
436	/* Now that we have acquired the pins, encode the mux setting */
437	for (i = 0; i < num_pins; i++) {
438		desc = pin_desc_get(pctldev, pins[i]);
439		if (desc == NULL) {
440			dev_warn(pctldev->dev,
441				 "could not get pin desc for pin %d\n",
442				 pins[i]);
443			continue;
444		}
445		desc->mux_setting = &(setting->data.mux);
446	}
447
448	ret = ops->set_mux(pctldev, setting->data.mux.func,
449			   setting->data.mux.group);
450
451	if (ret)
452		goto err_set_mux;
453
454	return 0;
455
456err_set_mux:
457	for (i = 0; i < num_pins; i++) {
458		desc = pin_desc_get(pctldev, pins[i]);
459		if (desc)
460			desc->mux_setting = NULL;
461	}
462err_pin_request:
463	/* On error release all taken pins */
464	while (--i >= 0)
465		pin_free(pctldev, pins[i], NULL);
466
467	return ret;
468}
469
470void pinmux_disable_setting(struct pinctrl_setting const *setting)
471{
472	struct pinctrl_dev *pctldev = setting->pctldev;
473	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
474	int ret = 0;
475	const unsigned *pins = NULL;
476	unsigned num_pins = 0;
477	int i;
478	struct pin_desc *desc;
479
480	if (pctlops->get_group_pins)
481		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
482					      &pins, &num_pins);
483	if (ret) {
484		const char *gname;
485
486		/* errors only affect debug data, so just warn */
487		gname = pctlops->get_group_name(pctldev,
488						setting->data.mux.group);
489		dev_warn(pctldev->dev,
490			 "could not get pins for group %s\n",
491			 gname);
492		num_pins = 0;
493	}
494
495	/* Flag the descs that no setting is active */
496	for (i = 0; i < num_pins; i++) {
497		desc = pin_desc_get(pctldev, pins[i]);
498		if (desc == NULL) {
499			dev_warn(pctldev->dev,
500				 "could not get pin desc for pin %d\n",
501				 pins[i]);
502			continue;
503		}
504		if (desc->mux_setting == &(setting->data.mux)) {
505			desc->mux_setting = NULL;
506			/* And release the pin */
507			pin_free(pctldev, pins[i], NULL);
508		} else {
509			const char *gname;
510
511			gname = pctlops->get_group_name(pctldev,
512						setting->data.mux.group);
513			dev_warn(pctldev->dev,
514				 "not freeing pin %d (%s) as part of "
515				 "deactivating group %s - it is already "
516				 "used for some other setting",
517				 pins[i], desc->name, gname);
518		}
519	}
520}
521
522#ifdef CONFIG_DEBUG_FS
523
524/* Called from pincontrol core */
525static int pinmux_functions_show(struct seq_file *s, void *what)
526{
527	struct pinctrl_dev *pctldev = s->private;
528	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
529	unsigned nfuncs;
530	unsigned func_selector = 0;
531
532	if (!pmxops)
533		return 0;
534
535	mutex_lock(&pctldev->mutex);
536	nfuncs = pmxops->get_functions_count(pctldev);
537	while (func_selector < nfuncs) {
538		const char *func = pmxops->get_function_name(pctldev,
539							  func_selector);
540		const char * const *groups;
541		unsigned num_groups;
542		int ret;
543		int i;
544
545		ret = pmxops->get_function_groups(pctldev, func_selector,
546						  &groups, &num_groups);
547		if (ret)
548			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
549				   func);
550
551		seq_printf(s, "function: %s, groups = [ ", func);
552		for (i = 0; i < num_groups; i++)
553			seq_printf(s, "%s ", groups[i]);
554		seq_puts(s, "]\n");
555
556		func_selector++;
557	}
558
559	mutex_unlock(&pctldev->mutex);
560
561	return 0;
562}
563
564static int pinmux_pins_show(struct seq_file *s, void *what)
565{
566	struct pinctrl_dev *pctldev = s->private;
567	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
568	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
569	unsigned i, pin;
570
571	if (!pmxops)
572		return 0;
573
574	seq_puts(s, "Pinmux settings per pin\n");
575	seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
576
577	mutex_lock(&pctldev->mutex);
578
579	/* The pin number can be retrived from the pin controller descriptor */
580	for (i = 0; i < pctldev->desc->npins; i++) {
581		struct pin_desc *desc;
582		bool is_hog = false;
583
584		pin = pctldev->desc->pins[i].number;
585		desc = pin_desc_get(pctldev, pin);
586		/* Skip if we cannot search the pin */
587		if (desc == NULL)
588			continue;
589
590		if (desc->mux_owner &&
591		    !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
592			is_hog = true;
593
594		seq_printf(s, "pin %d (%s): %s %s%s", pin,
595			   desc->name ? desc->name : "unnamed",
596			   desc->mux_owner ? desc->mux_owner
597				: "(MUX UNCLAIMED)",
598			   desc->gpio_owner ? desc->gpio_owner
599				: "(GPIO UNCLAIMED)",
600			   is_hog ? " (HOG)" : "");
601
602		if (desc->mux_setting)
603			seq_printf(s, " function %s group %s\n",
604				   pmxops->get_function_name(pctldev,
605					desc->mux_setting->func),
606				   pctlops->get_group_name(pctldev,
607					desc->mux_setting->group));
608		else
609			seq_printf(s, "\n");
610	}
611
612	mutex_unlock(&pctldev->mutex);
613
614	return 0;
615}
616
617void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
618{
619	seq_printf(s, "group %s\nfunction %s\n",
620		map->data.mux.group ? map->data.mux.group : "(default)",
621		map->data.mux.function);
622}
623
624void pinmux_show_setting(struct seq_file *s,
625			 struct pinctrl_setting const *setting)
626{
627	struct pinctrl_dev *pctldev = setting->pctldev;
628	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
629	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
630
631	seq_printf(s, "group: %s (%u) function: %s (%u)\n",
632		   pctlops->get_group_name(pctldev, setting->data.mux.group),
633		   setting->data.mux.group,
634		   pmxops->get_function_name(pctldev, setting->data.mux.func),
635		   setting->data.mux.func);
636}
637
638static int pinmux_functions_open(struct inode *inode, struct file *file)
639{
640	return single_open(file, pinmux_functions_show, inode->i_private);
641}
642
643static int pinmux_pins_open(struct inode *inode, struct file *file)
644{
645	return single_open(file, pinmux_pins_show, inode->i_private);
646}
647
648static const struct file_operations pinmux_functions_ops = {
649	.open		= pinmux_functions_open,
650	.read		= seq_read,
651	.llseek		= seq_lseek,
652	.release	= single_release,
653};
654
655static const struct file_operations pinmux_pins_ops = {
656	.open		= pinmux_pins_open,
657	.read		= seq_read,
658	.llseek		= seq_lseek,
659	.release	= single_release,
660};
661
662void pinmux_init_device_debugfs(struct dentry *devroot,
663			 struct pinctrl_dev *pctldev)
664{
665	debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
666			    devroot, pctldev, &pinmux_functions_ops);
667	debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
668			    devroot, pctldev, &pinmux_pins_ops);
669}
670
671#endif /* CONFIG_DEBUG_FS */
672