blob: fa0357bd88ff638ed28f36ce8c84c9d167a80f6e [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 ||
45 !ops->disable)
46 return -EINVAL;
47
48 /* Check that all functions registered have names */
Dong Aishenga1d31f72012-04-06 20:18:09 +080049 nfuncs = ops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053050 while (selector < nfuncs) {
Stephen Warren03665e02012-02-19 23:45:45 -070051 const char *fname = ops->get_function_name(pctldev,
52 selector);
53 if (!fname) {
Dong Aishenga1d31f72012-04-06 20:18:09 +080054 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
Stephen Warren03665e02012-02-19 23:45:45 -070055 selector);
56 return -EINVAL;
57 }
58 selector++;
59 }
60
61 return 0;
62}
63
Stephen Warren1e2082b2012-03-02 13:05:48 -070064int pinmux_validate_map(struct pinctrl_map const *map, int i)
65{
66 if (!map->data.mux.function) {
67 pr_err("failed to register map %s (%d): no function given\n",
68 map->name, i);
69 return -EINVAL;
70 }
71
72 return 0;
73}
74
Linus Walleij2744e8a2011-05-02 20:50:54 +020075/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020076 * pin_request() - request a single pin to be muxed in, typically for GPIO
77 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070078 * @owner: a representation of the owner of this pin; typically the device
79 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020080 * @gpio_range: the range matching the GPIO pin if this is a request for a
81 * single GPIO pin
82 */
83static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070084 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020085 struct pinctrl_gpio_range *gpio_range)
86{
87 struct pin_desc *desc;
88 const struct pinmux_ops *ops = pctldev->desc->pmxops;
89 int status = -EINVAL;
90
Linus Walleij2744e8a2011-05-02 20:50:54 +020091 desc = pin_desc_get(pctldev, pin);
92 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070093 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020094 "pin is not registered so it cannot be requested\n");
95 goto out;
96 }
97
Dong Aishengd0bd8df2012-04-17 15:00:45 +080098 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
99 pin, desc->name, owner);
100
Stephen Warren652162d2012-03-05 17:22:15 -0700101 if (gpio_range) {
102 /* There's no need to support multiple GPIO requests */
103 if (desc->gpio_owner) {
104 dev_err(pctldev->dev,
105 "pin already requested\n");
106 goto out;
107 }
108
109 desc->gpio_owner = owner;
110 } else {
111 if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
112 dev_err(pctldev->dev,
113 "pin already requested\n");
114 goto out;
115 }
116
117 desc->mux_usecount++;
118 if (desc->mux_usecount > 1)
119 return 0;
120
121 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200122 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700123
Linus Walleij2744e8a2011-05-02 20:50:54 +0200124 /* Let each pin increase references to this module */
125 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700126 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200127 "could not increase module refcount for pin %d\n",
128 pin);
129 status = -EINVAL;
130 goto out_free_pin;
131 }
132
133 /*
134 * If there is no kind of request function for the pin we just assume
135 * we got it by default and proceed.
136 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600137 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200138 /* This requests and enables a single GPIO pin */
139 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
140 else if (ops->request)
141 status = ops->request(pctldev, pin);
142 else
143 status = 0;
144
Stephen Warren0e3db1732012-03-02 13:05:46 -0700145 if (status) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100146 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200147 pctldev->desc->name, pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700148 module_put(pctldev->owner);
149 }
150
Linus Walleij2744e8a2011-05-02 20:50:54 +0200151out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700152 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700153 if (gpio_range) {
154 desc->gpio_owner = NULL;
155 } else {
156 desc->mux_usecount--;
157 if (!desc->mux_usecount)
158 desc->mux_owner = NULL;
159 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700160 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200161out:
162 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700163 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700164 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200165
166 return status;
167}
168
169/**
170 * pin_free() - release a single muxed in pin so something else can be muxed
171 * @pctldev: pin controller device handling this pin
172 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600173 * @gpio_range: the range matching the GPIO pin if this is a request for a
174 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100175 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700176 * This function returns a pointer to the previous owner. This is used
177 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100178 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200179 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600180static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
181 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182{
183 const struct pinmux_ops *ops = pctldev->desc->pmxops;
184 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700185 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186
187 desc = pin_desc_get(pctldev, pin);
188 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700189 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200190 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600191 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192 }
193
Stephen Warren652162d2012-03-05 17:22:15 -0700194 if (!gpio_range) {
195 desc->mux_usecount--;
196 if (desc->mux_usecount)
197 return NULL;
198 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700199
Stephen Warren3712a3c2011-10-21 12:25:53 -0600200 /*
201 * If there is no kind of request function for the pin we just assume
202 * we got it by default and proceed.
203 */
204 if (gpio_range && ops->gpio_disable_free)
205 ops->gpio_disable_free(pctldev, gpio_range, pin);
206 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207 ops->free(pctldev, pin);
208
Stephen Warren652162d2012-03-05 17:22:15 -0700209 if (gpio_range) {
210 owner = desc->gpio_owner;
211 desc->gpio_owner = NULL;
212 } else {
213 owner = desc->mux_owner;
214 desc->mux_owner = NULL;
215 desc->mux_setting = NULL;
216 }
217
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600219
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700220 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221}
222
223/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100224 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
225 * @pctldev: pin controller device affected
226 * @pin: the pin to mux in for GPIO
227 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100229int pinmux_request_gpio(struct pinctrl_dev *pctldev,
230 struct pinctrl_gpio_range *range,
231 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232{
233 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700234 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236
237 /* Conjure some name stating what chip and pin this is taken by */
238 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
239
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700240 owner = kstrdup(gpiostr, GFP_KERNEL);
241 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600242 return -EINVAL;
243
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700244 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600245 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700246 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600247
248 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200249}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250
251/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100252 * pinmux_free_gpio() - release a pin from GPIO muxing
253 * @pctldev: the pin controller device for the pin
254 * @pin: the affected currently GPIO-muxed in pin
255 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200256 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100257void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
258 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700260 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700262 owner = pin_free(pctldev, pin, range);
263 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200264}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100266/**
267 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
268 * @pctldev: the pin controller handling this pin
269 * @range: applicable GPIO range
270 * @pin: the affected GPIO pin in this controller
271 * @input: true if we set the pin as input, false for output
272 */
273int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
274 struct pinctrl_gpio_range *range,
275 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100276{
Linus Walleij542e7042011-11-14 10:06:22 +0100277 const struct pinmux_ops *ops;
278 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100279
280 ops = pctldev->desc->pmxops;
281
Linus Walleij542e7042011-11-14 10:06:22 +0100282 if (ops->gpio_set_direction)
283 ret = ops->gpio_set_direction(pctldev, range, pin, input);
284 else
285 ret = 0;
286
287 return ret;
288}
289
Stephen Warren7ecdb162012-03-02 13:05:45 -0700290static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
291 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200292{
293 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530294 unsigned nfuncs = ops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295 unsigned selector = 0;
296
297 /* See if this pctldev has this function */
Viresh Kumard1e90e92012-03-30 11:25:40 +0530298 while (selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200299 const char *fname = ops->get_function_name(pctldev,
300 selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200301
Stephen Warren7ecdb162012-03-02 13:05:45 -0700302 if (!strcmp(function, fname))
303 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200304
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 selector++;
306 }
307
308 pr_err("%s does not support function %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700309 pinctrl_dev_get_name(pctldev), function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310 return -EINVAL;
311}
312
Stephen Warren7ecdb162012-03-02 13:05:45 -0700313int pinmux_map_to_setting(struct pinctrl_map const *map,
314 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200315{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700316 struct pinctrl_dev *pctldev = setting->pctldev;
317 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
318 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
319 char const * const *groups;
320 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200321 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700322 const char *group;
323 int i;
324 const unsigned *pins;
325 unsigned num_pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200326
Dong Aishengad8bb722012-04-10 12:41:34 +0800327 if (!pmxops) {
328 dev_err(pctldev->dev, "does not support mux function\n");
329 return -EINVAL;
330 }
331
Stephen Warren1e2082b2012-03-02 13:05:48 -0700332 setting->data.mux.func =
333 pinmux_func_name_to_selector(pctldev, map->data.mux.function);
334 if (setting->data.mux.func < 0)
335 return setting->data.mux.func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336
Stephen Warren1e2082b2012-03-02 13:05:48 -0700337 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700338 &groups, &num_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200339 if (ret < 0)
340 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700341 if (!num_groups)
342 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200343
Stephen Warren1e2082b2012-03-02 13:05:48 -0700344 if (map->data.mux.group) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700345 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700346 group = map->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700347 for (i = 0; i < num_groups; i++) {
348 if (!strcmp(group, groups[i])) {
349 found = true;
350 break;
351 }
352 }
353 if (!found)
354 return -EINVAL;
355 } else {
356 group = groups[0];
357 }
358
Stephen Warren1e2082b2012-03-02 13:05:48 -0700359 setting->data.mux.group = pinctrl_get_group_selector(pctldev, group);
360 if (setting->data.mux.group < 0)
361 return setting->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700362
Stephen Warren1e2082b2012-03-02 13:05:48 -0700363 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
364 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200365 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700366 dev_err(pctldev->dev,
367 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700368 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700369 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200370 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700371
372 /* Try to allocate all pins in this group, one by one */
373 for (i = 0; i < num_pins; i++) {
374 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
375 if (ret) {
376 dev_err(pctldev->dev,
377 "could not get request pin %d on device %s\n",
378 pins[i], pinctrl_dev_get_name(pctldev));
379 /* On error release all taken pins */
380 i--; /* this pin just failed */
381 for (; i >= 0; i--)
382 pin_free(pctldev, pins[i], NULL);
383 return -ENODEV;
384 }
385 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200386
387 return 0;
388}
389
Stephen Warren7ecdb162012-03-02 13:05:45 -0700390void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100391{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700392 struct pinctrl_dev *pctldev = setting->pctldev;
393 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
394 const unsigned *pins;
395 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100396 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700397 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398
Stephen Warren1e2082b2012-03-02 13:05:48 -0700399 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700400 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100401 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700402 dev_err(pctldev->dev,
403 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700404 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700405 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100406 }
407
Stephen Warren7ecdb162012-03-02 13:05:45 -0700408 for (i = 0; i < num_pins; i++)
409 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100410}
411
Stephen Warren7ecdb162012-03-02 13:05:45 -0700412int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200413{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700414 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700415 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100416 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700417 int ret;
418 const unsigned *pins;
419 unsigned num_pins;
420 int i;
421 struct pin_desc *desc;
422
423 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
424 &pins, &num_pins);
425 if (ret) {
426 /* errors only affect debug data, so just warn */
427 dev_warn(pctldev->dev,
428 "could not get pins for group selector %d\n",
429 setting->data.mux.group);
430 num_pins = 0;
431 }
432
433 for (i = 0; i < num_pins; i++) {
434 desc = pin_desc_get(pctldev, pins[i]);
435 if (desc == NULL) {
436 dev_warn(pctldev->dev,
437 "could not get pin desc for pin %d\n",
438 pins[i]);
439 continue;
440 }
441 desc->mux_setting = &(setting->data.mux);
442 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200443
Stephen Warren1e2082b2012-03-02 13:05:48 -0700444 return ops->enable(pctldev, setting->data.mux.func,
445 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200446}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200447
Stephen Warren7ecdb162012-03-02 13:05:45 -0700448void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200449{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700450 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700451 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100452 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700453 int ret;
454 const unsigned *pins;
455 unsigned num_pins;
456 int i;
457 struct pin_desc *desc;
458
459 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
460 &pins, &num_pins);
461 if (ret) {
462 /* errors only affect debug data, so just warn */
463 dev_warn(pctldev->dev,
464 "could not get pins for group selector %d\n",
465 setting->data.mux.group);
466 num_pins = 0;
467 }
468
469 for (i = 0; i < num_pins; i++) {
470 desc = pin_desc_get(pctldev, pins[i]);
471 if (desc == NULL) {
472 dev_warn(pctldev->dev,
473 "could not get pin desc for pin %d\n",
474 pins[i]);
475 continue;
476 }
477 desc->mux_setting = NULL;
478 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200479
Stephen Warren1e2082b2012-03-02 13:05:48 -0700480 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200481}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200482
Linus Walleij2744e8a2011-05-02 20:50:54 +0200483#ifdef CONFIG_DEBUG_FS
484
485/* Called from pincontrol core */
486static int pinmux_functions_show(struct seq_file *s, void *what)
487{
488 struct pinctrl_dev *pctldev = s->private;
489 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Dong Aishengad8bb722012-04-10 12:41:34 +0800490 unsigned nfuncs;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200491 unsigned func_selector = 0;
492
Dong Aishengad8bb722012-04-10 12:41:34 +0800493 if (!pmxops)
494 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700495
Dong Aishengad8bb722012-04-10 12:41:34 +0800496 mutex_lock(&pinctrl_mutex);
497 nfuncs = pmxops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +0530498 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200499 const char *func = pmxops->get_function_name(pctldev,
500 func_selector);
501 const char * const *groups;
502 unsigned num_groups;
503 int ret;
504 int i;
505
506 ret = pmxops->get_function_groups(pctldev, func_selector,
507 &groups, &num_groups);
508 if (ret)
509 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
510 func);
511
512 seq_printf(s, "function: %s, groups = [ ", func);
513 for (i = 0; i < num_groups; i++)
514 seq_printf(s, "%s ", groups[i]);
515 seq_puts(s, "]\n");
516
517 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200518 }
519
Stephen Warren57b676f2012-03-02 13:05:44 -0700520 mutex_unlock(&pinctrl_mutex);
521
Linus Walleij2744e8a2011-05-02 20:50:54 +0200522 return 0;
523}
524
525static int pinmux_pins_show(struct seq_file *s, void *what)
526{
527 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700528 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
529 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900530 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200531
Dong Aishengad8bb722012-04-10 12:41:34 +0800532 if (!pmxops)
533 return 0;
534
Linus Walleij2744e8a2011-05-02 20:50:54 +0200535 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren652162d2012-03-05 17:22:15 -0700536 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200537
Stephen Warren57b676f2012-03-02 13:05:44 -0700538 mutex_lock(&pinctrl_mutex);
539
Chanho Park706e8522012-01-03 16:47:50 +0900540 /* The pin number can be retrived from the pin controller descriptor */
541 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200542 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100543 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200544
Chanho Park706e8522012-01-03 16:47:50 +0900545 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200546 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900547 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200548 if (desc == NULL)
549 continue;
550
Stephen Warren652162d2012-03-05 17:22:15 -0700551 if (desc->mux_owner &&
552 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100553 is_hog = true;
554
Stephen Warren652162d2012-03-05 17:22:15 -0700555 seq_printf(s, "pin %d (%s): %s %s%s", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200556 desc->name ? desc->name : "unnamed",
Stephen Warren652162d2012-03-05 17:22:15 -0700557 desc->mux_owner ? desc->mux_owner
558 : "(MUX UNCLAIMED)",
559 desc->gpio_owner ? desc->gpio_owner
560 : "(GPIO UNCLAIMED)",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100561 is_hog ? " (HOG)" : "");
Stephen Warrenba110d92012-03-02 13:05:49 -0700562
563 if (desc->mux_setting)
564 seq_printf(s, " function %s group %s\n",
565 pmxops->get_function_name(pctldev,
566 desc->mux_setting->func),
567 pctlops->get_group_name(pctldev,
568 desc->mux_setting->group));
569 else
570 seq_printf(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200571 }
572
Stephen Warren57b676f2012-03-02 13:05:44 -0700573 mutex_unlock(&pinctrl_mutex);
574
Linus Walleij2744e8a2011-05-02 20:50:54 +0200575 return 0;
576}
577
Stephen Warren1e2082b2012-03-02 13:05:48 -0700578void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
579{
580 seq_printf(s, "group %s\nfunction %s\n",
581 map->data.mux.group ? map->data.mux.group : "(default)",
582 map->data.mux.function);
583}
584
585void pinmux_show_setting(struct seq_file *s,
586 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200587{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700588 struct pinctrl_dev *pctldev = setting->pctldev;
589 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
590 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200591
Stephen Warren1e2082b2012-03-02 13:05:48 -0700592 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
593 pctlops->get_group_name(pctldev, setting->data.mux.group),
594 setting->data.mux.group,
595 pmxops->get_function_name(pctldev, setting->data.mux.func),
596 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200597}
598
599static int pinmux_functions_open(struct inode *inode, struct file *file)
600{
601 return single_open(file, pinmux_functions_show, inode->i_private);
602}
603
604static int pinmux_pins_open(struct inode *inode, struct file *file)
605{
606 return single_open(file, pinmux_pins_show, inode->i_private);
607}
608
Linus Walleij2744e8a2011-05-02 20:50:54 +0200609static const struct file_operations pinmux_functions_ops = {
610 .open = pinmux_functions_open,
611 .read = seq_read,
612 .llseek = seq_lseek,
613 .release = single_release,
614};
615
616static const struct file_operations pinmux_pins_ops = {
617 .open = pinmux_pins_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621};
622
Linus Walleij2744e8a2011-05-02 20:50:54 +0200623void pinmux_init_device_debugfs(struct dentry *devroot,
624 struct pinctrl_dev *pctldev)
625{
626 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
627 devroot, pctldev, &pinmux_functions_ops);
628 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
629 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200630}
631
632#endif /* CONFIG_DEBUG_FS */