blob: 59f5a965bdc40ad47e566bc1969f236ba06d8689 [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>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060026#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
29#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060030#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020032#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020033
Linus Walleijbefe5bd2012-02-09 19:47:48 +010034/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070035 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
39 */
40struct pinctrl_maps {
41 struct list_head node;
42 struct pinctrl_map const *maps;
43 unsigned num_maps;
44};
45
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080046static bool pinctrl_dummy_state;
47
Stephen Warren57b676f2012-03-02 13:05:44 -070048/* Mutex taken by all entry points */
49DEFINE_MUTEX(pinctrl_mutex);
50
51/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060052LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020053
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055static LIST_HEAD(pinctrl_list);
56
Stephen Warren57b676f2012-03-02 13:05:44 -070057/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070058static LIST_HEAD(pinctrl_maps);
59
60#define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
Guennadi Liakhovetskibc664682012-05-23 00:20:17 +020064 _i_++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010065
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080066/**
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
68 *
69 * Usually this function is called by platforms without pinctrl driver support
70 * but run with some shared drivers using pinctrl APIs.
71 * After calling this function, the pinctrl core will return successfully
72 * with creating a dummy state for the driver to keep going smoothly.
73 */
74void pinctrl_provide_dummies(void)
75{
76 pinctrl_dummy_state = true;
77}
78
Linus Walleij2744e8a2011-05-02 20:50:54 +020079const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
80{
81 /* We're not allowed to register devices without name */
82 return pctldev->desc->name;
83}
84EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
85
86void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
87{
88 return pctldev->driver_data;
89}
90EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
91
92/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010093 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 *
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
98 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010099struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100{
101 struct pinctrl_dev *pctldev = NULL;
102 bool found = false;
103
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100104 if (!devname)
105 return NULL;
106
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100108 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 /* Matched on device name */
110 found = true;
111 break;
112 }
113 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114
115 return found ? pctldev : NULL;
116}
117
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200119 * pin_get_from_name() - look up a pin number from a name
120 * @pctldev: the pin control device to lookup the pin on
121 * @name: the name of the pin to look up
122 */
123int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
124{
Chanho Park706e8522012-01-03 16:47:50 +0900125 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200126
Chanho Park706e8522012-01-03 16:47:50 +0900127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200129 struct pin_desc *desc;
130
Chanho Park706e8522012-01-03 16:47:50 +0900131 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200132 desc = pin_desc_get(pctldev, pin);
133 /* Pin space may be sparse */
134 if (desc == NULL)
135 continue;
136 if (desc->name && !strcmp(name, desc->name))
137 return pin;
138 }
139
140 return -EINVAL;
141}
142
143/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800144 * pin_get_name_from_id() - look up a pin name from a pin id
145 * @pctldev: the pin control device to lookup the pin on
146 * @name: the name of the pin to look up
147 */
148const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
149{
150 const struct pin_desc *desc;
151
152 desc = pin_desc_get(pctldev, pin);
153 if (desc == NULL) {
154 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
155 pin);
156 return NULL;
157 }
158
159 return desc->name;
160}
161
162/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200163 * pin_is_valid() - check if pin exists on controller
164 * @pctldev: the pin control device to check the pin on
165 * @pin: pin to check, use the local pin controller index number
166 *
167 * This tells us whether a certain pin exist on a certain pin controller or
168 * not. Pin lists may be sparse, so some pins may not exist.
169 */
170bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
171{
172 struct pin_desc *pindesc;
173
174 if (pin < 0)
175 return false;
176
Stephen Warren57b676f2012-03-02 13:05:44 -0700177 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700179 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200180
Stephen Warren57b676f2012-03-02 13:05:44 -0700181 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182}
183EXPORT_SYMBOL_GPL(pin_is_valid);
184
185/* Deletes a range of pin descriptors */
186static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
187 const struct pinctrl_pin_desc *pins,
188 unsigned num_pins)
189{
190 int i;
191
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192 for (i = 0; i < num_pins; i++) {
193 struct pin_desc *pindesc;
194
195 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
196 pins[i].number);
197 if (pindesc != NULL) {
198 radix_tree_delete(&pctldev->pin_desc_tree,
199 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100200 if (pindesc->dynamic_name)
201 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202 }
203 kfree(pindesc);
204 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205}
206
207static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
208 unsigned number, const char *name)
209{
210 struct pin_desc *pindesc;
211
212 pindesc = pin_desc_get(pctldev, number);
213 if (pindesc != NULL) {
214 pr_err("pin %d already registered on %s\n", number,
215 pctldev->desc->name);
216 return -EINVAL;
217 }
218
219 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700220 if (pindesc == NULL) {
221 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700223 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200224
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225 /* Set owner */
226 pindesc->pctldev = pctldev;
227
Stephen Warren9af1e442011-10-19 16:19:27 -0600228 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100229 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100230 pindesc->name = name;
231 } else {
232 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530233 if (pindesc->name == NULL) {
234 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100235 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530236 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100237 pindesc->dynamic_name = true;
238 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100242 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 return 0;
244}
245
246static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
247 struct pinctrl_pin_desc const *pins,
248 unsigned num_descs)
249{
250 unsigned i;
251 int ret = 0;
252
253 for (i = 0; i < num_descs; i++) {
254 ret = pinctrl_register_one_pin(pctldev,
255 pins[i].number, pins[i].name);
256 if (ret)
257 return ret;
258 }
259
260 return 0;
261}
262
263/**
264 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
265 * @pctldev: pin controller device to check
266 * @gpio: gpio pin to check taken from the global GPIO pin space
267 *
268 * Tries to match a GPIO pin number to the ranges handled by a certain pin
269 * controller, return the range or NULL
270 */
271static struct pinctrl_gpio_range *
272pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
273{
274 struct pinctrl_gpio_range *range = NULL;
275
276 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
278 /* Check if we're in the valid range */
279 if (gpio >= range->base &&
280 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200281 return range;
282 }
283 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200284
285 return NULL;
286}
287
288/**
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
293 *
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800296 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
297 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200298 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700299static int pinctrl_get_device_gpio_range(unsigned gpio,
300 struct pinctrl_dev **outdev,
301 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302{
303 struct pinctrl_dev *pctldev = NULL;
304
305 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 list_for_each_entry(pctldev, &pinctrldev_list, node) {
307 struct pinctrl_gpio_range *range;
308
309 range = pinctrl_match_gpio_range(pctldev, gpio);
310 if (range != NULL) {
311 *outdev = pctldev;
312 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313 return 0;
314 }
315 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800317 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200318}
319
320/**
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
324 *
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
327 */
328void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
330{
Stephen Warren57b676f2012-03-02 13:05:44 -0700331 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700332 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700333 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700335EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800337void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
338 struct pinctrl_gpio_range *ranges,
339 unsigned nranges)
340{
341 int i;
342
343 for (i = 0; i < nranges; i++)
344 pinctrl_add_gpio_range(pctldev, &ranges[i]);
345}
346EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
347
Linus Walleij192c3692012-11-20 14:03:37 +0100348struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530349 struct pinctrl_gpio_range *range)
350{
351 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
352
Linus Walleijdfa97512012-11-20 14:54:18 +0100353 /*
354 * If we can't find this device, let's assume that is because
355 * it has not probed yet, so the driver trying to register this
356 * range need to defer probing.
357 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530358 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100359 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530360
361 pinctrl_add_gpio_range(pctldev, range);
362 return pctldev;
363}
Linus Walleij192c3692012-11-20 14:03:37 +0100364EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530365
Linus Walleij2744e8a2011-05-02 20:50:54 +0200366/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100367 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
368 * @pctldev: the pin controller device to look in
369 * @pin: a controller-local number to find the range for
370 */
371struct pinctrl_gpio_range *
372pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
373 unsigned int pin)
374{
375 struct pinctrl_gpio_range *range = NULL;
376
377 /* Loop over the ranges */
378 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
379 /* Check if we're in the valid range */
380 if (pin >= range->pin_base &&
381 pin < range->pin_base + range->npins) {
382 return range;
383 }
384 }
385
386 return NULL;
387}
388EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
389
390/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530391 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
392 * @pctldev: pin controller device to remove the range from
393 * @range: the GPIO range to remove
394 */
395void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
396 struct pinctrl_gpio_range *range)
397{
398 mutex_lock(&pinctrl_mutex);
399 list_del(&range->node);
400 mutex_unlock(&pinctrl_mutex);
401}
402EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
403
404/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200405 * pinctrl_get_group_selector() - returns the group selector for a group
406 * @pctldev: the pin controller handling the group
407 * @pin_group: the pin group to look up
408 */
409int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
410 const char *pin_group)
411{
412 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530413 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200414 unsigned group_selector = 0;
415
Viresh Kumard1e90e92012-03-30 11:25:40 +0530416 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200417 const char *gname = pctlops->get_group_name(pctldev,
418 group_selector);
419 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700420 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200421 "found group selector %u for %s\n",
422 group_selector,
423 pin_group);
424 return group_selector;
425 }
426
427 group_selector++;
428 }
429
Stephen Warren51cd24e2011-12-09 16:59:05 -0700430 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200431 pin_group);
432
433 return -EINVAL;
434}
435
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100436/**
437 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
438 * @gpio: the GPIO pin number from the GPIO subsystem number space
439 *
440 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
441 * as part of their gpio_request() semantics, platforms and individual drivers
442 * shall *NOT* request GPIO pins to be muxed in.
443 */
444int pinctrl_request_gpio(unsigned gpio)
445{
446 struct pinctrl_dev *pctldev;
447 struct pinctrl_gpio_range *range;
448 int ret;
449 int pin;
450
Stephen Warren57b676f2012-03-02 13:05:44 -0700451 mutex_lock(&pinctrl_mutex);
452
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100453 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700454 if (ret) {
455 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800456 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700457 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100458
459 /* Convert to the pin controllers number space */
460 pin = gpio - range->base + range->pin_base;
461
Stephen Warren57b676f2012-03-02 13:05:44 -0700462 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
463
464 mutex_unlock(&pinctrl_mutex);
465 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100466}
467EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
468
469/**
470 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
471 * @gpio: the GPIO pin number from the GPIO subsystem number space
472 *
473 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
474 * as part of their gpio_free() semantics, platforms and individual drivers
475 * shall *NOT* request GPIO pins to be muxed out.
476 */
477void pinctrl_free_gpio(unsigned gpio)
478{
479 struct pinctrl_dev *pctldev;
480 struct pinctrl_gpio_range *range;
481 int ret;
482 int pin;
483
Stephen Warren57b676f2012-03-02 13:05:44 -0700484 mutex_lock(&pinctrl_mutex);
485
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100486 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700487 if (ret) {
488 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100489 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700490 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100491
492 /* Convert to the pin controllers number space */
493 pin = gpio - range->base + range->pin_base;
494
Stephen Warren57b676f2012-03-02 13:05:44 -0700495 pinmux_free_gpio(pctldev, pin, range);
496
497 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100498}
499EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
500
501static int pinctrl_gpio_direction(unsigned gpio, bool input)
502{
503 struct pinctrl_dev *pctldev;
504 struct pinctrl_gpio_range *range;
505 int ret;
506 int pin;
507
508 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
509 if (ret)
510 return ret;
511
512 /* Convert to the pin controllers number space */
513 pin = gpio - range->base + range->pin_base;
514
515 return pinmux_gpio_direction(pctldev, range, pin, input);
516}
517
518/**
519 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
520 * @gpio: the GPIO pin number from the GPIO subsystem number space
521 *
522 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
523 * as part of their gpio_direction_input() semantics, platforms and individual
524 * drivers shall *NOT* touch pin control GPIO calls.
525 */
526int pinctrl_gpio_direction_input(unsigned gpio)
527{
Stephen Warren57b676f2012-03-02 13:05:44 -0700528 int ret;
529 mutex_lock(&pinctrl_mutex);
530 ret = pinctrl_gpio_direction(gpio, true);
531 mutex_unlock(&pinctrl_mutex);
532 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100533}
534EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
535
536/**
537 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
538 * @gpio: the GPIO pin number from the GPIO subsystem number space
539 *
540 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
541 * as part of their gpio_direction_output() semantics, platforms and individual
542 * drivers shall *NOT* touch pin control GPIO calls.
543 */
544int pinctrl_gpio_direction_output(unsigned gpio)
545{
Stephen Warren57b676f2012-03-02 13:05:44 -0700546 int ret;
547 mutex_lock(&pinctrl_mutex);
548 ret = pinctrl_gpio_direction(gpio, false);
549 mutex_unlock(&pinctrl_mutex);
550 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100551}
552EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
553
Stephen Warren6e5e9592012-03-02 13:05:47 -0700554static struct pinctrl_state *find_state(struct pinctrl *p,
555 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100556{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700557 struct pinctrl_state *state;
558
559 list_for_each_entry(state, &p->states, node)
560 if (!strcmp(state->name, name))
561 return state;
562
563 return NULL;
564}
565
566static struct pinctrl_state *create_state(struct pinctrl *p,
567 const char *name)
568{
569 struct pinctrl_state *state;
570
571 state = kzalloc(sizeof(*state), GFP_KERNEL);
572 if (state == NULL) {
573 dev_err(p->dev,
574 "failed to alloc struct pinctrl_state\n");
575 return ERR_PTR(-ENOMEM);
576 }
577
578 state->name = name;
579 INIT_LIST_HEAD(&state->settings);
580
581 list_add_tail(&state->node, &p->states);
582
583 return state;
584}
585
586static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
587{
588 struct pinctrl_state *state;
589 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700590 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700591
592 state = find_state(p, map->name);
593 if (!state)
594 state = create_state(p, map->name);
595 if (IS_ERR(state))
596 return PTR_ERR(state);
597
Stephen Warren1e2082b2012-03-02 13:05:48 -0700598 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
599 return 0;
600
Stephen Warren6e5e9592012-03-02 13:05:47 -0700601 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
602 if (setting == NULL) {
603 dev_err(p->dev,
604 "failed to alloc struct pinctrl_setting\n");
605 return -ENOMEM;
606 }
607
Stephen Warren1e2082b2012-03-02 13:05:48 -0700608 setting->type = map->type;
609
Stephen Warren6e5e9592012-03-02 13:05:47 -0700610 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
611 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200612 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700613 map->ctrl_dev_name);
614 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200615 /*
616 * OK let us guess that the driver is not there yet, and
617 * let's defer obtaining this pinctrl handle to later...
618 */
619 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700620 }
621
Linus Walleij1a789582012-10-17 20:51:54 +0200622 setting->dev_name = map->dev_name;
623
Stephen Warren1e2082b2012-03-02 13:05:48 -0700624 switch (map->type) {
625 case PIN_MAP_TYPE_MUX_GROUP:
626 ret = pinmux_map_to_setting(map, setting);
627 break;
628 case PIN_MAP_TYPE_CONFIGS_PIN:
629 case PIN_MAP_TYPE_CONFIGS_GROUP:
630 ret = pinconf_map_to_setting(map, setting);
631 break;
632 default:
633 ret = -EINVAL;
634 break;
635 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700636 if (ret < 0) {
637 kfree(setting);
638 return ret;
639 }
640
641 list_add_tail(&setting->node, &state->settings);
642
643 return 0;
644}
645
646static struct pinctrl *find_pinctrl(struct device *dev)
647{
648 struct pinctrl *p;
649
Stephen Warren1e2082b2012-03-02 13:05:48 -0700650 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700651 if (p->dev == dev)
652 return p;
653
654 return NULL;
655}
656
657static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
658
659static struct pinctrl *create_pinctrl(struct device *dev)
660{
661 struct pinctrl *p;
662 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700663 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100664 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700665 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700666 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100667
668 /*
669 * create the state cookie holder struct pinctrl for each
670 * mapping, this is what consumers will get when requesting
671 * a pin control handle with pinctrl_get()
672 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700673 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700674 if (p == NULL) {
675 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100676 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700677 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700678 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700679 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600680 INIT_LIST_HEAD(&p->dt_maps);
681
682 ret = pinctrl_dt_to_map(p);
683 if (ret < 0) {
684 kfree(p);
685 return ERR_PTR(ret);
686 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700687
688 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100689
690 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700691 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700692 /* Map must be for this device */
693 if (strcmp(map->dev_name, devname))
694 continue;
695
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696 ret = add_setting(p, map);
697 if (ret < 0) {
698 pinctrl_put_locked(p, false);
699 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100700 }
701 }
702
Linus Walleijb0666ba2012-12-11 13:12:35 +0100703 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700704 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100705
706 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700707}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700708
Stephen Warren6e5e9592012-03-02 13:05:47 -0700709static struct pinctrl *pinctrl_get_locked(struct device *dev)
710{
711 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700712
Stephen Warren6e5e9592012-03-02 13:05:47 -0700713 if (WARN_ON(!dev))
714 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700715
Stephen Warren6e5e9592012-03-02 13:05:47 -0700716 p = find_pinctrl(dev);
717 if (p != NULL)
718 return ERR_PTR(-EBUSY);
719
Richard Genoudd599bfb2012-08-10 16:53:49 +0200720 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100721}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700722
723/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700724 * pinctrl_get() - retrieves the pinctrl handle for a device
725 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700726 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700727struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700728{
729 struct pinctrl *p;
730
Stephen Warren57b676f2012-03-02 13:05:44 -0700731 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700733 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700734
735 return p;
736}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100737EXPORT_SYMBOL_GPL(pinctrl_get);
738
Stephen Warren6e5e9592012-03-02 13:05:47 -0700739static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700740{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700741 struct pinctrl_state *state, *n1;
742 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700743
Stephen Warren6e5e9592012-03-02 13:05:47 -0700744 list_for_each_entry_safe(state, n1, &p->states, node) {
745 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700746 switch (setting->type) {
747 case PIN_MAP_TYPE_MUX_GROUP:
748 if (state == p->state)
749 pinmux_disable_setting(setting);
750 pinmux_free_setting(setting);
751 break;
752 case PIN_MAP_TYPE_CONFIGS_PIN:
753 case PIN_MAP_TYPE_CONFIGS_GROUP:
754 pinconf_free_setting(setting);
755 break;
756 default:
757 break;
758 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700759 list_del(&setting->node);
760 kfree(setting);
761 }
762 list_del(&state->node);
763 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700764 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700765
Stephen Warren57291ce2012-03-23 10:29:46 -0600766 pinctrl_dt_free_maps(p);
767
Stephen Warren6e5e9592012-03-02 13:05:47 -0700768 if (inlist)
769 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700770 kfree(p);
771}
772
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100773/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700774 * pinctrl_put() - release a previously claimed pinctrl handle
775 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100776 */
777void pinctrl_put(struct pinctrl *p)
778{
Stephen Warren57b676f2012-03-02 13:05:44 -0700779 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700780 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700781 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100782}
783EXPORT_SYMBOL_GPL(pinctrl_put);
784
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
786 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700787{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700788 struct pinctrl_state *state;
789
790 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800791 if (!state) {
792 if (pinctrl_dummy_state) {
793 /* create dummy state */
794 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
795 name);
796 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200797 } else
798 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800799 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700800
801 return state;
802}
803
804/**
805 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
806 * @p: the pinctrl handle to retrieve the state from
807 * @name: the state name to retrieve
808 */
809struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
810{
811 struct pinctrl_state *s;
812
813 mutex_lock(&pinctrl_mutex);
814 s = pinctrl_lookup_state_locked(p, name);
815 mutex_unlock(&pinctrl_mutex);
816
817 return s;
818}
819EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
820
821static int pinctrl_select_state_locked(struct pinctrl *p,
822 struct pinctrl_state *state)
823{
824 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700825 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700826
Stephen Warren6e5e9592012-03-02 13:05:47 -0700827 if (p->state == state)
828 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700829
Stephen Warren6e5e9592012-03-02 13:05:47 -0700830 if (p->state) {
831 /*
832 * The set of groups with a mux configuration in the old state
833 * may not be identical to the set of groups with a mux setting
834 * in the new state. While this might be unusual, it's entirely
835 * possible for the "user"-supplied mapping table to be written
836 * that way. For each group that was configured in the old state
837 * but not in the new state, this code puts that group into a
838 * safe/disabled state.
839 */
840 list_for_each_entry(setting, &p->state->settings, node) {
841 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700842 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
843 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700844 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700845 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
846 continue;
847 if (setting2->data.mux.group ==
848 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700849 found = true;
850 break;
851 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700852 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700853 if (!found)
854 pinmux_disable_setting(setting);
855 }
856 }
857
858 p->state = state;
859
860 /* Apply all the settings for the new state */
861 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700862 switch (setting->type) {
863 case PIN_MAP_TYPE_MUX_GROUP:
864 ret = pinmux_enable_setting(setting);
865 break;
866 case PIN_MAP_TYPE_CONFIGS_PIN:
867 case PIN_MAP_TYPE_CONFIGS_GROUP:
868 ret = pinconf_apply_setting(setting);
869 break;
870 default:
871 ret = -EINVAL;
872 break;
873 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700874 if (ret < 0) {
875 /* FIXME: Difficult to return to prev state */
876 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700877 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700878 }
879
Stephen Warren7ecdb162012-03-02 13:05:45 -0700880 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700881}
882
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100883/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700884 * pinctrl_select() - select/activate/program a pinctrl state to HW
885 * @p: the pinctrl handle for the device that requests configuratio
886 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100887 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700888int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100889{
Stephen Warren57b676f2012-03-02 13:05:44 -0700890 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700891
Stephen Warren57b676f2012-03-02 13:05:44 -0700892 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700893 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700894 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700895
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100896 return ret;
897}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700898EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100899
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600900static void devm_pinctrl_release(struct device *dev, void *res)
901{
902 pinctrl_put(*(struct pinctrl **)res);
903}
904
905/**
906 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
907 * @dev: the device to obtain the handle for
908 *
909 * If there is a need to explicitly destroy the returned struct pinctrl,
910 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
911 */
912struct pinctrl *devm_pinctrl_get(struct device *dev)
913{
914 struct pinctrl **ptr, *p;
915
916 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
917 if (!ptr)
918 return ERR_PTR(-ENOMEM);
919
920 p = pinctrl_get(dev);
921 if (!IS_ERR(p)) {
922 *ptr = p;
923 devres_add(dev, ptr);
924 } else {
925 devres_free(ptr);
926 }
927
928 return p;
929}
930EXPORT_SYMBOL_GPL(devm_pinctrl_get);
931
932static int devm_pinctrl_match(struct device *dev, void *res, void *data)
933{
934 struct pinctrl **p = res;
935
936 return *p == data;
937}
938
939/**
940 * devm_pinctrl_put() - Resource managed pinctrl_put()
941 * @p: the pinctrl handle to release
942 *
943 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
944 * this function will not need to be called and the resource management
945 * code will ensure that the resource is freed.
946 */
947void devm_pinctrl_put(struct pinctrl *p)
948{
949 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
950 devm_pinctrl_match, p));
951 pinctrl_put(p);
952}
953EXPORT_SYMBOL_GPL(devm_pinctrl_put);
954
Stephen Warren57291ce2012-03-23 10:29:46 -0600955int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
956 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100957{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700958 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700959 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100960
961 pr_debug("add %d pinmux maps\n", num_maps);
962
963 /* First sanity check the new mapping */
964 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700965 if (!maps[i].dev_name) {
966 pr_err("failed to register map %s (%d): no device given\n",
967 maps[i].name, i);
968 return -EINVAL;
969 }
970
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100971 if (!maps[i].name) {
972 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700973 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100974 return -EINVAL;
975 }
976
Stephen Warren1e2082b2012-03-02 13:05:48 -0700977 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
978 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100979 pr_err("failed to register map %s (%d): no pin control device given\n",
980 maps[i].name, i);
981 return -EINVAL;
982 }
983
Stephen Warren1e2082b2012-03-02 13:05:48 -0700984 switch (maps[i].type) {
985 case PIN_MAP_TYPE_DUMMY_STATE:
986 break;
987 case PIN_MAP_TYPE_MUX_GROUP:
988 ret = pinmux_validate_map(&maps[i], i);
989 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600990 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700991 break;
992 case PIN_MAP_TYPE_CONFIGS_PIN:
993 case PIN_MAP_TYPE_CONFIGS_GROUP:
994 ret = pinconf_validate_map(&maps[i], i);
995 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600996 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700997 break;
998 default:
999 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001000 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001001 return -EINVAL;
1002 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001003 }
1004
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001005 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1006 if (!maps_node) {
1007 pr_err("failed to alloc struct pinctrl_maps\n");
1008 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001009 }
1010
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001011 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001012 if (dup) {
1013 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1014 GFP_KERNEL);
1015 if (!maps_node->maps) {
1016 pr_err("failed to duplicate mapping table\n");
1017 kfree(maps_node);
1018 return -ENOMEM;
1019 }
1020 } else {
1021 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001022 }
1023
Stephen Warren57291ce2012-03-23 10:29:46 -06001024 if (!locked)
1025 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001026 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001027 if (!locked)
1028 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001029
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001030 return 0;
1031}
1032
Stephen Warren57291ce2012-03-23 10:29:46 -06001033/**
1034 * pinctrl_register_mappings() - register a set of pin controller mappings
1035 * @maps: the pincontrol mappings table to register. This should probably be
1036 * marked with __initdata so it can be discarded after boot. This
1037 * function will perform a shallow copy for the mapping entries.
1038 * @num_maps: the number of maps in the mapping table
1039 */
1040int pinctrl_register_mappings(struct pinctrl_map const *maps,
1041 unsigned num_maps)
1042{
1043 return pinctrl_register_map(maps, num_maps, true, false);
1044}
1045
1046void pinctrl_unregister_map(struct pinctrl_map const *map)
1047{
1048 struct pinctrl_maps *maps_node;
1049
1050 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1051 if (maps_node->maps == map) {
1052 list_del(&maps_node->node);
1053 return;
1054 }
1055 }
1056}
1057
Linus Walleij2744e8a2011-05-02 20:50:54 +02001058#ifdef CONFIG_DEBUG_FS
1059
1060static int pinctrl_pins_show(struct seq_file *s, void *what)
1061{
1062 struct pinctrl_dev *pctldev = s->private;
1063 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001064 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001065
1066 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067
Stephen Warren57b676f2012-03-02 13:05:44 -07001068 mutex_lock(&pinctrl_mutex);
1069
Chanho Park706e8522012-01-03 16:47:50 +09001070 /* The pin number can be retrived from the pin controller descriptor */
1071 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001072 struct pin_desc *desc;
1073
Chanho Park706e8522012-01-03 16:47:50 +09001074 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001075 desc = pin_desc_get(pctldev, pin);
1076 /* Pin space may be sparse */
1077 if (desc == NULL)
1078 continue;
1079
1080 seq_printf(s, "pin %d (%s) ", pin,
1081 desc->name ? desc->name : "unnamed");
1082
1083 /* Driver-specific info per pin */
1084 if (ops->pin_dbg_show)
1085 ops->pin_dbg_show(pctldev, s, pin);
1086
1087 seq_puts(s, "\n");
1088 }
1089
Stephen Warren57b676f2012-03-02 13:05:44 -07001090 mutex_unlock(&pinctrl_mutex);
1091
Linus Walleij2744e8a2011-05-02 20:50:54 +02001092 return 0;
1093}
1094
1095static int pinctrl_groups_show(struct seq_file *s, void *what)
1096{
1097 struct pinctrl_dev *pctldev = s->private;
1098 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301099 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001100
Viresh Kumard1e90e92012-03-30 11:25:40 +05301101 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001102 mutex_lock(&pinctrl_mutex);
1103
Linus Walleij2744e8a2011-05-02 20:50:54 +02001104 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301105 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001106 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001107 unsigned num_pins;
1108 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001109 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001110 int ret;
1111 int i;
1112
1113 ret = ops->get_group_pins(pctldev, selector,
1114 &pins, &num_pins);
1115 if (ret)
1116 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1117 gname);
1118 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001119 seq_printf(s, "group: %s\n", gname);
1120 for (i = 0; i < num_pins; i++) {
1121 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001122 if (WARN_ON(!pname)) {
1123 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001124 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001125 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001126 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1127 }
1128 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001129 }
1130 selector++;
1131 }
1132
Stephen Warren57b676f2012-03-02 13:05:44 -07001133 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001134
1135 return 0;
1136}
1137
1138static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1139{
1140 struct pinctrl_dev *pctldev = s->private;
1141 struct pinctrl_gpio_range *range = NULL;
1142
1143 seq_puts(s, "GPIO ranges handled:\n");
1144
Stephen Warren57b676f2012-03-02 13:05:44 -07001145 mutex_lock(&pinctrl_mutex);
1146
Linus Walleij2744e8a2011-05-02 20:50:54 +02001147 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001148 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001149 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1150 range->id, range->name,
1151 range->base, (range->base + range->npins - 1),
1152 range->pin_base,
1153 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001154 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001155
1156 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001157
1158 return 0;
1159}
1160
1161static int pinctrl_devices_show(struct seq_file *s, void *what)
1162{
1163 struct pinctrl_dev *pctldev;
1164
Linus Walleijae6b4d82011-10-19 18:14:33 +02001165 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001166
1167 mutex_lock(&pinctrl_mutex);
1168
Linus Walleij2744e8a2011-05-02 20:50:54 +02001169 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1170 seq_printf(s, "%s ", pctldev->desc->name);
1171 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001172 seq_puts(s, "yes ");
1173 else
1174 seq_puts(s, "no ");
1175 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001176 seq_puts(s, "yes");
1177 else
1178 seq_puts(s, "no");
1179 seq_puts(s, "\n");
1180 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001181
1182 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001183
1184 return 0;
1185}
1186
Stephen Warren1e2082b2012-03-02 13:05:48 -07001187static inline const char *map_type(enum pinctrl_map_type type)
1188{
1189 static const char * const names[] = {
1190 "INVALID",
1191 "DUMMY_STATE",
1192 "MUX_GROUP",
1193 "CONFIGS_PIN",
1194 "CONFIGS_GROUP",
1195 };
1196
1197 if (type >= ARRAY_SIZE(names))
1198 return "UNKNOWN";
1199
1200 return names[type];
1201}
1202
Stephen Warren3eedb432012-02-23 17:04:40 -07001203static int pinctrl_maps_show(struct seq_file *s, void *what)
1204{
1205 struct pinctrl_maps *maps_node;
1206 int i;
1207 struct pinctrl_map const *map;
1208
1209 seq_puts(s, "Pinctrl maps:\n");
1210
Stephen Warren57b676f2012-03-02 13:05:44 -07001211 mutex_lock(&pinctrl_mutex);
1212
Stephen Warren3eedb432012-02-23 17:04:40 -07001213 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001214 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1215 map->dev_name, map->name, map_type(map->type),
1216 map->type);
1217
1218 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1219 seq_printf(s, "controlling device %s\n",
1220 map->ctrl_dev_name);
1221
1222 switch (map->type) {
1223 case PIN_MAP_TYPE_MUX_GROUP:
1224 pinmux_show_map(s, map);
1225 break;
1226 case PIN_MAP_TYPE_CONFIGS_PIN:
1227 case PIN_MAP_TYPE_CONFIGS_GROUP:
1228 pinconf_show_map(s, map);
1229 break;
1230 default:
1231 break;
1232 }
1233
1234 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001235 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001236
1237 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001238
1239 return 0;
1240}
1241
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001242static int pinctrl_show(struct seq_file *s, void *what)
1243{
1244 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001245 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001246 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001247
1248 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001249
1250 mutex_lock(&pinctrl_mutex);
1251
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001252 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001253 seq_printf(s, "device: %s current state: %s\n",
1254 dev_name(p->dev),
1255 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001256
Stephen Warren6e5e9592012-03-02 13:05:47 -07001257 list_for_each_entry(state, &p->states, node) {
1258 seq_printf(s, " state: %s\n", state->name);
1259
1260 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001261 struct pinctrl_dev *pctldev = setting->pctldev;
1262
1263 seq_printf(s, " type: %s controller %s ",
1264 map_type(setting->type),
1265 pinctrl_dev_get_name(pctldev));
1266
1267 switch (setting->type) {
1268 case PIN_MAP_TYPE_MUX_GROUP:
1269 pinmux_show_setting(s, setting);
1270 break;
1271 case PIN_MAP_TYPE_CONFIGS_PIN:
1272 case PIN_MAP_TYPE_CONFIGS_GROUP:
1273 pinconf_show_setting(s, setting);
1274 break;
1275 default:
1276 break;
1277 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001278 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001279 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001280 }
1281
Stephen Warren57b676f2012-03-02 13:05:44 -07001282 mutex_unlock(&pinctrl_mutex);
1283
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001284 return 0;
1285}
1286
Linus Walleij2744e8a2011-05-02 20:50:54 +02001287static int pinctrl_pins_open(struct inode *inode, struct file *file)
1288{
1289 return single_open(file, pinctrl_pins_show, inode->i_private);
1290}
1291
1292static int pinctrl_groups_open(struct inode *inode, struct file *file)
1293{
1294 return single_open(file, pinctrl_groups_show, inode->i_private);
1295}
1296
1297static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1298{
1299 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1300}
1301
1302static int pinctrl_devices_open(struct inode *inode, struct file *file)
1303{
1304 return single_open(file, pinctrl_devices_show, NULL);
1305}
1306
Stephen Warren3eedb432012-02-23 17:04:40 -07001307static int pinctrl_maps_open(struct inode *inode, struct file *file)
1308{
1309 return single_open(file, pinctrl_maps_show, NULL);
1310}
1311
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001312static int pinctrl_open(struct inode *inode, struct file *file)
1313{
1314 return single_open(file, pinctrl_show, NULL);
1315}
1316
Linus Walleij2744e8a2011-05-02 20:50:54 +02001317static const struct file_operations pinctrl_pins_ops = {
1318 .open = pinctrl_pins_open,
1319 .read = seq_read,
1320 .llseek = seq_lseek,
1321 .release = single_release,
1322};
1323
1324static const struct file_operations pinctrl_groups_ops = {
1325 .open = pinctrl_groups_open,
1326 .read = seq_read,
1327 .llseek = seq_lseek,
1328 .release = single_release,
1329};
1330
1331static const struct file_operations pinctrl_gpioranges_ops = {
1332 .open = pinctrl_gpioranges_open,
1333 .read = seq_read,
1334 .llseek = seq_lseek,
1335 .release = single_release,
1336};
1337
1338static const struct file_operations pinctrl_devices_ops = {
1339 .open = pinctrl_devices_open,
1340 .read = seq_read,
1341 .llseek = seq_lseek,
1342 .release = single_release,
1343};
1344
Stephen Warren3eedb432012-02-23 17:04:40 -07001345static const struct file_operations pinctrl_maps_ops = {
1346 .open = pinctrl_maps_open,
1347 .read = seq_read,
1348 .llseek = seq_lseek,
1349 .release = single_release,
1350};
1351
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001352static const struct file_operations pinctrl_ops = {
1353 .open = pinctrl_open,
1354 .read = seq_read,
1355 .llseek = seq_lseek,
1356 .release = single_release,
1357};
1358
Linus Walleij2744e8a2011-05-02 20:50:54 +02001359static struct dentry *debugfs_root;
1360
1361static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1362{
Tony Lindgren02157162012-01-20 08:17:22 -08001363 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001364
Stephen Warren51cd24e2011-12-09 16:59:05 -07001365 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001366 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001367 pctldev->device_root = device_root;
1368
Linus Walleij2744e8a2011-05-02 20:50:54 +02001369 if (IS_ERR(device_root) || !device_root) {
1370 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001371 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001372 return;
1373 }
1374 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1375 device_root, pctldev, &pinctrl_pins_ops);
1376 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1377 device_root, pctldev, &pinctrl_groups_ops);
1378 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1379 device_root, pctldev, &pinctrl_gpioranges_ops);
1380 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001381 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001382}
1383
Tony Lindgren02157162012-01-20 08:17:22 -08001384static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1385{
1386 debugfs_remove_recursive(pctldev->device_root);
1387}
1388
Linus Walleij2744e8a2011-05-02 20:50:54 +02001389static void pinctrl_init_debugfs(void)
1390{
1391 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1392 if (IS_ERR(debugfs_root) || !debugfs_root) {
1393 pr_warn("failed to create debugfs directory\n");
1394 debugfs_root = NULL;
1395 return;
1396 }
1397
1398 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1399 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001400 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1401 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001402 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1403 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001404}
1405
1406#else /* CONFIG_DEBUG_FS */
1407
1408static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1409{
1410}
1411
1412static void pinctrl_init_debugfs(void)
1413{
1414}
1415
Tony Lindgren02157162012-01-20 08:17:22 -08001416static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1417{
1418}
1419
Linus Walleij2744e8a2011-05-02 20:50:54 +02001420#endif
1421
Stephen Warrend26bc492012-03-16 14:54:25 -06001422static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1423{
1424 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1425
1426 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301427 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001428 !ops->get_group_name ||
1429 !ops->get_group_pins)
1430 return -EINVAL;
1431
Stephen Warren57291ce2012-03-23 10:29:46 -06001432 if (ops->dt_node_to_map && !ops->dt_free_map)
1433 return -EINVAL;
1434
Stephen Warrend26bc492012-03-16 14:54:25 -06001435 return 0;
1436}
1437
Linus Walleij2744e8a2011-05-02 20:50:54 +02001438/**
1439 * pinctrl_register() - register a pin controller device
1440 * @pctldesc: descriptor for this pin controller
1441 * @dev: parent device for this pin controller
1442 * @driver_data: private pin controller data for this pin controller
1443 */
1444struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1445 struct device *dev, void *driver_data)
1446{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001447 struct pinctrl_dev *pctldev;
1448 int ret;
1449
Devendra Nagada9aecb2012-06-16 23:43:16 +05301450 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001451 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301452 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001453 return NULL;
1454
Stephen Warren02f5b982012-02-22 14:26:00 -07001455 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001456 if (pctldev == NULL) {
1457 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001458 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001459 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001460
1461 /* Initialize pin control device struct */
1462 pctldev->owner = pctldesc->owner;
1463 pctldev->desc = pctldesc;
1464 pctldev->driver_data = driver_data;
1465 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001466 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001467 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001468
Stephen Warrend26bc492012-03-16 14:54:25 -06001469 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301470 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001471 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001472 goto out_err;
1473 }
1474
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001475 /* If we're implementing pinmuxing, check the ops for sanity */
1476 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301477 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001478 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001479 }
1480
1481 /* If we're implementing pinconfig, check the ops for sanity */
1482 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301483 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001484 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001485 }
1486
Linus Walleij2744e8a2011-05-02 20:50:54 +02001487 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001488 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001489 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1490 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001491 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001492 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1493 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001494 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001495 }
1496
Stephen Warren57b676f2012-03-02 13:05:44 -07001497 mutex_lock(&pinctrl_mutex);
1498
Stephen Warren8b9c1392012-02-19 23:45:42 -07001499 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001500
Stephen Warren6e5e9592012-03-02 13:05:47 -07001501 pctldev->p = pinctrl_get_locked(pctldev->dev);
1502 if (!IS_ERR(pctldev->p)) {
1503 struct pinctrl_state *s =
1504 pinctrl_lookup_state_locked(pctldev->p,
1505 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001506 if (IS_ERR(s)) {
1507 dev_dbg(dev, "failed to lookup the default state\n");
1508 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301509 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001510 dev_err(dev,
1511 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001512 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001513 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001514
1515 mutex_unlock(&pinctrl_mutex);
1516
Stephen Warren2304b472012-02-22 14:26:01 -07001517 pinctrl_init_device_debugfs(pctldev);
1518
Linus Walleij2744e8a2011-05-02 20:50:54 +02001519 return pctldev;
1520
Stephen Warren51cd24e2011-12-09 16:59:05 -07001521out_err:
1522 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001523 return NULL;
1524}
1525EXPORT_SYMBOL_GPL(pinctrl_register);
1526
1527/**
1528 * pinctrl_unregister() - unregister pinmux
1529 * @pctldev: pin controller to unregister
1530 *
1531 * Called by pinmux drivers to unregister a pinmux.
1532 */
1533void pinctrl_unregister(struct pinctrl_dev *pctldev)
1534{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001535 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001536 if (pctldev == NULL)
1537 return;
1538
Tony Lindgren02157162012-01-20 08:17:22 -08001539 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001540
1541 mutex_lock(&pinctrl_mutex);
1542
Stephen Warren6e5e9592012-03-02 13:05:47 -07001543 if (!IS_ERR(pctldev->p))
1544 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001545
Linus Walleij2744e8a2011-05-02 20:50:54 +02001546 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001547 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001548 /* Destroy descriptor tree */
1549 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1550 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001551 /* remove gpio ranges map */
1552 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1553 list_del(&range->node);
1554
Stephen Warren51cd24e2011-12-09 16:59:05 -07001555 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001556
1557 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001558}
1559EXPORT_SYMBOL_GPL(pinctrl_unregister);
1560
1561static int __init pinctrl_init(void)
1562{
1563 pr_info("initialized pinctrl subsystem\n");
1564 pinctrl_init_debugfs();
1565 return 0;
1566}
1567
1568/* init early since many drivers really need to initialized pinmux early */
1569core_initcall(pinctrl_init);