blob: 375b214780e938ca25646757318f996fb351009f [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;
Viresh Kumard1e90e92012-03-30 11:25:40 +053036 unsigned nfuncs = ops->get_functions_count(pctldev);
Stephen Warren03665e02012-02-19 23:45:45 -070037 unsigned selector = 0;
38
39 /* Check that we implement required operations */
Viresh Kumard1e90e92012-03-30 11:25:40 +053040 if (!ops->get_functions_count ||
Stephen Warren03665e02012-02-19 23:45:45 -070041 !ops->get_function_name ||
42 !ops->get_function_groups ||
43 !ops->enable ||
44 !ops->disable)
45 return -EINVAL;
46
47 /* Check that all functions registered have names */
Viresh Kumard1e90e92012-03-30 11:25:40 +053048 while (selector < nfuncs) {
Stephen Warren03665e02012-02-19 23:45:45 -070049 const char *fname = ops->get_function_name(pctldev,
50 selector);
51 if (!fname) {
52 pr_err("pinmux ops has no name for function%u\n",
53 selector);
54 return -EINVAL;
55 }
56 selector++;
57 }
58
59 return 0;
60}
61
Stephen Warren1e2082b2012-03-02 13:05:48 -070062int pinmux_validate_map(struct pinctrl_map const *map, int i)
63{
64 if (!map->data.mux.function) {
65 pr_err("failed to register map %s (%d): no function given\n",
66 map->name, i);
67 return -EINVAL;
68 }
69
70 return 0;
71}
72
Linus Walleij2744e8a2011-05-02 20:50:54 +020073/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020074 * pin_request() - request a single pin to be muxed in, typically for GPIO
75 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070076 * @owner: a representation of the owner of this pin; typically the device
77 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020078 * @gpio_range: the range matching the GPIO pin if this is a request for a
79 * single GPIO pin
80 */
81static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070082 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020083 struct pinctrl_gpio_range *gpio_range)
84{
85 struct pin_desc *desc;
86 const struct pinmux_ops *ops = pctldev->desc->pmxops;
87 int status = -EINVAL;
88
Stephen Warren3cc70ed2012-02-19 23:45:44 -070089 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +020090
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
Stephen Warren652162d2012-03-05 17:22:15 -070098 if (gpio_range) {
99 /* There's no need to support multiple GPIO requests */
100 if (desc->gpio_owner) {
101 dev_err(pctldev->dev,
102 "pin already requested\n");
103 goto out;
104 }
105
106 desc->gpio_owner = owner;
107 } else {
108 if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
109 dev_err(pctldev->dev,
110 "pin already requested\n");
111 goto out;
112 }
113
114 desc->mux_usecount++;
115 if (desc->mux_usecount > 1)
116 return 0;
117
118 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200119 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700120
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121 /* Let each pin increase references to this module */
122 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700123 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200124 "could not increase module refcount for pin %d\n",
125 pin);
126 status = -EINVAL;
127 goto out_free_pin;
128 }
129
130 /*
131 * If there is no kind of request function for the pin we just assume
132 * we got it by default and proceed.
133 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600134 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200135 /* This requests and enables a single GPIO pin */
136 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
137 else if (ops->request)
138 status = ops->request(pctldev, pin);
139 else
140 status = 0;
141
Stephen Warren0e3db1732012-03-02 13:05:46 -0700142 if (status) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100143 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200144 pctldev->desc->name, pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700145 module_put(pctldev->owner);
146 }
147
Linus Walleij2744e8a2011-05-02 20:50:54 +0200148out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700149 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700150 if (gpio_range) {
151 desc->gpio_owner = NULL;
152 } else {
153 desc->mux_usecount--;
154 if (!desc->mux_usecount)
155 desc->mux_owner = NULL;
156 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700157 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200158out:
159 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700160 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700161 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200162
163 return status;
164}
165
166/**
167 * pin_free() - release a single muxed in pin so something else can be muxed
168 * @pctldev: pin controller device handling this pin
169 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600170 * @gpio_range: the range matching the GPIO pin if this is a request for a
171 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100172 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700173 * This function returns a pointer to the previous owner. This is used
174 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100175 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200176 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600177static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
178 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200179{
180 const struct pinmux_ops *ops = pctldev->desc->pmxops;
181 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700182 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183
184 desc = pin_desc_get(pctldev, pin);
185 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700186 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600188 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189 }
190
Stephen Warren652162d2012-03-05 17:22:15 -0700191 if (!gpio_range) {
192 desc->mux_usecount--;
193 if (desc->mux_usecount)
194 return NULL;
195 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700196
Stephen Warren3712a3c2011-10-21 12:25:53 -0600197 /*
198 * If there is no kind of request function for the pin we just assume
199 * we got it by default and proceed.
200 */
201 if (gpio_range && ops->gpio_disable_free)
202 ops->gpio_disable_free(pctldev, gpio_range, pin);
203 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204 ops->free(pctldev, pin);
205
Stephen Warren652162d2012-03-05 17:22:15 -0700206 if (gpio_range) {
207 owner = desc->gpio_owner;
208 desc->gpio_owner = NULL;
209 } else {
210 owner = desc->mux_owner;
211 desc->mux_owner = NULL;
212 desc->mux_setting = NULL;
213 }
214
Linus Walleij2744e8a2011-05-02 20:50:54 +0200215 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600216
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700217 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218}
219
220/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100221 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
222 * @pctldev: pin controller device affected
223 * @pin: the pin to mux in for GPIO
224 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100226int pinmux_request_gpio(struct pinctrl_dev *pctldev,
227 struct pinctrl_gpio_range *range,
228 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229{
230 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700231 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233
234 /* Conjure some name stating what chip and pin this is taken by */
235 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
236
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700237 owner = kstrdup(gpiostr, GFP_KERNEL);
238 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600239 return -EINVAL;
240
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700241 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600242 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700243 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600244
245 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247
248/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100249 * pinmux_free_gpio() - release a pin from GPIO muxing
250 * @pctldev: the pin controller device for the pin
251 * @pin: the affected currently GPIO-muxed in pin
252 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200253 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100254void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
255 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200256{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700257 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200258
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700259 owner = pin_free(pctldev, pin, range);
260 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100263/**
264 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
265 * @pctldev: the pin controller handling this pin
266 * @range: applicable GPIO range
267 * @pin: the affected GPIO pin in this controller
268 * @input: true if we set the pin as input, false for output
269 */
270int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
271 struct pinctrl_gpio_range *range,
272 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100273{
Linus Walleij542e7042011-11-14 10:06:22 +0100274 const struct pinmux_ops *ops;
275 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100276
277 ops = pctldev->desc->pmxops;
278
Linus Walleij542e7042011-11-14 10:06:22 +0100279 if (ops->gpio_set_direction)
280 ret = ops->gpio_set_direction(pctldev, range, pin, input);
281 else
282 ret = 0;
283
284 return ret;
285}
286
Stephen Warren7ecdb162012-03-02 13:05:45 -0700287static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
288 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200289{
290 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530291 unsigned nfuncs = ops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200292 unsigned selector = 0;
293
294 /* See if this pctldev has this function */
Viresh Kumard1e90e92012-03-30 11:25:40 +0530295 while (selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 const char *fname = ops->get_function_name(pctldev,
297 selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200298
Stephen Warren7ecdb162012-03-02 13:05:45 -0700299 if (!strcmp(function, fname))
300 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200301
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302 selector++;
303 }
304
305 pr_err("%s does not support function %s\n",
Stephen Warren7ecdb162012-03-02 13:05:45 -0700306 pinctrl_dev_get_name(pctldev), function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 return -EINVAL;
308}
309
Stephen Warren7ecdb162012-03-02 13:05:45 -0700310int pinmux_map_to_setting(struct pinctrl_map const *map,
311 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200312{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700313 struct pinctrl_dev *pctldev = setting->pctldev;
314 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
315 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
316 char const * const *groups;
317 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200318 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700319 const char *group;
320 int i;
321 const unsigned *pins;
322 unsigned num_pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323
Stephen Warren1e2082b2012-03-02 13:05:48 -0700324 setting->data.mux.func =
325 pinmux_func_name_to_selector(pctldev, map->data.mux.function);
326 if (setting->data.mux.func < 0)
327 return setting->data.mux.func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200328
Stephen Warren1e2082b2012-03-02 13:05:48 -0700329 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700330 &groups, &num_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331 if (ret < 0)
332 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700333 if (!num_groups)
334 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200335
Stephen Warren1e2082b2012-03-02 13:05:48 -0700336 if (map->data.mux.group) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700337 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700338 group = map->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700339 for (i = 0; i < num_groups; i++) {
340 if (!strcmp(group, groups[i])) {
341 found = true;
342 break;
343 }
344 }
345 if (!found)
346 return -EINVAL;
347 } else {
348 group = groups[0];
349 }
350
Stephen Warren1e2082b2012-03-02 13:05:48 -0700351 setting->data.mux.group = pinctrl_get_group_selector(pctldev, group);
352 if (setting->data.mux.group < 0)
353 return setting->data.mux.group;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700354
Stephen Warren1e2082b2012-03-02 13:05:48 -0700355 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
356 &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700358 dev_err(pctldev->dev,
359 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700360 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700361 return -ENODEV;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200362 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700363
364 /* Try to allocate all pins in this group, one by one */
365 for (i = 0; i < num_pins; i++) {
366 ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
367 if (ret) {
368 dev_err(pctldev->dev,
369 "could not get request pin %d on device %s\n",
370 pins[i], pinctrl_dev_get_name(pctldev));
371 /* On error release all taken pins */
372 i--; /* this pin just failed */
373 for (; i >= 0; i--)
374 pin_free(pctldev, pins[i], NULL);
375 return -ENODEV;
376 }
377 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200378
379 return 0;
380}
381
Stephen Warren7ecdb162012-03-02 13:05:45 -0700382void pinmux_free_setting(struct pinctrl_setting const *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100383{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700384 struct pinctrl_dev *pctldev = setting->pctldev;
385 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
386 const unsigned *pins;
387 unsigned num_pins;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100388 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700389 int i;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100390
Stephen Warren1e2082b2012-03-02 13:05:48 -0700391 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700392 &pins, &num_pins);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100393 if (ret) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700394 dev_err(pctldev->dev,
395 "could not get pins for device %s group selector %d\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -0700396 pinctrl_dev_get_name(pctldev), setting->data.mux.group);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700397 return;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398 }
399
Stephen Warren7ecdb162012-03-02 13:05:45 -0700400 for (i = 0; i < num_pins; i++)
401 pin_free(pctldev, pins[i], NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100402}
403
Stephen Warren7ecdb162012-03-02 13:05:45 -0700404int pinmux_enable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200405{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700406 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700407 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100408 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700409 int ret;
410 const unsigned *pins;
411 unsigned num_pins;
412 int i;
413 struct pin_desc *desc;
414
415 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
416 &pins, &num_pins);
417 if (ret) {
418 /* errors only affect debug data, so just warn */
419 dev_warn(pctldev->dev,
420 "could not get pins for group selector %d\n",
421 setting->data.mux.group);
422 num_pins = 0;
423 }
424
425 for (i = 0; i < num_pins; i++) {
426 desc = pin_desc_get(pctldev, pins[i]);
427 if (desc == NULL) {
428 dev_warn(pctldev->dev,
429 "could not get pin desc for pin %d\n",
430 pins[i]);
431 continue;
432 }
433 desc->mux_setting = &(setting->data.mux);
434 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200435
Stephen Warren1e2082b2012-03-02 13:05:48 -0700436 return ops->enable(pctldev, setting->data.mux.func,
437 setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200438}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200439
Stephen Warren7ecdb162012-03-02 13:05:45 -0700440void pinmux_disable_setting(struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200441{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700442 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700443 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100444 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Stephen Warrenba110d92012-03-02 13:05:49 -0700445 int ret;
446 const unsigned *pins;
447 unsigned num_pins;
448 int i;
449 struct pin_desc *desc;
450
451 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
452 &pins, &num_pins);
453 if (ret) {
454 /* errors only affect debug data, so just warn */
455 dev_warn(pctldev->dev,
456 "could not get pins for group selector %d\n",
457 setting->data.mux.group);
458 num_pins = 0;
459 }
460
461 for (i = 0; i < num_pins; i++) {
462 desc = pin_desc_get(pctldev, pins[i]);
463 if (desc == NULL) {
464 dev_warn(pctldev->dev,
465 "could not get pin desc for pin %d\n",
466 pins[i]);
467 continue;
468 }
469 desc->mux_setting = NULL;
470 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200471
Stephen Warren1e2082b2012-03-02 13:05:48 -0700472 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200473}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200474
Linus Walleij2744e8a2011-05-02 20:50:54 +0200475#ifdef CONFIG_DEBUG_FS
476
477/* Called from pincontrol core */
478static int pinmux_functions_show(struct seq_file *s, void *what)
479{
480 struct pinctrl_dev *pctldev = s->private;
481 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530482 unsigned nfuncs = pmxops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200483 unsigned func_selector = 0;
484
Stephen Warren57b676f2012-03-02 13:05:44 -0700485 mutex_lock(&pinctrl_mutex);
486
Viresh Kumard1e90e92012-03-30 11:25:40 +0530487 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200488 const char *func = pmxops->get_function_name(pctldev,
489 func_selector);
490 const char * const *groups;
491 unsigned num_groups;
492 int ret;
493 int i;
494
495 ret = pmxops->get_function_groups(pctldev, func_selector,
496 &groups, &num_groups);
497 if (ret)
498 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
499 func);
500
501 seq_printf(s, "function: %s, groups = [ ", func);
502 for (i = 0; i < num_groups; i++)
503 seq_printf(s, "%s ", groups[i]);
504 seq_puts(s, "]\n");
505
506 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200507 }
508
Stephen Warren57b676f2012-03-02 13:05:44 -0700509 mutex_unlock(&pinctrl_mutex);
510
Linus Walleij2744e8a2011-05-02 20:50:54 +0200511 return 0;
512}
513
514static int pinmux_pins_show(struct seq_file *s, void *what)
515{
516 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700517 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
518 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900519 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200520
521 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren652162d2012-03-05 17:22:15 -0700522 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200523
Stephen Warren57b676f2012-03-02 13:05:44 -0700524 mutex_lock(&pinctrl_mutex);
525
Chanho Park706e8522012-01-03 16:47:50 +0900526 /* The pin number can be retrived from the pin controller descriptor */
527 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200528 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100529 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200530
Chanho Park706e8522012-01-03 16:47:50 +0900531 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200532 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900533 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200534 if (desc == NULL)
535 continue;
536
Stephen Warren652162d2012-03-05 17:22:15 -0700537 if (desc->mux_owner &&
538 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100539 is_hog = true;
540
Stephen Warren652162d2012-03-05 17:22:15 -0700541 seq_printf(s, "pin %d (%s): %s %s%s", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200542 desc->name ? desc->name : "unnamed",
Stephen Warren652162d2012-03-05 17:22:15 -0700543 desc->mux_owner ? desc->mux_owner
544 : "(MUX UNCLAIMED)",
545 desc->gpio_owner ? desc->gpio_owner
546 : "(GPIO UNCLAIMED)",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100547 is_hog ? " (HOG)" : "");
Stephen Warrenba110d92012-03-02 13:05:49 -0700548
549 if (desc->mux_setting)
550 seq_printf(s, " function %s group %s\n",
551 pmxops->get_function_name(pctldev,
552 desc->mux_setting->func),
553 pctlops->get_group_name(pctldev,
554 desc->mux_setting->group));
555 else
556 seq_printf(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200557 }
558
Stephen Warren57b676f2012-03-02 13:05:44 -0700559 mutex_unlock(&pinctrl_mutex);
560
Linus Walleij2744e8a2011-05-02 20:50:54 +0200561 return 0;
562}
563
Stephen Warren1e2082b2012-03-02 13:05:48 -0700564void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
565{
566 seq_printf(s, "group %s\nfunction %s\n",
567 map->data.mux.group ? map->data.mux.group : "(default)",
568 map->data.mux.function);
569}
570
571void pinmux_show_setting(struct seq_file *s,
572 struct pinctrl_setting const *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200573{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700574 struct pinctrl_dev *pctldev = setting->pctldev;
575 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
576 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200577
Stephen Warren1e2082b2012-03-02 13:05:48 -0700578 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
579 pctlops->get_group_name(pctldev, setting->data.mux.group),
580 setting->data.mux.group,
581 pmxops->get_function_name(pctldev, setting->data.mux.func),
582 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200583}
584
585static int pinmux_functions_open(struct inode *inode, struct file *file)
586{
587 return single_open(file, pinmux_functions_show, inode->i_private);
588}
589
590static int pinmux_pins_open(struct inode *inode, struct file *file)
591{
592 return single_open(file, pinmux_pins_show, inode->i_private);
593}
594
Linus Walleij2744e8a2011-05-02 20:50:54 +0200595static const struct file_operations pinmux_functions_ops = {
596 .open = pinmux_functions_open,
597 .read = seq_read,
598 .llseek = seq_lseek,
599 .release = single_release,
600};
601
602static const struct file_operations pinmux_pins_ops = {
603 .open = pinmux_pins_open,
604 .read = seq_read,
605 .llseek = seq_lseek,
606 .release = single_release,
607};
608
Linus Walleij2744e8a2011-05-02 20:50:54 +0200609void pinmux_init_device_debugfs(struct dentry *devroot,
610 struct pinctrl_dev *pctldev)
611{
612 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
613 devroot, pctldev, &pinmux_functions_ops);
614 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
615 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200616}
617
618#endif /* CONFIG_DEBUG_FS */