diff options
author | Linus Walleij <linus.walleij@linaro.org> | 2011-05-02 14:50:54 -0400 |
---|---|---|
committer | Linus Walleij <linus.walleij@stericsson.com> | 2011-10-13 06:49:17 -0400 |
commit | 2744e8afb3b76343e7eb8197e8b3e085036010a5 (patch) | |
tree | 2a96122ef4a6114483bda0fe9696d61a6e6f1bb5 /drivers/pinctrl | |
parent | a102a9ece5489e1718cd7543aa079082450ac3a2 (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')
-rw-r--r-- | drivers/pinctrl/Kconfig | 29 | ||||
-rw-r--r-- | drivers/pinctrl/Makefile | 6 | ||||
-rw-r--r-- | drivers/pinctrl/core.c | 599 | ||||
-rw-r--r-- | drivers/pinctrl/core.h | 72 | ||||
-rw-r--r-- | drivers/pinctrl/pinmux.c | 1180 | ||||
-rw-r--r-- | drivers/pinctrl/pinmux.h | 47 |
6 files changed, 1933 insertions, 0 deletions
diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig new file mode 100644 index 00000000000..02b4d4e92ff --- /dev/null +++ b/drivers/pinctrl/Kconfig | |||
@@ -0,0 +1,29 @@ | |||
1 | # | ||
2 | # PINCTRL infrastructure and drivers | ||
3 | # | ||
4 | |||
5 | menuconfig PINCTRL | ||
6 | bool "PINCTRL Support" | ||
7 | depends on EXPERIMENTAL | ||
8 | help | ||
9 | This enables the PINCTRL subsystem for controlling pins | ||
10 | on chip packages, for example multiplexing pins on primarily | ||
11 | PGA and BGA packages for systems on chip. | ||
12 | |||
13 | If unsure, say N. | ||
14 | |||
15 | if PINCTRL | ||
16 | |||
17 | config PINMUX | ||
18 | bool "Support pinmux controllers" | ||
19 | help | ||
20 | Say Y here if you want the pincontrol subsystem to handle pin | ||
21 | multiplexing drivers. | ||
22 | |||
23 | config DEBUG_PINCTRL | ||
24 | bool "Debug PINCTRL calls" | ||
25 | depends on DEBUG_KERNEL | ||
26 | help | ||
27 | Say Y here to add some extra checks and diagnostics to PINCTRL calls. | ||
28 | |||
29 | endif | ||
diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile new file mode 100644 index 00000000000..596ce9f2289 --- /dev/null +++ b/drivers/pinctrl/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # generic pinmux support | ||
2 | |||
3 | ccflags-$(CONFIG_DEBUG_PINMUX) += -DDEBUG | ||
4 | |||
5 | obj-$(CONFIG_PINCTRL) += core.o | ||
6 | obj-$(CONFIG_PINMUX) += pinmux.o | ||
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c new file mode 100644 index 00000000000..b2eaf8d4412 --- /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 */ | ||
32 | static DEFINE_MUTEX(pinctrldev_list_mutex); | ||
33 | static LIST_HEAD(pinctrldev_list); | ||
34 | |||
35 | static void pinctrl_dev_release(struct device *dev) | ||
36 | { | ||
37 | struct pinctrl_dev *pctldev = dev_get_drvdata(dev); | ||
38 | kfree(pctldev); | ||
39 | } | ||
40 | |||
41 | const 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 | } | ||
46 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); | ||
47 | |||
48 | void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) | ||
49 | { | ||
50 | return pctldev->driver_data; | ||
51 | } | ||
52 | EXPORT_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 | */ | ||
64 | struct 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 | |||
90 | struct 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 | */ | ||
110 | bool 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 | } | ||
123 | EXPORT_SYMBOL_GPL(pin_is_valid); | ||
124 | |||
125 | /* Deletes a range of pin descriptors */ | ||
126 | static 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 | |||
147 | static 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 | |||
179 | static 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 | */ | ||
204 | static struct pinctrl_gpio_range * | ||
205 | pinctrl_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 | */ | ||
234 | int 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 | */ | ||
266 | void 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 | */ | ||
279 | void 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 | |||
289 | static 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 | |||
320 | static 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 | |||
356 | static 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 | |||
374 | static 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 | |||
393 | static int pinctrl_pins_open(struct inode *inode, struct file *file) | ||
394 | { | ||
395 | return single_open(file, pinctrl_pins_show, inode->i_private); | ||
396 | } | ||
397 | |||
398 | static int pinctrl_groups_open(struct inode *inode, struct file *file) | ||
399 | { | ||
400 | return single_open(file, pinctrl_groups_show, inode->i_private); | ||
401 | } | ||
402 | |||
403 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) | ||
404 | { | ||
405 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); | ||
406 | } | ||
407 | |||
408 | static int pinctrl_devices_open(struct inode *inode, struct file *file) | ||
409 | { | ||
410 | return single_open(file, pinctrl_devices_show, NULL); | ||
411 | } | ||
412 | |||
413 | static 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 | |||
420 | static 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 | |||
427 | static 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 | |||
434 | static 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 | |||
441 | static struct dentry *debugfs_root; | ||
442 | |||
443 | static 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 | |||
463 | static 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 | |||
479 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) | ||
480 | { | ||
481 | } | ||
482 | |||
483 | static 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 | */ | ||
495 | struct 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 | |||
560 | out_reg_pins_err: | ||
561 | device_del(&pctldev->dev); | ||
562 | out_reg_dev_err: | ||
563 | put_device(&pctldev->dev); | ||
564 | return NULL; | ||
565 | } | ||
566 | EXPORT_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 | */ | ||
574 | void 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 | } | ||
589 | EXPORT_SYMBOL_GPL(pinctrl_unregister); | ||
590 | |||
591 | static 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 */ | ||
599 | core_initcall(pinctrl_init); | ||
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h new file mode 100644 index 00000000000..17e07772160 --- /dev/null +++ b/drivers/pinctrl/core.h | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * Core private header for 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 | |||
12 | /** | ||
13 | * struct pinctrl_dev - pin control class device | ||
14 | * @node: node to include this pin controller in the global pin controller list | ||
15 | * @desc: the pin controller descriptor supplied when initializing this pin | ||
16 | * controller | ||
17 | * @pin_desc_tree: each pin descriptor for this pin controller is stored in | ||
18 | * this radix tree | ||
19 | * @pin_desc_tree_lock: lock for the descriptor tree | ||
20 | * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller, | ||
21 | * ranges are added to this list at runtime | ||
22 | * @gpio_ranges_lock: lock for the GPIO ranges list | ||
23 | * @dev: the device entry for this pin controller | ||
24 | * @owner: module providing the pin controller, used for refcounting | ||
25 | * @driver_data: driver data for drivers registering to the pin controller | ||
26 | * subsystem | ||
27 | * @pinmux_hogs_lock: lock for the pinmux hog list | ||
28 | * @pinmux_hogs: list of pinmux maps hogged by this device | ||
29 | */ | ||
30 | struct pinctrl_dev { | ||
31 | struct list_head node; | ||
32 | struct pinctrl_desc *desc; | ||
33 | struct radix_tree_root pin_desc_tree; | ||
34 | spinlock_t pin_desc_tree_lock; | ||
35 | struct list_head gpio_ranges; | ||
36 | struct mutex gpio_ranges_lock; | ||
37 | struct device dev; | ||
38 | struct module *owner; | ||
39 | void *driver_data; | ||
40 | #ifdef CONFIG_PINMUX | ||
41 | struct mutex pinmux_hogs_lock; | ||
42 | struct list_head pinmux_hogs; | ||
43 | #endif | ||
44 | }; | ||
45 | |||
46 | /** | ||
47 | * struct pin_desc - pin descriptor for each physical pin in the arch | ||
48 | * @pctldev: corresponding pin control device | ||
49 | * @name: a name for the pin, e.g. the name of the pin/pad/finger on a | ||
50 | * datasheet or such | ||
51 | * @lock: a lock to protect the descriptor structure | ||
52 | * @mux_requested: whether the pin is already requested by pinmux or not | ||
53 | * @mux_function: a named muxing function for the pin that will be passed to | ||
54 | * subdrivers and shown in debugfs etc | ||
55 | */ | ||
56 | struct pin_desc { | ||
57 | struct pinctrl_dev *pctldev; | ||
58 | char name[16]; | ||
59 | spinlock_t lock; | ||
60 | /* These fields only added when supporting pinmux drivers */ | ||
61 | #ifdef CONFIG_PINMUX | ||
62 | bool mux_requested; | ||
63 | char mux_function[16]; | ||
64 | #endif | ||
65 | }; | ||
66 | |||
67 | struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev, | ||
68 | const char *dev_name); | ||
69 | struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin); | ||
70 | int pinctrl_get_device_gpio_range(unsigned gpio, | ||
71 | struct pinctrl_dev **outdev, | ||
72 | struct pinctrl_gpio_range **outrange); | ||
diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c new file mode 100644 index 00000000000..6544d98b2cf --- /dev/null +++ b/drivers/pinctrl/pinmux.c | |||
@@ -0,0 +1,1180 @@ | |||
1 | /* | ||
2 | * Core driver for the pin muxing portions of 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) "pinmux core: " fmt | ||
13 | |||
14 | #include <linux/kernel.h> | ||
15 | #include <linux/module.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/machine.h> | ||
28 | #include <linux/pinctrl/pinmux.h> | ||
29 | #include "core.h" | ||
30 | |||
31 | /* List of pinmuxes */ | ||
32 | static DEFINE_MUTEX(pinmux_list_mutex); | ||
33 | static LIST_HEAD(pinmux_list); | ||
34 | |||
35 | /* List of pinmux hogs */ | ||
36 | static DEFINE_MUTEX(pinmux_hoglist_mutex); | ||
37 | static LIST_HEAD(pinmux_hoglist); | ||
38 | |||
39 | /* Global pinmux maps, we allow one set only */ | ||
40 | static struct pinmux_map const *pinmux_maps; | ||
41 | static unsigned pinmux_maps_num; | ||
42 | |||
43 | /** | ||
44 | * struct pinmux_group - group list item for pinmux groups | ||
45 | * @node: pinmux group list node | ||
46 | * @group_selector: the group selector for this group | ||
47 | */ | ||
48 | struct pinmux_group { | ||
49 | struct list_head node; | ||
50 | unsigned group_selector; | ||
51 | }; | ||
52 | |||
53 | /** | ||
54 | * struct pinmux - per-device pinmux state holder | ||
55 | * @node: global list node | ||
56 | * @dev: the device using this pinmux | ||
57 | * @usecount: the number of active users of this mux setting, used to keep | ||
58 | * track of nested use cases | ||
59 | * @pins: an array of discrete physical pins used in this mapping, taken | ||
60 | * from the global pin enumeration space (copied from pinmux map) | ||
61 | * @num_pins: the number of pins in this mapping array, i.e. the number of | ||
62 | * elements in .pins so we can iterate over that array (copied from | ||
63 | * pinmux map) | ||
64 | * @pctldev: pin control device handling this pinmux | ||
65 | * @func_selector: the function selector for the pinmux device handling | ||
66 | * this pinmux | ||
67 | * @groups: the group selectors for the pinmux device and | ||
68 | * selector combination handling this pinmux, this is a list that | ||
69 | * will be traversed on all pinmux operations such as | ||
70 | * get/put/enable/disable | ||
71 | * @mutex: a lock for the pinmux state holder | ||
72 | */ | ||
73 | struct pinmux { | ||
74 | struct list_head node; | ||
75 | struct device *dev; | ||
76 | unsigned usecount; | ||
77 | struct pinctrl_dev *pctldev; | ||
78 | unsigned func_selector; | ||
79 | struct list_head groups; | ||
80 | struct mutex mutex; | ||
81 | }; | ||
82 | |||
83 | /** | ||
84 | * struct pinmux_hog - a list item to stash mux hogs | ||
85 | * @node: pinmux hog list node | ||
86 | * @map: map entry responsible for this hogging | ||
87 | * @pmx: the pinmux hogged by this item | ||
88 | */ | ||
89 | struct pinmux_hog { | ||
90 | struct list_head node; | ||
91 | struct pinmux_map const *map; | ||
92 | struct pinmux *pmx; | ||
93 | }; | ||
94 | |||
95 | /** | ||
96 | * pin_request() - request a single pin to be muxed in, typically for GPIO | ||
97 | * @pin: the pin number in the global pin space | ||
98 | * @function: a functional name to give to this pin, passed to the driver | ||
99 | * so it knows what function to mux in, e.g. the string "gpioNN" | ||
100 | * means that you want to mux in the pin for use as GPIO number NN | ||
101 | * @gpio: if this request concerns a single GPIO pin | ||
102 | * @gpio_range: the range matching the GPIO pin if this is a request for a | ||
103 | * single GPIO pin | ||
104 | */ | ||
105 | static int pin_request(struct pinctrl_dev *pctldev, | ||
106 | int pin, const char *function, bool gpio, | ||
107 | struct pinctrl_gpio_range *gpio_range) | ||
108 | { | ||
109 | struct pin_desc *desc; | ||
110 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | ||
111 | int status = -EINVAL; | ||
112 | |||
113 | dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function); | ||
114 | |||
115 | if (!pin_is_valid(pctldev, pin)) { | ||
116 | dev_err(&pctldev->dev, "pin is invalid\n"); | ||
117 | return -EINVAL; | ||
118 | } | ||
119 | |||
120 | if (!function) { | ||
121 | dev_err(&pctldev->dev, "no function name given\n"); | ||
122 | return -EINVAL; | ||
123 | } | ||
124 | |||
125 | desc = pin_desc_get(pctldev, pin); | ||
126 | if (desc == NULL) { | ||
127 | dev_err(&pctldev->dev, | ||
128 | "pin is not registered so it cannot be requested\n"); | ||
129 | goto out; | ||
130 | } | ||
131 | |||
132 | spin_lock(&desc->lock); | ||
133 | if (desc->mux_requested) { | ||
134 | spin_unlock(&desc->lock); | ||
135 | dev_err(&pctldev->dev, | ||
136 | "pin already requested\n"); | ||
137 | goto out; | ||
138 | } | ||
139 | desc->mux_requested = true; | ||
140 | strncpy(desc->mux_function, function, sizeof(desc->mux_function)); | ||
141 | spin_unlock(&desc->lock); | ||
142 | |||
143 | /* Let each pin increase references to this module */ | ||
144 | if (!try_module_get(pctldev->owner)) { | ||
145 | dev_err(&pctldev->dev, | ||
146 | "could not increase module refcount for pin %d\n", | ||
147 | pin); | ||
148 | status = -EINVAL; | ||
149 | goto out_free_pin; | ||
150 | } | ||
151 | |||
152 | /* | ||
153 | * If there is no kind of request function for the pin we just assume | ||
154 | * we got it by default and proceed. | ||
155 | */ | ||
156 | if (gpio && ops->gpio_request_enable) | ||
157 | /* This requests and enables a single GPIO pin */ | ||
158 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); | ||
159 | else if (ops->request) | ||
160 | status = ops->request(pctldev, pin); | ||
161 | else | ||
162 | status = 0; | ||
163 | |||
164 | if (status) | ||
165 | dev_err(&pctldev->dev, "->request on device %s failed " | ||
166 | "for pin %d\n", | ||
167 | pctldev->desc->name, pin); | ||
168 | out_free_pin: | ||
169 | if (status) { | ||
170 | spin_lock(&desc->lock); | ||
171 | desc->mux_requested = false; | ||
172 | desc->mux_function[0] = '\0'; | ||
173 | spin_unlock(&desc->lock); | ||
174 | } | ||
175 | out: | ||
176 | if (status) | ||
177 | dev_err(&pctldev->dev, "pin-%d (%s) status %d\n", | ||
178 | pin, function ? : "?", status); | ||
179 | |||
180 | return status; | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * pin_free() - release a single muxed in pin so something else can be muxed | ||
185 | * @pctldev: pin controller device handling this pin | ||
186 | * @pin: the pin to free | ||
187 | */ | ||
188 | static void pin_free(struct pinctrl_dev *pctldev, int pin) | ||
189 | { | ||
190 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | ||
191 | struct pin_desc *desc; | ||
192 | |||
193 | desc = pin_desc_get(pctldev, pin); | ||
194 | if (desc == NULL) { | ||
195 | dev_err(&pctldev->dev, | ||
196 | "pin is not registered so it cannot be freed\n"); | ||
197 | return; | ||
198 | } | ||
199 | |||
200 | if (ops->free) | ||
201 | ops->free(pctldev, pin); | ||
202 | |||
203 | spin_lock(&desc->lock); | ||
204 | desc->mux_requested = false; | ||
205 | desc->mux_function[0] = '\0'; | ||
206 | spin_unlock(&desc->lock); | ||
207 | module_put(pctldev->owner); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * pinmux_request_gpio() - request a single pin to be muxed in as GPIO | ||
212 | * @gpio: the GPIO pin number from the GPIO subsystem number space | ||
213 | */ | ||
214 | int pinmux_request_gpio(unsigned gpio) | ||
215 | { | ||
216 | char gpiostr[16]; | ||
217 | struct pinctrl_dev *pctldev; | ||
218 | struct pinctrl_gpio_range *range; | ||
219 | int ret; | ||
220 | int pin; | ||
221 | |||
222 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); | ||
223 | if (ret) | ||
224 | return -EINVAL; | ||
225 | |||
226 | /* Convert to the pin controllers number space */ | ||
227 | pin = gpio - range->base; | ||
228 | |||
229 | /* Conjure some name stating what chip and pin this is taken by */ | ||
230 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); | ||
231 | |||
232 | return pin_request(pctldev, pin, gpiostr, true, range); | ||
233 | } | ||
234 | EXPORT_SYMBOL_GPL(pinmux_request_gpio); | ||
235 | |||
236 | /** | ||
237 | * pinmux_free_gpio() - free a single pin, currently used as GPIO | ||
238 | * @gpio: the GPIO pin number from the GPIO subsystem number space | ||
239 | */ | ||
240 | void pinmux_free_gpio(unsigned gpio) | ||
241 | { | ||
242 | struct pinctrl_dev *pctldev; | ||
243 | struct pinctrl_gpio_range *range; | ||
244 | int ret; | ||
245 | int pin; | ||
246 | |||
247 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); | ||
248 | if (ret) | ||
249 | return; | ||
250 | |||
251 | /* Convert to the pin controllers number space */ | ||
252 | pin = gpio - range->base; | ||
253 | |||
254 | pin_free(pctldev, pin); | ||
255 | } | ||
256 | EXPORT_SYMBOL_GPL(pinmux_free_gpio); | ||
257 | |||
258 | /** | ||
259 | * pinmux_register_mappings() - register a set of pinmux mappings | ||
260 | * @maps: the pinmux mappings table to register | ||
261 | * @num_maps: the number of maps in the mapping table | ||
262 | * | ||
263 | * Only call this once during initialization of your machine, the function is | ||
264 | * tagged as __init and won't be callable after init has completed. The map | ||
265 | * passed into this function will be owned by the pinmux core and cannot be | ||
266 | * free:d. | ||
267 | */ | ||
268 | int __init pinmux_register_mappings(struct pinmux_map const *maps, | ||
269 | unsigned num_maps) | ||
270 | { | ||
271 | int i; | ||
272 | |||
273 | if (pinmux_maps != NULL) { | ||
274 | pr_err("pinmux mappings already registered, you can only " | ||
275 | "register one set of maps\n"); | ||
276 | return -EINVAL; | ||
277 | } | ||
278 | |||
279 | pr_debug("add %d pinmux maps\n", num_maps); | ||
280 | for (i = 0; i < num_maps; i++) { | ||
281 | /* Sanity check the mapping */ | ||
282 | if (!maps[i].name) { | ||
283 | pr_err("failed to register map %d: " | ||
284 | "no map name given\n", i); | ||
285 | return -EINVAL; | ||
286 | } | ||
287 | if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) { | ||
288 | pr_err("failed to register map %s (%d): " | ||
289 | "no pin control device given\n", | ||
290 | maps[i].name, i); | ||
291 | return -EINVAL; | ||
292 | } | ||
293 | if (!maps[i].function) { | ||
294 | pr_err("failed to register map %s (%d): " | ||
295 | "no function ID given\n", maps[i].name, i); | ||
296 | return -EINVAL; | ||
297 | } | ||
298 | |||
299 | if (!maps[i].dev && !maps[i].dev_name) | ||
300 | pr_debug("add system map %s function %s with no device\n", | ||
301 | maps[i].name, | ||
302 | maps[i].function); | ||
303 | else | ||
304 | pr_debug("register map %s, function %s\n", | ||
305 | maps[i].name, | ||
306 | maps[i].function); | ||
307 | } | ||
308 | |||
309 | pinmux_maps = maps; | ||
310 | pinmux_maps_num = num_maps; | ||
311 | |||
312 | return 0; | ||
313 | } | ||
314 | |||
315 | /** | ||
316 | * acquire_pins() - acquire all the pins for a certain funcion on a pinmux | ||
317 | * @pctldev: the device to take the pins on | ||
318 | * @func_selector: the function selector to acquire the pins for | ||
319 | * @group_selector: the group selector containing the pins to acquire | ||
320 | */ | ||
321 | static int acquire_pins(struct pinctrl_dev *pctldev, | ||
322 | unsigned func_selector, | ||
323 | unsigned group_selector) | ||
324 | { | ||
325 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | ||
326 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | ||
327 | const char *func = pmxops->get_function_name(pctldev, | ||
328 | func_selector); | ||
329 | unsigned *pins; | ||
330 | unsigned num_pins; | ||
331 | int ret; | ||
332 | int i; | ||
333 | |||
334 | ret = pctlops->get_group_pins(pctldev, group_selector, | ||
335 | &pins, &num_pins); | ||
336 | if (ret) | ||
337 | return ret; | ||
338 | |||
339 | dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n", | ||
340 | num_pins, group_selector); | ||
341 | |||
342 | /* Try to allocate all pins in this group, one by one */ | ||
343 | for (i = 0; i < num_pins; i++) { | ||
344 | ret = pin_request(pctldev, pins[i], func, false, NULL); | ||
345 | if (ret) { | ||
346 | dev_err(&pctldev->dev, | ||
347 | "could not get pin %d for function %s " | ||
348 | "on device %s - conflicting mux mappings?\n", | ||
349 | pins[i], func ? : "(undefined)", | ||
350 | pinctrl_dev_get_name(pctldev)); | ||
351 | /* On error release all taken pins */ | ||
352 | i--; /* this pin just failed */ | ||
353 | for (; i >= 0; i--) | ||
354 | pin_free(pctldev, pins[i]); | ||
355 | return -ENODEV; | ||
356 | } | ||
357 | } | ||
358 | return 0; | ||
359 | } | ||
360 | |||
361 | /** | ||
362 | * release_pins() - release pins taken by earlier acquirement | ||
363 | * @pctldev: the device to free the pinx on | ||
364 | * @group_selector: the group selector containing the pins to free | ||
365 | */ | ||
366 | static void release_pins(struct pinctrl_dev *pctldev, | ||
367 | unsigned group_selector) | ||
368 | { | ||
369 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | ||
370 | unsigned *pins; | ||
371 | unsigned num_pins; | ||
372 | int ret; | ||
373 | int i; | ||
374 | |||
375 | ret = pctlops->get_group_pins(pctldev, group_selector, | ||
376 | &pins, &num_pins); | ||
377 | if (ret) { | ||
378 | dev_err(&pctldev->dev, "could not get pins to release for " | ||
379 | "group selector %d\n", | ||
380 | group_selector); | ||
381 | return; | ||
382 | } | ||
383 | for (i = 0; i < num_pins; i++) | ||
384 | pin_free(pctldev, pins[i]); | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * pinmux_get_group_selector() - returns the group selector for a group | ||
389 | * @pctldev: the pin controller handling the group | ||
390 | * @pin_group: the pin group to look up | ||
391 | */ | ||
392 | static int pinmux_get_group_selector(struct pinctrl_dev *pctldev, | ||
393 | const char *pin_group) | ||
394 | { | ||
395 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | ||
396 | unsigned group_selector = 0; | ||
397 | |||
398 | while (pctlops->list_groups(pctldev, group_selector) >= 0) { | ||
399 | const char *gname = pctlops->get_group_name(pctldev, | ||
400 | group_selector); | ||
401 | if (!strcmp(gname, pin_group)) { | ||
402 | dev_dbg(&pctldev->dev, | ||
403 | "found group selector %u for %s\n", | ||
404 | group_selector, | ||
405 | pin_group); | ||
406 | return group_selector; | ||
407 | } | ||
408 | |||
409 | group_selector++; | ||
410 | } | ||
411 | |||
412 | dev_err(&pctldev->dev, "does not have pin group %s\n", | ||
413 | pin_group); | ||
414 | |||
415 | return -EINVAL; | ||
416 | } | ||
417 | |||
418 | /** | ||
419 | * pinmux_check_pin_group() - check function and pin group combo | ||
420 | * @pctldev: device to check the pin group vs function for | ||
421 | * @func_selector: the function selector to check the pin group for, we have | ||
422 | * already looked this up in the calling function | ||
423 | * @pin_group: the pin group to match to the function | ||
424 | * | ||
425 | * This function will check that the pinmux driver can supply the | ||
426 | * selected pin group for a certain function, returns the group selector if | ||
427 | * the group and function selector will work fine together, else returns | ||
428 | * negative | ||
429 | */ | ||
430 | static int pinmux_check_pin_group(struct pinctrl_dev *pctldev, | ||
431 | unsigned func_selector, | ||
432 | const char *pin_group) | ||
433 | { | ||
434 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | ||
435 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | ||
436 | int ret; | ||
437 | |||
438 | /* | ||
439 | * If the driver does not support different pin groups for the | ||
440 | * functions, we only support group 0, and assume this exists. | ||
441 | */ | ||
442 | if (!pctlops || !pctlops->list_groups) | ||
443 | return 0; | ||
444 | |||
445 | /* | ||
446 | * Passing NULL (no specific group) will select the first and | ||
447 | * hopefully only group of pins available for this function. | ||
448 | */ | ||
449 | if (!pin_group) { | ||
450 | char const * const *groups; | ||
451 | unsigned num_groups; | ||
452 | |||
453 | ret = pmxops->get_function_groups(pctldev, func_selector, | ||
454 | &groups, &num_groups); | ||
455 | if (ret) | ||
456 | return ret; | ||
457 | if (num_groups < 1) | ||
458 | return -EINVAL; | ||
459 | ret = pinmux_get_group_selector(pctldev, groups[0]); | ||
460 | if (ret < 0) { | ||
461 | dev_err(&pctldev->dev, | ||
462 | "function %s wants group %s but the pin " | ||
463 | "controller does not seem to have that group\n", | ||
464 | pmxops->get_function_name(pctldev, func_selector), | ||
465 | groups[0]); | ||
466 | return ret; | ||
467 | } | ||
468 | |||
469 | if (num_groups > 1) | ||
470 | dev_dbg(&pctldev->dev, | ||
471 | "function %s support more than one group, " | ||
472 | "default-selecting first group %s (%d)\n", | ||
473 | pmxops->get_function_name(pctldev, func_selector), | ||
474 | groups[0], | ||
475 | ret); | ||
476 | |||
477 | return ret; | ||
478 | } | ||
479 | |||
480 | dev_dbg(&pctldev->dev, | ||
481 | "check if we have pin group %s on controller %s\n", | ||
482 | pin_group, pinctrl_dev_get_name(pctldev)); | ||
483 | |||
484 | ret = pinmux_get_group_selector(pctldev, pin_group); | ||
485 | if (ret < 0) { | ||
486 | dev_dbg(&pctldev->dev, | ||
487 | "%s does not support pin group %s with function %s\n", | ||
488 | pinctrl_dev_get_name(pctldev), | ||
489 | pin_group, | ||
490 | pmxops->get_function_name(pctldev, func_selector)); | ||
491 | } | ||
492 | return ret; | ||
493 | } | ||
494 | |||
495 | /** | ||
496 | * pinmux_search_function() - check pin control driver for a certain function | ||
497 | * @pctldev: device to check for function and position | ||
498 | * @map: function map containing the function and position to look for | ||
499 | * @func_selector: returns the applicable function selector if found | ||
500 | * @group_selector: returns the applicable group selector if found | ||
501 | * | ||
502 | * This will search the pinmux driver for an applicable | ||
503 | * function with a specific pin group, returns 0 if these can be mapped | ||
504 | * negative otherwise | ||
505 | */ | ||
506 | static int pinmux_search_function(struct pinctrl_dev *pctldev, | ||
507 | struct pinmux_map const *map, | ||
508 | unsigned *func_selector, | ||
509 | unsigned *group_selector) | ||
510 | { | ||
511 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | ||
512 | unsigned selector = 0; | ||
513 | |||
514 | /* See if this pctldev has this function */ | ||
515 | while (ops->list_functions(pctldev, selector) >= 0) { | ||
516 | const char *fname = ops->get_function_name(pctldev, | ||
517 | selector); | ||
518 | int ret; | ||
519 | |||
520 | if (!strcmp(map->function, fname)) { | ||
521 | /* Found the function, check pin group */ | ||
522 | ret = pinmux_check_pin_group(pctldev, selector, | ||
523 | map->group); | ||
524 | if (ret < 0) | ||
525 | return ret; | ||
526 | |||
527 | /* This function and group selector can be used */ | ||
528 | *func_selector = selector; | ||
529 | *group_selector = ret; | ||
530 | return 0; | ||
531 | |||
532 | } | ||
533 | selector++; | ||
534 | } | ||
535 | |||
536 | pr_err("%s does not support function %s\n", | ||
537 | pinctrl_dev_get_name(pctldev), map->function); | ||
538 | return -EINVAL; | ||
539 | } | ||
540 | |||
541 | /** | ||
542 | * pinmux_enable_muxmap() - enable a map entry for a certain pinmux | ||
543 | */ | ||
544 | static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev, | ||
545 | struct pinmux *pmx, | ||
546 | struct device *dev, | ||
547 | const char *devname, | ||
548 | struct pinmux_map const *map) | ||
549 | { | ||
550 | unsigned func_selector; | ||
551 | unsigned group_selector; | ||
552 | struct pinmux_group *grp; | ||
553 | int ret; | ||
554 | |||
555 | /* | ||
556 | * Note that we're not locking the pinmux mutex here, because | ||
557 | * this is only called at pinmux initialization time when it | ||
558 | * has not been added to any list and thus is not reachable | ||
559 | * by anyone else. | ||
560 | */ | ||
561 | |||
562 | if (pmx->pctldev && pmx->pctldev != pctldev) { | ||
563 | dev_err(&pctldev->dev, | ||
564 | "different pin control devices given for device %s, " | ||
565 | "function %s\n", | ||
566 | devname, | ||
567 | map->function); | ||
568 | return -EINVAL; | ||
569 | } | ||
570 | pmx->dev = dev; | ||
571 | pmx->pctldev = pctldev; | ||
572 | |||
573 | /* Now go into the driver and try to match a function and group */ | ||
574 | ret = pinmux_search_function(pctldev, map, &func_selector, | ||
575 | &group_selector); | ||
576 | if (ret < 0) | ||
577 | return ret; | ||
578 | |||
579 | /* | ||
580 | * If the function selector is already set, it needs to be identical, | ||
581 | * we support several groups with one function but not several | ||
582 | * functions with one or several groups in the same pinmux. | ||
583 | */ | ||
584 | if (pmx->func_selector != UINT_MAX && | ||
585 | pmx->func_selector != func_selector) { | ||
586 | dev_err(&pctldev->dev, | ||
587 | "dual function defines in the map for device %s\n", | ||
588 | devname); | ||
589 | return -EINVAL; | ||
590 | } | ||
591 | pmx->func_selector = func_selector; | ||
592 | |||
593 | /* Now add this group selector, we may have many of them */ | ||
594 | grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL); | ||
595 | if (!grp) | ||
596 | return -ENOMEM; | ||
597 | grp->group_selector = group_selector; | ||
598 | ret = acquire_pins(pctldev, func_selector, group_selector); | ||
599 | if (ret) { | ||
600 | kfree(grp); | ||
601 | return ret; | ||
602 | } | ||
603 | list_add(&grp->node, &pmx->groups); | ||
604 | |||
605 | return 0; | ||
606 | } | ||
607 | |||
608 | static void pinmux_free_groups(struct pinmux *pmx) | ||
609 | { | ||
610 | struct list_head *node, *tmp; | ||
611 | |||
612 | list_for_each_safe(node, tmp, &pmx->groups) { | ||
613 | struct pinmux_group *grp = | ||
614 | list_entry(node, struct pinmux_group, node); | ||
615 | /* Release all pins taken by this group */ | ||
616 | release_pins(pmx->pctldev, grp->group_selector); | ||
617 | list_del(node); | ||
618 | kfree(grp); | ||
619 | } | ||
620 | } | ||
621 | |||
622 | /** | ||
623 | * pinmux_get() - retrieves the pinmux for a certain device | ||
624 | * @dev: the device to get the pinmux for | ||
625 | * @name: an optional specific mux mapping name or NULL, the name is only | ||
626 | * needed if you want to have more than one mapping per device, or if you | ||
627 | * need an anonymous pinmux (not tied to any specific device) | ||
628 | */ | ||
629 | struct pinmux *pinmux_get(struct device *dev, const char *name) | ||
630 | { | ||
631 | |||
632 | struct pinmux_map const *map = NULL; | ||
633 | struct pinctrl_dev *pctldev = NULL; | ||
634 | const char *devname = NULL; | ||
635 | struct pinmux *pmx; | ||
636 | bool found_map; | ||
637 | unsigned num_maps = 0; | ||
638 | int ret = -ENODEV; | ||
639 | int i; | ||
640 | |||
641 | /* We must have dev or ID or both */ | ||
642 | if (!dev && !name) | ||
643 | return ERR_PTR(-EINVAL); | ||
644 | |||
645 | if (dev) | ||
646 | devname = dev_name(dev); | ||
647 | |||
648 | pr_debug("get mux %s for device %s\n", name, | ||
649 | devname ? devname : "(none)"); | ||
650 | |||
651 | /* | ||
652 | * create the state cookie holder struct pinmux for each | ||
653 | * mapping, this is what consumers will get when requesting | ||
654 | * a pinmux handle with pinmux_get() | ||
655 | */ | ||
656 | pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL); | ||
657 | if (pmx == NULL) | ||
658 | return ERR_PTR(-ENOMEM); | ||
659 | mutex_init(&pmx->mutex); | ||
660 | pmx->func_selector = UINT_MAX; | ||
661 | INIT_LIST_HEAD(&pmx->groups); | ||
662 | |||
663 | /* Iterate over the pinmux maps to locate the right ones */ | ||
664 | for (i = 0; i < pinmux_maps_num; i++) { | ||
665 | map = &pinmux_maps[i]; | ||
666 | found_map = false; | ||
667 | |||
668 | /* | ||
669 | * First, try to find the pctldev given in the map | ||
670 | */ | ||
671 | pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev, | ||
672 | map->ctrl_dev_name); | ||
673 | if (!pctldev) { | ||
674 | const char *devname = NULL; | ||
675 | |||
676 | if (map->ctrl_dev) | ||
677 | devname = dev_name(map->ctrl_dev); | ||
678 | else if (map->ctrl_dev_name) | ||
679 | devname = map->ctrl_dev_name; | ||
680 | |||
681 | pr_warning("could not find a pinctrl device for pinmux " | ||
682 | "function %s, fishy, they shall all have one\n", | ||
683 | map->function); | ||
684 | pr_warning("given pinctrl device name: %s", | ||
685 | devname ? devname : "UNDEFINED"); | ||
686 | |||
687 | /* Continue to check the other mappings anyway... */ | ||
688 | continue; | ||
689 | } | ||
690 | |||
691 | pr_debug("in map, found pctldev %s to handle function %s", | ||
692 | dev_name(&pctldev->dev), map->function); | ||
693 | |||
694 | |||
695 | /* | ||
696 | * If we're looking for a specific named map, this must match, | ||
697 | * else we loop and look for the next. | ||
698 | */ | ||
699 | if (name != NULL) { | ||
700 | if (map->name == NULL) | ||
701 | continue; | ||
702 | if (strcmp(map->name, name)) | ||
703 | continue; | ||
704 | } | ||
705 | |||
706 | /* | ||
707 | * This is for the case where no device name is given, we | ||
708 | * already know that the function name matches from above | ||
709 | * code. | ||
710 | */ | ||
711 | if (!map->dev_name && (name != NULL)) | ||
712 | found_map = true; | ||
713 | |||
714 | /* If the mapping has a device set up it must match */ | ||
715 | if (map->dev_name && | ||
716 | (!devname || !strcmp(map->dev_name, devname))) | ||
717 | /* MATCH! */ | ||
718 | found_map = true; | ||
719 | |||
720 | /* If this map is applicable, then apply it */ | ||
721 | if (found_map) { | ||
722 | ret = pinmux_enable_muxmap(pctldev, pmx, dev, | ||
723 | devname, map); | ||
724 | if (ret) { | ||
725 | pinmux_free_groups(pmx); | ||
726 | kfree(pmx); | ||
727 | return ERR_PTR(ret); | ||
728 | } | ||
729 | num_maps++; | ||
730 | } | ||
731 | } | ||
732 | |||
733 | |||
734 | /* We should have atleast one map, right */ | ||
735 | if (!num_maps) { | ||
736 | pr_err("could not find any mux maps for device %s, ID %s\n", | ||
737 | devname ? devname : "(anonymous)", | ||
738 | name ? name : "(undefined)"); | ||
739 | kfree(pmx); | ||
740 | return ERR_PTR(-EINVAL); | ||
741 | } | ||
742 | |||
743 | pr_debug("found %u mux maps for device %s, UD %s\n", | ||
744 | num_maps, | ||
745 | devname ? devname : "(anonymous)", | ||
746 | name ? name : "(undefined)"); | ||
747 | |||
748 | /* Add the pinmux to the global list */ | ||
749 | mutex_lock(&pinmux_list_mutex); | ||
750 | list_add(&pmx->node, &pinmux_list); | ||
751 | mutex_unlock(&pinmux_list_mutex); | ||
752 | |||
753 | return pmx; | ||
754 | } | ||
755 | EXPORT_SYMBOL_GPL(pinmux_get); | ||
756 | |||
757 | /** | ||
758 | * pinmux_put() - release a previously claimed pinmux | ||
759 | * @pmx: a pinmux previously claimed by pinmux_get() | ||
760 | */ | ||
761 | void pinmux_put(struct pinmux *pmx) | ||
762 | { | ||
763 | if (pmx == NULL) | ||
764 | return; | ||
765 | |||
766 | mutex_lock(&pmx->mutex); | ||
767 | if (pmx->usecount) | ||
768 | pr_warn("releasing pinmux with active users!\n"); | ||
769 | /* Free the groups and all acquired pins */ | ||
770 | pinmux_free_groups(pmx); | ||
771 | mutex_unlock(&pmx->mutex); | ||
772 | |||
773 | /* Remove from list */ | ||
774 | mutex_lock(&pinmux_list_mutex); | ||
775 | list_del(&pmx->node); | ||
776 | mutex_unlock(&pinmux_list_mutex); | ||
777 | |||
778 | kfree(pmx); | ||
779 | } | ||
780 | EXPORT_SYMBOL_GPL(pinmux_put); | ||
781 | |||
782 | /** | ||
783 | * pinmux_enable() - enable a certain pinmux setting | ||
784 | * @pmx: the pinmux to enable, previously claimed by pinmux_get() | ||
785 | */ | ||
786 | int pinmux_enable(struct pinmux *pmx) | ||
787 | { | ||
788 | int ret = 0; | ||
789 | |||
790 | if (pmx == NULL) | ||
791 | return -EINVAL; | ||
792 | mutex_lock(&pmx->mutex); | ||
793 | if (pmx->usecount++ == 0) { | ||
794 | struct pinctrl_dev *pctldev = pmx->pctldev; | ||
795 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | ||
796 | struct pinmux_group *grp; | ||
797 | |||
798 | list_for_each_entry(grp, &pmx->groups, node) { | ||
799 | ret = ops->enable(pctldev, pmx->func_selector, | ||
800 | grp->group_selector); | ||
801 | if (ret) { | ||
802 | /* | ||
803 | * TODO: call disable() on all groups we called | ||
804 | * enable() on to this point? | ||
805 | */ | ||
806 | pmx->usecount--; | ||
807 | break; | ||
808 | } | ||
809 | } | ||
810 | } | ||
811 | mutex_unlock(&pmx->mutex); | ||
812 | return ret; | ||
813 | } | ||
814 | EXPORT_SYMBOL_GPL(pinmux_enable); | ||
815 | |||
816 | /** | ||
817 | * pinmux_disable() - disable a certain pinmux setting | ||
818 | * @pmx: the pinmux to disable, previously claimed by pinmux_get() | ||
819 | */ | ||
820 | void pinmux_disable(struct pinmux *pmx) | ||
821 | { | ||
822 | if (pmx == NULL) | ||
823 | return; | ||
824 | |||
825 | mutex_lock(&pmx->mutex); | ||
826 | if (--pmx->usecount == 0) { | ||
827 | struct pinctrl_dev *pctldev = pmx->pctldev; | ||
828 | const struct pinmux_ops *ops = pctldev->desc->pmxops; | ||
829 | struct pinmux_group *grp; | ||
830 | |||
831 | list_for_each_entry(grp, &pmx->groups, node) { | ||
832 | ops->disable(pctldev, pmx->func_selector, | ||
833 | grp->group_selector); | ||
834 | } | ||
835 | } | ||
836 | mutex_unlock(&pmx->mutex); | ||
837 | } | ||
838 | EXPORT_SYMBOL_GPL(pinmux_disable); | ||
839 | |||
840 | int pinmux_check_ops(const struct pinmux_ops *ops) | ||
841 | { | ||
842 | /* Check that we implement required operations */ | ||
843 | if (!ops->list_functions || | ||
844 | !ops->get_function_name || | ||
845 | !ops->get_function_groups || | ||
846 | !ops->enable || | ||
847 | !ops->disable) | ||
848 | return -EINVAL; | ||
849 | |||
850 | return 0; | ||
851 | } | ||
852 | |||
853 | /* Hog a single map entry and add to the hoglist */ | ||
854 | static int pinmux_hog_map(struct pinctrl_dev *pctldev, | ||
855 | struct pinmux_map const *map) | ||
856 | { | ||
857 | struct pinmux_hog *hog; | ||
858 | struct pinmux *pmx; | ||
859 | int ret; | ||
860 | |||
861 | if (map->dev || map->dev_name) { | ||
862 | /* | ||
863 | * TODO: the day we have device tree support, we can | ||
864 | * traverse the device tree and hog to specific device nodes | ||
865 | * without any problems, so then we can hog pinmuxes for | ||
866 | * all devices that just want a static pin mux at this point. | ||
867 | */ | ||
868 | dev_err(&pctldev->dev, "map %s wants to hog a non-system " | ||
869 | "pinmux, this is not going to work\n", map->name); | ||
870 | return -EINVAL; | ||
871 | } | ||
872 | |||
873 | hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL); | ||
874 | if (!hog) | ||
875 | return -ENOMEM; | ||
876 | |||
877 | pmx = pinmux_get(NULL, map->name); | ||
878 | if (IS_ERR(pmx)) { | ||
879 | kfree(hog); | ||
880 | dev_err(&pctldev->dev, | ||
881 | "could not get the %s pinmux mapping for hogging\n", | ||
882 | map->name); | ||
883 | return PTR_ERR(pmx); | ||
884 | } | ||
885 | |||
886 | ret = pinmux_enable(pmx); | ||
887 | if (ret) { | ||
888 | pinmux_put(pmx); | ||
889 | kfree(hog); | ||
890 | dev_err(&pctldev->dev, | ||
891 | "could not enable the %s pinmux mapping for hogging\n", | ||
892 | map->name); | ||
893 | return ret; | ||
894 | } | ||
895 | |||
896 | hog->map = map; | ||
897 | hog->pmx = pmx; | ||
898 | |||
899 | dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name, | ||
900 | map->function); | ||
901 | mutex_lock(&pctldev->pinmux_hogs_lock); | ||
902 | list_add(&hog->node, &pctldev->pinmux_hogs); | ||
903 | mutex_unlock(&pctldev->pinmux_hogs_lock); | ||
904 | |||
905 | return 0; | ||
906 | } | ||
907 | |||
908 | /** | ||
909 | * pinmux_hog_maps() - hog specific map entries on controller device | ||
910 | * @pctldev: the pin control device to hog entries on | ||
911 | * | ||
912 | * When the pin controllers are registered, there may be some specific pinmux | ||
913 | * map entries that need to be hogged, i.e. get+enabled until the system shuts | ||
914 | * down. | ||
915 | */ | ||
916 | int pinmux_hog_maps(struct pinctrl_dev *pctldev) | ||
917 | { | ||
918 | struct device *dev = &pctldev->dev; | ||
919 | const char *devname = dev_name(dev); | ||
920 | int ret; | ||
921 | int i; | ||
922 | |||
923 | INIT_LIST_HEAD(&pctldev->pinmux_hogs); | ||
924 | mutex_init(&pctldev->pinmux_hogs_lock); | ||
925 | |||
926 | for (i = 0; i < pinmux_maps_num; i++) { | ||
927 | struct pinmux_map const *map = &pinmux_maps[i]; | ||
928 | |||
929 | if (((map->ctrl_dev == dev) || | ||
930 | !strcmp(map->ctrl_dev_name, devname)) && | ||
931 | map->hog_on_boot) { | ||
932 | /* OK time to hog! */ | ||
933 | ret = pinmux_hog_map(pctldev, map); | ||
934 | if (ret) | ||
935 | return ret; | ||
936 | } | ||
937 | } | ||
938 | return 0; | ||
939 | } | ||
940 | |||
941 | /** | ||
942 | * pinmux_hog_maps() - unhog specific map entries on controller device | ||
943 | * @pctldev: the pin control device to unhog entries on | ||
944 | */ | ||
945 | void pinmux_unhog_maps(struct pinctrl_dev *pctldev) | ||
946 | { | ||
947 | struct list_head *node, *tmp; | ||
948 | |||
949 | mutex_lock(&pctldev->pinmux_hogs_lock); | ||
950 | list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) { | ||
951 | struct pinmux_hog *hog = | ||
952 | list_entry(node, struct pinmux_hog, node); | ||
953 | pinmux_disable(hog->pmx); | ||
954 | pinmux_put(hog->pmx); | ||
955 | list_del(node); | ||
956 | kfree(hog); | ||
957 | } | ||
958 | mutex_unlock(&pctldev->pinmux_hogs_lock); | ||
959 | } | ||
960 | |||
961 | #ifdef CONFIG_DEBUG_FS | ||
962 | |||
963 | /* Called from pincontrol core */ | ||
964 | static int pinmux_functions_show(struct seq_file *s, void *what) | ||
965 | { | ||
966 | struct pinctrl_dev *pctldev = s->private; | ||
967 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; | ||
968 | unsigned func_selector = 0; | ||
969 | |||
970 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { | ||
971 | const char *func = pmxops->get_function_name(pctldev, | ||
972 | func_selector); | ||
973 | const char * const *groups; | ||
974 | unsigned num_groups; | ||
975 | int ret; | ||
976 | int i; | ||
977 | |||
978 | ret = pmxops->get_function_groups(pctldev, func_selector, | ||
979 | &groups, &num_groups); | ||
980 | if (ret) | ||
981 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", | ||
982 | func); | ||
983 | |||
984 | seq_printf(s, "function: %s, groups = [ ", func); | ||
985 | for (i = 0; i < num_groups; i++) | ||
986 | seq_printf(s, "%s ", groups[i]); | ||
987 | seq_puts(s, "]\n"); | ||
988 | |||
989 | func_selector++; | ||
990 | |||
991 | } | ||
992 | |||
993 | return 0; | ||
994 | } | ||
995 | |||
996 | static int pinmux_pins_show(struct seq_file *s, void *what) | ||
997 | { | ||
998 | struct pinctrl_dev *pctldev = s->private; | ||
999 | unsigned pin; | ||
1000 | |||
1001 | seq_puts(s, "Pinmux settings per pin\n"); | ||
1002 | seq_puts(s, "Format: pin (name): pinmuxfunction\n"); | ||
1003 | |||
1004 | /* The highest pin number need to be included in the loop, thus <= */ | ||
1005 | for (pin = 0; pin <= pctldev->desc->maxpin; pin++) { | ||
1006 | |||
1007 | struct pin_desc *desc; | ||
1008 | |||
1009 | desc = pin_desc_get(pctldev, pin); | ||
1010 | /* Pin space may be sparse */ | ||
1011 | if (desc == NULL) | ||
1012 | continue; | ||
1013 | |||
1014 | seq_printf(s, "pin %d (%s): %s\n", pin, | ||
1015 | desc->name ? desc->name : "unnamed", | ||
1016 | desc->mux_requested ? desc->mux_function : "UNCLAIMED"); | ||
1017 | } | ||
1018 | |||
1019 | return 0; | ||
1020 | } | ||
1021 | |||
1022 | static int pinmux_hogs_show(struct seq_file *s, void *what) | ||
1023 | { | ||
1024 | struct pinctrl_dev *pctldev = s->private; | ||
1025 | struct pinmux_hog *hog; | ||
1026 | |||
1027 | seq_puts(s, "Pinmux map hogs held by device\n"); | ||
1028 | |||
1029 | list_for_each_entry(hog, &pctldev->pinmux_hogs, node) | ||
1030 | seq_printf(s, "%s\n", hog->map->name); | ||
1031 | |||
1032 | return 0; | ||
1033 | } | ||
1034 | |||
1035 | static int pinmux_show(struct seq_file *s, void *what) | ||
1036 | { | ||
1037 | struct pinmux *pmx; | ||
1038 | |||
1039 | seq_puts(s, "Requested pinmuxes and their maps:\n"); | ||
1040 | list_for_each_entry(pmx, &pinmux_list, node) { | ||
1041 | struct pinctrl_dev *pctldev = pmx->pctldev; | ||
1042 | const struct pinmux_ops *pmxops; | ||
1043 | const struct pinctrl_ops *pctlops; | ||
1044 | struct pinmux_group *grp; | ||
1045 | |||
1046 | if (!pctldev) { | ||
1047 | seq_puts(s, "NO PIN CONTROLLER DEVICE\n"); | ||
1048 | continue; | ||
1049 | } | ||
1050 | |||
1051 | pmxops = pctldev->desc->pmxops; | ||
1052 | pctlops = pctldev->desc->pctlops; | ||
1053 | |||
1054 | seq_printf(s, "device: %s function: %s (%u),", | ||
1055 | pinctrl_dev_get_name(pmx->pctldev), | ||
1056 | pmxops->get_function_name(pctldev, pmx->func_selector), | ||
1057 | pmx->func_selector); | ||
1058 | |||
1059 | seq_printf(s, " groups: ["); | ||
1060 | list_for_each_entry(grp, &pmx->groups, node) { | ||
1061 | seq_printf(s, " %s (%u)", | ||
1062 | pctlops->get_group_name(pctldev, grp->group_selector), | ||
1063 | grp->group_selector); | ||
1064 | } | ||
1065 | seq_printf(s, " ]"); | ||
1066 | |||
1067 | seq_printf(s, " users: %u map-> %s\n", | ||
1068 | pmx->usecount, | ||
1069 | pmx->dev ? dev_name(pmx->dev) : "(system)"); | ||
1070 | } | ||
1071 | |||
1072 | return 0; | ||
1073 | } | ||
1074 | |||
1075 | static int pinmux_maps_show(struct seq_file *s, void *what) | ||
1076 | { | ||
1077 | int i; | ||
1078 | |||
1079 | seq_puts(s, "Pinmux maps:\n"); | ||
1080 | |||
1081 | for (i = 0; i < pinmux_maps_num; i++) { | ||
1082 | struct pinmux_map const *map = &pinmux_maps[i]; | ||
1083 | |||
1084 | seq_printf(s, "%s:\n", map->name); | ||
1085 | if (map->dev || map->dev_name) | ||
1086 | seq_printf(s, " device: %s\n", | ||
1087 | map->dev ? dev_name(map->dev) : | ||
1088 | map->dev_name); | ||
1089 | else | ||
1090 | seq_printf(s, " SYSTEM MUX\n"); | ||
1091 | seq_printf(s, " controlling device %s\n", | ||
1092 | map->ctrl_dev ? dev_name(map->ctrl_dev) : | ||
1093 | map->ctrl_dev_name); | ||
1094 | seq_printf(s, " function: %s\n", map->function); | ||
1095 | seq_printf(s, " group: %s\n", map->group ? map->group : | ||
1096 | "(default)"); | ||
1097 | } | ||
1098 | return 0; | ||
1099 | } | ||
1100 | |||
1101 | static int pinmux_functions_open(struct inode *inode, struct file *file) | ||
1102 | { | ||
1103 | return single_open(file, pinmux_functions_show, inode->i_private); | ||
1104 | } | ||
1105 | |||
1106 | static int pinmux_pins_open(struct inode *inode, struct file *file) | ||
1107 | { | ||
1108 | return single_open(file, pinmux_pins_show, inode->i_private); | ||
1109 | } | ||
1110 | |||
1111 | static int pinmux_hogs_open(struct inode *inode, struct file *file) | ||
1112 | { | ||
1113 | return single_open(file, pinmux_hogs_show, inode->i_private); | ||
1114 | } | ||
1115 | |||
1116 | static int pinmux_open(struct inode *inode, struct file *file) | ||
1117 | { | ||
1118 | return single_open(file, pinmux_show, NULL); | ||
1119 | } | ||
1120 | |||
1121 | static int pinmux_maps_open(struct inode *inode, struct file *file) | ||
1122 | { | ||
1123 | return single_open(file, pinmux_maps_show, NULL); | ||
1124 | } | ||
1125 | |||
1126 | static const struct file_operations pinmux_functions_ops = { | ||
1127 | .open = pinmux_functions_open, | ||
1128 | .read = seq_read, | ||
1129 | .llseek = seq_lseek, | ||
1130 | .release = single_release, | ||
1131 | }; | ||
1132 | |||
1133 | static const struct file_operations pinmux_pins_ops = { | ||
1134 | .open = pinmux_pins_open, | ||
1135 | .read = seq_read, | ||
1136 | .llseek = seq_lseek, | ||
1137 | .release = single_release, | ||
1138 | }; | ||
1139 | |||
1140 | static const struct file_operations pinmux_hogs_ops = { | ||
1141 | .open = pinmux_hogs_open, | ||
1142 | .read = seq_read, | ||
1143 | .llseek = seq_lseek, | ||
1144 | .release = single_release, | ||
1145 | }; | ||
1146 | |||
1147 | static const struct file_operations pinmux_ops = { | ||
1148 | .open = pinmux_open, | ||
1149 | .read = seq_read, | ||
1150 | .llseek = seq_lseek, | ||
1151 | .release = single_release, | ||
1152 | }; | ||
1153 | |||
1154 | static const struct file_operations pinmux_maps_ops = { | ||
1155 | .open = pinmux_maps_open, | ||
1156 | .read = seq_read, | ||
1157 | .llseek = seq_lseek, | ||
1158 | .release = single_release, | ||
1159 | }; | ||
1160 | |||
1161 | void pinmux_init_device_debugfs(struct dentry *devroot, | ||
1162 | struct pinctrl_dev *pctldev) | ||
1163 | { | ||
1164 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, | ||
1165 | devroot, pctldev, &pinmux_functions_ops); | ||
1166 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, | ||
1167 | devroot, pctldev, &pinmux_pins_ops); | ||
1168 | debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO, | ||
1169 | devroot, pctldev, &pinmux_hogs_ops); | ||
1170 | } | ||
1171 | |||
1172 | void pinmux_init_debugfs(struct dentry *subsys_root) | ||
1173 | { | ||
1174 | debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO, | ||
1175 | subsys_root, NULL, &pinmux_ops); | ||
1176 | debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO, | ||
1177 | subsys_root, NULL, &pinmux_maps_ops); | ||
1178 | } | ||
1179 | |||
1180 | #endif /* CONFIG_DEBUG_FS */ | ||
diff --git a/drivers/pinctrl/pinmux.h b/drivers/pinctrl/pinmux.h new file mode 100644 index 00000000000..844500b3331 --- /dev/null +++ b/drivers/pinctrl/pinmux.h | |||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * Internal interface between the core pin control system and the | ||
3 | * pinmux portions | ||
4 | * | ||
5 | * Copyright (C) 2011 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 | * License terms: GNU General Public License (GPL) version 2 | ||
12 | */ | ||
13 | #ifdef CONFIG_PINMUX | ||
14 | |||
15 | int pinmux_check_ops(const struct pinmux_ops *ops); | ||
16 | void pinmux_init_device_debugfs(struct dentry *devroot, | ||
17 | struct pinctrl_dev *pctldev); | ||
18 | void pinmux_init_debugfs(struct dentry *subsys_root); | ||
19 | int pinmux_hog_maps(struct pinctrl_dev *pctldev); | ||
20 | void pinmux_unhog_maps(struct pinctrl_dev *pctldev); | ||
21 | |||
22 | #else | ||
23 | |||
24 | static inline int pinmux_check_ops(const struct pinmux_ops *ops) | ||
25 | { | ||
26 | return 0; | ||
27 | } | ||
28 | |||
29 | static inline void pinmux_init_device_debugfs(struct dentry *devroot, | ||
30 | struct pinctrl_dev *pctldev) | ||
31 | { | ||
32 | } | ||
33 | |||
34 | static inline void pinmux_init_debugfs(struct dentry *subsys_root) | ||
35 | { | ||
36 | } | ||
37 | |||
38 | static inline int pinmux_hog_maps(struct pinctrl_dev *pctldev) | ||
39 | { | ||
40 | return 0; | ||
41 | } | ||
42 | |||
43 | static inline void pinmux_unhog_maps(struct pinctrl_dev *pctldev) | ||
44 | { | ||
45 | } | ||
46 | |||
47 | #endif | ||