blob: b0de6e7f1fdb8cb68566b24f04eee8722ab77862 [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
Stephen Warrenb2b3e662012-02-19 23:45:43 -070035
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080036static bool pinctrl_dummy_state;
37
Stephen Warren57b676f2012-03-02 13:05:44 -070038/* Mutex taken by all entry points */
39DEFINE_MUTEX(pinctrl_mutex);
40
41/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060042LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020043
Stephen Warren57b676f2012-03-02 13:05:44 -070044/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010045static LIST_HEAD(pinctrl_list);
46
Stephen Warren57b676f2012-03-02 13:05:44 -070047/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010048LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070049
Linus Walleijbefe5bd2012-02-09 19:47:48 +010050
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080051/**
52 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
53 *
54 * Usually this function is called by platforms without pinctrl driver support
55 * but run with some shared drivers using pinctrl APIs.
56 * After calling this function, the pinctrl core will return successfully
57 * with creating a dummy state for the driver to keep going smoothly.
58 */
59void pinctrl_provide_dummies(void)
60{
61 pinctrl_dummy_state = true;
62}
63
Linus Walleij2744e8a2011-05-02 20:50:54 +020064const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
65{
66 /* We're not allowed to register devices without name */
67 return pctldev->desc->name;
68}
69EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
70
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080071const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
72{
73 return dev_name(pctldev->dev);
74}
75EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
76
Linus Walleij2744e8a2011-05-02 20:50:54 +020077void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
78{
79 return pctldev->driver_data;
80}
81EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
82
83/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010084 * get_pinctrl_dev_from_devname() - look up pin controller device
85 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020086 *
87 * Looks up a pin control device matching a certain device name or pure device
88 * pointer, the pure device pointer will take precedence.
89 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010090struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020091{
92 struct pinctrl_dev *pctldev = NULL;
93 bool found = false;
94
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095 if (!devname)
96 return NULL;
97
Linus Walleij2744e8a2011-05-02 20:50:54 +020098 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010099 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100 /* Matched on device name */
101 found = true;
102 break;
103 }
104 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105
106 return found ? pctldev : NULL;
107}
108
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200110 * pin_get_from_name() - look up a pin number from a name
111 * @pctldev: the pin control device to lookup the pin on
112 * @name: the name of the pin to look up
113 */
114int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
115{
Chanho Park706e8522012-01-03 16:47:50 +0900116 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200117
Chanho Park706e8522012-01-03 16:47:50 +0900118 /* The pin number can be retrived from the pin controller descriptor */
119 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200120 struct pin_desc *desc;
121
Chanho Park706e8522012-01-03 16:47:50 +0900122 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200123 desc = pin_desc_get(pctldev, pin);
124 /* Pin space may be sparse */
125 if (desc == NULL)
126 continue;
127 if (desc->name && !strcmp(name, desc->name))
128 return pin;
129 }
130
131 return -EINVAL;
132}
133
134/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800135 * pin_get_name_from_id() - look up a pin name from a pin id
136 * @pctldev: the pin control device to lookup the pin on
137 * @name: the name of the pin to look up
138 */
139const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
140{
141 const struct pin_desc *desc;
142
143 desc = pin_desc_get(pctldev, pin);
144 if (desc == NULL) {
145 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
146 pin);
147 return NULL;
148 }
149
150 return desc->name;
151}
152
153/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200154 * pin_is_valid() - check if pin exists on controller
155 * @pctldev: the pin control device to check the pin on
156 * @pin: pin to check, use the local pin controller index number
157 *
158 * This tells us whether a certain pin exist on a certain pin controller or
159 * not. Pin lists may be sparse, so some pins may not exist.
160 */
161bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
162{
163 struct pin_desc *pindesc;
164
165 if (pin < 0)
166 return false;
167
Stephen Warren57b676f2012-03-02 13:05:44 -0700168 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200169 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700170 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200171
Stephen Warren57b676f2012-03-02 13:05:44 -0700172 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200173}
174EXPORT_SYMBOL_GPL(pin_is_valid);
175
176/* Deletes a range of pin descriptors */
177static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
178 const struct pinctrl_pin_desc *pins,
179 unsigned num_pins)
180{
181 int i;
182
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 for (i = 0; i < num_pins; i++) {
184 struct pin_desc *pindesc;
185
186 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
187 pins[i].number);
188 if (pindesc != NULL) {
189 radix_tree_delete(&pctldev->pin_desc_tree,
190 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100191 if (pindesc->dynamic_name)
192 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193 }
194 kfree(pindesc);
195 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200196}
197
198static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
199 unsigned number, const char *name)
200{
201 struct pin_desc *pindesc;
202
203 pindesc = pin_desc_get(pctldev, number);
204 if (pindesc != NULL) {
205 pr_err("pin %d already registered on %s\n", number,
206 pctldev->desc->name);
207 return -EINVAL;
208 }
209
210 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700211 if (pindesc == NULL) {
212 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700214 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200215
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216 /* Set owner */
217 pindesc->pctldev = pctldev;
218
Stephen Warren9af1e442011-10-19 16:19:27 -0600219 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100220 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100221 pindesc->name = name;
222 } else {
223 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530224 if (pindesc->name == NULL) {
225 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100226 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530227 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100228 pindesc->dynamic_name = true;
229 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200230
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100233 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200234 return 0;
235}
236
237static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
238 struct pinctrl_pin_desc const *pins,
239 unsigned num_descs)
240{
241 unsigned i;
242 int ret = 0;
243
244 for (i = 0; i < num_descs; i++) {
245 ret = pinctrl_register_one_pin(pctldev,
246 pins[i].number, pins[i].name);
247 if (ret)
248 return ret;
249 }
250
251 return 0;
252}
253
254/**
255 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
256 * @pctldev: pin controller device to check
257 * @gpio: gpio pin to check taken from the global GPIO pin space
258 *
259 * Tries to match a GPIO pin number to the ranges handled by a certain pin
260 * controller, return the range or NULL
261 */
262static struct pinctrl_gpio_range *
263pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
264{
265 struct pinctrl_gpio_range *range = NULL;
266
267 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200268 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
269 /* Check if we're in the valid range */
270 if (gpio >= range->base &&
271 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200272 return range;
273 }
274 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275
276 return NULL;
277}
278
279/**
280 * pinctrl_get_device_gpio_range() - find device for GPIO range
281 * @gpio: the pin to locate the pin controller for
282 * @outdev: the pin control device if found
283 * @outrange: the GPIO range if found
284 *
285 * Find the pin controller handling a certain GPIO pin from the pinspace of
286 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800287 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
288 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200289 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700290static int pinctrl_get_device_gpio_range(unsigned gpio,
291 struct pinctrl_dev **outdev,
292 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200293{
294 struct pinctrl_dev *pctldev = NULL;
295
296 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297 list_for_each_entry(pctldev, &pinctrldev_list, node) {
298 struct pinctrl_gpio_range *range;
299
300 range = pinctrl_match_gpio_range(pctldev, gpio);
301 if (range != NULL) {
302 *outdev = pctldev;
303 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200304 return 0;
305 }
306 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800308 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309}
310
311/**
312 * pinctrl_add_gpio_range() - register a GPIO range for a controller
313 * @pctldev: pin controller device to add the range to
314 * @range: the GPIO range to add
315 *
316 * This adds a range of GPIOs to be handled by a certain pin controller. Call
317 * this to register handled ranges after registering your pin controller.
318 */
319void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
320 struct pinctrl_gpio_range *range)
321{
Stephen Warren57b676f2012-03-02 13:05:44 -0700322 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700323 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700324 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700326EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200327
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800328void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *ranges,
330 unsigned nranges)
331{
332 int i;
333
334 for (i = 0; i < nranges; i++)
335 pinctrl_add_gpio_range(pctldev, &ranges[i]);
336}
337EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
338
Linus Walleij192c3692012-11-20 14:03:37 +0100339struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530340 struct pinctrl_gpio_range *range)
341{
342 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
343
Linus Walleijdfa97512012-11-20 14:54:18 +0100344 /*
345 * If we can't find this device, let's assume that is because
346 * it has not probed yet, so the driver trying to register this
347 * range need to defer probing.
348 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530349 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100350 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530351
352 pinctrl_add_gpio_range(pctldev, range);
353 return pctldev;
354}
Linus Walleij192c3692012-11-20 14:03:37 +0100355EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530356
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100358 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
359 * @pctldev: the pin controller device to look in
360 * @pin: a controller-local number to find the range for
361 */
362struct pinctrl_gpio_range *
363pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
364 unsigned int pin)
365{
366 struct pinctrl_gpio_range *range = NULL;
367
368 /* Loop over the ranges */
369 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
370 /* Check if we're in the valid range */
371 if (pin >= range->pin_base &&
372 pin < range->pin_base + range->npins) {
373 return range;
374 }
375 }
376
377 return NULL;
378}
379EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
380
381/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530382 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
383 * @pctldev: pin controller device to remove the range from
384 * @range: the GPIO range to remove
385 */
386void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
387 struct pinctrl_gpio_range *range)
388{
389 mutex_lock(&pinctrl_mutex);
390 list_del(&range->node);
391 mutex_unlock(&pinctrl_mutex);
392}
393EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
394
395/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200396 * pinctrl_get_group_selector() - returns the group selector for a group
397 * @pctldev: the pin controller handling the group
398 * @pin_group: the pin group to look up
399 */
400int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
401 const char *pin_group)
402{
403 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530404 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200405 unsigned group_selector = 0;
406
Viresh Kumard1e90e92012-03-30 11:25:40 +0530407 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200408 const char *gname = pctlops->get_group_name(pctldev,
409 group_selector);
410 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700411 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200412 "found group selector %u for %s\n",
413 group_selector,
414 pin_group);
415 return group_selector;
416 }
417
418 group_selector++;
419 }
420
Stephen Warren51cd24e2011-12-09 16:59:05 -0700421 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200422 pin_group);
423
424 return -EINVAL;
425}
426
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100427/**
428 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
429 * @gpio: the GPIO pin number from the GPIO subsystem number space
430 *
431 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
432 * as part of their gpio_request() semantics, platforms and individual drivers
433 * shall *NOT* request GPIO pins to be muxed in.
434 */
435int pinctrl_request_gpio(unsigned gpio)
436{
437 struct pinctrl_dev *pctldev;
438 struct pinctrl_gpio_range *range;
439 int ret;
440 int pin;
441
Stephen Warren57b676f2012-03-02 13:05:44 -0700442 mutex_lock(&pinctrl_mutex);
443
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100444 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700445 if (ret) {
446 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800447 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700448 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100449
450 /* Convert to the pin controllers number space */
451 pin = gpio - range->base + range->pin_base;
452
Stephen Warren57b676f2012-03-02 13:05:44 -0700453 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
454
455 mutex_unlock(&pinctrl_mutex);
456 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100457}
458EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
459
460/**
461 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
462 * @gpio: the GPIO pin number from the GPIO subsystem number space
463 *
464 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
465 * as part of their gpio_free() semantics, platforms and individual drivers
466 * shall *NOT* request GPIO pins to be muxed out.
467 */
468void pinctrl_free_gpio(unsigned gpio)
469{
470 struct pinctrl_dev *pctldev;
471 struct pinctrl_gpio_range *range;
472 int ret;
473 int pin;
474
Stephen Warren57b676f2012-03-02 13:05:44 -0700475 mutex_lock(&pinctrl_mutex);
476
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100477 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700478 if (ret) {
479 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100480 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700481 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100482
483 /* Convert to the pin controllers number space */
484 pin = gpio - range->base + range->pin_base;
485
Stephen Warren57b676f2012-03-02 13:05:44 -0700486 pinmux_free_gpio(pctldev, pin, range);
487
488 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100489}
490EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
491
492static int pinctrl_gpio_direction(unsigned gpio, bool input)
493{
494 struct pinctrl_dev *pctldev;
495 struct pinctrl_gpio_range *range;
496 int ret;
497 int pin;
498
499 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
500 if (ret)
501 return ret;
502
503 /* Convert to the pin controllers number space */
504 pin = gpio - range->base + range->pin_base;
505
506 return pinmux_gpio_direction(pctldev, range, pin, input);
507}
508
509/**
510 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
511 * @gpio: the GPIO pin number from the GPIO subsystem number space
512 *
513 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
514 * as part of their gpio_direction_input() semantics, platforms and individual
515 * drivers shall *NOT* touch pin control GPIO calls.
516 */
517int pinctrl_gpio_direction_input(unsigned gpio)
518{
Stephen Warren57b676f2012-03-02 13:05:44 -0700519 int ret;
520 mutex_lock(&pinctrl_mutex);
521 ret = pinctrl_gpio_direction(gpio, true);
522 mutex_unlock(&pinctrl_mutex);
523 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100524}
525EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
526
527/**
528 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
529 * @gpio: the GPIO pin number from the GPIO subsystem number space
530 *
531 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
532 * as part of their gpio_direction_output() semantics, platforms and individual
533 * drivers shall *NOT* touch pin control GPIO calls.
534 */
535int pinctrl_gpio_direction_output(unsigned gpio)
536{
Stephen Warren57b676f2012-03-02 13:05:44 -0700537 int ret;
538 mutex_lock(&pinctrl_mutex);
539 ret = pinctrl_gpio_direction(gpio, false);
540 mutex_unlock(&pinctrl_mutex);
541 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100542}
543EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
544
Stephen Warren6e5e9592012-03-02 13:05:47 -0700545static struct pinctrl_state *find_state(struct pinctrl *p,
546 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100547{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700548 struct pinctrl_state *state;
549
550 list_for_each_entry(state, &p->states, node)
551 if (!strcmp(state->name, name))
552 return state;
553
554 return NULL;
555}
556
557static struct pinctrl_state *create_state(struct pinctrl *p,
558 const char *name)
559{
560 struct pinctrl_state *state;
561
562 state = kzalloc(sizeof(*state), GFP_KERNEL);
563 if (state == NULL) {
564 dev_err(p->dev,
565 "failed to alloc struct pinctrl_state\n");
566 return ERR_PTR(-ENOMEM);
567 }
568
569 state->name = name;
570 INIT_LIST_HEAD(&state->settings);
571
572 list_add_tail(&state->node, &p->states);
573
574 return state;
575}
576
577static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
578{
579 struct pinctrl_state *state;
580 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700581 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700582
583 state = find_state(p, map->name);
584 if (!state)
585 state = create_state(p, map->name);
586 if (IS_ERR(state))
587 return PTR_ERR(state);
588
Stephen Warren1e2082b2012-03-02 13:05:48 -0700589 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
590 return 0;
591
Stephen Warren6e5e9592012-03-02 13:05:47 -0700592 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
593 if (setting == NULL) {
594 dev_err(p->dev,
595 "failed to alloc struct pinctrl_setting\n");
596 return -ENOMEM;
597 }
598
Stephen Warren1e2082b2012-03-02 13:05:48 -0700599 setting->type = map->type;
600
Stephen Warren6e5e9592012-03-02 13:05:47 -0700601 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
602 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700603 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100604 /* Do not defer probing of hogs (circular loop) */
605 if (!strcmp(map->ctrl_dev_name, map->dev_name))
606 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200607 /*
608 * OK let us guess that the driver is not there yet, and
609 * let's defer obtaining this pinctrl handle to later...
610 */
Linus Walleij89216492012-12-11 14:14:32 +0100611 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
612 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200613 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700614 }
615
Linus Walleij1a789582012-10-17 20:51:54 +0200616 setting->dev_name = map->dev_name;
617
Stephen Warren1e2082b2012-03-02 13:05:48 -0700618 switch (map->type) {
619 case PIN_MAP_TYPE_MUX_GROUP:
620 ret = pinmux_map_to_setting(map, setting);
621 break;
622 case PIN_MAP_TYPE_CONFIGS_PIN:
623 case PIN_MAP_TYPE_CONFIGS_GROUP:
624 ret = pinconf_map_to_setting(map, setting);
625 break;
626 default:
627 ret = -EINVAL;
628 break;
629 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700630 if (ret < 0) {
631 kfree(setting);
632 return ret;
633 }
634
635 list_add_tail(&setting->node, &state->settings);
636
637 return 0;
638}
639
640static struct pinctrl *find_pinctrl(struct device *dev)
641{
642 struct pinctrl *p;
643
Stephen Warren1e2082b2012-03-02 13:05:48 -0700644 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700645 if (p->dev == dev)
646 return p;
647
648 return NULL;
649}
650
651static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
652
653static struct pinctrl *create_pinctrl(struct device *dev)
654{
655 struct pinctrl *p;
656 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700657 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100658 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700659 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700660 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661
662 /*
663 * create the state cookie holder struct pinctrl for each
664 * mapping, this is what consumers will get when requesting
665 * a pin control handle with pinctrl_get()
666 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700667 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700668 if (p == NULL) {
669 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700671 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700672 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700673 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600674 INIT_LIST_HEAD(&p->dt_maps);
675
676 ret = pinctrl_dt_to_map(p);
677 if (ret < 0) {
678 kfree(p);
679 return ERR_PTR(ret);
680 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700681
682 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100683
684 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700685 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700686 /* Map must be for this device */
687 if (strcmp(map->dev_name, devname))
688 continue;
689
Stephen Warren6e5e9592012-03-02 13:05:47 -0700690 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100691 /*
692 * At this point the adding of a setting may:
693 *
694 * - Defer, if the pinctrl device is not yet available
695 * - Fail, if the pinctrl device is not yet available,
696 * AND the setting is a hog. We cannot defer that, since
697 * the hog will kick in immediately after the device
698 * is registered.
699 *
700 * If the error returned was not -EPROBE_DEFER then we
701 * accumulate the errors to see if we end up with
702 * an -EPROBE_DEFER later, as that is the worst case.
703 */
704 if (ret == -EPROBE_DEFER) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700705 pinctrl_put_locked(p, false);
706 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100707 }
708 }
Linus Walleij89216492012-12-11 14:14:32 +0100709 if (ret < 0) {
710 /* If some other error than deferral occured, return here */
711 pinctrl_put_locked(p, false);
712 return ERR_PTR(ret);
713 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100714
Linus Walleijab780292013-01-22 10:56:14 -0700715 kref_init(&p->users);
716
Linus Walleijb0666ba2012-12-11 13:12:35 +0100717 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700718 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100719
720 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700721}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700722
Stephen Warren6e5e9592012-03-02 13:05:47 -0700723static struct pinctrl *pinctrl_get_locked(struct device *dev)
724{
725 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700726
Stephen Warren6e5e9592012-03-02 13:05:47 -0700727 if (WARN_ON(!dev))
728 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700729
Linus Walleijab780292013-01-22 10:56:14 -0700730 /*
731 * See if somebody else (such as the device core) has already
732 * obtained a handle to the pinctrl for this device. In that case,
733 * return another pointer to it.
734 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700735 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700736 if (p != NULL) {
737 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
738 kref_get(&p->users);
739 return p;
740 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700741
Richard Genoudd599bfb2012-08-10 16:53:49 +0200742 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100743}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700744
745/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700746 * pinctrl_get() - retrieves the pinctrl handle for a device
747 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700748 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700750{
751 struct pinctrl *p;
752
Stephen Warren57b676f2012-03-02 13:05:44 -0700753 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700754 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700755 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700756
757 return p;
758}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100759EXPORT_SYMBOL_GPL(pinctrl_get);
760
Stephen Warren6e5e9592012-03-02 13:05:47 -0700761static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700762{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700763 struct pinctrl_state *state, *n1;
764 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700765
Stephen Warren6e5e9592012-03-02 13:05:47 -0700766 list_for_each_entry_safe(state, n1, &p->states, node) {
767 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700768 switch (setting->type) {
769 case PIN_MAP_TYPE_MUX_GROUP:
770 if (state == p->state)
771 pinmux_disable_setting(setting);
772 pinmux_free_setting(setting);
773 break;
774 case PIN_MAP_TYPE_CONFIGS_PIN:
775 case PIN_MAP_TYPE_CONFIGS_GROUP:
776 pinconf_free_setting(setting);
777 break;
778 default:
779 break;
780 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700781 list_del(&setting->node);
782 kfree(setting);
783 }
784 list_del(&state->node);
785 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700786 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700787
Stephen Warren57291ce2012-03-23 10:29:46 -0600788 pinctrl_dt_free_maps(p);
789
Stephen Warren6e5e9592012-03-02 13:05:47 -0700790 if (inlist)
791 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700792 kfree(p);
793}
794
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795/**
Linus Walleijab780292013-01-22 10:56:14 -0700796 * pinctrl_release() - release the pinctrl handle
797 * @kref: the kref in the pinctrl being released
798 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530799static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700800{
801 struct pinctrl *p = container_of(kref, struct pinctrl, users);
802
803 pinctrl_put_locked(p, true);
804}
805
806/**
807 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700808 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809 */
810void pinctrl_put(struct pinctrl *p)
811{
Stephen Warren57b676f2012-03-02 13:05:44 -0700812 mutex_lock(&pinctrl_mutex);
Linus Walleijab780292013-01-22 10:56:14 -0700813 kref_put(&p->users, pinctrl_release);
Stephen Warren57b676f2012-03-02 13:05:44 -0700814 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100815}
816EXPORT_SYMBOL_GPL(pinctrl_put);
817
Stephen Warren6e5e9592012-03-02 13:05:47 -0700818static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
819 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700820{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700821 struct pinctrl_state *state;
822
823 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800824 if (!state) {
825 if (pinctrl_dummy_state) {
826 /* create dummy state */
827 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
828 name);
829 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200830 } else
831 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800832 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700833
834 return state;
835}
836
837/**
838 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
839 * @p: the pinctrl handle to retrieve the state from
840 * @name: the state name to retrieve
841 */
842struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
843{
844 struct pinctrl_state *s;
845
846 mutex_lock(&pinctrl_mutex);
847 s = pinctrl_lookup_state_locked(p, name);
848 mutex_unlock(&pinctrl_mutex);
849
850 return s;
851}
852EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
853
854static int pinctrl_select_state_locked(struct pinctrl *p,
855 struct pinctrl_state *state)
856{
857 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700858 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700859
Stephen Warren6e5e9592012-03-02 13:05:47 -0700860 if (p->state == state)
861 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700862
Stephen Warren6e5e9592012-03-02 13:05:47 -0700863 if (p->state) {
864 /*
865 * The set of groups with a mux configuration in the old state
866 * may not be identical to the set of groups with a mux setting
867 * in the new state. While this might be unusual, it's entirely
868 * possible for the "user"-supplied mapping table to be written
869 * that way. For each group that was configured in the old state
870 * but not in the new state, this code puts that group into a
871 * safe/disabled state.
872 */
873 list_for_each_entry(setting, &p->state->settings, node) {
874 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700875 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
876 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700877 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700878 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
879 continue;
880 if (setting2->data.mux.group ==
881 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700882 found = true;
883 break;
884 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700885 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700886 if (!found)
887 pinmux_disable_setting(setting);
888 }
889 }
890
891 p->state = state;
892
893 /* Apply all the settings for the new state */
894 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700895 switch (setting->type) {
896 case PIN_MAP_TYPE_MUX_GROUP:
897 ret = pinmux_enable_setting(setting);
898 break;
899 case PIN_MAP_TYPE_CONFIGS_PIN:
900 case PIN_MAP_TYPE_CONFIGS_GROUP:
901 ret = pinconf_apply_setting(setting);
902 break;
903 default:
904 ret = -EINVAL;
905 break;
906 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700907 if (ret < 0) {
908 /* FIXME: Difficult to return to prev state */
909 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700910 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700911 }
912
Stephen Warren7ecdb162012-03-02 13:05:45 -0700913 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700914}
915
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100916/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700917 * pinctrl_select() - select/activate/program a pinctrl state to HW
918 * @p: the pinctrl handle for the device that requests configuratio
919 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100920 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700921int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100922{
Stephen Warren57b676f2012-03-02 13:05:44 -0700923 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700924
Stephen Warren57b676f2012-03-02 13:05:44 -0700925 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700926 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700927 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100929 return ret;
930}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700931EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100932
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600933static void devm_pinctrl_release(struct device *dev, void *res)
934{
935 pinctrl_put(*(struct pinctrl **)res);
936}
937
938/**
939 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
940 * @dev: the device to obtain the handle for
941 *
942 * If there is a need to explicitly destroy the returned struct pinctrl,
943 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
944 */
945struct pinctrl *devm_pinctrl_get(struct device *dev)
946{
947 struct pinctrl **ptr, *p;
948
949 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
950 if (!ptr)
951 return ERR_PTR(-ENOMEM);
952
953 p = pinctrl_get(dev);
954 if (!IS_ERR(p)) {
955 *ptr = p;
956 devres_add(dev, ptr);
957 } else {
958 devres_free(ptr);
959 }
960
961 return p;
962}
963EXPORT_SYMBOL_GPL(devm_pinctrl_get);
964
965static int devm_pinctrl_match(struct device *dev, void *res, void *data)
966{
967 struct pinctrl **p = res;
968
969 return *p == data;
970}
971
972/**
973 * devm_pinctrl_put() - Resource managed pinctrl_put()
974 * @p: the pinctrl handle to release
975 *
976 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
977 * this function will not need to be called and the resource management
978 * code will ensure that the resource is freed.
979 */
980void devm_pinctrl_put(struct pinctrl *p)
981{
982 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
983 devm_pinctrl_match, p));
984 pinctrl_put(p);
985}
986EXPORT_SYMBOL_GPL(devm_pinctrl_put);
987
Stephen Warren57291ce2012-03-23 10:29:46 -0600988int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
989 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100990{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700991 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700992 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100993
994 pr_debug("add %d pinmux maps\n", num_maps);
995
996 /* First sanity check the new mapping */
997 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700998 if (!maps[i].dev_name) {
999 pr_err("failed to register map %s (%d): no device given\n",
1000 maps[i].name, i);
1001 return -EINVAL;
1002 }
1003
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001004 if (!maps[i].name) {
1005 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001006 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001007 return -EINVAL;
1008 }
1009
Stephen Warren1e2082b2012-03-02 13:05:48 -07001010 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1011 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001012 pr_err("failed to register map %s (%d): no pin control device given\n",
1013 maps[i].name, i);
1014 return -EINVAL;
1015 }
1016
Stephen Warren1e2082b2012-03-02 13:05:48 -07001017 switch (maps[i].type) {
1018 case PIN_MAP_TYPE_DUMMY_STATE:
1019 break;
1020 case PIN_MAP_TYPE_MUX_GROUP:
1021 ret = pinmux_validate_map(&maps[i], i);
1022 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001023 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001024 break;
1025 case PIN_MAP_TYPE_CONFIGS_PIN:
1026 case PIN_MAP_TYPE_CONFIGS_GROUP:
1027 ret = pinconf_validate_map(&maps[i], i);
1028 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001029 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001030 break;
1031 default:
1032 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001033 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001034 return -EINVAL;
1035 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001036 }
1037
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001038 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1039 if (!maps_node) {
1040 pr_err("failed to alloc struct pinctrl_maps\n");
1041 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001042 }
1043
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001044 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001045 if (dup) {
1046 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1047 GFP_KERNEL);
1048 if (!maps_node->maps) {
1049 pr_err("failed to duplicate mapping table\n");
1050 kfree(maps_node);
1051 return -ENOMEM;
1052 }
1053 } else {
1054 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001055 }
1056
Stephen Warren57291ce2012-03-23 10:29:46 -06001057 if (!locked)
1058 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001059 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001060 if (!locked)
1061 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001062
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001063 return 0;
1064}
1065
Stephen Warren57291ce2012-03-23 10:29:46 -06001066/**
1067 * pinctrl_register_mappings() - register a set of pin controller mappings
1068 * @maps: the pincontrol mappings table to register. This should probably be
1069 * marked with __initdata so it can be discarded after boot. This
1070 * function will perform a shallow copy for the mapping entries.
1071 * @num_maps: the number of maps in the mapping table
1072 */
1073int pinctrl_register_mappings(struct pinctrl_map const *maps,
1074 unsigned num_maps)
1075{
1076 return pinctrl_register_map(maps, num_maps, true, false);
1077}
1078
1079void pinctrl_unregister_map(struct pinctrl_map const *map)
1080{
1081 struct pinctrl_maps *maps_node;
1082
1083 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1084 if (maps_node->maps == map) {
1085 list_del(&maps_node->node);
1086 return;
1087 }
1088 }
1089}
1090
Julien Delacou840a47b2012-12-10 14:47:33 +01001091/**
1092 * pinctrl_force_sleep() - turn a given controller device into sleep state
1093 * @pctldev: pin controller device
1094 */
1095int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1096{
1097 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1098 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1102
1103/**
1104 * pinctrl_force_default() - turn a given controller device into default state
1105 * @pctldev: pin controller device
1106 */
1107int pinctrl_force_default(struct pinctrl_dev *pctldev)
1108{
1109 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1110 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1111 return 0;
1112}
1113EXPORT_SYMBOL_GPL(pinctrl_force_default);
1114
Linus Walleij2744e8a2011-05-02 20:50:54 +02001115#ifdef CONFIG_DEBUG_FS
1116
1117static int pinctrl_pins_show(struct seq_file *s, void *what)
1118{
1119 struct pinctrl_dev *pctldev = s->private;
1120 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001121 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001122
1123 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001124
Stephen Warren57b676f2012-03-02 13:05:44 -07001125 mutex_lock(&pinctrl_mutex);
1126
Chanho Park706e8522012-01-03 16:47:50 +09001127 /* The pin number can be retrived from the pin controller descriptor */
1128 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001129 struct pin_desc *desc;
1130
Chanho Park706e8522012-01-03 16:47:50 +09001131 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001132 desc = pin_desc_get(pctldev, pin);
1133 /* Pin space may be sparse */
1134 if (desc == NULL)
1135 continue;
1136
1137 seq_printf(s, "pin %d (%s) ", pin,
1138 desc->name ? desc->name : "unnamed");
1139
1140 /* Driver-specific info per pin */
1141 if (ops->pin_dbg_show)
1142 ops->pin_dbg_show(pctldev, s, pin);
1143
1144 seq_puts(s, "\n");
1145 }
1146
Stephen Warren57b676f2012-03-02 13:05:44 -07001147 mutex_unlock(&pinctrl_mutex);
1148
Linus Walleij2744e8a2011-05-02 20:50:54 +02001149 return 0;
1150}
1151
1152static int pinctrl_groups_show(struct seq_file *s, void *what)
1153{
1154 struct pinctrl_dev *pctldev = s->private;
1155 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301156 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001157
Viresh Kumard1e90e92012-03-30 11:25:40 +05301158 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001159 mutex_lock(&pinctrl_mutex);
1160
Linus Walleij2744e8a2011-05-02 20:50:54 +02001161 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301162 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001163 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001164 unsigned num_pins;
1165 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001166 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001167 int ret;
1168 int i;
1169
1170 ret = ops->get_group_pins(pctldev, selector,
1171 &pins, &num_pins);
1172 if (ret)
1173 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1174 gname);
1175 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001176 seq_printf(s, "group: %s\n", gname);
1177 for (i = 0; i < num_pins; i++) {
1178 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001179 if (WARN_ON(!pname)) {
1180 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001181 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001182 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001183 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1184 }
1185 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001186 }
1187 selector++;
1188 }
1189
Stephen Warren57b676f2012-03-02 13:05:44 -07001190 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001191
1192 return 0;
1193}
1194
1195static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1196{
1197 struct pinctrl_dev *pctldev = s->private;
1198 struct pinctrl_gpio_range *range = NULL;
1199
1200 seq_puts(s, "GPIO ranges handled:\n");
1201
Stephen Warren57b676f2012-03-02 13:05:44 -07001202 mutex_lock(&pinctrl_mutex);
1203
Linus Walleij2744e8a2011-05-02 20:50:54 +02001204 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001205 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001206 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1207 range->id, range->name,
1208 range->base, (range->base + range->npins - 1),
1209 range->pin_base,
1210 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001211 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001212
1213 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001214
1215 return 0;
1216}
1217
1218static int pinctrl_devices_show(struct seq_file *s, void *what)
1219{
1220 struct pinctrl_dev *pctldev;
1221
Linus Walleijae6b4d82011-10-19 18:14:33 +02001222 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001223
1224 mutex_lock(&pinctrl_mutex);
1225
Linus Walleij2744e8a2011-05-02 20:50:54 +02001226 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1227 seq_printf(s, "%s ", pctldev->desc->name);
1228 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001229 seq_puts(s, "yes ");
1230 else
1231 seq_puts(s, "no ");
1232 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001233 seq_puts(s, "yes");
1234 else
1235 seq_puts(s, "no");
1236 seq_puts(s, "\n");
1237 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001238
1239 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001240
1241 return 0;
1242}
1243
Stephen Warren1e2082b2012-03-02 13:05:48 -07001244static inline const char *map_type(enum pinctrl_map_type type)
1245{
1246 static const char * const names[] = {
1247 "INVALID",
1248 "DUMMY_STATE",
1249 "MUX_GROUP",
1250 "CONFIGS_PIN",
1251 "CONFIGS_GROUP",
1252 };
1253
1254 if (type >= ARRAY_SIZE(names))
1255 return "UNKNOWN";
1256
1257 return names[type];
1258}
1259
Stephen Warren3eedb432012-02-23 17:04:40 -07001260static int pinctrl_maps_show(struct seq_file *s, void *what)
1261{
1262 struct pinctrl_maps *maps_node;
1263 int i;
1264 struct pinctrl_map const *map;
1265
1266 seq_puts(s, "Pinctrl maps:\n");
1267
Stephen Warren57b676f2012-03-02 13:05:44 -07001268 mutex_lock(&pinctrl_mutex);
1269
Stephen Warren3eedb432012-02-23 17:04:40 -07001270 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001271 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1272 map->dev_name, map->name, map_type(map->type),
1273 map->type);
1274
1275 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1276 seq_printf(s, "controlling device %s\n",
1277 map->ctrl_dev_name);
1278
1279 switch (map->type) {
1280 case PIN_MAP_TYPE_MUX_GROUP:
1281 pinmux_show_map(s, map);
1282 break;
1283 case PIN_MAP_TYPE_CONFIGS_PIN:
1284 case PIN_MAP_TYPE_CONFIGS_GROUP:
1285 pinconf_show_map(s, map);
1286 break;
1287 default:
1288 break;
1289 }
1290
1291 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001292 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001293
1294 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001295
1296 return 0;
1297}
1298
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001299static int pinctrl_show(struct seq_file *s, void *what)
1300{
1301 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001302 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001303 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001304
1305 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001306
1307 mutex_lock(&pinctrl_mutex);
1308
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001309 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001310 seq_printf(s, "device: %s current state: %s\n",
1311 dev_name(p->dev),
1312 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001313
Stephen Warren6e5e9592012-03-02 13:05:47 -07001314 list_for_each_entry(state, &p->states, node) {
1315 seq_printf(s, " state: %s\n", state->name);
1316
1317 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001318 struct pinctrl_dev *pctldev = setting->pctldev;
1319
1320 seq_printf(s, " type: %s controller %s ",
1321 map_type(setting->type),
1322 pinctrl_dev_get_name(pctldev));
1323
1324 switch (setting->type) {
1325 case PIN_MAP_TYPE_MUX_GROUP:
1326 pinmux_show_setting(s, setting);
1327 break;
1328 case PIN_MAP_TYPE_CONFIGS_PIN:
1329 case PIN_MAP_TYPE_CONFIGS_GROUP:
1330 pinconf_show_setting(s, setting);
1331 break;
1332 default:
1333 break;
1334 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001335 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001336 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001337 }
1338
Stephen Warren57b676f2012-03-02 13:05:44 -07001339 mutex_unlock(&pinctrl_mutex);
1340
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001341 return 0;
1342}
1343
Linus Walleij2744e8a2011-05-02 20:50:54 +02001344static int pinctrl_pins_open(struct inode *inode, struct file *file)
1345{
1346 return single_open(file, pinctrl_pins_show, inode->i_private);
1347}
1348
1349static int pinctrl_groups_open(struct inode *inode, struct file *file)
1350{
1351 return single_open(file, pinctrl_groups_show, inode->i_private);
1352}
1353
1354static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1355{
1356 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1357}
1358
1359static int pinctrl_devices_open(struct inode *inode, struct file *file)
1360{
1361 return single_open(file, pinctrl_devices_show, NULL);
1362}
1363
Stephen Warren3eedb432012-02-23 17:04:40 -07001364static int pinctrl_maps_open(struct inode *inode, struct file *file)
1365{
1366 return single_open(file, pinctrl_maps_show, NULL);
1367}
1368
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001369static int pinctrl_open(struct inode *inode, struct file *file)
1370{
1371 return single_open(file, pinctrl_show, NULL);
1372}
1373
Linus Walleij2744e8a2011-05-02 20:50:54 +02001374static const struct file_operations pinctrl_pins_ops = {
1375 .open = pinctrl_pins_open,
1376 .read = seq_read,
1377 .llseek = seq_lseek,
1378 .release = single_release,
1379};
1380
1381static const struct file_operations pinctrl_groups_ops = {
1382 .open = pinctrl_groups_open,
1383 .read = seq_read,
1384 .llseek = seq_lseek,
1385 .release = single_release,
1386};
1387
1388static const struct file_operations pinctrl_gpioranges_ops = {
1389 .open = pinctrl_gpioranges_open,
1390 .read = seq_read,
1391 .llseek = seq_lseek,
1392 .release = single_release,
1393};
1394
1395static const struct file_operations pinctrl_devices_ops = {
1396 .open = pinctrl_devices_open,
1397 .read = seq_read,
1398 .llseek = seq_lseek,
1399 .release = single_release,
1400};
1401
Stephen Warren3eedb432012-02-23 17:04:40 -07001402static const struct file_operations pinctrl_maps_ops = {
1403 .open = pinctrl_maps_open,
1404 .read = seq_read,
1405 .llseek = seq_lseek,
1406 .release = single_release,
1407};
1408
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001409static const struct file_operations pinctrl_ops = {
1410 .open = pinctrl_open,
1411 .read = seq_read,
1412 .llseek = seq_lseek,
1413 .release = single_release,
1414};
1415
Linus Walleij2744e8a2011-05-02 20:50:54 +02001416static struct dentry *debugfs_root;
1417
1418static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1419{
Tony Lindgren02157162012-01-20 08:17:22 -08001420 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001421
Stephen Warren51cd24e2011-12-09 16:59:05 -07001422 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001423 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001424 pctldev->device_root = device_root;
1425
Linus Walleij2744e8a2011-05-02 20:50:54 +02001426 if (IS_ERR(device_root) || !device_root) {
1427 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001428 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001429 return;
1430 }
1431 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1432 device_root, pctldev, &pinctrl_pins_ops);
1433 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1434 device_root, pctldev, &pinctrl_groups_ops);
1435 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1436 device_root, pctldev, &pinctrl_gpioranges_ops);
1437 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001438 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439}
1440
Tony Lindgren02157162012-01-20 08:17:22 -08001441static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1442{
1443 debugfs_remove_recursive(pctldev->device_root);
1444}
1445
Linus Walleij2744e8a2011-05-02 20:50:54 +02001446static void pinctrl_init_debugfs(void)
1447{
1448 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1449 if (IS_ERR(debugfs_root) || !debugfs_root) {
1450 pr_warn("failed to create debugfs directory\n");
1451 debugfs_root = NULL;
1452 return;
1453 }
1454
1455 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1456 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001457 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1458 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001459 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1460 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001461}
1462
1463#else /* CONFIG_DEBUG_FS */
1464
1465static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1466{
1467}
1468
1469static void pinctrl_init_debugfs(void)
1470{
1471}
1472
Tony Lindgren02157162012-01-20 08:17:22 -08001473static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1474{
1475}
1476
Linus Walleij2744e8a2011-05-02 20:50:54 +02001477#endif
1478
Stephen Warrend26bc492012-03-16 14:54:25 -06001479static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1480{
1481 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1482
1483 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301484 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001485 !ops->get_group_name ||
1486 !ops->get_group_pins)
1487 return -EINVAL;
1488
Stephen Warren57291ce2012-03-23 10:29:46 -06001489 if (ops->dt_node_to_map && !ops->dt_free_map)
1490 return -EINVAL;
1491
Stephen Warrend26bc492012-03-16 14:54:25 -06001492 return 0;
1493}
1494
Linus Walleij2744e8a2011-05-02 20:50:54 +02001495/**
1496 * pinctrl_register() - register a pin controller device
1497 * @pctldesc: descriptor for this pin controller
1498 * @dev: parent device for this pin controller
1499 * @driver_data: private pin controller data for this pin controller
1500 */
1501struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1502 struct device *dev, void *driver_data)
1503{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001504 struct pinctrl_dev *pctldev;
1505 int ret;
1506
Devendra Nagada9aecb2012-06-16 23:43:16 +05301507 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001508 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301509 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001510 return NULL;
1511
Stephen Warren02f5b982012-02-22 14:26:00 -07001512 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001513 if (pctldev == NULL) {
1514 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001515 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001516 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001517
1518 /* Initialize pin control device struct */
1519 pctldev->owner = pctldesc->owner;
1520 pctldev->desc = pctldesc;
1521 pctldev->driver_data = driver_data;
1522 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001523 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001524 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001525
Stephen Warrend26bc492012-03-16 14:54:25 -06001526 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301527 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001528 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001529 goto out_err;
1530 }
1531
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001532 /* If we're implementing pinmuxing, check the ops for sanity */
1533 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301534 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001535 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001536 }
1537
1538 /* If we're implementing pinconfig, check the ops for sanity */
1539 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301540 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001541 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001542 }
1543
Linus Walleij2744e8a2011-05-02 20:50:54 +02001544 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001545 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001546 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1547 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001548 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001549 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1550 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001551 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001552 }
1553
Stephen Warren57b676f2012-03-02 13:05:44 -07001554 mutex_lock(&pinctrl_mutex);
1555
Stephen Warren8b9c1392012-02-19 23:45:42 -07001556 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001557
Stephen Warren6e5e9592012-03-02 13:05:47 -07001558 pctldev->p = pinctrl_get_locked(pctldev->dev);
1559 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001560 pctldev->hog_default =
Stephen Warren6e5e9592012-03-02 13:05:47 -07001561 pinctrl_lookup_state_locked(pctldev->p,
1562 PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001563 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001564 dev_dbg(dev, "failed to lookup the default state\n");
1565 } else {
Julien Delacou840a47b2012-12-10 14:47:33 +01001566 if (pinctrl_select_state_locked(pctldev->p,
1567 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001568 dev_err(dev,
1569 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001570 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001571
1572 pctldev->hog_sleep =
1573 pinctrl_lookup_state_locked(pctldev->p,
1574 PINCTRL_STATE_SLEEP);
1575 if (IS_ERR(pctldev->hog_sleep))
1576 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001577 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001578
1579 mutex_unlock(&pinctrl_mutex);
1580
Stephen Warren2304b472012-02-22 14:26:01 -07001581 pinctrl_init_device_debugfs(pctldev);
1582
Linus Walleij2744e8a2011-05-02 20:50:54 +02001583 return pctldev;
1584
Stephen Warren51cd24e2011-12-09 16:59:05 -07001585out_err:
1586 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001587 return NULL;
1588}
1589EXPORT_SYMBOL_GPL(pinctrl_register);
1590
1591/**
1592 * pinctrl_unregister() - unregister pinmux
1593 * @pctldev: pin controller to unregister
1594 *
1595 * Called by pinmux drivers to unregister a pinmux.
1596 */
1597void pinctrl_unregister(struct pinctrl_dev *pctldev)
1598{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001599 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001600 if (pctldev == NULL)
1601 return;
1602
Tony Lindgren02157162012-01-20 08:17:22 -08001603 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001604
1605 mutex_lock(&pinctrl_mutex);
1606
Stephen Warren6e5e9592012-03-02 13:05:47 -07001607 if (!IS_ERR(pctldev->p))
1608 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001609
Linus Walleij2744e8a2011-05-02 20:50:54 +02001610 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001611 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001612 /* Destroy descriptor tree */
1613 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1614 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001615 /* remove gpio ranges map */
1616 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1617 list_del(&range->node);
1618
Stephen Warren51cd24e2011-12-09 16:59:05 -07001619 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001620
1621 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001622}
1623EXPORT_SYMBOL_GPL(pinctrl_unregister);
1624
1625static int __init pinctrl_init(void)
1626{
1627 pr_info("initialized pinctrl subsystem\n");
1628 pinctrl_init_debugfs();
1629 return 0;
1630}
1631
1632/* init early since many drivers really need to initialized pinmux early */
1633core_initcall(pinctrl_init);