blob: 4852ebe5712e8ee4f0fceeaddf6c30eaa500c543 [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 *
Linus Walleije93bcee2012-02-09 07:23:28 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * 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 *
Stephen Warren7ecdb162012-03-02 13:05:45 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinmux core: " fmt
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
21#include <linux/radix-tree.h>
22#include <linux/err.h>
23#include <linux/list.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"
Linus Walleijbefe5bd2012-02-09 19:47:48 +010031#include "pinmux.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020032
Stephen Warren03665e02012-02-19 23:45:45 -070033int pinmux_check_ops(struct pinctrl_dev *pctldev)
34{
35 const struct pinmux_ops *ops = pctldev->desc->pmxops;
36 unsigned selector = 0;
37
38 /* Check that we implement required operations */
39 if (!ops->list_functions ||
40 !ops->get_function_name ||
41 !ops->get_function_groups ||
42 !ops->enable ||
43 !ops->disable)
44 return -EINVAL;
45
46 /* Check that all functions registered have names */
47 while (ops->list_functions(pctldev, selector) >= 0) {
48 const char *fname = ops->get_function_name(pctldev,
49 selector);
50 if (!fname) {
51 pr_err("pinmux ops has no name for function%u\n",
52 selector);
53 return -EINVAL;
54 }
55 selector++;
56 }
57
58 return 0;
59}
60
Stephen Warren1e2082b2012-03-02 13:05:48 -070061int pinmux_validate_map(struct pinctrl_map const *map, int i)
62{
63 if (!map->data.mux.function) {
64 pr_err("failed to register map %s (%d): no function given\n",
65 map->name, i);
66 return -EINVAL;
67 }
68
69 return 0;
70}
71
Linus Walleij2744e8a2011-05-02 20:50:54 +020072/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020073 * pin_request() - request a single pin to be muxed in, typically for GPIO
74 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070075 * @owner: a representation of the owner of this pin; typically the device
76 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020077 * @gpio_range: the range matching the GPIO pin if this is a request for a
78 * single GPIO pin
79 */
80static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070081 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020082 struct pinctrl_gpio_range *gpio_range)
83{
84 struct pin_desc *desc;
85 const struct pinmux_ops *ops = pctldev->desc->pmxops;
86 int status = -EINVAL;
87
Stephen Warren3cc70ed2012-02-19 23:45:44 -070088 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +020089
Linus Walleij2744e8a2011-05-02 20:50:54 +020090 desc = pin_desc_get(pctldev, pin);
91 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070092 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 "pin is not registered so it cannot be requested\n");
94 goto out;
95 }
96
Stephen Warren0e3db1732012-03-02 13:05:46 -070097 if (desc->usecount && strcmp(desc->owner, owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070098 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020099 "pin already requested\n");
100 goto out;
101 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700102
103 desc->usecount++;
104 if (desc->usecount > 1)
105 return 0;
106
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700107 desc->owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200108
109 /* Let each pin increase references to this module */
110 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700111 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 "could not increase module refcount for pin %d\n",
113 pin);
114 status = -EINVAL;
115 goto out_free_pin;
116 }
117
118 /*
119 * If there is no kind of request function for the pin we just assume
120 * we got it by default and proceed.
121 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600122 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200123 /* This requests and enables a single GPIO pin */
124 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
125 else if (ops->request)
126 status = ops->request(pctldev, pin);
127 else
128 status = 0;
129
Stephen Warren0e3db1732012-03-02 13:05:46 -0700130 if (status) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100131 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200132 pctldev->desc->name, pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700133 module_put(pctldev->owner);
134 }
135
Linus Walleij2744e8a2011-05-02 20:50:54 +0200136out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700137 if (status) {
138 desc->usecount--;
139 if (!desc->usecount)
140 desc->owner = NULL;
141 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200142out:
143 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700144 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700145 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200146
147 return status;
148}
149
150/**
151 * pin_free() - release a single muxed in pin so something else can be muxed
152 * @pctldev: pin controller device handling this pin
153 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600154 * @gpio_range: the range matching the GPIO pin if this is a request for a
155 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100156 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700157 * This function returns a pointer to the previous owner. This is used
158 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100159 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200160 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600161static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
162 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200163{
164 const struct pinmux_ops *ops = pctldev->desc->pmxops;
165 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700166 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200167
168 desc = pin_desc_get(pctldev, pin);
169 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700170 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200171 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600172 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200173 }
174
Stephen Warren0e3db1732012-03-02 13:05:46 -0700175 desc->usecount--;
176 if (desc->usecount)
177 return NULL;
178
Stephen Warren3712a3c2011-10-21 12:25:53 -0600179 /*
180 * If there is no kind of request function for the pin we just assume
181 * we got it by default and proceed.
182 */
183 if (gpio_range && ops->gpio_disable_free)
184 ops->gpio_disable_free(pctldev, gpio_range, pin);
185 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186 ops->free(pctldev, pin);
187
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700188 owner = desc->owner;
189 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200190 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600191
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700192 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193}
194
195/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100196 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
197 * @pctldev: pin controller device affected
198 * @pin: the pin to mux in for GPIO
199 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100201int pinmux_request_gpio(struct pinctrl_dev *pctldev,
202 struct pinctrl_gpio_range *range,
203 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204{
205 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700206 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200208
209 /* Conjure some name stating what chip and pin this is taken by */
210 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
211
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700212 owner = kstrdup(gpiostr, GFP_KERNEL);
213 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600214 return -EINVAL;
215
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700216 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600217 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700218 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600219
220 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222
223/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100224 * pinmux_free_gpio() - release a pin from GPIO muxing
225 * @pctldev: the pin controller device for the pin
226 * @pin: the affected currently GPIO-muxed in pin
227 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100229void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
230 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700232 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700234 owner = pin_free(pctldev, pin, range);
235 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200237
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100238/**
239 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
240 * @pctldev: the pin controller handling this pin
241 * @range: applicable GPIO range
242 * @pin: the affected GPIO pin in this controller
243 * @input: true if we set the pin as input, false for output
244 */
245int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
246 struct pinctrl_gpio_range *range,
247 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100248{
Linus Walleij542e7042011-11-14 10:06:22 +0100249 const struct pinmux_ops *ops;
250 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100251
252 ops = pctldev->desc->pmxops;
253
Linus Walleij542e7042011-11-14 10:06:22 +0100254 if (ops->gpio_set_direction)
255 ret = ops->gpio_set_direction(pctldev, range, pin, input);
256 else
257 ret = 0;
258
259 return ret;
260}
261
Stephen Warren7ecdb162012-03-02 13:05:45 -0700262static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
263 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200264{
265 const struct pinmux_ops *ops = pctldev->desc->pmxops;
266 unsigned selector = 0;
267
268 /* See if this pctldev has this function */
269 while (ops->list_functions(pctldev, selector) >= 0) {
270 const char *fname = ops->get_function_name(pctldev,
271 selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200272
Stephen Warren7ecdb162012-03-02 13:05:45 -0700273 if (!strcmp(function, fname))
274 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275
Linus Walleij2744e8a2011-05-02 20:50:54 +0200276 selector++;
277 }
278
279 pr_err("%s does not support function %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700280 pinctrl_dev_get_name(pctldev), function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200281 return -EINVAL;
282}
283
Stephen Warren7ecdb162012-03-02 13:05:45 -0700284int pinmux_map_to_setting(struct pinctrl_map const *map,
285 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200286{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700287 struct pinctrl_dev *pctldev = setting->pctldev;
288 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
289 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
290 char const * const *groups;
291 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200292 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700293 const char *group;
294 int i;
295 const unsigned *pins;
296 unsigned num_pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297
Stephen Warren1e2082b2012-03-02 13:05:48 -0700298 setting->data.mux.func =
299 pinmux_func_name_to_selector(pctldev, map->data.mux.function);
300 if (setting->data.mux.func < 0)
301 return setting->data.mux.func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302
Stephen Warren1e2082b2012-03-02 13:05:48 -0700303 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700304 &groups, &num_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 if (ret < 0)
306 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700307 if (!num_groups)
308 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309
Stephen Warren1e2082b2012-03-02 13:05:48 -0700310 if (map->data.mux.group) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700311 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700312 group = map->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700313 for (i = 0; i < num_groups; i++) {
314 if (!strcmp(group, groups[i])) {
315 found = true;
316 break;
317 }
318 }
319 if (!found)
320 return -EINVAL;
321 } else {
322 group = groups[0];
323 }
324
Stephen Warren1e2082b2012-03-02 13:05:48 -0700325 setting->data.mux.group = pinctrl_get_group_selector(pctldev, group);
326 if (setting->data.mux.group < 0)
327 return setting->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700328
Stephen Warren1e2082b2012-03-02 13:05:48 -0700329 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
330 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700332 dev_err(pctldev->dev,
333 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700334 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700335 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700337
338 /* Try to allocate all pins in this group, one by one */
339 for (i = 0; i < num_pins; i++) {
340 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
341 if (ret) {
342 dev_err(pctldev->dev,
343 "could not get request pin %d on device %s\n",
344 pins[i], pinctrl_dev_get_name(pctldev));
345 /* On error release all taken pins */
346 i--; /* this pin just failed */
347 for (; i >= 0; i--)
348 pin_free(pctldev, pins[i], NULL);
349 return -ENODEV;
350 }
351 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200352
353 return 0;
354}
355
Stephen Warren7ecdb162012-03-02 13:05:45 -0700356void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100357{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700358 struct pinctrl_dev *pctldev = setting->pctldev;
359 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
360 const unsigned *pins;
361 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100362 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700363 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100364
Stephen Warren1e2082b2012-03-02 13:05:48 -0700365 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700366 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100367 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700368 dev_err(pctldev->dev,
369 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700370 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700371 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100372 }
373
Stephen Warren7ecdb162012-03-02 13:05:45 -0700374 for (i = 0; i < num_pins; i++)
375 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100376}
377
Stephen Warren7ecdb162012-03-02 13:05:45 -0700378int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200379{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700380 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100381 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382
Stephen Warren1e2082b2012-03-02 13:05:48 -0700383 return ops->enable(pctldev, setting->data.mux.func,
384 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200385}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200386
Stephen Warren7ecdb162012-03-02 13:05:45 -0700387void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200388{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700389 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100390 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391
Stephen Warren1e2082b2012-03-02 13:05:48 -0700392 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200394
Linus Walleij2744e8a2011-05-02 20:50:54 +0200395#ifdef CONFIG_DEBUG_FS
396
397/* Called from pincontrol core */
398static int pinmux_functions_show(struct seq_file *s, void *what)
399{
400 struct pinctrl_dev *pctldev = s->private;
401 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
402 unsigned func_selector = 0;
403
Stephen Warren57b676f2012-03-02 13:05:44 -0700404 mutex_lock(&pinctrl_mutex);
405
Linus Walleij2744e8a2011-05-02 20:50:54 +0200406 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
407 const char *func = pmxops->get_function_name(pctldev,
408 func_selector);
409 const char * const *groups;
410 unsigned num_groups;
411 int ret;
412 int i;
413
414 ret = pmxops->get_function_groups(pctldev, func_selector,
415 &groups, &num_groups);
416 if (ret)
417 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
418 func);
419
420 seq_printf(s, "function: %s, groups = [ ", func);
421 for (i = 0; i < num_groups; i++)
422 seq_printf(s, "%s ", groups[i]);
423 seq_puts(s, "]\n");
424
425 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200426 }
427
Stephen Warren57b676f2012-03-02 13:05:44 -0700428 mutex_unlock(&pinctrl_mutex);
429
Linus Walleij2744e8a2011-05-02 20:50:54 +0200430 return 0;
431}
432
433static int pinmux_pins_show(struct seq_file *s, void *what)
434{
435 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900436 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200437
438 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700439 seq_puts(s, "Format: pin (name): owner\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200440
Stephen Warren57b676f2012-03-02 13:05:44 -0700441 mutex_lock(&pinctrl_mutex);
442
Chanho Park706e8522012-01-03 16:47:50 +0900443 /* The pin number can be retrived from the pin controller descriptor */
444 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200445 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100446 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200447
Chanho Park706e8522012-01-03 16:47:50 +0900448 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200449 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900450 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200451 if (desc == NULL)
452 continue;
453
Linus Walleij1cf94c42012-02-24 06:53:04 +0100454 if (desc->owner &&
455 !strcmp(desc->owner, pinctrl_dev_get_name(pctldev)))
456 is_hog = true;
457
458 seq_printf(s, "pin %d (%s): %s%s\n", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200459 desc->name ? desc->name : "unnamed",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100460 desc->owner ? desc->owner : "UNCLAIMED",
461 is_hog ? " (HOG)" : "");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200462 }
463
Stephen Warren57b676f2012-03-02 13:05:44 -0700464 mutex_unlock(&pinctrl_mutex);
465
Linus Walleij2744e8a2011-05-02 20:50:54 +0200466 return 0;
467}
468
Stephen Warren1e2082b2012-03-02 13:05:48 -0700469void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
470{
471 seq_printf(s, "group %s\nfunction %s\n",
472 map->data.mux.group ? map->data.mux.group : "(default)",
473 map->data.mux.function);
474}
475
476void pinmux_show_setting(struct seq_file *s,
477 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200478{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700479 struct pinctrl_dev *pctldev = setting->pctldev;
480 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
481 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200482
Stephen Warren1e2082b2012-03-02 13:05:48 -0700483 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
484 pctlops->get_group_name(pctldev, setting->data.mux.group),
485 setting->data.mux.group,
486 pmxops->get_function_name(pctldev, setting->data.mux.func),
487 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200488}
489
490static int pinmux_functions_open(struct inode *inode, struct file *file)
491{
492 return single_open(file, pinmux_functions_show, inode->i_private);
493}
494
495static int pinmux_pins_open(struct inode *inode, struct file *file)
496{
497 return single_open(file, pinmux_pins_show, inode->i_private);
498}
499
Linus Walleij2744e8a2011-05-02 20:50:54 +0200500static const struct file_operations pinmux_functions_ops = {
501 .open = pinmux_functions_open,
502 .read = seq_read,
503 .llseek = seq_lseek,
504 .release = single_release,
505};
506
507static const struct file_operations pinmux_pins_ops = {
508 .open = pinmux_pins_open,
509 .read = seq_read,
510 .llseek = seq_lseek,
511 .release = single_release,
512};
513
Linus Walleij2744e8a2011-05-02 20:50:54 +0200514void pinmux_init_device_debugfs(struct dentry *devroot,
515 struct pinctrl_dev *pctldev)
516{
517 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
518 devroot, pctldev, &pinmux_functions_ops);
519 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
520 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200521}
522
523#endif /* CONFIG_DEBUG_FS */