blob: 4e62783a573a2443efa7db6f807e4fbda5deacd5 [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 Warren652162d2012-03-05 17:22:15 -070097 if (gpio_range) {
98 /* There's no need to support multiple GPIO requests */
99 if (desc->gpio_owner) {
100 dev_err(pctldev->dev,
101 "pin already requested\n");
102 goto out;
103 }
104
105 desc->gpio_owner = owner;
106 } else {
107 if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
108 dev_err(pctldev->dev,
109 "pin already requested\n");
110 goto out;
111 }
112
113 desc->mux_usecount++;
114 if (desc->mux_usecount > 1)
115 return 0;
116
117 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700119
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120 /* Let each pin increase references to this module */
121 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700122 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200123 "could not increase module refcount for pin %d\n",
124 pin);
125 status = -EINVAL;
126 goto out_free_pin;
127 }
128
129 /*
130 * If there is no kind of request function for the pin we just assume
131 * we got it by default and proceed.
132 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600133 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200134 /* This requests and enables a single GPIO pin */
135 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
136 else if (ops->request)
137 status = ops->request(pctldev, pin);
138 else
139 status = 0;
140
Stephen Warren0e3db1732012-03-02 13:05:46 -0700141 if (status) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100142 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200143 pctldev->desc->name, pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700144 module_put(pctldev->owner);
145 }
146
Linus Walleij2744e8a2011-05-02 20:50:54 +0200147out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700148 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700149 if (gpio_range) {
150 desc->gpio_owner = NULL;
151 } else {
152 desc->mux_usecount--;
153 if (!desc->mux_usecount)
154 desc->mux_owner = NULL;
155 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700156 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200157out:
158 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700159 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700160 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200161
162 return status;
163}
164
165/**
166 * pin_free() - release a single muxed in pin so something else can be muxed
167 * @pctldev: pin controller device handling this pin
168 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600169 * @gpio_range: the range matching the GPIO pin if this is a request for a
170 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100171 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700172 * This function returns a pointer to the previous owner. This is used
173 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100174 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200175 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600176static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
177 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178{
179 const struct pinmux_ops *ops = pctldev->desc->pmxops;
180 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700181 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182
183 desc = pin_desc_get(pctldev, pin);
184 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700185 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600187 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188 }
189
Stephen Warren652162d2012-03-05 17:22:15 -0700190 if (!gpio_range) {
191 desc->mux_usecount--;
192 if (desc->mux_usecount)
193 return NULL;
194 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700195
Stephen Warren3712a3c2011-10-21 12:25:53 -0600196 /*
197 * If there is no kind of request function for the pin we just assume
198 * we got it by default and proceed.
199 */
200 if (gpio_range && ops->gpio_disable_free)
201 ops->gpio_disable_free(pctldev, gpio_range, pin);
202 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 ops->free(pctldev, pin);
204
Stephen Warren652162d2012-03-05 17:22:15 -0700205 if (gpio_range) {
206 owner = desc->gpio_owner;
207 desc->gpio_owner = NULL;
208 } else {
209 owner = desc->mux_owner;
210 desc->mux_owner = NULL;
211 desc->mux_setting = NULL;
212 }
213
Linus Walleij2744e8a2011-05-02 20:50:54 +0200214 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600215
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700216 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217}
218
219/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100220 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
221 * @pctldev: pin controller device affected
222 * @pin: the pin to mux in for GPIO
223 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100225int pinmux_request_gpio(struct pinctrl_dev *pctldev,
226 struct pinctrl_gpio_range *range,
227 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228{
229 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700230 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231 int ret;
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 Warren3cc70ed2012-02-19 23:45:44 -0700236 owner = kstrdup(gpiostr, GFP_KERNEL);
237 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600238 return -EINVAL;
239
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700240 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600241 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700242 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600243
244 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246
247/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100248 * pinmux_free_gpio() - release a pin from GPIO muxing
249 * @pctldev: the pin controller device for the pin
250 * @pin: the affected currently GPIO-muxed in pin
251 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200252 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100253void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
254 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200255{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700256 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700258 owner = pin_free(pctldev, pin, range);
259 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100262/**
263 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
264 * @pctldev: the pin controller handling this pin
265 * @range: applicable GPIO range
266 * @pin: the affected GPIO pin in this controller
267 * @input: true if we set the pin as input, false for output
268 */
269int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
270 struct pinctrl_gpio_range *range,
271 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100272{
Linus Walleij542e7042011-11-14 10:06:22 +0100273 const struct pinmux_ops *ops;
274 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100275
276 ops = pctldev->desc->pmxops;
277
Linus Walleij542e7042011-11-14 10:06:22 +0100278 if (ops->gpio_set_direction)
279 ret = ops->gpio_set_direction(pctldev, range, pin, input);
280 else
281 ret = 0;
282
283 return ret;
284}
285
Stephen Warren7ecdb162012-03-02 13:05:45 -0700286static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
287 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200288{
289 const struct pinmux_ops *ops = pctldev->desc->pmxops;
290 unsigned selector = 0;
291
292 /* See if this pctldev has this function */
293 while (ops->list_functions(pctldev, selector) >= 0) {
294 const char *fname = ops->get_function_name(pctldev,
295 selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296
Stephen Warren7ecdb162012-03-02 13:05:45 -0700297 if (!strcmp(function, fname))
298 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200299
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300 selector++;
301 }
302
303 pr_err("%s does not support function %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700304 pinctrl_dev_get_name(pctldev), function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 return -EINVAL;
306}
307
Stephen Warren7ecdb162012-03-02 13:05:45 -0700308int pinmux_map_to_setting(struct pinctrl_map const *map,
309 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700311 struct pinctrl_dev *pctldev = setting->pctldev;
312 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
313 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
314 char const * const *groups;
315 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700317 const char *group;
318 int i;
319 const unsigned *pins;
320 unsigned num_pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200321
Stephen Warren1e2082b2012-03-02 13:05:48 -0700322 setting->data.mux.func =
323 pinmux_func_name_to_selector(pctldev, map->data.mux.function);
324 if (setting->data.mux.func < 0)
325 return setting->data.mux.func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200326
Stephen Warren1e2082b2012-03-02 13:05:48 -0700327 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700328 &groups, &num_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329 if (ret < 0)
330 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700331 if (!num_groups)
332 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333
Stephen Warren1e2082b2012-03-02 13:05:48 -0700334 if (map->data.mux.group) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700335 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700336 group = map->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700337 for (i = 0; i < num_groups; i++) {
338 if (!strcmp(group, groups[i])) {
339 found = true;
340 break;
341 }
342 }
343 if (!found)
344 return -EINVAL;
345 } else {
346 group = groups[0];
347 }
348
Stephen Warren1e2082b2012-03-02 13:05:48 -0700349 setting->data.mux.group = pinctrl_get_group_selector(pctldev, group);
350 if (setting->data.mux.group < 0)
351 return setting->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700352
Stephen Warren1e2082b2012-03-02 13:05:48 -0700353 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
354 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700356 dev_err(pctldev->dev,
357 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700358 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700359 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200360 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700361
362 /* Try to allocate all pins in this group, one by one */
363 for (i = 0; i < num_pins; i++) {
364 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
365 if (ret) {
366 dev_err(pctldev->dev,
367 "could not get request pin %d on device %s\n",
368 pins[i], pinctrl_dev_get_name(pctldev));
369 /* On error release all taken pins */
370 i--; /* this pin just failed */
371 for (; i >= 0; i--)
372 pin_free(pctldev, pins[i], NULL);
373 return -ENODEV;
374 }
375 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200376
377 return 0;
378}
379
Stephen Warren7ecdb162012-03-02 13:05:45 -0700380void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100381{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700382 struct pinctrl_dev *pctldev = setting->pctldev;
383 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
384 const unsigned *pins;
385 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100386 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700387 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100388
Stephen Warren1e2082b2012-03-02 13:05:48 -0700389 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700390 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100391 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700392 dev_err(pctldev->dev,
393 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700394 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700395 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100396 }
397
Stephen Warren7ecdb162012-03-02 13:05:45 -0700398 for (i = 0; i < num_pins; i++)
399 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100400}
401
Stephen Warren7ecdb162012-03-02 13:05:45 -0700402int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200403{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700404 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700405 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100406 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700407 int ret;
408 const unsigned *pins;
409 unsigned num_pins;
410 int i;
411 struct pin_desc *desc;
412
413 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
414 &pins, &num_pins);
415 if (ret) {
416 /* errors only affect debug data, so just warn */
417 dev_warn(pctldev->dev,
418 "could not get pins for group selector %d\n",
419 setting->data.mux.group);
420 num_pins = 0;
421 }
422
423 for (i = 0; i < num_pins; i++) {
424 desc = pin_desc_get(pctldev, pins[i]);
425 if (desc == NULL) {
426 dev_warn(pctldev->dev,
427 "could not get pin desc for pin %d\n",
428 pins[i]);
429 continue;
430 }
431 desc->mux_setting = &(setting->data.mux);
432 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200433
Stephen Warren1e2082b2012-03-02 13:05:48 -0700434 return ops->enable(pctldev, setting->data.mux.func,
435 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200436}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200437
Stephen Warren7ecdb162012-03-02 13:05:45 -0700438void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200439{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700440 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700441 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100442 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700443 int ret;
444 const unsigned *pins;
445 unsigned num_pins;
446 int i;
447 struct pin_desc *desc;
448
449 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
450 &pins, &num_pins);
451 if (ret) {
452 /* errors only affect debug data, so just warn */
453 dev_warn(pctldev->dev,
454 "could not get pins for group selector %d\n",
455 setting->data.mux.group);
456 num_pins = 0;
457 }
458
459 for (i = 0; i < num_pins; i++) {
460 desc = pin_desc_get(pctldev, pins[i]);
461 if (desc == NULL) {
462 dev_warn(pctldev->dev,
463 "could not get pin desc for pin %d\n",
464 pins[i]);
465 continue;
466 }
467 desc->mux_setting = NULL;
468 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200469
Stephen Warren1e2082b2012-03-02 13:05:48 -0700470 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200471}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200472
Linus Walleij2744e8a2011-05-02 20:50:54 +0200473#ifdef CONFIG_DEBUG_FS
474
475/* Called from pincontrol core */
476static int pinmux_functions_show(struct seq_file *s, void *what)
477{
478 struct pinctrl_dev *pctldev = s->private;
479 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
480 unsigned func_selector = 0;
481
Stephen Warren57b676f2012-03-02 13:05:44 -0700482 mutex_lock(&pinctrl_mutex);
483
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
485 const char *func = pmxops->get_function_name(pctldev,
486 func_selector);
487 const char * const *groups;
488 unsigned num_groups;
489 int ret;
490 int i;
491
492 ret = pmxops->get_function_groups(pctldev, func_selector,
493 &groups, &num_groups);
494 if (ret)
495 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
496 func);
497
498 seq_printf(s, "function: %s, groups = [ ", func);
499 for (i = 0; i < num_groups; i++)
500 seq_printf(s, "%s ", groups[i]);
501 seq_puts(s, "]\n");
502
503 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200504 }
505
Stephen Warren57b676f2012-03-02 13:05:44 -0700506 mutex_unlock(&pinctrl_mutex);
507
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508 return 0;
509}
510
511static int pinmux_pins_show(struct seq_file *s, void *what)
512{
513 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700514 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
515 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900516 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200517
518 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren652162d2012-03-05 17:22:15 -0700519 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200520
Stephen Warren57b676f2012-03-02 13:05:44 -0700521 mutex_lock(&pinctrl_mutex);
522
Chanho Park706e8522012-01-03 16:47:50 +0900523 /* The pin number can be retrived from the pin controller descriptor */
524 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200525 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100526 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200527
Chanho Park706e8522012-01-03 16:47:50 +0900528 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200529 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900530 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200531 if (desc == NULL)
532 continue;
533
Stephen Warren652162d2012-03-05 17:22:15 -0700534 if (desc->mux_owner &&
535 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100536 is_hog = true;
537
Stephen Warren652162d2012-03-05 17:22:15 -0700538 seq_printf(s, "pin %d (%s): %s %s%s", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200539 desc->name ? desc->name : "unnamed",
Stephen Warren652162d2012-03-05 17:22:15 -0700540 desc->mux_owner ? desc->mux_owner
541 : "(MUX UNCLAIMED)",
542 desc->gpio_owner ? desc->gpio_owner
543 : "(GPIO UNCLAIMED)",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100544 is_hog ? " (HOG)" : "");
Stephen Warrenba110d92012-03-02 13:05:49 -0700545
546 if (desc->mux_setting)
547 seq_printf(s, " function %s group %s\n",
548 pmxops->get_function_name(pctldev,
549 desc->mux_setting->func),
550 pctlops->get_group_name(pctldev,
551 desc->mux_setting->group));
552 else
553 seq_printf(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200554 }
555
Stephen Warren57b676f2012-03-02 13:05:44 -0700556 mutex_unlock(&pinctrl_mutex);
557
Linus Walleij2744e8a2011-05-02 20:50:54 +0200558 return 0;
559}
560
Stephen Warren1e2082b2012-03-02 13:05:48 -0700561void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
562{
563 seq_printf(s, "group %s\nfunction %s\n",
564 map->data.mux.group ? map->data.mux.group : "(default)",
565 map->data.mux.function);
566}
567
568void pinmux_show_setting(struct seq_file *s,
569 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200570{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700571 struct pinctrl_dev *pctldev = setting->pctldev;
572 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
573 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200574
Stephen Warren1e2082b2012-03-02 13:05:48 -0700575 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
576 pctlops->get_group_name(pctldev, setting->data.mux.group),
577 setting->data.mux.group,
578 pmxops->get_function_name(pctldev, setting->data.mux.func),
579 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200580}
581
582static int pinmux_functions_open(struct inode *inode, struct file *file)
583{
584 return single_open(file, pinmux_functions_show, inode->i_private);
585}
586
587static int pinmux_pins_open(struct inode *inode, struct file *file)
588{
589 return single_open(file, pinmux_pins_show, inode->i_private);
590}
591
Linus Walleij2744e8a2011-05-02 20:50:54 +0200592static const struct file_operations pinmux_functions_ops = {
593 .open = pinmux_functions_open,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .release = single_release,
597};
598
599static const struct file_operations pinmux_pins_ops = {
600 .open = pinmux_pins_open,
601 .read = seq_read,
602 .llseek = seq_lseek,
603 .release = single_release,
604};
605
Linus Walleij2744e8a2011-05-02 20:50:54 +0200606void pinmux_init_device_debugfs(struct dentry *devroot,
607 struct pinctrl_dev *pctldev)
608{
609 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
610 devroot, pctldev, &pinmux_functions_ops);
611 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
612 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200613}
614
615#endif /* CONFIG_DEBUG_FS */