blob: 09f79f251800ef0f3cc159c108c9becf1d393fff [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>
Haojian Zhuang51e13c22013-02-17 19:42:50 +080030#include <asm-generic/gpio.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020031#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060032#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020033#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020034#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020035
Stephen Warrenb2b3e662012-02-19 23:45:43 -070036
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080037static bool pinctrl_dummy_state;
38
Stephen Warren57b676f2012-03-02 13:05:44 -070039/* Mutex taken by all entry points */
40DEFINE_MUTEX(pinctrl_mutex);
41
42/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060043LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020044
Stephen Warren57b676f2012-03-02 13:05:44 -070045/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010046static LIST_HEAD(pinctrl_list);
47
Stephen Warren57b676f2012-03-02 13:05:44 -070048/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010049LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070050
Linus Walleijbefe5bd2012-02-09 19:47:48 +010051
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080052/**
53 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
54 *
55 * Usually this function is called by platforms without pinctrl driver support
56 * but run with some shared drivers using pinctrl APIs.
57 * After calling this function, the pinctrl core will return successfully
58 * with creating a dummy state for the driver to keep going smoothly.
59 */
60void pinctrl_provide_dummies(void)
61{
62 pinctrl_dummy_state = true;
63}
64
Linus Walleij2744e8a2011-05-02 20:50:54 +020065const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
66{
67 /* We're not allowed to register devices without name */
68 return pctldev->desc->name;
69}
70EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
71
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080072const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
73{
74 return dev_name(pctldev->dev);
75}
76EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
77
Linus Walleij2744e8a2011-05-02 20:50:54 +020078void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
79{
80 return pctldev->driver_data;
81}
82EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
83
84/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010085 * get_pinctrl_dev_from_devname() - look up pin controller device
86 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020087 *
88 * Looks up a pin control device matching a certain device name or pure device
89 * pointer, the pure device pointer will take precedence.
90 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010091struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020092{
93 struct pinctrl_dev *pctldev = NULL;
94 bool found = false;
95
Linus Walleij9dfac4f2012-02-01 18:02:47 +010096 if (!devname)
97 return NULL;
98
Linus Walleij2744e8a2011-05-02 20:50:54 +020099 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100100 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101 /* Matched on device name */
102 found = true;
103 break;
104 }
105 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200106
107 return found ? pctldev : NULL;
108}
109
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200111 * pin_get_from_name() - look up a pin number from a name
112 * @pctldev: the pin control device to lookup the pin on
113 * @name: the name of the pin to look up
114 */
115int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
116{
Chanho Park706e8522012-01-03 16:47:50 +0900117 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200118
Chanho Park706e8522012-01-03 16:47:50 +0900119 /* The pin number can be retrived from the pin controller descriptor */
120 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200121 struct pin_desc *desc;
122
Chanho Park706e8522012-01-03 16:47:50 +0900123 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200124 desc = pin_desc_get(pctldev, pin);
125 /* Pin space may be sparse */
126 if (desc == NULL)
127 continue;
128 if (desc->name && !strcmp(name, desc->name))
129 return pin;
130 }
131
132 return -EINVAL;
133}
134
135/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800136 * pin_get_name_from_id() - look up a pin name from a pin id
137 * @pctldev: the pin control device to lookup the pin on
138 * @name: the name of the pin to look up
139 */
140const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
141{
142 const struct pin_desc *desc;
143
144 desc = pin_desc_get(pctldev, pin);
145 if (desc == NULL) {
146 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
147 pin);
148 return NULL;
149 }
150
151 return desc->name;
152}
153
154/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200155 * pin_is_valid() - check if pin exists on controller
156 * @pctldev: the pin control device to check the pin on
157 * @pin: pin to check, use the local pin controller index number
158 *
159 * This tells us whether a certain pin exist on a certain pin controller or
160 * not. Pin lists may be sparse, so some pins may not exist.
161 */
162bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
163{
164 struct pin_desc *pindesc;
165
166 if (pin < 0)
167 return false;
168
Stephen Warren57b676f2012-03-02 13:05:44 -0700169 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200170 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700171 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200172
Stephen Warren57b676f2012-03-02 13:05:44 -0700173 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200174}
175EXPORT_SYMBOL_GPL(pin_is_valid);
176
177/* Deletes a range of pin descriptors */
178static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
179 const struct pinctrl_pin_desc *pins,
180 unsigned num_pins)
181{
182 int i;
183
Linus Walleij2744e8a2011-05-02 20:50:54 +0200184 for (i = 0; i < num_pins; i++) {
185 struct pin_desc *pindesc;
186
187 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
188 pins[i].number);
189 if (pindesc != NULL) {
190 radix_tree_delete(&pctldev->pin_desc_tree,
191 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100192 if (pindesc->dynamic_name)
193 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200194 }
195 kfree(pindesc);
196 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200197}
198
199static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
200 unsigned number, const char *name)
201{
202 struct pin_desc *pindesc;
203
204 pindesc = pin_desc_get(pctldev, number);
205 if (pindesc != NULL) {
206 pr_err("pin %d already registered on %s\n", number,
207 pctldev->desc->name);
208 return -EINVAL;
209 }
210
211 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700212 if (pindesc == NULL) {
213 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200214 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700215 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200216
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217 /* Set owner */
218 pindesc->pctldev = pctldev;
219
Stephen Warren9af1e442011-10-19 16:19:27 -0600220 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100221 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100222 pindesc->name = name;
223 } else {
224 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530225 if (pindesc->name == NULL) {
226 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100227 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530228 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100229 pindesc->dynamic_name = true;
230 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100234 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235 return 0;
236}
237
238static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
239 struct pinctrl_pin_desc const *pins,
240 unsigned num_descs)
241{
242 unsigned i;
243 int ret = 0;
244
245 for (i = 0; i < num_descs; i++) {
246 ret = pinctrl_register_one_pin(pctldev,
247 pins[i].number, pins[i].name);
248 if (ret)
249 return ret;
250 }
251
252 return 0;
253}
254
255/**
256 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
257 * @pctldev: pin controller device to check
258 * @gpio: gpio pin to check taken from the global GPIO pin space
259 *
260 * Tries to match a GPIO pin number to the ranges handled by a certain pin
261 * controller, return the range or NULL
262 */
263static struct pinctrl_gpio_range *
264pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
265{
266 struct pinctrl_gpio_range *range = NULL;
267
268 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200269 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
270 /* Check if we're in the valid range */
271 if (gpio >= range->base &&
272 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200273 return range;
274 }
275 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200276
277 return NULL;
278}
279
280/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800281 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
282 * the same GPIO chip are in range
283 * @gpio: gpio pin to check taken from the global GPIO pin space
284 *
285 * This function is complement of pinctrl_match_gpio_range(). If the return
286 * value of pinctrl_match_gpio_range() is NULL, this function could be used
287 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
288 * of the same GPIO chip don't have back-end pinctrl interface.
289 * If the return value is true, it means that pinctrl device is ready & the
290 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
291 * is false, it means that pinctrl device may not be ready.
292 */
293static bool pinctrl_ready_for_gpio_range(unsigned gpio)
294{
295 struct pinctrl_dev *pctldev;
296 struct pinctrl_gpio_range *range = NULL;
297 struct gpio_chip *chip = gpio_to_chip(gpio);
298
299 /* Loop over the pin controllers */
300 list_for_each_entry(pctldev, &pinctrldev_list, node) {
301 /* Loop over the ranges */
302 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
303 /* Check if any gpio range overlapped with gpio chip */
304 if (range->base + range->npins - 1 < chip->base ||
305 range->base > chip->base + chip->ngpio - 1)
306 continue;
307 return true;
308 }
309 }
310 return false;
311}
312
313/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200314 * pinctrl_get_device_gpio_range() - find device for GPIO range
315 * @gpio: the pin to locate the pin controller for
316 * @outdev: the pin control device if found
317 * @outrange: the GPIO range if found
318 *
319 * Find the pin controller handling a certain GPIO pin from the pinspace of
320 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800321 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
322 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700324static int pinctrl_get_device_gpio_range(unsigned gpio,
325 struct pinctrl_dev **outdev,
326 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200327{
328 struct pinctrl_dev *pctldev = NULL;
329
330 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331 list_for_each_entry(pctldev, &pinctrldev_list, node) {
332 struct pinctrl_gpio_range *range;
333
334 range = pinctrl_match_gpio_range(pctldev, gpio);
335 if (range != NULL) {
336 *outdev = pctldev;
337 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200338 return 0;
339 }
340 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200341
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800342 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200343}
344
345/**
346 * pinctrl_add_gpio_range() - register a GPIO range for a controller
347 * @pctldev: pin controller device to add the range to
348 * @range: the GPIO range to add
349 *
350 * This adds a range of GPIOs to be handled by a certain pin controller. Call
351 * this to register handled ranges after registering your pin controller.
352 */
353void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
354 struct pinctrl_gpio_range *range)
355{
Stephen Warren57b676f2012-03-02 13:05:44 -0700356 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700357 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700358 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200359}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700360EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200361
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800362void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
363 struct pinctrl_gpio_range *ranges,
364 unsigned nranges)
365{
366 int i;
367
368 for (i = 0; i < nranges; i++)
369 pinctrl_add_gpio_range(pctldev, &ranges[i]);
370}
371EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
372
Linus Walleij192c3692012-11-20 14:03:37 +0100373struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530374 struct pinctrl_gpio_range *range)
375{
376 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
377
Linus Walleijdfa97512012-11-20 14:54:18 +0100378 /*
379 * If we can't find this device, let's assume that is because
380 * it has not probed yet, so the driver trying to register this
381 * range need to defer probing.
382 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530383 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100384 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530385
386 pinctrl_add_gpio_range(pctldev, range);
387 return pctldev;
388}
Linus Walleij192c3692012-11-20 14:03:37 +0100389EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530390
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100392 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
393 * @pctldev: the pin controller device to look in
394 * @pin: a controller-local number to find the range for
395 */
396struct pinctrl_gpio_range *
397pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
398 unsigned int pin)
399{
400 struct pinctrl_gpio_range *range = NULL;
401
402 /* Loop over the ranges */
403 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
404 /* Check if we're in the valid range */
405 if (pin >= range->pin_base &&
406 pin < range->pin_base + range->npins) {
407 return range;
408 }
409 }
410
411 return NULL;
412}
413EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
414
415/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530416 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
417 * @pctldev: pin controller device to remove the range from
418 * @range: the GPIO range to remove
419 */
420void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
421 struct pinctrl_gpio_range *range)
422{
423 mutex_lock(&pinctrl_mutex);
424 list_del(&range->node);
425 mutex_unlock(&pinctrl_mutex);
426}
427EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
428
429/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200430 * pinctrl_get_group_selector() - returns the group selector for a group
431 * @pctldev: the pin controller handling the group
432 * @pin_group: the pin group to look up
433 */
434int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
435 const char *pin_group)
436{
437 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530438 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200439 unsigned group_selector = 0;
440
Viresh Kumard1e90e92012-03-30 11:25:40 +0530441 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200442 const char *gname = pctlops->get_group_name(pctldev,
443 group_selector);
444 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700445 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200446 "found group selector %u for %s\n",
447 group_selector,
448 pin_group);
449 return group_selector;
450 }
451
452 group_selector++;
453 }
454
Stephen Warren51cd24e2011-12-09 16:59:05 -0700455 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200456 pin_group);
457
458 return -EINVAL;
459}
460
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100461/**
462 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
463 * @gpio: the GPIO pin number from the GPIO subsystem number space
464 *
465 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
466 * as part of their gpio_request() semantics, platforms and individual drivers
467 * shall *NOT* request GPIO pins to be muxed in.
468 */
469int pinctrl_request_gpio(unsigned gpio)
470{
471 struct pinctrl_dev *pctldev;
472 struct pinctrl_gpio_range *range;
473 int ret;
474 int pin;
475
Stephen Warren57b676f2012-03-02 13:05:44 -0700476 mutex_lock(&pinctrl_mutex);
477
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100478 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700479 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800480 if (pinctrl_ready_for_gpio_range(gpio))
481 ret = 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700482 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800483 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700484 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100485
486 /* Convert to the pin controllers number space */
487 pin = gpio - range->base + range->pin_base;
488
Stephen Warren57b676f2012-03-02 13:05:44 -0700489 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
490
491 mutex_unlock(&pinctrl_mutex);
492 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100493}
494EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
495
496/**
497 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
498 * @gpio: the GPIO pin number from the GPIO subsystem number space
499 *
500 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
501 * as part of their gpio_free() semantics, platforms and individual drivers
502 * shall *NOT* request GPIO pins to be muxed out.
503 */
504void pinctrl_free_gpio(unsigned gpio)
505{
506 struct pinctrl_dev *pctldev;
507 struct pinctrl_gpio_range *range;
508 int ret;
509 int pin;
510
Stephen Warren57b676f2012-03-02 13:05:44 -0700511 mutex_lock(&pinctrl_mutex);
512
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100513 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700514 if (ret) {
515 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100516 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700517 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100518
519 /* Convert to the pin controllers number space */
520 pin = gpio - range->base + range->pin_base;
521
Stephen Warren57b676f2012-03-02 13:05:44 -0700522 pinmux_free_gpio(pctldev, pin, range);
523
524 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100525}
526EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
527
528static int pinctrl_gpio_direction(unsigned gpio, bool input)
529{
530 struct pinctrl_dev *pctldev;
531 struct pinctrl_gpio_range *range;
532 int ret;
533 int pin;
534
535 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
536 if (ret)
537 return ret;
538
539 /* Convert to the pin controllers number space */
540 pin = gpio - range->base + range->pin_base;
541
542 return pinmux_gpio_direction(pctldev, range, pin, input);
543}
544
545/**
546 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
547 * @gpio: the GPIO pin number from the GPIO subsystem number space
548 *
549 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
550 * as part of their gpio_direction_input() semantics, platforms and individual
551 * drivers shall *NOT* touch pin control GPIO calls.
552 */
553int pinctrl_gpio_direction_input(unsigned gpio)
554{
Stephen Warren57b676f2012-03-02 13:05:44 -0700555 int ret;
556 mutex_lock(&pinctrl_mutex);
557 ret = pinctrl_gpio_direction(gpio, true);
558 mutex_unlock(&pinctrl_mutex);
559 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100560}
561EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
562
563/**
564 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
565 * @gpio: the GPIO pin number from the GPIO subsystem number space
566 *
567 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
568 * as part of their gpio_direction_output() semantics, platforms and individual
569 * drivers shall *NOT* touch pin control GPIO calls.
570 */
571int pinctrl_gpio_direction_output(unsigned gpio)
572{
Stephen Warren57b676f2012-03-02 13:05:44 -0700573 int ret;
574 mutex_lock(&pinctrl_mutex);
575 ret = pinctrl_gpio_direction(gpio, false);
576 mutex_unlock(&pinctrl_mutex);
577 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100578}
579EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
580
Stephen Warren6e5e9592012-03-02 13:05:47 -0700581static struct pinctrl_state *find_state(struct pinctrl *p,
582 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700584 struct pinctrl_state *state;
585
586 list_for_each_entry(state, &p->states, node)
587 if (!strcmp(state->name, name))
588 return state;
589
590 return NULL;
591}
592
593static struct pinctrl_state *create_state(struct pinctrl *p,
594 const char *name)
595{
596 struct pinctrl_state *state;
597
598 state = kzalloc(sizeof(*state), GFP_KERNEL);
599 if (state == NULL) {
600 dev_err(p->dev,
601 "failed to alloc struct pinctrl_state\n");
602 return ERR_PTR(-ENOMEM);
603 }
604
605 state->name = name;
606 INIT_LIST_HEAD(&state->settings);
607
608 list_add_tail(&state->node, &p->states);
609
610 return state;
611}
612
613static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
614{
615 struct pinctrl_state *state;
616 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700617 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700618
619 state = find_state(p, map->name);
620 if (!state)
621 state = create_state(p, map->name);
622 if (IS_ERR(state))
623 return PTR_ERR(state);
624
Stephen Warren1e2082b2012-03-02 13:05:48 -0700625 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
626 return 0;
627
Stephen Warren6e5e9592012-03-02 13:05:47 -0700628 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
629 if (setting == NULL) {
630 dev_err(p->dev,
631 "failed to alloc struct pinctrl_setting\n");
632 return -ENOMEM;
633 }
634
Stephen Warren1e2082b2012-03-02 13:05:48 -0700635 setting->type = map->type;
636
Stephen Warren6e5e9592012-03-02 13:05:47 -0700637 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
638 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700639 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100640 /* Do not defer probing of hogs (circular loop) */
641 if (!strcmp(map->ctrl_dev_name, map->dev_name))
642 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200643 /*
644 * OK let us guess that the driver is not there yet, and
645 * let's defer obtaining this pinctrl handle to later...
646 */
Linus Walleij89216492012-12-11 14:14:32 +0100647 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
648 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200649 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700650 }
651
Linus Walleij1a789582012-10-17 20:51:54 +0200652 setting->dev_name = map->dev_name;
653
Stephen Warren1e2082b2012-03-02 13:05:48 -0700654 switch (map->type) {
655 case PIN_MAP_TYPE_MUX_GROUP:
656 ret = pinmux_map_to_setting(map, setting);
657 break;
658 case PIN_MAP_TYPE_CONFIGS_PIN:
659 case PIN_MAP_TYPE_CONFIGS_GROUP:
660 ret = pinconf_map_to_setting(map, setting);
661 break;
662 default:
663 ret = -EINVAL;
664 break;
665 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700666 if (ret < 0) {
667 kfree(setting);
668 return ret;
669 }
670
671 list_add_tail(&setting->node, &state->settings);
672
673 return 0;
674}
675
676static struct pinctrl *find_pinctrl(struct device *dev)
677{
678 struct pinctrl *p;
679
Stephen Warren1e2082b2012-03-02 13:05:48 -0700680 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700681 if (p->dev == dev)
682 return p;
683
684 return NULL;
685}
686
687static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
688
689static struct pinctrl *create_pinctrl(struct device *dev)
690{
691 struct pinctrl *p;
692 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700693 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100694 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700695 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100697
698 /*
699 * create the state cookie holder struct pinctrl for each
700 * mapping, this is what consumers will get when requesting
701 * a pin control handle with pinctrl_get()
702 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700703 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700704 if (p == NULL) {
705 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100706 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700707 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700708 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700709 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600710 INIT_LIST_HEAD(&p->dt_maps);
711
712 ret = pinctrl_dt_to_map(p);
713 if (ret < 0) {
714 kfree(p);
715 return ERR_PTR(ret);
716 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700717
718 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100719
720 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700721 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700722 /* Map must be for this device */
723 if (strcmp(map->dev_name, devname))
724 continue;
725
Stephen Warren6e5e9592012-03-02 13:05:47 -0700726 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100727 /*
728 * At this point the adding of a setting may:
729 *
730 * - Defer, if the pinctrl device is not yet available
731 * - Fail, if the pinctrl device is not yet available,
732 * AND the setting is a hog. We cannot defer that, since
733 * the hog will kick in immediately after the device
734 * is registered.
735 *
736 * If the error returned was not -EPROBE_DEFER then we
737 * accumulate the errors to see if we end up with
738 * an -EPROBE_DEFER later, as that is the worst case.
739 */
740 if (ret == -EPROBE_DEFER) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700741 pinctrl_put_locked(p, false);
742 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100743 }
744 }
Linus Walleij89216492012-12-11 14:14:32 +0100745 if (ret < 0) {
746 /* If some other error than deferral occured, return here */
747 pinctrl_put_locked(p, false);
748 return ERR_PTR(ret);
749 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100750
Linus Walleijab780292013-01-22 10:56:14 -0700751 kref_init(&p->users);
752
Linus Walleijb0666ba2012-12-11 13:12:35 +0100753 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700754 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100755
756 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700757}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700758
Stephen Warren6e5e9592012-03-02 13:05:47 -0700759static struct pinctrl *pinctrl_get_locked(struct device *dev)
760{
761 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700762
Stephen Warren6e5e9592012-03-02 13:05:47 -0700763 if (WARN_ON(!dev))
764 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700765
Linus Walleijab780292013-01-22 10:56:14 -0700766 /*
767 * See if somebody else (such as the device core) has already
768 * obtained a handle to the pinctrl for this device. In that case,
769 * return another pointer to it.
770 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700771 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700772 if (p != NULL) {
773 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
774 kref_get(&p->users);
775 return p;
776 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700777
Richard Genoudd599bfb2012-08-10 16:53:49 +0200778 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100779}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700780
781/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700782 * pinctrl_get() - retrieves the pinctrl handle for a device
783 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700784 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700786{
787 struct pinctrl *p;
788
Stephen Warren57b676f2012-03-02 13:05:44 -0700789 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700790 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700791 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700792
793 return p;
794}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795EXPORT_SYMBOL_GPL(pinctrl_get);
796
Richard Genoudd3cee8302013-03-25 15:47:21 +0100797static void pinctrl_free_setting(bool disable_setting,
798 struct pinctrl_setting *setting)
799{
800 switch (setting->type) {
801 case PIN_MAP_TYPE_MUX_GROUP:
802 if (disable_setting)
803 pinmux_disable_setting(setting);
804 pinmux_free_setting(setting);
805 break;
806 case PIN_MAP_TYPE_CONFIGS_PIN:
807 case PIN_MAP_TYPE_CONFIGS_GROUP:
808 pinconf_free_setting(setting);
809 break;
810 default:
811 break;
812 }
813}
814
Stephen Warren6e5e9592012-03-02 13:05:47 -0700815static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700816{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700817 struct pinctrl_state *state, *n1;
818 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700819
Stephen Warren6e5e9592012-03-02 13:05:47 -0700820 list_for_each_entry_safe(state, n1, &p->states, node) {
821 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100822 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700823 list_del(&setting->node);
824 kfree(setting);
825 }
826 list_del(&state->node);
827 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700828 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700829
Stephen Warren57291ce2012-03-23 10:29:46 -0600830 pinctrl_dt_free_maps(p);
831
Stephen Warren6e5e9592012-03-02 13:05:47 -0700832 if (inlist)
833 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700834 kfree(p);
835}
836
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100837/**
Linus Walleijab780292013-01-22 10:56:14 -0700838 * pinctrl_release() - release the pinctrl handle
839 * @kref: the kref in the pinctrl being released
840 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530841static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700842{
843 struct pinctrl *p = container_of(kref, struct pinctrl, users);
844
845 pinctrl_put_locked(p, true);
846}
847
848/**
849 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700850 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100851 */
852void pinctrl_put(struct pinctrl *p)
853{
Stephen Warren57b676f2012-03-02 13:05:44 -0700854 mutex_lock(&pinctrl_mutex);
Linus Walleijab780292013-01-22 10:56:14 -0700855 kref_put(&p->users, pinctrl_release);
Stephen Warren57b676f2012-03-02 13:05:44 -0700856 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100857}
858EXPORT_SYMBOL_GPL(pinctrl_put);
859
Stephen Warren6e5e9592012-03-02 13:05:47 -0700860static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
861 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700862{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700863 struct pinctrl_state *state;
864
865 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800866 if (!state) {
867 if (pinctrl_dummy_state) {
868 /* create dummy state */
869 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
870 name);
871 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200872 } else
873 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800874 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700875
876 return state;
877}
878
879/**
880 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
881 * @p: the pinctrl handle to retrieve the state from
882 * @name: the state name to retrieve
883 */
884struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
885{
886 struct pinctrl_state *s;
887
888 mutex_lock(&pinctrl_mutex);
889 s = pinctrl_lookup_state_locked(p, name);
890 mutex_unlock(&pinctrl_mutex);
891
892 return s;
893}
894EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
895
896static int pinctrl_select_state_locked(struct pinctrl *p,
897 struct pinctrl_state *state)
898{
899 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700900 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700901
Stephen Warren6e5e9592012-03-02 13:05:47 -0700902 if (p->state == state)
903 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700904
Stephen Warren6e5e9592012-03-02 13:05:47 -0700905 if (p->state) {
906 /*
907 * The set of groups with a mux configuration in the old state
908 * may not be identical to the set of groups with a mux setting
909 * in the new state. While this might be unusual, it's entirely
910 * possible for the "user"-supplied mapping table to be written
911 * that way. For each group that was configured in the old state
912 * but not in the new state, this code puts that group into a
913 * safe/disabled state.
914 */
915 list_for_each_entry(setting, &p->state->settings, node) {
916 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700917 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
918 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700919 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700920 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
921 continue;
922 if (setting2->data.mux.group ==
923 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700924 found = true;
925 break;
926 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700927 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928 if (!found)
929 pinmux_disable_setting(setting);
930 }
931 }
932
Richard Genoud3102a762013-03-25 15:47:22 +0100933 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700934
935 /* Apply all the settings for the new state */
936 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700937 switch (setting->type) {
938 case PIN_MAP_TYPE_MUX_GROUP:
939 ret = pinmux_enable_setting(setting);
940 break;
941 case PIN_MAP_TYPE_CONFIGS_PIN:
942 case PIN_MAP_TYPE_CONFIGS_GROUP:
943 ret = pinconf_apply_setting(setting);
944 break;
945 default:
946 ret = -EINVAL;
947 break;
948 }
Richard Genoud3102a762013-03-25 15:47:22 +0100949
Stephen Warren6e5e9592012-03-02 13:05:47 -0700950 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +0100951 goto unapply_new_state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700952 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700953 }
954
Richard Genoud3102a762013-03-25 15:47:22 +0100955 p->state = state;
956
Stephen Warren7ecdb162012-03-02 13:05:45 -0700957 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +0100958
959unapply_new_state:
960 pr_info("Error applying setting, reverse things back\n");
961
962 /*
963 * If the loop stopped on the 1st entry, nothing has been enabled,
964 * so jump directly to the 2nd phase
965 */
966 if (list_entry(&setting->node, typeof(*setting), node) ==
967 list_first_entry(&state->settings, typeof(*setting), node))
968 goto reapply_old_state;
969
970 list_for_each_entry(setting2, &state->settings, node) {
971 if (&setting2->node == &setting->node)
972 break;
973 pinctrl_free_setting(true, setting2);
974 }
975reapply_old_state:
976 /* FIXME: re-enable old setting */
977 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700978}
979
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100980/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700981 * pinctrl_select() - select/activate/program a pinctrl state to HW
982 * @p: the pinctrl handle for the device that requests configuratio
983 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100984 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700985int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100986{
Stephen Warren57b676f2012-03-02 13:05:44 -0700987 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700988
Stephen Warren57b676f2012-03-02 13:05:44 -0700989 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700990 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700991 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700992
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100993 return ret;
994}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700995EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100996
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600997static void devm_pinctrl_release(struct device *dev, void *res)
998{
999 pinctrl_put(*(struct pinctrl **)res);
1000}
1001
1002/**
1003 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1004 * @dev: the device to obtain the handle for
1005 *
1006 * If there is a need to explicitly destroy the returned struct pinctrl,
1007 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1008 */
1009struct pinctrl *devm_pinctrl_get(struct device *dev)
1010{
1011 struct pinctrl **ptr, *p;
1012
1013 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1014 if (!ptr)
1015 return ERR_PTR(-ENOMEM);
1016
1017 p = pinctrl_get(dev);
1018 if (!IS_ERR(p)) {
1019 *ptr = p;
1020 devres_add(dev, ptr);
1021 } else {
1022 devres_free(ptr);
1023 }
1024
1025 return p;
1026}
1027EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1028
1029static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1030{
1031 struct pinctrl **p = res;
1032
1033 return *p == data;
1034}
1035
1036/**
1037 * devm_pinctrl_put() - Resource managed pinctrl_put()
1038 * @p: the pinctrl handle to release
1039 *
1040 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1041 * this function will not need to be called and the resource management
1042 * code will ensure that the resource is freed.
1043 */
1044void devm_pinctrl_put(struct pinctrl *p)
1045{
Jingoo Hana72149e2013-02-26 11:34:07 +09001046 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001047 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001048}
1049EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1050
Stephen Warren57291ce2012-03-23 10:29:46 -06001051int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1052 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001053{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001054 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001055 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001056
1057 pr_debug("add %d pinmux maps\n", num_maps);
1058
1059 /* First sanity check the new mapping */
1060 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001061 if (!maps[i].dev_name) {
1062 pr_err("failed to register map %s (%d): no device given\n",
1063 maps[i].name, i);
1064 return -EINVAL;
1065 }
1066
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001067 if (!maps[i].name) {
1068 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001069 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001070 return -EINVAL;
1071 }
1072
Stephen Warren1e2082b2012-03-02 13:05:48 -07001073 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1074 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001075 pr_err("failed to register map %s (%d): no pin control device given\n",
1076 maps[i].name, i);
1077 return -EINVAL;
1078 }
1079
Stephen Warren1e2082b2012-03-02 13:05:48 -07001080 switch (maps[i].type) {
1081 case PIN_MAP_TYPE_DUMMY_STATE:
1082 break;
1083 case PIN_MAP_TYPE_MUX_GROUP:
1084 ret = pinmux_validate_map(&maps[i], i);
1085 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001086 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001087 break;
1088 case PIN_MAP_TYPE_CONFIGS_PIN:
1089 case PIN_MAP_TYPE_CONFIGS_GROUP:
1090 ret = pinconf_validate_map(&maps[i], i);
1091 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001092 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001093 break;
1094 default:
1095 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001096 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001097 return -EINVAL;
1098 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001099 }
1100
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001101 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1102 if (!maps_node) {
1103 pr_err("failed to alloc struct pinctrl_maps\n");
1104 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001105 }
1106
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001107 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001108 if (dup) {
1109 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1110 GFP_KERNEL);
1111 if (!maps_node->maps) {
1112 pr_err("failed to duplicate mapping table\n");
1113 kfree(maps_node);
1114 return -ENOMEM;
1115 }
1116 } else {
1117 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001118 }
1119
Stephen Warren57291ce2012-03-23 10:29:46 -06001120 if (!locked)
1121 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001122 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001123 if (!locked)
1124 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001125
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001126 return 0;
1127}
1128
Stephen Warren57291ce2012-03-23 10:29:46 -06001129/**
1130 * pinctrl_register_mappings() - register a set of pin controller mappings
1131 * @maps: the pincontrol mappings table to register. This should probably be
1132 * marked with __initdata so it can be discarded after boot. This
1133 * function will perform a shallow copy for the mapping entries.
1134 * @num_maps: the number of maps in the mapping table
1135 */
1136int pinctrl_register_mappings(struct pinctrl_map const *maps,
1137 unsigned num_maps)
1138{
1139 return pinctrl_register_map(maps, num_maps, true, false);
1140}
1141
1142void pinctrl_unregister_map(struct pinctrl_map const *map)
1143{
1144 struct pinctrl_maps *maps_node;
1145
1146 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1147 if (maps_node->maps == map) {
1148 list_del(&maps_node->node);
1149 return;
1150 }
1151 }
1152}
1153
Julien Delacou840a47b2012-12-10 14:47:33 +01001154/**
1155 * pinctrl_force_sleep() - turn a given controller device into sleep state
1156 * @pctldev: pin controller device
1157 */
1158int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1159{
1160 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1161 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1162 return 0;
1163}
1164EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1165
1166/**
1167 * pinctrl_force_default() - turn a given controller device into default state
1168 * @pctldev: pin controller device
1169 */
1170int pinctrl_force_default(struct pinctrl_dev *pctldev)
1171{
1172 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1173 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1174 return 0;
1175}
1176EXPORT_SYMBOL_GPL(pinctrl_force_default);
1177
Linus Walleij2744e8a2011-05-02 20:50:54 +02001178#ifdef CONFIG_DEBUG_FS
1179
1180static int pinctrl_pins_show(struct seq_file *s, void *what)
1181{
1182 struct pinctrl_dev *pctldev = s->private;
1183 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001184 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001185
1186 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001187
Stephen Warren57b676f2012-03-02 13:05:44 -07001188 mutex_lock(&pinctrl_mutex);
1189
Chanho Park706e8522012-01-03 16:47:50 +09001190 /* The pin number can be retrived from the pin controller descriptor */
1191 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001192 struct pin_desc *desc;
1193
Chanho Park706e8522012-01-03 16:47:50 +09001194 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001195 desc = pin_desc_get(pctldev, pin);
1196 /* Pin space may be sparse */
1197 if (desc == NULL)
1198 continue;
1199
1200 seq_printf(s, "pin %d (%s) ", pin,
1201 desc->name ? desc->name : "unnamed");
1202
1203 /* Driver-specific info per pin */
1204 if (ops->pin_dbg_show)
1205 ops->pin_dbg_show(pctldev, s, pin);
1206
1207 seq_puts(s, "\n");
1208 }
1209
Stephen Warren57b676f2012-03-02 13:05:44 -07001210 mutex_unlock(&pinctrl_mutex);
1211
Linus Walleij2744e8a2011-05-02 20:50:54 +02001212 return 0;
1213}
1214
1215static int pinctrl_groups_show(struct seq_file *s, void *what)
1216{
1217 struct pinctrl_dev *pctldev = s->private;
1218 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301219 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001220
Viresh Kumard1e90e92012-03-30 11:25:40 +05301221 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001222 mutex_lock(&pinctrl_mutex);
1223
Linus Walleij2744e8a2011-05-02 20:50:54 +02001224 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301225 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001226 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001227 unsigned num_pins;
1228 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001229 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001230 int ret;
1231 int i;
1232
1233 ret = ops->get_group_pins(pctldev, selector,
1234 &pins, &num_pins);
1235 if (ret)
1236 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1237 gname);
1238 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001239 seq_printf(s, "group: %s\n", gname);
1240 for (i = 0; i < num_pins; i++) {
1241 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001242 if (WARN_ON(!pname)) {
1243 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001244 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001245 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001246 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1247 }
1248 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001249 }
1250 selector++;
1251 }
1252
Stephen Warren57b676f2012-03-02 13:05:44 -07001253 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001254
1255 return 0;
1256}
1257
1258static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1259{
1260 struct pinctrl_dev *pctldev = s->private;
1261 struct pinctrl_gpio_range *range = NULL;
1262
1263 seq_puts(s, "GPIO ranges handled:\n");
1264
Stephen Warren57b676f2012-03-02 13:05:44 -07001265 mutex_lock(&pinctrl_mutex);
1266
Linus Walleij2744e8a2011-05-02 20:50:54 +02001267 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001268 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001269 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1270 range->id, range->name,
1271 range->base, (range->base + range->npins - 1),
1272 range->pin_base,
1273 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001274 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001275
1276 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001277
1278 return 0;
1279}
1280
1281static int pinctrl_devices_show(struct seq_file *s, void *what)
1282{
1283 struct pinctrl_dev *pctldev;
1284
Linus Walleijae6b4d82011-10-19 18:14:33 +02001285 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001286
1287 mutex_lock(&pinctrl_mutex);
1288
Linus Walleij2744e8a2011-05-02 20:50:54 +02001289 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1290 seq_printf(s, "%s ", pctldev->desc->name);
1291 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001292 seq_puts(s, "yes ");
1293 else
1294 seq_puts(s, "no ");
1295 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001296 seq_puts(s, "yes");
1297 else
1298 seq_puts(s, "no");
1299 seq_puts(s, "\n");
1300 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001301
1302 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001303
1304 return 0;
1305}
1306
Stephen Warren1e2082b2012-03-02 13:05:48 -07001307static inline const char *map_type(enum pinctrl_map_type type)
1308{
1309 static const char * const names[] = {
1310 "INVALID",
1311 "DUMMY_STATE",
1312 "MUX_GROUP",
1313 "CONFIGS_PIN",
1314 "CONFIGS_GROUP",
1315 };
1316
1317 if (type >= ARRAY_SIZE(names))
1318 return "UNKNOWN";
1319
1320 return names[type];
1321}
1322
Stephen Warren3eedb432012-02-23 17:04:40 -07001323static int pinctrl_maps_show(struct seq_file *s, void *what)
1324{
1325 struct pinctrl_maps *maps_node;
1326 int i;
1327 struct pinctrl_map const *map;
1328
1329 seq_puts(s, "Pinctrl maps:\n");
1330
Stephen Warren57b676f2012-03-02 13:05:44 -07001331 mutex_lock(&pinctrl_mutex);
1332
Stephen Warren3eedb432012-02-23 17:04:40 -07001333 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001334 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1335 map->dev_name, map->name, map_type(map->type),
1336 map->type);
1337
1338 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1339 seq_printf(s, "controlling device %s\n",
1340 map->ctrl_dev_name);
1341
1342 switch (map->type) {
1343 case PIN_MAP_TYPE_MUX_GROUP:
1344 pinmux_show_map(s, map);
1345 break;
1346 case PIN_MAP_TYPE_CONFIGS_PIN:
1347 case PIN_MAP_TYPE_CONFIGS_GROUP:
1348 pinconf_show_map(s, map);
1349 break;
1350 default:
1351 break;
1352 }
1353
1354 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001355 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001356
1357 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001358
1359 return 0;
1360}
1361
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001362static int pinctrl_show(struct seq_file *s, void *what)
1363{
1364 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001365 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001366 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001367
1368 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001369
1370 mutex_lock(&pinctrl_mutex);
1371
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001372 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001373 seq_printf(s, "device: %s current state: %s\n",
1374 dev_name(p->dev),
1375 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001376
Stephen Warren6e5e9592012-03-02 13:05:47 -07001377 list_for_each_entry(state, &p->states, node) {
1378 seq_printf(s, " state: %s\n", state->name);
1379
1380 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001381 struct pinctrl_dev *pctldev = setting->pctldev;
1382
1383 seq_printf(s, " type: %s controller %s ",
1384 map_type(setting->type),
1385 pinctrl_dev_get_name(pctldev));
1386
1387 switch (setting->type) {
1388 case PIN_MAP_TYPE_MUX_GROUP:
1389 pinmux_show_setting(s, setting);
1390 break;
1391 case PIN_MAP_TYPE_CONFIGS_PIN:
1392 case PIN_MAP_TYPE_CONFIGS_GROUP:
1393 pinconf_show_setting(s, setting);
1394 break;
1395 default:
1396 break;
1397 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001398 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001399 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001400 }
1401
Stephen Warren57b676f2012-03-02 13:05:44 -07001402 mutex_unlock(&pinctrl_mutex);
1403
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001404 return 0;
1405}
1406
Linus Walleij2744e8a2011-05-02 20:50:54 +02001407static int pinctrl_pins_open(struct inode *inode, struct file *file)
1408{
1409 return single_open(file, pinctrl_pins_show, inode->i_private);
1410}
1411
1412static int pinctrl_groups_open(struct inode *inode, struct file *file)
1413{
1414 return single_open(file, pinctrl_groups_show, inode->i_private);
1415}
1416
1417static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1418{
1419 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1420}
1421
1422static int pinctrl_devices_open(struct inode *inode, struct file *file)
1423{
1424 return single_open(file, pinctrl_devices_show, NULL);
1425}
1426
Stephen Warren3eedb432012-02-23 17:04:40 -07001427static int pinctrl_maps_open(struct inode *inode, struct file *file)
1428{
1429 return single_open(file, pinctrl_maps_show, NULL);
1430}
1431
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001432static int pinctrl_open(struct inode *inode, struct file *file)
1433{
1434 return single_open(file, pinctrl_show, NULL);
1435}
1436
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437static const struct file_operations pinctrl_pins_ops = {
1438 .open = pinctrl_pins_open,
1439 .read = seq_read,
1440 .llseek = seq_lseek,
1441 .release = single_release,
1442};
1443
1444static const struct file_operations pinctrl_groups_ops = {
1445 .open = pinctrl_groups_open,
1446 .read = seq_read,
1447 .llseek = seq_lseek,
1448 .release = single_release,
1449};
1450
1451static const struct file_operations pinctrl_gpioranges_ops = {
1452 .open = pinctrl_gpioranges_open,
1453 .read = seq_read,
1454 .llseek = seq_lseek,
1455 .release = single_release,
1456};
1457
1458static const struct file_operations pinctrl_devices_ops = {
1459 .open = pinctrl_devices_open,
1460 .read = seq_read,
1461 .llseek = seq_lseek,
1462 .release = single_release,
1463};
1464
Stephen Warren3eedb432012-02-23 17:04:40 -07001465static const struct file_operations pinctrl_maps_ops = {
1466 .open = pinctrl_maps_open,
1467 .read = seq_read,
1468 .llseek = seq_lseek,
1469 .release = single_release,
1470};
1471
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001472static const struct file_operations pinctrl_ops = {
1473 .open = pinctrl_open,
1474 .read = seq_read,
1475 .llseek = seq_lseek,
1476 .release = single_release,
1477};
1478
Linus Walleij2744e8a2011-05-02 20:50:54 +02001479static struct dentry *debugfs_root;
1480
1481static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1482{
Tony Lindgren02157162012-01-20 08:17:22 -08001483 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001484
Stephen Warren51cd24e2011-12-09 16:59:05 -07001485 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001486 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001487 pctldev->device_root = device_root;
1488
Linus Walleij2744e8a2011-05-02 20:50:54 +02001489 if (IS_ERR(device_root) || !device_root) {
1490 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001491 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001492 return;
1493 }
1494 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1495 device_root, pctldev, &pinctrl_pins_ops);
1496 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1497 device_root, pctldev, &pinctrl_groups_ops);
1498 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1499 device_root, pctldev, &pinctrl_gpioranges_ops);
1500 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001501 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001502}
1503
Tony Lindgren02157162012-01-20 08:17:22 -08001504static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1505{
1506 debugfs_remove_recursive(pctldev->device_root);
1507}
1508
Linus Walleij2744e8a2011-05-02 20:50:54 +02001509static void pinctrl_init_debugfs(void)
1510{
1511 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1512 if (IS_ERR(debugfs_root) || !debugfs_root) {
1513 pr_warn("failed to create debugfs directory\n");
1514 debugfs_root = NULL;
1515 return;
1516 }
1517
1518 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1519 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001520 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1521 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001522 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1523 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001524}
1525
1526#else /* CONFIG_DEBUG_FS */
1527
1528static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1529{
1530}
1531
1532static void pinctrl_init_debugfs(void)
1533{
1534}
1535
Tony Lindgren02157162012-01-20 08:17:22 -08001536static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1537{
1538}
1539
Linus Walleij2744e8a2011-05-02 20:50:54 +02001540#endif
1541
Stephen Warrend26bc492012-03-16 14:54:25 -06001542static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1543{
1544 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1545
1546 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301547 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001548 !ops->get_group_name ||
1549 !ops->get_group_pins)
1550 return -EINVAL;
1551
Stephen Warren57291ce2012-03-23 10:29:46 -06001552 if (ops->dt_node_to_map && !ops->dt_free_map)
1553 return -EINVAL;
1554
Stephen Warrend26bc492012-03-16 14:54:25 -06001555 return 0;
1556}
1557
Linus Walleij2744e8a2011-05-02 20:50:54 +02001558/**
1559 * pinctrl_register() - register a pin controller device
1560 * @pctldesc: descriptor for this pin controller
1561 * @dev: parent device for this pin controller
1562 * @driver_data: private pin controller data for this pin controller
1563 */
1564struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1565 struct device *dev, void *driver_data)
1566{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001567 struct pinctrl_dev *pctldev;
1568 int ret;
1569
Devendra Nagada9aecb2012-06-16 23:43:16 +05301570 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001571 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301572 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001573 return NULL;
1574
Stephen Warren02f5b982012-02-22 14:26:00 -07001575 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001576 if (pctldev == NULL) {
1577 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001578 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001579 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001580
1581 /* Initialize pin control device struct */
1582 pctldev->owner = pctldesc->owner;
1583 pctldev->desc = pctldesc;
1584 pctldev->driver_data = driver_data;
1585 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001586 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001587 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001588
Stephen Warrend26bc492012-03-16 14:54:25 -06001589 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301590 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001591 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001592 goto out_err;
1593 }
1594
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001595 /* If we're implementing pinmuxing, check the ops for sanity */
1596 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301597 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001598 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001599 }
1600
1601 /* If we're implementing pinconfig, check the ops for sanity */
1602 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301603 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001604 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001605 }
1606
Linus Walleij2744e8a2011-05-02 20:50:54 +02001607 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001608 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001609 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1610 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001611 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001612 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1613 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001614 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001615 }
1616
Stephen Warren57b676f2012-03-02 13:05:44 -07001617 mutex_lock(&pinctrl_mutex);
1618
Stephen Warren8b9c1392012-02-19 23:45:42 -07001619 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001620
Stephen Warren6e5e9592012-03-02 13:05:47 -07001621 pctldev->p = pinctrl_get_locked(pctldev->dev);
1622 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001623 pctldev->hog_default =
Stephen Warren6e5e9592012-03-02 13:05:47 -07001624 pinctrl_lookup_state_locked(pctldev->p,
1625 PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001626 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001627 dev_dbg(dev, "failed to lookup the default state\n");
1628 } else {
Julien Delacou840a47b2012-12-10 14:47:33 +01001629 if (pinctrl_select_state_locked(pctldev->p,
1630 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001631 dev_err(dev,
1632 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001633 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001634
1635 pctldev->hog_sleep =
1636 pinctrl_lookup_state_locked(pctldev->p,
1637 PINCTRL_STATE_SLEEP);
1638 if (IS_ERR(pctldev->hog_sleep))
1639 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001640 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001641
1642 mutex_unlock(&pinctrl_mutex);
1643
Stephen Warren2304b472012-02-22 14:26:01 -07001644 pinctrl_init_device_debugfs(pctldev);
1645
Linus Walleij2744e8a2011-05-02 20:50:54 +02001646 return pctldev;
1647
Stephen Warren51cd24e2011-12-09 16:59:05 -07001648out_err:
1649 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001650 return NULL;
1651}
1652EXPORT_SYMBOL_GPL(pinctrl_register);
1653
1654/**
1655 * pinctrl_unregister() - unregister pinmux
1656 * @pctldev: pin controller to unregister
1657 *
1658 * Called by pinmux drivers to unregister a pinmux.
1659 */
1660void pinctrl_unregister(struct pinctrl_dev *pctldev)
1661{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001662 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001663 if (pctldev == NULL)
1664 return;
1665
Tony Lindgren02157162012-01-20 08:17:22 -08001666 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001667
1668 mutex_lock(&pinctrl_mutex);
1669
Stephen Warren6e5e9592012-03-02 13:05:47 -07001670 if (!IS_ERR(pctldev->p))
1671 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001672
Linus Walleij2744e8a2011-05-02 20:50:54 +02001673 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001674 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001675 /* Destroy descriptor tree */
1676 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1677 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001678 /* remove gpio ranges map */
1679 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1680 list_del(&range->node);
1681
Stephen Warren51cd24e2011-12-09 16:59:05 -07001682 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001683
1684 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001685}
1686EXPORT_SYMBOL_GPL(pinctrl_unregister);
1687
1688static int __init pinctrl_init(void)
1689{
1690 pr_info("initialized pinctrl subsystem\n");
1691 pinctrl_init_debugfs();
1692 return 0;
1693}
1694
1695/* init early since many drivers really need to initialized pinmux early */
1696core_initcall(pinctrl_init);