blob: 7342c26f4246f87f47eb9fa34a6ef46ce92835d4 [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>
Linus Walleij97607d12011-11-29 12:52:39 +010022#include <linux/string.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020023#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/pinctrl/machine.h>
27#include <linux/pinctrl/pinmux.h>
28#include "core.h"
Linus Walleijbefe5bd2012-02-09 19:47:48 +010029#include "pinmux.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020030
31/**
32 * struct pinmux_group - group list item for pinmux groups
33 * @node: pinmux group list node
Stephen Warrend4e31982012-03-01 18:48:31 -070034 * @func_selector: the function selector for the pinmux device handling
35 * this pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +020036 * @group_selector: the group selector for this group
37 */
38struct pinmux_group {
39 struct list_head node;
Stephen Warrend4e31982012-03-01 18:48:31 -070040 unsigned func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +020041 unsigned group_selector;
42};
43
Stephen Warren03665e02012-02-19 23:45:45 -070044int pinmux_check_ops(struct pinctrl_dev *pctldev)
45{
46 const struct pinmux_ops *ops = pctldev->desc->pmxops;
47 unsigned selector = 0;
48
49 /* Check that we implement required operations */
50 if (!ops->list_functions ||
51 !ops->get_function_name ||
52 !ops->get_function_groups ||
53 !ops->enable ||
54 !ops->disable)
55 return -EINVAL;
56
57 /* Check that all functions registered have names */
58 while (ops->list_functions(pctldev, selector) >= 0) {
59 const char *fname = ops->get_function_name(pctldev,
60 selector);
61 if (!fname) {
62 pr_err("pinmux ops has no name for function%u\n",
63 selector);
64 return -EINVAL;
65 }
66 selector++;
67 }
68
69 return 0;
70}
71
Linus Walleij2744e8a2011-05-02 20:50:54 +020072/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020073 * pin_request() - request a single pin to be muxed in, typically for GPIO
74 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070075 * @owner: a representation of the owner of this pin; typically the device
76 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020077 * @gpio_range: the range matching the GPIO pin if this is a request for a
78 * single GPIO pin
79 */
80static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070081 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020082 struct pinctrl_gpio_range *gpio_range)
83{
84 struct pin_desc *desc;
85 const struct pinmux_ops *ops = pctldev->desc->pmxops;
86 int status = -EINVAL;
87
Stephen Warren3cc70ed2012-02-19 23:45:44 -070088 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +020089
Linus Walleij2744e8a2011-05-02 20:50:54 +020090 desc = pin_desc_get(pctldev, pin);
91 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070092 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 "pin is not registered so it cannot be requested\n");
94 goto out;
95 }
96
Stephen Warren3cc70ed2012-02-19 23:45:44 -070097 if (desc->owner && strcmp(desc->owner, owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070098 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020099 "pin already requested\n");
100 goto out;
101 }
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700102 desc->owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103
104 /* Let each pin increase references to this module */
105 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700106 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107 "could not increase module refcount for pin %d\n",
108 pin);
109 status = -EINVAL;
110 goto out_free_pin;
111 }
112
113 /*
114 * If there is no kind of request function for the pin we just assume
115 * we got it by default and proceed.
116 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600117 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118 /* This requests and enables a single GPIO pin */
119 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
120 else if (ops->request)
121 status = ops->request(pctldev, pin);
122 else
123 status = 0;
124
125 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100126 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200127 pctldev->desc->name, pin);
128out_free_pin:
Stephen Warren57b676f2012-03-02 13:05:44 -0700129 if (status)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700130 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200131out:
132 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700133 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700134 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200135
136 return status;
137}
138
139/**
140 * pin_free() - release a single muxed in pin so something else can be muxed
141 * @pctldev: pin controller device handling this pin
142 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600143 * @gpio_range: the range matching the GPIO pin if this is a request for a
144 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100145 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700146 * This function returns a pointer to the previous owner. This is used
147 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100148 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200149 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600150static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
151 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200152{
153 const struct pinmux_ops *ops = pctldev->desc->pmxops;
154 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700155 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156
157 desc = pin_desc_get(pctldev, pin);
158 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700159 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200160 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600161 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200162 }
163
Stephen Warren3712a3c2011-10-21 12:25:53 -0600164 /*
165 * If there is no kind of request function for the pin we just assume
166 * we got it by default and proceed.
167 */
168 if (gpio_range && ops->gpio_disable_free)
169 ops->gpio_disable_free(pctldev, gpio_range, pin);
170 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200171 ops->free(pctldev, pin);
172
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700173 owner = desc->owner;
174 desc->owner = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200175 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600176
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700177 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178}
179
180/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100181 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
182 * @pctldev: pin controller device affected
183 * @pin: the pin to mux in for GPIO
184 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200185 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100186int pinmux_request_gpio(struct pinctrl_dev *pctldev,
187 struct pinctrl_gpio_range *range,
188 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189{
190 char gpiostr[16];
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700191 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193
194 /* Conjure some name stating what chip and pin this is taken by */
195 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
196
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700197 owner = kstrdup(gpiostr, GFP_KERNEL);
198 if (!owner)
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600199 return -EINVAL;
200
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700201 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600202 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700203 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600204
205 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207
208/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100209 * pinmux_free_gpio() - release a pin from GPIO muxing
210 * @pctldev: the pin controller device for the pin
211 * @pin: the affected currently GPIO-muxed in pin
212 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100214void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
215 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700217 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700219 owner = pin_free(pctldev, pin, range);
220 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100223/**
224 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
225 * @pctldev: the pin controller handling this pin
226 * @range: applicable GPIO range
227 * @pin: the affected GPIO pin in this controller
228 * @input: true if we set the pin as input, false for output
229 */
230int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
231 struct pinctrl_gpio_range *range,
232 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100233{
Linus Walleij542e7042011-11-14 10:06:22 +0100234 const struct pinmux_ops *ops;
235 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100236
237 ops = pctldev->desc->pmxops;
238
Linus Walleij542e7042011-11-14 10:06:22 +0100239 if (ops->gpio_set_direction)
240 ret = ops->gpio_set_direction(pctldev, range, pin, input);
241 else
242 ret = 0;
243
244 return ret;
245}
246
247/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800248 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200249 * @pctldev: the device to take the pins on
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700250 * @owner: a representation of the owner of this pin; typically the device
251 * name that controls its mux function
Linus Walleij2744e8a2011-05-02 20:50:54 +0200252 * @group_selector: the group selector containing the pins to acquire
253 */
254static int acquire_pins(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700255 const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200256 unsigned group_selector)
257{
258 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600259 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 unsigned num_pins;
261 int ret;
262 int i;
263
264 ret = pctlops->get_group_pins(pctldev, group_selector,
265 &pins, &num_pins);
266 if (ret)
267 return ret;
268
Stephen Warren51cd24e2011-12-09 16:59:05 -0700269 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200270 num_pins, group_selector);
271
272 /* Try to allocate all pins in this group, one by one */
273 for (i = 0; i < num_pins; i++) {
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700274 ret = pin_request(pctldev, pins[i], owner, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700276 dev_err(pctldev->dev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700277 "could not get request pin %d on device %s - conflicting mux mappings?\n",
278 pins[i],
Linus Walleij2744e8a2011-05-02 20:50:54 +0200279 pinctrl_dev_get_name(pctldev));
280 /* On error release all taken pins */
281 i--; /* this pin just failed */
282 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600283 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200284 return -ENODEV;
285 }
286 }
287 return 0;
288}
289
290/**
291 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800292 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200293 * @group_selector: the group selector containing the pins to free
294 */
295static void release_pins(struct pinctrl_dev *pctldev,
296 unsigned group_selector)
297{
298 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600299 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300 unsigned num_pins;
301 int ret;
302 int i;
303
304 ret = pctlops->get_group_pins(pctldev, group_selector,
305 &pins, &num_pins);
306 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100307 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200308 group_selector);
309 return;
310 }
311 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600312 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313}
314
315/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316 * pinmux_check_pin_group() - check function and pin group combo
317 * @pctldev: device to check the pin group vs function for
318 * @func_selector: the function selector to check the pin group for, we have
319 * already looked this up in the calling function
320 * @pin_group: the pin group to match to the function
321 *
322 * This function will check that the pinmux driver can supply the
323 * selected pin group for a certain function, returns the group selector if
324 * the group and function selector will work fine together, else returns
325 * negative
326 */
327static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
328 unsigned func_selector,
329 const char *pin_group)
330{
331 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
332 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
333 int ret;
334
335 /*
336 * If the driver does not support different pin groups for the
337 * functions, we only support group 0, and assume this exists.
338 */
339 if (!pctlops || !pctlops->list_groups)
340 return 0;
341
342 /*
343 * Passing NULL (no specific group) will select the first and
344 * hopefully only group of pins available for this function.
345 */
346 if (!pin_group) {
347 char const * const *groups;
348 unsigned num_groups;
349
350 ret = pmxops->get_function_groups(pctldev, func_selector,
351 &groups, &num_groups);
352 if (ret)
353 return ret;
354 if (num_groups < 1)
355 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200356 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700358 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100359 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200360 pmxops->get_function_name(pctldev, func_selector),
361 groups[0]);
362 return ret;
363 }
364
365 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700366 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100367 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200368 pmxops->get_function_name(pctldev, func_selector),
369 groups[0],
370 ret);
371
372 return ret;
373 }
374
Stephen Warren51cd24e2011-12-09 16:59:05 -0700375 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200376 "check if we have pin group %s on controller %s\n",
377 pin_group, pinctrl_dev_get_name(pctldev));
378
Linus Walleij7afde8b2011-10-19 17:07:16 +0200379 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200380 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700381 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382 "%s does not support pin group %s with function %s\n",
383 pinctrl_dev_get_name(pctldev),
384 pin_group,
385 pmxops->get_function_name(pctldev, func_selector));
386 }
387 return ret;
388}
389
390/**
391 * pinmux_search_function() - check pin control driver for a certain function
392 * @pctldev: device to check for function and position
393 * @map: function map containing the function and position to look for
394 * @func_selector: returns the applicable function selector if found
395 * @group_selector: returns the applicable group selector if found
396 *
397 * This will search the pinmux driver for an applicable
398 * function with a specific pin group, returns 0 if these can be mapped
399 * negative otherwise
400 */
401static int pinmux_search_function(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100402 struct pinctrl_map const *map,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200403 unsigned *func_selector,
404 unsigned *group_selector)
405{
406 const struct pinmux_ops *ops = pctldev->desc->pmxops;
407 unsigned selector = 0;
408
409 /* See if this pctldev has this function */
410 while (ops->list_functions(pctldev, selector) >= 0) {
411 const char *fname = ops->get_function_name(pctldev,
412 selector);
413 int ret;
414
415 if (!strcmp(map->function, fname)) {
416 /* Found the function, check pin group */
417 ret = pinmux_check_pin_group(pctldev, selector,
418 map->group);
419 if (ret < 0)
420 return ret;
421
422 /* This function and group selector can be used */
423 *func_selector = selector;
424 *group_selector = ret;
425 return 0;
426
427 }
428 selector++;
429 }
430
431 pr_err("%s does not support function %s\n",
432 pinctrl_dev_get_name(pctldev), map->function);
433 return -EINVAL;
434}
435
436/**
437 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
438 */
439static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100440 struct pinctrl *p,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200441 struct device *dev,
442 const char *devname,
Linus Walleije93bcee2012-02-09 07:23:28 +0100443 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200444{
445 unsigned func_selector;
446 unsigned group_selector;
447 struct pinmux_group *grp;
448 int ret;
449
450 /*
451 * Note that we're not locking the pinmux mutex here, because
452 * this is only called at pinmux initialization time when it
453 * has not been added to any list and thus is not reachable
454 * by anyone else.
455 */
456
Linus Walleije93bcee2012-02-09 07:23:28 +0100457 if (p->pctldev && p->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700458 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100459 "different pin control devices given for device %s, function %s\n",
460 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200461 return -EINVAL;
462 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100463 p->dev = dev;
464 p->pctldev = pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200465
466 /* Now go into the driver and try to match a function and group */
467 ret = pinmux_search_function(pctldev, map, &func_selector,
468 &group_selector);
469 if (ret < 0)
470 return ret;
471
Linus Walleij2744e8a2011-05-02 20:50:54 +0200472 /* Now add this group selector, we may have many of them */
Stephen Warren02f5b982012-02-22 14:26:00 -0700473 grp = kmalloc(sizeof(*grp), GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200474 if (!grp)
475 return -ENOMEM;
Stephen Warrend4e31982012-03-01 18:48:31 -0700476 grp->func_selector = func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200477 grp->group_selector = group_selector;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700478 ret = acquire_pins(pctldev, devname, group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200479 if (ret) {
480 kfree(grp);
481 return ret;
482 }
Stephen Warren8b9c1392012-02-19 23:45:42 -0700483 list_add_tail(&grp->node, &p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484
485 return 0;
486}
487
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100488/**
489 * pinmux_apply_muxmap() - apply a certain mux mapping entry
490 */
491int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
492 struct pinctrl *p,
493 struct device *dev,
494 const char *devname,
495 struct pinctrl_map const *map)
496{
497 int ret;
498
499 ret = pinmux_enable_muxmap(pctldev, p, dev,
500 devname, map);
501 if (ret) {
502 pinmux_put(p);
503 return ret;
504 }
505
506 return 0;
507}
508
509/**
510 * pinmux_put() - free up the pinmux portions of a pin controller handle
511 */
512void pinmux_put(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200513{
514 struct list_head *node, *tmp;
515
Linus Walleije93bcee2012-02-09 07:23:28 +0100516 list_for_each_safe(node, tmp, &p->groups) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200517 struct pinmux_group *grp =
518 list_entry(node, struct pinmux_group, node);
519 /* Release all pins taken by this group */
Linus Walleije93bcee2012-02-09 07:23:28 +0100520 release_pins(p->pctldev, grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200521 list_del(node);
522 kfree(grp);
523 }
524}
525
526/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100527 * pinmux_enable() - enable the pinmux portion of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200528 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100529int pinmux_enable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200530{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100531 struct pinctrl_dev *pctldev = p->pctldev;
532 const struct pinmux_ops *ops = pctldev->desc->pmxops;
533 struct pinmux_group *grp;
534 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200535
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100536 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700537 ret = ops->enable(pctldev, grp->func_selector,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100538 grp->group_selector);
539 if (ret)
540 /*
541 * TODO: call disable() on all groups we called
542 * enable() on to this point?
543 */
544 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200545 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100546 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200547}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200548
549/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100550 * pinmux_disable() - disable the pinmux portions of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200551 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100552void pinmux_disable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200553{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100554 struct pinctrl_dev *pctldev = p->pctldev;
555 const struct pinmux_ops *ops = pctldev->desc->pmxops;
556 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200557
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100558 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700559 ops->disable(pctldev, grp->func_selector,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100560 grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200561 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200562}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200563
Linus Walleij2744e8a2011-05-02 20:50:54 +0200564#ifdef CONFIG_DEBUG_FS
565
566/* Called from pincontrol core */
567static int pinmux_functions_show(struct seq_file *s, void *what)
568{
569 struct pinctrl_dev *pctldev = s->private;
570 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
571 unsigned func_selector = 0;
572
Stephen Warren57b676f2012-03-02 13:05:44 -0700573 mutex_lock(&pinctrl_mutex);
574
Linus Walleij2744e8a2011-05-02 20:50:54 +0200575 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
576 const char *func = pmxops->get_function_name(pctldev,
577 func_selector);
578 const char * const *groups;
579 unsigned num_groups;
580 int ret;
581 int i;
582
583 ret = pmxops->get_function_groups(pctldev, func_selector,
584 &groups, &num_groups);
585 if (ret)
586 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
587 func);
588
589 seq_printf(s, "function: %s, groups = [ ", func);
590 for (i = 0; i < num_groups; i++)
591 seq_printf(s, "%s ", groups[i]);
592 seq_puts(s, "]\n");
593
594 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200595 }
596
Stephen Warren57b676f2012-03-02 13:05:44 -0700597 mutex_unlock(&pinctrl_mutex);
598
Linus Walleij2744e8a2011-05-02 20:50:54 +0200599 return 0;
600}
601
602static int pinmux_pins_show(struct seq_file *s, void *what)
603{
604 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900605 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200606
607 seq_puts(s, "Pinmux settings per pin\n");
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700608 seq_puts(s, "Format: pin (name): owner\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200609
Stephen Warren57b676f2012-03-02 13:05:44 -0700610 mutex_lock(&pinctrl_mutex);
611
Chanho Park706e8522012-01-03 16:47:50 +0900612 /* The pin number can be retrived from the pin controller descriptor */
613 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200614 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100615 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200616
Chanho Park706e8522012-01-03 16:47:50 +0900617 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200618 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900619 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200620 if (desc == NULL)
621 continue;
622
Linus Walleij1cf94c42012-02-24 06:53:04 +0100623 if (desc->owner &&
624 !strcmp(desc->owner, pinctrl_dev_get_name(pctldev)))
625 is_hog = true;
626
627 seq_printf(s, "pin %d (%s): %s%s\n", pin,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200628 desc->name ? desc->name : "unnamed",
Linus Walleij1cf94c42012-02-24 06:53:04 +0100629 desc->owner ? desc->owner : "UNCLAIMED",
630 is_hog ? " (HOG)" : "");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200631 }
632
Stephen Warren57b676f2012-03-02 13:05:44 -0700633 mutex_unlock(&pinctrl_mutex);
634
Linus Walleij2744e8a2011-05-02 20:50:54 +0200635 return 0;
636}
637
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100638void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200639{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100640 struct pinctrl_dev *pctldev = p->pctldev;
641 const struct pinmux_ops *pmxops;
642 const struct pinctrl_ops *pctlops;
643 struct pinmux_group *grp;
Stephen Warrend4e31982012-03-01 18:48:31 -0700644 const char *sep = "";
Linus Walleij2744e8a2011-05-02 20:50:54 +0200645
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100646 pmxops = pctldev->desc->pmxops;
647 pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200648
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100649 seq_printf(s, " groups: [");
650 list_for_each_entry(grp, &p->groups, node) {
Stephen Warrend4e31982012-03-01 18:48:31 -0700651 seq_printf(s, "%s%s (%u)=%s (%u)",
652 sep,
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100653 pctlops->get_group_name(pctldev,
654 grp->group_selector),
Stephen Warrend4e31982012-03-01 18:48:31 -0700655 grp->group_selector,
656 pmxops->get_function_name(pctldev,
657 grp->func_selector),
658 grp->func_selector);
659 sep = ", ";
Linus Walleij2744e8a2011-05-02 20:50:54 +0200660 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661 seq_printf(s, " ]");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200662}
663
664static int pinmux_functions_open(struct inode *inode, struct file *file)
665{
666 return single_open(file, pinmux_functions_show, inode->i_private);
667}
668
669static int pinmux_pins_open(struct inode *inode, struct file *file)
670{
671 return single_open(file, pinmux_pins_show, inode->i_private);
672}
673
Linus Walleij2744e8a2011-05-02 20:50:54 +0200674static const struct file_operations pinmux_functions_ops = {
675 .open = pinmux_functions_open,
676 .read = seq_read,
677 .llseek = seq_lseek,
678 .release = single_release,
679};
680
681static const struct file_operations pinmux_pins_ops = {
682 .open = pinmux_pins_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = single_release,
686};
687
Linus Walleij2744e8a2011-05-02 20:50:54 +0200688void pinmux_init_device_debugfs(struct dentry *devroot,
689 struct pinctrl_dev *pctldev)
690{
691 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
692 devroot, pctldev, &pinmux_functions_ops);
693 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
694 devroot, pctldev, &pinmux_pins_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200695}
696
697#endif /* CONFIG_DEBUG_FS */