blob: 773835d18f55f790e02fef2f1d53895912ce37bc [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"
31
Linus Walleije93bcee2012-02-09 07:23:28 +010032/* List of pin controller handles */
33static DEFINE_MUTEX(pinctrl_list_mutex);
34static LIST_HEAD(pinctrl_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020035
Linus Walleije93bcee2012-02-09 07:23:28 +010036/* Global pinctrl maps */
37static struct pinctrl_map *pinctrl_maps;
38static unsigned pinctrl_maps_num;
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
40/**
41 * struct pinmux_group - group list item for pinmux groups
42 * @node: pinmux group list node
43 * @group_selector: the group selector for this group
44 */
45struct pinmux_group {
46 struct list_head node;
47 unsigned group_selector;
48};
49
50/**
Linus Walleije93bcee2012-02-09 07:23:28 +010051 * struct pinctrl - per-device pin control state holder
Linus Walleij2744e8a2011-05-02 20:50:54 +020052 * @node: global list node
Linus Walleije93bcee2012-02-09 07:23:28 +010053 * @dev: the device using this pin control handle
54 * @usecount: the number of active users of this pin controller setting, used
55 * to keep track of nested use cases
56 * @pctldev: pin control device handling this pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +020057 * @func_selector: the function selector for the pinmux device handling
58 * this pinmux
59 * @groups: the group selectors for the pinmux device and
60 * selector combination handling this pinmux, this is a list that
61 * will be traversed on all pinmux operations such as
62 * get/put/enable/disable
63 * @mutex: a lock for the pinmux state holder
64 */
Linus Walleije93bcee2012-02-09 07:23:28 +010065struct pinctrl {
Linus Walleij2744e8a2011-05-02 20:50:54 +020066 struct list_head node;
67 struct device *dev;
68 unsigned usecount;
69 struct pinctrl_dev *pctldev;
70 unsigned func_selector;
71 struct list_head groups;
72 struct mutex mutex;
73};
74
75/**
Linus Walleije93bcee2012-02-09 07:23:28 +010076 * struct pinctrl_hog - a list item to stash control hogs
77 * @node: pin control hog list node
Linus Walleij2744e8a2011-05-02 20:50:54 +020078 * @map: map entry responsible for this hogging
Linus Walleije93bcee2012-02-09 07:23:28 +010079 * @pmx: the pin control hogged by this item
Linus Walleij2744e8a2011-05-02 20:50:54 +020080 */
Linus Walleije93bcee2012-02-09 07:23:28 +010081struct pinctrl_hog {
Linus Walleij2744e8a2011-05-02 20:50:54 +020082 struct list_head node;
Linus Walleije93bcee2012-02-09 07:23:28 +010083 struct pinctrl_map const *map;
84 struct pinctrl *p;
Linus Walleij2744e8a2011-05-02 20:50:54 +020085};
86
87/**
88 * pin_request() - request a single pin to be muxed in, typically for GPIO
89 * @pin: the pin number in the global pin space
90 * @function: a functional name to give to this pin, passed to the driver
91 * so it knows what function to mux in, e.g. the string "gpioNN"
92 * means that you want to mux in the pin for use as GPIO number NN
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 * @gpio_range: the range matching the GPIO pin if this is a request for a
94 * single GPIO pin
95 */
96static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3712a3c2011-10-21 12:25:53 -060097 int pin, const char *function,
Linus Walleij2744e8a2011-05-02 20:50:54 +020098 struct pinctrl_gpio_range *gpio_range)
99{
100 struct pin_desc *desc;
101 const struct pinmux_ops *ops = pctldev->desc->pmxops;
102 int status = -EINVAL;
103
Stephen Warren51cd24e2011-12-09 16:59:05 -0700104 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105
Linus Walleij2744e8a2011-05-02 20:50:54 +0200106 desc = pin_desc_get(pctldev, pin);
107 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700108 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 "pin is not registered so it cannot be requested\n");
110 goto out;
111 }
112
Marek Beliskod2f6a1c2011-10-26 22:57:20 +0200113 if (!function) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700114 dev_err(pctldev->dev, "no function name given\n");
Marek Beliskod2f6a1c2011-10-26 22:57:20 +0200115 return -EINVAL;
116 }
117
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600119 if (desc->mux_function) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120 spin_unlock(&desc->lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700121 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200122 "pin already requested\n");
123 goto out;
124 }
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600125 desc->mux_function = function;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200126 spin_unlock(&desc->lock);
127
128 /* Let each pin increase references to this module */
129 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700130 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200131 "could not increase module refcount for pin %d\n",
132 pin);
133 status = -EINVAL;
134 goto out_free_pin;
135 }
136
137 /*
138 * If there is no kind of request function for the pin we just assume
139 * we got it by default and proceed.
140 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600141 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200142 /* This requests and enables a single GPIO pin */
143 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
144 else if (ops->request)
145 status = ops->request(pctldev, pin);
146 else
147 status = 0;
148
149 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100150 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200151 pctldev->desc->name, pin);
152out_free_pin:
153 if (status) {
154 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600155 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 spin_unlock(&desc->lock);
157 }
158out:
159 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700160 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200161 pin, function ? : "?", status);
162
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 *
173 * This function returns a pointer to the function name in use. This is used
174 * for callers that dynamically allocate a function name so it can be freed
175 * 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 Warren3712a3c2011-10-21 12:25:53 -0600182 const char *func;
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 Warren3712a3c2011-10-21 12:25:53 -0600191 /*
192 * If there is no kind of request function for the pin we just assume
193 * we got it by default and proceed.
194 */
195 if (gpio_range && ops->gpio_disable_free)
196 ops->gpio_disable_free(pctldev, gpio_range, pin);
197 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 ops->free(pctldev, pin);
199
200 spin_lock(&desc->lock);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600201 func = desc->mux_function;
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600202 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 spin_unlock(&desc->lock);
204 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600205
206 return func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207}
208
209/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100210 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
Linus Walleij2744e8a2011-05-02 20:50:54 +0200211 * @gpio: the GPIO pin number from the GPIO subsystem number space
Linus Walleij542e7042011-11-14 10:06:22 +0100212 *
213 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
214 * as part of their gpio_request() semantics, platforms and individual drivers
215 * shall *NOT* request GPIO pins to be muxed in.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100217int pinctrl_request_gpio(unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218{
219 char gpiostr[16];
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600220 const char *function;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 struct pinctrl_dev *pctldev;
222 struct pinctrl_gpio_range *range;
223 int ret;
224 int pin;
225
226 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
227 if (ret)
228 return -EINVAL;
229
230 /* Convert to the pin controllers number space */
Chanho Park3c739ad2011-11-11 18:47:58 +0900231 pin = gpio - range->base + range->pin_base;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
233 /* Conjure some name stating what chip and pin this is taken by */
234 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
235
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600236 function = kstrdup(gpiostr, GFP_KERNEL);
237 if (!function)
238 return -EINVAL;
239
Stephen Warren3712a3c2011-10-21 12:25:53 -0600240 ret = pin_request(pctldev, pin, function, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600241 if (ret < 0)
242 kfree(function);
243
244 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245}
Linus Walleije93bcee2012-02-09 07:23:28 +0100246EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247
248/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100249 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250 * @gpio: the GPIO pin number from the GPIO subsystem number space
Linus Walleij542e7042011-11-14 10:06:22 +0100251 *
252 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
253 * as part of their gpio_free() semantics, platforms and individual drivers
254 * shall *NOT* request GPIO pins to be muxed out.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200255 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100256void pinctrl_free_gpio(unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257{
258 struct pinctrl_dev *pctldev;
259 struct pinctrl_gpio_range *range;
260 int ret;
261 int pin;
Stephen Warren3712a3c2011-10-21 12:25:53 -0600262 const char *func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263
264 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
265 if (ret)
266 return;
267
268 /* Convert to the pin controllers number space */
Chanho Park3c739ad2011-11-11 18:47:58 +0900269 pin = gpio - range->base + range->pin_base;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200270
Stephen Warren3712a3c2011-10-21 12:25:53 -0600271 func = pin_free(pctldev, pin, range);
272 kfree(func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200273}
Linus Walleije93bcee2012-02-09 07:23:28 +0100274EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275
Linus Walleije93bcee2012-02-09 07:23:28 +0100276static int pinctrl_gpio_direction(unsigned gpio, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100277{
278 struct pinctrl_dev *pctldev;
279 struct pinctrl_gpio_range *range;
280 const struct pinmux_ops *ops;
281 int ret;
282 int pin;
283
284 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
285 if (ret)
286 return ret;
287
288 ops = pctldev->desc->pmxops;
289
290 /* Convert to the pin controllers number space */
291 pin = gpio - range->base + range->pin_base;
292
293 if (ops->gpio_set_direction)
294 ret = ops->gpio_set_direction(pctldev, range, pin, input);
295 else
296 ret = 0;
297
298 return ret;
299}
300
301/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100302 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
Linus Walleij542e7042011-11-14 10:06:22 +0100303 * @gpio: the GPIO pin number from the GPIO subsystem number space
304 *
305 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
306 * as part of their gpio_direction_input() semantics, platforms and individual
Linus Walleije93bcee2012-02-09 07:23:28 +0100307 * drivers shall *NOT* touch pin control GPIO calls.
Linus Walleij542e7042011-11-14 10:06:22 +0100308 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100309int pinctrl_gpio_direction_input(unsigned gpio)
Linus Walleij542e7042011-11-14 10:06:22 +0100310{
Linus Walleije93bcee2012-02-09 07:23:28 +0100311 return pinctrl_gpio_direction(gpio, true);
Linus Walleij542e7042011-11-14 10:06:22 +0100312}
Linus Walleije93bcee2012-02-09 07:23:28 +0100313EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
Linus Walleij542e7042011-11-14 10:06:22 +0100314
315/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100316 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
Linus Walleij542e7042011-11-14 10:06:22 +0100317 * @gpio: the GPIO pin number from the GPIO subsystem number space
318 *
319 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
320 * as part of their gpio_direction_output() semantics, platforms and individual
Linus Walleije93bcee2012-02-09 07:23:28 +0100321 * drivers shall *NOT* touch pin control GPIO calls.
Linus Walleij542e7042011-11-14 10:06:22 +0100322 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100323int pinctrl_gpio_direction_output(unsigned gpio)
Linus Walleij542e7042011-11-14 10:06:22 +0100324{
Linus Walleije93bcee2012-02-09 07:23:28 +0100325 return pinctrl_gpio_direction(gpio, false);
Linus Walleij542e7042011-11-14 10:06:22 +0100326}
Linus Walleije93bcee2012-02-09 07:23:28 +0100327EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
Linus Walleij542e7042011-11-14 10:06:22 +0100328
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100330 * pinctrl_register_mappings() - register a set of pin controller mappings
331 * @maps: the pincontrol mappings table to register, this should be marked with
Linus Walleij97607d12011-11-29 12:52:39 +0100332 * __initdata so it can be discarded after boot, this function will
333 * perform a shallow copy for the mapping entries.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334 * @num_maps: the number of maps in the mapping table
335 *
336 * Only call this once during initialization of your machine, the function is
337 * tagged as __init and won't be callable after init has completed. The map
338 * passed into this function will be owned by the pinmux core and cannot be
Dong Aishenge6337c32011-12-20 17:51:59 +0800339 * freed.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200340 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100341int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
342 unsigned num_maps)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200343{
Linus Walleij59b099b2011-11-30 13:28:14 +0100344 void *tmp_maps;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200345 int i;
346
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347 pr_debug("add %d pinmux maps\n", num_maps);
Linus Walleij97607d12011-11-29 12:52:39 +0100348
Linus Walleij59b099b2011-11-30 13:28:14 +0100349 /* First sanity check the new mapping */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200350 for (i = 0; i < num_maps; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200351 if (!maps[i].name) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100352 pr_err("failed to register map %d: no map name given\n",
353 i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100354 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355 }
Linus Walleij97607d12011-11-29 12:52:39 +0100356
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100357 if (!maps[i].ctrl_dev_name) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100358 pr_err("failed to register map %s (%d): no pin control device given\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200359 maps[i].name, i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100360 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200361 }
Linus Walleij97607d12011-11-29 12:52:39 +0100362
Linus Walleij2744e8a2011-05-02 20:50:54 +0200363 if (!maps[i].function) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100364 pr_err("failed to register map %s (%d): no function ID given\n",
365 maps[i].name, i);
Linus Walleij59b099b2011-11-30 13:28:14 +0100366 return -EINVAL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 }
368
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100369 if (!maps[i].dev_name)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200370 pr_debug("add system map %s function %s with no device\n",
371 maps[i].name,
372 maps[i].function);
373 else
374 pr_debug("register map %s, function %s\n",
375 maps[i].name,
376 maps[i].function);
377 }
378
Linus Walleij59b099b2011-11-30 13:28:14 +0100379 /*
380 * Make a copy of the map array - string pointers will end up in the
381 * kernel const section anyway so these do not need to be deep copied.
382 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100383 if (!pinctrl_maps_num) {
Linus Walleij59b099b2011-11-30 13:28:14 +0100384 /* On first call, just copy them */
385 tmp_maps = kmemdup(maps,
Linus Walleije93bcee2012-02-09 07:23:28 +0100386 sizeof(struct pinctrl_map) * num_maps,
Linus Walleij59b099b2011-11-30 13:28:14 +0100387 GFP_KERNEL);
388 if (!tmp_maps)
389 return -ENOMEM;
390 } else {
391 /* Subsequent calls, reallocate array to new size */
Linus Walleije93bcee2012-02-09 07:23:28 +0100392 size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num;
393 size_t newsize = sizeof(struct pinctrl_map) * num_maps;
Linus Walleij97607d12011-11-29 12:52:39 +0100394
Linus Walleije93bcee2012-02-09 07:23:28 +0100395 tmp_maps = krealloc(pinctrl_maps,
396 oldsize + newsize, GFP_KERNEL);
Linus Walleij59b099b2011-11-30 13:28:14 +0100397 if (!tmp_maps)
398 return -ENOMEM;
399 memcpy((tmp_maps + oldsize), maps, newsize);
400 }
401
Linus Walleije93bcee2012-02-09 07:23:28 +0100402 pinctrl_maps = tmp_maps;
403 pinctrl_maps_num += num_maps;
Linus Walleij59b099b2011-11-30 13:28:14 +0100404 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200405}
406
407/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800408 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409 * @pctldev: the device to take the pins on
410 * @func_selector: the function selector to acquire the pins for
411 * @group_selector: the group selector containing the pins to acquire
412 */
413static int acquire_pins(struct pinctrl_dev *pctldev,
414 unsigned func_selector,
415 unsigned group_selector)
416{
417 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
418 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
419 const char *func = pmxops->get_function_name(pctldev,
420 func_selector);
Stephen Warrena5818a82011-10-19 16:19:25 -0600421 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200422 unsigned num_pins;
423 int ret;
424 int i;
425
426 ret = pctlops->get_group_pins(pctldev, group_selector,
427 &pins, &num_pins);
428 if (ret)
429 return ret;
430
Stephen Warren51cd24e2011-12-09 16:59:05 -0700431 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432 num_pins, group_selector);
433
434 /* Try to allocate all pins in this group, one by one */
435 for (i = 0; i < num_pins; i++) {
Stephen Warren3712a3c2011-10-21 12:25:53 -0600436 ret = pin_request(pctldev, pins[i], func, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200437 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700438 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100439 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200440 pins[i], func ? : "(undefined)",
441 pinctrl_dev_get_name(pctldev));
442 /* On error release all taken pins */
443 i--; /* this pin just failed */
444 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600445 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200446 return -ENODEV;
447 }
448 }
449 return 0;
450}
451
452/**
453 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800454 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200455 * @group_selector: the group selector containing the pins to free
456 */
457static void release_pins(struct pinctrl_dev *pctldev,
458 unsigned group_selector)
459{
460 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600461 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200462 unsigned num_pins;
463 int ret;
464 int i;
465
466 ret = pctlops->get_group_pins(pctldev, group_selector,
467 &pins, &num_pins);
468 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100469 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200470 group_selector);
471 return;
472 }
473 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600474 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200475}
476
477/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200478 * pinmux_check_pin_group() - check function and pin group combo
479 * @pctldev: device to check the pin group vs function for
480 * @func_selector: the function selector to check the pin group for, we have
481 * already looked this up in the calling function
482 * @pin_group: the pin group to match to the function
483 *
484 * This function will check that the pinmux driver can supply the
485 * selected pin group for a certain function, returns the group selector if
486 * the group and function selector will work fine together, else returns
487 * negative
488 */
489static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
490 unsigned func_selector,
491 const char *pin_group)
492{
493 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
494 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
495 int ret;
496
497 /*
498 * If the driver does not support different pin groups for the
499 * functions, we only support group 0, and assume this exists.
500 */
501 if (!pctlops || !pctlops->list_groups)
502 return 0;
503
504 /*
505 * Passing NULL (no specific group) will select the first and
506 * hopefully only group of pins available for this function.
507 */
508 if (!pin_group) {
509 char const * const *groups;
510 unsigned num_groups;
511
512 ret = pmxops->get_function_groups(pctldev, func_selector,
513 &groups, &num_groups);
514 if (ret)
515 return ret;
516 if (num_groups < 1)
517 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200518 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200519 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700520 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100521 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200522 pmxops->get_function_name(pctldev, func_selector),
523 groups[0]);
524 return ret;
525 }
526
527 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700528 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100529 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200530 pmxops->get_function_name(pctldev, func_selector),
531 groups[0],
532 ret);
533
534 return ret;
535 }
536
Stephen Warren51cd24e2011-12-09 16:59:05 -0700537 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200538 "check if we have pin group %s on controller %s\n",
539 pin_group, pinctrl_dev_get_name(pctldev));
540
Linus Walleij7afde8b2011-10-19 17:07:16 +0200541 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200542 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700543 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200544 "%s does not support pin group %s with function %s\n",
545 pinctrl_dev_get_name(pctldev),
546 pin_group,
547 pmxops->get_function_name(pctldev, func_selector));
548 }
549 return ret;
550}
551
552/**
553 * pinmux_search_function() - check pin control driver for a certain function
554 * @pctldev: device to check for function and position
555 * @map: function map containing the function and position to look for
556 * @func_selector: returns the applicable function selector if found
557 * @group_selector: returns the applicable group selector if found
558 *
559 * This will search the pinmux driver for an applicable
560 * function with a specific pin group, returns 0 if these can be mapped
561 * negative otherwise
562 */
563static int pinmux_search_function(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100564 struct pinctrl_map const *map,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200565 unsigned *func_selector,
566 unsigned *group_selector)
567{
568 const struct pinmux_ops *ops = pctldev->desc->pmxops;
569 unsigned selector = 0;
570
571 /* See if this pctldev has this function */
572 while (ops->list_functions(pctldev, selector) >= 0) {
573 const char *fname = ops->get_function_name(pctldev,
574 selector);
575 int ret;
576
577 if (!strcmp(map->function, fname)) {
578 /* Found the function, check pin group */
579 ret = pinmux_check_pin_group(pctldev, selector,
580 map->group);
581 if (ret < 0)
582 return ret;
583
584 /* This function and group selector can be used */
585 *func_selector = selector;
586 *group_selector = ret;
587 return 0;
588
589 }
590 selector++;
591 }
592
593 pr_err("%s does not support function %s\n",
594 pinctrl_dev_get_name(pctldev), map->function);
595 return -EINVAL;
596}
597
598/**
599 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
600 */
601static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100602 struct pinctrl *p,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200603 struct device *dev,
604 const char *devname,
Linus Walleije93bcee2012-02-09 07:23:28 +0100605 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200606{
607 unsigned func_selector;
608 unsigned group_selector;
609 struct pinmux_group *grp;
610 int ret;
611
612 /*
613 * Note that we're not locking the pinmux mutex here, because
614 * this is only called at pinmux initialization time when it
615 * has not been added to any list and thus is not reachable
616 * by anyone else.
617 */
618
Linus Walleije93bcee2012-02-09 07:23:28 +0100619 if (p->pctldev && p->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700620 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100621 "different pin control devices given for device %s, function %s\n",
622 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200623 return -EINVAL;
624 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100625 p->dev = dev;
626 p->pctldev = pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200627
628 /* Now go into the driver and try to match a function and group */
629 ret = pinmux_search_function(pctldev, map, &func_selector,
630 &group_selector);
631 if (ret < 0)
632 return ret;
633
634 /*
635 * If the function selector is already set, it needs to be identical,
636 * we support several groups with one function but not several
637 * functions with one or several groups in the same pinmux.
638 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100639 if (p->func_selector != UINT_MAX &&
640 p->func_selector != func_selector) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700641 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200642 "dual function defines in the map for device %s\n",
643 devname);
644 return -EINVAL;
645 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100646 p->func_selector = func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200647
648 /* Now add this group selector, we may have many of them */
649 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
650 if (!grp)
651 return -ENOMEM;
652 grp->group_selector = group_selector;
653 ret = acquire_pins(pctldev, func_selector, group_selector);
654 if (ret) {
655 kfree(grp);
656 return ret;
657 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100658 list_add(&grp->node, &p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200659
660 return 0;
661}
662
Linus Walleije93bcee2012-02-09 07:23:28 +0100663static void pinmux_free_groups(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200664{
665 struct list_head *node, *tmp;
666
Linus Walleije93bcee2012-02-09 07:23:28 +0100667 list_for_each_safe(node, tmp, &p->groups) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200668 struct pinmux_group *grp =
669 list_entry(node, struct pinmux_group, node);
670 /* Release all pins taken by this group */
Linus Walleije93bcee2012-02-09 07:23:28 +0100671 release_pins(p->pctldev, grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200672 list_del(node);
673 kfree(grp);
674 }
675}
676
677/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100678 * pinctrl_get() - retrieves the pin controller handle for a certain device
679 * @dev: the device to get the pin controller handle for
680 * @name: an optional specific control mapping name or NULL, the name is only
Linus Walleij2744e8a2011-05-02 20:50:54 +0200681 * needed if you want to have more than one mapping per device, or if you
Linus Walleije93bcee2012-02-09 07:23:28 +0100682 * need an anonymous pin control (not tied to any specific device)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200683 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100684struct pinctrl *pinctrl_get(struct device *dev, const char *name)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200685{
Linus Walleije93bcee2012-02-09 07:23:28 +0100686 struct pinctrl_map const *map = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200687 struct pinctrl_dev *pctldev = NULL;
688 const char *devname = NULL;
Linus Walleije93bcee2012-02-09 07:23:28 +0100689 struct pinctrl *p;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200690 bool found_map;
691 unsigned num_maps = 0;
692 int ret = -ENODEV;
693 int i;
694
695 /* We must have dev or ID or both */
696 if (!dev && !name)
697 return ERR_PTR(-EINVAL);
698
699 if (dev)
700 devname = dev_name(dev);
701
702 pr_debug("get mux %s for device %s\n", name,
703 devname ? devname : "(none)");
704
705 /*
706 * create the state cookie holder struct pinmux for each
707 * mapping, this is what consumers will get when requesting
708 * a pinmux handle with pinmux_get()
709 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100710 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
711 if (p == NULL)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200712 return ERR_PTR(-ENOMEM);
Linus Walleije93bcee2012-02-09 07:23:28 +0100713 mutex_init(&p->mutex);
714 p->func_selector = UINT_MAX;
715 INIT_LIST_HEAD(&p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200716
Linus Walleije93bcee2012-02-09 07:23:28 +0100717 /* Iterate over the pin control maps to locate the right ones */
718 for (i = 0; i < pinctrl_maps_num; i++) {
719 map = &pinctrl_maps[i];
Linus Walleij2744e8a2011-05-02 20:50:54 +0200720 found_map = false;
721
722 /*
723 * First, try to find the pctldev given in the map
724 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100725 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200726 if (!pctldev) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100727 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200728 map->function);
729 pr_warning("given pinctrl device name: %s",
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100730 map->ctrl_dev_name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200731
732 /* Continue to check the other mappings anyway... */
733 continue;
734 }
735
736 pr_debug("in map, found pctldev %s to handle function %s",
Stephen Warren51cd24e2011-12-09 16:59:05 -0700737 dev_name(pctldev->dev), map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200738
739
740 /*
741 * If we're looking for a specific named map, this must match,
742 * else we loop and look for the next.
743 */
744 if (name != NULL) {
745 if (map->name == NULL)
746 continue;
747 if (strcmp(map->name, name))
748 continue;
749 }
750
751 /*
752 * This is for the case where no device name is given, we
753 * already know that the function name matches from above
754 * code.
755 */
756 if (!map->dev_name && (name != NULL))
757 found_map = true;
758
759 /* If the mapping has a device set up it must match */
760 if (map->dev_name &&
761 (!devname || !strcmp(map->dev_name, devname)))
762 /* MATCH! */
763 found_map = true;
764
765 /* If this map is applicable, then apply it */
766 if (found_map) {
Linus Walleije93bcee2012-02-09 07:23:28 +0100767 ret = pinmux_enable_muxmap(pctldev, p, dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200768 devname, map);
769 if (ret) {
Linus Walleije93bcee2012-02-09 07:23:28 +0100770 pinmux_free_groups(p);
771 kfree(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200772 return ERR_PTR(ret);
773 }
774 num_maps++;
775 }
776 }
777
778
779 /* We should have atleast one map, right */
780 if (!num_maps) {
781 pr_err("could not find any mux maps for device %s, ID %s\n",
782 devname ? devname : "(anonymous)",
783 name ? name : "(undefined)");
Linus Walleije93bcee2012-02-09 07:23:28 +0100784 kfree(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200785 return ERR_PTR(-EINVAL);
786 }
787
788 pr_debug("found %u mux maps for device %s, UD %s\n",
789 num_maps,
790 devname ? devname : "(anonymous)",
791 name ? name : "(undefined)");
792
793 /* Add the pinmux to the global list */
Linus Walleije93bcee2012-02-09 07:23:28 +0100794 mutex_lock(&pinctrl_list_mutex);
795 list_add(&p->node, &pinctrl_list);
796 mutex_unlock(&pinctrl_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200797
Linus Walleije93bcee2012-02-09 07:23:28 +0100798 return p;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200799}
Linus Walleije93bcee2012-02-09 07:23:28 +0100800EXPORT_SYMBOL_GPL(pinctrl_get);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200801
802/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100803 * pinctrl_put() - release a previously claimed pin control handle
804 * @p: a pin control handle previously claimed by pinctrl_get()
Linus Walleij2744e8a2011-05-02 20:50:54 +0200805 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100806void pinctrl_put(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200807{
Linus Walleije93bcee2012-02-09 07:23:28 +0100808 if (p == NULL)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200809 return;
810
Linus Walleije93bcee2012-02-09 07:23:28 +0100811 mutex_lock(&p->mutex);
812 if (p->usecount)
813 pr_warn("releasing pin control handle with active users!\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200814 /* Free the groups and all acquired pins */
Linus Walleije93bcee2012-02-09 07:23:28 +0100815 pinmux_free_groups(p);
816 mutex_unlock(&p->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200817
818 /* Remove from list */
Linus Walleije93bcee2012-02-09 07:23:28 +0100819 mutex_lock(&pinctrl_list_mutex);
820 list_del(&p->node);
821 mutex_unlock(&pinctrl_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200822
Linus Walleije93bcee2012-02-09 07:23:28 +0100823 kfree(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200824}
Linus Walleije93bcee2012-02-09 07:23:28 +0100825EXPORT_SYMBOL_GPL(pinctrl_put);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200826
827/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100828 * pinctrl_enable() - enable a certain pin controller setting
829 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
Linus Walleij2744e8a2011-05-02 20:50:54 +0200830 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100831int pinctrl_enable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200832{
833 int ret = 0;
834
Linus Walleije93bcee2012-02-09 07:23:28 +0100835 if (p == NULL)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200836 return -EINVAL;
Linus Walleije93bcee2012-02-09 07:23:28 +0100837 mutex_lock(&p->mutex);
838 if (p->usecount++ == 0) {
839 struct pinctrl_dev *pctldev = p->pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200840 const struct pinmux_ops *ops = pctldev->desc->pmxops;
841 struct pinmux_group *grp;
842
Linus Walleije93bcee2012-02-09 07:23:28 +0100843 list_for_each_entry(grp, &p->groups, node) {
844 ret = ops->enable(pctldev, p->func_selector,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200845 grp->group_selector);
846 if (ret) {
847 /*
848 * TODO: call disable() on all groups we called
849 * enable() on to this point?
850 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100851 p->usecount--;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200852 break;
853 }
854 }
855 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100856 mutex_unlock(&p->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200857 return ret;
858}
Linus Walleije93bcee2012-02-09 07:23:28 +0100859EXPORT_SYMBOL_GPL(pinctrl_enable);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200860
861/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100862 * pinctrl_disable() - disable a certain pin control setting
863 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
Linus Walleij2744e8a2011-05-02 20:50:54 +0200864 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100865void pinctrl_disable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200866{
Linus Walleije93bcee2012-02-09 07:23:28 +0100867 if (p == NULL)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200868 return;
869
Linus Walleije93bcee2012-02-09 07:23:28 +0100870 mutex_lock(&p->mutex);
871 if (--p->usecount == 0) {
872 struct pinctrl_dev *pctldev = p->pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200873 const struct pinmux_ops *ops = pctldev->desc->pmxops;
874 struct pinmux_group *grp;
875
Linus Walleije93bcee2012-02-09 07:23:28 +0100876 list_for_each_entry(grp, &p->groups, node) {
877 ops->disable(pctldev, p->func_selector,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200878 grp->group_selector);
879 }
880 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100881 mutex_unlock(&p->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200882}
Linus Walleije93bcee2012-02-09 07:23:28 +0100883EXPORT_SYMBOL_GPL(pinctrl_disable);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200884
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800885int pinmux_check_ops(struct pinctrl_dev *pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200886{
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800887 const struct pinmux_ops *ops = pctldev->desc->pmxops;
888 unsigned selector = 0;
889
Linus Walleij2744e8a2011-05-02 20:50:54 +0200890 /* Check that we implement required operations */
891 if (!ops->list_functions ||
892 !ops->get_function_name ||
893 !ops->get_function_groups ||
894 !ops->enable ||
895 !ops->disable)
896 return -EINVAL;
897
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800898 /* Check that all functions registered have names */
899 while (ops->list_functions(pctldev, selector) >= 0) {
900 const char *fname = ops->get_function_name(pctldev,
901 selector);
902 if (!fname) {
903 pr_err("pinmux ops has no name for function%u\n",
904 selector);
905 return -EINVAL;
906 }
907 selector++;
908 }
909
Linus Walleij2744e8a2011-05-02 20:50:54 +0200910 return 0;
911}
912
913/* Hog a single map entry and add to the hoglist */
Linus Walleije93bcee2012-02-09 07:23:28 +0100914static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
915 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200916{
Linus Walleije93bcee2012-02-09 07:23:28 +0100917 struct pinctrl_hog *hog;
918 struct pinctrl *p;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200919 int ret;
920
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100921 if (map->dev_name) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200922 /*
923 * TODO: the day we have device tree support, we can
924 * traverse the device tree and hog to specific device nodes
925 * without any problems, so then we can hog pinmuxes for
926 * all devices that just want a static pin mux at this point.
927 */
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100928 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
929 map->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200930 return -EINVAL;
931 }
932
Linus Walleije93bcee2012-02-09 07:23:28 +0100933 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200934 if (!hog)
935 return -ENOMEM;
936
Linus Walleije93bcee2012-02-09 07:23:28 +0100937 p = pinctrl_get(NULL, map->name);
938 if (IS_ERR(p)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200939 kfree(hog);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700940 dev_err(pctldev->dev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100941 "could not get the %s pin control mapping for hogging\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200942 map->name);
Linus Walleije93bcee2012-02-09 07:23:28 +0100943 return PTR_ERR(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200944 }
945
Linus Walleije93bcee2012-02-09 07:23:28 +0100946 ret = pinctrl_enable(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200947 if (ret) {
Linus Walleije93bcee2012-02-09 07:23:28 +0100948 pinctrl_put(p);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200949 kfree(hog);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700950 dev_err(pctldev->dev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100951 "could not enable the %s pin control mapping for hogging\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200952 map->name);
953 return ret;
954 }
955
956 hog->map = map;
Linus Walleije93bcee2012-02-09 07:23:28 +0100957 hog->p = p;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200958
Stephen Warren51cd24e2011-12-09 16:59:05 -0700959 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200960 map->function);
Linus Walleije93bcee2012-02-09 07:23:28 +0100961 mutex_lock(&pctldev->pinctrl_hogs_lock);
962 list_add(&hog->node, &pctldev->pinctrl_hogs);
963 mutex_unlock(&pctldev->pinctrl_hogs_lock);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200964
965 return 0;
966}
967
968/**
Linus Walleije93bcee2012-02-09 07:23:28 +0100969 * pinctrl_hog_maps() - hog specific map entries on controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +0200970 * @pctldev: the pin control device to hog entries on
971 *
972 * When the pin controllers are registered, there may be some specific pinmux
973 * map entries that need to be hogged, i.e. get+enabled until the system shuts
974 * down.
975 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100976int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200977{
Stephen Warren51cd24e2011-12-09 16:59:05 -0700978 struct device *dev = pctldev->dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200979 const char *devname = dev_name(dev);
980 int ret;
981 int i;
982
Linus Walleije93bcee2012-02-09 07:23:28 +0100983 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
984 mutex_init(&pctldev->pinctrl_hogs_lock);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200985
Linus Walleije93bcee2012-02-09 07:23:28 +0100986 for (i = 0; i < pinctrl_maps_num; i++) {
987 struct pinctrl_map const *map = &pinctrl_maps[i];
Linus Walleij2744e8a2011-05-02 20:50:54 +0200988
Tony Lindgren9e2551e2012-01-20 07:43:53 -0800989 if (!map->hog_on_boot)
990 continue;
991
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100992 if (map->ctrl_dev_name &&
993 !strcmp(map->ctrl_dev_name, devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200994 /* OK time to hog! */
Linus Walleije93bcee2012-02-09 07:23:28 +0100995 ret = pinctrl_hog_map(pctldev, map);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200996 if (ret)
997 return ret;
998 }
999 }
1000 return 0;
1001}
1002
1003/**
Linus Walleije93bcee2012-02-09 07:23:28 +01001004 * pinctrl_unhog_maps() - unhog specific map entries on controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001005 * @pctldev: the pin control device to unhog entries on
1006 */
Linus Walleije93bcee2012-02-09 07:23:28 +01001007void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001008{
1009 struct list_head *node, *tmp;
1010
Linus Walleije93bcee2012-02-09 07:23:28 +01001011 mutex_lock(&pctldev->pinctrl_hogs_lock);
1012 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
1013 struct pinctrl_hog *hog =
1014 list_entry(node, struct pinctrl_hog, node);
1015 pinctrl_disable(hog->p);
1016 pinctrl_put(hog->p);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001017 list_del(node);
1018 kfree(hog);
1019 }
Linus Walleije93bcee2012-02-09 07:23:28 +01001020 mutex_unlock(&pctldev->pinctrl_hogs_lock);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001021}
1022
1023#ifdef CONFIG_DEBUG_FS
1024
1025/* Called from pincontrol core */
1026static int pinmux_functions_show(struct seq_file *s, void *what)
1027{
1028 struct pinctrl_dev *pctldev = s->private;
1029 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1030 unsigned func_selector = 0;
1031
1032 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1033 const char *func = pmxops->get_function_name(pctldev,
1034 func_selector);
1035 const char * const *groups;
1036 unsigned num_groups;
1037 int ret;
1038 int i;
1039
1040 ret = pmxops->get_function_groups(pctldev, func_selector,
1041 &groups, &num_groups);
1042 if (ret)
1043 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1044 func);
1045
1046 seq_printf(s, "function: %s, groups = [ ", func);
1047 for (i = 0; i < num_groups; i++)
1048 seq_printf(s, "%s ", groups[i]);
1049 seq_puts(s, "]\n");
1050
1051 func_selector++;
1052
1053 }
1054
1055 return 0;
1056}
1057
1058static int pinmux_pins_show(struct seq_file *s, void *what)
1059{
1060 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +09001061 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001062
1063 seq_puts(s, "Pinmux settings per pin\n");
1064 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1065
Chanho Park706e8522012-01-03 16:47:50 +09001066 /* The pin number can be retrived from the pin controller descriptor */
1067 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001068
1069 struct pin_desc *desc;
1070
Chanho Park706e8522012-01-03 16:47:50 +09001071 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001072 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +09001073 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001074 if (desc == NULL)
1075 continue;
1076
1077 seq_printf(s, "pin %d (%s): %s\n", pin,
1078 desc->name ? desc->name : "unnamed",
Stephen Warren5d2eaf82011-10-19 16:19:28 -06001079 desc->mux_function ? desc->mux_function
1080 : "UNCLAIMED");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081 }
1082
1083 return 0;
1084}
1085
1086static int pinmux_hogs_show(struct seq_file *s, void *what)
1087{
1088 struct pinctrl_dev *pctldev = s->private;
Linus Walleije93bcee2012-02-09 07:23:28 +01001089 struct pinctrl_hog *hog;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001090
Linus Walleije93bcee2012-02-09 07:23:28 +01001091 seq_puts(s, "Pin control map hogs held by device\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001092
Linus Walleije93bcee2012-02-09 07:23:28 +01001093 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001094 seq_printf(s, "%s\n", hog->map->name);
1095
1096 return 0;
1097}
1098
1099static int pinmux_show(struct seq_file *s, void *what)
1100{
Linus Walleije93bcee2012-02-09 07:23:28 +01001101 struct pinctrl *p;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001102
1103 seq_puts(s, "Requested pinmuxes and their maps:\n");
Linus Walleije93bcee2012-02-09 07:23:28 +01001104 list_for_each_entry(p, &pinctrl_list, node) {
1105 struct pinctrl_dev *pctldev = p->pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001106 const struct pinmux_ops *pmxops;
1107 const struct pinctrl_ops *pctlops;
1108 struct pinmux_group *grp;
1109
1110 if (!pctldev) {
1111 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1112 continue;
1113 }
1114
1115 pmxops = pctldev->desc->pmxops;
1116 pctlops = pctldev->desc->pctlops;
1117
1118 seq_printf(s, "device: %s function: %s (%u),",
Linus Walleije93bcee2012-02-09 07:23:28 +01001119 pinctrl_dev_get_name(p->pctldev),
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +01001120 pmxops->get_function_name(pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +01001121 p->func_selector),
1122 p->func_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001123
1124 seq_printf(s, " groups: [");
Linus Walleije93bcee2012-02-09 07:23:28 +01001125 list_for_each_entry(grp, &p->groups, node) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001126 seq_printf(s, " %s (%u)",
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +01001127 pctlops->get_group_name(pctldev,
1128 grp->group_selector),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001129 grp->group_selector);
1130 }
1131 seq_printf(s, " ]");
1132
1133 seq_printf(s, " users: %u map-> %s\n",
Linus Walleije93bcee2012-02-09 07:23:28 +01001134 p->usecount,
1135 p->dev ? dev_name(p->dev) : "(system)");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001136 }
1137
1138 return 0;
1139}
1140
Linus Walleije93bcee2012-02-09 07:23:28 +01001141static int pinctrl_maps_show(struct seq_file *s, void *what)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001142{
1143 int i;
1144
Linus Walleije93bcee2012-02-09 07:23:28 +01001145 seq_puts(s, "Pinctrl maps:\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001146
Linus Walleije93bcee2012-02-09 07:23:28 +01001147 for (i = 0; i < pinctrl_maps_num; i++) {
1148 struct pinctrl_map const *map = &pinctrl_maps[i];
Linus Walleij2744e8a2011-05-02 20:50:54 +02001149
1150 seq_printf(s, "%s:\n", map->name);
Linus Walleij9dfac4f2012-02-01 18:02:47 +01001151 if (map->dev_name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001152 seq_printf(s, " device: %s\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +02001153 map->dev_name);
1154 else
1155 seq_printf(s, " SYSTEM MUX\n");
1156 seq_printf(s, " controlling device %s\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +02001157 map->ctrl_dev_name);
1158 seq_printf(s, " function: %s\n", map->function);
1159 seq_printf(s, " group: %s\n", map->group ? map->group :
1160 "(default)");
1161 }
1162 return 0;
1163}
1164
1165static int pinmux_functions_open(struct inode *inode, struct file *file)
1166{
1167 return single_open(file, pinmux_functions_show, inode->i_private);
1168}
1169
1170static int pinmux_pins_open(struct inode *inode, struct file *file)
1171{
1172 return single_open(file, pinmux_pins_show, inode->i_private);
1173}
1174
1175static int pinmux_hogs_open(struct inode *inode, struct file *file)
1176{
1177 return single_open(file, pinmux_hogs_show, inode->i_private);
1178}
1179
1180static int pinmux_open(struct inode *inode, struct file *file)
1181{
1182 return single_open(file, pinmux_show, NULL);
1183}
1184
Linus Walleije93bcee2012-02-09 07:23:28 +01001185static int pinctrl_maps_open(struct inode *inode, struct file *file)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001186{
Linus Walleije93bcee2012-02-09 07:23:28 +01001187 return single_open(file, pinctrl_maps_show, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001188}
1189
1190static const struct file_operations pinmux_functions_ops = {
1191 .open = pinmux_functions_open,
1192 .read = seq_read,
1193 .llseek = seq_lseek,
1194 .release = single_release,
1195};
1196
1197static const struct file_operations pinmux_pins_ops = {
1198 .open = pinmux_pins_open,
1199 .read = seq_read,
1200 .llseek = seq_lseek,
1201 .release = single_release,
1202};
1203
1204static const struct file_operations pinmux_hogs_ops = {
1205 .open = pinmux_hogs_open,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = single_release,
1209};
1210
1211static const struct file_operations pinmux_ops = {
1212 .open = pinmux_open,
1213 .read = seq_read,
1214 .llseek = seq_lseek,
1215 .release = single_release,
1216};
1217
Linus Walleije93bcee2012-02-09 07:23:28 +01001218static const struct file_operations pinctrl_maps_ops = {
1219 .open = pinctrl_maps_open,
Linus Walleij2744e8a2011-05-02 20:50:54 +02001220 .read = seq_read,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223};
1224
1225void pinmux_init_device_debugfs(struct dentry *devroot,
1226 struct pinctrl_dev *pctldev)
1227{
1228 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1229 devroot, pctldev, &pinmux_functions_ops);
1230 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1231 devroot, pctldev, &pinmux_pins_ops);
1232 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1233 devroot, pctldev, &pinmux_hogs_ops);
1234}
1235
1236void pinmux_init_debugfs(struct dentry *subsys_root)
1237{
1238 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1239 subsys_root, NULL, &pinmux_ops);
Linus Walleije93bcee2012-02-09 07:23:28 +01001240 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1241 subsys_root, NULL, &pinctrl_maps_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001242}
1243
1244#endif /* CONFIG_DEBUG_FS */