1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) "pinctrl core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/export.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/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
29#include "core.h"
30#include "pinmux.h"
31
32
33static DEFINE_MUTEX(pinctrldev_list_mutex);
34static LIST_HEAD(pinctrldev_list);
35
36static void pinctrl_dev_release(struct device *dev)
37{
38 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
39 kfree(pctldev);
40}
41
42const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
43{
44
45 return pctldev->desc->name;
46}
47EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
48
49void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
50{
51 return pctldev->driver_data;
52}
53EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
54
55
56
57
58
59
60
61
62
63
64
65struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
66 const char *devname)
67{
68 struct pinctrl_dev *pctldev = NULL;
69 bool found = false;
70
71 mutex_lock(&pinctrldev_list_mutex);
72 list_for_each_entry(pctldev, &pinctrldev_list, node) {
73 if (dev && &pctldev->dev == dev) {
74
75 found = true;
76 break;
77 }
78
79 if (devname &&
80 !strcmp(dev_name(&pctldev->dev), devname)) {
81
82 found = true;
83 break;
84 }
85 }
86 mutex_unlock(&pinctrldev_list_mutex);
87
88 return found ? pctldev : NULL;
89}
90
91struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
92{
93 struct pin_desc *pindesc;
94 unsigned long flags;
95
96 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
97 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
98 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
99
100 return pindesc;
101}
102
103
104
105
106
107
108
109
110
111bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
112{
113 struct pin_desc *pindesc;
114
115 if (pin < 0)
116 return false;
117
118 pindesc = pin_desc_get(pctldev, pin);
119 if (pindesc == NULL)
120 return false;
121
122 return true;
123}
124EXPORT_SYMBOL_GPL(pin_is_valid);
125
126
127static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
128 const struct pinctrl_pin_desc *pins,
129 unsigned num_pins)
130{
131 int i;
132
133 spin_lock(&pctldev->pin_desc_tree_lock);
134 for (i = 0; i < num_pins; i++) {
135 struct pin_desc *pindesc;
136
137 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
138 pins[i].number);
139 if (pindesc != NULL) {
140 radix_tree_delete(&pctldev->pin_desc_tree,
141 pins[i].number);
142 }
143 kfree(pindesc);
144 }
145 spin_unlock(&pctldev->pin_desc_tree_lock);
146}
147
148static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
149 unsigned number, const char *name)
150{
151 struct pin_desc *pindesc;
152
153 pindesc = pin_desc_get(pctldev, number);
154 if (pindesc != NULL) {
155 pr_err("pin %d already registered on %s\n", number,
156 pctldev->desc->name);
157 return -EINVAL;
158 }
159
160 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
161 if (pindesc == NULL)
162 return -ENOMEM;
163 spin_lock_init(&pindesc->lock);
164
165
166 pindesc->pctldev = pctldev;
167
168
169 pindesc->name = name;
170
171 spin_lock(&pctldev->pin_desc_tree_lock);
172 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
173 spin_unlock(&pctldev->pin_desc_tree_lock);
174 pr_debug("registered pin %d (%s) on %s\n",
175 number, name ? name : "(unnamed)", pctldev->desc->name);
176 return 0;
177}
178
179static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
180 struct pinctrl_pin_desc const *pins,
181 unsigned num_descs)
182{
183 unsigned i;
184 int ret = 0;
185
186 for (i = 0; i < num_descs; i++) {
187 ret = pinctrl_register_one_pin(pctldev,
188 pins[i].number, pins[i].name);
189 if (ret)
190 return ret;
191 }
192
193 return 0;
194}
195
196
197
198
199
200
201
202
203
204static struct pinctrl_gpio_range *
205pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
206{
207 struct pinctrl_gpio_range *range = NULL;
208
209
210 mutex_lock(&pctldev->gpio_ranges_lock);
211 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
212
213 if (gpio >= range->base &&
214 gpio < range->base + range->npins) {
215 mutex_unlock(&pctldev->gpio_ranges_lock);
216 return range;
217 }
218 }
219 mutex_unlock(&pctldev->gpio_ranges_lock);
220
221 return NULL;
222}
223
224
225
226
227
228
229
230
231
232
233
234int pinctrl_get_device_gpio_range(unsigned gpio,
235 struct pinctrl_dev **outdev,
236 struct pinctrl_gpio_range **outrange)
237{
238 struct pinctrl_dev *pctldev = NULL;
239
240
241 mutex_lock(&pinctrldev_list_mutex);
242 list_for_each_entry(pctldev, &pinctrldev_list, node) {
243 struct pinctrl_gpio_range *range;
244
245 range = pinctrl_match_gpio_range(pctldev, gpio);
246 if (range != NULL) {
247 *outdev = pctldev;
248 *outrange = range;
249 mutex_unlock(&pinctrldev_list_mutex);
250 return 0;
251 }
252 }
253 mutex_unlock(&pinctrldev_list_mutex);
254
255 return -EINVAL;
256}
257
258
259
260
261
262
263
264
265
266void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
267 struct pinctrl_gpio_range *range)
268{
269 mutex_lock(&pctldev->gpio_ranges_lock);
270 list_add(&range->node, &pctldev->gpio_ranges);
271 mutex_unlock(&pctldev->gpio_ranges_lock);
272}
273
274
275
276
277
278
279void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
280 struct pinctrl_gpio_range *range)
281{
282 mutex_lock(&pctldev->gpio_ranges_lock);
283 list_del(&range->node);
284 mutex_unlock(&pctldev->gpio_ranges_lock);
285}
286
287#ifdef CONFIG_DEBUG_FS
288
289static int pinctrl_pins_show(struct seq_file *s, void *what)
290{
291 struct pinctrl_dev *pctldev = s->private;
292 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
293 unsigned pin;
294
295 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
296 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
297
298
299 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
300 struct pin_desc *desc;
301
302 desc = pin_desc_get(pctldev, pin);
303
304 if (desc == NULL)
305 continue;
306
307 seq_printf(s, "pin %d (%s) ", pin,
308 desc->name ? desc->name : "unnamed");
309
310
311 if (ops->pin_dbg_show)
312 ops->pin_dbg_show(pctldev, s, pin);
313
314 seq_puts(s, "\n");
315 }
316
317 return 0;
318}
319
320static int pinctrl_groups_show(struct seq_file *s, void *what)
321{
322 struct pinctrl_dev *pctldev = s->private;
323 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
324 unsigned selector = 0;
325
326
327 if (!ops)
328 return 0;
329
330 seq_puts(s, "registered pin groups:\n");
331 while (ops->list_groups(pctldev, selector) >= 0) {
332 const unsigned *pins;
333 unsigned num_pins;
334 const char *gname = ops->get_group_name(pctldev, selector);
335 int ret;
336 int i;
337
338 ret = ops->get_group_pins(pctldev, selector,
339 &pins, &num_pins);
340 if (ret)
341 seq_printf(s, "%s [ERROR GETTING PINS]\n",
342 gname);
343 else {
344 seq_printf(s, "group: %s, pins = [ ", gname);
345 for (i = 0; i < num_pins; i++)
346 seq_printf(s, "%d ", pins[i]);
347 seq_puts(s, "]\n");
348 }
349 selector++;
350 }
351
352
353 return 0;
354}
355
356static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
357{
358 struct pinctrl_dev *pctldev = s->private;
359 struct pinctrl_gpio_range *range = NULL;
360
361 seq_puts(s, "GPIO ranges handled:\n");
362
363
364 mutex_lock(&pctldev->gpio_ranges_lock);
365 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
366 seq_printf(s, "%u: %s [%u - %u]\n", range->id, range->name,
367 range->base, (range->base + range->npins - 1));
368 }
369 mutex_unlock(&pctldev->gpio_ranges_lock);
370
371 return 0;
372}
373
374static int pinctrl_devices_show(struct seq_file *s, void *what)
375{
376 struct pinctrl_dev *pctldev;
377
378 seq_puts(s, "name [pinmux]\n");
379 mutex_lock(&pinctrldev_list_mutex);
380 list_for_each_entry(pctldev, &pinctrldev_list, node) {
381 seq_printf(s, "%s ", pctldev->desc->name);
382 if (pctldev->desc->pmxops)
383 seq_puts(s, "yes");
384 else
385 seq_puts(s, "no");
386 seq_puts(s, "\n");
387 }
388 mutex_unlock(&pinctrldev_list_mutex);
389
390 return 0;
391}
392
393static int pinctrl_pins_open(struct inode *inode, struct file *file)
394{
395 return single_open(file, pinctrl_pins_show, inode->i_private);
396}
397
398static int pinctrl_groups_open(struct inode *inode, struct file *file)
399{
400 return single_open(file, pinctrl_groups_show, inode->i_private);
401}
402
403static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
404{
405 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
406}
407
408static int pinctrl_devices_open(struct inode *inode, struct file *file)
409{
410 return single_open(file, pinctrl_devices_show, NULL);
411}
412
413static const struct file_operations pinctrl_pins_ops = {
414 .open = pinctrl_pins_open,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
418};
419
420static const struct file_operations pinctrl_groups_ops = {
421 .open = pinctrl_groups_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = single_release,
425};
426
427static const struct file_operations pinctrl_gpioranges_ops = {
428 .open = pinctrl_gpioranges_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = single_release,
432};
433
434static const struct file_operations pinctrl_devices_ops = {
435 .open = pinctrl_devices_open,
436 .read = seq_read,
437 .llseek = seq_lseek,
438 .release = single_release,
439};
440
441static struct dentry *debugfs_root;
442
443static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
444{
445 static struct dentry *device_root;
446
447 device_root = debugfs_create_dir(dev_name(&pctldev->dev),
448 debugfs_root);
449 if (IS_ERR(device_root) || !device_root) {
450 pr_warn("failed to create debugfs directory for %s\n",
451 dev_name(&pctldev->dev));
452 return;
453 }
454 debugfs_create_file("pins", S_IFREG | S_IRUGO,
455 device_root, pctldev, &pinctrl_pins_ops);
456 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
457 device_root, pctldev, &pinctrl_groups_ops);
458 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
459 device_root, pctldev, &pinctrl_gpioranges_ops);
460 pinmux_init_device_debugfs(device_root, pctldev);
461}
462
463static void pinctrl_init_debugfs(void)
464{
465 debugfs_root = debugfs_create_dir("pinctrl", NULL);
466 if (IS_ERR(debugfs_root) || !debugfs_root) {
467 pr_warn("failed to create debugfs directory\n");
468 debugfs_root = NULL;
469 return;
470 }
471
472 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
473 debugfs_root, NULL, &pinctrl_devices_ops);
474 pinmux_init_debugfs(debugfs_root);
475}
476
477#else
478
479static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
480{
481}
482
483static void pinctrl_init_debugfs(void)
484{
485}
486
487#endif
488
489
490
491
492
493
494
495struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
496 struct device *dev, void *driver_data)
497{
498 static atomic_t pinmux_no = ATOMIC_INIT(0);
499 struct pinctrl_dev *pctldev;
500 int ret;
501
502 if (pctldesc == NULL)
503 return NULL;
504 if (pctldesc->name == NULL)
505 return NULL;
506
507
508 if (pctldesc->pmxops) {
509 ret = pinmux_check_ops(pctldesc->pmxops);
510 if (ret) {
511 pr_err("%s pinmux ops lacks necessary functions\n",
512 pctldesc->name);
513 return NULL;
514 }
515 }
516
517 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
518 if (pctldev == NULL)
519 return NULL;
520
521
522 pctldev->owner = pctldesc->owner;
523 pctldev->desc = pctldesc;
524 pctldev->driver_data = driver_data;
525 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
526 spin_lock_init(&pctldev->pin_desc_tree_lock);
527 INIT_LIST_HEAD(&pctldev->gpio_ranges);
528 mutex_init(&pctldev->gpio_ranges_lock);
529
530
531 pctldev->dev.parent = dev;
532 dev_set_name(&pctldev->dev, "pinctrl.%d",
533 atomic_inc_return(&pinmux_no) - 1);
534 pctldev->dev.release = pinctrl_dev_release;
535 ret = device_register(&pctldev->dev);
536 if (ret != 0) {
537 pr_err("error in device registration\n");
538 goto out_reg_dev_err;
539 }
540 dev_set_drvdata(&pctldev->dev, pctldev);
541
542
543 pr_debug("try to register %d pins on %s...\n",
544 pctldesc->npins, pctldesc->name);
545 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
546 if (ret) {
547 pr_err("error during pin registration\n");
548 pinctrl_free_pindescs(pctldev, pctldesc->pins,
549 pctldesc->npins);
550 goto out_reg_pins_err;
551 }
552
553 pinctrl_init_device_debugfs(pctldev);
554 mutex_lock(&pinctrldev_list_mutex);
555 list_add(&pctldev->node, &pinctrldev_list);
556 mutex_unlock(&pinctrldev_list_mutex);
557 pinmux_hog_maps(pctldev);
558 return pctldev;
559
560out_reg_pins_err:
561 device_del(&pctldev->dev);
562out_reg_dev_err:
563 put_device(&pctldev->dev);
564 return NULL;
565}
566EXPORT_SYMBOL_GPL(pinctrl_register);
567
568
569
570
571
572
573
574void pinctrl_unregister(struct pinctrl_dev *pctldev)
575{
576 if (pctldev == NULL)
577 return;
578
579 pinmux_unhog_maps(pctldev);
580
581 mutex_lock(&pinctrldev_list_mutex);
582 list_del(&pctldev->node);
583 mutex_unlock(&pinctrldev_list_mutex);
584
585 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
586 pctldev->desc->npins);
587 device_unregister(&pctldev->dev);
588}
589EXPORT_SYMBOL_GPL(pinctrl_unregister);
590
591static int __init pinctrl_init(void)
592{
593 pr_info("initialized pinctrl subsystem\n");
594 pinctrl_init_debugfs();
595 return 0;
596}
597
598
599core_initcall(pinctrl_init);
600