aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pinctrl/core.c
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2011-05-02 14:50:54 -0400
committerLinus Walleij <linus.walleij@stericsson.com>2011-10-13 06:49:17 -0400
commit2744e8afb3b76343e7eb8197e8b3e085036010a5 (patch)
tree2a96122ef4a6114483bda0fe9696d61a6e6f1bb5 /drivers/pinctrl/core.c
parenta102a9ece5489e1718cd7543aa079082450ac3a2 (diff)
drivers: create a pin control subsystem
This creates a subsystem for handling of pin control devices. These are devices that control different aspects of package pins. Currently it handles pinmuxing, i.e. assigning electronic functions to groups of pins on primarily PGA and BGA type of chip packages which are common in embedded systems. The plan is to also handle other I/O pin control aspects such as biasing, driving, input properties such as schmitt-triggering, load capacitance etc within this subsystem, to remove a lot of ARM arch code as well as feature-creepy GPIO drivers which are implementing the same thing over and over again. This is being done to depopulate the arch/arm/* directory of such custom drivers and try to abstract the infrastructure they all need. See the Documentation/pinctrl.txt file that is part of this patch for more details. ChangeLog v1->v2: - Various minor fixes from Joe's and Stephens review comments - Added a pinmux_config() that can invoke custom configuration with arbitrary data passed in or out to/from the pinmux driver ChangeLog v2->v3: - Renamed subsystem folder to "pinctrl" since we will likely want to keep other pin control such as biasing in this subsystem too, so let us keep to something generic even though we're mainly doing pinmux now. - As a consequence, register pins as an abstract entity separate from the pinmux. The muxing functions will claim pins out of the pin pool and make sure they do not collide. Pins can now be named by the pinctrl core. - Converted the pin lookup from a static array into a radix tree, I agreed with Grant Likely to try to avoid any static allocation (which is crap for device tree stuff) so I just rewrote this to be dynamic, just like irq number descriptors. The platform-wide definition of number of pins goes away - this is now just the sum total of the pins registered to the subsystem. - Make sure mappings with only a function name and no device works properly. ChangeLog v3->v4: - Define a number space per controller instead of globally, Stephen and Grant requested the same thing so now maps need to define target controller, and the radix tree of pin descriptors is a property on each pin controller device. - Add a compulsory pinctrl device entry to the pinctrl mapping table. This must match the pinctrl device, like "pinctrl.0" - Split the file core.c in two: core.c and pinmux.c where the latter carry all pinmux stuff, the core is for generic pin control, and use local headers to access functionality between files. It is now possible to implement a "blank" pin controller without pinmux capabilities. This split will make new additions like pindrive.c, pinbias.c etc possible for combined drivers and chunks of functionality which is a GoodThing(TM). - Rewrite the interaction with the GPIO subsystem - the pin controller descriptor now handles this by defining an offset into the GPIO numberspace for its handled pin range. This is used to look up the apropriate pin controller for a GPIO pin. Then that specific GPIO range is matched 1-1 for the target controller instance. - Fixed a number of review comments from Joe Perches. - Broke out a header file pinctrl.h for the core pin handling stuff that will be reused by other stuff than pinmux. - Fixed some erroneous EXPORT() stuff. - Remove mispatched U300 Kconfig and Makefile entries - Fixed a number of review comments from Stephen Warren, not all of them - still WIP. But I think the new mapping that will specify which function goes to which pin mux controller address 50% of your concerns (else beat me up). ChangeLog v4->v5: - Defined a "position" for each function, so the pin controller now tracks a function in a certain position, and the pinmux maps define what position you want the function in. (Feedback from Stephen Warren and Sascha Hauer). - Since we now need to request a combined function+position from the machine mapping table that connect mux settings to drivers, it was extended with a position field and a name field. The name field is now used if you e.g. need to switch between two mux map settings at runtime. - Switched from a class device to using struct bus_type for this subsystem. Verified sysfs functionality: seems to work fine. (Feedback from Arnd Bergmann and Greg Kroah-Hartman) - Define a per pincontroller list of GPIO ranges from the GPIO pin space that can be handled by the pin controller. These can be added one by one at runtime. (Feedback from Barry Song) - Expanded documentation of regulator_[get|enable|disable|put] semantics. - Fixed a number of review comments from Barry Song. (Thanks!) ChangeLog v5->v6: - Create an abstract pin group concept that can sort pins into named and enumerated groups no matter what the use of these groups may be, one possible usecase is a group of pins being muxed in or so. The intention is however to also use these groups for other pin control activities. - Make it compulsory for pinmux functions to associate with at least one group, so the abstract pin group concept is used to define the groups of pins affected by a pinmux function. The pinmux driver interface has been altered so as to enforce a function to list applicable groups per function. - Provide an optional .group entry in the pinmux machine map so the map can select beteween different available groups to be used with a certain function. - Consequent changes all over the place so that e.g. debugfs present reasonable information about the world. - Drop the per-pin mux (*config) function in the pinmux_ops struct - I was afraid that this would start to be used for things totally unrelated to muxing, we can introduce that to the generic struct pinctrl_ops if needed. I want to keep muxing orthogonal to other pin control subjects and not mix these things up. ChangeLog v6->v7: - Make it possible to have several map entries matching the same device, pin controller and function, but using a different group, and alter the semantics so that pinmux_get() will pick all matching map entries, and store the associated groups in a list. The list will then be iterated over at pinmux_enable()/pinmux_disable() and corresponding driver functions called for each defined group. Notice that you're only allowed to map multiple *groups* to the same { device, pin controller, function } triplet, attempts to map the same device to multiple pin controllers will for example fail. This is hopefully the crucial feature requested by Stephen Warren. - Add a pinmux hogging field to the pinmux mapping entries, and enable the pinmux core to hog pinmux map entries. This currently only works for pinmuxes without assigned devices as it looks now, but with device trees we can look up the corresponding struct device * entries when we register the pinmux driver, and have it hog each pinmux map in turn, for a simple approach to non-dynamic pin muxing. This addresses an issue from Grant Likely that the machine should take care of as much of the pinmux setup as possible, not the devices. By supplying a list of hogs, it can now instruct the core to take care of any static mappings. - Switch pinmux group retrieveal function to grab an array of strings representing the groups rather than an array of unsigned and rewrite accordingly. - Alter debugfs to show the grouplist handled by each pinmux. Also add a list of hogs. - Dynamically allocate a struct pinmux at pinmux_get() and free it at pinmux_put(), then add these to the global list of pinmuxes active as we go along. - Go over the list of pinmux maps at pinmux_get() time and repeatedly apply matches. - Retrieve applicable groups per function from the driver as a string array rather than a unsigned array, then lookup the enumerators. - Make the device to pinmux map a singleton - only allow the mapping table to be registered once and even tag the registration function with __init so it surely won't be abused. - Create a separate debugfs file to view the pinmux map at runtime. - Introduce a spin lock to the pin descriptor struct, lock it when modifying pin status entries. Reported by Stijn Devriendt. - Fix up the documentation after review from Stephen Warren. - Let the GPIO ranges give names as const char * instead of some fixed-length string. - add a function to unregister GPIO ranges to mirror the registration function. - Privatized the struct pinctrl_device and removed it from the <linux/pinctrl/pinctrl.h> API, the drivers do not need to know the members of this struct. It is now in the local header "core.h". - Rename the concept of "anonymous" mux maps to "system" muxes and add convenience macros and documentation. ChangeLog v7->v8: - Delete the leftover pinmux_config() function from the <linux/pinctrl/pinmux.h> header. - Fix a race condition found by Stijn Devriendt in pin_request() ChangeLog v8->v9: - Drop the bus_type and the sysfs attributes and all, we're not on the clear about how this should be used for e.g. userspace interfaces so let us save this for the future. - Use the right name in MAINTAINERS, PIN CONTROL rather than PINMUX - Don't kfree() the device state holder, let the .remove() callback handle this. - Fix up numerous kerneldoc headers to have one line for the function description and more verbose documentation below the parameters ChangeLog v9->v10: - pinctrl: EXPORT_SYMBOL needs export.h, folded in a patch from Steven Rothwell - fix pinctrl_register error handling, folded in a patch from Axel Lin - Various fixes to documentation text so that it's consistent. - Removed pointless comment from drivers/Kconfig - Removed dependency on SYSFS since we removed the bus in v9. - Renamed hopelessly abbreviated pctldev_* functions to the more verbose pinctrl_dev_* - Drop mutex properly when looking up GPIO ranges - Return NULL instead of ERR_PTR() errors on registration of pin controllers, using cast pointers is fragile. We can live without the detailed error codes for sure. Cc: Stijn Devriendt <highguy@gmail.com> Cc: Joe Perches <joe@perches.com> Cc: Russell King <linux@arm.linux.org.uk> Acked-by: Grant Likely <grant.likely@secretlab.ca> Acked-by: Stephen Warren <swarren@nvidia.com> Tested-by: Barry Song <21cnbao@gmail.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/pinctrl/core.c')
-rw-r--r--drivers/pinctrl/core.c599
1 files changed, 599 insertions, 0 deletions
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
new file mode 100644
index 000000000000..b2eaf8d4412a
--- /dev/null
+++ b/drivers/pinctrl/core.c
@@ -0,0 +1,599 @@
1/*
2 * Core driver for the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinctrl core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/radix-tree.h>
19#include <linux/err.h>
20#include <linux/list.h>
21#include <linux/mutex.h>
22#include <linux/spinlock.h>
23#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/machine.h>
28#include "core.h"
29#include "pinmux.h"
30
31/* Global list of pin control devices */
32static DEFINE_MUTEX(pinctrldev_list_mutex);
33static LIST_HEAD(pinctrldev_list);
34
35static void pinctrl_dev_release(struct device *dev)
36{
37 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
38 kfree(pctldev);
39}
40
41const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
42{
43 /* We're not allowed to register devices without name */
44 return pctldev->desc->name;
45}
46EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
47
48void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
49{
50 return pctldev->driver_data;
51}
52EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
53
54/**
55 * get_pinctrl_dev_from_dev() - look up pin controller device
56 * @dev: a device pointer, this may be NULL but then devname needs to be
57 * defined instead
58 * @devname: the name of a device instance, as returned by dev_name(), this
59 * may be NULL but then dev needs to be defined instead
60 *
61 * Looks up a pin control device matching a certain device name or pure device
62 * pointer, the pure device pointer will take precedence.
63 */
64struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
65 const char *devname)
66{
67 struct pinctrl_dev *pctldev = NULL;
68 bool found = false;
69
70 mutex_lock(&pinctrldev_list_mutex);
71 list_for_each_entry(pctldev, &pinctrldev_list, node) {
72 if (dev && &pctldev->dev == dev) {
73 /* Matched on device pointer */
74 found = true;
75 break;
76 }
77
78 if (devname &&
79 !strcmp(dev_name(&pctldev->dev), devname)) {
80 /* Matched on device name */
81 found = true;
82 break;
83 }
84 }
85 mutex_unlock(&pinctrldev_list_mutex);
86
87 return found ? pctldev : NULL;
88}
89
90struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
91{
92 struct pin_desc *pindesc;
93 unsigned long flags;
94
95 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
96 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
97 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
98
99 return pindesc;
100}
101
102/**
103 * pin_is_valid() - check if pin exists on controller
104 * @pctldev: the pin control device to check the pin on
105 * @pin: pin to check, use the local pin controller index number
106 *
107 * This tells us whether a certain pin exist on a certain pin controller or
108 * not. Pin lists may be sparse, so some pins may not exist.
109 */
110bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
111{
112 struct pin_desc *pindesc;
113
114 if (pin < 0)
115 return false;
116
117 pindesc = pin_desc_get(pctldev, pin);
118 if (pindesc == NULL)
119 return false;
120
121 return true;
122}
123EXPORT_SYMBOL_GPL(pin_is_valid);
124
125/* Deletes a range of pin descriptors */
126static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
127 const struct pinctrl_pin_desc *pins,
128 unsigned num_pins)
129{
130 int i;
131
132 spin_lock(&pctldev->pin_desc_tree_lock);
133 for (i = 0; i < num_pins; i++) {
134 struct pin_desc *pindesc;
135
136 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
137 pins[i].number);
138 if (pindesc != NULL) {
139 radix_tree_delete(&pctldev->pin_desc_tree,
140 pins[i].number);
141 }
142 kfree(pindesc);
143 }
144 spin_unlock(&pctldev->pin_desc_tree_lock);
145}
146
147static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
148 unsigned number, const char *name)
149{
150 struct pin_desc *pindesc;
151
152 pindesc = pin_desc_get(pctldev, number);
153 if (pindesc != NULL) {
154 pr_err("pin %d already registered on %s\n", number,
155 pctldev->desc->name);
156 return -EINVAL;
157 }
158
159 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
160 if (pindesc == NULL)
161 return -ENOMEM;
162 spin_lock_init(&pindesc->lock);
163
164 /* Set owner */
165 pindesc->pctldev = pctldev;
166
167 /* Copy optional basic pin info */
168 if (name)
169 strlcpy(pindesc->name, name, sizeof(pindesc->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 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
198 * @pctldev: pin controller device to check
199 * @gpio: gpio pin to check taken from the global GPIO pin space
200 *
201 * Tries to match a GPIO pin number to the ranges handled by a certain pin
202 * controller, return the range or NULL
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 /* Loop over the ranges */
210 mutex_lock(&pctldev->gpio_ranges_lock);
211 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
212 /* Check if we're in the valid range */
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 * pinctrl_get_device_gpio_range() - find device for GPIO range
226 * @gpio: the pin to locate the pin controller for
227 * @outdev: the pin control device if found
228 * @outrange: the GPIO range if found
229 *
230 * Find the pin controller handling a certain GPIO pin from the pinspace of
231 * the GPIO subsystem, return the device and the matching GPIO range. Returns
232 * negative if the GPIO range could not be found in any device.
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 /* Loop over the pin controllers */
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 * pinctrl_add_gpio_range() - register a GPIO range for a controller
260 * @pctldev: pin controller device to add the range to
261 * @range: the GPIO range to add
262 *
263 * This adds a range of GPIOs to be handled by a certain pin controller. Call
264 * this to register handled ranges after registering your pin controller.
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 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
276 * @pctldev: pin controller device to remove the range from
277 * @range: the GPIO range to remove
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 /* The highest pin number need to be included in the loop, thus <= */
299 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
300 struct pin_desc *desc;
301
302 desc = pin_desc_get(pctldev, pin);
303 /* Pin space may be sparse */
304 if (desc == NULL)
305 continue;
306
307 seq_printf(s, "pin %d (%s) ", pin,
308 desc->name ? desc->name : "unnamed");
309
310 /* Driver-specific info per pin */
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 /* No grouping */
327 if (!ops)
328 return 0;
329
330 seq_puts(s, "registered pin groups:\n");
331 while (ops->list_groups(pctldev, selector) >= 0) {
332 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 /* Loop over the ranges */
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 /* CONFIG_DEBUG_FS */
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 * pinctrl_register() - register a pin controller device
491 * @pctldesc: descriptor for this pin controller
492 * @dev: parent device for this pin controller
493 * @driver_data: private pin controller data for this pin controller
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 /* If we're implementing pinmuxing, check the ops for sanity */
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 /* Initialize pin control device struct */
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 /* Register device */
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 /* Register all the pins */
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 * pinctrl_unregister() - unregister pinmux
570 * @pctldev: pin controller to unregister
571 *
572 * Called by pinmux drivers to unregister a pinmux.
573 */
574void pinctrl_unregister(struct pinctrl_dev *pctldev)
575{
576 if (pctldev == NULL)
577 return;
578
579 pinmux_unhog_maps(pctldev);
580 /* TODO: check that no pinmuxes are still active? */
581 mutex_lock(&pinctrldev_list_mutex);
582 list_del(&pctldev->node);
583 mutex_unlock(&pinctrldev_list_mutex);
584 /* Destroy descriptor tree */
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/* init early since many drivers really need to initialized pinmux early */
599core_initcall(pinctrl_init);