Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Core driver for the pin muxing portions of 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) "pinmux core: " fmt
 14
 15#include <linux/array_size.h>
 16#include <linux/ctype.h>
 17#include <linux/debugfs.h>
 18#include <linux/device.h>
 
 
 19#include <linux/err.h>
 20#include <linux/init.h>
 21#include <linux/list.h>
 22#include <linux/module.h>
 23#include <linux/radix-tree.h>
 24#include <linux/seq_file.h>
 25#include <linux/slab.h>
 26#include <linux/string.h>
 27
 
 
 28#include <linux/pinctrl/machine.h>
 29#include <linux/pinctrl/pinctrl.h>
 30#include <linux/pinctrl/pinmux.h>
 31
 32#include "core.h"
 33#include "pinmux.h"
 34
 35int pinmux_check_ops(struct pinctrl_dev *pctldev)
 36{
 37	const struct pinmux_ops *ops = pctldev->desc->pmxops;
 38	unsigned int nfuncs;
 39	unsigned int selector = 0;
 40
 41	/* Check that we implement required operations */
 42	if (!ops ||
 43	    !ops->get_functions_count ||
 44	    !ops->get_function_name ||
 45	    !ops->get_function_groups ||
 46	    !ops->set_mux) {
 47		dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
 48		return -EINVAL;
 49	}
 50	/* Check that all functions registered have names */
 51	nfuncs = ops->get_functions_count(pctldev);
 52	while (selector < nfuncs) {
 53		const char *fname = ops->get_function_name(pctldev,
 54							   selector);
 55		if (!fname) {
 56			dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
 57				selector);
 58			return -EINVAL;
 59		}
 60		selector++;
 61	}
 62
 63	return 0;
 64}
 65
 66int pinmux_validate_map(const struct pinctrl_map *map, int i)
 67{
 68	if (!map->data.mux.function) {
 69		pr_err("failed to register map %s (%d): no function given\n",
 70		       map->name, i);
 71		return -EINVAL;
 72	}
 73
 74	return 0;
 75}
 76
 77/**
 78 * pinmux_can_be_used_for_gpio() - check if a specific pin
 79 *	is either muxed to a different function or used as gpio.
 80 *
 81 * @pctldev: the associated pin controller device
 82 * @pin: the pin number in the global pin space
 83 *
 84 * Controllers not defined as strict will always return true,
 85 * menaning that the gpio can be used.
 86 */
 87bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned int pin)
 88{
 89	struct pin_desc *desc = pin_desc_get(pctldev, pin);
 90	const struct pinmux_ops *ops = pctldev->desc->pmxops;
 91
 92	/* Can't inspect pin, assume it can be used */
 93	if (!desc || !ops)
 94		return true;
 95
 96	if (ops->strict && desc->mux_usecount)
 97		return false;
 98
 99	return !(ops->strict && !!desc->gpio_owner);
100}
101
102/**
103 * pin_request() - request a single pin to be muxed in, typically for GPIO
104 * @pctldev: the associated pin controller device
105 * @pin: the pin number in the global pin space
106 * @owner: a representation of the owner of this pin; typically the device
107 *	name that controls its mux function, or the requested GPIO name
108 * @gpio_range: the range matching the GPIO pin if this is a request for a
109 *	single GPIO pin
110 */
111static int pin_request(struct pinctrl_dev *pctldev,
112		       int pin, const char *owner,
113		       struct pinctrl_gpio_range *gpio_range)
114{
115	struct pin_desc *desc;
116	const struct pinmux_ops *ops = pctldev->desc->pmxops;
117	int status = -EINVAL;
118
119	desc = pin_desc_get(pctldev, pin);
120	if (desc == NULL) {
121		dev_err(pctldev->dev,
122			"pin %d is not registered so it cannot be requested\n",
123			pin);
124		goto out;
125	}
126
127	dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
128		pin, desc->name, owner);
129
130	if ((!gpio_range || ops->strict) &&
131	    desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
132		dev_err(pctldev->dev,
133			"pin %s already requested by %s; cannot claim for %s\n",
134			desc->name, desc->mux_owner, owner);
135		goto out;
136	}
137
138	if ((gpio_range || ops->strict) && desc->gpio_owner) {
139		dev_err(pctldev->dev,
140			"pin %s already requested by %s; cannot claim for %s\n",
141			desc->name, desc->gpio_owner, owner);
142		goto out;
143	}
144
145	if (gpio_range) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146		desc->gpio_owner = owner;
147	} else {
 
 
 
 
 
 
 
 
 
 
 
 
 
148		desc->mux_usecount++;
149		if (desc->mux_usecount > 1)
150			return 0;
151
152		desc->mux_owner = owner;
153	}
154
155	/* Let each pin increase references to this module */
156	if (!try_module_get(pctldev->owner)) {
157		dev_err(pctldev->dev,
158			"could not increase module refcount for pin %d\n",
159			pin);
160		status = -EINVAL;
161		goto out_free_pin;
162	}
163
164	/*
165	 * If there is no kind of request function for the pin we just assume
166	 * we got it by default and proceed.
167	 */
168	if (gpio_range && ops->gpio_request_enable)
169		/* This requests and enables a single GPIO pin */
170		status = ops->gpio_request_enable(pctldev, gpio_range, pin);
171	else if (ops->request)
172		status = ops->request(pctldev, pin);
173	else
174		status = 0;
175
176	if (status)
 
177		module_put(pctldev->owner);
 
