blob: 2df753508ecabf2cb5284ec150c6a8cb07996fcb [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;
Dong Aishenga1d31f72012-04-06 20:18:09 +080036 unsigned nfuncs;
Stephen Warren03665e02012-02-19 23:45:45 -070037 unsigned selector = 0;
38
39 /* Check that we implement required operations */
Dong Aishenga1d31f72012-04-06 20:18:09 +080040 if (!ops ||
41 !ops->get_functions_count ||
Stephen Warren03665e02012-02-19 23:45:45 -070042 !ops->get_function_name ||
43 !ops->get_function_groups ||
44 !ops->enable ||
John Crispinad6e1102012-04-26 16:47:11 +020045 !ops->disable) {
46 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
Stephen Warren03665e02012-02-19 23:45:45 -070047 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020048 }
Stephen Warren03665e02012-02-19 23:45:45 -070049 /* Check that all functions registered have names */
Dong Aishenga1d31f72012-04-06 20:18:09 +080050 nfuncs = ops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053051 while (selector < nfuncs) {
Stephen Warren03665e02012-02-19 23:45:45 -070052 const char *fname = ops->get_function_name(pctldev,
53 selector);
54 if (!fname) {
Dong Aishenga1d31f72012-04-06 20:18:09 +080055 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
Stephen Warren03665e02012-02-19 23:45:45 -070056 selector);
57 return -EINVAL;
58 }
59 selector++;
60 }
61
62 return 0;
63}
64
Stephen Warren1e2082b2012-03-02 13:05:48 -070065int pinmux_validate_map(struct pinctrl_map const *map, int i)
66{
67 if (!map->data.mux.function) {
68 pr_err("failed to register map %s (%d): no function given\n",
69 map->name, i);
70 return -EINVAL;
71 }
72
73 return 0;
74}
75
Linus Walleij2744e8a2011-05-02 20:50:54 +020076/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020077 * pin_request() - request a single pin to be muxed in, typically for GPIO
78 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070079 * @owner: a representation of the owner of this pin; typically the device
80 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020081 * @gpio_range: the range matching the GPIO pin if this is a request for a
82 * single GPIO pin
83 */
84static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070085 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020086 struct pinctrl_gpio_range *gpio_range)
87{
88 struct pin_desc *desc;
89 const struct pinmux_ops *ops = pctldev->desc->pmxops;
90 int status = -EINVAL;
91
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 desc = pin_desc_get(pctldev, pin);
93 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070094 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 "pin is not registered so it cannot be requested\n");
96 goto out;
97 }
98
Dong Aishengd0bd8df2012-04-17 15:00:45 +080099 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
100 pin, desc->name, owner);
101
Stephen Warren652162d2012-03-05 17:22:15 -0700102 if (gpio_range) {
103 /* There's no need to support multiple GPIO requests */
104 if (desc->gpio_owner) {
105 dev_err(pctldev->dev,
106 "pin already requested\n");
107 goto out;
108 }
109
110 desc->gpio_owner = owner;
111 } else {
112 if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
113 dev_err(pctldev->dev,
114 "pin already requested\n");
115 goto out;
116 }
117
118 desc->mux_usecount++;
119 if (desc->mux_usecount > 1)
120 return 0;
121
122 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200123 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700124
Linus Walleij2744e8a2011-05-02 20:50:54 +0200125 /* Let each pin increase references to this module */
126 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700127 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200128 "could not increase module refcount for pin %d\n",
129 pin);
130 status = -EINVAL;
131 goto out_free_pin;
132 }
133
134 /*
135 * If there is no kind of request function for the pin we just assume
136 * we got it by default and proceed.
137 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600138 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200139 /* This requests and enables a single GPIO pin */
140 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
141 else if (ops->request)
142 status = ops->request(pctldev, pin);
143 else
144 status = 0;
145
Stephen Warren0e3db1732012-03-02 13:05:46 -0700146 if (status) {
John Crispinad6e1102012-04-26 16:47:11 +0200147 dev_err(pctldev->dev, "request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200148 pctldev->desc->name, pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700149 module_put(pctldev->owner);
150 }
151
Linus Walleij2744e8a2011-05-02 20:50:54 +0200152out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700153 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700154 if (gpio_range) {
155 desc->gpio_owner = NULL;
156 } else {
157 desc->mux_usecount--;
158 if (!desc->mux_usecount)
159 desc->mux_owner = NULL;
160 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700161 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200162out:
163 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700164 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700165 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200166
167 return status;
168}
169
170/**
171 * pin_free() - release a single muxed in pin so something else can be muxed
172 * @pctldev: pin controller device handling this pin
173 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600174 * @gpio_range: the range matching the GPIO pin if this is a request for a
175 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100176 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700177 * This function returns a pointer to the previous owner. This is used
178 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100179 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200180 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600181static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
182 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183{
184 const struct pinmux_ops *ops = pctldev->desc->pmxops;
185 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700186 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187
188 desc = pin_desc_get(pctldev, pin);
189 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700190 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200191 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600192 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193 }
194
Stephen Warren652162d2012-03-05 17:22:15 -0700195 if (!gpio_range) {
196 desc->mux_usecount--;
197 if (desc->mux_usecount)
198 return NULL;
199 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700200
Stephen Warren3712a3c2011-10-21 12:25:53 -0600201 /*
202 * If there is no kind of request function for the pin we just assume
203 * we got it by default and proceed.
204 */
205 if (gpio_range && ops->gpio_disable_free)
206 ops->gpio_disable_free(pctldev, gpio_range, pin);
207 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200208 ops->free(pctldev, pin);
209
Stephen Warren652162d2012-03-05 17:22:15 -0700210 if (gpio_range) {
211 owner = desc->gpio_owner;
212 desc->gpio_owner = NULL;
213 } else {
214 owner = desc->mux_owner;
215 desc->mux_owner = NULL;
216 desc->mux_setting = NULL;
217 }
218
Linus Walleij2744e8a2011-05-02 20:50:54 +0200219 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600220
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700221 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222}
223
224/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100225 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
226 * @pctldev: pin controller device affected
227 * @pin: the pin to mux in for GPIO
228 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100230int pinmux_request_gpio(struct pinctrl_dev *pctldev,
231 struct pinctrl_gpio_range *range,
232 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233{
234 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700235 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200237
238 /* Conjure some name stating what chip and pin this is taken by */
239 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
240
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700241 owner = kstrdup(gpiostr, GFP_KERNEL);
242 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600243 return -EINVAL;
244
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700245 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600246 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700247 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600248
249 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200251
252/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100253 * pinmux_free_gpio() - release a pin from GPIO muxing
254 * @pctldev: the pin controller device for the pin
255 * @pin: the affected currently GPIO-muxed in pin
256 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100258void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
259 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700261 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700263 owner = pin_free(pctldev, pin, range);
264 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200266
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100267/**
268 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
269 * @pctldev: the pin controller handling this pin
270 * @range: applicable GPIO range
271 * @pin: the affected GPIO pin in this controller
272 * @input: true if we set the pin as input, false for output
273 */
274int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
275 struct pinctrl_gpio_range *range,
276 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100277{
Linus Walleij542e7042011-11-14 10:06:22 +0100278 const struct pinmux_ops *ops;
279 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100280
281 ops = pctldev->desc->pmxops;
282
Linus Walleij542e7042011-11-14 10:06:22 +0100283 if (ops->gpio_set_direction)
284 ret = ops->gpio_set_direction(pctldev, range, pin, input);
285 else
286 ret = 0;
287
288 return ret;
289}
290
Stephen Warren7ecdb162012-03-02 13:05:45 -0700291static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
292 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200293{
294 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530295 unsigned nfuncs = ops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 unsigned selector = 0;
297
298 /* See if this pctldev has this function */
Viresh Kumard1e90e92012-03-30 11:25:40 +0530299 while (selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300 const char *fname = ops->get_function_name(pctldev,
301 selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302
Stephen Warren7ecdb162012-03-02 13:05:45 -0700303 if (!strcmp(function, fname))
304 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 selector++;
307 }
308
309 pr_err("%s does not support function %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700310 pinctrl_dev_get_name(pctldev), function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200311 return -EINVAL;
312}
313
Stephen Warren7ecdb162012-03-02 13:05:45 -0700314int pinmux_map_to_setting(struct pinctrl_map const *map,
315 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700317 struct pinctrl_dev *pctldev = setting->pctldev;
318 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
319 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
320 char const * const *groups;
321 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700323 const char *group;
324 int i;
325 const unsigned *pins;
326 unsigned num_pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200327
Dong Aishengad8bb722012-04-10 12:41:34 +0800328 if (!pmxops) {
329 dev_err(pctldev->dev, "does not support mux function\n");
330 return -EINVAL;
331 }
332
John Crispin15f70e12012-04-23 19:01:58 +0200333 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
John Crispinad6e1102012-04-26 16:47:11 +0200334 if (ret < 0) {
335 dev_err(pctldev->dev, "invalid function %s in map table\n",
336 map->data.mux.function);
John Crispin15f70e12012-04-23 19:01:58 +0200337 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200338 }
John Crispin15f70e12012-04-23 19:01:58 +0200339 setting->data.mux.func = ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200340
Stephen Warren1e2082b2012-03-02 13:05:48 -0700341 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700342 &groups, &num_groups);
John Crispinad6e1102012-04-26 16:47:11 +0200343 if (ret < 0) {
344 dev_err(pctldev->dev, "can't query groups for function %s\n",
345 map->data.mux.function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200346 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200347 }
348 if (!num_groups) {
349 dev_err(pctldev->dev,
350 "function %s can't be selected on any group\n",
351 map->data.mux.function);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700352 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +0200353 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700354 if (map->data.mux.group) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700355 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700356 group = map->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700357 for (i = 0; i < num_groups; i++) {
358 if (!strcmp(group, groups[i])) {
359 found = true;
360 break;
361 }
362 }
John Crispinad6e1102012-04-26 16:47:11 +0200363 if (!found) {
364 dev_err(pctldev->dev,
365 "invalid group \"%s\" for function \"%s\"\n",
366 group, map->data.mux.function);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700367 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +0200368 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700369 } else {
370 group = groups[0];
371 }
372
John Crispin15f70e12012-04-23 19:01:58 +0200373 ret = pinctrl_get_group_selector(pctldev, group);
John Crispinad6e1102012-04-26 16:47:11 +0200374 if (ret < 0) {
375 dev_err(pctldev->dev, "invalid group %s in map table\n",
376 map->data.mux.group);
John Crispin15f70e12012-04-23 19:01:58 +0200377 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200378 }
John Crispin15f70e12012-04-23 19:01:58 +0200379 setting->data.mux.group = ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700380
Stephen Warren1e2082b2012-03-02 13:05:48 -0700381 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
382 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200383 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700384 dev_err(pctldev->dev,
385 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700386 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700387 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200388 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700389
390 /* Try to allocate all pins in this group, one by one */
391 for (i = 0; i < num_pins; i++) {
392 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
393 if (ret) {
394 dev_err(pctldev->dev,
John Crispinad6e1102012-04-26 16:47:11 +0200395 "could not request pin %d on device %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700396 pins[i], pinctrl_dev_get_name(pctldev));
397 /* On error release all taken pins */
398 i--; /* this pin just failed */
399 for (; i >= 0; i--)
400 pin_free(pctldev, pins[i], NULL);
401 return -ENODEV;
402 }
403 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200404
405 return 0;
406}
407
Stephen Warren7ecdb162012-03-02 13:05:45 -0700408void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100409{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700410 struct pinctrl_dev *pctldev = setting->pctldev;
411 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
412 const unsigned *pins;
413 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100414 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700415 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100416
Stephen Warren1e2082b2012-03-02 13:05:48 -0700417 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700418 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100419 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700420 dev_err(pctldev->dev,
421 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700422 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700423 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100424 }
425
Stephen Warren7ecdb162012-03-02 13:05:45 -0700426 for (i = 0; i < num_pins; i++)
427 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100428}
429
Stephen Warren7ecdb162012-03-02 13:05:45 -0700430int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200431{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700432 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700433 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100434 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700435 int ret;
436 const unsigned *pins;
437 unsigned num_pins;
438 int i;
439 struct pin_desc *desc;
440
441 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
442 &pins, &num_pins);
443 if (ret) {
444 /* errors only affect debug data, so just warn */
445 dev_warn(pctldev->dev,
446 "could not get pins for group selector %d\n",
447 setting->data.mux.group);
448 num_pins = 0;
449 }
450
451 for (i = 0; i < num_pins; i++) {
452 desc = pin_desc_get(pctldev, pins[i]);
453 if (desc == NULL) {
454 dev_warn(pctldev->dev,
455 "could not get pin desc for pin %d\n",
456 pins[i]);
457 continue;
458 }
459 desc->mux_setting = &(setting->data.mux);
460 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200461
Stephen Warren1e2082b2012-03-02 13:05:48 -0700462 return ops->enable(pctldev, setting->data.mux.func,
463 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200464}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200465
Stephen Warren7ecdb162012-03-02 13:05:45 -0700466void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200467{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700468 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700469 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100470 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700471 int ret;
472 const unsigned *pins;
473 unsigned num_pins;
474 int i;
475 struct pin_desc *desc;
476
477 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
478 &pins, &num_pins);
479 if (ret) {
480 /* errors only affect debug data, so just warn */
481 dev_warn(pctldev->dev,
482 "could not get pins for group selector %d\n",
483 setting->data.mux.group);
484 num_pins = 0;
485 }
486
487 for (i = 0; i < num_pins; i++) {
488 desc = pin_desc_get(pctldev, pins[i]);
489 if (desc == NULL) {
490 dev_warn(pctldev->dev,
491 "could not get pin desc for pin %d\n",
492 pins[i]);
493 continue;
494 }
495 desc->mux_setting = NULL;
496 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200497
Stephen Warren1e2082b2012-03-02 13:05:48 -0700498 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200499}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200500
Linus Walleij2744e8a2011-05-02 20:50:54 +0200501#ifdef CONFIG_DEBUG_FS
502
503/* Called from pincontrol core */
504static int pinmux_functions_show(struct seq_file *s, void *what)
505{
506 struct pinctrl_dev *pctldev = s->private;
507 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Dong Aishengad8bb722012-04-10 12:41:34 +0800508 unsigned nfuncs;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200509 unsigned func_selector = 0;
510
Dong Aishengad8bb722012-04-10 12:41:34 +0800511 if (!pmxops)
512 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700513
Dong Aishengad8bb722012-04-10 12:41:34 +0800514 mutex_lock(&pinctrl_mutex);
515 nfuncs = pmxops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +0530516 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200517 const char *func = pmxops->get_function_name(pctldev,
518 func_selector);
519 const char * const *groups;
520 unsigned num_groups;
521 int ret;
522 int i;
523
524 ret = pmxops->get_function_groups(pctldev, func_selector,
525 &groups, &num_groups);
526 if (ret)
527 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
528 func);
529
530 seq_printf(s, "function: %s, groups = [ ", func);
531 for (i = 0; i < num_groups; i++)
532 seq_printf(s, "%s ", groups[i]);
533 seq_puts(s, "]\n");
534
535 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200536 }
537
Stephen Warren57b676f2012-03-02 13:05:44 -0700538 mutex_unlock(&pinctrl_mutex);
539
Linus Walleij2744e8a2011-05-02 20:50:54 +0200540 return 0;
541}
542
543static int pinmux_pins_show(struct seq_file *s, void *what)
544{
545 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700546 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
547 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900548 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200549
Dong Aishengad8bb722012-04-10 12:41:34 +0800550 if (!pmxops)
551 return 0;
552
Linus Walleij2744e8a2011-05-02 20:50:54 +0200553 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren652162d2012-03-05 17:22:15 -0700554 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200555
Stephen Warren57b676f2012-03-02 13:05:44 -0700556 mutex_lock(&pinctrl_mutex);
557
Chanho Park706e8522012-01-03 16:47:50 +0900558 /* The pin number can be retrived from the pin controller descriptor */
559 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200560 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100561 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200562
Chanho Park706e8522012-01-03 16:47:50 +0900563 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200564 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900565 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200566 if (desc == NULL)
567 continue;
568
Stephen Warren652162d2012-03-05 17:22:15 -0700569 if (desc->mux_owner &&
570 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100571 is_hog = true;
572
Stephen Warren652162d2012-03-05 17:22:15 -0700573 seq_printf(s, "pin %d (%s): %s %s%s", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200574 desc->name ? desc->name : "unnamed",
Stephen Warren652162d2012-03-05 17:22:15 -0700575 desc->mux_owner ? desc->mux_owner
576 : "(MUX UNCLAIMED)",
577 desc->gpio_owner ? desc->gpio_owner
578 : "(GPIO UNCLAIMED)",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100579 is_hog ? " (HOG)" : "");
Stephen Warrenba110d92012-03-02 13:05:49 -0700580
581 if (desc->mux_setting)
582 seq_printf(s, " function %s group %s\n",
583 pmxops->get_function_name(pctldev,
584 desc->mux_setting->func),
585 pctlops->get_group_name(pctldev,
586 desc->mux_setting->group));
587 else
588 seq_printf(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200589 }
590
Stephen Warren57b676f2012-03-02 13:05:44 -0700591 mutex_unlock(&pinctrl_mutex);
592
Linus Walleij2744e8a2011-05-02 20:50:54 +0200593 return 0;
594}
595
Stephen Warren1e2082b2012-03-02 13:05:48 -0700596void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
597{
598 seq_printf(s, "group %s\nfunction %s\n",
599 map->data.mux.group ? map->data.mux.group : "(default)",
600 map->data.mux.function);
601}
602
603void pinmux_show_setting(struct seq_file *s,
604 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200605{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700606 struct pinctrl_dev *pctldev = setting->pctldev;
607 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
608 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200609
Stephen Warren1e2082b2012-03-02 13:05:48 -0700610 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
611 pctlops->get_group_name(pctldev, setting->data.mux.group),
612 setting->data.mux.group,
613 pmxops->get_function_name(pctldev, setting->data.mux.func),
614 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200615}
616
617static int pinmux_functions_open(struct inode *inode, struct file *file)
618{
619 return single_open(file, pinmux_functions_show, inode->i_private);
620}
621
622static int pinmux_pins_open(struct inode *inode, struct file *file)
623{
624 return single_open(file, pinmux_pins_show, inode->i_private);
625}
626
Linus Walleij2744e8a2011-05-02 20:50:54 +0200627static const struct file_operations pinmux_functions_ops = {
628 .open = pinmux_functions_open,
629 .read = seq_read,
630 .llseek = seq_lseek,
631 .release = single_release,
632};
633
634static const struct file_operations pinmux_pins_ops = {
635 .open = pinmux_pins_open,
636 .read = seq_read,
637 .llseek = seq_lseek,
638 .release = single_release,
639};
640
Linus Walleij2744e8a2011-05-02 20:50:54 +0200641void pinmux_init_device_debugfs(struct dentry *devroot,
642 struct pinctrl_dev *pctldev)
643{
644 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
645 devroot, pctldev, &pinmux_functions_ops);
646 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
647 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200648}
649
650#endif /* CONFIG_DEBUG_FS */