Commit | Line | Data |
---|---|---|
2744e8af LW |
1 | /* |
2 | * Core driver for the pin control subsystem | |
3 | * | |
befe5bdf | 4 | * Copyright (C) 2011-2012 ST-Ericsson SA |
2744e8af LW |
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 | * | |
b2b3e66e SW |
10 | * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. |
11 | * | |
2744e8af LW |
12 | * License terms: GNU General Public License (GPL) version 2 |
13 | */ | |
14 | #define pr_fmt(fmt) "pinctrl core: " fmt | |
15 | ||
16 | #include <linux/kernel.h> | |
a5a697cd | 17 | #include <linux/export.h> |
2744e8af LW |
18 | #include <linux/init.h> |
19 | #include <linux/device.h> | |
20 | #include <linux/slab.h> | |
2744e8af LW |
21 | #include <linux/err.h> |
22 | #include <linux/list.h> | |
2744e8af LW |
23 | #include <linux/sysfs.h> |
24 | #include <linux/debugfs.h> | |
25 | #include <linux/seq_file.h> | |
6d4ca1fb | 26 | #include <linux/pinctrl/consumer.h> |
2744e8af LW |
27 | #include <linux/pinctrl/pinctrl.h> |
28 | #include <linux/pinctrl/machine.h> | |
29 | #include "core.h" | |
57291ce2 | 30 | #include "devicetree.h" |
2744e8af | 31 | #include "pinmux.h" |
ae6b4d85 | 32 | #include "pinconf.h" |
2744e8af | 33 | |
b2b3e66e SW |
34 | /** |
35 | * struct pinctrl_maps - a list item containing part of the mapping table | |
36 | * @node: mapping table list node | |
37 | * @maps: array of mapping table entries | |
38 | * @num_maps: the number of entries in @maps | |
39 | */ | |
40 | struct pinctrl_maps { | |
41 | struct list_head node; | |
42 | struct pinctrl_map const *maps; | |
43 | unsigned num_maps; | |
44 | }; | |
45 | ||
5b3aa5f7 DA |
46 | static bool pinctrl_dummy_state; |
47 | ||
57b676f9 SW |
48 | /* Mutex taken by all entry points */ |
49 | DEFINE_MUTEX(pinctrl_mutex); | |
50 | ||
51 | /* Global list of pin control devices (struct pinctrl_dev) */ | |
57291ce2 | 52 | LIST_HEAD(pinctrldev_list); |
2744e8af | 53 | |
57b676f9 | 54 | /* List of pin controller handles (struct pinctrl) */ |
befe5bdf LW |
55 | static LIST_HEAD(pinctrl_list); |
56 | ||
57b676f9 | 57 | /* List of pinctrl maps (struct pinctrl_maps) */ |
b2b3e66e SW |
58 | static LIST_HEAD(pinctrl_maps); |
59 | ||
60 | #define for_each_maps(_maps_node_, _i_, _map_) \ | |
61 | list_for_each_entry(_maps_node_, &pinctrl_maps, node) \ | |
62 | for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \ | |
63 | _i_ < _maps_node_->num_maps; \ | |
64 | i++, _map_ = &_maps_node_->maps[_i_]) | |
befe5bdf | 65 | |
5b3aa5f7 DA |
66 | /** |
67 | * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support | |
68 | * | |
69 | * Usually this function is called by platforms without pinctrl driver support | |
70 | * but run with some shared drivers using pinctrl APIs. | |
71 | * After calling this function, the pinctrl core will return successfully | |
72 | * with creating a dummy state for the driver to keep going smoothly. | |
73 | */ | |
74 | void pinctrl_provide_dummies(void) | |
75 | { | |
76 | pinctrl_dummy_state = true; | |
77 | } | |
78 | ||
2744e8af LW |
79 | const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) |
80 | { | |
81 | /* We're not allowed to register devices without name */ | |
82 | return pctldev->desc->name; | |
83 | } | |
84 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); | |
85 | ||
86 | void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) | |
87 | { | |
88 | return pctldev->driver_data; | |
89 | } | |
90 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); | |
91 | ||
92 | /** | |
9dfac4fd LW |
93 | * get_pinctrl_dev_from_devname() - look up pin controller device |
94 | * @devname: the name of a device instance, as returned by dev_name() | |
2744e8af LW |
95 | * |
96 | * Looks up a pin control device matching a certain device name or pure device | |
97 | * pointer, the pure device pointer will take precedence. | |
98 | */ | |
9dfac4fd | 99 | struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname) |
2744e8af LW |
100 | { |
101 | struct pinctrl_dev *pctldev = NULL; | |
102 | bool found = false; | |
103 | ||
9dfac4fd LW |
104 | if (!devname) |
105 | return NULL; | |
106 | ||
2744e8af | 107 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
9dfac4fd | 108 | if (!strcmp(dev_name(pctldev->dev), devname)) { |
2744e8af LW |
109 | /* Matched on device name */ |
110 | found = true; | |
111 | break; | |
112 | } | |
113 | } | |
2744e8af LW |
114 | |
115 | return found ? pctldev : NULL; | |
116 | } | |
117 | ||
ae6b4d85 LW |
118 | /** |
119 | * pin_get_from_name() - look up a pin number from a name | |
120 | * @pctldev: the pin control device to lookup the pin on | |
121 | * @name: the name of the pin to look up | |
122 | */ | |
123 | int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name) | |
124 | { | |
706e8520 | 125 | unsigned i, pin; |
ae6b4d85 | 126 | |
706e8520 CP |
127 | /* The pin number can be retrived from the pin controller descriptor */ |
128 | for (i = 0; i < pctldev->desc->npins; i++) { | |
ae6b4d85 LW |
129 | struct pin_desc *desc; |
130 | ||
706e8520 | 131 | pin = pctldev->desc->pins[i].number; |
ae6b4d85 LW |
132 | desc = pin_desc_get(pctldev, pin); |
133 | /* Pin space may be sparse */ | |
134 | if (desc == NULL) | |
135 | continue; | |
136 | if (desc->name && !strcmp(name, desc->name)) | |
137 | return pin; | |
138 | } | |
139 | ||
140 | return -EINVAL; | |
141 | } | |
142 | ||
dcb5dbc3 DA |
143 | /** |
144 | * pin_get_name_from_id() - look up a pin name from a pin id | |
145 | * @pctldev: the pin control device to lookup the pin on | |
146 | * @name: the name of the pin to look up | |
147 | */ | |
148 | const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin) | |
149 | { | |
150 | const struct pin_desc *desc; | |
151 | ||
152 | desc = pin_desc_get(pctldev, pin); | |
153 | if (desc == NULL) { | |
154 | dev_err(pctldev->dev, "failed to get pin(%d) name\n", | |
155 | pin); | |
156 | return NULL; | |
157 | } | |
158 | ||
159 | return desc->name; | |
160 | } | |
161 | ||
2744e8af LW |
162 | /** |
163 | * pin_is_valid() - check if pin exists on controller | |
164 | * @pctldev: the pin control device to check the pin on | |
165 | * @pin: pin to check, use the local pin controller index number | |
166 | * | |
167 | * This tells us whether a certain pin exist on a certain pin controller or | |
168 | * not. Pin lists may be sparse, so some pins may not exist. | |
169 | */ | |
170 | bool pin_is_valid(struct pinctrl_dev *pctldev, int pin) | |
171 | { | |
172 | struct pin_desc *pindesc; | |
173 | ||
174 | if (pin < 0) | |
175 | return false; | |
176 | ||
57b676f9 | 177 | mutex_lock(&pinctrl_mutex); |
2744e8af | 178 | pindesc = pin_desc_get(pctldev, pin); |
57b676f9 | 179 | mutex_unlock(&pinctrl_mutex); |
2744e8af | 180 | |
57b676f9 | 181 | return pindesc != NULL; |
2744e8af LW |
182 | } |
183 | EXPORT_SYMBOL_GPL(pin_is_valid); | |
184 | ||
185 | /* Deletes a range of pin descriptors */ | |
186 | static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev, | |
187 | const struct pinctrl_pin_desc *pins, | |
188 | unsigned num_pins) | |
189 | { | |
190 | int i; | |
191 | ||
2744e8af LW |
192 | for (i = 0; i < num_pins; i++) { |
193 | struct pin_desc *pindesc; | |
194 | ||
195 | pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, | |
196 | pins[i].number); | |
197 | if (pindesc != NULL) { | |
198 | radix_tree_delete(&pctldev->pin_desc_tree, | |
199 | pins[i].number); | |
ca53c5f1 LW |
200 | if (pindesc->dynamic_name) |
201 | kfree(pindesc->name); | |
2744e8af LW |
202 | } |
203 | kfree(pindesc); | |
204 | } | |
2744e8af LW |
205 | } |
206 | ||
207 | static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, | |
208 | unsigned number, const char *name) | |
209 | { | |
210 | struct pin_desc *pindesc; | |
211 | ||
212 | pindesc = pin_desc_get(pctldev, number); | |
213 | if (pindesc != NULL) { | |
214 | pr_err("pin %d already registered on %s\n", number, | |
215 | pctldev->desc->name); | |
216 | return -EINVAL; | |
217 | } | |
218 | ||
219 | pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); | |
95dcd4ae SW |
220 | if (pindesc == NULL) { |
221 | dev_err(pctldev->dev, "failed to alloc struct pin_desc\n"); | |
2744e8af | 222 | return -ENOMEM; |
95dcd4ae | 223 | } |
ae6b4d85 | 224 | |
2744e8af LW |
225 | /* Set owner */ |
226 | pindesc->pctldev = pctldev; | |
227 | ||
9af1e44f | 228 | /* Copy basic pin info */ |
8dc6ae4d | 229 | if (name) { |
ca53c5f1 LW |
230 | pindesc->name = name; |
231 | } else { | |
232 | pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number); | |
233 | if (pindesc->name == NULL) | |
234 | return -ENOMEM; | |
235 | pindesc->dynamic_name = true; | |
236 | } | |
2744e8af | 237 | |
2744e8af | 238 | radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); |
2744e8af | 239 | pr_debug("registered pin %d (%s) on %s\n", |
ca53c5f1 | 240 | number, pindesc->name, pctldev->desc->name); |
2744e8af LW |
241 | return 0; |
242 | } | |
243 | ||
244 | static int pinctrl_register_pins(struct pinctrl_dev *pctldev, | |
245 | struct pinctrl_pin_desc const *pins, | |
246 | unsigned num_descs) | |
247 | { | |
248 | unsigned i; | |
249 | int ret = 0; | |
250 | ||
251 | for (i = 0; i < num_descs; i++) { | |
252 | ret = pinctrl_register_one_pin(pctldev, | |
253 | pins[i].number, pins[i].name); | |
254 | if (ret) | |
255 | return ret; | |
256 | } | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | /** | |
262 | * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range | |
263 | * @pctldev: pin controller device to check | |
264 | * @gpio: gpio pin to check taken from the global GPIO pin space | |
265 | * | |
266 | * Tries to match a GPIO pin number to the ranges handled by a certain pin | |
267 | * controller, return the range or NULL | |
268 | */ | |
269 | static struct pinctrl_gpio_range * | |
270 | pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) | |
271 | { | |
272 | struct pinctrl_gpio_range *range = NULL; | |
273 | ||
274 | /* Loop over the ranges */ | |
2744e8af LW |
275 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
276 | /* Check if we're in the valid range */ | |
277 | if (gpio >= range->base && | |
278 | gpio < range->base + range->npins) { | |
2744e8af LW |
279 | return range; |
280 | } | |
281 | } | |
2744e8af LW |
282 | |
283 | return NULL; | |
284 | } | |
285 | ||
286 | /** | |
287 | * pinctrl_get_device_gpio_range() - find device for GPIO range | |
288 | * @gpio: the pin to locate the pin controller for | |
289 | * @outdev: the pin control device if found | |
290 | * @outrange: the GPIO range if found | |
291 | * | |
292 | * Find the pin controller handling a certain GPIO pin from the pinspace of | |
293 | * the GPIO subsystem, return the device and the matching GPIO range. Returns | |
294 | * negative if the GPIO range could not be found in any device. | |
295 | */ | |
4ecce45d SW |
296 | static int pinctrl_get_device_gpio_range(unsigned gpio, |
297 | struct pinctrl_dev **outdev, | |
298 | struct pinctrl_gpio_range **outrange) | |
2744e8af LW |
299 | { |
300 | struct pinctrl_dev *pctldev = NULL; | |
301 | ||
302 | /* Loop over the pin controllers */ | |
2744e8af LW |
303 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
304 | struct pinctrl_gpio_range *range; | |
305 | ||
306 | range = pinctrl_match_gpio_range(pctldev, gpio); | |
307 | if (range != NULL) { | |
308 | *outdev = pctldev; | |
309 | *outrange = range; | |
2744e8af LW |
310 | return 0; |
311 | } | |
312 | } | |
2744e8af LW |
313 | |
314 | return -EINVAL; | |
315 | } | |
316 | ||
317 | /** | |
318 | * pinctrl_add_gpio_range() - register a GPIO range for a controller | |
319 | * @pctldev: pin controller device to add the range to | |
320 | * @range: the GPIO range to add | |
321 | * | |
322 | * This adds a range of GPIOs to be handled by a certain pin controller. Call | |
323 | * this to register handled ranges after registering your pin controller. | |
324 | */ | |
325 | void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, | |
326 | struct pinctrl_gpio_range *range) | |
327 | { | |
57b676f9 | 328 | mutex_lock(&pinctrl_mutex); |
8b9c139f | 329 | list_add_tail(&range->node, &pctldev->gpio_ranges); |
57b676f9 | 330 | mutex_unlock(&pinctrl_mutex); |
2744e8af | 331 | } |
4ecce45d | 332 | EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); |
2744e8af LW |
333 | |
334 | /** | |
335 | * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller | |
336 | * @pctldev: pin controller device to remove the range from | |
337 | * @range: the GPIO range to remove | |
338 | */ | |
339 | void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, | |
340 | struct pinctrl_gpio_range *range) | |
341 | { | |
57b676f9 | 342 | mutex_lock(&pinctrl_mutex); |
2744e8af | 343 | list_del(&range->node); |
57b676f9 | 344 | mutex_unlock(&pinctrl_mutex); |
2744e8af | 345 | } |
4ecce45d | 346 | EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); |
2744e8af | 347 | |
7afde8ba LW |
348 | /** |
349 | * pinctrl_get_group_selector() - returns the group selector for a group | |
350 | * @pctldev: the pin controller handling the group | |
351 | * @pin_group: the pin group to look up | |
352 | */ | |
353 | int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, | |
354 | const char *pin_group) | |
355 | { | |
356 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | |
d1e90e9e | 357 | unsigned ngroups = pctlops->get_groups_count(pctldev); |
7afde8ba LW |
358 | unsigned group_selector = 0; |
359 | ||
d1e90e9e | 360 | while (group_selector < ngroups) { |
7afde8ba LW |
361 | const char *gname = pctlops->get_group_name(pctldev, |
362 | group_selector); | |
363 | if (!strcmp(gname, pin_group)) { | |
51cd24ee | 364 | dev_dbg(pctldev->dev, |
7afde8ba LW |
365 | "found group selector %u for %s\n", |
366 | group_selector, | |
367 | pin_group); | |
368 | return group_selector; | |
369 | } | |
370 | ||
371 | group_selector++; | |
372 | } | |
373 | ||
51cd24ee | 374 | dev_err(pctldev->dev, "does not have pin group %s\n", |
7afde8ba LW |
375 | pin_group); |
376 | ||
377 | return -EINVAL; | |
378 | } | |
379 | ||
befe5bdf LW |
380 | /** |
381 | * pinctrl_request_gpio() - request a single pin to be used in as GPIO | |
382 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
383 | * | |
384 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, | |
385 | * as part of their gpio_request() semantics, platforms and individual drivers | |
386 | * shall *NOT* request GPIO pins to be muxed in. | |
387 | */ | |
388 | int pinctrl_request_gpio(unsigned gpio) | |
389 | { | |
390 | struct pinctrl_dev *pctldev; | |
391 | struct pinctrl_gpio_range *range; | |
392 | int ret; | |
393 | int pin; | |
394 | ||
57b676f9 SW |
395 | mutex_lock(&pinctrl_mutex); |
396 | ||
befe5bdf | 397 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
57b676f9 SW |
398 | if (ret) { |
399 | mutex_unlock(&pinctrl_mutex); | |
befe5bdf | 400 | return -EINVAL; |
57b676f9 | 401 | } |
befe5bdf LW |
402 | |
403 | /* Convert to the pin controllers number space */ | |
404 | pin = gpio - range->base + range->pin_base; | |
405 | ||
57b676f9 SW |
406 | ret = pinmux_request_gpio(pctldev, range, pin, gpio); |
407 | ||
408 | mutex_unlock(&pinctrl_mutex); | |
409 | return ret; | |
befe5bdf LW |
410 | } |
411 | EXPORT_SYMBOL_GPL(pinctrl_request_gpio); | |
412 | ||
413 | /** | |
414 | * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO | |
415 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
416 | * | |
417 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, | |
418 | * as part of their gpio_free() semantics, platforms and individual drivers | |
419 | * shall *NOT* request GPIO pins to be muxed out. | |
420 | */ | |
421 | void pinctrl_free_gpio(unsigned gpio) | |
422 | { | |
423 | struct pinctrl_dev *pctldev; | |
424 | struct pinctrl_gpio_range *range; | |
425 | int ret; | |
426 | int pin; | |
427 | ||
57b676f9 SW |
428 | mutex_lock(&pinctrl_mutex); |
429 | ||
befe5bdf | 430 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
57b676f9 SW |
431 | if (ret) { |
432 | mutex_unlock(&pinctrl_mutex); | |
befe5bdf | 433 | return; |
57b676f9 | 434 | } |
befe5bdf LW |
435 | |
436 | /* Convert to the pin controllers number space */ | |
437 | pin = gpio - range->base + range->pin_base; | |
438 | ||
57b676f9 SW |
439 | pinmux_free_gpio(pctldev, pin, range); |
440 | ||
441 | mutex_unlock(&pinctrl_mutex); | |
befe5bdf LW |
442 | } |
443 | EXPORT_SYMBOL_GPL(pinctrl_free_gpio); | |
444 | ||
445 | static int pinctrl_gpio_direction(unsigned gpio, bool input) | |
446 | { | |
447 | struct pinctrl_dev *pctldev; | |
448 | struct pinctrl_gpio_range *range; | |
449 | int ret; | |
450 | int pin; | |
451 | ||
452 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); | |
453 | if (ret) | |
454 | return ret; | |
455 | ||
456 | /* Convert to the pin controllers number space */ | |
457 | pin = gpio - range->base + range->pin_base; | |
458 | ||
459 | return pinmux_gpio_direction(pctldev, range, pin, input); | |
460 | } | |
461 | ||
462 | /** | |
463 | * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode | |
464 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
465 | * | |
466 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, | |
467 | * as part of their gpio_direction_input() semantics, platforms and individual | |
468 | * drivers shall *NOT* touch pin control GPIO calls. | |
469 | */ | |
470 | int pinctrl_gpio_direction_input(unsigned gpio) | |
471 | { | |
57b676f9 SW |
472 | int ret; |
473 | mutex_lock(&pinctrl_mutex); | |
474 | ret = pinctrl_gpio_direction(gpio, true); | |
475 | mutex_unlock(&pinctrl_mutex); | |
476 | return ret; | |
befe5bdf LW |
477 | } |
478 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); | |
479 | ||
480 | /** | |
481 | * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode | |
482 | * @gpio: the GPIO pin number from the GPIO subsystem number space | |
483 | * | |
484 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, | |
485 | * as part of their gpio_direction_output() semantics, platforms and individual | |
486 | * drivers shall *NOT* touch pin control GPIO calls. | |
487 | */ | |
488 | int pinctrl_gpio_direction_output(unsigned gpio) | |
489 | { | |
57b676f9 SW |
490 | int ret; |
491 | mutex_lock(&pinctrl_mutex); | |
492 | ret = pinctrl_gpio_direction(gpio, false); | |
493 | mutex_unlock(&pinctrl_mutex); | |
494 | return ret; | |
befe5bdf LW |
495 | } |
496 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); | |
497 | ||
6e5e959d SW |
498 | static struct pinctrl_state *find_state(struct pinctrl *p, |
499 | const char *name) | |
befe5bdf | 500 | { |
6e5e959d SW |
501 | struct pinctrl_state *state; |
502 | ||
503 | list_for_each_entry(state, &p->states, node) | |
504 | if (!strcmp(state->name, name)) | |
505 | return state; | |
506 | ||
507 | return NULL; | |
508 | } | |
509 | ||
510 | static struct pinctrl_state *create_state(struct pinctrl *p, | |
511 | const char *name) | |
512 | { | |
513 | struct pinctrl_state *state; | |
514 | ||
515 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
516 | if (state == NULL) { | |
517 | dev_err(p->dev, | |
518 | "failed to alloc struct pinctrl_state\n"); | |
519 | return ERR_PTR(-ENOMEM); | |
520 | } | |
521 | ||
522 | state->name = name; | |
523 | INIT_LIST_HEAD(&state->settings); | |
524 | ||
525 | list_add_tail(&state->node, &p->states); | |
526 | ||
527 | return state; | |
528 | } | |
529 | ||
530 | static int add_setting(struct pinctrl *p, struct pinctrl_map const *map) | |
531 | { | |
532 | struct pinctrl_state *state; | |
7ecdb16f | 533 | struct pinctrl_setting *setting; |
6e5e959d | 534 | int ret; |
befe5bdf | 535 | |
6e5e959d SW |
536 | state = find_state(p, map->name); |
537 | if (!state) | |
538 | state = create_state(p, map->name); | |
539 | if (IS_ERR(state)) | |
540 | return PTR_ERR(state); | |
befe5bdf | 541 | |
1e2082b5 SW |
542 | if (map->type == PIN_MAP_TYPE_DUMMY_STATE) |
543 | return 0; | |
544 | ||
6e5e959d SW |
545 | setting = kzalloc(sizeof(*setting), GFP_KERNEL); |
546 | if (setting == NULL) { | |
547 | dev_err(p->dev, | |
548 | "failed to alloc struct pinctrl_setting\n"); | |
549 | return -ENOMEM; | |
550 | } | |
befe5bdf | 551 | |
1e2082b5 SW |
552 | setting->type = map->type; |
553 | ||
6e5e959d SW |
554 | setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
555 | if (setting->pctldev == NULL) { | |
c05127c4 | 556 | dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", |
6e5e959d SW |
557 | map->ctrl_dev_name); |
558 | kfree(setting); | |
c05127c4 LW |
559 | /* |
560 | * OK let us guess that the driver is not there yet, and | |
561 | * let's defer obtaining this pinctrl handle to later... | |
562 | */ | |
563 | return -EPROBE_DEFER; | |
6e5e959d SW |
564 | } |
565 | ||
1e2082b5 SW |
566 | switch (map->type) { |
567 | case PIN_MAP_TYPE_MUX_GROUP: | |
568 | ret = pinmux_map_to_setting(map, setting); | |
569 | break; | |
570 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
571 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
572 | ret = pinconf_map_to_setting(map, setting); | |
573 | break; | |
574 | default: | |
575 | ret = -EINVAL; | |
576 | break; | |
577 | } | |
6e5e959d SW |
578 | if (ret < 0) { |
579 | kfree(setting); | |
580 | return ret; | |
581 | } | |
582 | ||
583 | list_add_tail(&setting->node, &state->settings); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static struct pinctrl *find_pinctrl(struct device *dev) | |
589 | { | |
590 | struct pinctrl *p; | |
591 | ||
1e2082b5 | 592 | list_for_each_entry(p, &pinctrl_list, node) |
6e5e959d SW |
593 | if (p->dev == dev) |
594 | return p; | |
595 | ||
596 | return NULL; | |
597 | } | |
598 | ||
599 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist); | |
600 | ||
601 | static struct pinctrl *create_pinctrl(struct device *dev) | |
602 | { | |
603 | struct pinctrl *p; | |
604 | const char *devname; | |
605 | struct pinctrl_maps *maps_node; | |
606 | int i; | |
607 | struct pinctrl_map const *map; | |
608 | int ret; | |
befe5bdf LW |
609 | |
610 | /* | |
611 | * create the state cookie holder struct pinctrl for each | |
612 | * mapping, this is what consumers will get when requesting | |
613 | * a pin control handle with pinctrl_get() | |
614 | */ | |
02f5b989 | 615 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
95dcd4ae SW |
616 | if (p == NULL) { |
617 | dev_err(dev, "failed to alloc struct pinctrl\n"); | |
befe5bdf | 618 | return ERR_PTR(-ENOMEM); |
95dcd4ae | 619 | } |
7ecdb16f | 620 | p->dev = dev; |
6e5e959d | 621 | INIT_LIST_HEAD(&p->states); |
57291ce2 SW |
622 | INIT_LIST_HEAD(&p->dt_maps); |
623 | ||
624 | ret = pinctrl_dt_to_map(p); | |
625 | if (ret < 0) { | |
626 | kfree(p); | |
627 | return ERR_PTR(ret); | |
628 | } | |
6e5e959d SW |
629 | |
630 | devname = dev_name(dev); | |
befe5bdf LW |
631 | |
632 | /* Iterate over the pin control maps to locate the right ones */ | |
b2b3e66e | 633 | for_each_maps(maps_node, i, map) { |
7ecdb16f SW |
634 | /* Map must be for this device */ |
635 | if (strcmp(map->dev_name, devname)) | |
636 | continue; | |
637 | ||
6e5e959d SW |
638 | ret = add_setting(p, map); |
639 | if (ret < 0) { | |
640 | pinctrl_put_locked(p, false); | |
641 | return ERR_PTR(ret); | |
7ecdb16f | 642 | } |
befe5bdf LW |
643 | } |
644 | ||
befe5bdf | 645 | /* Add the pinmux to the global list */ |
8b9c139f | 646 | list_add_tail(&p->node, &pinctrl_list); |
befe5bdf LW |
647 | |
648 | return p; | |
6e5e959d | 649 | } |
7ecdb16f | 650 | |
6e5e959d SW |
651 | static struct pinctrl *pinctrl_get_locked(struct device *dev) |
652 | { | |
653 | struct pinctrl *p; | |
7ecdb16f | 654 | |
6e5e959d SW |
655 | if (WARN_ON(!dev)) |
656 | return ERR_PTR(-EINVAL); | |
657 | ||
658 | p = find_pinctrl(dev); | |
659 | if (p != NULL) | |
660 | return ERR_PTR(-EBUSY); | |
7ecdb16f | 661 | |
6e5e959d SW |
662 | p = create_pinctrl(dev); |
663 | if (IS_ERR(p)) | |
664 | return p; | |
665 | ||
666 | return p; | |
befe5bdf | 667 | } |
b2b3e66e SW |
668 | |
669 | /** | |
6e5e959d SW |
670 | * pinctrl_get() - retrieves the pinctrl handle for a device |
671 | * @dev: the device to obtain the handle for | |
b2b3e66e | 672 | */ |
6e5e959d | 673 | struct pinctrl *pinctrl_get(struct device *dev) |
b2b3e66e SW |
674 | { |
675 | struct pinctrl *p; | |
676 | ||
57b676f9 | 677 | mutex_lock(&pinctrl_mutex); |
6e5e959d | 678 | p = pinctrl_get_locked(dev); |
57b676f9 | 679 | mutex_unlock(&pinctrl_mutex); |
b2b3e66e SW |
680 | |
681 | return p; | |
682 | } | |
befe5bdf LW |
683 | EXPORT_SYMBOL_GPL(pinctrl_get); |
684 | ||
6e5e959d | 685 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist) |
befe5bdf | 686 | { |
6e5e959d SW |
687 | struct pinctrl_state *state, *n1; |
688 | struct pinctrl_setting *setting, *n2; | |
689 | ||
690 | list_for_each_entry_safe(state, n1, &p->states, node) { | |
691 | list_for_each_entry_safe(setting, n2, &state->settings, node) { | |
1e2082b5 SW |
692 | switch (setting->type) { |
693 | case PIN_MAP_TYPE_MUX_GROUP: | |
694 | if (state == p->state) | |
695 | pinmux_disable_setting(setting); | |
696 | pinmux_free_setting(setting); | |
697 | break; | |
698 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
699 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
700 | pinconf_free_setting(setting); | |
701 | break; | |
702 | default: | |
703 | break; | |
704 | } | |
6e5e959d SW |
705 | list_del(&setting->node); |
706 | kfree(setting); | |
707 | } | |
708 | list_del(&state->node); | |
709 | kfree(state); | |
7ecdb16f | 710 | } |
befe5bdf | 711 | |
57291ce2 SW |
712 | pinctrl_dt_free_maps(p); |
713 | ||
6e5e959d SW |
714 | if (inlist) |
715 | list_del(&p->node); | |
befe5bdf LW |
716 | kfree(p); |
717 | } | |
befe5bdf LW |
718 | |
719 | /** | |
6e5e959d SW |
720 | * pinctrl_put() - release a previously claimed pinctrl handle |
721 | * @p: the pinctrl handle to release | |
befe5bdf | 722 | */ |
57b676f9 SW |
723 | void pinctrl_put(struct pinctrl *p) |
724 | { | |
725 | mutex_lock(&pinctrl_mutex); | |
6e5e959d | 726 | pinctrl_put_locked(p, true); |
57b676f9 SW |
727 | mutex_unlock(&pinctrl_mutex); |
728 | } | |
729 | EXPORT_SYMBOL_GPL(pinctrl_put); | |
730 | ||
6e5e959d SW |
731 | static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p, |
732 | const char *name) | |
befe5bdf | 733 | { |
6e5e959d | 734 | struct pinctrl_state *state; |
befe5bdf | 735 | |
6e5e959d | 736 | state = find_state(p, name); |
5b3aa5f7 DA |
737 | if (!state) { |
738 | if (pinctrl_dummy_state) { | |
739 | /* create dummy state */ | |
740 | dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", | |
741 | name); | |
742 | state = create_state(p, name); | |
743 | if (IS_ERR(state)) | |
744 | return state; | |
745 | } else { | |
746 | return ERR_PTR(-ENODEV); | |
747 | } | |
748 | } | |
57b676f9 | 749 | |
6e5e959d | 750 | return state; |
befe5bdf | 751 | } |
befe5bdf LW |
752 | |
753 | /** | |
6e5e959d SW |
754 | * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle |
755 | * @p: the pinctrl handle to retrieve the state from | |
756 | * @name: the state name to retrieve | |
befe5bdf | 757 | */ |
6e5e959d | 758 | struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name) |
57b676f9 | 759 | { |
6e5e959d SW |
760 | struct pinctrl_state *s; |
761 | ||
57b676f9 | 762 | mutex_lock(&pinctrl_mutex); |
6e5e959d | 763 | s = pinctrl_lookup_state_locked(p, name); |
57b676f9 | 764 | mutex_unlock(&pinctrl_mutex); |
6e5e959d SW |
765 | |
766 | return s; | |
57b676f9 | 767 | } |
6e5e959d | 768 | EXPORT_SYMBOL_GPL(pinctrl_lookup_state); |
57b676f9 | 769 | |
6e5e959d SW |
770 | static int pinctrl_select_state_locked(struct pinctrl *p, |
771 | struct pinctrl_state *state) | |
befe5bdf | 772 | { |
6e5e959d SW |
773 | struct pinctrl_setting *setting, *setting2; |
774 | int ret; | |
7ecdb16f | 775 | |
6e5e959d SW |
776 | if (p->state == state) |
777 | return 0; | |
befe5bdf | 778 | |
6e5e959d SW |
779 | if (p->state) { |
780 | /* | |
781 | * The set of groups with a mux configuration in the old state | |
782 | * may not be identical to the set of groups with a mux setting | |
783 | * in the new state. While this might be unusual, it's entirely | |
784 | * possible for the "user"-supplied mapping table to be written | |
785 | * that way. For each group that was configured in the old state | |
786 | * but not in the new state, this code puts that group into a | |
787 | * safe/disabled state. | |
788 | */ | |
789 | list_for_each_entry(setting, &p->state->settings, node) { | |
790 | bool found = false; | |
1e2082b5 SW |
791 | if (setting->type != PIN_MAP_TYPE_MUX_GROUP) |
792 | continue; | |
6e5e959d | 793 | list_for_each_entry(setting2, &state->settings, node) { |
1e2082b5 SW |
794 | if (setting2->type != PIN_MAP_TYPE_MUX_GROUP) |
795 | continue; | |
796 | if (setting2->data.mux.group == | |
797 | setting->data.mux.group) { | |
6e5e959d SW |
798 | found = true; |
799 | break; | |
800 | } | |
801 | } | |
802 | if (!found) | |
803 | pinmux_disable_setting(setting); | |
804 | } | |
805 | } | |
806 | ||
807 | p->state = state; | |
808 | ||
809 | /* Apply all the settings for the new state */ | |
810 | list_for_each_entry(setting, &state->settings, node) { | |
1e2082b5 SW |
811 | switch (setting->type) { |
812 | case PIN_MAP_TYPE_MUX_GROUP: | |
813 | ret = pinmux_enable_setting(setting); | |
814 | break; | |
815 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
816 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
817 | ret = pinconf_apply_setting(setting); | |
818 | break; | |
819 | default: | |
820 | ret = -EINVAL; | |
821 | break; | |
822 | } | |
6e5e959d SW |
823 | if (ret < 0) { |
824 | /* FIXME: Difficult to return to prev state */ | |
825 | return ret; | |
826 | } | |
befe5bdf | 827 | } |
6e5e959d SW |
828 | |
829 | return 0; | |
57b676f9 SW |
830 | } |
831 | ||
832 | /** | |
6e5e959d SW |
833 | * pinctrl_select() - select/activate/program a pinctrl state to HW |
834 | * @p: the pinctrl handle for the device that requests configuratio | |
835 | * @state: the state handle to select/activate/program | |
57b676f9 | 836 | */ |
6e5e959d | 837 | int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) |
57b676f9 | 838 | { |
6e5e959d SW |
839 | int ret; |
840 | ||
57b676f9 | 841 | mutex_lock(&pinctrl_mutex); |
6e5e959d | 842 | ret = pinctrl_select_state_locked(p, state); |
57b676f9 | 843 | mutex_unlock(&pinctrl_mutex); |
6e5e959d SW |
844 | |
845 | return ret; | |
befe5bdf | 846 | } |
6e5e959d | 847 | EXPORT_SYMBOL_GPL(pinctrl_select_state); |
befe5bdf | 848 | |
6d4ca1fb SW |
849 | static void devm_pinctrl_release(struct device *dev, void *res) |
850 | { | |
851 | pinctrl_put(*(struct pinctrl **)res); | |
852 | } | |
853 | ||
854 | /** | |
855 | * struct devm_pinctrl_get() - Resource managed pinctrl_get() | |
856 | * @dev: the device to obtain the handle for | |
857 | * | |
858 | * If there is a need to explicitly destroy the returned struct pinctrl, | |
859 | * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). | |
860 | */ | |
861 | struct pinctrl *devm_pinctrl_get(struct device *dev) | |
862 | { | |
863 | struct pinctrl **ptr, *p; | |
864 | ||
865 | ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); | |
866 | if (!ptr) | |
867 | return ERR_PTR(-ENOMEM); | |
868 | ||
869 | p = pinctrl_get(dev); | |
870 | if (!IS_ERR(p)) { | |
871 | *ptr = p; | |
872 | devres_add(dev, ptr); | |
873 | } else { | |
874 | devres_free(ptr); | |
875 | } | |
876 | ||
877 | return p; | |
878 | } | |
879 | EXPORT_SYMBOL_GPL(devm_pinctrl_get); | |
880 | ||
881 | static int devm_pinctrl_match(struct device *dev, void *res, void *data) | |
882 | { | |
883 | struct pinctrl **p = res; | |
884 | ||
885 | return *p == data; | |
886 | } | |
887 | ||
888 | /** | |
889 | * devm_pinctrl_put() - Resource managed pinctrl_put() | |
890 | * @p: the pinctrl handle to release | |
891 | * | |
892 | * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally | |
893 | * this function will not need to be called and the resource management | |
894 | * code will ensure that the resource is freed. | |
895 | */ | |
896 | void devm_pinctrl_put(struct pinctrl *p) | |
897 | { | |
898 | WARN_ON(devres_destroy(p->dev, devm_pinctrl_release, | |
899 | devm_pinctrl_match, p)); | |
900 | pinctrl_put(p); | |
901 | } | |
902 | EXPORT_SYMBOL_GPL(devm_pinctrl_put); | |
903 | ||
57291ce2 SW |
904 | int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, |
905 | bool dup, bool locked) | |
befe5bdf | 906 | { |
1e2082b5 | 907 | int i, ret; |
b2b3e66e | 908 | struct pinctrl_maps *maps_node; |
befe5bdf LW |
909 | |
910 | pr_debug("add %d pinmux maps\n", num_maps); | |
911 | ||
912 | /* First sanity check the new mapping */ | |
913 | for (i = 0; i < num_maps; i++) { | |
1e2082b5 SW |
914 | if (!maps[i].dev_name) { |
915 | pr_err("failed to register map %s (%d): no device given\n", | |
916 | maps[i].name, i); | |
917 | return -EINVAL; | |
918 | } | |
919 | ||
befe5bdf LW |
920 | if (!maps[i].name) { |
921 | pr_err("failed to register map %d: no map name given\n", | |
95dcd4ae | 922 | i); |
befe5bdf LW |
923 | return -EINVAL; |
924 | } | |
925 | ||
1e2082b5 SW |
926 | if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && |
927 | !maps[i].ctrl_dev_name) { | |
befe5bdf LW |
928 | pr_err("failed to register map %s (%d): no pin control device given\n", |
929 | maps[i].name, i); | |
930 | return -EINVAL; | |
931 | } | |
932 | ||
1e2082b5 SW |
933 | switch (maps[i].type) { |
934 | case PIN_MAP_TYPE_DUMMY_STATE: | |
935 | break; | |
936 | case PIN_MAP_TYPE_MUX_GROUP: | |
937 | ret = pinmux_validate_map(&maps[i], i); | |
938 | if (ret < 0) | |
fde04f41 | 939 | return ret; |
1e2082b5 SW |
940 | break; |
941 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
942 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
943 | ret = pinconf_validate_map(&maps[i], i); | |
944 | if (ret < 0) | |
fde04f41 | 945 | return ret; |
1e2082b5 SW |
946 | break; |
947 | default: | |
948 | pr_err("failed to register map %s (%d): invalid type given\n", | |
95dcd4ae | 949 | maps[i].name, i); |
1681f5ae SW |
950 | return -EINVAL; |
951 | } | |
befe5bdf LW |
952 | } |
953 | ||
b2b3e66e SW |
954 | maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); |
955 | if (!maps_node) { | |
956 | pr_err("failed to alloc struct pinctrl_maps\n"); | |
957 | return -ENOMEM; | |
958 | } | |
befe5bdf | 959 | |
b2b3e66e | 960 | maps_node->num_maps = num_maps; |
57291ce2 SW |
961 | if (dup) { |
962 | maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, | |
963 | GFP_KERNEL); | |
964 | if (!maps_node->maps) { | |
965 | pr_err("failed to duplicate mapping table\n"); | |
966 | kfree(maps_node); | |
967 | return -ENOMEM; | |
968 | } | |
969 | } else { | |
970 | maps_node->maps = maps; | |
befe5bdf LW |
971 | } |
972 | ||
57291ce2 SW |
973 | if (!locked) |
974 | mutex_lock(&pinctrl_mutex); | |
b2b3e66e | 975 | list_add_tail(&maps_node->node, &pinctrl_maps); |
57291ce2 SW |
976 | if (!locked) |
977 | mutex_unlock(&pinctrl_mutex); | |
b2b3e66e | 978 | |
befe5bdf LW |
979 | return 0; |
980 | } | |
981 | ||
57291ce2 SW |
982 | /** |
983 | * pinctrl_register_mappings() - register a set of pin controller mappings | |
984 | * @maps: the pincontrol mappings table to register. This should probably be | |
985 | * marked with __initdata so it can be discarded after boot. This | |
986 | * function will perform a shallow copy for the mapping entries. | |
987 | * @num_maps: the number of maps in the mapping table | |
988 | */ | |
989 | int pinctrl_register_mappings(struct pinctrl_map const *maps, | |
990 | unsigned num_maps) | |
991 | { | |
992 | return pinctrl_register_map(maps, num_maps, true, false); | |
993 | } | |
994 | ||
995 | void pinctrl_unregister_map(struct pinctrl_map const *map) | |
996 | { | |
997 | struct pinctrl_maps *maps_node; | |
998 | ||
999 | list_for_each_entry(maps_node, &pinctrl_maps, node) { | |
1000 | if (maps_node->maps == map) { | |
1001 | list_del(&maps_node->node); | |
1002 | return; | |
1003 | } | |
1004 | } | |
1005 | } | |
1006 | ||
2744e8af LW |
1007 | #ifdef CONFIG_DEBUG_FS |
1008 | ||
1009 | static int pinctrl_pins_show(struct seq_file *s, void *what) | |
1010 | { | |
1011 | struct pinctrl_dev *pctldev = s->private; | |
1012 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; | |
706e8520 | 1013 | unsigned i, pin; |
2744e8af LW |
1014 | |
1015 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); | |
2744e8af | 1016 | |
57b676f9 SW |
1017 | mutex_lock(&pinctrl_mutex); |
1018 | ||
706e8520 CP |
1019 | /* The pin number can be retrived from the pin controller descriptor */ |
1020 | for (i = 0; i < pctldev->desc->npins; i++) { | |
2744e8af LW |
1021 | struct pin_desc *desc; |
1022 | ||
706e8520 | 1023 | pin = pctldev->desc->pins[i].number; |
2744e8af LW |
1024 | desc = pin_desc_get(pctldev, pin); |
1025 | /* Pin space may be sparse */ | |
1026 | if (desc == NULL) | |
1027 | continue; | |
1028 | ||
1029 | seq_printf(s, "pin %d (%s) ", pin, | |
1030 | desc->name ? desc->name : "unnamed"); | |
1031 | ||
1032 | /* Driver-specific info per pin */ | |
1033 | if (ops->pin_dbg_show) | |
1034 | ops->pin_dbg_show(pctldev, s, pin); | |
1035 | ||
1036 | seq_puts(s, "\n"); | |
1037 | } | |
1038 | ||
57b676f9 SW |
1039 | mutex_unlock(&pinctrl_mutex); |
1040 | ||
2744e8af LW |
1041 | return 0; |
1042 | } | |
1043 | ||
1044 | static int pinctrl_groups_show(struct seq_file *s, void *what) | |
1045 | { | |
1046 | struct pinctrl_dev *pctldev = s->private; | |
1047 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; | |
d1e90e9e | 1048 | unsigned ngroups, selector = 0; |
2744e8af | 1049 | |
d1e90e9e | 1050 | ngroups = ops->get_groups_count(pctldev); |
57b676f9 SW |
1051 | mutex_lock(&pinctrl_mutex); |
1052 | ||
2744e8af | 1053 | seq_puts(s, "registered pin groups:\n"); |
d1e90e9e | 1054 | while (selector < ngroups) { |
a5818a8b | 1055 | const unsigned *pins; |
2744e8af LW |
1056 | unsigned num_pins; |
1057 | const char *gname = ops->get_group_name(pctldev, selector); | |
dcb5dbc3 | 1058 | const char *pname; |
2744e8af LW |
1059 | int ret; |
1060 | int i; | |
1061 | ||
1062 | ret = ops->get_group_pins(pctldev, selector, | |
1063 | &pins, &num_pins); | |
1064 | if (ret) | |
1065 | seq_printf(s, "%s [ERROR GETTING PINS]\n", | |
1066 | gname); | |
1067 | else { | |
dcb5dbc3 DA |
1068 | seq_printf(s, "group: %s\n", gname); |
1069 | for (i = 0; i < num_pins; i++) { | |
1070 | pname = pin_get_name(pctldev, pins[i]); | |
1071 | if (WARN_ON(!pname)) | |
1072 | return -EINVAL; | |
1073 | seq_printf(s, "pin %d (%s)\n", pins[i], pname); | |
1074 | } | |
1075 | seq_puts(s, "\n"); | |
2744e8af LW |
1076 | } |
1077 | selector++; | |
1078 | } | |
1079 | ||
57b676f9 | 1080 | mutex_unlock(&pinctrl_mutex); |
2744e8af LW |
1081 | |
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) | |
1086 | { | |
1087 | struct pinctrl_dev *pctldev = s->private; | |
1088 | struct pinctrl_gpio_range *range = NULL; | |
1089 | ||
1090 | seq_puts(s, "GPIO ranges handled:\n"); | |
1091 | ||
57b676f9 SW |
1092 | mutex_lock(&pinctrl_mutex); |
1093 | ||
2744e8af | 1094 | /* Loop over the ranges */ |
2744e8af | 1095 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
75d6642a LW |
1096 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
1097 | range->id, range->name, | |
1098 | range->base, (range->base + range->npins - 1), | |
1099 | range->pin_base, | |
1100 | (range->pin_base + range->npins - 1)); | |
2744e8af | 1101 | } |
57b676f9 SW |
1102 | |
1103 | mutex_unlock(&pinctrl_mutex); | |
2744e8af LW |
1104 | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | static int pinctrl_devices_show(struct seq_file *s, void *what) | |
1109 | { | |
1110 | struct pinctrl_dev *pctldev; | |
1111 | ||
ae6b4d85 | 1112 | seq_puts(s, "name [pinmux] [pinconf]\n"); |
57b676f9 SW |
1113 | |
1114 | mutex_lock(&pinctrl_mutex); | |
1115 | ||
2744e8af LW |
1116 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
1117 | seq_printf(s, "%s ", pctldev->desc->name); | |
1118 | if (pctldev->desc->pmxops) | |
ae6b4d85 LW |
1119 | seq_puts(s, "yes "); |
1120 | else | |
1121 | seq_puts(s, "no "); | |
1122 | if (pctldev->desc->confops) | |
2744e8af LW |
1123 | seq_puts(s, "yes"); |
1124 | else | |
1125 | seq_puts(s, "no"); | |
1126 | seq_puts(s, "\n"); | |
1127 | } | |
57b676f9 SW |
1128 | |
1129 | mutex_unlock(&pinctrl_mutex); | |
2744e8af LW |
1130 | |
1131 | return 0; | |
1132 | } | |
1133 | ||
1e2082b5 SW |
1134 | static inline const char *map_type(enum pinctrl_map_type type) |
1135 | { | |
1136 | static const char * const names[] = { | |
1137 | "INVALID", | |
1138 | "DUMMY_STATE", | |
1139 | "MUX_GROUP", | |
1140 | "CONFIGS_PIN", | |
1141 | "CONFIGS_GROUP", | |
1142 | }; | |
1143 | ||
1144 | if (type >= ARRAY_SIZE(names)) | |
1145 | return "UNKNOWN"; | |
1146 | ||
1147 | return names[type]; | |
1148 | } | |
1149 | ||
3eedb437 SW |
1150 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
1151 | { | |
1152 | struct pinctrl_maps *maps_node; | |
1153 | int i; | |
1154 | struct pinctrl_map const *map; | |
1155 | ||
1156 | seq_puts(s, "Pinctrl maps:\n"); | |
1157 | ||
57b676f9 SW |
1158 | mutex_lock(&pinctrl_mutex); |
1159 | ||
3eedb437 | 1160 | for_each_maps(maps_node, i, map) { |
1e2082b5 SW |
1161 | seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", |
1162 | map->dev_name, map->name, map_type(map->type), | |
1163 | map->type); | |
1164 | ||
1165 | if (map->type != PIN_MAP_TYPE_DUMMY_STATE) | |
1166 | seq_printf(s, "controlling device %s\n", | |
1167 | map->ctrl_dev_name); | |
1168 | ||
1169 | switch (map->type) { | |
1170 | case PIN_MAP_TYPE_MUX_GROUP: | |
1171 | pinmux_show_map(s, map); | |
1172 | break; | |
1173 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
1174 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
1175 | pinconf_show_map(s, map); | |
1176 | break; | |
1177 | default: | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | seq_printf(s, "\n"); | |
3eedb437 | 1182 | } |
57b676f9 SW |
1183 | |
1184 | mutex_unlock(&pinctrl_mutex); | |
3eedb437 SW |
1185 | |
1186 | return 0; | |
1187 | } | |
1188 | ||
befe5bdf LW |
1189 | static int pinctrl_show(struct seq_file *s, void *what) |
1190 | { | |
1191 | struct pinctrl *p; | |
6e5e959d | 1192 | struct pinctrl_state *state; |
7ecdb16f | 1193 | struct pinctrl_setting *setting; |
befe5bdf LW |
1194 | |
1195 | seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); | |
57b676f9 SW |
1196 | |
1197 | mutex_lock(&pinctrl_mutex); | |
1198 | ||
befe5bdf | 1199 | list_for_each_entry(p, &pinctrl_list, node) { |
6e5e959d SW |
1200 | seq_printf(s, "device: %s current state: %s\n", |
1201 | dev_name(p->dev), | |
1202 | p->state ? p->state->name : "none"); | |
1203 | ||
1204 | list_for_each_entry(state, &p->states, node) { | |
1205 | seq_printf(s, " state: %s\n", state->name); | |
befe5bdf | 1206 | |
6e5e959d | 1207 | list_for_each_entry(setting, &state->settings, node) { |
1e2082b5 SW |
1208 | struct pinctrl_dev *pctldev = setting->pctldev; |
1209 | ||
1210 | seq_printf(s, " type: %s controller %s ", | |
1211 | map_type(setting->type), | |
1212 | pinctrl_dev_get_name(pctldev)); | |
1213 | ||
1214 | switch (setting->type) { | |
1215 | case PIN_MAP_TYPE_MUX_GROUP: | |
1216 | pinmux_show_setting(s, setting); | |
1217 | break; | |
1218 | case PIN_MAP_TYPE_CONFIGS_PIN: | |
1219 | case PIN_MAP_TYPE_CONFIGS_GROUP: | |
1220 | pinconf_show_setting(s, setting); | |
1221 | break; | |
1222 | default: | |
1223 | break; | |
1224 | } | |
6e5e959d | 1225 | } |
befe5bdf | 1226 | } |
befe5bdf LW |
1227 | } |
1228 | ||
57b676f9 SW |
1229 | mutex_unlock(&pinctrl_mutex); |
1230 | ||
befe5bdf LW |
1231 | return 0; |
1232 | } | |
1233 | ||
2744e8af LW |
1234 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
1235 | { | |
1236 | return single_open(file, pinctrl_pins_show, inode->i_private); | |
1237 | } | |
1238 | ||
1239 | static int pinctrl_groups_open(struct inode *inode, struct file *file) | |
1240 | { | |
1241 | return single_open(file, pinctrl_groups_show, inode->i_private); | |
1242 | } | |
1243 | ||
1244 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) | |
1245 | { | |
1246 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); | |
1247 | } | |
1248 | ||
1249 | static int pinctrl_devices_open(struct inode *inode, struct file *file) | |
1250 | { | |
1251 | return single_open(file, pinctrl_devices_show, NULL); | |
1252 | } | |
1253 | ||
3eedb437 SW |
1254 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
1255 | { | |
1256 | return single_open(file, pinctrl_maps_show, NULL); | |
1257 | } | |
1258 | ||
befe5bdf LW |
1259 | static int pinctrl_open(struct inode *inode, struct file *file) |
1260 | { | |
1261 | return single_open(file, pinctrl_show, NULL); | |
1262 | } | |
1263 | ||
2744e8af LW |
1264 | static const struct file_operations pinctrl_pins_ops = { |
1265 | .open = pinctrl_pins_open, | |
1266 | .read = seq_read, | |
1267 | .llseek = seq_lseek, | |
1268 | .release = single_release, | |
1269 | }; | |
1270 | ||
1271 | static const struct file_operations pinctrl_groups_ops = { | |
1272 | .open = pinctrl_groups_open, | |
1273 | .read = seq_read, | |
1274 | .llseek = seq_lseek, | |
1275 | .release = single_release, | |
1276 | }; | |
1277 | ||
1278 | static const struct file_operations pinctrl_gpioranges_ops = { | |
1279 | .open = pinctrl_gpioranges_open, | |
1280 | .read = seq_read, | |
1281 | .llseek = seq_lseek, | |
1282 | .release = single_release, | |
1283 | }; | |
1284 | ||
3eedb437 SW |
1285 | static const struct file_operations pinctrl_devices_ops = { |
1286 | .open = pinctrl_devices_open, | |
befe5bdf LW |
1287 | .read = seq_read, |
1288 | .llseek = seq_lseek, | |
1289 | .release = single_release, | |
1290 | }; | |
1291 | ||
3eedb437 SW |
1292 | static const struct file_operations pinctrl_maps_ops = { |
1293 | .open = pinctrl_maps_open, | |
2744e8af LW |
1294 | .read = seq_read, |
1295 | .llseek = seq_lseek, | |
1296 | .release = single_release, | |
1297 | }; | |
1298 | ||
befe5bdf LW |
1299 | static const struct file_operations pinctrl_ops = { |
1300 | .open = pinctrl_open, | |
1301 | .read = seq_read, | |
1302 | .llseek = seq_lseek, | |
1303 | .release = single_release, | |
1304 | }; | |
1305 | ||
2744e8af LW |
1306 | static struct dentry *debugfs_root; |
1307 | ||
1308 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) | |
1309 | { | |
02157160 | 1310 | struct dentry *device_root; |
2744e8af | 1311 | |
51cd24ee | 1312 | device_root = debugfs_create_dir(dev_name(pctldev->dev), |
2744e8af | 1313 | debugfs_root); |
02157160 TL |
1314 | pctldev->device_root = device_root; |
1315 | ||
2744e8af LW |
1316 | if (IS_ERR(device_root) || !device_root) { |
1317 | pr_warn("failed to create debugfs directory for %s\n", | |
51cd24ee | 1318 | dev_name(pctldev->dev)); |
2744e8af LW |
1319 | return; |
1320 | } | |
1321 | debugfs_create_file("pins", S_IFREG | S_IRUGO, | |
1322 | device_root, pctldev, &pinctrl_pins_ops); | |
1323 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, | |
1324 | device_root, pctldev, &pinctrl_groups_ops); | |
1325 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, | |
1326 | device_root, pctldev, &pinctrl_gpioranges_ops); | |
1327 | pinmux_init_device_debugfs(device_root, pctldev); | |
ae6b4d85 | 1328 | pinconf_init_device_debugfs(device_root, pctldev); |
2744e8af LW |
1329 | } |
1330 | ||
02157160 TL |
1331 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
1332 | { | |
1333 | debugfs_remove_recursive(pctldev->device_root); | |
1334 | } | |
1335 | ||
2744e8af LW |
1336 | static void pinctrl_init_debugfs(void) |
1337 | { | |
1338 | debugfs_root = debugfs_create_dir("pinctrl", NULL); | |
1339 | if (IS_ERR(debugfs_root) || !debugfs_root) { | |
1340 | pr_warn("failed to create debugfs directory\n"); | |
1341 | debugfs_root = NULL; | |
1342 | return; | |
1343 | } | |
1344 | ||
1345 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, | |
1346 | debugfs_root, NULL, &pinctrl_devices_ops); | |
3eedb437 SW |
1347 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
1348 | debugfs_root, NULL, &pinctrl_maps_ops); | |
befe5bdf LW |
1349 | debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, |
1350 | debugfs_root, NULL, &pinctrl_ops); | |
2744e8af LW |
1351 | } |
1352 | ||
1353 | #else /* CONFIG_DEBUG_FS */ | |
1354 | ||
1355 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) | |
1356 | { | |
1357 | } | |
1358 | ||
1359 | static void pinctrl_init_debugfs(void) | |
1360 | { | |
1361 | } | |
1362 | ||
02157160 TL |
1363 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
1364 | { | |
1365 | } | |
1366 | ||
2744e8af LW |
1367 | #endif |
1368 | ||
d26bc49f SW |
1369 | static int pinctrl_check_ops(struct pinctrl_dev *pctldev) |
1370 | { | |
1371 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; | |
1372 | ||
1373 | if (!ops || | |
d1e90e9e | 1374 | !ops->get_groups_count || |
d26bc49f SW |
1375 | !ops->get_group_name || |
1376 | !ops->get_group_pins) | |
1377 | return -EINVAL; | |
1378 | ||
57291ce2 SW |
1379 | if (ops->dt_node_to_map && !ops->dt_free_map) |
1380 | return -EINVAL; | |
1381 | ||
d26bc49f SW |
1382 | return 0; |
1383 | } | |
1384 | ||
2744e8af LW |
1385 | /** |
1386 | * pinctrl_register() - register a pin controller device | |
1387 | * @pctldesc: descriptor for this pin controller | |
1388 | * @dev: parent device for this pin controller | |
1389 | * @driver_data: private pin controller data for this pin controller | |
1390 | */ | |
1391 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, | |
1392 | struct device *dev, void *driver_data) | |
1393 | { | |
2744e8af LW |
1394 | struct pinctrl_dev *pctldev; |
1395 | int ret; | |
1396 | ||
1397 | if (pctldesc == NULL) | |
1398 | return NULL; | |
1399 | if (pctldesc->name == NULL) | |
1400 | return NULL; | |
1401 | ||
02f5b989 | 1402 | pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); |
95dcd4ae SW |
1403 | if (pctldev == NULL) { |
1404 | dev_err(dev, "failed to alloc struct pinctrl_dev\n"); | |
b9130b77 | 1405 | return NULL; |
95dcd4ae | 1406 | } |
b9130b77 TL |
1407 | |
1408 | /* Initialize pin control device struct */ | |
1409 | pctldev->owner = pctldesc->owner; | |
1410 | pctldev->desc = pctldesc; | |
1411 | pctldev->driver_data = driver_data; | |
1412 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); | |
b9130b77 | 1413 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
b9130b77 TL |
1414 | pctldev->dev = dev; |
1415 | ||
d26bc49f SW |
1416 | /* check core ops for sanity */ |
1417 | ret = pinctrl_check_ops(pctldev); | |
1418 | if (ret) { | |
ad6e1107 | 1419 | dev_err(dev, "pinctrl ops lacks necessary functions\n"); |
d26bc49f SW |
1420 | goto out_err; |
1421 | } | |
1422 | ||
2744e8af LW |
1423 | /* If we're implementing pinmuxing, check the ops for sanity */ |
1424 | if (pctldesc->pmxops) { | |
b9130b77 | 1425 | ret = pinmux_check_ops(pctldev); |
ad6e1107 | 1426 | if (ret) |
b9130b77 | 1427 | goto out_err; |
2744e8af LW |
1428 | } |
1429 | ||
ae6b4d85 LW |
1430 | /* If we're implementing pinconfig, check the ops for sanity */ |
1431 | if (pctldesc->confops) { | |
b9130b77 | 1432 | ret = pinconf_check_ops(pctldev); |
ad6e1107 | 1433 | if (ret) |
b9130b77 | 1434 | goto out_err; |
ae6b4d85 LW |
1435 | } |
1436 | ||
2744e8af | 1437 | /* Register all the pins */ |
ad6e1107 | 1438 | dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); |
2744e8af LW |
1439 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
1440 | if (ret) { | |
ad6e1107 | 1441 | dev_err(dev, "error during pin registration\n"); |
2744e8af LW |
1442 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
1443 | pctldesc->npins); | |
51cd24ee | 1444 | goto out_err; |
2744e8af LW |
1445 | } |
1446 | ||
57b676f9 SW |
1447 | mutex_lock(&pinctrl_mutex); |
1448 | ||
8b9c139f | 1449 | list_add_tail(&pctldev->node, &pinctrldev_list); |
57b676f9 | 1450 | |
6e5e959d SW |
1451 | pctldev->p = pinctrl_get_locked(pctldev->dev); |
1452 | if (!IS_ERR(pctldev->p)) { | |
1453 | struct pinctrl_state *s = | |
1454 | pinctrl_lookup_state_locked(pctldev->p, | |
1455 | PINCTRL_STATE_DEFAULT); | |
ad6e1107 JC |
1456 | if (IS_ERR(s)) { |
1457 | dev_dbg(dev, "failed to lookup the default state\n"); | |
1458 | } else { | |
1459 | ret = pinctrl_select_state_locked(pctldev->p, s); | |
1460 | if (ret) { | |
1461 | dev_err(dev, | |
1462 | "failed to select default state\n"); | |
1463 | } | |
1464 | } | |
6e5e959d | 1465 | } |
57b676f9 SW |
1466 | |
1467 | mutex_unlock(&pinctrl_mutex); | |
1468 | ||
2304b473 SW |
1469 | pinctrl_init_device_debugfs(pctldev); |
1470 | ||
2744e8af LW |
1471 | return pctldev; |
1472 | ||
51cd24ee SW |
1473 | out_err: |
1474 | kfree(pctldev); | |
2744e8af LW |
1475 | return NULL; |
1476 | } | |
1477 | EXPORT_SYMBOL_GPL(pinctrl_register); | |
1478 | ||
1479 | /** | |
1480 | * pinctrl_unregister() - unregister pinmux | |
1481 | * @pctldev: pin controller to unregister | |
1482 | * | |
1483 | * Called by pinmux drivers to unregister a pinmux. | |
1484 | */ | |
1485 | void pinctrl_unregister(struct pinctrl_dev *pctldev) | |
1486 | { | |
1487 | if (pctldev == NULL) | |
1488 | return; | |
1489 | ||
02157160 | 1490 | pinctrl_remove_device_debugfs(pctldev); |
57b676f9 SW |
1491 | |
1492 | mutex_lock(&pinctrl_mutex); | |
1493 | ||
6e5e959d SW |
1494 | if (!IS_ERR(pctldev->p)) |
1495 | pinctrl_put_locked(pctldev->p, true); | |
57b676f9 | 1496 | |
2744e8af | 1497 | /* TODO: check that no pinmuxes are still active? */ |
2744e8af | 1498 | list_del(&pctldev->node); |
2744e8af LW |
1499 | /* Destroy descriptor tree */ |
1500 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, | |
1501 | pctldev->desc->npins); | |
51cd24ee | 1502 | kfree(pctldev); |
57b676f9 SW |
1503 | |
1504 | mutex_unlock(&pinctrl_mutex); | |
2744e8af LW |
1505 | } |
1506 | EXPORT_SYMBOL_GPL(pinctrl_unregister); | |
1507 | ||
1508 | static int __init pinctrl_init(void) | |
1509 | { | |
1510 | pr_info("initialized pinctrl subsystem\n"); | |
1511 | pinctrl_init_debugfs(); | |
1512 | return 0; | |
1513 | } | |
1514 | ||
1515 | /* init early since many drivers really need to initialized pinmux early */ | |
1516 | core_initcall(pinctrl_init); |