178
179out_free_pin:
180	if (status) {
181		if (gpio_range) {
182			desc->gpio_owner = NULL;
183		} else {
184			desc->mux_usecount--;
185			if (!desc->mux_usecount)
186				desc->mux_owner = NULL;
187		}
188	}
189out:
190	if (status)
191		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
192			pin, owner, status);
193
194	return status;
195}
196
197/**
198 * pin_free() - release a single muxed in pin so something else can be muxed
199 * @pctldev: pin controller device handling this pin
200 * @pin: the pin to free
201 * @gpio_range: the range matching the GPIO pin if this is a request for a
202 *	single GPIO pin
203 *
204 * This function returns a pointer to the previous owner. This is used
205 * for callers that dynamically allocate an owner name so it can be freed
206 * once the pin is free. This is done for GPIO request functions.
207 */
208static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
209			    struct pinctrl_gpio_range *gpio_range)
210{
211	const struct pinmux_ops *ops = pctldev->desc->pmxops;
212	struct pin_desc *desc;
213	const char *owner;
214
215	desc = pin_desc_get(pctldev, pin);
216	if (desc == NULL) {
217		dev_err(pctldev->dev,
218			"pin is not registered so it cannot be freed\n");
219		return NULL;
220	}
221
222	if (!gpio_range) {
223		/*
224		 * A pin should not be freed more times than allocated.
225		 */
226		if (WARN_ON(!desc->mux_usecount))
227			return NULL;
228		desc->mux_usecount--;
229		if (desc->mux_usecount)
230			return NULL;
231	}
232
233	/*
234	 * If there is no kind of request function for the pin we just assume
235	 * we got it by default and proceed.
236	 */
237	if (gpio_range && ops->gpio_disable_free)
238		ops->gpio_disable_free(pctldev, gpio_range, pin);
239	else if (ops->free)
240		ops->free(pctldev, pin);
241
242	if (gpio_range) {
243		owner = desc->gpio_owner;
244		desc->gpio_owner = NULL;
245	} else {
246		owner = desc->mux_owner;
247		desc->mux_owner = NULL;
248		desc->mux_setting = NULL;
249	}
250
251	module_put(pctldev->owner);
252
253	return owner;
254}
255
256/**
257 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
258 * @pctldev: pin controller device affected
259 * @pin: the pin to mux in for GPIO
260 * @range: the applicable GPIO range
261 * @gpio: number of requested GPIO
262 */
263int pinmux_request_gpio(struct pinctrl_dev *pctldev,
264			struct pinctrl_gpio_range *range,
265			unsigned int pin, unsigned int gpio)
266{
267	const char *owner;
268	int ret;
269
270	/* Conjure some name stating what chip and pin this is taken by */
271	owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
272	if (!owner)
273		return -ENOMEM;
274
275	ret = pin_request(pctldev, pin, owner, range);
276	if (ret < 0)
277		kfree(owner);
278
279	return ret;
280}
281
282/**
283 * pinmux_free_gpio() - release a pin from GPIO muxing
284 * @pctldev: the pin controller device for the pin
285 * @pin: the affected currently GPIO-muxed in pin
286 * @range: applicable GPIO range
287 */
288void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,
289		      struct pinctrl_gpio_range *range)
290{
291	const char *owner;
292
293	owner = pin_free(pctldev, pin, range);
294	kfree(owner);
295}
296
297/**
298 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
299 * @pctldev: the pin controller handling this pin
300 * @range: applicable GPIO range
301 * @pin: the affected GPIO pin in this controller
302 * @input: true if we set the pin as input, false for output
303 */
304int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
305			  struct pinctrl_gpio_range *range,
306			  unsigned int pin, bool input)
307{
308	const struct pinmux_ops *ops;
309	int ret;
310
311	ops = pctldev->desc->pmxops;
312
313	if (ops->gpio_set_direction)
314		ret = ops->gpio_set_direction(pctldev, range, pin, input);
315	else
316		ret = 0;
317
318	return ret;
319}
320
321static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
322					const char *function)
323{
324	const struct pinmux_ops *ops = pctldev->desc->pmxops;
325	unsigned int nfuncs = ops->get_functions_count(pctldev);
326	unsigned int selector = 0;
327
328	/* See if this pctldev has this function */
329	while (selector < nfuncs) {
330		const char *fname = ops->get_function_name(pctldev, selector);
331
332		if (!strcmp(function, fname))
333			return selector;
334
335		selector++;
336	}
337
 
338	return -EINVAL;
339}
340
341int pinmux_map_to_setting(const struct pinctrl_map *map,
342			  struct pinctrl_setting *setting)
343{
344	struct pinctrl_dev *pctldev = setting->pctldev;
345	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
346	char const * const *groups;
347	unsigned int num_groups;
348	int ret;
349	const char *group;
350
351	if (!pmxops) {
352		dev_err(pctldev->dev, "does not support mux function\n");
353		return -EINVAL;
354	}
355
356	ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
357	if (ret < 0) {
358		dev_err(pctldev->dev, "invalid function %s in map table\n",
359			map->data.mux.function);
360		return ret;
361	}
362	setting->data.mux.func = ret;
363
364	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
365					  &groups, &num_groups);
366	if (ret < 0) {
367		dev_err(pctldev->dev, "can't query groups for function %s\n",
368			map->data.mux.function);
369		return ret;
370	}
371	if (!num_groups) {
372		dev_err(pctldev->dev,
373			"function %s can't be selected on any group\n",
374			map->data.mux.function);
375		return -EINVAL;
376	}
377	if (map->data.mux.group) {
378		group = map->data.mux.group;
379		ret = match_string(groups, num_groups, group);
380		if (ret < 0) {
381			dev_err(pctldev->dev,
382				"invalid group \"%s\" for function \"%s\"\n",
383				group, map->data.mux.function);
384			return ret;
385		}
386	} else {
387		group = groups[0];
388	}
389
390	ret = pinctrl_get_group_selector(pctldev, group);
391	if (ret < 0) {
392		dev_err(pctldev->dev, "invalid group %s in map table\n",
393			map->data.mux.group);
394		return ret;
395	}
396	setting->data.mux.group = ret;
397
398	return 0;
399}
400
401void pinmux_free_setting(const struct pinctrl_setting *setting)
402{
403	/* This function is currently unused */
404}
405
406int pinmux_enable_setting(const struct pinctrl_setting *setting)
407{
408	struct pinctrl_dev *pctldev = setting->pctldev;
409	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
410	const struct pinmux_ops *ops = pctldev->desc->pmxops;
411	int ret = 0;
412	const unsigned int *pins = NULL;
413	unsigned int num_pins = 0;
414	int i;
415	struct pin_desc *desc;
416
417	if (pctlops->get_group_pins)
418		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
419					      &pins, &num_pins);
420
421	if (ret) {
422		const char *gname;
423
424		/* errors only affect debug data, so just warn */
425		gname = pctlops->get_group_name(pctldev,
426						setting->data.mux.group);
427		dev_warn(pctldev->dev,
428			 "could not get pins for group %s\n",
429			 gname);
430		num_pins = 0;
431	}
432
433	/* Try to allocate all pins in this group, one by one */
434	for (i = 0; i < num_pins; i++) {
435		ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
436		if (ret) {
437			const char *gname;
438			const char *pname;
439
440			desc = pin_desc_get(pctldev, pins[i]);
441			pname = desc ? desc->name : "non-existing";
442			gname = pctlops->get_group_name(pctldev,
443						setting->data.mux.group);
444			dev_err(pctldev->dev,
445				"could not request pin %d (%s) from group %s "
446				" on device %s\n",
447				pins[i], pname, gname,
448				pinctrl_dev_get_name(pctldev));
449			goto err_pin_request;
450		}
451	}
452
453	/* Now that we have acquired the pins, encode the mux setting */
454	for (i = 0; i < num_pins; i++) {
455		desc = pin_desc_get(pctldev, pins[i]);
456		if (desc == NULL) {
457			dev_warn(pctldev->dev,
458				 "could not get pin desc for pin %d\n",
459				 pins[i]);
460			continue;
461		}
462		desc->mux_setting = &(setting->data.mux);
463	}
464
465	ret = ops->set_mux(pctldev, setting->data.mux.func,
466			   setting->data.mux.group);
467
468	if (ret)
469		goto err_set_mux;
470
471	return 0;
472
473err_set_mux:
474	for (i = 0; i < num_pins; i++) {
475		desc = pin_desc_get(pctldev, pins[i]);
476		if (desc)
477			desc->mux_setting = NULL;
478	}
479err_pin_request:
480	/* On error release all taken pins */
481	while (--i >= 0)
482		pin_free(pctldev, pins[i], NULL);
483
484	return ret;
485}
486
487void pinmux_disable_setting(const struct pinctrl_setting *setting)
488{
489	struct pinctrl_dev *pctldev = setting->pctldev;
490	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
491	int ret = 0;
492	const unsigned int *pins = NULL;
493	unsigned int num_pins = 0;
494	int i;
495	struct pin_desc *desc;
496
497	if (pctlops->get_group_pins)
498		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
499					      &pins, &num_pins);
500	if (ret) {
501		const char *gname;
502
503		/* errors only affect debug data, so just warn */
504		gname = pctlops->get_group_name(pctldev,
505						setting->data.mux.group);
506		dev_warn(pctldev->dev,
507			 "could not get pins for group %s\n",
508			 gname);
509		num_pins = 0;
510	}
511
512	/* Flag the descs that no setting is active */
513	for (i = 0; i < num_pins; i++) {
514		desc = pin_desc_get(pctldev, pins[i]);
515		if (desc == NULL) {
516			dev_warn(pctldev->dev,
517				 "could not get pin desc for pin %d\n",
518				 pins[i]);
519			continue;
520		}
521		if (desc->mux_setting == &(setting->data.mux)) {
 
 
522			pin_free(pctldev, pins[i], NULL);
523		} else {
524			const char *gname;
525
526			gname = pctlops->get_group_name(pctldev,
527						setting->data.mux.group);
528			dev_warn(pctldev->dev,
529				 "not freeing pin %d (%s) as part of "
530				 "deactivating group %s - it is already "
531				 "used for some other setting",
532				 pins[i], desc->name, gname);
533		}
534	}
535}
536
537#ifdef CONFIG_DEBUG_FS
538
539/* Called from pincontrol core */
540static int pinmux_functions_show(struct seq_file *s, void *what)
541{
542	struct pinctrl_dev *pctldev = s->private;
543	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
544	unsigned int nfuncs;
545	unsigned int func_selector = 0;
546
547	if (!pmxops)
548		return 0;
549
550	mutex_lock(&pctldev->mutex);
551	nfuncs = pmxops->get_functions_count(pctldev);
552	while (func_selector < nfuncs) {
553		const char *func = pmxops->get_function_name(pctldev,
554							  func_selector);
555		const char * const *groups;
556		unsigned int num_groups;
557		int ret;
558		int i;
559
560		ret = pmxops->get_function_groups(pctldev, func_selector,
561						  &groups, &num_groups);
562		if (ret) {
563			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
564				   func);
565			func_selector++;
566			continue;
567		}
568
569		seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
570		for (i = 0; i < num_groups; i++)
571			seq_printf(s, "%s ", groups[i]);
572		seq_puts(s, "]\n");
573
574		func_selector++;
575	}
576
577	mutex_unlock(&pctldev->mutex);
578
579	return 0;
580}
581
582static int pinmux_pins_show(struct seq_file *s, void *what)
583{
584	struct pinctrl_dev *pctldev = s->private;
585	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
586	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
587	unsigned int i, pin;
588
589	if (!pmxops)
590		return 0;
591
592	seq_puts(s, "Pinmux settings per pin\n");
593	if (pmxops->strict)
594		seq_puts(s,
595		 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
596	else
597		seq_puts(s,
598		"Format: pin (name): mux_owner gpio_owner hog?\n");
599
600	mutex_lock(&pctldev->mutex);
601
602	/* The pin number can be retrived from the pin controller descriptor */
603	for (i = 0; i < pctldev->desc->npins; i++) {
604		struct pin_desc *desc;
605		bool is_hog = false;
606
607		pin = pctldev->desc->pins[i].number;
608		desc = pin_desc_get(pctldev, pin);
609		/* Skip if we cannot search the pin */
610		if (desc == NULL)
611			continue;
612
613		if (desc->mux_owner &&
614		    !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
615			is_hog = true;
616
617		if (pmxops->strict) {
618			if (desc->mux_owner)
619				seq_printf(s, "pin %d (%s): device %s%s",
620					   pin, desc->name, desc->mux_owner,
 
 
621					   is_hog ? " (HOG)" : "");
622			else if (desc->gpio_owner)
623				seq_printf(s, "pin %d (%s): GPIO %s",
624					   pin, desc->name, desc->gpio_owner);
 
 
625			else
626				seq_printf(s, "pin %d (%s): UNCLAIMED",
627					   pin, desc->name);
 
628		} else {
629			/* For non-strict controllers */
630			seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
 
631				   desc->mux_owner ? desc->mux_owner
632				   : "(MUX UNCLAIMED)",
633				   desc->gpio_owner ? desc->gpio_owner
634				   : "(GPIO UNCLAIMED)",
635				   is_hog ? " (HOG)" : "");
636		}
637
638		/* If mux: print function+group claiming the pin */
639		if (desc->mux_setting)
640			seq_printf(s, " function %s group %s\n",
641				   pmxops->get_function_name(pctldev,
642					desc->mux_setting->func),
643				   pctlops->get_group_name(pctldev,
644					desc->mux_setting->group));
645		else
646			seq_putc(s, '\n');
647	}
648
649	mutex_unlock(&pctldev->mutex);
650
651	return 0;
652}
653
654void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
655{
656	seq_printf(s, "group %s\nfunction %s\n",
657		map->data.mux.group ? map->data.mux.group : "(default)",
658		map->data.mux.function);
659}
660
661void pinmux_show_setting(struct seq_file *s,
662			 const struct pinctrl_setting *setting)
663{
664	struct pinctrl_dev *pctldev = setting->pctldev;
665	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
666	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
667
668	seq_printf(s, "group: %s (%u) function: %s (%u)\n",
669		   pctlops->get_group_name(pctldev, setting->data.mux.group),
670		   setting->data.mux.group,
671		   pmxops->get_function_name(pctldev, setting->data.mux.func),
672		   setting->data.mux.func);
673}
674
675DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
676DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
677
678static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
679				   size_t len, loff_t *ppos)
680{
681	struct seq_file *sfile = file->private_data;
682	struct pinctrl_dev *pctldev = sfile->private;
683	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
684	const char *const *groups;
685	char *buf, *gname, *fname;
686	unsigned int num_groups;
687	int fsel, gsel, ret;
688
689	buf = memdup_user_nul(user_buf, len);
690	if (IS_ERR(buf))
691		return PTR_ERR(buf);
692
693	/* remove leading and trailing spaces of input buffer */
694	gname = strstrip(buf);
695	if (*gname == '\0') {
696		ret = -EINVAL;
697		goto exit_free_buf;
698	}
699
700	/* find a separator which is a spacelike character */
701	for (fname = gname; !isspace(*fname); fname++) {
702		if (*fname == '\0') {
703			ret = -EINVAL;
704			goto exit_free_buf;
705		}
706	}
707	*fname = '\0';
708
709	/* drop extra spaces between function and group names */
710	fname = skip_spaces(fname + 1);
711	if (*fname == '\0') {
712		ret = -EINVAL;
713		goto exit_free_buf;
714	}
715
716	ret = pinmux_func_name_to_selector(pctldev, fname);
717	if (ret < 0) {
718		dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
719		goto exit_free_buf;
720	}
721	fsel = ret;
722
723	ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
724	if (ret) {
725		dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
726		goto exit_free_buf;
727	}
728
729	ret = match_string(groups, num_groups, gname);
730	if (ret < 0) {
731		dev_err(pctldev->dev, "invalid group %s", gname);
732		goto exit_free_buf;
733	}
734
735	ret = pinctrl_get_group_selector(pctldev, gname);
736	if (ret < 0)
737		goto exit_free_buf;
738	gsel = ret;
739
740	ret = pmxops->set_mux(pctldev, fsel, gsel);
741	if (ret) {
742		dev_err(pctldev->dev, "set_mux() failed: %d", ret);
743		goto exit_free_buf;
744	}
745	ret = len;
746
747exit_free_buf:
748	kfree(buf);
749
750	return ret;
751}
752
753static int pinmux_select_open(struct inode *inode, struct file *file)
754{
755	return single_open(file, NULL, inode->i_private);
756}
757
758static const struct file_operations pinmux_select_ops = {
759	.owner = THIS_MODULE,
760	.open = pinmux_select_open,
761	.write = pinmux_select,
762	.llseek = no_llseek,
763	.release = single_release,
 
 
 
 
 
 
764};
765
766void pinmux_init_device_debugfs(struct dentry *devroot,
767			 struct pinctrl_dev *pctldev)
768{
769	debugfs_create_file("pinmux-functions", 0444,
770			    devroot, pctldev, &pinmux_functions_fops);
771	debugfs_create_file("pinmux-pins", 0444,
772			    devroot, pctldev, &pinmux_pins_fops);
773	debugfs_create_file("pinmux-select", 0200,
774			    devroot, pctldev, &pinmux_select_ops);
775}
776
777#endif /* CONFIG_DEBUG_FS */
778
779#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
780
781/**
782 * pinmux_generic_get_function_count() - returns number of functions
783 * @pctldev: pin controller device
784 */
785int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
786{
787	return pctldev->num_functions;
788}
789EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
790
791/**
792 * pinmux_generic_get_function_name() - returns the function name
793 * @pctldev: pin controller device
794 * @selector: function number
795 */
796const char *
797pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
798				 unsigned int selector)
799{
800	struct function_desc *function;
801
802	function = radix_tree_lookup(&pctldev->pin_function_tree,
803				     selector);
804	if (!function)
805		return NULL;
806
807	return function->name;
808}
809EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
810
811/**
812 * pinmux_generic_get_function_groups() - gets the function groups
813 * @pctldev: pin controller device
814 * @selector: function number
815 * @groups: array of pin groups
816 * @num_groups: number of pin groups
817 */
818int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
819				       unsigned int selector,
820				       const char * const **groups,
821				       unsigned int * const num_groups)
822{
823	struct function_desc *function;
824
825	function = radix_tree_lookup(&pctldev->pin_function_tree,
826				     selector);
827	if (!function) {
828		dev_err(pctldev->dev, "%s could not find function%i\n",
829			__func__, selector);
830		return -EINVAL;
831	}
832	*groups = function->group_names;
833	*num_groups = function->num_group_names;
834
835	return 0;
836}
837EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
838
839/**
840 * pinmux_generic_get_function() - returns a function based on the number
841 * @pctldev: pin controller device
842 * @selector: function number
843 */
844struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
845						  unsigned int selector)
846{
847	struct function_desc *function;
848
849	function = radix_tree_lookup(&pctldev->pin_function_tree,
850				     selector);
851	if (!function)
852		return NULL;
853
854	return function;
855}
856EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
857
858/**
859 * pinmux_generic_add_function() - adds a function group
860 * @pctldev: pin controller device
861 * @name: name of the function
862 * @groups: array of pin groups
863 * @num_groups: number of pin groups
864 * @data: pin controller driver specific data
865 */
866int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
867				const char *name,
868				const char * const *groups,
869				const unsigned int num_groups,
870				void *data)
871{
872	struct function_desc *function;
873	int selector, error;
874
875	if (!name)
876		return -EINVAL;
877
878	selector = pinmux_func_name_to_selector(pctldev, name);
879	if (selector >= 0)
880		return selector;
881
882	selector = pctldev->num_functions;
883
884	function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
885	if (!function)
886		return -ENOMEM;
887
888	function->name = name;
889	function->group_names = groups;
890	function->num_group_names = num_groups;
891	function->data = data;
892
893	error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
894	if (error)
895		return error;
896
897	pctldev->num_functions++;
898
899	return selector;
900}
901EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
902
903/**
904 * pinmux_generic_remove_function() - removes a numbered function
905 * @pctldev: pin controller device
906 * @selector: function number
907 *
908 * Note that the caller must take care of locking.
909 */
910int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
911				   unsigned int selector)
912{
913	struct function_desc *function;
914
915	function = radix_tree_lookup(&pctldev->pin_function_tree,
916				     selector);
917	if (!function)
918		return -ENOENT;
919
920	radix_tree_delete(&pctldev->pin_function_tree, selector);
921	devm_kfree(pctldev->dev, function);
922
923	pctldev->num_functions--;
924
925	return 0;
926}
927EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
928
929/**
930 * pinmux_generic_free_functions() - removes all functions
931 * @pctldev: pin controller device
932 *
933 * Note that the caller must take care of locking. The pinctrl
934 * functions are allocated with devm_kzalloc() so no need to free
935 * them here.
936 */
937void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
938{
939	struct radix_tree_iter iter;
940	void __rcu **slot;
941
942	radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
943		radix_tree_delete(&pctldev->pin_function_tree, iter.index);
944
945	pctldev->num_functions = 0;
946}
947
948#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */
v4.6
 
  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		if (ops->strict && desc->mux_usecount &&
111		    strcmp(desc->mux_owner, owner)) {
112			dev_err(pctldev->dev,
113				"pin %s already requested by %s; cannot claim for %s\n",
114				desc->name, desc->mux_owner, owner);
115			goto out;
116		}
117
118		desc->gpio_owner = owner;
119	} else {
120		if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
121			dev_err(pctldev->dev,
122				"pin %s already requested by %s; cannot claim for %s\n",
123				desc->name, desc->mux_owner, owner);
124			goto out;
125		}
126		if (ops->strict && desc->gpio_owner) {
127			dev_err(pctldev->dev,
128				"pin %s already requested by %s; cannot claim for %s\n",
129				desc->name, desc->gpio_owner, owner);
130			goto out;
131		}
132
133		desc->mux_usecount++;
134		if (desc->mux_usecount > 1)
135			return 0;
136
137		desc->mux_owner = owner;
138	}
139
140	/* Let each pin increase references to this module */
141	if (!try_module_get(pctldev->owner)) {
142		dev_err(pctldev->dev,
143			"could not increase module refcount for pin %d\n",
144			pin);
145		status = -EINVAL;
146		goto out_free_pin;
147	}
148
149	/*
150	 * If there is no kind of request function for the pin we just assume
151	 * we got it by default and proceed.
152	 */
153	if (gpio_range && ops->gpio_request_enable)
154		/* This requests and enables a single GPIO pin */
155		status = ops->gpio_request_enable(pctldev, gpio_range, pin);
156	else if (ops->request)
157		status = ops->request(pctldev, pin);
158	else
159		status = 0;
160
161	if (status) {
162		dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
163		module_put(pctldev->owner);
164	}
165
166out_free_pin:
167	if (status) {
168		if (gpio_range) {
169			desc->gpio_owner = NULL;
170		} else {
171			desc->mux_usecount--;
172			if (!desc->mux_usecount)
173				desc->mux_owner = NULL;
174		}
175	}
176out:
177	if (status)
178		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
179			pin, owner, status);
180
181	return status;
182}
183
184/**
185 * pin_free() - release a single muxed in pin so something else can be muxed
186 * @pctldev: pin controller device handling this pin
187 * @pin: the pin to free
188 * @gpio_range: the range matching the GPIO pin if this is a request for a
189 *	single GPIO pin
190 *
191 * This function returns a pointer to the previous owner. This is used
192 * for callers that dynamically allocate an owner name so it can be freed
193 * once the pin is free. This is done for GPIO request functions.
194 */
195static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
196			    struct pinctrl_gpio_range *gpio_range)
197{
198	const struct pinmux_ops *ops = pctldev->desc->pmxops;
199	struct pin_desc *desc;
200	const char *owner;
201
202	desc = pin_desc_get(pctldev, pin);
203	if (desc == NULL) {
204		dev_err(pctldev->dev,
205			"pin is not registered so it cannot be freed\n");
206		return NULL;
207	}
208
209	if (!gpio_range) {
210		/*
211		 * A pin should not be freed more times than allocated.
212		 */
213		if (WARN_ON(!desc->mux_usecount))
214			return NULL;
215		desc->mux_usecount--;
216		if (desc->mux_usecount)
217			return NULL;
218	}
219
220	/*
221	 * If there is no kind of request function for the pin we just assume
222	 * we got it by default and proceed.
223	 */
224	if (gpio_range && ops->gpio_disable_free)
225		ops->gpio_disable_free(pctldev, gpio_range, pin);
226	else if (ops->free)
227		ops->free(pctldev, pin);
228
229	if (gpio_range) {
230		owner = desc->gpio_owner;
231		desc->gpio_owner = NULL;
232	} else {
233		owner = desc->mux_owner;
234		desc->mux_owner = NULL;
235		desc->mux_setting = NULL;
236	}
237
238	module_put(pctldev->owner);
239
240	return owner;
241}
242
243/**
244 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
245 * @pctldev: pin controller device affected
246 * @pin: the pin to mux in for GPIO
247 * @range: the applicable GPIO range
 
248 */
249int pinmux_request_gpio(struct pinctrl_dev *pctldev,
250			struct pinctrl_gpio_range *range,
251			unsigned pin, unsigned gpio)
252{
253	const char *owner;
254	int ret;
255
256	/* Conjure some name stating what chip and pin this is taken by */
257	owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
258	if (!owner)
259		return -EINVAL;
260
261	ret = pin_request(pctldev, pin, owner, range);
262	if (ret < 0)
263		kfree(owner);
264
265	return ret;
266}
267
268/**
269 * pinmux_free_gpio() - release a pin from GPIO muxing
270 * @pctldev: the pin controller device for the pin
271 * @pin: the affected currently GPIO-muxed in pin
272 * @range: applicable GPIO range
273 */
274void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
275		      struct pinctrl_gpio_range *range)
276{
277	const char *owner;
278
279	owner = pin_free(pctldev, pin, range);
280	kfree(owner);
281}
282
283/**
284 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
285 * @pctldev: the pin controller handling this pin
286 * @range: applicable GPIO range
287 * @pin: the affected GPIO pin in this controller
288 * @input: true if we set the pin as input, false for output
289 */
290int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
291			  struct pinctrl_gpio_range *range,
292			  unsigned pin, bool input)
293{
294	const struct pinmux_ops *ops;
295	int ret;
296
297	ops = pctldev->desc->pmxops;
298
299	if (ops->gpio_set_direction)
300		ret = ops->gpio_set_direction(pctldev, range, pin, input);
301	else
302		ret = 0;
303
304	return ret;
305}
306
307static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
308					const char *function)
309{
310	const struct pinmux_ops *ops = pctldev->desc->pmxops;
311	unsigned nfuncs = ops->get_functions_count(pctldev);
312	unsigned selector = 0;
313
314	/* See if this pctldev has this function */
315	while (selector < nfuncs) {
316		const char *fname = ops->get_function_name(pctldev, selector);
317
318		if (!strcmp(function, fname))
319			return selector;
320
321		selector++;
322	}
323
324	dev_err(pctldev->dev, "function '%s' not supported\n", function);
325	return -EINVAL;
326}
327
328int pinmux_map_to_setting(struct pinctrl_map const *map,
329			  struct pinctrl_setting *setting)
330{
331	struct pinctrl_dev *pctldev = setting->pctldev;
332	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
333	char const * const *groups;
334	unsigned num_groups;
335	int ret;
336	const char *group;
337
338	if (!pmxops) {
339		dev_err(pctldev->dev, "does not support mux function\n");
340		return -EINVAL;
341	}
342
343	ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
344	if (ret < 0) {
345		dev_err(pctldev->dev, "invalid function %s in map table\n",
346			map->data.mux.function);
347		return ret;
348	}
349	setting->data.mux.func = ret;
350
351	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
352					  &groups, &num_groups);
353	if (ret < 0) {
354		dev_err(pctldev->dev, "can't query groups for function %s\n",
355			map->data.mux.function);
356		return ret;
357	}
358	if (!num_groups) {
359		dev_err(pctldev->dev,
360			"function %s can't be selected on any group\n",
361			map->data.mux.function);
362		return -EINVAL;
363	}
364	if (map->data.mux.group) {
365		group = map->data.mux.group;
366		ret = match_string(groups, num_groups, group);
367		if (ret < 0) {
368			dev_err(pctldev->dev,
369				"invalid group \"%s\" for function \"%s\"\n",
370				group, map->data.mux.function);
371			return ret;
372		}
373	} else {
374		group = groups[0];
375	}
376
377	ret = pinctrl_get_group_selector(pctldev, group);
378	if (ret < 0) {
379		dev_err(pctldev->dev, "invalid group %s in map table\n",
380			map->data.mux.group);
381		return ret;
382	}
383	setting->data.mux.group = ret;
384
385	return 0;
386}
387
388void pinmux_free_setting(struct pinctrl_setting const *setting)
389{
390	/* This function is currently unused */
391}
392
393int pinmux_enable_setting(struct pinctrl_setting const *setting)
394{
395	struct pinctrl_dev *pctldev = setting->pctldev;
396	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
397	const struct pinmux_ops *ops = pctldev->desc->pmxops;
398	int ret = 0;
399	const unsigned *pins = NULL;
400	unsigned num_pins = 0;
401	int i;
402	struct pin_desc *desc;
403
404	if (pctlops->get_group_pins)
405		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
406					      &pins, &num_pins);
407
408	if (ret) {
409		const char *gname;
410
411		/* errors only affect debug data, so just warn */
412		gname = pctlops->get_group_name(pctldev,
413						setting->data.mux.group);
414		dev_warn(pctldev->dev,
415			 "could not get pins for group %s\n",
416			 gname);
417		num_pins = 0;
418	}
419
420	/* Try to allocate all pins in this group, one by one */
421	for (i = 0; i < num_pins; i++) {
422		ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
423		if (ret) {
424			const char *gname;
425			const char *pname;
426
427			desc = pin_desc_get(pctldev, pins[i]);
428			pname = desc ? desc->name : "non-existing";
429			gname = pctlops->get_group_name(pctldev,
430						setting->data.mux.group);
431			dev_err(pctldev->dev,
432				"could not request pin %d (%s) from group %s "
433				" on device %s\n",
434				pins[i], pname, gname,
435				pinctrl_dev_get_name(pctldev));
436			goto err_pin_request;
437		}
438	}
439
440	/* Now that we have acquired the pins, encode the mux setting */
441	for (i = 0; i < num_pins; i++) {
442		desc = pin_desc_get(pctldev, pins[i]);
443		if (desc == NULL) {
444			dev_warn(pctldev->dev,
445				 "could not get pin desc for pin %d\n",
446				 pins[i]);
447			continue;
448		}
449		desc->mux_setting = &(setting->data.mux);
450	}
451
452	ret = ops->set_mux(pctldev, setting->data.mux.func,
453			   setting->data.mux.group);
454
455	if (ret)
456		goto err_set_mux;
457
458	return 0;
459
460err_set_mux:
461	for (i = 0; i < num_pins; i++) {
462		desc = pin_desc_get(pctldev, pins[i]);
463		if (desc)
464			desc->mux_setting = NULL;
465	}
466err_pin_request:
467	/* On error release all taken pins */
468	while (--i >= 0)
469		pin_free(pctldev, pins[i], NULL);
470
471	return ret;
472}
473
474void pinmux_disable_setting(struct pinctrl_setting const *setting)
475{
476	struct pinctrl_dev *pctldev = setting->pctldev;
477	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
478	int ret = 0;
479	const unsigned *pins = NULL;
480	unsigned num_pins = 0;
481	int i;
482	struct pin_desc *desc;
483
484	if (pctlops->get_group_pins)
485		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
486					      &pins, &num_pins);
487	if (ret) {
488		const char *gname;
489
490		/* errors only affect debug data, so just warn */
491		gname = pctlops->get_group_name(pctldev,
492						setting->data.mux.group);
493		dev_warn(pctldev->dev,
494			 "could not get pins for group %s\n",
495			 gname);
496		num_pins = 0;
497	}
498
499	/* Flag the descs that no setting is active */
500	for (i = 0; i < num_pins; i++) {
501		desc = pin_desc_get(pctldev, pins[i]);
502		if (desc == NULL) {
503			dev_warn(pctldev->dev,
504				 "could not get pin desc for pin %d\n",
505				 pins[i]);
506			continue;
507		}
508		if (desc->mux_setting == &(setting->data.mux)) {
509			desc->mux_setting = NULL;
510			/* And release the pin */
511			pin_free(pctldev, pins[i], NULL);
512		} else {
513			const char *gname;
514
515			gname = pctlops->get_group_name(pctldev,
516						setting->data.mux.group);
517			dev_warn(pctldev->dev,
518				 "not freeing pin %d (%s) as part of "
519				 "deactivating group %s - it is already "
520				 "used for some other setting",
521				 pins[i], desc->name, gname);
522		}
523	}
524}
525
526#ifdef CONFIG_DEBUG_FS
527
528/* Called from pincontrol core */
529static int pinmux_functions_show(struct seq_file *s, void *what)
530{
531	struct pinctrl_dev *pctldev = s->private;
532	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
533	unsigned nfuncs;
534	unsigned func_selector = 0;
535
536	if (!pmxops)
537		return 0;
538
539	mutex_lock(&pctldev->mutex);
540	nfuncs = pmxops->get_functions_count(pctldev);
541	while (func_selector < nfuncs) {
542		const char *func = pmxops->get_function_name(pctldev,
543							  func_selector);
544		const char * const *groups;
545		unsigned num_groups;
546		int ret;
547		int i;
548
549		ret = pmxops->get_function_groups(pctldev, func_selector,
550						  &groups, &num_groups);
551		if (ret) {
552			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
553				   func);
554			func_selector++;
555			continue;
556		}
557
558		seq_printf(s, "function: %s, groups = [ ", func);
559		for (i = 0; i < num_groups; i++)
560			seq_printf(s, "%s ", groups[i]);
561		seq_puts(s, "]\n");
562
563		func_selector++;
564	}
565
566	mutex_unlock(&pctldev->mutex);
567
568	return 0;
569}
570
571static int pinmux_pins_show(struct seq_file *s, void *what)
572{
573	struct pinctrl_dev *pctldev = s->private;
574	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
575	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
576	unsigned i, pin;
577
578	if (!pmxops)
579		return 0;
580
581	seq_puts(s, "Pinmux settings per pin\n");
582	if (pmxops->strict)
583		seq_puts(s,
584		 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
585	else
586		seq_puts(s,
587		"Format: pin (name): mux_owner gpio_owner hog?\n");
588
589	mutex_lock(&pctldev->mutex);
590
591	/* The pin number can be retrived from the pin controller descriptor */
592	for (i = 0; i < pctldev->desc->npins; i++) {
593		struct pin_desc *desc;
594		bool is_hog = false;
595
596		pin = pctldev->desc->pins[i].number;
597		desc = pin_desc_get(pctldev, pin);
598		/* Skip if we cannot search the pin */
599		if (desc == NULL)
600			continue;
601
602		if (desc->mux_owner &&
603		    !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
604			is_hog = true;
605
606		if (pmxops->strict) {
607			if (desc->mux_owner)
608				seq_printf(s, "pin %d (%s): device %s%s",
609					   pin,
610					   desc->name ? desc->name : "unnamed",
611					   desc->mux_owner,
612					   is_hog ? " (HOG)" : "");
613			else if (desc->gpio_owner)
614				seq_printf(s, "pin %d (%s): GPIO %s",
615					   pin,
616					   desc->name ? desc->name : "unnamed",
617					   desc->gpio_owner);
618			else
619				seq_printf(s, "pin %d (%s): UNCLAIMED",
620					   pin,
621					   desc->name ? desc->name : "unnamed");
622		} else {
623			/* For non-strict controllers */
624			seq_printf(s, "pin %d (%s): %s %s%s", pin,
625				   desc->name ? desc->name : "unnamed",
626				   desc->mux_owner ? desc->mux_owner
627				   : "(MUX UNCLAIMED)",
628				   desc->gpio_owner ? desc->gpio_owner
629				   : "(GPIO UNCLAIMED)",
630				   is_hog ? " (HOG)" : "");
631		}
632
633		/* If mux: print function+group claiming the pin */
634		if (desc->mux_setting)
635			seq_printf(s, " function %s group %s\n",
636				   pmxops->get_function_name(pctldev,
637					desc->mux_setting->func),
638				   pctlops->get_group_name(pctldev,
639					desc->mux_setting->group));
640		else
641			seq_printf(s, "\n");
642	}
643
644	mutex_unlock(&pctldev->mutex);
645
646	return 0;
647}
648
649void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
650{
651	seq_printf(s, "group %s\nfunction %s\n",
652		map->data.mux.group ? map->data.mux.group : "(default)",
653		map->data.mux.function);
654}
655
656void pinmux_show_setting(struct seq_file *s,
657			 struct pinctrl_setting const *setting)
658{
659	struct pinctrl_dev *pctldev = setting->pctldev;
660	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
661	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
662
663	seq_printf(s, "group: %s (%u) function: %s (%u)\n",
664		   pctlops->get_group_name(pctldev, setting->data.mux.group),
665		   setting->data.mux.group,
666		   pmxops->get_function_name(pctldev, setting->data.mux.func),
667		   setting->data.mux.func);
668}
669
670static int pinmux_functions_open(struct inode *inode, struct file *file)
 
 
 
 
671{
672	return single_open(file, pinmux_functions_show, inode->i_private);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673}
674
675static int pinmux_pins_open(struct inode *inode, struct file *file)
676{
677	return single_open(file, pinmux_pins_show, inode->i_private);
678}
679
680static const struct file_operations pinmux_functions_ops = {
681	.open		= pinmux_functions_open,
682	.read		= seq_read,
683	.llseek		= seq_lseek,
684	.release	= single_release,
685};
686
687static const struct file_operations pinmux_pins_ops = {
688	.open		= pinmux_pins_open,
689	.read		= seq_read,
690	.llseek		= seq_lseek,
691	.release	= single_release,
692};
693
694void pinmux_init_device_debugfs(struct dentry *devroot,
695			 struct pinctrl_dev *pctldev)
696{
697	debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
698			    devroot, pctldev, &pinmux_functions_ops);
699	debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
700			    devroot, pctldev, &pinmux_pins_ops);
 
 
701}
702
703#endif /* CONFIG_DEBUG_FS */