blob: 535f8d53c28947994763a86f38290f07fd83525a [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020021#include <linux/err.h>
22#include <linux/list.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/pinctrl.h>
27#include <linux/pinctrl/machine.h>
28#include "core.h"
29#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020030#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031
Linus Walleijbefe5bd2012-02-09 19:47:48 +010032/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070033 * struct pinctrl_maps - a list item containing part of the mapping table
34 * @node: mapping table list node
35 * @maps: array of mapping table entries
36 * @num_maps: the number of entries in @maps
37 */
38struct pinctrl_maps {
39 struct list_head node;
40 struct pinctrl_map const *maps;
41 unsigned num_maps;
42};
43
Stephen Warren57b676f2012-03-02 13:05:44 -070044/* Mutex taken by all entry points */
45DEFINE_MUTEX(pinctrl_mutex);
46
47/* Global list of pin control devices (struct pinctrl_dev) */
Linus Walleij2744e8a2011-05-02 20:50:54 +020048static LIST_HEAD(pinctrldev_list);
49
Stephen Warren57b676f2012-03-02 13:05:44 -070050/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010051static LIST_HEAD(pinctrl_list);
52
Stephen Warren57b676f2012-03-02 13:05:44 -070053/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070054static LIST_HEAD(pinctrl_maps);
55
56#define for_each_maps(_maps_node_, _i_, _map_) \
57 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
58 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
59 _i_ < _maps_node_->num_maps; \
60 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Linus Walleij2744e8a2011-05-02 20:50:54 +020062const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
63{
64 /* We're not allowed to register devices without name */
65 return pctldev->desc->name;
66}
67EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
68
69void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
70{
71 return pctldev->driver_data;
72}
73EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
74
75/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010076 * get_pinctrl_dev_from_devname() - look up pin controller device
77 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020078 *
79 * Looks up a pin control device matching a certain device name or pure device
80 * pointer, the pure device pointer will take precedence.
81 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010082struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020083{
84 struct pinctrl_dev *pctldev = NULL;
85 bool found = false;
86
Linus Walleij9dfac4f2012-02-01 18:02:47 +010087 if (!devname)
88 return NULL;
89
Linus Walleij2744e8a2011-05-02 20:50:54 +020090 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010091 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 /* Matched on device name */
93 found = true;
94 break;
95 }
96 }
Linus Walleij2744e8a2011-05-02 20:50:54 +020097
98 return found ? pctldev : NULL;
99}
100
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200102 * pin_get_from_name() - look up a pin number from a name
103 * @pctldev: the pin control device to lookup the pin on
104 * @name: the name of the pin to look up
105 */
106int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
107{
Chanho Park706e8522012-01-03 16:47:50 +0900108 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200109
Chanho Park706e8522012-01-03 16:47:50 +0900110 /* The pin number can be retrived from the pin controller descriptor */
111 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200112 struct pin_desc *desc;
113
Chanho Park706e8522012-01-03 16:47:50 +0900114 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200115 desc = pin_desc_get(pctldev, pin);
116 /* Pin space may be sparse */
117 if (desc == NULL)
118 continue;
119 if (desc->name && !strcmp(name, desc->name))
120 return pin;
121 }
122
123 return -EINVAL;
124}
125
126/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200127 * pin_is_valid() - check if pin exists on controller
128 * @pctldev: the pin control device to check the pin on
129 * @pin: pin to check, use the local pin controller index number
130 *
131 * This tells us whether a certain pin exist on a certain pin controller or
132 * not. Pin lists may be sparse, so some pins may not exist.
133 */
134bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
135{
136 struct pin_desc *pindesc;
137
138 if (pin < 0)
139 return false;
140
Stephen Warren57b676f2012-03-02 13:05:44 -0700141 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200142 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700143 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200144
Stephen Warren57b676f2012-03-02 13:05:44 -0700145 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200146}
147EXPORT_SYMBOL_GPL(pin_is_valid);
148
149/* Deletes a range of pin descriptors */
150static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
151 const struct pinctrl_pin_desc *pins,
152 unsigned num_pins)
153{
154 int i;
155
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 for (i = 0; i < num_pins; i++) {
157 struct pin_desc *pindesc;
158
159 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
160 pins[i].number);
161 if (pindesc != NULL) {
162 radix_tree_delete(&pctldev->pin_desc_tree,
163 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100164 if (pindesc->dynamic_name)
165 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200166 }
167 kfree(pindesc);
168 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200169}
170
171static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
172 unsigned number, const char *name)
173{
174 struct pin_desc *pindesc;
175
176 pindesc = pin_desc_get(pctldev, number);
177 if (pindesc != NULL) {
178 pr_err("pin %d already registered on %s\n", number,
179 pctldev->desc->name);
180 return -EINVAL;
181 }
182
183 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700184 if (pindesc == NULL) {
185 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700187 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200188
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189 /* Set owner */
190 pindesc->pctldev = pctldev;
191
Stephen Warren9af1e442011-10-19 16:19:27 -0600192 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100193 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100194 pindesc->name = name;
195 } else {
196 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
197 if (pindesc->name == NULL)
198 return -ENOMEM;
199 pindesc->dynamic_name = true;
200 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100204 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205 return 0;
206}
207
208static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
209 struct pinctrl_pin_desc const *pins,
210 unsigned num_descs)
211{
212 unsigned i;
213 int ret = 0;
214
215 for (i = 0; i < num_descs; i++) {
216 ret = pinctrl_register_one_pin(pctldev,
217 pins[i].number, pins[i].name);
218 if (ret)
219 return ret;
220 }
221
222 return 0;
223}
224
225/**
226 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
227 * @pctldev: pin controller device to check
228 * @gpio: gpio pin to check taken from the global GPIO pin space
229 *
230 * Tries to match a GPIO pin number to the ranges handled by a certain pin
231 * controller, return the range or NULL
232 */
233static struct pinctrl_gpio_range *
234pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
235{
236 struct pinctrl_gpio_range *range = NULL;
237
238 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
240 /* Check if we're in the valid range */
241 if (gpio >= range->base &&
242 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 return range;
244 }
245 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246
247 return NULL;
248}
249
250/**
251 * pinctrl_get_device_gpio_range() - find device for GPIO range
252 * @gpio: the pin to locate the pin controller for
253 * @outdev: the pin control device if found
254 * @outrange: the GPIO range if found
255 *
256 * Find the pin controller handling a certain GPIO pin from the pinspace of
257 * the GPIO subsystem, return the device and the matching GPIO range. Returns
258 * negative if the GPIO range could not be found in any device.
259 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700260static int pinctrl_get_device_gpio_range(unsigned gpio,
261 struct pinctrl_dev **outdev,
262 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263{
264 struct pinctrl_dev *pctldev = NULL;
265
266 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267 list_for_each_entry(pctldev, &pinctrldev_list, node) {
268 struct pinctrl_gpio_range *range;
269
270 range = pinctrl_match_gpio_range(pctldev, gpio);
271 if (range != NULL) {
272 *outdev = pctldev;
273 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200274 return 0;
275 }
276 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277
278 return -EINVAL;
279}
280
281/**
282 * pinctrl_add_gpio_range() - register a GPIO range for a controller
283 * @pctldev: pin controller device to add the range to
284 * @range: the GPIO range to add
285 *
286 * This adds a range of GPIOs to be handled by a certain pin controller. Call
287 * this to register handled ranges after registering your pin controller.
288 */
289void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
290 struct pinctrl_gpio_range *range)
291{
Stephen Warren57b676f2012-03-02 13:05:44 -0700292 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700293 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700294 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700296EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297
298/**
299 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
300 * @pctldev: pin controller device to remove the range from
301 * @range: the GPIO range to remove
302 */
303void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range)
305{
Stephen Warren57b676f2012-03-02 13:05:44 -0700306 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700308 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700310EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200311
Linus Walleij7afde8b2011-10-19 17:07:16 +0200312/**
313 * pinctrl_get_group_selector() - returns the group selector for a group
314 * @pctldev: the pin controller handling the group
315 * @pin_group: the pin group to look up
316 */
317int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
318 const char *pin_group)
319{
320 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
321 unsigned group_selector = 0;
322
323 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
324 const char *gname = pctlops->get_group_name(pctldev,
325 group_selector);
326 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700327 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200328 "found group selector %u for %s\n",
329 group_selector,
330 pin_group);
331 return group_selector;
332 }
333
334 group_selector++;
335 }
336
Stephen Warren51cd24e2011-12-09 16:59:05 -0700337 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200338 pin_group);
339
340 return -EINVAL;
341}
342
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100343/**
344 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
345 * @gpio: the GPIO pin number from the GPIO subsystem number space
346 *
347 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
348 * as part of their gpio_request() semantics, platforms and individual drivers
349 * shall *NOT* request GPIO pins to be muxed in.
350 */
351int pinctrl_request_gpio(unsigned gpio)
352{
353 struct pinctrl_dev *pctldev;
354 struct pinctrl_gpio_range *range;
355 int ret;
356 int pin;
357
Stephen Warren57b676f2012-03-02 13:05:44 -0700358 mutex_lock(&pinctrl_mutex);
359
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100360 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700361 if (ret) {
362 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100363 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700364 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100365
366 /* Convert to the pin controllers number space */
367 pin = gpio - range->base + range->pin_base;
368
Stephen Warren57b676f2012-03-02 13:05:44 -0700369 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
370
371 mutex_unlock(&pinctrl_mutex);
372 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100373}
374EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
375
376/**
377 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
378 * @gpio: the GPIO pin number from the GPIO subsystem number space
379 *
380 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
381 * as part of their gpio_free() semantics, platforms and individual drivers
382 * shall *NOT* request GPIO pins to be muxed out.
383 */
384void pinctrl_free_gpio(unsigned gpio)
385{
386 struct pinctrl_dev *pctldev;
387 struct pinctrl_gpio_range *range;
388 int ret;
389 int pin;
390
Stephen Warren57b676f2012-03-02 13:05:44 -0700391 mutex_lock(&pinctrl_mutex);
392
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100393 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700394 if (ret) {
395 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100396 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700397 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398
399 /* Convert to the pin controllers number space */
400 pin = gpio - range->base + range->pin_base;
401
Stephen Warren57b676f2012-03-02 13:05:44 -0700402 pinmux_free_gpio(pctldev, pin, range);
403
404 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100405}
406EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
407
408static int pinctrl_gpio_direction(unsigned gpio, bool input)
409{
410 struct pinctrl_dev *pctldev;
411 struct pinctrl_gpio_range *range;
412 int ret;
413 int pin;
414
415 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
416 if (ret)
417 return ret;
418
419 /* Convert to the pin controllers number space */
420 pin = gpio - range->base + range->pin_base;
421
422 return pinmux_gpio_direction(pctldev, range, pin, input);
423}
424
425/**
426 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
427 * @gpio: the GPIO pin number from the GPIO subsystem number space
428 *
429 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
430 * as part of their gpio_direction_input() semantics, platforms and individual
431 * drivers shall *NOT* touch pin control GPIO calls.
432 */
433int pinctrl_gpio_direction_input(unsigned gpio)
434{
Stephen Warren57b676f2012-03-02 13:05:44 -0700435 int ret;
436 mutex_lock(&pinctrl_mutex);
437 ret = pinctrl_gpio_direction(gpio, true);
438 mutex_unlock(&pinctrl_mutex);
439 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100440}
441EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
442
443/**
444 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
445 * @gpio: the GPIO pin number from the GPIO subsystem number space
446 *
447 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
448 * as part of their gpio_direction_output() semantics, platforms and individual
449 * drivers shall *NOT* touch pin control GPIO calls.
450 */
451int pinctrl_gpio_direction_output(unsigned gpio)
452{
Stephen Warren57b676f2012-03-02 13:05:44 -0700453 int ret;
454 mutex_lock(&pinctrl_mutex);
455 ret = pinctrl_gpio_direction(gpio, false);
456 mutex_unlock(&pinctrl_mutex);
457 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100458}
459EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
460
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700461static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100462{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700463 struct pinctrl_dev *pctldev;
Stephen Warren1681f5a2012-02-22 14:25:58 -0700464 const char *devname;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100465 struct pinctrl *p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100466 unsigned num_maps = 0;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700467 int ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700468 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100469 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700470 struct pinctrl_map const *map;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700471 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100472
Stephen Warren110e4ec2012-03-01 18:48:33 -0700473 /* We must have both a dev and state name */
474 if (WARN_ON(!dev || !name))
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100475 return ERR_PTR(-EINVAL);
476
Stephen Warren1681f5a2012-02-22 14:25:58 -0700477 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100478
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700479 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100480
481 /*
482 * create the state cookie holder struct pinctrl for each
483 * mapping, this is what consumers will get when requesting
484 * a pin control handle with pinctrl_get()
485 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700486 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700487 if (p == NULL) {
488 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100489 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700490 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700491 p->dev = dev;
492 p->state = name;
493 INIT_LIST_HEAD(&p->settings);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100494
495 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700496 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700497 /* Map must be for this device */
498 if (strcmp(map->dev_name, devname))
499 continue;
500
Stephen Warren110e4ec2012-03-01 18:48:33 -0700501 /* State name must be the one we're looking for */
502 if (strcmp(map->name, name))
503 continue;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100504
Stephen Warren7ecdb162012-03-02 13:05:45 -0700505 /*
506 * Try to find the pctldev given in the map
507 */
508 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
509 if (!pctldev) {
510 dev_err(dev, "unknown pinctrl device %s in map entry",
511 map->ctrl_dev_name);
512 /* Eventually, this should trigger deferred probe */
513 ret = -ENODEV;
514 goto error;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100515 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700516
517 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
518 dev_name(pctldev->dev), map->function);
519
520 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
521 if (setting == NULL) {
522 dev_err(dev,
523 "failed to alloc struct pinctrl_setting\n");
524 ret = -ENOMEM;
525 goto error;
526 }
527
528 setting->pctldev = pctldev;
529 ret = pinmux_map_to_setting(map, setting);
530 if (ret < 0)
531 goto error;
532
533 list_add_tail(&setting->node, &p->settings);
534
Stephen Warren1681f5a2012-02-22 14:25:58 -0700535 num_maps++;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100536 }
537
Stephen Warrenf026fe32012-02-19 23:45:51 -0700538 /*
539 * This may be perfectly legitimate. An IP block may get re-used
540 * across SoCs. Not all of those SoCs may need pinmux settings for the
541 * IP block, e.g. if one SoC dedicates pins to that function but
542 * another doesn't. The driver won't know this, and will always
543 * attempt to set up the pinmux. The mapping table defines whether any
544 * HW programming is actually needed.
545 */
546 if (!num_maps)
547 dev_info(dev, "zero maps found for mapping %s\n", name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100548
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700549 dev_dbg(dev, "found %u maps for device %s state %s\n",
550 num_maps, devname, name ? name : "(undefined)");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100551
552 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700553 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100554
555 return p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700556
557error:
558 list_for_each_entry(setting, &p->settings, node)
559 pinmux_free_setting(setting);
560
561 kfree(p);
562
563 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100564}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700565
566/**
567 * pinctrl_get() - retrieves the pin controller handle for a certain device
568 * @dev: the device to get the pin controller handle for
569 * @name: an optional specific control mapping name or NULL, the name is only
570 * needed if you want to have more than one mapping per device, or if you
571 * need an anonymous pin control (not tied to any specific device)
572 */
573struct pinctrl *pinctrl_get(struct device *dev, const char *name)
574{
575 struct pinctrl *p;
576
Stephen Warren57b676f2012-03-02 13:05:44 -0700577 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700578 p = pinctrl_get_locked(dev, name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700579 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700580
581 return p;
582}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583EXPORT_SYMBOL_GPL(pinctrl_get);
584
Stephen Warren57b676f2012-03-02 13:05:44 -0700585static void pinctrl_put_locked(struct pinctrl *p)
586{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700587 struct pinctrl_setting *setting, *n;
588
Stephen Warren57b676f2012-03-02 13:05:44 -0700589 if (p == NULL)
590 return;
591
592 if (p->usecount)
593 pr_warn("releasing pin control handle with active users!\n");
Stephen Warren7ecdb162012-03-02 13:05:45 -0700594 list_for_each_entry_safe(setting, n, &p->settings, node) {
595 pinmux_free_setting(setting);
596 list_del(&setting->node);
597 kfree(setting);
598 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700599
600 /* Remove from list */
601 list_del(&p->node);
602
603 kfree(p);
604}
605
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100606/**
607 * pinctrl_put() - release a previously claimed pin control handle
608 * @p: a pin control handle previously claimed by pinctrl_get()
609 */
610void pinctrl_put(struct pinctrl *p)
611{
Stephen Warren57b676f2012-03-02 13:05:44 -0700612 mutex_lock(&pinctrl_mutex);
613 pinctrl_put(p);
614 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100615}
616EXPORT_SYMBOL_GPL(pinctrl_put);
617
Stephen Warren57b676f2012-03-02 13:05:44 -0700618static int pinctrl_enable_locked(struct pinctrl *p)
619{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700620 struct pinctrl_setting *setting;
621 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700622
623 if (p == NULL)
624 return -EINVAL;
625
626 if (p->usecount++ == 0) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700627 list_for_each_entry(setting, &p->settings, node) {
628 ret = pinmux_enable_setting(setting);
629 if (ret < 0) {
630 /* FIXME: Difficult to return to prev state */
631 p->usecount--;
632 return ret;
633 }
634 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700635 }
636
Stephen Warren7ecdb162012-03-02 13:05:45 -0700637 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700638}
639
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100640/**
641 * pinctrl_enable() - enable a certain pin controller setting
642 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
643 */
644int pinctrl_enable(struct pinctrl *p)
645{
Stephen Warren57b676f2012-03-02 13:05:44 -0700646 int ret;
647 mutex_lock(&pinctrl_mutex);
648 ret = pinctrl_enable_locked(p);
649 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100650 return ret;
651}
652EXPORT_SYMBOL_GPL(pinctrl_enable);
653
Stephen Warren57b676f2012-03-02 13:05:44 -0700654static void pinctrl_disable_locked(struct pinctrl *p)
655{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700656 struct pinctrl_setting *setting;
657
Stephen Warren57b676f2012-03-02 13:05:44 -0700658 if (p == NULL)
659 return;
660
661 if (--p->usecount == 0) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700662 list_for_each_entry(setting, &p->settings, node)
663 pinmux_disable_setting(setting);
Stephen Warren57b676f2012-03-02 13:05:44 -0700664 }
665}
666
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100667/**
668 * pinctrl_disable() - disable a certain pin control setting
669 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
670 */
671void pinctrl_disable(struct pinctrl *p)
672{
Stephen Warren57b676f2012-03-02 13:05:44 -0700673 mutex_lock(&pinctrl_mutex);
674 pinctrl_disable_locked(p);
675 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100676}
677EXPORT_SYMBOL_GPL(pinctrl_disable);
678
679/**
680 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700681 * @maps: the pincontrol mappings table to register. This should probably be
682 * marked with __initdata so it can be discarded after boot. This
683 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100685 */
Stephen Warren13398a42012-02-19 23:45:41 -0700686int pinctrl_register_mappings(struct pinctrl_map const *maps,
687 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100688{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100689 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700690 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100691
692 pr_debug("add %d pinmux maps\n", num_maps);
693
694 /* First sanity check the new mapping */
695 for (i = 0; i < num_maps; i++) {
696 if (!maps[i].name) {
697 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700698 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100699 return -EINVAL;
700 }
701
702 if (!maps[i].ctrl_dev_name) {
703 pr_err("failed to register map %s (%d): no pin control device given\n",
704 maps[i].name, i);
705 return -EINVAL;
706 }
707
708 if (!maps[i].function) {
709 pr_err("failed to register map %s (%d): no function ID given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700710 maps[i].name, i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100711 return -EINVAL;
712 }
713
Stephen Warren1681f5a2012-02-22 14:25:58 -0700714 if (!maps[i].dev_name) {
715 pr_err("failed to register map %s (%d): no device given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700716 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700717 return -EINVAL;
718 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100719 }
720
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700721 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
722 if (!maps_node) {
723 pr_err("failed to alloc struct pinctrl_maps\n");
724 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100725 }
726
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700727 maps_node->num_maps = num_maps;
728 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
729 if (!maps_node->maps) {
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700730 pr_err("failed to duplicate mapping table\n");
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700731 kfree(maps_node);
732 return -ENOMEM;
733 }
734
Stephen Warren57b676f2012-03-02 13:05:44 -0700735 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700736 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57b676f2012-03-02 13:05:44 -0700737 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700738
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100739 return 0;
740}
741
Linus Walleij2744e8a2011-05-02 20:50:54 +0200742#ifdef CONFIG_DEBUG_FS
743
744static int pinctrl_pins_show(struct seq_file *s, void *what)
745{
746 struct pinctrl_dev *pctldev = s->private;
747 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900748 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200749
750 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200751
Stephen Warren57b676f2012-03-02 13:05:44 -0700752 mutex_lock(&pinctrl_mutex);
753
Chanho Park706e8522012-01-03 16:47:50 +0900754 /* The pin number can be retrived from the pin controller descriptor */
755 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200756 struct pin_desc *desc;
757
Chanho Park706e8522012-01-03 16:47:50 +0900758 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200759 desc = pin_desc_get(pctldev, pin);
760 /* Pin space may be sparse */
761 if (desc == NULL)
762 continue;
763
764 seq_printf(s, "pin %d (%s) ", pin,
765 desc->name ? desc->name : "unnamed");
766
767 /* Driver-specific info per pin */
768 if (ops->pin_dbg_show)
769 ops->pin_dbg_show(pctldev, s, pin);
770
771 seq_puts(s, "\n");
772 }
773
Stephen Warren57b676f2012-03-02 13:05:44 -0700774 mutex_unlock(&pinctrl_mutex);
775
Linus Walleij2744e8a2011-05-02 20:50:54 +0200776 return 0;
777}
778
779static int pinctrl_groups_show(struct seq_file *s, void *what)
780{
781 struct pinctrl_dev *pctldev = s->private;
782 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
783 unsigned selector = 0;
784
785 /* No grouping */
786 if (!ops)
787 return 0;
788
Stephen Warren57b676f2012-03-02 13:05:44 -0700789 mutex_lock(&pinctrl_mutex);
790
Linus Walleij2744e8a2011-05-02 20:50:54 +0200791 seq_puts(s, "registered pin groups:\n");
792 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600793 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200794 unsigned num_pins;
795 const char *gname = ops->get_group_name(pctldev, selector);
796 int ret;
797 int i;
798
799 ret = ops->get_group_pins(pctldev, selector,
800 &pins, &num_pins);
801 if (ret)
802 seq_printf(s, "%s [ERROR GETTING PINS]\n",
803 gname);
804 else {
805 seq_printf(s, "group: %s, pins = [ ", gname);
806 for (i = 0; i < num_pins; i++)
807 seq_printf(s, "%d ", pins[i]);
808 seq_puts(s, "]\n");
809 }
810 selector++;
811 }
812
Stephen Warren57b676f2012-03-02 13:05:44 -0700813 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200814
815 return 0;
816}
817
818static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
819{
820 struct pinctrl_dev *pctldev = s->private;
821 struct pinctrl_gpio_range *range = NULL;
822
823 seq_puts(s, "GPIO ranges handled:\n");
824
Stephen Warren57b676f2012-03-02 13:05:44 -0700825 mutex_lock(&pinctrl_mutex);
826
Linus Walleij2744e8a2011-05-02 20:50:54 +0200827 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200828 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100829 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
830 range->id, range->name,
831 range->base, (range->base + range->npins - 1),
832 range->pin_base,
833 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200834 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700835
836 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200837
838 return 0;
839}
840
841static int pinctrl_devices_show(struct seq_file *s, void *what)
842{
843 struct pinctrl_dev *pctldev;
844
Linus Walleijae6b4d82011-10-19 18:14:33 +0200845 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700846
847 mutex_lock(&pinctrl_mutex);
848
Linus Walleij2744e8a2011-05-02 20:50:54 +0200849 list_for_each_entry(pctldev, &pinctrldev_list, node) {
850 seq_printf(s, "%s ", pctldev->desc->name);
851 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200852 seq_puts(s, "yes ");
853 else
854 seq_puts(s, "no ");
855 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200856 seq_puts(s, "yes");
857 else
858 seq_puts(s, "no");
859 seq_puts(s, "\n");
860 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700861
862 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200863
864 return 0;
865}
866
Stephen Warren3eedb432012-02-23 17:04:40 -0700867static int pinctrl_maps_show(struct seq_file *s, void *what)
868{
869 struct pinctrl_maps *maps_node;
870 int i;
871 struct pinctrl_map const *map;
872
873 seq_puts(s, "Pinctrl maps:\n");
874
Stephen Warren57b676f2012-03-02 13:05:44 -0700875 mutex_lock(&pinctrl_mutex);
876
Stephen Warren3eedb432012-02-23 17:04:40 -0700877 for_each_maps(maps_node, i, map) {
878 seq_printf(s, "%s:\n", map->name);
879 seq_printf(s, " device: %s\n", map->dev_name);
880 seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
881 seq_printf(s, " function: %s\n", map->function);
882 seq_printf(s, " group: %s\n", map->group ? map->group :
883 "(default)");
884 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700885
886 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -0700887
888 return 0;
889}
890
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100891static int pinctrl_show(struct seq_file *s, void *what)
892{
893 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700894 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100895
896 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700897
898 mutex_lock(&pinctrl_mutex);
899
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100900 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren7ecdb162012-03-02 13:05:45 -0700901 seq_printf(s, "device: %s state: %s users: %u\n",
902 dev_name(p->dev), p->state, p->usecount);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100903
Stephen Warren7ecdb162012-03-02 13:05:45 -0700904 list_for_each_entry(setting, &p->settings, node) {
905 seq_printf(s, " ");
906 pinmux_dbg_show(s, setting);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100907 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100908 }
909
Stephen Warren57b676f2012-03-02 13:05:44 -0700910 mutex_unlock(&pinctrl_mutex);
911
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100912 return 0;
913}
914
Linus Walleij2744e8a2011-05-02 20:50:54 +0200915static int pinctrl_pins_open(struct inode *inode, struct file *file)
916{
917 return single_open(file, pinctrl_pins_show, inode->i_private);
918}
919
920static int pinctrl_groups_open(struct inode *inode, struct file *file)
921{
922 return single_open(file, pinctrl_groups_show, inode->i_private);
923}
924
925static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
926{
927 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
928}
929
930static int pinctrl_devices_open(struct inode *inode, struct file *file)
931{
932 return single_open(file, pinctrl_devices_show, NULL);
933}
934
Stephen Warren3eedb432012-02-23 17:04:40 -0700935static int pinctrl_maps_open(struct inode *inode, struct file *file)
936{
937 return single_open(file, pinctrl_maps_show, NULL);
938}
939
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100940static int pinctrl_open(struct inode *inode, struct file *file)
941{
942 return single_open(file, pinctrl_show, NULL);
943}
944
Linus Walleij2744e8a2011-05-02 20:50:54 +0200945static const struct file_operations pinctrl_pins_ops = {
946 .open = pinctrl_pins_open,
947 .read = seq_read,
948 .llseek = seq_lseek,
949 .release = single_release,
950};
951
952static const struct file_operations pinctrl_groups_ops = {
953 .open = pinctrl_groups_open,
954 .read = seq_read,
955 .llseek = seq_lseek,
956 .release = single_release,
957};
958
959static const struct file_operations pinctrl_gpioranges_ops = {
960 .open = pinctrl_gpioranges_open,
961 .read = seq_read,
962 .llseek = seq_lseek,
963 .release = single_release,
964};
965
966static const struct file_operations pinctrl_devices_ops = {
967 .open = pinctrl_devices_open,
968 .read = seq_read,
969 .llseek = seq_lseek,
970 .release = single_release,
971};
972
Stephen Warren3eedb432012-02-23 17:04:40 -0700973static const struct file_operations pinctrl_maps_ops = {
974 .open = pinctrl_maps_open,
975 .read = seq_read,
976 .llseek = seq_lseek,
977 .release = single_release,
978};
979
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100980static const struct file_operations pinctrl_ops = {
981 .open = pinctrl_open,
982 .read = seq_read,
983 .llseek = seq_lseek,
984 .release = single_release,
985};
986
Linus Walleij2744e8a2011-05-02 20:50:54 +0200987static struct dentry *debugfs_root;
988
989static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
990{
Tony Lindgren02157162012-01-20 08:17:22 -0800991 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200992
Stephen Warren51cd24e2011-12-09 16:59:05 -0700993 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +0200994 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -0800995 pctldev->device_root = device_root;
996
Linus Walleij2744e8a2011-05-02 20:50:54 +0200997 if (IS_ERR(device_root) || !device_root) {
998 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -0700999 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001000 return;
1001 }
1002 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1003 device_root, pctldev, &pinctrl_pins_ops);
1004 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1005 device_root, pctldev, &pinctrl_groups_ops);
1006 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1007 device_root, pctldev, &pinctrl_gpioranges_ops);
1008 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001009 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001010}
1011
Tony Lindgren02157162012-01-20 08:17:22 -08001012static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1013{
1014 debugfs_remove_recursive(pctldev->device_root);
1015}
1016
Linus Walleij2744e8a2011-05-02 20:50:54 +02001017static void pinctrl_init_debugfs(void)
1018{
1019 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1020 if (IS_ERR(debugfs_root) || !debugfs_root) {
1021 pr_warn("failed to create debugfs directory\n");
1022 debugfs_root = NULL;
1023 return;
1024 }
1025
1026 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1027 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001028 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1029 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001030 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1031 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001032}
1033
1034#else /* CONFIG_DEBUG_FS */
1035
1036static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1037{
1038}
1039
1040static void pinctrl_init_debugfs(void)
1041{
1042}
1043
Tony Lindgren02157162012-01-20 08:17:22 -08001044static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1045{
1046}
1047
Linus Walleij2744e8a2011-05-02 20:50:54 +02001048#endif
1049
1050/**
1051 * pinctrl_register() - register a pin controller device
1052 * @pctldesc: descriptor for this pin controller
1053 * @dev: parent device for this pin controller
1054 * @driver_data: private pin controller data for this pin controller
1055 */
1056struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1057 struct device *dev, void *driver_data)
1058{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001059 struct pinctrl_dev *pctldev;
1060 int ret;
1061
1062 if (pctldesc == NULL)
1063 return NULL;
1064 if (pctldesc->name == NULL)
1065 return NULL;
1066
Stephen Warren02f5b982012-02-22 14:26:00 -07001067 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001068 if (pctldev == NULL) {
1069 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001070 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001071 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001072
1073 /* Initialize pin control device struct */
1074 pctldev->owner = pctldesc->owner;
1075 pctldev->desc = pctldesc;
1076 pctldev->driver_data = driver_data;
1077 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001078 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001079 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001080
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001081 /* If we're implementing pinmuxing, check the ops for sanity */
1082 if (pctldesc->pmxops) {
1083 ret = pinmux_check_ops(pctldev);
1084 if (ret) {
1085 pr_err("%s pinmux ops lacks necessary functions\n",
1086 pctldesc->name);
1087 goto out_err;
1088 }
1089 }
1090
1091 /* If we're implementing pinconfig, check the ops for sanity */
1092 if (pctldesc->confops) {
1093 ret = pinconf_check_ops(pctldev);
1094 if (ret) {
1095 pr_err("%s pin config ops lacks necessary functions\n",
1096 pctldesc->name);
1097 goto out_err;
1098 }
1099 }
1100
Linus Walleij2744e8a2011-05-02 20:50:54 +02001101 /* Register all the pins */
1102 pr_debug("try to register %d pins on %s...\n",
1103 pctldesc->npins, pctldesc->name);
1104 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1105 if (ret) {
1106 pr_err("error during pin registration\n");
1107 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1108 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001109 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001110 }
1111
Stephen Warren57b676f2012-03-02 13:05:44 -07001112 mutex_lock(&pinctrl_mutex);
1113
Stephen Warren8b9c1392012-02-19 23:45:42 -07001114 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001115
1116 pctldev->p = pinctrl_get_locked(pctldev->dev, PINCTRL_STATE_DEFAULT);
Stephen Warren46919ae2012-03-01 18:48:32 -07001117 if (!IS_ERR(pctldev->p))
Stephen Warren57b676f2012-03-02 13:05:44 -07001118 pinctrl_enable_locked(pctldev->p);
1119
1120 mutex_unlock(&pinctrl_mutex);
1121
Stephen Warren2304b472012-02-22 14:26:01 -07001122 pinctrl_init_device_debugfs(pctldev);
1123
Linus Walleij2744e8a2011-05-02 20:50:54 +02001124 return pctldev;
1125
Stephen Warren51cd24e2011-12-09 16:59:05 -07001126out_err:
1127 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001128 return NULL;
1129}
1130EXPORT_SYMBOL_GPL(pinctrl_register);
1131
1132/**
1133 * pinctrl_unregister() - unregister pinmux
1134 * @pctldev: pin controller to unregister
1135 *
1136 * Called by pinmux drivers to unregister a pinmux.
1137 */
1138void pinctrl_unregister(struct pinctrl_dev *pctldev)
1139{
1140 if (pctldev == NULL)
1141 return;
1142
Tony Lindgren02157162012-01-20 08:17:22 -08001143 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001144
1145 mutex_lock(&pinctrl_mutex);
1146
Stephen Warren46919ae2012-03-01 18:48:32 -07001147 if (!IS_ERR(pctldev->p)) {
Stephen Warren57b676f2012-03-02 13:05:44 -07001148 pinctrl_disable_locked(pctldev->p);
1149 pinctrl_put_locked(pctldev->p);
Stephen Warren46919ae2012-03-01 18:48:32 -07001150 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001151
Linus Walleij2744e8a2011-05-02 20:50:54 +02001152 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001153 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001154 /* Destroy descriptor tree */
1155 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1156 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001157 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001158
1159 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001160}
1161EXPORT_SYMBOL_GPL(pinctrl_unregister);
1162
1163static int __init pinctrl_init(void)
1164{
1165 pr_info("initialized pinctrl subsystem\n");
1166 pinctrl_init_debugfs();
1167 return 0;
1168}
1169
1170/* init early since many drivers really need to initialized pinmux early */
1171core_initcall(pinctrl_init);