blob: e6373f30aeeb26895283ab486893b6396c814371 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020021#include <linux/err.h>
22#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020023#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060026#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
29#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060030#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020032#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020033
Linus Walleijbefe5bd2012-02-09 19:47:48 +010034/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070035 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
39 */
40struct pinctrl_maps {
41 struct list_head node;
42 struct pinctrl_map const *maps;
43 unsigned num_maps;
44};
45
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080046static bool pinctrl_dummy_state;
47
Stephen Warren57b676f2012-03-02 13:05:44 -070048/* Mutex taken by all entry points */
49DEFINE_MUTEX(pinctrl_mutex);
50
51/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060052LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020053
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055static LIST_HEAD(pinctrl_list);
56
Stephen Warren57b676f2012-03-02 13:05:44 -070057/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070058static LIST_HEAD(pinctrl_maps);
59
60#define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
Guennadi Liakhovetskibc664682012-05-23 00:20:17 +020064 _i_++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010065
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080066/**
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
68 *
69 * Usually this function is called by platforms without pinctrl driver support
70 * but run with some shared drivers using pinctrl APIs.
71 * After calling this function, the pinctrl core will return successfully
72 * with creating a dummy state for the driver to keep going smoothly.
73 */
74void pinctrl_provide_dummies(void)
75{
76 pinctrl_dummy_state = true;
77}
78
Linus Walleij2744e8a2011-05-02 20:50:54 +020079const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
80{
81 /* We're not allowed to register devices without name */
82 return pctldev->desc->name;
83}
84EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
85
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080086const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
87{
88 return dev_name(pctldev->dev);
89}
90EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
91
Linus Walleij2744e8a2011-05-02 20:50:54 +020092void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
93{
94 return pctldev->driver_data;
95}
96EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
97
98/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010099 * get_pinctrl_dev_from_devname() - look up pin controller device
100 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101 *
102 * Looks up a pin control device matching a certain device name or pure device
103 * pointer, the pure device pointer will take precedence.
104 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100105struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200106{
107 struct pinctrl_dev *pctldev = NULL;
108 bool found = false;
109
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100110 if (!devname)
111 return NULL;
112
Linus Walleij2744e8a2011-05-02 20:50:54 +0200113 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100114 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115 /* Matched on device name */
116 found = true;
117 break;
118 }
119 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120
121 return found ? pctldev : NULL;
122}
123
Linus Walleij2744e8a2011-05-02 20:50:54 +0200124/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200125 * pin_get_from_name() - look up a pin number from a name
126 * @pctldev: the pin control device to lookup the pin on
127 * @name: the name of the pin to look up
128 */
129int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
130{
Chanho Park706e8522012-01-03 16:47:50 +0900131 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200132
Chanho Park706e8522012-01-03 16:47:50 +0900133 /* The pin number can be retrived from the pin controller descriptor */
134 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200135 struct pin_desc *desc;
136
Chanho Park706e8522012-01-03 16:47:50 +0900137 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200138 desc = pin_desc_get(pctldev, pin);
139 /* Pin space may be sparse */
140 if (desc == NULL)
141 continue;
142 if (desc->name && !strcmp(name, desc->name))
143 return pin;
144 }
145
146 return -EINVAL;
147}
148
149/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800150 * pin_get_name_from_id() - look up a pin name from a pin id
151 * @pctldev: the pin control device to lookup the pin on
152 * @name: the name of the pin to look up
153 */
154const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
155{
156 const struct pin_desc *desc;
157
158 desc = pin_desc_get(pctldev, pin);
159 if (desc == NULL) {
160 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
161 pin);
162 return NULL;
163 }
164
165 return desc->name;
166}
167
168/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200169 * pin_is_valid() - check if pin exists on controller
170 * @pctldev: the pin control device to check the pin on
171 * @pin: pin to check, use the local pin controller index number
172 *
173 * This tells us whether a certain pin exist on a certain pin controller or
174 * not. Pin lists may be sparse, so some pins may not exist.
175 */
176bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
177{
178 struct pin_desc *pindesc;
179
180 if (pin < 0)
181 return false;
182
Stephen Warren57b676f2012-03-02 13:05:44 -0700183 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200184 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700185 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186
Stephen Warren57b676f2012-03-02 13:05:44 -0700187 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188}
189EXPORT_SYMBOL_GPL(pin_is_valid);
190
191/* Deletes a range of pin descriptors */
192static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
193 const struct pinctrl_pin_desc *pins,
194 unsigned num_pins)
195{
196 int i;
197
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 for (i = 0; i < num_pins; i++) {
199 struct pin_desc *pindesc;
200
201 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
202 pins[i].number);
203 if (pindesc != NULL) {
204 radix_tree_delete(&pctldev->pin_desc_tree,
205 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100206 if (pindesc->dynamic_name)
207 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200208 }
209 kfree(pindesc);
210 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200211}
212
213static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
214 unsigned number, const char *name)
215{
216 struct pin_desc *pindesc;
217
218 pindesc = pin_desc_get(pctldev, number);
219 if (pindesc != NULL) {
220 pr_err("pin %d already registered on %s\n", number,
221 pctldev->desc->name);
222 return -EINVAL;
223 }
224
225 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700226 if (pindesc == NULL) {
227 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700229 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200230
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231 /* Set owner */
232 pindesc->pctldev = pctldev;
233
Stephen Warren9af1e442011-10-19 16:19:27 -0600234 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100235 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100236 pindesc->name = name;
237 } else {
238 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530239 if (pindesc->name == NULL) {
240 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100241 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530242 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100243 pindesc->dynamic_name = true;
244 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100248 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200249 return 0;
250}
251
252static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
253 struct pinctrl_pin_desc const *pins,
254 unsigned num_descs)
255{
256 unsigned i;
257 int ret = 0;
258
259 for (i = 0; i < num_descs; i++) {
260 ret = pinctrl_register_one_pin(pctldev,
261 pins[i].number, pins[i].name);
262 if (ret)
263 return ret;
264 }
265
266 return 0;
267}
268
269/**
270 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
271 * @pctldev: pin controller device to check
272 * @gpio: gpio pin to check taken from the global GPIO pin space
273 *
274 * Tries to match a GPIO pin number to the ranges handled by a certain pin
275 * controller, return the range or NULL
276 */
277static struct pinctrl_gpio_range *
278pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
279{
280 struct pinctrl_gpio_range *range = NULL;
281
282 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200283 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
284 /* Check if we're in the valid range */
285 if (gpio >= range->base &&
286 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200287 return range;
288 }
289 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200290
291 return NULL;
292}
293
294/**
295 * pinctrl_get_device_gpio_range() - find device for GPIO range
296 * @gpio: the pin to locate the pin controller for
297 * @outdev: the pin control device if found
298 * @outrange: the GPIO range if found
299 *
300 * Find the pin controller handling a certain GPIO pin from the pinspace of
301 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800302 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
303 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200304 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700305static int pinctrl_get_device_gpio_range(unsigned gpio,
306 struct pinctrl_dev **outdev,
307 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200308{
309 struct pinctrl_dev *pctldev = NULL;
310
311 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200312 list_for_each_entry(pctldev, &pinctrldev_list, node) {
313 struct pinctrl_gpio_range *range;
314
315 range = pinctrl_match_gpio_range(pctldev, gpio);
316 if (range != NULL) {
317 *outdev = pctldev;
318 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 return 0;
320 }
321 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800323 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200324}
325
326/**
327 * pinctrl_add_gpio_range() - register a GPIO range for a controller
328 * @pctldev: pin controller device to add the range to
329 * @range: the GPIO range to add
330 *
331 * This adds a range of GPIOs to be handled by a certain pin controller. Call
332 * this to register handled ranges after registering your pin controller.
333 */
334void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
335 struct pinctrl_gpio_range *range)
336{
Stephen Warren57b676f2012-03-02 13:05:44 -0700337 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700338 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700339 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200340}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700341EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200342
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800343void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
344 struct pinctrl_gpio_range *ranges,
345 unsigned nranges)
346{
347 int i;
348
349 for (i = 0; i < nranges; i++)
350 pinctrl_add_gpio_range(pctldev, &ranges[i]);
351}
352EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
353
Linus Walleij192c3692012-11-20 14:03:37 +0100354struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530355 struct pinctrl_gpio_range *range)
356{
357 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
358
Linus Walleijdfa97512012-11-20 14:54:18 +0100359 /*
360 * If we can't find this device, let's assume that is because
361 * it has not probed yet, so the driver trying to register this
362 * range need to defer probing.
363 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530364 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100365 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530366
367 pinctrl_add_gpio_range(pctldev, range);
368 return pctldev;
369}
Linus Walleij192c3692012-11-20 14:03:37 +0100370EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530371
Linus Walleij2744e8a2011-05-02 20:50:54 +0200372/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100373 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
374 * @pctldev: the pin controller device to look in
375 * @pin: a controller-local number to find the range for
376 */
377struct pinctrl_gpio_range *
378pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
379 unsigned int pin)
380{
381 struct pinctrl_gpio_range *range = NULL;
382
383 /* Loop over the ranges */
384 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
385 /* Check if we're in the valid range */
386 if (pin >= range->pin_base &&
387 pin < range->pin_base + range->npins) {
388 return range;
389 }
390 }
391
392 return NULL;
393}
394EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
395
396/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530397 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
398 * @pctldev: pin controller device to remove the range from
399 * @range: the GPIO range to remove
400 */
401void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
402 struct pinctrl_gpio_range *range)
403{
404 mutex_lock(&pinctrl_mutex);
405 list_del(&range->node);
406 mutex_unlock(&pinctrl_mutex);
407}
408EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
409
410/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200411 * pinctrl_get_group_selector() - returns the group selector for a group
412 * @pctldev: the pin controller handling the group
413 * @pin_group: the pin group to look up
414 */
415int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
416 const char *pin_group)
417{
418 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530419 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200420 unsigned group_selector = 0;
421
Viresh Kumard1e90e92012-03-30 11:25:40 +0530422 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200423 const char *gname = pctlops->get_group_name(pctldev,
424 group_selector);
425 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700426 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200427 "found group selector %u for %s\n",
428 group_selector,
429 pin_group);
430 return group_selector;
431 }
432
433 group_selector++;
434 }
435
Stephen Warren51cd24e2011-12-09 16:59:05 -0700436 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200437 pin_group);
438
439 return -EINVAL;
440}
441
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100442/**
443 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
444 * @gpio: the GPIO pin number from the GPIO subsystem number space
445 *
446 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
447 * as part of their gpio_request() semantics, platforms and individual drivers
448 * shall *NOT* request GPIO pins to be muxed in.
449 */
450int pinctrl_request_gpio(unsigned gpio)
451{
452 struct pinctrl_dev *pctldev;
453 struct pinctrl_gpio_range *range;
454 int ret;
455 int pin;
456
Stephen Warren57b676f2012-03-02 13:05:44 -0700457 mutex_lock(&pinctrl_mutex);
458
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100459 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700460 if (ret) {
461 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800462 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700463 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100464
465 /* Convert to the pin controllers number space */
466 pin = gpio - range->base + range->pin_base;
467
Stephen Warren57b676f2012-03-02 13:05:44 -0700468 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
469
470 mutex_unlock(&pinctrl_mutex);
471 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100472}
473EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
474
475/**
476 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
477 * @gpio: the GPIO pin number from the GPIO subsystem number space
478 *
479 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
480 * as part of their gpio_free() semantics, platforms and individual drivers
481 * shall *NOT* request GPIO pins to be muxed out.
482 */
483void pinctrl_free_gpio(unsigned gpio)
484{
485 struct pinctrl_dev *pctldev;
486 struct pinctrl_gpio_range *range;
487 int ret;
488 int pin;
489
Stephen Warren57b676f2012-03-02 13:05:44 -0700490 mutex_lock(&pinctrl_mutex);
491
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100492 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700493 if (ret) {
494 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100495 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700496 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100497
498 /* Convert to the pin controllers number space */
499 pin = gpio - range->base + range->pin_base;
500
Stephen Warren57b676f2012-03-02 13:05:44 -0700501 pinmux_free_gpio(pctldev, pin, range);
502
503 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100504}
505EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
506
507static int pinctrl_gpio_direction(unsigned gpio, bool input)
508{
509 struct pinctrl_dev *pctldev;
510 struct pinctrl_gpio_range *range;
511 int ret;
512 int pin;
513
514 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
515 if (ret)
516 return ret;
517
518 /* Convert to the pin controllers number space */
519 pin = gpio - range->base + range->pin_base;
520
521 return pinmux_gpio_direction(pctldev, range, pin, input);
522}
523
524/**
525 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
526 * @gpio: the GPIO pin number from the GPIO subsystem number space
527 *
528 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
529 * as part of their gpio_direction_input() semantics, platforms and individual
530 * drivers shall *NOT* touch pin control GPIO calls.
531 */
532int pinctrl_gpio_direction_input(unsigned gpio)
533{
Stephen Warren57b676f2012-03-02 13:05:44 -0700534 int ret;
535 mutex_lock(&pinctrl_mutex);
536 ret = pinctrl_gpio_direction(gpio, true);
537 mutex_unlock(&pinctrl_mutex);
538 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100539}
540EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
541
542/**
543 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
544 * @gpio: the GPIO pin number from the GPIO subsystem number space
545 *
546 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
547 * as part of their gpio_direction_output() semantics, platforms and individual
548 * drivers shall *NOT* touch pin control GPIO calls.
549 */
550int pinctrl_gpio_direction_output(unsigned gpio)
551{
Stephen Warren57b676f2012-03-02 13:05:44 -0700552 int ret;
553 mutex_lock(&pinctrl_mutex);
554 ret = pinctrl_gpio_direction(gpio, false);
555 mutex_unlock(&pinctrl_mutex);
556 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100557}
558EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
559
Stephen Warren6e5e9592012-03-02 13:05:47 -0700560static struct pinctrl_state *find_state(struct pinctrl *p,
561 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100562{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700563 struct pinctrl_state *state;
564
565 list_for_each_entry(state, &p->states, node)
566 if (!strcmp(state->name, name))
567 return state;
568
569 return NULL;
570}
571
572static struct pinctrl_state *create_state(struct pinctrl *p,
573 const char *name)
574{
575 struct pinctrl_state *state;
576
577 state = kzalloc(sizeof(*state), GFP_KERNEL);
578 if (state == NULL) {
579 dev_err(p->dev,
580 "failed to alloc struct pinctrl_state\n");
581 return ERR_PTR(-ENOMEM);
582 }
583
584 state->name = name;
585 INIT_LIST_HEAD(&state->settings);
586
587 list_add_tail(&state->node, &p->states);
588
589 return state;
590}
591
592static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
593{
594 struct pinctrl_state *state;
595 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700596 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700597
598 state = find_state(p, map->name);
599 if (!state)
600 state = create_state(p, map->name);
601 if (IS_ERR(state))
602 return PTR_ERR(state);
603
Stephen Warren1e2082b2012-03-02 13:05:48 -0700604 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
605 return 0;
606
Stephen Warren6e5e9592012-03-02 13:05:47 -0700607 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
608 if (setting == NULL) {
609 dev_err(p->dev,
610 "failed to alloc struct pinctrl_setting\n");
611 return -ENOMEM;
612 }
613
Stephen Warren1e2082b2012-03-02 13:05:48 -0700614 setting->type = map->type;
615
Stephen Warren6e5e9592012-03-02 13:05:47 -0700616 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
617 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700618 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100619 /* Do not defer probing of hogs (circular loop) */
620 if (!strcmp(map->ctrl_dev_name, map->dev_name))
621 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200622 /*
623 * OK let us guess that the driver is not there yet, and
624 * let's defer obtaining this pinctrl handle to later...
625 */
Linus Walleij89216492012-12-11 14:14:32 +0100626 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
627 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200628 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700629 }
630
Linus Walleij1a789582012-10-17 20:51:54 +0200631 setting->dev_name = map->dev_name;
632
Stephen Warren1e2082b2012-03-02 13:05:48 -0700633 switch (map->type) {
634 case PIN_MAP_TYPE_MUX_GROUP:
635 ret = pinmux_map_to_setting(map, setting);
636 break;
637 case PIN_MAP_TYPE_CONFIGS_PIN:
638 case PIN_MAP_TYPE_CONFIGS_GROUP:
639 ret = pinconf_map_to_setting(map, setting);
640 break;
641 default:
642 ret = -EINVAL;
643 break;
644 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700645 if (ret < 0) {
646 kfree(setting);
647 return ret;
648 }
649
650 list_add_tail(&setting->node, &state->settings);
651
652 return 0;
653}
654
655static struct pinctrl *find_pinctrl(struct device *dev)
656{
657 struct pinctrl *p;
658
Stephen Warren1e2082b2012-03-02 13:05:48 -0700659 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700660 if (p->dev == dev)
661 return p;
662
663 return NULL;
664}
665
666static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
667
668static struct pinctrl *create_pinctrl(struct device *dev)
669{
670 struct pinctrl *p;
671 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700672 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100673 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700674 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700675 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100676
677 /*
678 * create the state cookie holder struct pinctrl for each
679 * mapping, this is what consumers will get when requesting
680 * a pin control handle with pinctrl_get()
681 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700682 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700683 if (p == NULL) {
684 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100685 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700686 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700687 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700688 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600689 INIT_LIST_HEAD(&p->dt_maps);
690
691 ret = pinctrl_dt_to_map(p);
692 if (ret < 0) {
693 kfree(p);
694 return ERR_PTR(ret);
695 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696
697 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100698
699 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700700 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700701 /* Map must be for this device */
702 if (strcmp(map->dev_name, devname))
703 continue;
704
Stephen Warren6e5e9592012-03-02 13:05:47 -0700705 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100706 /*
707 * At this point the adding of a setting may:
708 *
709 * - Defer, if the pinctrl device is not yet available
710 * - Fail, if the pinctrl device is not yet available,
711 * AND the setting is a hog. We cannot defer that, since
712 * the hog will kick in immediately after the device
713 * is registered.
714 *
715 * If the error returned was not -EPROBE_DEFER then we
716 * accumulate the errors to see if we end up with
717 * an -EPROBE_DEFER later, as that is the worst case.
718 */
719 if (ret == -EPROBE_DEFER) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700720 pinctrl_put_locked(p, false);
721 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100722 }
723 }
Linus Walleij89216492012-12-11 14:14:32 +0100724 if (ret < 0) {
725 /* If some other error than deferral occured, return here */
726 pinctrl_put_locked(p, false);
727 return ERR_PTR(ret);
728 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100729
Linus Walleijb0666ba2012-12-11 13:12:35 +0100730 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700731 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100732
733 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700735
Stephen Warren6e5e9592012-03-02 13:05:47 -0700736static struct pinctrl *pinctrl_get_locked(struct device *dev)
737{
738 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700739
Stephen Warren6e5e9592012-03-02 13:05:47 -0700740 if (WARN_ON(!dev))
741 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700742
Stephen Warren6e5e9592012-03-02 13:05:47 -0700743 p = find_pinctrl(dev);
744 if (p != NULL)
745 return ERR_PTR(-EBUSY);
746
Richard Genoudd599bfb2012-08-10 16:53:49 +0200747 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100748}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700749
750/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700751 * pinctrl_get() - retrieves the pinctrl handle for a device
752 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700753 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700754struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700755{
756 struct pinctrl *p;
757
Stephen Warren57b676f2012-03-02 13:05:44 -0700758 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700759 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700760 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700761
762 return p;
763}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100764EXPORT_SYMBOL_GPL(pinctrl_get);
765
Stephen Warren6e5e9592012-03-02 13:05:47 -0700766static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700767{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700768 struct pinctrl_state *state, *n1;
769 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700770
Stephen Warren6e5e9592012-03-02 13:05:47 -0700771 list_for_each_entry_safe(state, n1, &p->states, node) {
772 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700773 switch (setting->type) {
774 case PIN_MAP_TYPE_MUX_GROUP:
775 if (state == p->state)
776 pinmux_disable_setting(setting);
777 pinmux_free_setting(setting);
778 break;
779 case PIN_MAP_TYPE_CONFIGS_PIN:
780 case PIN_MAP_TYPE_CONFIGS_GROUP:
781 pinconf_free_setting(setting);
782 break;
783 default:
784 break;
785 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700786 list_del(&setting->node);
787 kfree(setting);
788 }
789 list_del(&state->node);
790 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700791 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700792
Stephen Warren57291ce2012-03-23 10:29:46 -0600793 pinctrl_dt_free_maps(p);
794
Stephen Warren6e5e9592012-03-02 13:05:47 -0700795 if (inlist)
796 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700797 kfree(p);
798}
799
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700801 * pinctrl_put() - release a previously claimed pinctrl handle
802 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803 */
804void pinctrl_put(struct pinctrl *p)
805{
Stephen Warren57b676f2012-03-02 13:05:44 -0700806 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700807 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700808 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809}
810EXPORT_SYMBOL_GPL(pinctrl_put);
811
Stephen Warren6e5e9592012-03-02 13:05:47 -0700812static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
813 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700814{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700815 struct pinctrl_state *state;
816
817 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800818 if (!state) {
819 if (pinctrl_dummy_state) {
820 /* create dummy state */
821 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
822 name);
823 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200824 } else
825 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800826 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700827
828 return state;
829}
830
831/**
832 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
833 * @p: the pinctrl handle to retrieve the state from
834 * @name: the state name to retrieve
835 */
836struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
837{
838 struct pinctrl_state *s;
839
840 mutex_lock(&pinctrl_mutex);
841 s = pinctrl_lookup_state_locked(p, name);
842 mutex_unlock(&pinctrl_mutex);
843
844 return s;
845}
846EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
847
848static int pinctrl_select_state_locked(struct pinctrl *p,
849 struct pinctrl_state *state)
850{
851 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700852 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700853
Stephen Warren6e5e9592012-03-02 13:05:47 -0700854 if (p->state == state)
855 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700856
Stephen Warren6e5e9592012-03-02 13:05:47 -0700857 if (p->state) {
858 /*
859 * The set of groups with a mux configuration in the old state
860 * may not be identical to the set of groups with a mux setting
861 * in the new state. While this might be unusual, it's entirely
862 * possible for the "user"-supplied mapping table to be written
863 * that way. For each group that was configured in the old state
864 * but not in the new state, this code puts that group into a
865 * safe/disabled state.
866 */
867 list_for_each_entry(setting, &p->state->settings, node) {
868 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700869 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
870 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700871 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700872 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
873 continue;
874 if (setting2->data.mux.group ==
875 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700876 found = true;
877 break;
878 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700879 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700880 if (!found)
881 pinmux_disable_setting(setting);
882 }
883 }
884
885 p->state = state;
886
887 /* Apply all the settings for the new state */
888 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700889 switch (setting->type) {
890 case PIN_MAP_TYPE_MUX_GROUP:
891 ret = pinmux_enable_setting(setting);
892 break;
893 case PIN_MAP_TYPE_CONFIGS_PIN:
894 case PIN_MAP_TYPE_CONFIGS_GROUP:
895 ret = pinconf_apply_setting(setting);
896 break;
897 default:
898 ret = -EINVAL;
899 break;
900 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700901 if (ret < 0) {
902 /* FIXME: Difficult to return to prev state */
903 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700904 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700905 }
906
Stephen Warren7ecdb162012-03-02 13:05:45 -0700907 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700908}
909
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100910/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700911 * pinctrl_select() - select/activate/program a pinctrl state to HW
912 * @p: the pinctrl handle for the device that requests configuratio
913 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100914 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100916{
Stephen Warren57b676f2012-03-02 13:05:44 -0700917 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700918
Stephen Warren57b676f2012-03-02 13:05:44 -0700919 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700920 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700921 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700922
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100923 return ret;
924}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700925EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100926
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600927static void devm_pinctrl_release(struct device *dev, void *res)
928{
929 pinctrl_put(*(struct pinctrl **)res);
930}
931
932/**
933 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
934 * @dev: the device to obtain the handle for
935 *
936 * If there is a need to explicitly destroy the returned struct pinctrl,
937 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
938 */
939struct pinctrl *devm_pinctrl_get(struct device *dev)
940{
941 struct pinctrl **ptr, *p;
942
943 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
944 if (!ptr)
945 return ERR_PTR(-ENOMEM);
946
947 p = pinctrl_get(dev);
948 if (!IS_ERR(p)) {
949 *ptr = p;
950 devres_add(dev, ptr);
951 } else {
952 devres_free(ptr);
953 }
954
955 return p;
956}
957EXPORT_SYMBOL_GPL(devm_pinctrl_get);
958
959static int devm_pinctrl_match(struct device *dev, void *res, void *data)
960{
961 struct pinctrl **p = res;
962
963 return *p == data;
964}
965
966/**
967 * devm_pinctrl_put() - Resource managed pinctrl_put()
968 * @p: the pinctrl handle to release
969 *
970 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
971 * this function will not need to be called and the resource management
972 * code will ensure that the resource is freed.
973 */
974void devm_pinctrl_put(struct pinctrl *p)
975{
976 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
977 devm_pinctrl_match, p));
978 pinctrl_put(p);
979}
980EXPORT_SYMBOL_GPL(devm_pinctrl_put);
981
Stephen Warren57291ce2012-03-23 10:29:46 -0600982int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
983 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100984{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700985 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700986 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100987
988 pr_debug("add %d pinmux maps\n", num_maps);
989
990 /* First sanity check the new mapping */
991 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700992 if (!maps[i].dev_name) {
993 pr_err("failed to register map %s (%d): no device given\n",
994 maps[i].name, i);
995 return -EINVAL;
996 }
997
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100998 if (!maps[i].name) {
999 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001000 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001001 return -EINVAL;
1002 }
1003
Stephen Warren1e2082b2012-03-02 13:05:48 -07001004 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1005 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001006 pr_err("failed to register map %s (%d): no pin control device given\n",
1007 maps[i].name, i);
1008 return -EINVAL;
1009 }
1010
Stephen Warren1e2082b2012-03-02 13:05:48 -07001011 switch (maps[i].type) {
1012 case PIN_MAP_TYPE_DUMMY_STATE:
1013 break;
1014 case PIN_MAP_TYPE_MUX_GROUP:
1015 ret = pinmux_validate_map(&maps[i], i);
1016 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001017 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001018 break;
1019 case PIN_MAP_TYPE_CONFIGS_PIN:
1020 case PIN_MAP_TYPE_CONFIGS_GROUP:
1021 ret = pinconf_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 default:
1026 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001027 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001028 return -EINVAL;
1029 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001030 }
1031
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001032 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1033 if (!maps_node) {
1034 pr_err("failed to alloc struct pinctrl_maps\n");
1035 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001036 }
1037
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001038 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001039 if (dup) {
1040 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1041 GFP_KERNEL);
1042 if (!maps_node->maps) {
1043 pr_err("failed to duplicate mapping table\n");
1044 kfree(maps_node);
1045 return -ENOMEM;
1046 }
1047 } else {
1048 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001049 }
1050
Stephen Warren57291ce2012-03-23 10:29:46 -06001051 if (!locked)
1052 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001053 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001054 if (!locked)
1055 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001056
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001057 return 0;
1058}
1059
Stephen Warren57291ce2012-03-23 10:29:46 -06001060/**
1061 * pinctrl_register_mappings() - register a set of pin controller mappings
1062 * @maps: the pincontrol mappings table to register. This should probably be
1063 * marked with __initdata so it can be discarded after boot. This
1064 * function will perform a shallow copy for the mapping entries.
1065 * @num_maps: the number of maps in the mapping table
1066 */
1067int pinctrl_register_mappings(struct pinctrl_map const *maps,
1068 unsigned num_maps)
1069{
1070 return pinctrl_register_map(maps, num_maps, true, false);
1071}
1072
1073void pinctrl_unregister_map(struct pinctrl_map const *map)
1074{
1075 struct pinctrl_maps *maps_node;
1076
1077 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1078 if (maps_node->maps == map) {
1079 list_del(&maps_node->node);
1080 return;
1081 }
1082 }
1083}
1084
Julien Delacou840a47b2012-12-10 14:47:33 +01001085/**
1086 * pinctrl_force_sleep() - turn a given controller device into sleep state
1087 * @pctldev: pin controller device
1088 */
1089int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1090{
1091 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1092 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1093 return 0;
1094}
1095EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1096
1097/**
1098 * pinctrl_force_default() - turn a given controller device into default state
1099 * @pctldev: pin controller device
1100 */
1101int pinctrl_force_default(struct pinctrl_dev *pctldev)
1102{
1103 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1104 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1105 return 0;
1106}
1107EXPORT_SYMBOL_GPL(pinctrl_force_default);
1108
Linus Walleij2744e8a2011-05-02 20:50:54 +02001109#ifdef CONFIG_DEBUG_FS
1110
1111static int pinctrl_pins_show(struct seq_file *s, void *what)
1112{
1113 struct pinctrl_dev *pctldev = s->private;
1114 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001115 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001116
1117 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001118
Stephen Warren57b676f2012-03-02 13:05:44 -07001119 mutex_lock(&pinctrl_mutex);
1120
Chanho Park706e8522012-01-03 16:47:50 +09001121 /* The pin number can be retrived from the pin controller descriptor */
1122 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001123 struct pin_desc *desc;
1124
Chanho Park706e8522012-01-03 16:47:50 +09001125 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001126 desc = pin_desc_get(pctldev, pin);
1127 /* Pin space may be sparse */
1128 if (desc == NULL)
1129 continue;
1130
1131 seq_printf(s, "pin %d (%s) ", pin,
1132 desc->name ? desc->name : "unnamed");
1133
1134 /* Driver-specific info per pin */
1135 if (ops->pin_dbg_show)
1136 ops->pin_dbg_show(pctldev, s, pin);
1137
1138 seq_puts(s, "\n");
1139 }
1140
Stephen Warren57b676f2012-03-02 13:05:44 -07001141 mutex_unlock(&pinctrl_mutex);
1142
Linus Walleij2744e8a2011-05-02 20:50:54 +02001143 return 0;
1144}
1145
1146static int pinctrl_groups_show(struct seq_file *s, void *what)
1147{
1148 struct pinctrl_dev *pctldev = s->private;
1149 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301150 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001151
Viresh Kumard1e90e92012-03-30 11:25:40 +05301152 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001153 mutex_lock(&pinctrl_mutex);
1154
Linus Walleij2744e8a2011-05-02 20:50:54 +02001155 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301156 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001157 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001158 unsigned num_pins;
1159 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001160 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001161 int ret;
1162 int i;
1163
1164 ret = ops->get_group_pins(pctldev, selector,
1165 &pins, &num_pins);
1166 if (ret)
1167 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1168 gname);
1169 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001170 seq_printf(s, "group: %s\n", gname);
1171 for (i = 0; i < num_pins; i++) {
1172 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001173 if (WARN_ON(!pname)) {
1174 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001175 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001176 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001177 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1178 }
1179 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001180 }
1181 selector++;
1182 }
1183
Stephen Warren57b676f2012-03-02 13:05:44 -07001184 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001185
1186 return 0;
1187}
1188
1189static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1190{
1191 struct pinctrl_dev *pctldev = s->private;
1192 struct pinctrl_gpio_range *range = NULL;
1193
1194 seq_puts(s, "GPIO ranges handled:\n");
1195
Stephen Warren57b676f2012-03-02 13:05:44 -07001196 mutex_lock(&pinctrl_mutex);
1197
Linus Walleij2744e8a2011-05-02 20:50:54 +02001198 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001199 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001200 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1201 range->id, range->name,
1202 range->base, (range->base + range->npins - 1),
1203 range->pin_base,
1204 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001205 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001206
1207 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001208
1209 return 0;
1210}
1211
1212static int pinctrl_devices_show(struct seq_file *s, void *what)
1213{
1214 struct pinctrl_dev *pctldev;
1215
Linus Walleijae6b4d82011-10-19 18:14:33 +02001216 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001217
1218 mutex_lock(&pinctrl_mutex);
1219
Linus Walleij2744e8a2011-05-02 20:50:54 +02001220 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1221 seq_printf(s, "%s ", pctldev->desc->name);
1222 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001223 seq_puts(s, "yes ");
1224 else
1225 seq_puts(s, "no ");
1226 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001227 seq_puts(s, "yes");
1228 else
1229 seq_puts(s, "no");
1230 seq_puts(s, "\n");
1231 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001232
1233 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001234
1235 return 0;
1236}
1237
Stephen Warren1e2082b2012-03-02 13:05:48 -07001238static inline const char *map_type(enum pinctrl_map_type type)
1239{
1240 static const char * const names[] = {
1241 "INVALID",
1242 "DUMMY_STATE",
1243 "MUX_GROUP",
1244 "CONFIGS_PIN",
1245 "CONFIGS_GROUP",
1246 };
1247
1248 if (type >= ARRAY_SIZE(names))
1249 return "UNKNOWN";
1250
1251 return names[type];
1252}
1253
Stephen Warren3eedb432012-02-23 17:04:40 -07001254static int pinctrl_maps_show(struct seq_file *s, void *what)
1255{
1256 struct pinctrl_maps *maps_node;
1257 int i;
1258 struct pinctrl_map const *map;
1259
1260 seq_puts(s, "Pinctrl maps:\n");
1261
Stephen Warren57b676f2012-03-02 13:05:44 -07001262 mutex_lock(&pinctrl_mutex);
1263
Stephen Warren3eedb432012-02-23 17:04:40 -07001264 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001265 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1266 map->dev_name, map->name, map_type(map->type),
1267 map->type);
1268
1269 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1270 seq_printf(s, "controlling device %s\n",
1271 map->ctrl_dev_name);
1272
1273 switch (map->type) {
1274 case PIN_MAP_TYPE_MUX_GROUP:
1275 pinmux_show_map(s, map);
1276 break;
1277 case PIN_MAP_TYPE_CONFIGS_PIN:
1278 case PIN_MAP_TYPE_CONFIGS_GROUP:
1279 pinconf_show_map(s, map);
1280 break;
1281 default:
1282 break;
1283 }
1284
1285 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001286 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001287
1288 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001289
1290 return 0;
1291}
1292
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001293static int pinctrl_show(struct seq_file *s, void *what)
1294{
1295 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001296 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001297 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001298
1299 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001300
1301 mutex_lock(&pinctrl_mutex);
1302
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001303 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001304 seq_printf(s, "device: %s current state: %s\n",
1305 dev_name(p->dev),
1306 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001307
Stephen Warren6e5e9592012-03-02 13:05:47 -07001308 list_for_each_entry(state, &p->states, node) {
1309 seq_printf(s, " state: %s\n", state->name);
1310
1311 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001312 struct pinctrl_dev *pctldev = setting->pctldev;
1313
1314 seq_printf(s, " type: %s controller %s ",
1315 map_type(setting->type),
1316 pinctrl_dev_get_name(pctldev));
1317
1318 switch (setting->type) {
1319 case PIN_MAP_TYPE_MUX_GROUP:
1320 pinmux_show_setting(s, setting);
1321 break;
1322 case PIN_MAP_TYPE_CONFIGS_PIN:
1323 case PIN_MAP_TYPE_CONFIGS_GROUP:
1324 pinconf_show_setting(s, setting);
1325 break;
1326 default:
1327 break;
1328 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001329 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001330 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001331 }
1332
Stephen Warren57b676f2012-03-02 13:05:44 -07001333 mutex_unlock(&pinctrl_mutex);
1334
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001335 return 0;
1336}
1337
Linus Walleij2744e8a2011-05-02 20:50:54 +02001338static int pinctrl_pins_open(struct inode *inode, struct file *file)
1339{
1340 return single_open(file, pinctrl_pins_show, inode->i_private);
1341}
1342
1343static int pinctrl_groups_open(struct inode *inode, struct file *file)
1344{
1345 return single_open(file, pinctrl_groups_show, inode->i_private);
1346}
1347
1348static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1349{
1350 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1351}
1352
1353static int pinctrl_devices_open(struct inode *inode, struct file *file)
1354{
1355 return single_open(file, pinctrl_devices_show, NULL);
1356}
1357
Stephen Warren3eedb432012-02-23 17:04:40 -07001358static int pinctrl_maps_open(struct inode *inode, struct file *file)
1359{
1360 return single_open(file, pinctrl_maps_show, NULL);
1361}
1362
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001363static int pinctrl_open(struct inode *inode, struct file *file)
1364{
1365 return single_open(file, pinctrl_show, NULL);
1366}
1367
Linus Walleij2744e8a2011-05-02 20:50:54 +02001368static const struct file_operations pinctrl_pins_ops = {
1369 .open = pinctrl_pins_open,
1370 .read = seq_read,
1371 .llseek = seq_lseek,
1372 .release = single_release,
1373};
1374
1375static const struct file_operations pinctrl_groups_ops = {
1376 .open = pinctrl_groups_open,
1377 .read = seq_read,
1378 .llseek = seq_lseek,
1379 .release = single_release,
1380};
1381
1382static const struct file_operations pinctrl_gpioranges_ops = {
1383 .open = pinctrl_gpioranges_open,
1384 .read = seq_read,
1385 .llseek = seq_lseek,
1386 .release = single_release,
1387};
1388
1389static const struct file_operations pinctrl_devices_ops = {
1390 .open = pinctrl_devices_open,
1391 .read = seq_read,
1392 .llseek = seq_lseek,
1393 .release = single_release,
1394};
1395
Stephen Warren3eedb432012-02-23 17:04:40 -07001396static const struct file_operations pinctrl_maps_ops = {
1397 .open = pinctrl_maps_open,
1398 .read = seq_read,
1399 .llseek = seq_lseek,
1400 .release = single_release,
1401};
1402
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001403static const struct file_operations pinctrl_ops = {
1404 .open = pinctrl_open,
1405 .read = seq_read,
1406 .llseek = seq_lseek,
1407 .release = single_release,
1408};
1409
Linus Walleij2744e8a2011-05-02 20:50:54 +02001410static struct dentry *debugfs_root;
1411
1412static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1413{
Tony Lindgren02157162012-01-20 08:17:22 -08001414 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001415
Stephen Warren51cd24e2011-12-09 16:59:05 -07001416 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001417 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001418 pctldev->device_root = device_root;
1419
Linus Walleij2744e8a2011-05-02 20:50:54 +02001420 if (IS_ERR(device_root) || !device_root) {
1421 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001422 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001423 return;
1424 }
1425 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1426 device_root, pctldev, &pinctrl_pins_ops);
1427 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1428 device_root, pctldev, &pinctrl_groups_ops);
1429 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1430 device_root, pctldev, &pinctrl_gpioranges_ops);
1431 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001432 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001433}
1434
Tony Lindgren02157162012-01-20 08:17:22 -08001435static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1436{
1437 debugfs_remove_recursive(pctldev->device_root);
1438}
1439
Linus Walleij2744e8a2011-05-02 20:50:54 +02001440static void pinctrl_init_debugfs(void)
1441{
1442 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1443 if (IS_ERR(debugfs_root) || !debugfs_root) {
1444 pr_warn("failed to create debugfs directory\n");
1445 debugfs_root = NULL;
1446 return;
1447 }
1448
1449 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1450 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001451 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1452 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001453 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1454 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001455}
1456
1457#else /* CONFIG_DEBUG_FS */
1458
1459static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1460{
1461}
1462
1463static void pinctrl_init_debugfs(void)
1464{
1465}
1466
Tony Lindgren02157162012-01-20 08:17:22 -08001467static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1468{
1469}
1470
Linus Walleij2744e8a2011-05-02 20:50:54 +02001471#endif
1472
Stephen Warrend26bc492012-03-16 14:54:25 -06001473static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1474{
1475 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1476
1477 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301478 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001479 !ops->get_group_name ||
1480 !ops->get_group_pins)
1481 return -EINVAL;
1482
Stephen Warren57291ce2012-03-23 10:29:46 -06001483 if (ops->dt_node_to_map && !ops->dt_free_map)
1484 return -EINVAL;
1485
Stephen Warrend26bc492012-03-16 14:54:25 -06001486 return 0;
1487}
1488
Linus Walleij2744e8a2011-05-02 20:50:54 +02001489/**
1490 * pinctrl_register() - register a pin controller device
1491 * @pctldesc: descriptor for this pin controller
1492 * @dev: parent device for this pin controller
1493 * @driver_data: private pin controller data for this pin controller
1494 */
1495struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1496 struct device *dev, void *driver_data)
1497{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001498 struct pinctrl_dev *pctldev;
1499 int ret;
1500
Devendra Nagada9aecb2012-06-16 23:43:16 +05301501 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001502 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301503 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001504 return NULL;
1505
Stephen Warren02f5b982012-02-22 14:26:00 -07001506 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001507 if (pctldev == NULL) {
1508 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001509 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001510 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001511
1512 /* Initialize pin control device struct */
1513 pctldev->owner = pctldesc->owner;
1514 pctldev->desc = pctldesc;
1515 pctldev->driver_data = driver_data;
1516 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001517 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001518 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001519
Stephen Warrend26bc492012-03-16 14:54:25 -06001520 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301521 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001522 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001523 goto out_err;
1524 }
1525
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001526 /* If we're implementing pinmuxing, check the ops for sanity */
1527 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301528 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001529 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001530 }
1531
1532 /* If we're implementing pinconfig, check the ops for sanity */
1533 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301534 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001535 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001536 }
1537
Linus Walleij2744e8a2011-05-02 20:50:54 +02001538 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001539 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001540 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1541 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001542 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001543 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1544 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001545 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001546 }
1547
Stephen Warren57b676f2012-03-02 13:05:44 -07001548 mutex_lock(&pinctrl_mutex);
1549
Stephen Warren8b9c1392012-02-19 23:45:42 -07001550 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001551
Stephen Warren6e5e9592012-03-02 13:05:47 -07001552 pctldev->p = pinctrl_get_locked(pctldev->dev);
1553 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001554 pctldev->hog_default =
Stephen Warren6e5e9592012-03-02 13:05:47 -07001555 pinctrl_lookup_state_locked(pctldev->p,
1556 PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001557 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001558 dev_dbg(dev, "failed to lookup the default state\n");
1559 } else {
Julien Delacou840a47b2012-12-10 14:47:33 +01001560 if (pinctrl_select_state_locked(pctldev->p,
1561 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001562 dev_err(dev,
1563 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001564 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001565
1566 pctldev->hog_sleep =
1567 pinctrl_lookup_state_locked(pctldev->p,
1568 PINCTRL_STATE_SLEEP);
1569 if (IS_ERR(pctldev->hog_sleep))
1570 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001571 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001572
1573 mutex_unlock(&pinctrl_mutex);
1574
Stephen Warren2304b472012-02-22 14:26:01 -07001575 pinctrl_init_device_debugfs(pctldev);
1576
Linus Walleij2744e8a2011-05-02 20:50:54 +02001577 return pctldev;
1578
Stephen Warren51cd24e2011-12-09 16:59:05 -07001579out_err:
1580 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001581 return NULL;
1582}
1583EXPORT_SYMBOL_GPL(pinctrl_register);
1584
1585/**
1586 * pinctrl_unregister() - unregister pinmux
1587 * @pctldev: pin controller to unregister
1588 *
1589 * Called by pinmux drivers to unregister a pinmux.
1590 */
1591void pinctrl_unregister(struct pinctrl_dev *pctldev)
1592{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001593 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001594 if (pctldev == NULL)
1595 return;
1596
Tony Lindgren02157162012-01-20 08:17:22 -08001597 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001598
1599 mutex_lock(&pinctrl_mutex);
1600
Stephen Warren6e5e9592012-03-02 13:05:47 -07001601 if (!IS_ERR(pctldev->p))
1602 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001603
Linus Walleij2744e8a2011-05-02 20:50:54 +02001604 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001605 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001606 /* Destroy descriptor tree */
1607 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1608 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001609 /* remove gpio ranges map */
1610 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1611 list_del(&range->node);
1612
Stephen Warren51cd24e2011-12-09 16:59:05 -07001613 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001614
1615 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001616}
1617EXPORT_SYMBOL_GPL(pinctrl_unregister);
1618
1619static int __init pinctrl_init(void)
1620{
1621 pr_info("initialized pinctrl subsystem\n");
1622 pinctrl_init_debugfs();
1623 return 0;
1624}
1625
1626/* init early since many drivers really need to initialized pinmux early */
1627core_initcall(pinctrl_init);