blob: 3ffa9324ed82f3a9b9c89b1713e909de1eb3fe19 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
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>
Linus Walleij97607d12011-11-29 12:52:39 +010024#include <linux/string.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020025#include <linux/sysfs.h>
26#include <linux/debugfs.h>
27#include <linux/seq_file.h>
28#include <linux/pinctrl/machine.h>
29#include <linux/pinctrl/pinmux.h>
30#include "core.h"
31
32/* List of pinmuxes */
33static DEFINE_MUTEX(pinmux_list_mutex);
34static LIST_HEAD(pinmux_list);
35
Linus Walleij59b099b2011-11-30 13:28:14 +010036/* Global pinmux maps */
Linus Walleij97607d12011-11-29 12:52:39 +010037static struct pinmux_map *pinmux_maps;
Linus Walleij2744e8a2011-05-02 20:50:54 +020038static unsigned pinmux_maps_num;
39
40/**
41 * struct pinmux_group - group list item for pinmux groups
42 * @node: pinmux group list node
43 * @group_selector: the group selector for this group
44 */
45struct pinmux_group {
46 struct list_head node;
47 unsigned group_selector;
48};
49
50/**
51 * struct pinmux - per-device pinmux state holder
52 * @node: global list node
53 * @dev: the device using this pinmux
54 * @usecount: the number of active users of this mux setting, used to keep
55 * track of nested use cases
Linus Walleij2744e8a2011-05-02 20:50:54 +020056 * @pctldev: pin control device handling this pinmux
57 * @func_selector: the function selector for the pinmux device handling
58 * this pinmux
59 * @groups: the group selectors for the pinmux device and
60 * selector combination handling this pinmux, this is a list that
61 * will be traversed on all pinmux operations such as
62 * get/put/enable/disable
63 * @mutex: a lock for the pinmux state holder
64 */
65struct pinmux {
66 struct list_head node;
67 struct device *dev;
68 unsigned usecount;
69 struct pinctrl_dev *pctldev;
70 unsigned func_selector;
71 struct list_head groups;
72 struct mutex mutex;
73};
74
75/**
76 * struct pinmux_hog - a list item to stash mux hogs
77 * @node: pinmux hog list node
78 * @map: map entry responsible for this hogging
79 * @pmx: the pinmux hogged by this item
80 */
81struct pinmux_hog {
82 struct list_head node;
83 struct pinmux_map const *map;
84 struct pinmux *pmx;
85};
86
87/**
88 * pin_request() - request a single pin to be muxed in, typically for GPIO
89 * @pin: the pin number in the global pin space
90 * @function: a functional name to give to this pin, passed to the driver
91 * so it knows what function to mux in, e.g. the string "gpioNN"
92 * means that you want to mux in the pin for use as GPIO number NN
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 * @gpio_range: the range matching the GPIO pin if this is a request for a
94 * single GPIO pin
95 */
96static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3712a3c2011-10-21 12:25:53 -060097 int pin, const char *function,
Linus Walleij2744e8a2011-05-02 20:50:54 +020098 struct pinctrl_gpio_range *gpio_range)
99{
100 struct pin_desc *desc;
101 const struct pinmux_ops *ops = pctldev->desc->pmxops;
102 int status = -EINVAL;
103
Stephen Warren51cd24e2011-12-09 16:59:05 -0700104 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105
Linus Walleij2744e8a2011-05-02 20:50:54 +0200106 desc = pin_desc_get(pctldev, pin);
107 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700108 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 "pin is not registered so it cannot be requested\n");
110 goto out;
111 }
112
Marek Beliskod2f6a1c2011-10-26 22:57:20 +0200113 if (!function) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700114 dev_err(pctldev->dev, "no function name given\n");
Marek Beliskod2f6a1c2011-10-26 22:57:20 +0200115 return -EINVAL;
116 }
117
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600119 if (desc->mux_function) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120 spin_unlock(&desc->lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700121 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200122 "pin already requested\n");
123 goto out;
124 }
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600125 desc->mux_function = function;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200126 spin_unlock(&desc->lock);
127
128 /* Let each pin increase references to this module */
129 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700130 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200131 "could not increase module refcount for pin %d\n",
132 pin);
133 status = -EINVAL;
134 goto out_free_pin;
135 }
136
137 /*
138 * If there is no kind of request function for the pin we just assume
139 * we got it by default and proceed.
140 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600141 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200142 /* This requests and enables a single GPIO pin */
143 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
144 else if (ops->request)
145 status = ops->request(pctldev, pin);
146 else
147 status = 0;
148
149 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100150 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200151 pctldev->desc->name, pin);
152out_free_pin:
153 if (status) {
154 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600155 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 spin_unlock(&desc->lock);
157 }
158out:
159 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700160 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200161 pin, function ? : "?", status);
162
163 return status;
164}
165
166/**
167 * pin_free() - release a single muxed in pin so something else can be muxed
168 * @pctldev: pin controller device handling this pin
169 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600170 * @gpio_range: the range matching the GPIO pin if this is a request for a
171 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100172 *
173 * This function returns a pointer to the function name in use. This is used
174 * for callers that dynamically allocate a function name so it can be freed
175 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200176 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600177static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
178 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200179{
180 const struct pinmux_ops *ops = pctldev->desc->pmxops;
181 struct pin_desc *desc;
Stephen Warren3712a3c2011-10-21 12:25:53 -0600182 const char *func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183
184 desc = pin_desc_get(pctldev, pin);
185 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700186 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600188 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189 }
190
Stephen Warren3712a3c2011-10-21 12:25:53 -0600191 /*
192 * If there is no kind of request function for the pin we just assume
193 * we got it by default and proceed.
194 */
195 if (gpio_range && ops->gpio_disable_free)
196 ops->gpio_disable_free(pctldev, gpio_range, pin);
197 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 ops->free(pctldev, pin);
199
200 spin_lock(&desc->lock);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600201 func = desc->mux_function;
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600202 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 spin_unlock(&desc->lock);
204 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600205
206 return func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207}
208
209/**
210 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
211 * @gpio: the GPIO pin number from the GPIO subsystem number space
Linus Walleij542e7042011-11-14 10:06:22 +0100212 *
213 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
214 * as part of their gpio_request() semantics, platforms and individual drivers
215 * shall *NOT* request GPIO pins to be muxed in.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216 */
217int pinmux_request_gpio(unsigned gpio)
218{
219 char gpiostr[16];
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600220 const char *function;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 struct pinctrl_dev *pctldev;
222 struct pinctrl_gpio_range *range;
223 int ret;
224 int pin;
225
226 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
227 if (ret)
228 return -EINVAL;
229
230 /* Convert to the pin controllers number space */
Chanho Park3c739ad2011-11-11 18:47:58 +0900231 pin = gpio - range->base + range->pin_base;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
233 /* Conjure some name stating what chip and pin this is taken by */
234 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
235
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600236 function = kstrdup(gpiostr, GFP_KERNEL);
237 if (!function)
238 return -EINVAL;
239
Stephen Warren3712a3c2011-10-21 12:25:53 -0600240 ret = pin_request(pctldev, pin, function, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600241 if (ret < 0)
242 kfree(function);
243
244 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245}
246EXPORT_SYMBOL_GPL(pinmux_request_gpio);
247
248/**
249 * pinmux_free_gpio() - free a single pin, currently used as GPIO
250 * @gpio: the GPIO pin number from the GPIO subsystem number space
Linus Walleij542e7042011-11-14 10:06:22 +0100251 *
252 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
253 * as part of their gpio_free() semantics, platforms and individual drivers
254 * shall *NOT* request GPIO pins to be muxed out.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200255 */
256void pinmux_free_gpio(unsigned gpio)
257{
258 struct pinctrl_dev *pctldev;
259 struct pinctrl_gpio_range *range;
260 int ret;
261 int pin;
Stephen Warren3712a3c2011-10-21 12:25:53 -0600262 const char *func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263
264 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
265 if (ret)
266 return;
267
268 /* Convert to the pin controllers number space */
Chanho Park3c739ad2011-11-11 18:47:58 +0900269 pin = gpio - range->base + range->pin_base;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200270
Stephen Warren3712a3c2011-10-21 12:25:53 -0600271 func = pin_free(pctldev, pin, range);
272 kfree(func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200273}
274EXPORT_SYMBOL_GPL(pinmux_free_gpio);
275
Linus Walleij542e7042011-11-14 10:06:22 +0100276static int pinmux_gpio_direction(unsigned gpio, bool input)
277{
278 struct pinctrl_dev *pctldev;
279 struct pinctrl_gpio_range *range;
280 const struct pinmux_ops *ops;
281 int ret;
282 int pin;
283
284 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
285 if (ret)
286 return ret;
287
288 ops = pctldev->desc->pmxops;
289
290 /* Convert to the pin controllers number space */
291 pin = gpio - range->base + range->pin_base;
292
293 if (ops->gpio_set_direction)
294 ret = ops->gpio_set_direction(pctldev, range, pin, input);
295 else
296 ret = 0;
297
298 return ret;
299}
300
301/**
302 * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode
303 * @gpio: the GPIO pin number from the GPIO subsystem number space
304 *
305 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
306 * as part of their gpio_direction_input() semantics, platforms and individual
307 * drivers shall *NOT* touch pinmux GPIO calls.
308 */
309int pinmux_gpio_direction_input(unsigned gpio)
310{
311 return pinmux_gpio_direction(gpio, true);
312}
313EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input);
314
315/**
316 * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode
317 * @gpio: the GPIO pin number from the GPIO subsystem number space
318 *
319 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
320 * as part of their gpio_direction_output() semantics, platforms and individual
321 * drivers shall *NOT* touch pinmux GPIO calls.
322 */
323int pinmux_gpio_direction_output(unsigned gpio)
324{
325 return pinmux_gpio_direction(gpio, false);
326}
327EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output);
328
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329/**
330 * pinmux_register_mappings() - register a set of pinmux mappings
Linus Walleij97607d12011-11-29 12:52:39 +0100331 * @maps: the pinmux mappings table to register, this should be marked with
332 * __initdata so it can be discarded after boot, this function will
333 * perform a shallow copy for the mapping entries.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334 * @num_maps: the number of maps in the mapping table
335 *
336 * Only call this once during initialization of your machine, the function is
337 * tagged as __init and won't be callable after init has completed. The map
338 * passed into this function will be owned by the pinmux core and cannot be
Dong Aishenge6337c32011-12-20 17:51:59 +0800339 * freed.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200340 */
341int __init pinmux_register_mappings(struct pinmux_map const *maps,
342 unsigned num_maps)
343{
Linus Walleij59b099b2011-11-30 13:28:14 +0100344 void *tmp_maps;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200345 int i;
346
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347 pr_debug("add %d pinmux maps\n", num_maps);
Linus Walleij97607d12011-11-29 12:52:39 +0100348
Linus Walleij59b099b2011-11-30 13:28:14 +0100349 /* First sanity check the new mapping */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200350 for (i = 0; i < num_maps; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200351 if (!maps[i].name) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100352 pr_err("failed to register map %d: no map name given\n",
353 i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100354 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355 }
Linus Walleij97607d12011-11-29 12:52:39 +0100356
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100358 pr_err("failed to register map %s (%d): no pin control device given\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200359 maps[i].name, i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100360 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200361 }
Linus Walleij97607d12011-11-29 12:52:39 +0100362
Linus Walleij2744e8a2011-05-02 20:50:54 +0200363 if (!maps[i].function) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100364 pr_err("failed to register map %s (%d): no function ID given\n",
365 maps[i].name, i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100366 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 }
368
369 if (!maps[i].dev && !maps[i].dev_name)
370 pr_debug("add system map %s function %s with no device\n",
371 maps[i].name,
372 maps[i].function);
373 else
374 pr_debug("register map %s, function %s\n",
375 maps[i].name,
376 maps[i].function);
377 }
378
Linus Walleij59b099b2011-11-30 13:28:14 +0100379 /*
380 * Make a copy of the map array - string pointers will end up in the
381 * kernel const section anyway so these do not need to be deep copied.
382 */
383 if (!pinmux_maps_num) {
384 /* On first call, just copy them */
385 tmp_maps = kmemdup(maps,
386 sizeof(struct pinmux_map) * num_maps,
387 GFP_KERNEL);
388 if (!tmp_maps)
389 return -ENOMEM;
390 } else {
391 /* Subsequent calls, reallocate array to new size */
392 size_t oldsize = sizeof(struct pinmux_map) * pinmux_maps_num;
393 size_t newsize = sizeof(struct pinmux_map) * num_maps;
Linus Walleij97607d12011-11-29 12:52:39 +0100394
Linus Walleij59b099b2011-11-30 13:28:14 +0100395 tmp_maps = krealloc(pinmux_maps, oldsize + newsize, GFP_KERNEL);
396 if (!tmp_maps)
397 return -ENOMEM;
398 memcpy((tmp_maps + oldsize), maps, newsize);
399 }
400
401 pinmux_maps = tmp_maps;
402 pinmux_maps_num += num_maps;
403 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200404}
405
406/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800407 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200408 * @pctldev: the device to take the pins on
409 * @func_selector: the function selector to acquire the pins for
410 * @group_selector: the group selector containing the pins to acquire
411 */
412static int acquire_pins(struct pinctrl_dev *pctldev,
413 unsigned func_selector,
414 unsigned group_selector)
415{
416 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
417 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
418 const char *func = pmxops->get_function_name(pctldev,
419 func_selector);
Stephen Warrena5818a82011-10-19 16:19:25 -0600420 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200421 unsigned num_pins;
422 int ret;
423 int i;
424
425 ret = pctlops->get_group_pins(pctldev, group_selector,
426 &pins, &num_pins);
427 if (ret)
428 return ret;
429
Stephen Warren51cd24e2011-12-09 16:59:05 -0700430 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200431 num_pins, group_selector);
432
433 /* Try to allocate all pins in this group, one by one */
434 for (i = 0; i < num_pins; i++) {
Stephen Warren3712a3c2011-10-21 12:25:53 -0600435 ret = pin_request(pctldev, pins[i], func, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200436 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700437 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100438 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200439 pins[i], func ? : "(undefined)",
440 pinctrl_dev_get_name(pctldev));
441 /* On error release all taken pins */
442 i--; /* this pin just failed */
443 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600444 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200445 return -ENODEV;
446 }
447 }
448 return 0;
449}
450
451/**
452 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800453 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200454 * @group_selector: the group selector containing the pins to free
455 */
456static void release_pins(struct pinctrl_dev *pctldev,
457 unsigned group_selector)
458{
459 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600460 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200461 unsigned num_pins;
462 int ret;
463 int i;
464
465 ret = pctlops->get_group_pins(pctldev, group_selector,
466 &pins, &num_pins);
467 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100468 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200469 group_selector);
470 return;
471 }
472 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600473 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200474}
475
476/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200477 * pinmux_check_pin_group() - check function and pin group combo
478 * @pctldev: device to check the pin group vs function for
479 * @func_selector: the function selector to check the pin group for, we have
480 * already looked this up in the calling function
481 * @pin_group: the pin group to match to the function
482 *
483 * This function will check that the pinmux driver can supply the
484 * selected pin group for a certain function, returns the group selector if
485 * the group and function selector will work fine together, else returns
486 * negative
487 */
488static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
489 unsigned func_selector,
490 const char *pin_group)
491{
492 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
493 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
494 int ret;
495
496 /*
497 * If the driver does not support different pin groups for the
498 * functions, we only support group 0, and assume this exists.
499 */
500 if (!pctlops || !pctlops->list_groups)
501 return 0;
502
503 /*
504 * Passing NULL (no specific group) will select the first and
505 * hopefully only group of pins available for this function.
506 */
507 if (!pin_group) {
508 char const * const *groups;
509 unsigned num_groups;
510
511 ret = pmxops->get_function_groups(pctldev, func_selector,
512 &groups, &num_groups);
513 if (ret)
514 return ret;
515 if (num_groups < 1)
516 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200517 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200518 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700519 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100520 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200521 pmxops->get_function_name(pctldev, func_selector),
522 groups[0]);
523 return ret;
524 }
525
526 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700527 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100528 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200529 pmxops->get_function_name(pctldev, func_selector),
530 groups[0],
531 ret);
532
533 return ret;
534 }
535
Stephen Warren51cd24e2011-12-09 16:59:05 -0700536 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200537 "check if we have pin group %s on controller %s\n",
538 pin_group, pinctrl_dev_get_name(pctldev));
539
Linus Walleij7afde8b2011-10-19 17:07:16 +0200540 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200541 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700542 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200543 "%s does not support pin group %s with function %s\n",
544 pinctrl_dev_get_name(pctldev),
545 pin_group,
546 pmxops->get_function_name(pctldev, func_selector));
547 }
548 return ret;
549}
550
551/**
552 * pinmux_search_function() - check pin control driver for a certain function
553 * @pctldev: device to check for function and position
554 * @map: function map containing the function and position to look for
555 * @func_selector: returns the applicable function selector if found
556 * @group_selector: returns the applicable group selector if found
557 *
558 * This will search the pinmux driver for an applicable
559 * function with a specific pin group, returns 0 if these can be mapped
560 * negative otherwise
561 */
562static int pinmux_search_function(struct pinctrl_dev *pctldev,
563 struct pinmux_map const *map,
564 unsigned *func_selector,
565 unsigned *group_selector)
566{
567 const struct pinmux_ops *ops = pctldev->desc->pmxops;
568 unsigned selector = 0;
569
570 /* See if this pctldev has this function */
571 while (ops->list_functions(pctldev, selector) >= 0) {
572 const char *fname = ops->get_function_name(pctldev,
573 selector);
574 int ret;
575
576 if (!strcmp(map->function, fname)) {
577 /* Found the function, check pin group */
578 ret = pinmux_check_pin_group(pctldev, selector,
579 map->group);
580 if (ret < 0)
581 return ret;
582
583 /* This function and group selector can be used */
584 *func_selector = selector;
585 *group_selector = ret;
586 return 0;
587
588 }
589 selector++;
590 }
591
592 pr_err("%s does not support function %s\n",
593 pinctrl_dev_get_name(pctldev), map->function);
594 return -EINVAL;
595}
596
597/**
598 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
599 */
600static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
601 struct pinmux *pmx,
602 struct device *dev,
603 const char *devname,
604 struct pinmux_map const *map)
605{
606 unsigned func_selector;
607 unsigned group_selector;
608 struct pinmux_group *grp;
609 int ret;
610
611 /*
612 * Note that we're not locking the pinmux mutex here, because
613 * this is only called at pinmux initialization time when it
614 * has not been added to any list and thus is not reachable
615 * by anyone else.
616 */
617
618 if (pmx->pctldev && pmx->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700619 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100620 "different pin control devices given for device %s, function %s\n",
621 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200622 return -EINVAL;
623 }
624 pmx->dev = dev;
625 pmx->pctldev = pctldev;
626
627 /* Now go into the driver and try to match a function and group */
628 ret = pinmux_search_function(pctldev, map, &func_selector,
629 &group_selector);
630 if (ret < 0)
631 return ret;
632
633 /*
634 * If the function selector is already set, it needs to be identical,
635 * we support several groups with one function but not several
636 * functions with one or several groups in the same pinmux.
637 */
638 if (pmx->func_selector != UINT_MAX &&
639 pmx->func_selector != func_selector) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700640 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200641 "dual function defines in the map for device %s\n",
642 devname);
643 return -EINVAL;
644 }
645 pmx->func_selector = func_selector;
646
647 /* Now add this group selector, we may have many of them */
648 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
649 if (!grp)
650 return -ENOMEM;
651 grp->group_selector = group_selector;
652 ret = acquire_pins(pctldev, func_selector, group_selector);
653 if (ret) {
654 kfree(grp);
655 return ret;
656 }
657 list_add(&grp->node, &pmx->groups);
658
659 return 0;
660}
661
662static void pinmux_free_groups(struct pinmux *pmx)
663{
664 struct list_head *node, *tmp;
665
666 list_for_each_safe(node, tmp, &pmx->groups) {
667 struct pinmux_group *grp =
668 list_entry(node, struct pinmux_group, node);
669 /* Release all pins taken by this group */
670 release_pins(pmx->pctldev, grp->group_selector);
671 list_del(node);
672 kfree(grp);
673 }
674}
675
676/**
677 * pinmux_get() - retrieves the pinmux for a certain device
678 * @dev: the device to get the pinmux for
679 * @name: an optional specific mux mapping name or NULL, the name is only
680 * needed if you want to have more than one mapping per device, or if you
681 * need an anonymous pinmux (not tied to any specific device)
682 */
683struct pinmux *pinmux_get(struct device *dev, const char *name)
684{
Linus Walleij2744e8a2011-05-02 20:50:54 +0200685 struct pinmux_map const *map = NULL;
686 struct pinctrl_dev *pctldev = NULL;
687 const char *devname = NULL;
688 struct pinmux *pmx;
689 bool found_map;
690 unsigned num_maps = 0;
691 int ret = -ENODEV;
692 int i;
693
694 /* We must have dev or ID or both */
695 if (!dev && !name)
696 return ERR_PTR(-EINVAL);
697
698 if (dev)
699 devname = dev_name(dev);
700
701 pr_debug("get mux %s for device %s\n", name,
702 devname ? devname : "(none)");
703
704 /*
705 * create the state cookie holder struct pinmux for each
706 * mapping, this is what consumers will get when requesting
707 * a pinmux handle with pinmux_get()
708 */
709 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
710 if (pmx == NULL)
711 return ERR_PTR(-ENOMEM);
712 mutex_init(&pmx->mutex);
713 pmx->func_selector = UINT_MAX;
714 INIT_LIST_HEAD(&pmx->groups);
715
716 /* Iterate over the pinmux maps to locate the right ones */
717 for (i = 0; i < pinmux_maps_num; i++) {
718 map = &pinmux_maps[i];
719 found_map = false;
720
721 /*
722 * First, try to find the pctldev given in the map
723 */
724 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
725 map->ctrl_dev_name);
726 if (!pctldev) {
727 const char *devname = NULL;
728
729 if (map->ctrl_dev)
730 devname = dev_name(map->ctrl_dev);
731 else if (map->ctrl_dev_name)
732 devname = map->ctrl_dev_name;
733
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100734 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200735 map->function);
736 pr_warning("given pinctrl device name: %s",
737 devname ? devname : "UNDEFINED");
738
739 /* Continue to check the other mappings anyway... */
740 continue;
741 }
742
743 pr_debug("in map, found pctldev %s to handle function %s",
Stephen Warren51cd24e2011-12-09 16:59:05 -0700744 dev_name(pctldev->dev), map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200745
746
747 /*
748 * If we're looking for a specific named map, this must match,
749 * else we loop and look for the next.
750 */
751 if (name != NULL) {
752 if (map->name == NULL)
753 continue;
754 if (strcmp(map->name, name))
755 continue;
756 }
757
758 /*
759 * This is for the case where no device name is given, we
760 * already know that the function name matches from above
761 * code.
762 */
763 if (!map->dev_name && (name != NULL))
764 found_map = true;
765
766 /* If the mapping has a device set up it must match */
767 if (map->dev_name &&
768 (!devname || !strcmp(map->dev_name, devname)))
769 /* MATCH! */
770 found_map = true;
771
772 /* If this map is applicable, then apply it */
773 if (found_map) {
774 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
775 devname, map);
776 if (ret) {
777 pinmux_free_groups(pmx);
778 kfree(pmx);
779 return ERR_PTR(ret);
780 }
781 num_maps++;
782 }
783 }
784
785
786 /* We should have atleast one map, right */
787 if (!num_maps) {
788 pr_err("could not find any mux maps for device %s, ID %s\n",
789 devname ? devname : "(anonymous)",
790 name ? name : "(undefined)");
791 kfree(pmx);
792 return ERR_PTR(-EINVAL);
793 }
794
795 pr_debug("found %u mux maps for device %s, UD %s\n",
796 num_maps,
797 devname ? devname : "(anonymous)",
798 name ? name : "(undefined)");
799
800 /* Add the pinmux to the global list */
801 mutex_lock(&pinmux_list_mutex);
802 list_add(&pmx->node, &pinmux_list);
803 mutex_unlock(&pinmux_list_mutex);
804
805 return pmx;
806}
807EXPORT_SYMBOL_GPL(pinmux_get);
808
809/**
810 * pinmux_put() - release a previously claimed pinmux
811 * @pmx: a pinmux previously claimed by pinmux_get()
812 */
813void pinmux_put(struct pinmux *pmx)
814{
815 if (pmx == NULL)
816 return;
817
818 mutex_lock(&pmx->mutex);
819 if (pmx->usecount)
820 pr_warn("releasing pinmux with active users!\n");
821 /* Free the groups and all acquired pins */
822 pinmux_free_groups(pmx);
823 mutex_unlock(&pmx->mutex);
824
825 /* Remove from list */
826 mutex_lock(&pinmux_list_mutex);
827 list_del(&pmx->node);
828 mutex_unlock(&pinmux_list_mutex);
829
830 kfree(pmx);
831}
832EXPORT_SYMBOL_GPL(pinmux_put);
833
834/**
835 * pinmux_enable() - enable a certain pinmux setting
836 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
837 */
838int pinmux_enable(struct pinmux *pmx)
839{
840 int ret = 0;
841
842 if (pmx == NULL)
843 return -EINVAL;
844 mutex_lock(&pmx->mutex);
845 if (pmx->usecount++ == 0) {
846 struct pinctrl_dev *pctldev = pmx->pctldev;
847 const struct pinmux_ops *ops = pctldev->desc->pmxops;
848 struct pinmux_group *grp;
849
850 list_for_each_entry(grp, &pmx->groups, node) {
851 ret = ops->enable(pctldev, pmx->func_selector,
852 grp->group_selector);
853 if (ret) {
854 /*
855 * TODO: call disable() on all groups we called
856 * enable() on to this point?
857 */
858 pmx->usecount--;
859 break;
860 }
861 }
862 }
863 mutex_unlock(&pmx->mutex);
864 return ret;
865}
866EXPORT_SYMBOL_GPL(pinmux_enable);
867
868/**
869 * pinmux_disable() - disable a certain pinmux setting
870 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
871 */
872void pinmux_disable(struct pinmux *pmx)
873{
874 if (pmx == NULL)
875 return;
876
877 mutex_lock(&pmx->mutex);
878 if (--pmx->usecount == 0) {
879 struct pinctrl_dev *pctldev = pmx->pctldev;
880 const struct pinmux_ops *ops = pctldev->desc->pmxops;
881 struct pinmux_group *grp;
882
883 list_for_each_entry(grp, &pmx->groups, node) {
884 ops->disable(pctldev, pmx->func_selector,
885 grp->group_selector);
886 }
887 }
888 mutex_unlock(&pmx->mutex);
889}
890EXPORT_SYMBOL_GPL(pinmux_disable);
891
892int pinmux_check_ops(const struct pinmux_ops *ops)
893{
894 /* Check that we implement required operations */
895 if (!ops->list_functions ||
896 !ops->get_function_name ||
897 !ops->get_function_groups ||
898 !ops->enable ||
899 !ops->disable)
900 return -EINVAL;
901
902 return 0;
903}
904
905/* Hog a single map entry and add to the hoglist */
906static int pinmux_hog_map(struct pinctrl_dev *pctldev,
907 struct pinmux_map const *map)
908{
909 struct pinmux_hog *hog;
910 struct pinmux *pmx;
911 int ret;
912
913 if (map->dev || map->dev_name) {
914 /*
915 * TODO: the day we have device tree support, we can
916 * traverse the device tree and hog to specific device nodes
917 * without any problems, so then we can hog pinmuxes for
918 * all devices that just want a static pin mux at this point.
919 */
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100920 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
921 map->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200922 return -EINVAL;
923 }
924
925 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
926 if (!hog)
927 return -ENOMEM;
928
929 pmx = pinmux_get(NULL, map->name);
930 if (IS_ERR(pmx)) {
931 kfree(hog);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700932 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200933 "could not get the %s pinmux mapping for hogging\n",
934 map->name);
935 return PTR_ERR(pmx);
936 }
937
938 ret = pinmux_enable(pmx);
939 if (ret) {
940 pinmux_put(pmx);
941 kfree(hog);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700942 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200943 "could not enable the %s pinmux mapping for hogging\n",
944 map->name);
945 return ret;
946 }
947
948 hog->map = map;
949 hog->pmx = pmx;
950
Stephen Warren51cd24e2011-12-09 16:59:05 -0700951 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200952 map->function);
953 mutex_lock(&pctldev->pinmux_hogs_lock);
954 list_add(&hog->node, &pctldev->pinmux_hogs);
955 mutex_unlock(&pctldev->pinmux_hogs_lock);
956
957 return 0;
958}
959
960/**
961 * pinmux_hog_maps() - hog specific map entries on controller device
962 * @pctldev: the pin control device to hog entries on
963 *
964 * When the pin controllers are registered, there may be some specific pinmux
965 * map entries that need to be hogged, i.e. get+enabled until the system shuts
966 * down.
967 */
968int pinmux_hog_maps(struct pinctrl_dev *pctldev)
969{
Stephen Warren51cd24e2011-12-09 16:59:05 -0700970 struct device *dev = pctldev->dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200971 const char *devname = dev_name(dev);
972 int ret;
973 int i;
974
975 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
976 mutex_init(&pctldev->pinmux_hogs_lock);
977
978 for (i = 0; i < pinmux_maps_num; i++) {
979 struct pinmux_map const *map = &pinmux_maps[i];
980
Tony Lindgren9e2551e2012-01-20 07:43:53 -0800981 if (!map->hog_on_boot)
982 continue;
983
984 if ((map->ctrl_dev == dev) ||
985 (map->ctrl_dev_name &&
986 !strcmp(map->ctrl_dev_name, devname))) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200987 /* OK time to hog! */
988 ret = pinmux_hog_map(pctldev, map);
989 if (ret)
990 return ret;
991 }
992 }
993 return 0;
994}
995
996/**
Linus Walleij336cdba02011-11-10 09:27:41 +0100997 * pinmux_unhog_maps() - unhog specific map entries on controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +0200998 * @pctldev: the pin control device to unhog entries on
999 */
1000void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
1001{
1002 struct list_head *node, *tmp;
1003
1004 mutex_lock(&pctldev->pinmux_hogs_lock);
1005 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
1006 struct pinmux_hog *hog =
1007 list_entry(node, struct pinmux_hog, node);
1008 pinmux_disable(hog->pmx);
1009 pinmux_put(hog->pmx);
1010 list_del(node);
1011 kfree(hog);
1012 }
1013 mutex_unlock(&pctldev->pinmux_hogs_lock);
1014}
1015
1016#ifdef CONFIG_DEBUG_FS
1017
1018/* Called from pincontrol core */
1019static int pinmux_functions_show(struct seq_file *s, void *what)
1020{
1021 struct pinctrl_dev *pctldev = s->private;
1022 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1023 unsigned func_selector = 0;
1024
1025 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1026 const char *func = pmxops->get_function_name(pctldev,
1027 func_selector);
1028 const char * const *groups;
1029 unsigned num_groups;
1030 int ret;
1031 int i;
1032
1033 ret = pmxops->get_function_groups(pctldev, func_selector,
1034 &groups, &num_groups);
1035 if (ret)
1036 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1037 func);
1038
1039 seq_printf(s, "function: %s, groups = [ ", func);
1040 for (i = 0; i < num_groups; i++)
1041 seq_printf(s, "%s ", groups[i]);
1042 seq_puts(s, "]\n");
1043
1044 func_selector++;
1045
1046 }
1047
1048 return 0;
1049}
1050
1051static int pinmux_pins_show(struct seq_file *s, void *what)
1052{
1053 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +09001054 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001055
1056 seq_puts(s, "Pinmux settings per pin\n");
1057 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1058
Chanho Park706e8522012-01-03 16:47:50 +09001059 /* The pin number can be retrived from the pin controller descriptor */
1060 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001061
1062 struct pin_desc *desc;
1063
Chanho Park706e8522012-01-03 16:47:50 +09001064 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001065 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +09001066 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067 if (desc == NULL)
1068 continue;
1069
1070 seq_printf(s, "pin %d (%s): %s\n", pin,
1071 desc->name ? desc->name : "unnamed",
Stephen Warren5d2eaf82011-10-19 16:19:28 -06001072 desc->mux_function ? desc->mux_function
1073 : "UNCLAIMED");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001074 }
1075
1076 return 0;
1077}
1078
1079static int pinmux_hogs_show(struct seq_file *s, void *what)
1080{
1081 struct pinctrl_dev *pctldev = s->private;
1082 struct pinmux_hog *hog;
1083
1084 seq_puts(s, "Pinmux map hogs held by device\n");
1085
1086 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1087 seq_printf(s, "%s\n", hog->map->name);
1088
1089 return 0;
1090}
1091
1092static int pinmux_show(struct seq_file *s, void *what)
1093{
1094 struct pinmux *pmx;
1095
1096 seq_puts(s, "Requested pinmuxes and their maps:\n");
1097 list_for_each_entry(pmx, &pinmux_list, node) {
1098 struct pinctrl_dev *pctldev = pmx->pctldev;
1099 const struct pinmux_ops *pmxops;
1100 const struct pinctrl_ops *pctlops;
1101 struct pinmux_group *grp;
1102
1103 if (!pctldev) {
1104 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1105 continue;
1106 }
1107
1108 pmxops = pctldev->desc->pmxops;
1109 pctlops = pctldev->desc->pctlops;
1110
1111 seq_printf(s, "device: %s function: %s (%u),",
1112 pinctrl_dev_get_name(pmx->pctldev),
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +01001113 pmxops->get_function_name(pctldev,
1114 pmx->func_selector),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001115 pmx->func_selector);
1116
1117 seq_printf(s, " groups: [");
1118 list_for_each_entry(grp, &pmx->groups, node) {
1119 seq_printf(s, " %s (%u)",
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +01001120 pctlops->get_group_name(pctldev,
1121 grp->group_selector),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001122 grp->group_selector);
1123 }
1124 seq_printf(s, " ]");
1125
1126 seq_printf(s, " users: %u map-> %s\n",
1127 pmx->usecount,
1128 pmx->dev ? dev_name(pmx->dev) : "(system)");
1129 }
1130
1131 return 0;
1132}
1133
1134static int pinmux_maps_show(struct seq_file *s, void *what)
1135{
1136 int i;
1137
1138 seq_puts(s, "Pinmux maps:\n");
1139
1140 for (i = 0; i < pinmux_maps_num; i++) {
1141 struct pinmux_map const *map = &pinmux_maps[i];
1142
1143 seq_printf(s, "%s:\n", map->name);
1144 if (map->dev || map->dev_name)
1145 seq_printf(s, " device: %s\n",
1146 map->dev ? dev_name(map->dev) :
1147 map->dev_name);
1148 else
1149 seq_printf(s, " SYSTEM MUX\n");
1150 seq_printf(s, " controlling device %s\n",
1151 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1152 map->ctrl_dev_name);
1153 seq_printf(s, " function: %s\n", map->function);
1154 seq_printf(s, " group: %s\n", map->group ? map->group :
1155 "(default)");
1156 }
1157 return 0;
1158}
1159
1160static int pinmux_functions_open(struct inode *inode, struct file *file)
1161{
1162 return single_open(file, pinmux_functions_show, inode->i_private);
1163}
1164
1165static int pinmux_pins_open(struct inode *inode, struct file *file)
1166{
1167 return single_open(file, pinmux_pins_show, inode->i_private);
1168}
1169
1170static int pinmux_hogs_open(struct inode *inode, struct file *file)
1171{
1172 return single_open(file, pinmux_hogs_show, inode->i_private);
1173}
1174
1175static int pinmux_open(struct inode *inode, struct file *file)
1176{
1177 return single_open(file, pinmux_show, NULL);
1178}
1179
1180static int pinmux_maps_open(struct inode *inode, struct file *file)
1181{
1182 return single_open(file, pinmux_maps_show, NULL);
1183}
1184
1185static const struct file_operations pinmux_functions_ops = {
1186 .open = pinmux_functions_open,
1187 .read = seq_read,
1188 .llseek = seq_lseek,
1189 .release = single_release,
1190};
1191
1192static const struct file_operations pinmux_pins_ops = {
1193 .open = pinmux_pins_open,
1194 .read = seq_read,
1195 .llseek = seq_lseek,
1196 .release = single_release,
1197};
1198
1199static const struct file_operations pinmux_hogs_ops = {
1200 .open = pinmux_hogs_open,
1201 .read = seq_read,
1202 .llseek = seq_lseek,
1203 .release = single_release,
1204};
1205
1206static const struct file_operations pinmux_ops = {
1207 .open = pinmux_open,
1208 .read = seq_read,
1209 .llseek = seq_lseek,
1210 .release = single_release,
1211};
1212
1213static const struct file_operations pinmux_maps_ops = {
1214 .open = pinmux_maps_open,
1215 .read = seq_read,
1216 .llseek = seq_lseek,
1217 .release = single_release,
1218};
1219
1220void pinmux_init_device_debugfs(struct dentry *devroot,
1221 struct pinctrl_dev *pctldev)
1222{
1223 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1224 devroot, pctldev, &pinmux_functions_ops);
1225 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1226 devroot, pctldev, &pinmux_pins_ops);
1227 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1228 devroot, pctldev, &pinmux_hogs_ops);
1229}
1230
1231void pinmux_init_debugfs(struct dentry *subsys_root)
1232{
1233 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1234 subsys_root, NULL, &pinmux_ops);
1235 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1236 subsys_root, NULL, &pinmux_maps_ops);
1237}
1238
1239#endif /* CONFIG_DEBUG_FS */