• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Core driver for the pin config portions of the pin control subsystem
3  *
4  * Copyright (C) 2011 ST-Ericsson SA
5  * Written on behalf of Linaro for ST-Ericsson
6  *
7  * Author: Linus Walleij <linus.walleij@linaro.org>
8  *
9  * License terms: GNU General Public License (GPL) version 2
10  */
11 #define pr_fmt(fmt) "pinconfig core: " fmt
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include "core.h"
25 #include "pinconf.h"
26 
pinconf_check_ops(struct pinctrl_dev * pctldev)27 int pinconf_check_ops(struct pinctrl_dev *pctldev)
28 {
29 	const struct pinconf_ops *ops = pctldev->desc->confops;
30 
31 	/* We have to be able to config the pins in SOME way */
32 	if (!ops->pin_config_set && !ops->pin_config_group_set) {
33 		dev_err(pctldev->dev,
34 			"pinconf has to be able to set a pins config\n");
35 		return -EINVAL;
36 	}
37 	return 0;
38 }
39 
pinconf_validate_map(struct pinctrl_map const * map,int i)40 int pinconf_validate_map(struct pinctrl_map const *map, int i)
41 {
42 	if (!map->data.configs.group_or_pin) {
43 		pr_err("failed to register map %s (%d): no group/pin given\n",
44 		       map->name, i);
45 		return -EINVAL;
46 	}
47 
48 	if (!map->data.configs.num_configs ||
49 			!map->data.configs.configs) {
50 		pr_err("failed to register map %s (%d): no configs given\n",
51 		       map->name, i);
52 		return -EINVAL;
53 	}
54 
55 	return 0;
56 }
57 
pin_config_get_for_pin(struct pinctrl_dev * pctldev,unsigned pin,unsigned long * config)58 int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
59 			   unsigned long *config)
60 {
61 	const struct pinconf_ops *ops = pctldev->desc->confops;
62 
63 	if (!ops || !ops->pin_config_get) {
64 		dev_dbg(pctldev->dev,
65 			"cannot get pin configuration, .pin_config_get missing in driver\n");
66 		return -ENOTSUPP;
67 	}
68 
69 	return ops->pin_config_get(pctldev, pin, config);
70 }
71 
pin_config_group_get(const char * dev_name,const char * pin_group,unsigned long * config)72 int pin_config_group_get(const char *dev_name, const char *pin_group,
73 			 unsigned long *config)
74 {
75 	struct pinctrl_dev *pctldev;
76 	const struct pinconf_ops *ops;
77 	int selector, ret;
78 
79 	pctldev = get_pinctrl_dev_from_devname(dev_name);
80 	if (!pctldev) {
81 		ret = -EINVAL;
82 		return ret;
83 	}
84 
85 	mutex_lock(&pctldev->mutex);
86 
87 	ops = pctldev->desc->confops;
88 
89 	if (!ops || !ops->pin_config_group_get) {
90 		dev_dbg(pctldev->dev, "cannot get configuration for pin "
91 			"group, missing group config get function in "
92 			"driver\n");
93 		ret = -ENOTSUPP;
94 		goto unlock;
95 	}
96 
97 	selector = pinctrl_get_group_selector(pctldev, pin_group);
98 	if (selector < 0) {
99 		ret = selector;
100 		goto unlock;
101 	}
102 
103 	ret = ops->pin_config_group_get(pctldev, selector, config);
104 
105 unlock:
106 	mutex_unlock(&pctldev->mutex);
107 	return ret;
108 }
109 
pinconf_map_to_setting(struct pinctrl_map const * map,struct pinctrl_setting * setting)110 int pinconf_map_to_setting(struct pinctrl_map const *map,
111 			  struct pinctrl_setting *setting)
112 {
113 	struct pinctrl_dev *pctldev = setting->pctldev;
114 	int pin;
115 
116 	switch (setting->type) {
117 	case PIN_MAP_TYPE_CONFIGS_PIN:
118 		pin = pin_get_from_name(pctldev,
119 					map->data.configs.group_or_pin);
120 		if (pin < 0) {
121 			dev_err(pctldev->dev, "could not map pin config for \"%s\"",
122 				map->data.configs.group_or_pin);
123 			return pin;
124 		}
125 		setting->data.configs.group_or_pin = pin;
126 		break;
127 	case PIN_MAP_TYPE_CONFIGS_GROUP:
128 		pin = pinctrl_get_group_selector(pctldev,
129 					 map->data.configs.group_or_pin);
130 		if (pin < 0) {
131 			dev_err(pctldev->dev, "could not map group config for \"%s\"",
132 				map->data.configs.group_or_pin);
133 			return pin;
134 		}
135 		setting->data.configs.group_or_pin = pin;
136 		break;
137 	default:
138 		return -EINVAL;
139 	}
140 
141 	setting->data.configs.num_configs = map->data.configs.num_configs;
142 	setting->data.configs.configs = map->data.configs.configs;
143 
144 	return 0;
145 }
146 
pinconf_free_setting(struct pinctrl_setting const * setting)147 void pinconf_free_setting(struct pinctrl_setting const *setting)
148 {
149 }
150 
pinconf_apply_setting(struct pinctrl_setting const * setting)151 int pinconf_apply_setting(struct pinctrl_setting const *setting)
152 {
153 	struct pinctrl_dev *pctldev = setting->pctldev;
154 	const struct pinconf_ops *ops = pctldev->desc->confops;
155 	int ret;
156 
157 	if (!ops) {
158 		dev_err(pctldev->dev, "missing confops\n");
159 		return -EINVAL;
160 	}
161 
162 	switch (setting->type) {
163 	case PIN_MAP_TYPE_CONFIGS_PIN:
164 		if (!ops->pin_config_set) {
165 			dev_err(pctldev->dev, "missing pin_config_set op\n");
166 			return -EINVAL;
167 		}
168 		ret = ops->pin_config_set(pctldev,
169 				setting->data.configs.group_or_pin,
170 				setting->data.configs.configs,
171 				setting->data.configs.num_configs);
172 		if (ret < 0) {
173 			dev_err(pctldev->dev,
174 				"pin_config_set op failed for pin %d\n",
175 				setting->data.configs.group_or_pin);
176 			return ret;
177 		}
178 		break;
179 	case PIN_MAP_TYPE_CONFIGS_GROUP:
180 		if (!ops->pin_config_group_set) {
181 			dev_err(pctldev->dev,
182 				"missing pin_config_group_set op\n");
183 			return -EINVAL;
184 		}
185 		ret = ops->pin_config_group_set(pctldev,
186 				setting->data.configs.group_or_pin,
187 				setting->data.configs.configs,
188 				setting->data.configs.num_configs);
189 		if (ret < 0) {
190 			dev_err(pctldev->dev,
191 				"pin_config_group_set op failed for group %d\n",
192 				setting->data.configs.group_or_pin);
193 			return ret;
194 		}
195 		break;
196 	default:
197 		return -EINVAL;
198 	}
199 
200 	return 0;
201 }
202 
203 #ifdef CONFIG_DEBUG_FS
204 
pinconf_show_config(struct seq_file * s,struct pinctrl_dev * pctldev,unsigned long * configs,unsigned num_configs)205 static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
206 		      unsigned long *configs, unsigned num_configs)
207 {
208 	const struct pinconf_ops *confops;
209 	int i;
210 
211 	if (pctldev)
212 		confops = pctldev->desc->confops;
213 	else
214 		confops = NULL;
215 
216 	for (i = 0; i < num_configs; i++) {
217 		seq_puts(s, "config ");
218 		if (confops && confops->pin_config_config_dbg_show)
219 			confops->pin_config_config_dbg_show(pctldev, s,
220 							    configs[i]);
221 		else
222 			seq_printf(s, "%08lx", configs[i]);
223 		seq_puts(s, "\n");
224 	}
225 }
226 
pinconf_show_map(struct seq_file * s,struct pinctrl_map const * map)227 void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
228 {
229 	struct pinctrl_dev *pctldev;
230 
231 	pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
232 
233 	switch (map->type) {
234 	case PIN_MAP_TYPE_CONFIGS_PIN:
235 		seq_printf(s, "pin ");
236 		break;
237 	case PIN_MAP_TYPE_CONFIGS_GROUP:
238 		seq_printf(s, "group ");
239 		break;
240 	default:
241 		break;
242 	}
243 
244 	seq_printf(s, "%s\n", map->data.configs.group_or_pin);
245 
246 	pinconf_show_config(s, pctldev, map->data.configs.configs,
247 			    map->data.configs.num_configs);
248 }
249 
pinconf_show_setting(struct seq_file * s,struct pinctrl_setting const * setting)250 void pinconf_show_setting(struct seq_file *s,
251 			  struct pinctrl_setting const *setting)
252 {
253 	struct pinctrl_dev *pctldev = setting->pctldev;
254 	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
255 	struct pin_desc *desc;
256 
257 	switch (setting->type) {
258 	case PIN_MAP_TYPE_CONFIGS_PIN:
259 		desc = pin_desc_get(setting->pctldev,
260 				    setting->data.configs.group_or_pin);
261 		seq_printf(s, "pin %s (%d)", desc->name,
262 			   setting->data.configs.group_or_pin);
263 		break;
264 	case PIN_MAP_TYPE_CONFIGS_GROUP:
265 		seq_printf(s, "group %s (%d)",
266 			   pctlops->get_group_name(pctldev,
267 					setting->data.configs.group_or_pin),
268 			   setting->data.configs.group_or_pin);
269 		break;
270 	default:
271 		break;
272 	}
273 
274 	/*
275 	 * FIXME: We should really get the pin controler to dump the config
276 	 * values, so they can be decoded to something meaningful.
277 	 */
278 	pinconf_show_config(s, pctldev, setting->data.configs.configs,
279 			    setting->data.configs.num_configs);
280 }
281 
pinconf_dump_pin(struct pinctrl_dev * pctldev,struct seq_file * s,int pin)282 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
283 			     struct seq_file *s, int pin)
284 {
285 	const struct pinconf_ops *ops = pctldev->desc->confops;
286 
287 	/* no-op when not using generic pin config */
288 	pinconf_generic_dump_pins(pctldev, s, NULL, pin);
289 	if (ops && ops->pin_config_dbg_show)
290 		ops->pin_config_dbg_show(pctldev, s, pin);
291 }
292 
pinconf_pins_show(struct seq_file * s,void * what)293 static int pinconf_pins_show(struct seq_file *s, void *what)
294 {
295 	struct pinctrl_dev *pctldev = s->private;
296 	unsigned i, pin;
297 
298 	seq_puts(s, "Pin config settings per pin\n");
299 	seq_puts(s, "Format: pin (name): configs\n");
300 
301 	mutex_lock(&pctldev->mutex);
302 
303 	/* The pin number can be retrived from the pin controller descriptor */
304 	for (i = 0; i < pctldev->desc->npins; i++) {
305 		struct pin_desc *desc;
306 
307 		pin = pctldev->desc->pins[i].number;
308 		desc = pin_desc_get(pctldev, pin);
309 		/* Skip if we cannot search the pin */
310 		if (desc == NULL)
311 			continue;
312 
313 		seq_printf(s, "pin %d (%s): ", pin, desc->name);
314 
315 		pinconf_dump_pin(pctldev, s, pin);
316 
317 		seq_printf(s, "\n");
318 	}
319 
320 	mutex_unlock(&pctldev->mutex);
321 
322 	return 0;
323 }
324 
pinconf_dump_group(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned selector,const char * gname)325 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
326 			       struct seq_file *s, unsigned selector,
327 			       const char *gname)
328 {
329 	const struct pinconf_ops *ops = pctldev->desc->confops;
330 
331 	/* no-op when not using generic pin config */
332 	pinconf_generic_dump_pins(pctldev, s, gname, 0);
333 	if (ops && ops->pin_config_group_dbg_show)
334 		ops->pin_config_group_dbg_show(pctldev, s, selector);
335 }
336 
pinconf_groups_show(struct seq_file * s,void * what)337 static int pinconf_groups_show(struct seq_file *s, void *what)
338 {
339 	struct pinctrl_dev *pctldev = s->private;
340 	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
341 	unsigned ngroups = pctlops->get_groups_count(pctldev);
342 	unsigned selector = 0;
343 
344 	seq_puts(s, "Pin config settings per pin group\n");
345 	seq_puts(s, "Format: group (name): configs\n");
346 
347 	while (selector < ngroups) {
348 		const char *gname = pctlops->get_group_name(pctldev, selector);
349 
350 		seq_printf(s, "%u (%s): ", selector, gname);
351 		pinconf_dump_group(pctldev, s, selector, gname);
352 		seq_printf(s, "\n");
353 
354 		selector++;
355 	}
356 
357 	return 0;
358 }
359 
pinconf_pins_open(struct inode * inode,struct file * file)360 static int pinconf_pins_open(struct inode *inode, struct file *file)
361 {
362 	return single_open(file, pinconf_pins_show, inode->i_private);
363 }
364 
pinconf_groups_open(struct inode * inode,struct file * file)365 static int pinconf_groups_open(struct inode *inode, struct file *file)
366 {
367 	return single_open(file, pinconf_groups_show, inode->i_private);
368 }
369 
370 static const struct file_operations pinconf_pins_ops = {
371 	.open		= pinconf_pins_open,
372 	.read		= seq_read,
373 	.llseek		= seq_lseek,
374 	.release	= single_release,
375 };
376 
377 static const struct file_operations pinconf_groups_ops = {
378 	.open		= pinconf_groups_open,
379 	.read		= seq_read,
380 	.llseek		= seq_lseek,
381 	.release	= single_release,
382 };
383 
384 #define MAX_NAME_LEN 15
385 
386 struct dbg_cfg {
387 	enum pinctrl_map_type map_type;
388 	char dev_name[MAX_NAME_LEN+1];
389 	char state_name[MAX_NAME_LEN+1];
390 	char pin_name[MAX_NAME_LEN+1];
391 };
392 
393 /*
394  * Goal is to keep this structure as global in order to simply read the
395  * pinconf-config file after a write to check config is as expected
396  */
397 static struct dbg_cfg pinconf_dbg_conf;
398 
399 /**
400  * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
401  * map, of the dev/pin/state that was last written to pinconf-config file.
402  * @s: string filled in  with config description
403  * @d: not used
404  */
pinconf_dbg_config_print(struct seq_file * s,void * d)405 static int pinconf_dbg_config_print(struct seq_file *s, void *d)
406 {
407 	struct pinctrl_maps *maps_node;
408 	const struct pinctrl_map *map;
409 	const struct pinctrl_map *found = NULL;
410 	struct pinctrl_dev *pctldev;
411 	struct dbg_cfg *dbg = &pinconf_dbg_conf;
412 	int i;
413 
414 	mutex_lock(&pinctrl_maps_mutex);
415 
416 	/* Parse the pinctrl map and look for the elected pin/state */
417 	for_each_maps(maps_node, i, map) {
418 		if (map->type != dbg->map_type)
419 			continue;
420 		if (strcmp(map->dev_name, dbg->dev_name))
421 			continue;
422 		if (strcmp(map->name, dbg->state_name))
423 			continue;
424 
425 		if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
426 			/* We found the right pin */
427 			found = map;
428 			break;
429 		}
430 	}
431 
432 	if (!found) {
433 		seq_printf(s, "No config found for dev/state/pin, expected:\n");
434 		seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
435 		seq_printf(s, "Searched state:%s\n", dbg->state_name);
436 		seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
437 		seq_printf(s, "Use: modify config_pin <devname> "\
438 				"<state> <pinname> <value>\n");
439 		goto exit;
440 	}
441 
442 	pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
443 	seq_printf(s, "Dev %s has config of %s in state %s:\n",
444 		   dbg->dev_name, dbg->pin_name, dbg->state_name);
445 	pinconf_show_config(s, pctldev, found->data.configs.configs,
446 			    found->data.configs.num_configs);
447 
448 exit:
449 	mutex_unlock(&pinctrl_maps_mutex);
450 
451 	return 0;
452 }
453 
454 /**
455  * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
456  * map, of a dev/pin/state entry based on user entries to pinconf-config
457  * @user_buf: contains the modification request with expected format:
458  *     modify <config> <devicename> <state> <name> <newvalue>
459  * modify is literal string, alternatives like add/delete not supported yet
460  * <config> is the configuration to be changed. Supported configs are
461  *     "config_pin" or "config_group", alternatives like config_mux are not
462  *     supported yet.
463  * <devicename> <state> <name> are values that should match the pinctrl-maps
464  * <newvalue> reflects the new config and is driver dependant
465  */
pinconf_dbg_config_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)466 static ssize_t pinconf_dbg_config_write(struct file *file,
467 	const char __user *user_buf, size_t count, loff_t *ppos)
468 {
469 	struct pinctrl_maps *maps_node;
470 	const struct pinctrl_map *map;
471 	const struct pinctrl_map *found = NULL;
472 	struct pinctrl_dev *pctldev;
473 	const struct pinconf_ops *confops = NULL;
474 	struct dbg_cfg *dbg = &pinconf_dbg_conf;
475 	const struct pinctrl_map_configs *configs;
476 	char config[MAX_NAME_LEN+1];
477 	char buf[128];
478 	char *b = &buf[0];
479 	int buf_size;
480 	char *token;
481 	int i;
482 
483 	/* Get userspace string and assure termination */
484 	buf_size = min(count, sizeof(buf) - 1);
485 	if (copy_from_user(buf, user_buf, buf_size))
486 		return -EFAULT;
487 	buf[buf_size] = 0;
488 
489 	/*
490 	 * need to parse entry and extract parameters:
491 	 * modify configs_pin devicename state pinname newvalue
492 	 */
493 
494 	/* Get arg: 'modify' */
495 	token = strsep(&b, " ");
496 	if (!token)
497 		return -EINVAL;
498 	if (strcmp(token, "modify"))
499 		return -EINVAL;
500 
501 	/*
502 	 * Get arg type: "config_pin" and "config_group"
503 	 *                types are supported so far
504 	 */
505 	token = strsep(&b, " ");
506 	if (!token)
507 		return -EINVAL;
508 	if (!strcmp(token, "config_pin"))
509 		dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
510 	else if (!strcmp(token, "config_group"))
511 		dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
512 	else
513 		return -EINVAL;
514 
515 	/* get arg 'device_name' */
516 	token = strsep(&b, " ");
517 	if (token == NULL)
518 		return -EINVAL;
519 	if (strlen(token) >= MAX_NAME_LEN)
520 		return -EINVAL;
521 	strncpy(dbg->dev_name, token, MAX_NAME_LEN);
522 
523 	/* get arg 'state_name' */
524 	token = strsep(&b, " ");
525 	if (token == NULL)
526 		return -EINVAL;
527 	if (strlen(token) >= MAX_NAME_LEN)
528 		return -EINVAL;
529 	strncpy(dbg->state_name, token, MAX_NAME_LEN);
530 
531 	/* get arg 'pin_name' */
532 	token = strsep(&b, " ");
533 	if (token == NULL)
534 		return -EINVAL;
535 	if (strlen(token) >= MAX_NAME_LEN)
536 		return -EINVAL;
537 	strncpy(dbg->pin_name, token, MAX_NAME_LEN);
538 
539 	/* get new_value of config' */
540 	token = strsep(&b, " ");
541 	if (token == NULL)
542 		return -EINVAL;
543 	if (strlen(token) >= MAX_NAME_LEN)
544 		return -EINVAL;
545 	strncpy(config, token, MAX_NAME_LEN);
546 
547 	mutex_lock(&pinctrl_maps_mutex);
548 
549 	/* Parse the pinctrl map and look for the selected dev/state/pin */
550 	for_each_maps(maps_node, i, map) {
551 		if (strcmp(map->dev_name, dbg->dev_name))
552 			continue;
553 		if (map->type != dbg->map_type)
554 			continue;
555 		if (strcmp(map->name, dbg->state_name))
556 			continue;
557 
558 		/*  we found the right pin / state, so overwrite config */
559 		if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
560 			found = map;
561 			break;
562 		}
563 	}
564 
565 	if (!found) {
566 		count = -EINVAL;
567 		goto exit;
568 	}
569 
570 	pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
571 	if (pctldev)
572 		confops = pctldev->desc->confops;
573 
574 	if (confops && confops->pin_config_dbg_parse_modify) {
575 		configs = &found->data.configs;
576 		for (i = 0; i < configs->num_configs; i++) {
577 			confops->pin_config_dbg_parse_modify(pctldev,
578 						     config,
579 						     &configs->configs[i]);
580 		}
581 	}
582 
583 exit:
584 	mutex_unlock(&pinctrl_maps_mutex);
585 
586 	return count;
587 }
588 
pinconf_dbg_config_open(struct inode * inode,struct file * file)589 static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
590 {
591 	return single_open(file, pinconf_dbg_config_print, inode->i_private);
592 }
593 
594 static const struct file_operations pinconf_dbg_pinconfig_fops = {
595 	.open = pinconf_dbg_config_open,
596 	.write = pinconf_dbg_config_write,
597 	.read = seq_read,
598 	.llseek = seq_lseek,
599 	.release = single_release,
600 	.owner = THIS_MODULE,
601 };
602 
pinconf_init_device_debugfs(struct dentry * devroot,struct pinctrl_dev * pctldev)603 void pinconf_init_device_debugfs(struct dentry *devroot,
604 			 struct pinctrl_dev *pctldev)
605 {
606 	debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
607 			    devroot, pctldev, &pinconf_pins_ops);
608 	debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
609 			    devroot, pctldev, &pinconf_groups_ops);
610 	debugfs_create_file("pinconf-config",  (S_IRUGO | S_IWUSR | S_IWGRP),
611 			    devroot, pctldev, &pinconf_dbg_pinconfig_fops);
612 }
613 
614 #endif
615