blob: 288789750f96eeca1f8f67eb5677da913a34601d [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 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinmux core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
18#include <linux/slab.h>
19#include <linux/radix-tree.h>
20#include <linux/err.h>
21#include <linux/list.h>
22#include <linux/mutex.h>
23#include <linux/spinlock.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
33/**
34 * struct pinmux_group - group list item for pinmux groups
35 * @node: pinmux group list node
36 * @group_selector: the group selector for this group
37 */
38struct pinmux_group {
39 struct list_head node;
40 unsigned group_selector;
41};
42
Stephen Warren03665e02012-02-19 23:45:45 -070043int pinmux_check_ops(struct pinctrl_dev *pctldev)
44{
45 const struct pinmux_ops *ops = pctldev->desc->pmxops;
46 unsigned selector = 0;
47
48 /* Check that we implement required operations */
49 if (!ops->list_functions ||
50 !ops->get_function_name ||
51 !ops->get_function_groups ||
52 !ops->enable ||
53 !ops->disable)
54 return -EINVAL;
55
56 /* Check that all functions registered have names */
57 while (ops->list_functions(pctldev, selector) >= 0) {
58 const char *fname = ops->get_function_name(pctldev,
59 selector);
60 if (!fname) {
61 pr_err("pinmux ops has no name for function%u\n",
62 selector);
63 return -EINVAL;
64 }
65 selector++;
66 }
67
68 return 0;
69}
70
Linus Walleij2744e8a2011-05-02 20:50:54 +020071/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020072 * pin_request() - request a single pin to be muxed in, typically for GPIO
73 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070074 * @owner: a representation of the owner of this pin; typically the device
75 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020076 * @gpio_range: the range matching the GPIO pin if this is a request for a
77 * single GPIO pin
78 */
79static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070080 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020081 struct pinctrl_gpio_range *gpio_range)
82{
83 struct pin_desc *desc;
84 const struct pinmux_ops *ops = pctldev->desc->pmxops;
85 int status = -EINVAL;
86
Stephen Warren3cc70ed2012-02-19 23:45:44 -070087 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +020088
Linus Walleij2744e8a2011-05-02 20:50:54 +020089 desc = pin_desc_get(pctldev, pin);
90 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070091 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 "pin is not registered so it cannot be requested\n");
93 goto out;
94 }
95
96 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -070097 if (desc->owner && strcmp(desc->owner, owner)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020098 spin_unlock(&desc->lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -070099 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100 "pin already requested\n");
101 goto out;
102 }
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700103 desc->owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200104 spin_unlock(&desc->lock);
105
106 /* Let each pin increase references to this module */
107 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700108 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 "could not increase module refcount for pin %d\n",
110 pin);
111 status = -EINVAL;
112 goto out_free_pin;
113 }
114
115 /*
116 * If there is no kind of request function for the pin we just assume
117 * we got it by default and proceed.
118 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600119 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120 /* This requests and enables a single GPIO pin */
121 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
122 else if (ops->request)
123 status = ops->request(pctldev, pin);
124 else
125 status = 0;
126
127 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100128 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200129 pctldev->desc->name, pin);
130out_free_pin:
131 if (status) {
132 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700133 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200134 spin_unlock(&desc->lock);
135 }
136out:
137 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700138 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700139 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140
141 return status;
142}
143
144/**
145 * pin_free() - release a single muxed in pin so something else can be muxed
146 * @pctldev: pin controller device handling this pin
147 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600148 * @gpio_range: the range matching the GPIO pin if this is a request for a
149 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100150 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700151 * This function returns a pointer to the previous owner. This is used
152 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100153 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200154 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600155static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
156 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200157{
158 const struct pinmux_ops *ops = pctldev->desc->pmxops;
159 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700160 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200161
162 desc = pin_desc_get(pctldev, pin);
163 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700164 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200165 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600166 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200167 }
168
Stephen Warren3712a3c2011-10-21 12:25:53 -0600169 /*
170 * If there is no kind of request function for the pin we just assume
171 * we got it by default and proceed.
172 */
173 if (gpio_range && ops->gpio_disable_free)
174 ops->gpio_disable_free(pctldev, gpio_range, pin);
175 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200176 ops->free(pctldev, pin);
177
178 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700179 owner = desc->owner;
180 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200181 spin_unlock(&desc->lock);
182 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600183
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700184 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200185}
186
187/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100188 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
189 * @pctldev: pin controller device affected
190 * @pin: the pin to mux in for GPIO
191 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100193int pinmux_request_gpio(struct pinctrl_dev *pctldev,
194 struct pinctrl_gpio_range *range,
195 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200196{
197 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700198 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
201 /* Conjure some name stating what chip and pin this is taken by */
202 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
203
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700204 owner = kstrdup(gpiostr, GFP_KERNEL);
205 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600206 return -EINVAL;
207
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700208 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600209 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700210 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600211
212 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200214
215/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100216 * pinmux_free_gpio() - release a pin from GPIO muxing
217 * @pctldev: the pin controller device for the pin
218 * @pin: the affected currently GPIO-muxed in pin
219 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200220 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100221void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
222 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200223{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700224 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700226 owner = pin_free(pctldev, pin, range);
227 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100230/**
231 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
232 * @pctldev: the pin controller handling this pin
233 * @range: applicable GPIO range
234 * @pin: the affected GPIO pin in this controller
235 * @input: true if we set the pin as input, false for output
236 */
237int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
238 struct pinctrl_gpio_range *range,
239 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100240{
Linus Walleij542e7042011-11-14 10:06:22 +0100241 const struct pinmux_ops *ops;
242 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100243
244 ops = pctldev->desc->pmxops;
245
Linus Walleij542e7042011-11-14 10:06:22 +0100246 if (ops->gpio_set_direction)
247 ret = ops->gpio_set_direction(pctldev, range, pin, input);
248 else
249 ret = 0;
250
251 return ret;
252}
253
254/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800255 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200256 * @pctldev: the device to take the pins on
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700257 * @owner: a representation of the owner of this pin; typically the device
258 * name that controls its mux function
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259 * @group_selector: the group selector containing the pins to acquire
260 */
261static int acquire_pins(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700262 const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 unsigned group_selector)
264{
265 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600266 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267 unsigned num_pins;
268 int ret;
269 int i;
270
271 ret = pctlops->get_group_pins(pctldev, group_selector,
272 &pins, &num_pins);
273 if (ret)
274 return ret;
275
Stephen Warren51cd24e2011-12-09 16:59:05 -0700276 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277 num_pins, group_selector);
278
279 /* Try to allocate all pins in this group, one by one */
280 for (i = 0; i < num_pins; i++) {
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700281 ret = pin_request(pctldev, pins[i], owner, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200282 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700283 dev_err(pctldev->dev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700284 "could not get request pin %d on device %s - conflicting mux mappings?\n",
285 pins[i],
Linus Walleij2744e8a2011-05-02 20:50:54 +0200286 pinctrl_dev_get_name(pctldev));
287 /* On error release all taken pins */
288 i--; /* this pin just failed */
289 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600290 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200291 return -ENODEV;
292 }
293 }
294 return 0;
295}
296
297/**
298 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800299 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300 * @group_selector: the group selector containing the pins to free
301 */
302static void release_pins(struct pinctrl_dev *pctldev,
303 unsigned group_selector)
304{
305 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600306 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 unsigned num_pins;
308 int ret;
309 int i;
310
311 ret = pctlops->get_group_pins(pctldev, group_selector,
312 &pins, &num_pins);
313 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100314 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200315 group_selector);
316 return;
317 }
318 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600319 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320}
321
322/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323 * pinmux_check_pin_group() - check function and pin group combo
324 * @pctldev: device to check the pin group vs function for
325 * @func_selector: the function selector to check the pin group for, we have
326 * already looked this up in the calling function
327 * @pin_group: the pin group to match to the function
328 *
329 * This function will check that the pinmux driver can supply the
330 * selected pin group for a certain function, returns the group selector if
331 * the group and function selector will work fine together, else returns
332 * negative
333 */
334static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
335 unsigned func_selector,
336 const char *pin_group)
337{
338 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
339 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
340 int ret;
341
342 /*
343 * If the driver does not support different pin groups for the
344 * functions, we only support group 0, and assume this exists.
345 */
346 if (!pctlops || !pctlops->list_groups)
347 return 0;
348
349 /*
350 * Passing NULL (no specific group) will select the first and
351 * hopefully only group of pins available for this function.
352 */
353 if (!pin_group) {
354 char const * const *groups;
355 unsigned num_groups;
356
357 ret = pmxops->get_function_groups(pctldev, func_selector,
358 &groups, &num_groups);
359 if (ret)
360 return ret;
361 if (num_groups < 1)
362 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200363 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200364 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700365 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100366 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 pmxops->get_function_name(pctldev, func_selector),
368 groups[0]);
369 return ret;
370 }
371
372 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700373 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100374 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200375 pmxops->get_function_name(pctldev, func_selector),
376 groups[0],
377 ret);
378
379 return ret;
380 }
381
Stephen Warren51cd24e2011-12-09 16:59:05 -0700382 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200383 "check if we have pin group %s on controller %s\n",
384 pin_group, pinctrl_dev_get_name(pctldev));
385
Linus Walleij7afde8b2011-10-19 17:07:16 +0200386 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200387 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700388 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200389 "%s does not support pin group %s with function %s\n",
390 pinctrl_dev_get_name(pctldev),
391 pin_group,
392 pmxops->get_function_name(pctldev, func_selector));
393 }
394 return ret;
395}
396
397/**
398 * pinmux_search_function() - check pin control driver for a certain function
399 * @pctldev: device to check for function and position
400 * @map: function map containing the function and position to look for
401 * @func_selector: returns the applicable function selector if found
402 * @group_selector: returns the applicable group selector if found
403 *
404 * This will search the pinmux driver for an applicable
405 * function with a specific pin group, returns 0 if these can be mapped
406 * negative otherwise
407 */
408static int pinmux_search_function(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100409 struct pinctrl_map const *map,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200410 unsigned *func_selector,
411 unsigned *group_selector)
412{
413 const struct pinmux_ops *ops = pctldev->desc->pmxops;
414 unsigned selector = 0;
415
416 /* See if this pctldev has this function */
417 while (ops->list_functions(pctldev, selector) >= 0) {
418 const char *fname = ops->get_function_name(pctldev,
419 selector);
420 int ret;
421
422 if (!strcmp(map->function, fname)) {
423 /* Found the function, check pin group */
424 ret = pinmux_check_pin_group(pctldev, selector,
425 map->group);
426 if (ret < 0)
427 return ret;
428
429 /* This function and group selector can be used */
430 *func_selector = selector;
431 *group_selector = ret;
432 return 0;
433
434 }
435 selector++;
436 }
437
438 pr_err("%s does not support function %s\n",
439 pinctrl_dev_get_name(pctldev), map->function);
440 return -EINVAL;
441}
442
443/**
444 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
445 */
446static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100447 struct pinctrl *p,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200448 struct device *dev,
449 const char *devname,
Linus Walleije93bcee2012-02-09 07:23:28 +0100450 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200451{
452 unsigned func_selector;
453 unsigned group_selector;
454 struct pinmux_group *grp;
455 int ret;
456
457 /*
458 * Note that we're not locking the pinmux mutex here, because
459 * this is only called at pinmux initialization time when it
460 * has not been added to any list and thus is not reachable
461 * by anyone else.
462 */
463
Linus Walleije93bcee2012-02-09 07:23:28 +0100464 if (p->pctldev && p->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700465 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100466 "different pin control devices given for device %s, function %s\n",
467 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200468 return -EINVAL;
469 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100470 p->dev = dev;
471 p->pctldev = pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200472
473 /* Now go into the driver and try to match a function and group */
474 ret = pinmux_search_function(pctldev, map, &func_selector,
475 &group_selector);
476 if (ret < 0)
477 return ret;
478
479 /*
480 * If the function selector is already set, it needs to be identical,
481 * we support several groups with one function but not several
482 * functions with one or several groups in the same pinmux.
483 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100484 if (p->func_selector != UINT_MAX &&
485 p->func_selector != func_selector) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700486 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200487 "dual function defines in the map for device %s\n",
488 devname);
489 return -EINVAL;
490 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100491 p->func_selector = func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200492
493 /* Now add this group selector, we may have many of them */
494 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
495 if (!grp)
496 return -ENOMEM;
497 grp->group_selector = group_selector;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700498 ret = acquire_pins(pctldev, devname, group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200499 if (ret) {
500 kfree(grp);
501 return ret;
502 }
Stephen Warren8b9c1392012-02-19 23:45:42 -0700503 list_add_tail(&grp->node, &p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200504
505 return 0;
506}
507
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100508/**
509 * pinmux_apply_muxmap() - apply a certain mux mapping entry
510 */
511int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
512 struct pinctrl *p,
513 struct device *dev,
514 const char *devname,
515 struct pinctrl_map const *map)
516{
517 int ret;
518
519 ret = pinmux_enable_muxmap(pctldev, p, dev,
520 devname, map);
521 if (ret) {
522 pinmux_put(p);
523 return ret;
524 }
525
526 return 0;
527}
528
529/**
530 * pinmux_put() - free up the pinmux portions of a pin controller handle
531 */
532void pinmux_put(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200533{
534 struct list_head *node, *tmp;
535
Linus Walleije93bcee2012-02-09 07:23:28 +0100536 list_for_each_safe(node, tmp, &p->groups) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200537 struct pinmux_group *grp =
538 list_entry(node, struct pinmux_group, node);
539 /* Release all pins taken by this group */
Linus Walleije93bcee2012-02-09 07:23:28 +0100540 release_pins(p->pctldev, grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200541 list_del(node);
542 kfree(grp);
543 }
544}
545
546/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100547 * pinmux_enable() - enable the pinmux portion of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200548 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100549int pinmux_enable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200550{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100551 struct pinctrl_dev *pctldev = p->pctldev;
552 const struct pinmux_ops *ops = pctldev->desc->pmxops;
553 struct pinmux_group *grp;
554 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200555
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100556 list_for_each_entry(grp, &p->groups, node) {
557 ret = ops->enable(pctldev, p->func_selector,
558 grp->group_selector);
559 if (ret)
560 /*
561 * TODO: call disable() on all groups we called
562 * enable() on to this point?
563 */
564 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200565 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100566 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200567}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200568
569/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100570 * pinmux_disable() - disable the pinmux portions of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200571 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100572void pinmux_disable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200573{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100574 struct pinctrl_dev *pctldev = p->pctldev;
575 const struct pinmux_ops *ops = pctldev->desc->pmxops;
576 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200577
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100578 list_for_each_entry(grp, &p->groups, node) {
579 ops->disable(pctldev, p->func_selector,
580 grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200581 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200582}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200583
Linus Walleij2744e8a2011-05-02 20:50:54 +0200584#ifdef CONFIG_DEBUG_FS
585
586/* Called from pincontrol core */
587static int pinmux_functions_show(struct seq_file *s, void *what)
588{
589 struct pinctrl_dev *pctldev = s->private;
590 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
591 unsigned func_selector = 0;
592
593 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
594 const char *func = pmxops->get_function_name(pctldev,
595 func_selector);
596 const char * const *groups;
597 unsigned num_groups;
598 int ret;
599 int i;
600
601 ret = pmxops->get_function_groups(pctldev, func_selector,
602 &groups, &num_groups);
603 if (ret)
604 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
605 func);
606
607 seq_printf(s, "function: %s, groups = [ ", func);
608 for (i = 0; i < num_groups; i++)
609 seq_printf(s, "%s ", groups[i]);
610 seq_puts(s, "]\n");
611
612 func_selector++;
613
614 }
615
616 return 0;
617}
618
619static int pinmux_pins_show(struct seq_file *s, void *what)
620{
621 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900622 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200623
624 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700625 seq_puts(s, "Format: pin (name): owner\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200626
Chanho Park706e8522012-01-03 16:47:50 +0900627 /* The pin number can be retrived from the pin controller descriptor */
628 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200629
630 struct pin_desc *desc;
631
Chanho Park706e8522012-01-03 16:47:50 +0900632 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200633 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900634 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200635 if (desc == NULL)
636 continue;
637
638 seq_printf(s, "pin %d (%s): %s\n", pin,
639 desc->name ? desc->name : "unnamed",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700640 desc->owner ? desc->owner : "UNCLAIMED");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200641 }
642
643 return 0;
644}
645
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100646void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200647{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100648 struct pinctrl_dev *pctldev = p->pctldev;
649 const struct pinmux_ops *pmxops;
650 const struct pinctrl_ops *pctlops;
651 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200652
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100653 pmxops = pctldev->desc->pmxops;
654 pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200655
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100656 seq_printf(s, " function: %s (%u),",
657 pmxops->get_function_name(pctldev,
658 p->func_selector),
659 p->func_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200660
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661 seq_printf(s, " groups: [");
662 list_for_each_entry(grp, &p->groups, node) {
663 seq_printf(s, " %s (%u)",
664 pctlops->get_group_name(pctldev,
665 grp->group_selector),
666 grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200667 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100668 seq_printf(s, " ]");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200669}
670
671static int pinmux_functions_open(struct inode *inode, struct file *file)
672{
673 return single_open(file, pinmux_functions_show, inode->i_private);
674}
675
676static int pinmux_pins_open(struct inode *inode, struct file *file)
677{
678 return single_open(file, pinmux_pins_show, inode->i_private);
679}
680
Linus Walleij2744e8a2011-05-02 20:50:54 +0200681static const struct file_operations pinmux_functions_ops = {
682 .open = pinmux_functions_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = single_release,
686};
687
688static const struct file_operations pinmux_pins_ops = {
689 .open = pinmux_pins_open,
690 .read = seq_read,
691 .llseek = seq_lseek,
692 .release = single_release,
693};
694
Linus Walleij2744e8a2011-05-02 20:50:54 +0200695void pinmux_init_device_debugfs(struct dentry *devroot,
696 struct pinctrl_dev *pctldev)
697{
698 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
699 devroot, pctldev, &pinmux_functions_ops);
700 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
701 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200702}
703
704#endif /* CONFIG_DEBUG_FS */