blob: b0a8e9a3839341ec672c93b587eb0949fe8799ee [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>
Linus Walleijab780292013-01-22 10:56:14 -070017#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100018#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020019#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020022#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020024#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060027#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
30#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060031#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020032#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020033#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020034
Linus Walleijbefe5bd2012-02-09 19:47:48 +010035/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070036 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
40 */
41struct pinctrl_maps {
42 struct list_head node;
43 struct pinctrl_map const *maps;
44 unsigned num_maps;
45};
46
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080047static bool pinctrl_dummy_state;
48
Stephen Warren57b676f2012-03-02 13:05:44 -070049/* Mutex taken by all entry points */
50DEFINE_MUTEX(pinctrl_mutex);
51
52/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060053LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020054
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010056static LIST_HEAD(pinctrl_list);
57
Stephen Warren57b676f2012-03-02 13:05:44 -070058/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070059static LIST_HEAD(pinctrl_maps);
60
61#define for_each_maps(_maps_node_, _i_, _map_) \
62 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
63 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
64 _i_ < _maps_node_->num_maps; \
Guennadi Liakhovetskibc664682012-05-23 00:20:17 +020065 _i_++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010066
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080067/**
68 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
69 *
70 * Usually this function is called by platforms without pinctrl driver support
71 * but run with some shared drivers using pinctrl APIs.
72 * After calling this function, the pinctrl core will return successfully
73 * with creating a dummy state for the driver to keep going smoothly.
74 */
75void pinctrl_provide_dummies(void)
76{
77 pinctrl_dummy_state = true;
78}
79
Linus Walleij2744e8a2011-05-02 20:50:54 +020080const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
81{
82 /* We're not allowed to register devices without name */
83 return pctldev->desc->name;
84}
85EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
86
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080087const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
88{
89 return dev_name(pctldev->dev);
90}
91EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
92
Linus Walleij2744e8a2011-05-02 20:50:54 +020093void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
94{
95 return pctldev->driver_data;
96}
97EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
98
99/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100100 * get_pinctrl_dev_from_devname() - look up pin controller device
101 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102 *
103 * Looks up a pin control device matching a certain device name or pure device
104 * pointer, the pure device pointer will take precedence.
105 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100106struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107{
108 struct pinctrl_dev *pctldev = NULL;
109 bool found = false;
110
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100111 if (!devname)
112 return NULL;
113
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100115 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116 /* Matched on device name */
117 found = true;
118 break;
119 }
120 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121
122 return found ? pctldev : NULL;
123}
124
Linus Walleij2744e8a2011-05-02 20:50:54 +0200125/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200126 * pin_get_from_name() - look up a pin number from a name
127 * @pctldev: the pin control device to lookup the pin on
128 * @name: the name of the pin to look up
129 */
130int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
131{
Chanho Park706e8522012-01-03 16:47:50 +0900132 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200133
Chanho Park706e8522012-01-03 16:47:50 +0900134 /* The pin number can be retrived from the pin controller descriptor */
135 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200136 struct pin_desc *desc;
137
Chanho Park706e8522012-01-03 16:47:50 +0900138 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200139 desc = pin_desc_get(pctldev, pin);
140 /* Pin space may be sparse */
141 if (desc == NULL)
142 continue;
143 if (desc->name && !strcmp(name, desc->name))
144 return pin;
145 }
146
147 return -EINVAL;
148}
149
150/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800151 * pin_get_name_from_id() - look up a pin name from a pin id
152 * @pctldev: the pin control device to lookup the pin on
153 * @name: the name of the pin to look up
154 */
155const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
156{
157 const struct pin_desc *desc;
158
159 desc = pin_desc_get(pctldev, pin);
160 if (desc == NULL) {
161 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
162 pin);
163 return NULL;
164 }
165
166 return desc->name;
167}
168
169/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200170 * pin_is_valid() - check if pin exists on controller
171 * @pctldev: the pin control device to check the pin on
172 * @pin: pin to check, use the local pin controller index number
173 *
174 * This tells us whether a certain pin exist on a certain pin controller or
175 * not. Pin lists may be sparse, so some pins may not exist.
176 */
177bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
178{
179 struct pin_desc *pindesc;
180
181 if (pin < 0)
182 return false;
183
Stephen Warren57b676f2012-03-02 13:05:44 -0700184 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200185 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700186 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187
Stephen Warren57b676f2012-03-02 13:05:44 -0700188 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189}
190EXPORT_SYMBOL_GPL(pin_is_valid);
191
192/* Deletes a range of pin descriptors */
193static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
194 const struct pinctrl_pin_desc *pins,
195 unsigned num_pins)
196{
197 int i;
198
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199 for (i = 0; i < num_pins; i++) {
200 struct pin_desc *pindesc;
201
202 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
203 pins[i].number);
204 if (pindesc != NULL) {
205 radix_tree_delete(&pctldev->pin_desc_tree,
206 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100207 if (pindesc->dynamic_name)
208 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200209 }
210 kfree(pindesc);
211 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212}
213
214static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
215 unsigned number, const char *name)
216{
217 struct pin_desc *pindesc;
218
219 pindesc = pin_desc_get(pctldev, number);
220 if (pindesc != NULL) {
221 pr_err("pin %d already registered on %s\n", number,
222 pctldev->desc->name);
223 return -EINVAL;
224 }
225
226 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700227 if (pindesc == NULL) {
228 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700230 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200231
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232 /* Set owner */
233 pindesc->pctldev = pctldev;
234
Stephen Warren9af1e442011-10-19 16:19:27 -0600235 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100236 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100237 pindesc->name = name;
238 } else {
239 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530240 if (pindesc->name == NULL) {
241 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100242 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530243 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100244 pindesc->dynamic_name = true;
245 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200248 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100249 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250 return 0;
251}
252
253static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
254 struct pinctrl_pin_desc const *pins,
255 unsigned num_descs)
256{
257 unsigned i;
258 int ret = 0;
259
260 for (i = 0; i < num_descs; i++) {
261 ret = pinctrl_register_one_pin(pctldev,
262 pins[i].number, pins[i].name);
263 if (ret)
264 return ret;
265 }
266
267 return 0;
268}
269
270/**
271 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
272 * @pctldev: pin controller device to check
273 * @gpio: gpio pin to check taken from the global GPIO pin space
274 *
275 * Tries to match a GPIO pin number to the ranges handled by a certain pin
276 * controller, return the range or NULL
277 */
278static struct pinctrl_gpio_range *
279pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
280{
281 struct pinctrl_gpio_range *range = NULL;
282
283 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200284 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
285 /* Check if we're in the valid range */
286 if (gpio >= range->base &&
287 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200288 return range;
289 }
290 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200291
292 return NULL;
293}
294
295/**
296 * pinctrl_get_device_gpio_range() - find device for GPIO range
297 * @gpio: the pin to locate the pin controller for
298 * @outdev: the pin control device if found
299 * @outrange: the GPIO range if found
300 *
301 * Find the pin controller handling a certain GPIO pin from the pinspace of
302 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800303 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
304 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700306static int pinctrl_get_device_gpio_range(unsigned gpio,
307 struct pinctrl_dev **outdev,
308 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309{
310 struct pinctrl_dev *pctldev = NULL;
311
312 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313 list_for_each_entry(pctldev, &pinctrldev_list, node) {
314 struct pinctrl_gpio_range *range;
315
316 range = pinctrl_match_gpio_range(pctldev, gpio);
317 if (range != NULL) {
318 *outdev = pctldev;
319 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 return 0;
321 }
322 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800324 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325}
326
327/**
328 * pinctrl_add_gpio_range() - register a GPIO range for a controller
329 * @pctldev: pin controller device to add the range to
330 * @range: the GPIO range to add
331 *
332 * This adds a range of GPIOs to be handled by a certain pin controller. Call
333 * this to register handled ranges after registering your pin controller.
334 */
335void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
336 struct pinctrl_gpio_range *range)
337{
Stephen Warren57b676f2012-03-02 13:05:44 -0700338 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700339 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700340 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200341}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700342EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200343
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800344void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
345 struct pinctrl_gpio_range *ranges,
346 unsigned nranges)
347{
348 int i;
349
350 for (i = 0; i < nranges; i++)
351 pinctrl_add_gpio_range(pctldev, &ranges[i]);
352}
353EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
354
Linus Walleij192c3692012-11-20 14:03:37 +0100355struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530356 struct pinctrl_gpio_range *range)
357{
358 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
359
Linus Walleijdfa97512012-11-20 14:54:18 +0100360 /*
361 * If we can't find this device, let's assume that is because
362 * it has not probed yet, so the driver trying to register this
363 * range need to defer probing.
364 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530365 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100366 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530367
368 pinctrl_add_gpio_range(pctldev, range);
369 return pctldev;
370}
Linus Walleij192c3692012-11-20 14:03:37 +0100371EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530372
Linus Walleij2744e8a2011-05-02 20:50:54 +0200373/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100374 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
375 * @pctldev: the pin controller device to look in
376 * @pin: a controller-local number to find the range for
377 */
378struct pinctrl_gpio_range *
379pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
380 unsigned int pin)
381{
382 struct pinctrl_gpio_range *range = NULL;
383
384 /* Loop over the ranges */
385 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
386 /* Check if we're in the valid range */
387 if (pin >= range->pin_base &&
388 pin < range->pin_base + range->npins) {
389 return range;
390 }
391 }
392
393 return NULL;
394}
395EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
396
397/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530398 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
399 * @pctldev: pin controller device to remove the range from
400 * @range: the GPIO range to remove
401 */
402void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
403 struct pinctrl_gpio_range *range)
404{
405 mutex_lock(&pinctrl_mutex);
406 list_del(&range->node);
407 mutex_unlock(&pinctrl_mutex);
408}
409EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
410
411/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200412 * pinctrl_get_group_selector() - returns the group selector for a group
413 * @pctldev: the pin controller handling the group
414 * @pin_group: the pin group to look up
415 */
416int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
417 const char *pin_group)
418{
419 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530420 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200421 unsigned group_selector = 0;
422
Viresh Kumard1e90e92012-03-30 11:25:40 +0530423 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200424 const char *gname = pctlops->get_group_name(pctldev,
425 group_selector);
426 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700427 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200428 "found group selector %u for %s\n",
429 group_selector,
430 pin_group);
431 return group_selector;
432 }
433
434 group_selector++;
435 }
436
Stephen Warren51cd24e2011-12-09 16:59:05 -0700437 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200438 pin_group);
439
440 return -EINVAL;
441}
442
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100443/**
444 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
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_request() semantics, platforms and individual drivers
449 * shall *NOT* request GPIO pins to be muxed in.
450 */
451int pinctrl_request_gpio(unsigned gpio)
452{
453 struct pinctrl_dev *pctldev;
454 struct pinctrl_gpio_range *range;
455 int ret;
456 int pin;
457
Stephen Warren57b676f2012-03-02 13:05:44 -0700458 mutex_lock(&pinctrl_mutex);
459
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100460 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700461 if (ret) {
462 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800463 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700464 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100465
466 /* Convert to the pin controllers number space */
467 pin = gpio - range->base + range->pin_base;
468
Stephen Warren57b676f2012-03-02 13:05:44 -0700469 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
470
471 mutex_unlock(&pinctrl_mutex);
472 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100473}
474EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
475
476/**
477 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
478 * @gpio: the GPIO pin number from the GPIO subsystem number space
479 *
480 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
481 * as part of their gpio_free() semantics, platforms and individual drivers
482 * shall *NOT* request GPIO pins to be muxed out.
483 */
484void pinctrl_free_gpio(unsigned gpio)
485{
486 struct pinctrl_dev *pctldev;
487 struct pinctrl_gpio_range *range;
488 int ret;
489 int pin;
490
Stephen Warren57b676f2012-03-02 13:05:44 -0700491 mutex_lock(&pinctrl_mutex);
492
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100493 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700494 if (ret) {
495 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100496 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700497 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100498
499 /* Convert to the pin controllers number space */
500 pin = gpio - range->base + range->pin_base;
501
Stephen Warren57b676f2012-03-02 13:05:44 -0700502 pinmux_free_gpio(pctldev, pin, range);
503
504 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100505}
506EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
507
508static int pinctrl_gpio_direction(unsigned gpio, bool input)
509{
510 struct pinctrl_dev *pctldev;
511 struct pinctrl_gpio_range *range;
512 int ret;
513 int pin;
514
515 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
516 if (ret)
517 return ret;
518
519 /* Convert to the pin controllers number space */
520 pin = gpio - range->base + range->pin_base;
521
522 return pinmux_gpio_direction(pctldev, range, pin, input);
523}
524
525/**
526 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
527 * @gpio: the GPIO pin number from the GPIO subsystem number space
528 *
529 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
530 * as part of their gpio_direction_input() semantics, platforms and individual
531 * drivers shall *NOT* touch pin control GPIO calls.
532 */
533int pinctrl_gpio_direction_input(unsigned gpio)
534{
Stephen Warren57b676f2012-03-02 13:05:44 -0700535 int ret;
536 mutex_lock(&pinctrl_mutex);
537 ret = pinctrl_gpio_direction(gpio, true);
538 mutex_unlock(&pinctrl_mutex);
539 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100540}
541EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
542
543/**
544 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
545 * @gpio: the GPIO pin number from the GPIO subsystem number space
546 *
547 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
548 * as part of their gpio_direction_output() semantics, platforms and individual
549 * drivers shall *NOT* touch pin control GPIO calls.
550 */
551int pinctrl_gpio_direction_output(unsigned gpio)
552{
Stephen Warren57b676f2012-03-02 13:05:44 -0700553 int ret;
554 mutex_lock(&pinctrl_mutex);
555 ret = pinctrl_gpio_direction(gpio, false);
556 mutex_unlock(&pinctrl_mutex);
557 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100558}
559EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
560
Stephen Warren6e5e9592012-03-02 13:05:47 -0700561static struct pinctrl_state *find_state(struct pinctrl *p,
562 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100563{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700564 struct pinctrl_state *state;
565
566 list_for_each_entry(state, &p->states, node)
567 if (!strcmp(state->name, name))
568 return state;
569
570 return NULL;
571}
572
573static struct pinctrl_state *create_state(struct pinctrl *p,
574 const char *name)
575{
576 struct pinctrl_state *state;
577
578 state = kzalloc(sizeof(*state), GFP_KERNEL);
579 if (state == NULL) {
580 dev_err(p->dev,
581 "failed to alloc struct pinctrl_state\n");
582 return ERR_PTR(-ENOMEM);
583 }
584
585 state->name = name;
586 INIT_LIST_HEAD(&state->settings);
587
588 list_add_tail(&state->node, &p->states);
589
590 return state;
591}
592
593static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
594{
595 struct pinctrl_state *state;
596 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700597 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700598
599 state = find_state(p, map->name);
600 if (!state)
601 state = create_state(p, map->name);
602 if (IS_ERR(state))
603 return PTR_ERR(state);
604
Stephen Warren1e2082b2012-03-02 13:05:48 -0700605 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
606 return 0;
607
Stephen Warren6e5e9592012-03-02 13:05:47 -0700608 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
609 if (setting == NULL) {
610 dev_err(p->dev,
611 "failed to alloc struct pinctrl_setting\n");
612 return -ENOMEM;
613 }
614
Stephen Warren1e2082b2012-03-02 13:05:48 -0700615 setting->type = map->type;
616
Stephen Warren6e5e9592012-03-02 13:05:47 -0700617 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
618 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700619 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100620 /* Do not defer probing of hogs (circular loop) */
621 if (!strcmp(map->ctrl_dev_name, map->dev_name))
622 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200623 /*
624 * OK let us guess that the driver is not there yet, and
625 * let's defer obtaining this pinctrl handle to later...
626 */
Linus Walleij89216492012-12-11 14:14:32 +0100627 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
628 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200629 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700630 }
631
Linus Walleij1a789582012-10-17 20:51:54 +0200632 setting->dev_name = map->dev_name;
633
Stephen Warren1e2082b2012-03-02 13:05:48 -0700634 switch (map->type) {
635 case PIN_MAP_TYPE_MUX_GROUP:
636 ret = pinmux_map_to_setting(map, setting);
637 break;
638 case PIN_MAP_TYPE_CONFIGS_PIN:
639 case PIN_MAP_TYPE_CONFIGS_GROUP:
640 ret = pinconf_map_to_setting(map, setting);
641 break;
642 default:
643 ret = -EINVAL;
644 break;
645 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700646 if (ret < 0) {
647 kfree(setting);
648 return ret;
649 }
650
651 list_add_tail(&setting->node, &state->settings);
652
653 return 0;
654}
655
656static struct pinctrl *find_pinctrl(struct device *dev)
657{
658 struct pinctrl *p;
659
Stephen Warren1e2082b2012-03-02 13:05:48 -0700660 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700661 if (p->dev == dev)
662 return p;
663
664 return NULL;
665}
666
667static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
668
669static struct pinctrl *create_pinctrl(struct device *dev)
670{
671 struct pinctrl *p;
672 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700673 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100674 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700675 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700676 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100677
678 /*
679 * create the state cookie holder struct pinctrl for each
680 * mapping, this is what consumers will get when requesting
681 * a pin control handle with pinctrl_get()
682 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700683 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700684 if (p == NULL) {
685 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100686 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700687 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700688 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700689 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600690 INIT_LIST_HEAD(&p->dt_maps);
691
692 ret = pinctrl_dt_to_map(p);
693 if (ret < 0) {
694 kfree(p);
695 return ERR_PTR(ret);
696 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700697
698 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100699
700 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700701 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700702 /* Map must be for this device */
703 if (strcmp(map->dev_name, devname))
704 continue;
705
Stephen Warren6e5e9592012-03-02 13:05:47 -0700706 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100707 /*
708 * At this point the adding of a setting may:
709 *
710 * - Defer, if the pinctrl device is not yet available
711 * - Fail, if the pinctrl device is not yet available,
712 * AND the setting is a hog. We cannot defer that, since
713 * the hog will kick in immediately after the device
714 * is registered.
715 *
716 * If the error returned was not -EPROBE_DEFER then we
717 * accumulate the errors to see if we end up with
718 * an -EPROBE_DEFER later, as that is the worst case.
719 */
720 if (ret == -EPROBE_DEFER) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700721 pinctrl_put_locked(p, false);
722 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100723 }
724 }
Linus Walleij89216492012-12-11 14:14:32 +0100725 if (ret < 0) {
726 /* If some other error than deferral occured, return here */
727 pinctrl_put_locked(p, false);
728 return ERR_PTR(ret);
729 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100730
Linus Walleijab780292013-01-22 10:56:14 -0700731 kref_init(&p->users);
732
Linus Walleijb0666ba2012-12-11 13:12:35 +0100733 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700734 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100735
736 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700737}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700738
Stephen Warren6e5e9592012-03-02 13:05:47 -0700739static struct pinctrl *pinctrl_get_locked(struct device *dev)
740{
741 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700742
Stephen Warren6e5e9592012-03-02 13:05:47 -0700743 if (WARN_ON(!dev))
744 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700745
Linus Walleijab780292013-01-22 10:56:14 -0700746 /*
747 * See if somebody else (such as the device core) has already
748 * obtained a handle to the pinctrl for this device. In that case,
749 * return another pointer to it.
750 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700751 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700752 if (p != NULL) {
753 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
754 kref_get(&p->users);
755 return p;
756 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700757
Richard Genoudd599bfb2012-08-10 16:53:49 +0200758 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100759}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700760
761/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700762 * pinctrl_get() - retrieves the pinctrl handle for a device
763 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700764 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700765struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700766{
767 struct pinctrl *p;
768
Stephen Warren57b676f2012-03-02 13:05:44 -0700769 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700770 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700771 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700772
773 return p;
774}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775EXPORT_SYMBOL_GPL(pinctrl_get);
776
Stephen Warren6e5e9592012-03-02 13:05:47 -0700777static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700778{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 struct pinctrl_state *state, *n1;
780 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700781
Stephen Warren6e5e9592012-03-02 13:05:47 -0700782 list_for_each_entry_safe(state, n1, &p->states, node) {
783 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700784 switch (setting->type) {
785 case PIN_MAP_TYPE_MUX_GROUP:
786 if (state == p->state)
787 pinmux_disable_setting(setting);
788 pinmux_free_setting(setting);
789 break;
790 case PIN_MAP_TYPE_CONFIGS_PIN:
791 case PIN_MAP_TYPE_CONFIGS_GROUP:
792 pinconf_free_setting(setting);
793 break;
794 default:
795 break;
796 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700797 list_del(&setting->node);
798 kfree(setting);
799 }
800 list_del(&state->node);
801 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700802 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700803
Stephen Warren57291ce2012-03-23 10:29:46 -0600804 pinctrl_dt_free_maps(p);
805
Stephen Warren6e5e9592012-03-02 13:05:47 -0700806 if (inlist)
807 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700808 kfree(p);
809}
810
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811/**
Linus Walleijab780292013-01-22 10:56:14 -0700812 * pinctrl_release() - release the pinctrl handle
813 * @kref: the kref in the pinctrl being released
814 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530815static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700816{
817 struct pinctrl *p = container_of(kref, struct pinctrl, users);
818
819 pinctrl_put_locked(p, true);
820}
821
822/**
823 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700824 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825 */
826void pinctrl_put(struct pinctrl *p)
827{
Stephen Warren57b676f2012-03-02 13:05:44 -0700828 mutex_lock(&pinctrl_mutex);
Linus Walleijab780292013-01-22 10:56:14 -0700829 kref_put(&p->users, pinctrl_release);
Stephen Warren57b676f2012-03-02 13:05:44 -0700830 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831}
832EXPORT_SYMBOL_GPL(pinctrl_put);
833
Stephen Warren6e5e9592012-03-02 13:05:47 -0700834static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
835 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700836{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700837 struct pinctrl_state *state;
838
839 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800840 if (!state) {
841 if (pinctrl_dummy_state) {
842 /* create dummy state */
843 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
844 name);
845 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200846 } else
847 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800848 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700849
850 return state;
851}
852
853/**
854 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
855 * @p: the pinctrl handle to retrieve the state from
856 * @name: the state name to retrieve
857 */
858struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
859{
860 struct pinctrl_state *s;
861
862 mutex_lock(&pinctrl_mutex);
863 s = pinctrl_lookup_state_locked(p, name);
864 mutex_unlock(&pinctrl_mutex);
865
866 return s;
867}
868EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
869
870static int pinctrl_select_state_locked(struct pinctrl *p,
871 struct pinctrl_state *state)
872{
873 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700874 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700875
Stephen Warren6e5e9592012-03-02 13:05:47 -0700876 if (p->state == state)
877 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700878
Stephen Warren6e5e9592012-03-02 13:05:47 -0700879 if (p->state) {
880 /*
881 * The set of groups with a mux configuration in the old state
882 * may not be identical to the set of groups with a mux setting
883 * in the new state. While this might be unusual, it's entirely
884 * possible for the "user"-supplied mapping table to be written
885 * that way. For each group that was configured in the old state
886 * but not in the new state, this code puts that group into a
887 * safe/disabled state.
888 */
889 list_for_each_entry(setting, &p->state->settings, node) {
890 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700891 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
892 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700893 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700894 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
895 continue;
896 if (setting2->data.mux.group ==
897 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700898 found = true;
899 break;
900 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700901 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700902 if (!found)
903 pinmux_disable_setting(setting);
904 }
905 }
906
907 p->state = state;
908
909 /* Apply all the settings for the new state */
910 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700911 switch (setting->type) {
912 case PIN_MAP_TYPE_MUX_GROUP:
913 ret = pinmux_enable_setting(setting);
914 break;
915 case PIN_MAP_TYPE_CONFIGS_PIN:
916 case PIN_MAP_TYPE_CONFIGS_GROUP:
917 ret = pinconf_apply_setting(setting);
918 break;
919 default:
920 ret = -EINVAL;
921 break;
922 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700923 if (ret < 0) {
924 /* FIXME: Difficult to return to prev state */
925 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700926 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700927 }
928
Stephen Warren7ecdb162012-03-02 13:05:45 -0700929 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700930}
931
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100932/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933 * pinctrl_select() - select/activate/program a pinctrl state to HW
934 * @p: the pinctrl handle for the device that requests configuratio
935 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100936 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700937int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100938{
Stephen Warren57b676f2012-03-02 13:05:44 -0700939 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700940
Stephen Warren57b676f2012-03-02 13:05:44 -0700941 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700942 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700943 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700944
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100945 return ret;
946}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700947EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100948
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600949static void devm_pinctrl_release(struct device *dev, void *res)
950{
951 pinctrl_put(*(struct pinctrl **)res);
952}
953
954/**
955 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
956 * @dev: the device to obtain the handle for
957 *
958 * If there is a need to explicitly destroy the returned struct pinctrl,
959 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
960 */
961struct pinctrl *devm_pinctrl_get(struct device *dev)
962{
963 struct pinctrl **ptr, *p;
964
965 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
966 if (!ptr)
967 return ERR_PTR(-ENOMEM);
968
969 p = pinctrl_get(dev);
970 if (!IS_ERR(p)) {
971 *ptr = p;
972 devres_add(dev, ptr);
973 } else {
974 devres_free(ptr);
975 }
976
977 return p;
978}
979EXPORT_SYMBOL_GPL(devm_pinctrl_get);
980
981static int devm_pinctrl_match(struct device *dev, void *res, void *data)
982{
983 struct pinctrl **p = res;
984
985 return *p == data;
986}
987
988/**
989 * devm_pinctrl_put() - Resource managed pinctrl_put()
990 * @p: the pinctrl handle to release
991 *
992 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
993 * this function will not need to be called and the resource management
994 * code will ensure that the resource is freed.
995 */
996void devm_pinctrl_put(struct pinctrl *p)
997{
998 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
999 devm_pinctrl_match, p));
1000 pinctrl_put(p);
1001}
1002EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1003
Stephen Warren57291ce2012-03-23 10:29:46 -06001004int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1005 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001006{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001007 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001008 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001009
1010 pr_debug("add %d pinmux maps\n", num_maps);
1011
1012 /* First sanity check the new mapping */
1013 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001014 if (!maps[i].dev_name) {
1015 pr_err("failed to register map %s (%d): no device given\n",
1016 maps[i].name, i);
1017 return -EINVAL;
1018 }
1019
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001020 if (!maps[i].name) {
1021 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001022 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001023 return -EINVAL;
1024 }
1025
Stephen Warren1e2082b2012-03-02 13:05:48 -07001026 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1027 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001028 pr_err("failed to register map %s (%d): no pin control device given\n",
1029 maps[i].name, i);
1030 return -EINVAL;
1031 }
1032
Stephen Warren1e2082b2012-03-02 13:05:48 -07001033 switch (maps[i].type) {
1034 case PIN_MAP_TYPE_DUMMY_STATE:
1035 break;
1036 case PIN_MAP_TYPE_MUX_GROUP:
1037 ret = pinmux_validate_map(&maps[i], i);
1038 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001039 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001040 break;
1041 case PIN_MAP_TYPE_CONFIGS_PIN:
1042 case PIN_MAP_TYPE_CONFIGS_GROUP:
1043 ret = pinconf_validate_map(&maps[i], i);
1044 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001045 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001046 break;
1047 default:
1048 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001049 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001050 return -EINVAL;
1051 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001052 }
1053
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001054 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1055 if (!maps_node) {
1056 pr_err("failed to alloc struct pinctrl_maps\n");
1057 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001058 }
1059
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001060 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001061 if (dup) {
1062 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1063 GFP_KERNEL);
1064 if (!maps_node->maps) {
1065 pr_err("failed to duplicate mapping table\n");
1066 kfree(maps_node);
1067 return -ENOMEM;
1068 }
1069 } else {
1070 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001071 }
1072
Stephen Warren57291ce2012-03-23 10:29:46 -06001073 if (!locked)
1074 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001075 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001076 if (!locked)
1077 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001078
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001079 return 0;
1080}
1081
Stephen Warren57291ce2012-03-23 10:29:46 -06001082/**
1083 * pinctrl_register_mappings() - register a set of pin controller mappings
1084 * @maps: the pincontrol mappings table to register. This should probably be
1085 * marked with __initdata so it can be discarded after boot. This
1086 * function will perform a shallow copy for the mapping entries.
1087 * @num_maps: the number of maps in the mapping table
1088 */
1089int pinctrl_register_mappings(struct pinctrl_map const *maps,
1090 unsigned num_maps)
1091{
1092 return pinctrl_register_map(maps, num_maps, true, false);
1093}
1094
1095void pinctrl_unregister_map(struct pinctrl_map const *map)
1096{
1097 struct pinctrl_maps *maps_node;
1098
1099 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1100 if (maps_node->maps == map) {
1101 list_del(&maps_node->node);
1102 return;
1103 }
1104 }
1105}
1106
Julien Delacou840a47b2012-12-10 14:47:33 +01001107/**
1108 * pinctrl_force_sleep() - turn a given controller device into sleep state
1109 * @pctldev: pin controller device
1110 */
1111int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1112{
1113 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1114 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1115 return 0;
1116}
1117EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1118
1119/**
1120 * pinctrl_force_default() - turn a given controller device into default state
1121 * @pctldev: pin controller device
1122 */
1123int pinctrl_force_default(struct pinctrl_dev *pctldev)
1124{
1125 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1126 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1127 return 0;
1128}
1129EXPORT_SYMBOL_GPL(pinctrl_force_default);
1130
Linus Walleij2744e8a2011-05-02 20:50:54 +02001131#ifdef CONFIG_DEBUG_FS
1132
1133static int pinctrl_pins_show(struct seq_file *s, void *what)
1134{
1135 struct pinctrl_dev *pctldev = s->private;
1136 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001137 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001138
1139 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001140
Stephen Warren57b676f2012-03-02 13:05:44 -07001141 mutex_lock(&pinctrl_mutex);
1142
Chanho Park706e8522012-01-03 16:47:50 +09001143 /* The pin number can be retrived from the pin controller descriptor */
1144 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001145 struct pin_desc *desc;
1146
Chanho Park706e8522012-01-03 16:47:50 +09001147 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001148 desc = pin_desc_get(pctldev, pin);
1149 /* Pin space may be sparse */
1150 if (desc == NULL)
1151 continue;
1152
1153 seq_printf(s, "pin %d (%s) ", pin,
1154 desc->name ? desc->name : "unnamed");
1155
1156 /* Driver-specific info per pin */
1157 if (ops->pin_dbg_show)
1158 ops->pin_dbg_show(pctldev, s, pin);
1159
1160 seq_puts(s, "\n");
1161 }
1162
Stephen Warren57b676f2012-03-02 13:05:44 -07001163 mutex_unlock(&pinctrl_mutex);
1164
Linus Walleij2744e8a2011-05-02 20:50:54 +02001165 return 0;
1166}
1167
1168static int pinctrl_groups_show(struct seq_file *s, void *what)
1169{
1170 struct pinctrl_dev *pctldev = s->private;
1171 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301172 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001173
Viresh Kumard1e90e92012-03-30 11:25:40 +05301174 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001175 mutex_lock(&pinctrl_mutex);
1176
Linus Walleij2744e8a2011-05-02 20:50:54 +02001177 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301178 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001179 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001180 unsigned num_pins;
1181 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001182 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001183 int ret;
1184 int i;
1185
1186 ret = ops->get_group_pins(pctldev, selector,
1187 &pins, &num_pins);
1188 if (ret)
1189 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1190 gname);
1191 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001192 seq_printf(s, "group: %s\n", gname);
1193 for (i = 0; i < num_pins; i++) {
1194 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001195 if (WARN_ON(!pname)) {
1196 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001197 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001198 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001199 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1200 }
1201 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001202 }
1203 selector++;
1204 }
1205
Stephen Warren57b676f2012-03-02 13:05:44 -07001206 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001207
1208 return 0;
1209}
1210
1211static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1212{
1213 struct pinctrl_dev *pctldev = s->private;
1214 struct pinctrl_gpio_range *range = NULL;
1215
1216 seq_puts(s, "GPIO ranges handled:\n");
1217
Stephen Warren57b676f2012-03-02 13:05:44 -07001218 mutex_lock(&pinctrl_mutex);
1219
Linus Walleij2744e8a2011-05-02 20:50:54 +02001220 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001221 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001222 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1223 range->id, range->name,
1224 range->base, (range->base + range->npins - 1),
1225 range->pin_base,
1226 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001227 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001228
1229 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001230
1231 return 0;
1232}
1233
1234static int pinctrl_devices_show(struct seq_file *s, void *what)
1235{
1236 struct pinctrl_dev *pctldev;
1237
Linus Walleijae6b4d82011-10-19 18:14:33 +02001238 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001239
1240 mutex_lock(&pinctrl_mutex);
1241
Linus Walleij2744e8a2011-05-02 20:50:54 +02001242 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1243 seq_printf(s, "%s ", pctldev->desc->name);
1244 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001245 seq_puts(s, "yes ");
1246 else
1247 seq_puts(s, "no ");
1248 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001249 seq_puts(s, "yes");
1250 else
1251 seq_puts(s, "no");
1252 seq_puts(s, "\n");
1253 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001254
1255 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001256
1257 return 0;
1258}
1259
Stephen Warren1e2082b2012-03-02 13:05:48 -07001260static inline const char *map_type(enum pinctrl_map_type type)
1261{
1262 static const char * const names[] = {
1263 "INVALID",
1264 "DUMMY_STATE",
1265 "MUX_GROUP",
1266 "CONFIGS_PIN",
1267 "CONFIGS_GROUP",
1268 };
1269
1270 if (type >= ARRAY_SIZE(names))
1271 return "UNKNOWN";
1272
1273 return names[type];
1274}
1275
Stephen Warren3eedb432012-02-23 17:04:40 -07001276static int pinctrl_maps_show(struct seq_file *s, void *what)
1277{
1278 struct pinctrl_maps *maps_node;
1279 int i;
1280 struct pinctrl_map const *map;
1281
1282 seq_puts(s, "Pinctrl maps:\n");
1283
Stephen Warren57b676f2012-03-02 13:05:44 -07001284 mutex_lock(&pinctrl_mutex);
1285
Stephen Warren3eedb432012-02-23 17:04:40 -07001286 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001287 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1288 map->dev_name, map->name, map_type(map->type),
1289 map->type);
1290
1291 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1292 seq_printf(s, "controlling device %s\n",
1293 map->ctrl_dev_name);
1294
1295 switch (map->type) {
1296 case PIN_MAP_TYPE_MUX_GROUP:
1297 pinmux_show_map(s, map);
1298 break;
1299 case PIN_MAP_TYPE_CONFIGS_PIN:
1300 case PIN_MAP_TYPE_CONFIGS_GROUP:
1301 pinconf_show_map(s, map);
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001308 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001309
1310 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001311
1312 return 0;
1313}
1314
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001315static int pinctrl_show(struct seq_file *s, void *what)
1316{
1317 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001318 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001319 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001320
1321 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001322
1323 mutex_lock(&pinctrl_mutex);
1324
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001325 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001326 seq_printf(s, "device: %s current state: %s\n",
1327 dev_name(p->dev),
1328 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001329
Stephen Warren6e5e9592012-03-02 13:05:47 -07001330 list_for_each_entry(state, &p->states, node) {
1331 seq_printf(s, " state: %s\n", state->name);
1332
1333 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001334 struct pinctrl_dev *pctldev = setting->pctldev;
1335
1336 seq_printf(s, " type: %s controller %s ",
1337 map_type(setting->type),
1338 pinctrl_dev_get_name(pctldev));
1339
1340 switch (setting->type) {
1341 case PIN_MAP_TYPE_MUX_GROUP:
1342 pinmux_show_setting(s, setting);
1343 break;
1344 case PIN_MAP_TYPE_CONFIGS_PIN:
1345 case PIN_MAP_TYPE_CONFIGS_GROUP:
1346 pinconf_show_setting(s, setting);
1347 break;
1348 default:
1349 break;
1350 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001351 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001352 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001353 }
1354
Stephen Warren57b676f2012-03-02 13:05:44 -07001355 mutex_unlock(&pinctrl_mutex);
1356
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001357 return 0;
1358}
1359
Linus Walleij2744e8a2011-05-02 20:50:54 +02001360static int pinctrl_pins_open(struct inode *inode, struct file *file)
1361{
1362 return single_open(file, pinctrl_pins_show, inode->i_private);
1363}
1364
1365static int pinctrl_groups_open(struct inode *inode, struct file *file)
1366{
1367 return single_open(file, pinctrl_groups_show, inode->i_private);
1368}
1369
1370static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1371{
1372 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1373}
1374
1375static int pinctrl_devices_open(struct inode *inode, struct file *file)
1376{
1377 return single_open(file, pinctrl_devices_show, NULL);
1378}
1379
Stephen Warren3eedb432012-02-23 17:04:40 -07001380static int pinctrl_maps_open(struct inode *inode, struct file *file)
1381{
1382 return single_open(file, pinctrl_maps_show, NULL);
1383}
1384
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001385static int pinctrl_open(struct inode *inode, struct file *file)
1386{
1387 return single_open(file, pinctrl_show, NULL);
1388}
1389
Linus Walleij2744e8a2011-05-02 20:50:54 +02001390static const struct file_operations pinctrl_pins_ops = {
1391 .open = pinctrl_pins_open,
1392 .read = seq_read,
1393 .llseek = seq_lseek,
1394 .release = single_release,
1395};
1396
1397static const struct file_operations pinctrl_groups_ops = {
1398 .open = pinctrl_groups_open,
1399 .read = seq_read,
1400 .llseek = seq_lseek,
1401 .release = single_release,
1402};
1403
1404static const struct file_operations pinctrl_gpioranges_ops = {
1405 .open = pinctrl_gpioranges_open,
1406 .read = seq_read,
1407 .llseek = seq_lseek,
1408 .release = single_release,
1409};
1410
1411static const struct file_operations pinctrl_devices_ops = {
1412 .open = pinctrl_devices_open,
1413 .read = seq_read,
1414 .llseek = seq_lseek,
1415 .release = single_release,
1416};
1417
Stephen Warren3eedb432012-02-23 17:04:40 -07001418static const struct file_operations pinctrl_maps_ops = {
1419 .open = pinctrl_maps_open,
1420 .read = seq_read,
1421 .llseek = seq_lseek,
1422 .release = single_release,
1423};
1424
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001425static const struct file_operations pinctrl_ops = {
1426 .open = pinctrl_open,
1427 .read = seq_read,
1428 .llseek = seq_lseek,
1429 .release = single_release,
1430};
1431
Linus Walleij2744e8a2011-05-02 20:50:54 +02001432static struct dentry *debugfs_root;
1433
1434static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1435{
Tony Lindgren02157162012-01-20 08:17:22 -08001436 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437
Stephen Warren51cd24e2011-12-09 16:59:05 -07001438 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001440 pctldev->device_root = device_root;
1441
Linus Walleij2744e8a2011-05-02 20:50:54 +02001442 if (IS_ERR(device_root) || !device_root) {
1443 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001444 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001445 return;
1446 }
1447 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1448 device_root, pctldev, &pinctrl_pins_ops);
1449 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1450 device_root, pctldev, &pinctrl_groups_ops);
1451 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1452 device_root, pctldev, &pinctrl_gpioranges_ops);
1453 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001454 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001455}
1456
Tony Lindgren02157162012-01-20 08:17:22 -08001457static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1458{
1459 debugfs_remove_recursive(pctldev->device_root);
1460}
1461
Linus Walleij2744e8a2011-05-02 20:50:54 +02001462static void pinctrl_init_debugfs(void)
1463{
1464 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1465 if (IS_ERR(debugfs_root) || !debugfs_root) {
1466 pr_warn("failed to create debugfs directory\n");
1467 debugfs_root = NULL;
1468 return;
1469 }
1470
1471 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1472 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001473 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1474 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001475 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1476 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001477}
1478
1479#else /* CONFIG_DEBUG_FS */
1480
1481static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1482{
1483}
1484
1485static void pinctrl_init_debugfs(void)
1486{
1487}
1488
Tony Lindgren02157162012-01-20 08:17:22 -08001489static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1490{
1491}
1492
Linus Walleij2744e8a2011-05-02 20:50:54 +02001493#endif
1494
Stephen Warrend26bc492012-03-16 14:54:25 -06001495static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1496{
1497 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1498
1499 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301500 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001501 !ops->get_group_name ||
1502 !ops->get_group_pins)
1503 return -EINVAL;
1504
Stephen Warren57291ce2012-03-23 10:29:46 -06001505 if (ops->dt_node_to_map && !ops->dt_free_map)
1506 return -EINVAL;
1507
Stephen Warrend26bc492012-03-16 14:54:25 -06001508 return 0;
1509}
1510
Linus Walleij2744e8a2011-05-02 20:50:54 +02001511/**
1512 * pinctrl_register() - register a pin controller device
1513 * @pctldesc: descriptor for this pin controller
1514 * @dev: parent device for this pin controller
1515 * @driver_data: private pin controller data for this pin controller
1516 */
1517struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1518 struct device *dev, void *driver_data)
1519{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001520 struct pinctrl_dev *pctldev;
1521 int ret;
1522
Devendra Nagada9aecb2012-06-16 23:43:16 +05301523 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001524 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301525 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001526 return NULL;
1527
Stephen Warren02f5b982012-02-22 14:26:00 -07001528 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001529 if (pctldev == NULL) {
1530 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001531 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001532 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001533
1534 /* Initialize pin control device struct */
1535 pctldev->owner = pctldesc->owner;
1536 pctldev->desc = pctldesc;
1537 pctldev->driver_data = driver_data;
1538 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001539 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001540 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001541
Stephen Warrend26bc492012-03-16 14:54:25 -06001542 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301543 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001544 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001545 goto out_err;
1546 }
1547
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001548 /* If we're implementing pinmuxing, check the ops for sanity */
1549 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301550 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001551 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001552 }
1553
1554 /* If we're implementing pinconfig, check the ops for sanity */
1555 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301556 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001557 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001558 }
1559
Linus Walleij2744e8a2011-05-02 20:50:54 +02001560 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001561 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001562 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1563 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001564 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001565 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1566 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001567 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001568 }
1569
Stephen Warren57b676f2012-03-02 13:05:44 -07001570 mutex_lock(&pinctrl_mutex);
1571
Stephen Warren8b9c1392012-02-19 23:45:42 -07001572 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001573
Stephen Warren6e5e9592012-03-02 13:05:47 -07001574 pctldev->p = pinctrl_get_locked(pctldev->dev);
1575 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001576 pctldev->hog_default =
Stephen Warren6e5e9592012-03-02 13:05:47 -07001577 pinctrl_lookup_state_locked(pctldev->p,
1578 PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001579 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001580 dev_dbg(dev, "failed to lookup the default state\n");
1581 } else {
Julien Delacou840a47b2012-12-10 14:47:33 +01001582 if (pinctrl_select_state_locked(pctldev->p,
1583 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001584 dev_err(dev,
1585 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001586 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001587
1588 pctldev->hog_sleep =
1589 pinctrl_lookup_state_locked(pctldev->p,
1590 PINCTRL_STATE_SLEEP);
1591 if (IS_ERR(pctldev->hog_sleep))
1592 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001593 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001594
1595 mutex_unlock(&pinctrl_mutex);
1596
Stephen Warren2304b472012-02-22 14:26:01 -07001597 pinctrl_init_device_debugfs(pctldev);
1598
Linus Walleij2744e8a2011-05-02 20:50:54 +02001599 return pctldev;
1600
Stephen Warren51cd24e2011-12-09 16:59:05 -07001601out_err:
1602 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001603 return NULL;
1604}
1605EXPORT_SYMBOL_GPL(pinctrl_register);
1606
1607/**
1608 * pinctrl_unregister() - unregister pinmux
1609 * @pctldev: pin controller to unregister
1610 *
1611 * Called by pinmux drivers to unregister a pinmux.
1612 */
1613void pinctrl_unregister(struct pinctrl_dev *pctldev)
1614{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001615 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001616 if (pctldev == NULL)
1617 return;
1618
Tony Lindgren02157162012-01-20 08:17:22 -08001619 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001620
1621 mutex_lock(&pinctrl_mutex);
1622
Stephen Warren6e5e9592012-03-02 13:05:47 -07001623 if (!IS_ERR(pctldev->p))
1624 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001625
Linus Walleij2744e8a2011-05-02 20:50:54 +02001626 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001627 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001628 /* Destroy descriptor tree */
1629 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1630 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001631 /* remove gpio ranges map */
1632 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1633 list_del(&range->node);
1634
Stephen Warren51cd24e2011-12-09 16:59:05 -07001635 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001636
1637 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001638}
1639EXPORT_SYMBOL_GPL(pinctrl_unregister);
1640
1641static int __init pinctrl_init(void)
1642{
1643 pr_info("initialized pinctrl subsystem\n");
1644 pinctrl_init_debugfs();
1645 return 0;
1646}
1647
1648/* init early since many drivers really need to initialized pinmux early */
1649core_initcall(pinctrl_init);