blob: f409f161ea1d1cd6732ed051f360bcfe758373b3 [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
Stephen Warrend4e31982012-03-01 18:48:31 -070036 * @func_selector: the function selector for the pinmux device handling
37 * this pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +020038 * @group_selector: the group selector for this group
39 */
40struct pinmux_group {
41 struct list_head node;
Stephen Warrend4e31982012-03-01 18:48:31 -070042 unsigned func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +020043 unsigned group_selector;
44};
45
Stephen Warren03665e02012-02-19 23:45:45 -070046int pinmux_check_ops(struct pinctrl_dev *pctldev)
47{
48 const struct pinmux_ops *ops = pctldev->desc->pmxops;
49 unsigned selector = 0;
50
51 /* Check that we implement required operations */
52 if (!ops->list_functions ||
53 !ops->get_function_name ||
54 !ops->get_function_groups ||
55 !ops->enable ||
56 !ops->disable)
57 return -EINVAL;
58
59 /* Check that all functions registered have names */
60 while (ops->list_functions(pctldev, selector) >= 0) {
61 const char *fname = ops->get_function_name(pctldev,
62 selector);
63 if (!fname) {
64 pr_err("pinmux ops has no name for function%u\n",
65 selector);
66 return -EINVAL;
67 }
68 selector++;
69 }
70
71 return 0;
72}
73
Linus Walleij2744e8a2011-05-02 20:50:54 +020074/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020075 * pin_request() - request a single pin to be muxed in, typically for GPIO
76 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070077 * @owner: a representation of the owner of this pin; typically the device
78 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020079 * @gpio_range: the range matching the GPIO pin if this is a request for a
80 * single GPIO pin
81 */
82static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070083 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020084 struct pinctrl_gpio_range *gpio_range)
85{
86 struct pin_desc *desc;
87 const struct pinmux_ops *ops = pctldev->desc->pmxops;
88 int status = -EINVAL;
89
Stephen Warren3cc70ed2012-02-19 23:45:44 -070090 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +020091
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 desc = pin_desc_get(pctldev, pin);
93 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070094 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 "pin is not registered so it cannot be requested\n");
96 goto out;
97 }
98
99 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700100 if (desc->owner && strcmp(desc->owner, owner)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101 spin_unlock(&desc->lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700102 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103 "pin already requested\n");
104 goto out;
105 }
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700106 desc->owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107 spin_unlock(&desc->lock);
108
109 /* Let each pin increase references to this module */
110 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700111 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 "could not increase module refcount for pin %d\n",
113 pin);
114 status = -EINVAL;
115 goto out_free_pin;
116 }
117
118 /*
119 * If there is no kind of request function for the pin we just assume
120 * we got it by default and proceed.
121 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600122 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200123 /* This requests and enables a single GPIO pin */
124 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
125 else if (ops->request)
126 status = ops->request(pctldev, pin);
127 else
128 status = 0;
129
130 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100131 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200132 pctldev->desc->name, pin);
133out_free_pin:
134 if (status) {
135 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700136 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200137 spin_unlock(&desc->lock);
138 }
139out:
140 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700141 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700142 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200143
144 return status;
145}
146
147/**
148 * pin_free() - release a single muxed in pin so something else can be muxed
149 * @pctldev: pin controller device handling this pin
150 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600151 * @gpio_range: the range matching the GPIO pin if this is a request for a
152 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100153 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700154 * This function returns a pointer to the previous owner. This is used
155 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100156 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200157 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600158static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
159 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200160{
161 const struct pinmux_ops *ops = pctldev->desc->pmxops;
162 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700163 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200164
165 desc = pin_desc_get(pctldev, pin);
166 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700167 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200168 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600169 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200170 }
171
Stephen Warren3712a3c2011-10-21 12:25:53 -0600172 /*
173 * If there is no kind of request function for the pin we just assume
174 * we got it by default and proceed.
175 */
176 if (gpio_range && ops->gpio_disable_free)
177 ops->gpio_disable_free(pctldev, gpio_range, pin);
178 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200179 ops->free(pctldev, pin);
180
181 spin_lock(&desc->lock);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700182 owner = desc->owner;
183 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200184 spin_unlock(&desc->lock);
185 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600186
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700187 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188}
189
190/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100191 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
192 * @pctldev: pin controller device affected
193 * @pin: the pin to mux in for GPIO
194 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200195 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100196int pinmux_request_gpio(struct pinctrl_dev *pctldev,
197 struct pinctrl_gpio_range *range,
198 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199{
200 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700201 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203
204 /* Conjure some name stating what chip and pin this is taken by */
205 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
206
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700207 owner = kstrdup(gpiostr, GFP_KERNEL);
208 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600209 return -EINVAL;
210
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700211 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600212 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700213 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600214
215 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217
218/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100219 * pinmux_free_gpio() - release a pin from GPIO muxing
220 * @pctldev: the pin controller device for the pin
221 * @pin: the affected currently GPIO-muxed in pin
222 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200223 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100224void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
225 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200226{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700227 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700229 owner = pin_free(pctldev, pin, range);
230 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100233/**
234 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
235 * @pctldev: the pin controller handling this pin
236 * @range: applicable GPIO range
237 * @pin: the affected GPIO pin in this controller
238 * @input: true if we set the pin as input, false for output
239 */
240int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
241 struct pinctrl_gpio_range *range,
242 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100243{
Linus Walleij542e7042011-11-14 10:06:22 +0100244 const struct pinmux_ops *ops;
245 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100246
247 ops = pctldev->desc->pmxops;
248
Linus Walleij542e7042011-11-14 10:06:22 +0100249 if (ops->gpio_set_direction)
250 ret = ops->gpio_set_direction(pctldev, range, pin, input);
251 else
252 ret = 0;
253
254 return ret;
255}
256
257/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800258 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259 * @pctldev: the device to take the pins on
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700260 * @owner: a representation of the owner of this pin; typically the device
261 * name that controls its mux function
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 * @group_selector: the group selector containing the pins to acquire
263 */
264static int acquire_pins(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700265 const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200266 unsigned group_selector)
267{
268 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600269 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200270 unsigned num_pins;
271 int ret;
272 int i;
273
274 ret = pctlops->get_group_pins(pctldev, group_selector,
275 &pins, &num_pins);
276 if (ret)
277 return ret;
278
Stephen Warren51cd24e2011-12-09 16:59:05 -0700279 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200280 num_pins, group_selector);
281
282 /* Try to allocate all pins in this group, one by one */
283 for (i = 0; i < num_pins; i++) {
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700284 ret = pin_request(pctldev, pins[i], owner, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200285 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700286 dev_err(pctldev->dev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700287 "could not get request pin %d on device %s - conflicting mux mappings?\n",
288 pins[i],
Linus Walleij2744e8a2011-05-02 20:50:54 +0200289 pinctrl_dev_get_name(pctldev));
290 /* On error release all taken pins */
291 i--; /* this pin just failed */
292 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600293 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200294 return -ENODEV;
295 }
296 }
297 return 0;
298}
299
300/**
301 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800302 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200303 * @group_selector: the group selector containing the pins to free
304 */
305static void release_pins(struct pinctrl_dev *pctldev,
306 unsigned group_selector)
307{
308 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600309 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310 unsigned num_pins;
311 int ret;
312 int i;
313
314 ret = pctlops->get_group_pins(pctldev, group_selector,
315 &pins, &num_pins);
316 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100317 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200318 group_selector);
319 return;
320 }
321 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600322 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323}
324
325/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200326 * pinmux_check_pin_group() - check function and pin group combo
327 * @pctldev: device to check the pin group vs function for
328 * @func_selector: the function selector to check the pin group for, we have
329 * already looked this up in the calling function
330 * @pin_group: the pin group to match to the function
331 *
332 * This function will check that the pinmux driver can supply the
333 * selected pin group for a certain function, returns the group selector if
334 * the group and function selector will work fine together, else returns
335 * negative
336 */
337static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
338 unsigned func_selector,
339 const char *pin_group)
340{
341 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
342 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
343 int ret;
344
345 /*
346 * If the driver does not support different pin groups for the
347 * functions, we only support group 0, and assume this exists.
348 */
349 if (!pctlops || !pctlops->list_groups)
350 return 0;
351
352 /*
353 * Passing NULL (no specific group) will select the first and
354 * hopefully only group of pins available for this function.
355 */
356 if (!pin_group) {
357 char const * const *groups;
358 unsigned num_groups;
359
360 ret = pmxops->get_function_groups(pctldev, func_selector,
361 &groups, &num_groups);
362 if (ret)
363 return ret;
364 if (num_groups < 1)
365 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200366 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700368 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100369 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200370 pmxops->get_function_name(pctldev, func_selector),
371 groups[0]);
372 return ret;
373 }
374
375 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700376 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100377 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200378 pmxops->get_function_name(pctldev, func_selector),
379 groups[0],
380 ret);
381
382 return ret;
383 }
384
Stephen Warren51cd24e2011-12-09 16:59:05 -0700385 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200386 "check if we have pin group %s on controller %s\n",
387 pin_group, pinctrl_dev_get_name(pctldev));
388
Linus Walleij7afde8b2011-10-19 17:07:16 +0200389 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200390 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700391 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200392 "%s does not support pin group %s with function %s\n",
393 pinctrl_dev_get_name(pctldev),
394 pin_group,
395 pmxops->get_function_name(pctldev, func_selector));
396 }
397 return ret;
398}
399
400/**
401 * pinmux_search_function() - check pin control driver for a certain function
402 * @pctldev: device to check for function and position
403 * @map: function map containing the function and position to look for
404 * @func_selector: returns the applicable function selector if found
405 * @group_selector: returns the applicable group selector if found
406 *
407 * This will search the pinmux driver for an applicable
408 * function with a specific pin group, returns 0 if these can be mapped
409 * negative otherwise
410 */
411static int pinmux_search_function(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100412 struct pinctrl_map const *map,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200413 unsigned *func_selector,
414 unsigned *group_selector)
415{
416 const struct pinmux_ops *ops = pctldev->desc->pmxops;
417 unsigned selector = 0;
418
419 /* See if this pctldev has this function */
420 while (ops->list_functions(pctldev, selector) >= 0) {
421 const char *fname = ops->get_function_name(pctldev,
422 selector);
423 int ret;
424
425 if (!strcmp(map->function, fname)) {
426 /* Found the function, check pin group */
427 ret = pinmux_check_pin_group(pctldev, selector,
428 map->group);
429 if (ret < 0)
430 return ret;
431
432 /* This function and group selector can be used */
433 *func_selector = selector;
434 *group_selector = ret;
435 return 0;
436
437 }
438 selector++;
439 }
440
441 pr_err("%s does not support function %s\n",
442 pinctrl_dev_get_name(pctldev), map->function);
443 return -EINVAL;
444}
445
446/**
447 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
448 */
449static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100450 struct pinctrl *p,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200451 struct device *dev,
452 const char *devname,
Linus Walleije93bcee2012-02-09 07:23:28 +0100453 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200454{
455 unsigned func_selector;
456 unsigned group_selector;
457 struct pinmux_group *grp;
458 int ret;
459
460 /*
461 * Note that we're not locking the pinmux mutex here, because
462 * this is only called at pinmux initialization time when it
463 * has not been added to any list and thus is not reachable
464 * by anyone else.
465 */
466
Linus Walleije93bcee2012-02-09 07:23:28 +0100467 if (p->pctldev && p->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700468 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100469 "different pin control devices given for device %s, function %s\n",
470 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200471 return -EINVAL;
472 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100473 p->dev = dev;
474 p->pctldev = pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200475
476 /* Now go into the driver and try to match a function and group */
477 ret = pinmux_search_function(pctldev, map, &func_selector,
478 &group_selector);
479 if (ret < 0)
480 return ret;
481
Linus Walleij2744e8a2011-05-02 20:50:54 +0200482 /* Now add this group selector, we may have many of them */
Stephen Warren02f5b982012-02-22 14:26:00 -0700483 grp = kmalloc(sizeof(*grp), GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484 if (!grp)
485 return -ENOMEM;
Stephen Warrend4e31982012-03-01 18:48:31 -0700486 grp->func_selector = func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200487 grp->group_selector = group_selector;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700488 ret = acquire_pins(pctldev, devname, group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200489 if (ret) {
490 kfree(grp);
491 return ret;
492 }
Stephen Warren8b9c1392012-02-19 23:45:42 -0700493 list_add_tail(&grp->node, &p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200494
495 return 0;
496}
497
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100498/**
499 * pinmux_apply_muxmap() - apply a certain mux mapping entry
500 */
501int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
502 struct pinctrl *p,
503 struct device *dev,
504 const char *devname,
505 struct pinctrl_map const *map)
506{
507 int ret;
508
509 ret = pinmux_enable_muxmap(pctldev, p, dev,
510 devname, map);
511 if (ret) {
512 pinmux_put(p);
513 return ret;
514 }
515
516 return 0;
517}
518
519/**
520 * pinmux_put() - free up the pinmux portions of a pin controller handle
521 */
522void pinmux_put(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200523{
524 struct list_head *node, *tmp;
525
Linus Walleije93bcee2012-02-09 07:23:28 +0100526 list_for_each_safe(node, tmp, &p->groups) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200527 struct pinmux_group *grp =
528 list_entry(node, struct pinmux_group, node);
529 /* Release all pins taken by this group */
Linus Walleije93bcee2012-02-09 07:23:28 +0100530 release_pins(p->pctldev, grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200531 list_del(node);
532 kfree(grp);
533 }
534}
535
536/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100537 * pinmux_enable() - enable the pinmux portion of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200538 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100539int pinmux_enable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200540{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100541 struct pinctrl_dev *pctldev = p->pctldev;
542 const struct pinmux_ops *ops = pctldev->desc->pmxops;
543 struct pinmux_group *grp;
544 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200545
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100546 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700547 ret = ops->enable(pctldev, grp->func_selector,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100548 grp->group_selector);
549 if (ret)
550 /*
551 * TODO: call disable() on all groups we called
552 * enable() on to this point?
553 */
554 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200555 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100556 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200557}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200558
559/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100560 * pinmux_disable() - disable the pinmux portions of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200561 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100562void pinmux_disable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200563{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100564 struct pinctrl_dev *pctldev = p->pctldev;
565 const struct pinmux_ops *ops = pctldev->desc->pmxops;
566 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200567
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100568 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700569 ops->disable(pctldev, grp->func_selector,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100570 grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200571 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200572}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200573
Linus Walleij2744e8a2011-05-02 20:50:54 +0200574#ifdef CONFIG_DEBUG_FS
575
576/* Called from pincontrol core */
577static int pinmux_functions_show(struct seq_file *s, void *what)
578{
579 struct pinctrl_dev *pctldev = s->private;
580 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
581 unsigned func_selector = 0;
582
583 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
584 const char *func = pmxops->get_function_name(pctldev,
585 func_selector);
586 const char * const *groups;
587 unsigned num_groups;
588 int ret;
589 int i;
590
591 ret = pmxops->get_function_groups(pctldev, func_selector,
592 &groups, &num_groups);
593 if (ret)
594 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
595 func);
596
597 seq_printf(s, "function: %s, groups = [ ", func);
598 for (i = 0; i < num_groups; i++)
599 seq_printf(s, "%s ", groups[i]);
600 seq_puts(s, "]\n");
601
602 func_selector++;
603
604 }
605
606 return 0;
607}
608
609static int pinmux_pins_show(struct seq_file *s, void *what)
610{
611 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900612 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200613
614 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700615 seq_puts(s, "Format: pin (name): owner\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200616
Chanho Park706e8522012-01-03 16:47:50 +0900617 /* The pin number can be retrived from the pin controller descriptor */
618 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200619 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100620 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200621
Chanho Park706e8522012-01-03 16:47:50 +0900622 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200623 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900624 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200625 if (desc == NULL)
626 continue;
627
Linus Walleij1cf94c42012-02-24 06:53:04 +0100628 if (desc->owner &&
629 !strcmp(desc->owner, pinctrl_dev_get_name(pctldev)))
630 is_hog = true;
631
632 seq_printf(s, "pin %d (%s): %s%s\n", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200633 desc->name ? desc->name : "unnamed",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100634 desc->owner ? desc->owner : "UNCLAIMED",
635 is_hog ? " (HOG)" : "");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200636 }
637
638 return 0;
639}
640
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100641void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200642{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100643 struct pinctrl_dev *pctldev = p->pctldev;
644 const struct pinmux_ops *pmxops;
645 const struct pinctrl_ops *pctlops;
646 struct pinmux_group *grp;
Stephen Warrend4e31982012-03-01 18:48:31 -0700647 const char *sep = "";
Linus Walleij2744e8a2011-05-02 20:50:54 +0200648
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100649 pmxops = pctldev->desc->pmxops;
650 pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200651
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100652 seq_printf(s, " groups: [");
653 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700654 seq_printf(s, "%s%s (%u)=%s (%u)",
655 sep,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100656 pctlops->get_group_name(pctldev,
657 grp->group_selector),
Stephen Warrend4e31982012-03-01 18:48:31 -0700658 grp->group_selector,
659 pmxops->get_function_name(pctldev,
660 grp->func_selector),
661 grp->func_selector);
662 sep = ", ";
Linus Walleij2744e8a2011-05-02 20:50:54 +0200663 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100664 seq_printf(s, " ]");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200665}
666
667static int pinmux_functions_open(struct inode *inode, struct file *file)
668{
669 return single_open(file, pinmux_functions_show, inode->i_private);
670}
671
672static int pinmux_pins_open(struct inode *inode, struct file *file)
673{
674 return single_open(file, pinmux_pins_show, inode->i_private);
675}
676
Linus Walleij2744e8a2011-05-02 20:50:54 +0200677static const struct file_operations pinmux_functions_ops = {
678 .open = pinmux_functions_open,
679 .read = seq_read,
680 .llseek = seq_lseek,
681 .release = single_release,
682};
683
684static const struct file_operations pinmux_pins_ops = {
685 .open = pinmux_pins_open,
686 .read = seq_read,
687 .llseek = seq_lseek,
688 .release = single_release,
689};
690
Linus Walleij2744e8a2011-05-02 20:50:54 +0200691void pinmux_init_device_debugfs(struct dentry *devroot,
692 struct pinctrl_dev *pctldev)
693{
694 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
695 devroot, pctldev, &pinmux_functions_ops);
696 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
697 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200698}
699
700#endif /* CONFIG_DEBUG_FS */