blob: 7cd0c7d0f2cf7344f7e83a53e0e2d404f4ce5969 [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
John Crispin15f70e12012-04-23 19:01:58 +0200332 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
333 if (ret < 0)
334 return ret;
335 setting->data.mux.func = ret;
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
John Crispin15f70e12012-04-23 19:01:58 +0200359 ret = pinctrl_get_group_selector(pctldev, group);
360 if (ret < 0)
361 return ret;
362 setting->data.mux.group = ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700363
Stephen Warren1e2082b2012-03-02 13:05:48 -0700364 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
365 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200366 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700367 dev_err(pctldev->dev,
368 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700369 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700370 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200371 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700372
373 /* Try to allocate all pins in this group, one by one */
374 for (i = 0; i < num_pins; i++) {
375 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
376 if (ret) {
377 dev_err(pctldev->dev,
378 "could not get request pin %d on device %s\n",
379 pins[i], pinctrl_dev_get_name(pctldev));
380 /* On error release all taken pins */
381 i--; /* this pin just failed */
382 for (; i >= 0; i--)
383 pin_free(pctldev, pins[i], NULL);
384 return -ENODEV;
385 }
386 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200387
388 return 0;
389}
390
Stephen Warren7ecdb162012-03-02 13:05:45 -0700391void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100392{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700393 struct pinctrl_dev *pctldev = setting->pctldev;
394 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
395 const unsigned *pins;
396 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100397 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700398 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100399
Stephen Warren1e2082b2012-03-02 13:05:48 -0700400 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700401 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100402 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700403 dev_err(pctldev->dev,
404 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700405 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700406 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100407 }
408
Stephen Warren7ecdb162012-03-02 13:05:45 -0700409 for (i = 0; i < num_pins; i++)
410 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100411}
412
Stephen Warren7ecdb162012-03-02 13:05:45 -0700413int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200414{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700415 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700416 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100417 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700418 int ret;
419 const unsigned *pins;
420 unsigned num_pins;
421 int i;
422 struct pin_desc *desc;
423
424 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
425 &pins, &num_pins);
426 if (ret) {
427 /* errors only affect debug data, so just warn */
428 dev_warn(pctldev->dev,
429 "could not get pins for group selector %d\n",
430 setting->data.mux.group);
431 num_pins = 0;
432 }
433
434 for (i = 0; i < num_pins; i++) {
435 desc = pin_desc_get(pctldev, pins[i]);
436 if (desc == NULL) {
437 dev_warn(pctldev->dev,
438 "could not get pin desc for pin %d\n",
439 pins[i]);
440 continue;
441 }
442 desc->mux_setting = &(setting->data.mux);
443 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200444
Stephen Warren1e2082b2012-03-02 13:05:48 -0700445 return ops->enable(pctldev, setting->data.mux.func,
446 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200447}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200448
Stephen Warren7ecdb162012-03-02 13:05:45 -0700449void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200450{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700451 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700452 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100453 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700454 int ret;
455 const unsigned *pins;
456 unsigned num_pins;
457 int i;
458 struct pin_desc *desc;
459
460 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
461 &pins, &num_pins);
462 if (ret) {
463 /* errors only affect debug data, so just warn */
464 dev_warn(pctldev->dev,
465 "could not get pins for group selector %d\n",
466 setting->data.mux.group);
467 num_pins = 0;
468 }
469
470 for (i = 0; i < num_pins; i++) {
471 desc = pin_desc_get(pctldev, pins[i]);
472 if (desc == NULL) {
473 dev_warn(pctldev->dev,
474 "could not get pin desc for pin %d\n",
475 pins[i]);
476 continue;
477 }
478 desc->mux_setting = NULL;
479 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200480
Stephen Warren1e2082b2012-03-02 13:05:48 -0700481 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200482}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200483
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484#ifdef CONFIG_DEBUG_FS
485
486/* Called from pincontrol core */
487static int pinmux_functions_show(struct seq_file *s, void *what)
488{
489 struct pinctrl_dev *pctldev = s->private;
490 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Dong Aishengad8bb722012-04-10 12:41:34 +0800491 unsigned nfuncs;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200492 unsigned func_selector = 0;
493
Dong Aishengad8bb722012-04-10 12:41:34 +0800494 if (!pmxops)
495 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700496
Dong Aishengad8bb722012-04-10 12:41:34 +0800497 mutex_lock(&pinctrl_mutex);
498 nfuncs = pmxops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +0530499 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200500 const char *func = pmxops->get_function_name(pctldev,
501 func_selector);
502 const char * const *groups;
503 unsigned num_groups;
504 int ret;
505 int i;
506
507 ret = pmxops->get_function_groups(pctldev, func_selector,
508 &groups, &num_groups);
509 if (ret)
510 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
511 func);
512
513 seq_printf(s, "function: %s, groups = [ ", func);
514 for (i = 0; i < num_groups; i++)
515 seq_printf(s, "%s ", groups[i]);
516 seq_puts(s, "]\n");
517
518 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200519 }
520
Stephen Warren57b676f2012-03-02 13:05:44 -0700521 mutex_unlock(&pinctrl_mutex);
522
Linus Walleij2744e8a2011-05-02 20:50:54 +0200523 return 0;
524}
525
526static int pinmux_pins_show(struct seq_file *s, void *what)
527{
528 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700529 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
530 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900531 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200532
Dong Aishengad8bb722012-04-10 12:41:34 +0800533 if (!pmxops)
534 return 0;
535
Linus Walleij2744e8a2011-05-02 20:50:54 +0200536 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren652162d2012-03-05 17:22:15 -0700537 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200538
Stephen Warren57b676f2012-03-02 13:05:44 -0700539 mutex_lock(&pinctrl_mutex);
540
Chanho Park706e8522012-01-03 16:47:50 +0900541 /* The pin number can be retrived from the pin controller descriptor */
542 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200543 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100544 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200545
Chanho Park706e8522012-01-03 16:47:50 +0900546 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200547 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900548 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200549 if (desc == NULL)
550 continue;
551
Stephen Warren652162d2012-03-05 17:22:15 -0700552 if (desc->mux_owner &&
553 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100554 is_hog = true;
555
Stephen Warren652162d2012-03-05 17:22:15 -0700556 seq_printf(s, "pin %d (%s): %s %s%s", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200557 desc->name ? desc->name : "unnamed",
Stephen Warren652162d2012-03-05 17:22:15 -0700558 desc->mux_owner ? desc->mux_owner
559 : "(MUX UNCLAIMED)",
560 desc->gpio_owner ? desc->gpio_owner
561 : "(GPIO UNCLAIMED)",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100562 is_hog ? " (HOG)" : "");
Stephen Warrenba110d92012-03-02 13:05:49 -0700563
564 if (desc->mux_setting)
565 seq_printf(s, " function %s group %s\n",
566 pmxops->get_function_name(pctldev,
567 desc->mux_setting->func),
568 pctlops->get_group_name(pctldev,
569 desc->mux_setting->group));
570 else
571 seq_printf(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200572 }
573
Stephen Warren57b676f2012-03-02 13:05:44 -0700574 mutex_unlock(&pinctrl_mutex);
575
Linus Walleij2744e8a2011-05-02 20:50:54 +0200576 return 0;
577}
578
Stephen Warren1e2082b2012-03-02 13:05:48 -0700579void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
580{
581 seq_printf(s, "group %s\nfunction %s\n",
582 map->data.mux.group ? map->data.mux.group : "(default)",
583 map->data.mux.function);
584}
585
586void pinmux_show_setting(struct seq_file *s,
587 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200588{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700589 struct pinctrl_dev *pctldev = setting->pctldev;
590 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
591 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200592
Stephen Warren1e2082b2012-03-02 13:05:48 -0700593 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
594 pctlops->get_group_name(pctldev, setting->data.mux.group),
595 setting->data.mux.group,
596 pmxops->get_function_name(pctldev, setting->data.mux.func),
597 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200598}
599
600static int pinmux_functions_open(struct inode *inode, struct file *file)
601{
602 return single_open(file, pinmux_functions_show, inode->i_private);
603}
604
605static int pinmux_pins_open(struct inode *inode, struct file *file)
606{
607 return single_open(file, pinmux_pins_show, inode->i_private);
608}
609
Linus Walleij2744e8a2011-05-02 20:50:54 +0200610static const struct file_operations pinmux_functions_ops = {
611 .open = pinmux_functions_open,
612 .read = seq_read,
613 .llseek = seq_lseek,
614 .release = single_release,
615};
616
617static const struct file_operations pinmux_pins_ops = {
618 .open = pinmux_pins_open,
619 .read = seq_read,
620 .llseek = seq_lseek,
621 .release = single_release,
622};
623
Linus Walleij2744e8a2011-05-02 20:50:54 +0200624void pinmux_init_device_debugfs(struct dentry *devroot,
625 struct pinctrl_dev *pctldev)
626{
627 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
628 devroot, pctldev, &pinmux_functions_ops);
629 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
630 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200631}
632
633#endif /* CONFIG_DEBUG_FS */