blob: deb3d04323b04edd77971df2de20456f66a5cf6e [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 Zhuang2afe8222013-03-28 07:34:19 +080030
31#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080032#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080033#endif
34
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060036#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020038#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
Stephen Warrenb2b3e662012-02-19 23:45:43 -070040
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080041static bool pinctrl_dummy_state;
42
Stephen Warren57b676f2012-03-02 13:05:44 -070043/* Mutex taken by all entry points */
44DEFINE_MUTEX(pinctrl_mutex);
45
46/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060047LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020048
Stephen Warren57b676f2012-03-02 13:05:44 -070049/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010050static LIST_HEAD(pinctrl_list);
51
Stephen Warren57b676f2012-03-02 13:05:44 -070052/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010053LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070054
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080056/**
57 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
58 *
59 * Usually this function is called by platforms without pinctrl driver support
60 * but run with some shared drivers using pinctrl APIs.
61 * After calling this function, the pinctrl core will return successfully
62 * with creating a dummy state for the driver to keep going smoothly.
63 */
64void pinctrl_provide_dummies(void)
65{
66 pinctrl_dummy_state = true;
67}
68
Linus Walleij2744e8a2011-05-02 20:50:54 +020069const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
70{
71 /* We're not allowed to register devices without name */
72 return pctldev->desc->name;
73}
74EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
75
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080076const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
77{
78 return dev_name(pctldev->dev);
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
81
Linus Walleij2744e8a2011-05-02 20:50:54 +020082void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
83{
84 return pctldev->driver_data;
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
87
88/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010089 * get_pinctrl_dev_from_devname() - look up pin controller device
90 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020091 *
92 * Looks up a pin control device matching a certain device name or pure device
93 * pointer, the pure device pointer will take precedence.
94 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020096{
97 struct pinctrl_dev *pctldev = NULL;
98 bool found = false;
99
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100100 if (!devname)
101 return NULL;
102
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100104 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105 /* Matched on device name */
106 found = true;
107 break;
108 }
109 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110
111 return found ? pctldev : NULL;
112}
113
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200115 * pin_get_from_name() - look up a pin number from a name
116 * @pctldev: the pin control device to lookup the pin on
117 * @name: the name of the pin to look up
118 */
119int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
120{
Chanho Park706e8522012-01-03 16:47:50 +0900121 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200122
Chanho Park706e8522012-01-03 16:47:50 +0900123 /* The pin number can be retrived from the pin controller descriptor */
124 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200125 struct pin_desc *desc;
126
Chanho Park706e8522012-01-03 16:47:50 +0900127 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200128 desc = pin_desc_get(pctldev, pin);
129 /* Pin space may be sparse */
130 if (desc == NULL)
131 continue;
132 if (desc->name && !strcmp(name, desc->name))
133 return pin;
134 }
135
136 return -EINVAL;
137}
138
139/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800140 * pin_get_name_from_id() - look up a pin name from a pin id
141 * @pctldev: the pin control device to lookup the pin on
142 * @name: the name of the pin to look up
143 */
144const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
145{
146 const struct pin_desc *desc;
147
148 desc = pin_desc_get(pctldev, pin);
149 if (desc == NULL) {
150 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
151 pin);
152 return NULL;
153 }
154
155 return desc->name;
156}
157
158/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200159 * pin_is_valid() - check if pin exists on controller
160 * @pctldev: the pin control device to check the pin on
161 * @pin: pin to check, use the local pin controller index number
162 *
163 * This tells us whether a certain pin exist on a certain pin controller or
164 * not. Pin lists may be sparse, so some pins may not exist.
165 */
166bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
167{
168 struct pin_desc *pindesc;
169
170 if (pin < 0)
171 return false;
172
Stephen Warren57b676f2012-03-02 13:05:44 -0700173 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200174 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700175 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200176
Stephen Warren57b676f2012-03-02 13:05:44 -0700177 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178}
179EXPORT_SYMBOL_GPL(pin_is_valid);
180
181/* Deletes a range of pin descriptors */
182static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
183 const struct pinctrl_pin_desc *pins,
184 unsigned num_pins)
185{
186 int i;
187
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188 for (i = 0; i < num_pins; i++) {
189 struct pin_desc *pindesc;
190
191 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
192 pins[i].number);
193 if (pindesc != NULL) {
194 radix_tree_delete(&pctldev->pin_desc_tree,
195 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100196 if (pindesc->dynamic_name)
197 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 }
199 kfree(pindesc);
200 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201}
202
203static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
204 unsigned number, const char *name)
205{
206 struct pin_desc *pindesc;
207
208 pindesc = pin_desc_get(pctldev, number);
209 if (pindesc != NULL) {
210 pr_err("pin %d already registered on %s\n", number,
211 pctldev->desc->name);
212 return -EINVAL;
213 }
214
215 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700216 if (pindesc == NULL) {
217 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700219 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200220
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 /* Set owner */
222 pindesc->pctldev = pctldev;
223
Stephen Warren9af1e442011-10-19 16:19:27 -0600224 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100225 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100226 pindesc->name = name;
227 } else {
228 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530229 if (pindesc->name == NULL) {
230 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100231 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530232 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100233 pindesc->dynamic_name = true;
234 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200237 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100238 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 return 0;
240}
241
242static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
243 struct pinctrl_pin_desc const *pins,
244 unsigned num_descs)
245{
246 unsigned i;
247 int ret = 0;
248
249 for (i = 0; i < num_descs; i++) {
250 ret = pinctrl_register_one_pin(pctldev,
251 pins[i].number, pins[i].name);
252 if (ret)
253 return ret;
254 }
255
256 return 0;
257}
258
259/**
260 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
261 * @pctldev: pin controller device to check
262 * @gpio: gpio pin to check taken from the global GPIO pin space
263 *
264 * Tries to match a GPIO pin number to the ranges handled by a certain pin
265 * controller, return the range or NULL
266 */
267static struct pinctrl_gpio_range *
268pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
269{
270 struct pinctrl_gpio_range *range = NULL;
271
272 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200273 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
274 /* Check if we're in the valid range */
275 if (gpio >= range->base &&
276 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277 return range;
278 }
279 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200280
281 return NULL;
282}
283
284/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800285 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
286 * the same GPIO chip are in range
287 * @gpio: gpio pin to check taken from the global GPIO pin space
288 *
289 * This function is complement of pinctrl_match_gpio_range(). If the return
290 * value of pinctrl_match_gpio_range() is NULL, this function could be used
291 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
292 * of the same GPIO chip don't have back-end pinctrl interface.
293 * If the return value is true, it means that pinctrl device is ready & the
294 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
295 * is false, it means that pinctrl device may not be ready.
296 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800297#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800298static bool pinctrl_ready_for_gpio_range(unsigned gpio)
299{
300 struct pinctrl_dev *pctldev;
301 struct pinctrl_gpio_range *range = NULL;
302 struct gpio_chip *chip = gpio_to_chip(gpio);
303
304 /* Loop over the pin controllers */
305 list_for_each_entry(pctldev, &pinctrldev_list, node) {
306 /* Loop over the ranges */
307 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
308 /* Check if any gpio range overlapped with gpio chip */
309 if (range->base + range->npins - 1 < chip->base ||
310 range->base > chip->base + chip->ngpio - 1)
311 continue;
312 return true;
313 }
314 }
315 return false;
316}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800317#else
318static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
319#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800320
321/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322 * pinctrl_get_device_gpio_range() - find device for GPIO range
323 * @gpio: the pin to locate the pin controller for
324 * @outdev: the pin control device if found
325 * @outrange: the GPIO range if found
326 *
327 * Find the pin controller handling a certain GPIO pin from the pinspace of
328 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800329 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
330 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700332static int pinctrl_get_device_gpio_range(unsigned gpio,
333 struct pinctrl_dev **outdev,
334 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200335{
336 struct pinctrl_dev *pctldev = NULL;
337
338 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200339 list_for_each_entry(pctldev, &pinctrldev_list, node) {
340 struct pinctrl_gpio_range *range;
341
342 range = pinctrl_match_gpio_range(pctldev, gpio);
343 if (range != NULL) {
344 *outdev = pctldev;
345 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200346 return 0;
347 }
348 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200349
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800350 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200351}
352
353/**
354 * pinctrl_add_gpio_range() - register a GPIO range for a controller
355 * @pctldev: pin controller device to add the range to
356 * @range: the GPIO range to add
357 *
358 * This adds a range of GPIOs to be handled by a certain pin controller. Call
359 * this to register handled ranges after registering your pin controller.
360 */
361void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
362 struct pinctrl_gpio_range *range)
363{
Stephen Warren57b676f2012-03-02 13:05:44 -0700364 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700365 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700366 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700368EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200369
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800370void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
371 struct pinctrl_gpio_range *ranges,
372 unsigned nranges)
373{
374 int i;
375
376 for (i = 0; i < nranges; i++)
377 pinctrl_add_gpio_range(pctldev, &ranges[i]);
378}
379EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
380
Linus Walleij192c3692012-11-20 14:03:37 +0100381struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530382 struct pinctrl_gpio_range *range)
383{
384 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
385
Linus Walleijdfa97512012-11-20 14:54:18 +0100386 /*
387 * If we can't find this device, let's assume that is because
388 * it has not probed yet, so the driver trying to register this
389 * range need to defer probing.
390 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530391 if (!pctldev)
Linus Walleijdfa97512012-11-20 14:54:18 +0100392 return ERR_PTR(-EPROBE_DEFER);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530393
394 pinctrl_add_gpio_range(pctldev, range);
395 return pctldev;
396}
Linus Walleij192c3692012-11-20 14:03:37 +0100397EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530398
Linus Walleij2744e8a2011-05-02 20:50:54 +0200399/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100400 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
401 * @pctldev: the pin controller device to look in
402 * @pin: a controller-local number to find the range for
403 */
404struct pinctrl_gpio_range *
405pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
406 unsigned int pin)
407{
408 struct pinctrl_gpio_range *range = NULL;
409
410 /* Loop over the ranges */
411 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
412 /* Check if we're in the valid range */
413 if (pin >= range->pin_base &&
414 pin < range->pin_base + range->npins) {
415 return range;
416 }
417 }
418
419 return NULL;
420}
421EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
422
423/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530424 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
425 * @pctldev: pin controller device to remove the range from
426 * @range: the GPIO range to remove
427 */
428void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
429 struct pinctrl_gpio_range *range)
430{
431 mutex_lock(&pinctrl_mutex);
432 list_del(&range->node);
433 mutex_unlock(&pinctrl_mutex);
434}
435EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
436
437/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200438 * pinctrl_get_group_selector() - returns the group selector for a group
439 * @pctldev: the pin controller handling the group
440 * @pin_group: the pin group to look up
441 */
442int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
443 const char *pin_group)
444{
445 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530446 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200447 unsigned group_selector = 0;
448
Viresh Kumard1e90e92012-03-30 11:25:40 +0530449 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200450 const char *gname = pctlops->get_group_name(pctldev,
451 group_selector);
452 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700453 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200454 "found group selector %u for %s\n",
455 group_selector,
456 pin_group);
457 return group_selector;
458 }
459
460 group_selector++;
461 }
462
Stephen Warren51cd24e2011-12-09 16:59:05 -0700463 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200464 pin_group);
465
466 return -EINVAL;
467}
468
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100469/**
470 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
471 * @gpio: the GPIO pin number from the GPIO subsystem number space
472 *
473 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
474 * as part of their gpio_request() semantics, platforms and individual drivers
475 * shall *NOT* request GPIO pins to be muxed in.
476 */
477int pinctrl_request_gpio(unsigned gpio)
478{
479 struct pinctrl_dev *pctldev;
480 struct pinctrl_gpio_range *range;
481 int ret;
482 int pin;
483
Stephen Warren57b676f2012-03-02 13:05:44 -0700484 mutex_lock(&pinctrl_mutex);
485
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100486 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700487 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800488 if (pinctrl_ready_for_gpio_range(gpio))
489 ret = 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700490 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800491 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700492 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100493
494 /* Convert to the pin controllers number space */
495 pin = gpio - range->base + range->pin_base;
496
Stephen Warren57b676f2012-03-02 13:05:44 -0700497 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
498
499 mutex_unlock(&pinctrl_mutex);
500 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100501}
502EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
503
504/**
505 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
506 * @gpio: the GPIO pin number from the GPIO subsystem number space
507 *
508 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
509 * as part of their gpio_free() semantics, platforms and individual drivers
510 * shall *NOT* request GPIO pins to be muxed out.
511 */
512void pinctrl_free_gpio(unsigned gpio)
513{
514 struct pinctrl_dev *pctldev;
515 struct pinctrl_gpio_range *range;
516 int ret;
517 int pin;
518
Stephen Warren57b676f2012-03-02 13:05:44 -0700519 mutex_lock(&pinctrl_mutex);
520
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100521 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700522 if (ret) {
523 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100524 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700525 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100526
527 /* Convert to the pin controllers number space */
528 pin = gpio - range->base + range->pin_base;
529
Stephen Warren57b676f2012-03-02 13:05:44 -0700530 pinmux_free_gpio(pctldev, pin, range);
531
532 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100533}
534EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
535
536static int pinctrl_gpio_direction(unsigned gpio, bool input)
537{
538 struct pinctrl_dev *pctldev;
539 struct pinctrl_gpio_range *range;
540 int ret;
541 int pin;
542
543 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
544 if (ret)
545 return ret;
546
547 /* Convert to the pin controllers number space */
548 pin = gpio - range->base + range->pin_base;
549
550 return pinmux_gpio_direction(pctldev, range, pin, input);
551}
552
553/**
554 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
555 * @gpio: the GPIO pin number from the GPIO subsystem number space
556 *
557 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
558 * as part of their gpio_direction_input() semantics, platforms and individual
559 * drivers shall *NOT* touch pin control GPIO calls.
560 */
561int pinctrl_gpio_direction_input(unsigned gpio)
562{
Stephen Warren57b676f2012-03-02 13:05:44 -0700563 int ret;
564 mutex_lock(&pinctrl_mutex);
565 ret = pinctrl_gpio_direction(gpio, true);
566 mutex_unlock(&pinctrl_mutex);
567 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100568}
569EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
570
571/**
572 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
573 * @gpio: the GPIO pin number from the GPIO subsystem number space
574 *
575 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
576 * as part of their gpio_direction_output() semantics, platforms and individual
577 * drivers shall *NOT* touch pin control GPIO calls.
578 */
579int pinctrl_gpio_direction_output(unsigned gpio)
580{
Stephen Warren57b676f2012-03-02 13:05:44 -0700581 int ret;
582 mutex_lock(&pinctrl_mutex);
583 ret = pinctrl_gpio_direction(gpio, false);
584 mutex_unlock(&pinctrl_mutex);
585 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100586}
587EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
588
Stephen Warren6e5e9592012-03-02 13:05:47 -0700589static struct pinctrl_state *find_state(struct pinctrl *p,
590 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100591{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700592 struct pinctrl_state *state;
593
594 list_for_each_entry(state, &p->states, node)
595 if (!strcmp(state->name, name))
596 return state;
597
598 return NULL;
599}
600
601static struct pinctrl_state *create_state(struct pinctrl *p,
602 const char *name)
603{
604 struct pinctrl_state *state;
605
606 state = kzalloc(sizeof(*state), GFP_KERNEL);
607 if (state == NULL) {
608 dev_err(p->dev,
609 "failed to alloc struct pinctrl_state\n");
610 return ERR_PTR(-ENOMEM);
611 }
612
613 state->name = name;
614 INIT_LIST_HEAD(&state->settings);
615
616 list_add_tail(&state->node, &p->states);
617
618 return state;
619}
620
621static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
622{
623 struct pinctrl_state *state;
624 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700625 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700626
627 state = find_state(p, map->name);
628 if (!state)
629 state = create_state(p, map->name);
630 if (IS_ERR(state))
631 return PTR_ERR(state);
632
Stephen Warren1e2082b2012-03-02 13:05:48 -0700633 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
634 return 0;
635
Stephen Warren6e5e9592012-03-02 13:05:47 -0700636 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
637 if (setting == NULL) {
638 dev_err(p->dev,
639 "failed to alloc struct pinctrl_setting\n");
640 return -ENOMEM;
641 }
642
Stephen Warren1e2082b2012-03-02 13:05:48 -0700643 setting->type = map->type;
644
Stephen Warren6e5e9592012-03-02 13:05:47 -0700645 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
646 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700647 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100648 /* Do not defer probing of hogs (circular loop) */
649 if (!strcmp(map->ctrl_dev_name, map->dev_name))
650 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200651 /*
652 * OK let us guess that the driver is not there yet, and
653 * let's defer obtaining this pinctrl handle to later...
654 */
Linus Walleij89216492012-12-11 14:14:32 +0100655 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
656 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200657 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700658 }
659
Linus Walleij1a789582012-10-17 20:51:54 +0200660 setting->dev_name = map->dev_name;
661
Stephen Warren1e2082b2012-03-02 13:05:48 -0700662 switch (map->type) {
663 case PIN_MAP_TYPE_MUX_GROUP:
664 ret = pinmux_map_to_setting(map, setting);
665 break;
666 case PIN_MAP_TYPE_CONFIGS_PIN:
667 case PIN_MAP_TYPE_CONFIGS_GROUP:
668 ret = pinconf_map_to_setting(map, setting);
669 break;
670 default:
671 ret = -EINVAL;
672 break;
673 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700674 if (ret < 0) {
675 kfree(setting);
676 return ret;
677 }
678
679 list_add_tail(&setting->node, &state->settings);
680
681 return 0;
682}
683
684static struct pinctrl *find_pinctrl(struct device *dev)
685{
686 struct pinctrl *p;
687
Stephen Warren1e2082b2012-03-02 13:05:48 -0700688 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700689 if (p->dev == dev)
690 return p;
691
692 return NULL;
693}
694
695static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
696
697static struct pinctrl *create_pinctrl(struct device *dev)
698{
699 struct pinctrl *p;
700 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700701 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100702 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700703 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700704 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100705
706 /*
707 * create the state cookie holder struct pinctrl for each
708 * mapping, this is what consumers will get when requesting
709 * a pin control handle with pinctrl_get()
710 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700711 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700712 if (p == NULL) {
713 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100714 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700715 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700716 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700717 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600718 INIT_LIST_HEAD(&p->dt_maps);
719
720 ret = pinctrl_dt_to_map(p);
721 if (ret < 0) {
722 kfree(p);
723 return ERR_PTR(ret);
724 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700725
726 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100727
728 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700729 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700730 /* Map must be for this device */
731 if (strcmp(map->dev_name, devname))
732 continue;
733
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100735 /*
736 * At this point the adding of a setting may:
737 *
738 * - Defer, if the pinctrl device is not yet available
739 * - Fail, if the pinctrl device is not yet available,
740 * AND the setting is a hog. We cannot defer that, since
741 * the hog will kick in immediately after the device
742 * is registered.
743 *
744 * If the error returned was not -EPROBE_DEFER then we
745 * accumulate the errors to see if we end up with
746 * an -EPROBE_DEFER later, as that is the worst case.
747 */
748 if (ret == -EPROBE_DEFER) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749 pinctrl_put_locked(p, false);
750 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100751 }
752 }
Linus Walleij89216492012-12-11 14:14:32 +0100753 if (ret < 0) {
754 /* If some other error than deferral occured, return here */
755 pinctrl_put_locked(p, false);
756 return ERR_PTR(ret);
757 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100758
Linus Walleijab780292013-01-22 10:56:14 -0700759 kref_init(&p->users);
760
Linus Walleijb0666ba2012-12-11 13:12:35 +0100761 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700762 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100763
764 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700765}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700766
Stephen Warren6e5e9592012-03-02 13:05:47 -0700767static struct pinctrl *pinctrl_get_locked(struct device *dev)
768{
769 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700770
Stephen Warren6e5e9592012-03-02 13:05:47 -0700771 if (WARN_ON(!dev))
772 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700773
Linus Walleijab780292013-01-22 10:56:14 -0700774 /*
775 * See if somebody else (such as the device core) has already
776 * obtained a handle to the pinctrl for this device. In that case,
777 * return another pointer to it.
778 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700780 if (p != NULL) {
781 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
782 kref_get(&p->users);
783 return p;
784 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785
Richard Genoudd599bfb2012-08-10 16:53:49 +0200786 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100787}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700788
789/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700790 * pinctrl_get() - retrieves the pinctrl handle for a device
791 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700792 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700793struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700794{
795 struct pinctrl *p;
796
Stephen Warren57b676f2012-03-02 13:05:44 -0700797 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700799 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700800
801 return p;
802}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803EXPORT_SYMBOL_GPL(pinctrl_get);
804
Richard Genoudd3cee8302013-03-25 15:47:21 +0100805static void pinctrl_free_setting(bool disable_setting,
806 struct pinctrl_setting *setting)
807{
808 switch (setting->type) {
809 case PIN_MAP_TYPE_MUX_GROUP:
810 if (disable_setting)
811 pinmux_disable_setting(setting);
812 pinmux_free_setting(setting);
813 break;
814 case PIN_MAP_TYPE_CONFIGS_PIN:
815 case PIN_MAP_TYPE_CONFIGS_GROUP:
816 pinconf_free_setting(setting);
817 break;
818 default:
819 break;
820 }
821}
822
Stephen Warren6e5e9592012-03-02 13:05:47 -0700823static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700824{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700825 struct pinctrl_state *state, *n1;
826 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700827
Stephen Warren6e5e9592012-03-02 13:05:47 -0700828 list_for_each_entry_safe(state, n1, &p->states, node) {
829 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100830 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700831 list_del(&setting->node);
832 kfree(setting);
833 }
834 list_del(&state->node);
835 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700836 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700837
Stephen Warren57291ce2012-03-23 10:29:46 -0600838 pinctrl_dt_free_maps(p);
839
Stephen Warren6e5e9592012-03-02 13:05:47 -0700840 if (inlist)
841 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700842 kfree(p);
843}
844
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845/**
Linus Walleijab780292013-01-22 10:56:14 -0700846 * pinctrl_release() - release the pinctrl handle
847 * @kref: the kref in the pinctrl being released
848 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530849static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700850{
851 struct pinctrl *p = container_of(kref, struct pinctrl, users);
852
853 pinctrl_put_locked(p, true);
854}
855
856/**
857 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700858 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100859 */
860void pinctrl_put(struct pinctrl *p)
861{
Stephen Warren57b676f2012-03-02 13:05:44 -0700862 mutex_lock(&pinctrl_mutex);
Linus Walleijab780292013-01-22 10:56:14 -0700863 kref_put(&p->users, pinctrl_release);
Stephen Warren57b676f2012-03-02 13:05:44 -0700864 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100865}
866EXPORT_SYMBOL_GPL(pinctrl_put);
867
Stephen Warren6e5e9592012-03-02 13:05:47 -0700868static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
869 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700870{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700871 struct pinctrl_state *state;
872
873 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800874 if (!state) {
875 if (pinctrl_dummy_state) {
876 /* create dummy state */
877 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
878 name);
879 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200880 } else
881 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800882 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700883
884 return state;
885}
886
887/**
888 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
889 * @p: the pinctrl handle to retrieve the state from
890 * @name: the state name to retrieve
891 */
892struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
893{
894 struct pinctrl_state *s;
895
896 mutex_lock(&pinctrl_mutex);
897 s = pinctrl_lookup_state_locked(p, name);
898 mutex_unlock(&pinctrl_mutex);
899
900 return s;
901}
902EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
903
904static int pinctrl_select_state_locked(struct pinctrl *p,
905 struct pinctrl_state *state)
906{
907 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100908 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700909 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700910
Stephen Warren6e5e9592012-03-02 13:05:47 -0700911 if (p->state == state)
912 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700913
Stephen Warren6e5e9592012-03-02 13:05:47 -0700914 if (p->state) {
915 /*
916 * The set of groups with a mux configuration in the old state
917 * may not be identical to the set of groups with a mux setting
918 * in the new state. While this might be unusual, it's entirely
919 * possible for the "user"-supplied mapping table to be written
920 * that way. For each group that was configured in the old state
921 * but not in the new state, this code puts that group into a
922 * safe/disabled state.
923 */
924 list_for_each_entry(setting, &p->state->settings, node) {
925 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700926 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
927 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700929 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
930 continue;
931 if (setting2->data.mux.group ==
932 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933 found = true;
934 break;
935 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700936 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700937 if (!found)
938 pinmux_disable_setting(setting);
939 }
940 }
941
Richard Genoud3102a762013-03-25 15:47:22 +0100942 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700943
944 /* Apply all the settings for the new state */
945 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700946 switch (setting->type) {
947 case PIN_MAP_TYPE_MUX_GROUP:
948 ret = pinmux_enable_setting(setting);
949 break;
950 case PIN_MAP_TYPE_CONFIGS_PIN:
951 case PIN_MAP_TYPE_CONFIGS_GROUP:
952 ret = pinconf_apply_setting(setting);
953 break;
954 default:
955 ret = -EINVAL;
956 break;
957 }
Richard Genoud3102a762013-03-25 15:47:22 +0100958
Richard Genoudda587512013-03-28 12:55:46 +0100959 if (ret < 0)
Richard Genoud3102a762013-03-25 15:47:22 +0100960 goto unapply_new_state;
Stephen Warren57b676f2012-03-02 13:05:44 -0700961 }
962
Richard Genoud3102a762013-03-25 15:47:22 +0100963 p->state = state;
964
Stephen Warren7ecdb162012-03-02 13:05:45 -0700965 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +0100966
967unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +0100968 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +0100969
Richard Genoud3102a762013-03-25 15:47:22 +0100970 list_for_each_entry(setting2, &state->settings, node) {
971 if (&setting2->node == &setting->node)
972 break;
Richard Genoudaf606172013-03-29 10:03:26 +0100973 /*
974 * All we can do here is pinmux_disable_setting.
975 * That means that some pins are muxed differently now
976 * than they were before applying the setting (We can't
977 * "unmux a pin"!), but it's not a big deal since the pins
978 * are free to be muxed by another apply_setting.
979 */
980 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
981 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +0100982 }
Richard Genoud8009d5f2013-03-28 12:55:47 +0100983
Richard Genoud50cf7c82013-03-25 15:47:23 +0100984 if (old_state) {
985 list_for_each_entry(setting, &old_state->settings, node) {
986 bool found = false;
987 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
988 continue;
989 list_for_each_entry(setting2, &state->settings, node) {
990 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
991 continue;
992 if (setting2->data.mux.group ==
993 setting->data.mux.group) {
994 found = true;
995 break;
996 }
997 }
998 if (!found)
999 pinmux_enable_setting(setting);
1000 }
1001 }
Richard Genoud175ca832013-03-28 12:55:48 +01001002
1003 p->state = old_state;
Richard Genoud3102a762013-03-25 15:47:22 +01001004 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001005}
1006
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001007/**
Stephen Warren6e5e9592012-03-02 13:05:47 -07001008 * pinctrl_select() - select/activate/program a pinctrl state to HW
1009 * @p: the pinctrl handle for the device that requests configuratio
1010 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001011 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001012int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001013{
Stephen Warren57b676f2012-03-02 13:05:44 -07001014 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001015
Stephen Warren57b676f2012-03-02 13:05:44 -07001016 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001017 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -07001018 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001019
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001020 return ret;
1021}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001022EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001023
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001024static void devm_pinctrl_release(struct device *dev, void *res)
1025{
1026 pinctrl_put(*(struct pinctrl **)res);
1027}
1028
1029/**
1030 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1031 * @dev: the device to obtain the handle for
1032 *
1033 * If there is a need to explicitly destroy the returned struct pinctrl,
1034 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1035 */
1036struct pinctrl *devm_pinctrl_get(struct device *dev)
1037{
1038 struct pinctrl **ptr, *p;
1039
1040 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1041 if (!ptr)
1042 return ERR_PTR(-ENOMEM);
1043
1044 p = pinctrl_get(dev);
1045 if (!IS_ERR(p)) {
1046 *ptr = p;
1047 devres_add(dev, ptr);
1048 } else {
1049 devres_free(ptr);
1050 }
1051
1052 return p;
1053}
1054EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1055
1056static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1057{
1058 struct pinctrl **p = res;
1059
1060 return *p == data;
1061}
1062
1063/**
1064 * devm_pinctrl_put() - Resource managed pinctrl_put()
1065 * @p: the pinctrl handle to release
1066 *
1067 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1068 * this function will not need to be called and the resource management
1069 * code will ensure that the resource is freed.
1070 */
1071void devm_pinctrl_put(struct pinctrl *p)
1072{
Jingoo Hana72149e2013-02-26 11:34:07 +09001073 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001074 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001075}
1076EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1077
Stephen Warren57291ce2012-03-23 10:29:46 -06001078int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1079 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001080{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001081 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001082 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001083
1084 pr_debug("add %d pinmux maps\n", num_maps);
1085
1086 /* First sanity check the new mapping */
1087 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001088 if (!maps[i].dev_name) {
1089 pr_err("failed to register map %s (%d): no device given\n",
1090 maps[i].name, i);
1091 return -EINVAL;
1092 }
1093
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001094 if (!maps[i].name) {
1095 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001096 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001097 return -EINVAL;
1098 }
1099
Stephen Warren1e2082b2012-03-02 13:05:48 -07001100 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1101 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001102 pr_err("failed to register map %s (%d): no pin control device given\n",
1103 maps[i].name, i);
1104 return -EINVAL;
1105 }
1106
Stephen Warren1e2082b2012-03-02 13:05:48 -07001107 switch (maps[i].type) {
1108 case PIN_MAP_TYPE_DUMMY_STATE:
1109 break;
1110 case PIN_MAP_TYPE_MUX_GROUP:
1111 ret = pinmux_validate_map(&maps[i], i);
1112 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001113 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001114 break;
1115 case PIN_MAP_TYPE_CONFIGS_PIN:
1116 case PIN_MAP_TYPE_CONFIGS_GROUP:
1117 ret = pinconf_validate_map(&maps[i], i);
1118 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001119 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001120 break;
1121 default:
1122 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001123 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001124 return -EINVAL;
1125 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001126 }
1127
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001128 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1129 if (!maps_node) {
1130 pr_err("failed to alloc struct pinctrl_maps\n");
1131 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001132 }
1133
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001134 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001135 if (dup) {
1136 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1137 GFP_KERNEL);
1138 if (!maps_node->maps) {
1139 pr_err("failed to duplicate mapping table\n");
1140 kfree(maps_node);
1141 return -ENOMEM;
1142 }
1143 } else {
1144 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001145 }
1146
Stephen Warren57291ce2012-03-23 10:29:46 -06001147 if (!locked)
1148 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001149 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001150 if (!locked)
1151 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001152
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001153 return 0;
1154}
1155
Stephen Warren57291ce2012-03-23 10:29:46 -06001156/**
1157 * pinctrl_register_mappings() - register a set of pin controller mappings
1158 * @maps: the pincontrol mappings table to register. This should probably be
1159 * marked with __initdata so it can be discarded after boot. This
1160 * function will perform a shallow copy for the mapping entries.
1161 * @num_maps: the number of maps in the mapping table
1162 */
1163int pinctrl_register_mappings(struct pinctrl_map const *maps,
1164 unsigned num_maps)
1165{
1166 return pinctrl_register_map(maps, num_maps, true, false);
1167}
1168
1169void pinctrl_unregister_map(struct pinctrl_map const *map)
1170{
1171 struct pinctrl_maps *maps_node;
1172
1173 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1174 if (maps_node->maps == map) {
1175 list_del(&maps_node->node);
1176 return;
1177 }
1178 }
1179}
1180
Julien Delacou840a47b2012-12-10 14:47:33 +01001181/**
1182 * pinctrl_force_sleep() - turn a given controller device into sleep state
1183 * @pctldev: pin controller device
1184 */
1185int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1186{
1187 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1188 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1189 return 0;
1190}
1191EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1192
1193/**
1194 * pinctrl_force_default() - turn a given controller device into default state
1195 * @pctldev: pin controller device
1196 */
1197int pinctrl_force_default(struct pinctrl_dev *pctldev)
1198{
1199 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1200 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1201 return 0;
1202}
1203EXPORT_SYMBOL_GPL(pinctrl_force_default);
1204
Linus Walleij2744e8a2011-05-02 20:50:54 +02001205#ifdef CONFIG_DEBUG_FS
1206
1207static int pinctrl_pins_show(struct seq_file *s, void *what)
1208{
1209 struct pinctrl_dev *pctldev = s->private;
1210 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001211 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001212
1213 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001214
Stephen Warren57b676f2012-03-02 13:05:44 -07001215 mutex_lock(&pinctrl_mutex);
1216
Chanho Park706e8522012-01-03 16:47:50 +09001217 /* The pin number can be retrived from the pin controller descriptor */
1218 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001219 struct pin_desc *desc;
1220
Chanho Park706e8522012-01-03 16:47:50 +09001221 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001222 desc = pin_desc_get(pctldev, pin);
1223 /* Pin space may be sparse */
1224 if (desc == NULL)
1225 continue;
1226
1227 seq_printf(s, "pin %d (%s) ", pin,
1228 desc->name ? desc->name : "unnamed");
1229
1230 /* Driver-specific info per pin */
1231 if (ops->pin_dbg_show)
1232 ops->pin_dbg_show(pctldev, s, pin);
1233
1234 seq_puts(s, "\n");
1235 }
1236
Stephen Warren57b676f2012-03-02 13:05:44 -07001237 mutex_unlock(&pinctrl_mutex);
1238
Linus Walleij2744e8a2011-05-02 20:50:54 +02001239 return 0;
1240}
1241
1242static int pinctrl_groups_show(struct seq_file *s, void *what)
1243{
1244 struct pinctrl_dev *pctldev = s->private;
1245 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301246 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001247
Viresh Kumard1e90e92012-03-30 11:25:40 +05301248 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001249 mutex_lock(&pinctrl_mutex);
1250
Linus Walleij2744e8a2011-05-02 20:50:54 +02001251 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301252 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001253 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001254 unsigned num_pins;
1255 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001256 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001257 int ret;
1258 int i;
1259
1260 ret = ops->get_group_pins(pctldev, selector,
1261 &pins, &num_pins);
1262 if (ret)
1263 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1264 gname);
1265 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001266 seq_printf(s, "group: %s\n", gname);
1267 for (i = 0; i < num_pins; i++) {
1268 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001269 if (WARN_ON(!pname)) {
1270 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001271 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001272 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001273 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1274 }
1275 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001276 }
1277 selector++;
1278 }
1279
Stephen Warren57b676f2012-03-02 13:05:44 -07001280 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001281
1282 return 0;
1283}
1284
1285static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1286{
1287 struct pinctrl_dev *pctldev = s->private;
1288 struct pinctrl_gpio_range *range = NULL;
1289
1290 seq_puts(s, "GPIO ranges handled:\n");
1291
Stephen Warren57b676f2012-03-02 13:05:44 -07001292 mutex_lock(&pinctrl_mutex);
1293
Linus Walleij2744e8a2011-05-02 20:50:54 +02001294 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001295 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001296 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1297 range->id, range->name,
1298 range->base, (range->base + range->npins - 1),
1299 range->pin_base,
1300 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001301 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001302
1303 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001304
1305 return 0;
1306}
1307
1308static int pinctrl_devices_show(struct seq_file *s, void *what)
1309{
1310 struct pinctrl_dev *pctldev;
1311
Linus Walleijae6b4d82011-10-19 18:14:33 +02001312 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001313
1314 mutex_lock(&pinctrl_mutex);
1315
Linus Walleij2744e8a2011-05-02 20:50:54 +02001316 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1317 seq_printf(s, "%s ", pctldev->desc->name);
1318 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001319 seq_puts(s, "yes ");
1320 else
1321 seq_puts(s, "no ");
1322 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001323 seq_puts(s, "yes");
1324 else
1325 seq_puts(s, "no");
1326 seq_puts(s, "\n");
1327 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001328
1329 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001330
1331 return 0;
1332}
1333
Stephen Warren1e2082b2012-03-02 13:05:48 -07001334static inline const char *map_type(enum pinctrl_map_type type)
1335{
1336 static const char * const names[] = {
1337 "INVALID",
1338 "DUMMY_STATE",
1339 "MUX_GROUP",
1340 "CONFIGS_PIN",
1341 "CONFIGS_GROUP",
1342 };
1343
1344 if (type >= ARRAY_SIZE(names))
1345 return "UNKNOWN";
1346
1347 return names[type];
1348}
1349
Stephen Warren3eedb432012-02-23 17:04:40 -07001350static int pinctrl_maps_show(struct seq_file *s, void *what)
1351{
1352 struct pinctrl_maps *maps_node;
1353 int i;
1354 struct pinctrl_map const *map;
1355
1356 seq_puts(s, "Pinctrl maps:\n");
1357
Stephen Warren57b676f2012-03-02 13:05:44 -07001358 mutex_lock(&pinctrl_mutex);
1359
Stephen Warren3eedb432012-02-23 17:04:40 -07001360 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001361 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1362 map->dev_name, map->name, map_type(map->type),
1363 map->type);
1364
1365 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1366 seq_printf(s, "controlling device %s\n",
1367 map->ctrl_dev_name);
1368
1369 switch (map->type) {
1370 case PIN_MAP_TYPE_MUX_GROUP:
1371 pinmux_show_map(s, map);
1372 break;
1373 case PIN_MAP_TYPE_CONFIGS_PIN:
1374 case PIN_MAP_TYPE_CONFIGS_GROUP:
1375 pinconf_show_map(s, map);
1376 break;
1377 default:
1378 break;
1379 }
1380
1381 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001382 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001383
1384 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001385
1386 return 0;
1387}
1388
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001389static int pinctrl_show(struct seq_file *s, void *what)
1390{
1391 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001392 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001393 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001394
1395 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001396
1397 mutex_lock(&pinctrl_mutex);
1398
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001399 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001400 seq_printf(s, "device: %s current state: %s\n",
1401 dev_name(p->dev),
1402 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001403
Stephen Warren6e5e9592012-03-02 13:05:47 -07001404 list_for_each_entry(state, &p->states, node) {
1405 seq_printf(s, " state: %s\n", state->name);
1406
1407 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001408 struct pinctrl_dev *pctldev = setting->pctldev;
1409
1410 seq_printf(s, " type: %s controller %s ",
1411 map_type(setting->type),
1412 pinctrl_dev_get_name(pctldev));
1413
1414 switch (setting->type) {
1415 case PIN_MAP_TYPE_MUX_GROUP:
1416 pinmux_show_setting(s, setting);
1417 break;
1418 case PIN_MAP_TYPE_CONFIGS_PIN:
1419 case PIN_MAP_TYPE_CONFIGS_GROUP:
1420 pinconf_show_setting(s, setting);
1421 break;
1422 default:
1423 break;
1424 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001425 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001426 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001427 }
1428
Stephen Warren57b676f2012-03-02 13:05:44 -07001429 mutex_unlock(&pinctrl_mutex);
1430
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001431 return 0;
1432}
1433
Linus Walleij2744e8a2011-05-02 20:50:54 +02001434static int pinctrl_pins_open(struct inode *inode, struct file *file)
1435{
1436 return single_open(file, pinctrl_pins_show, inode->i_private);
1437}
1438
1439static int pinctrl_groups_open(struct inode *inode, struct file *file)
1440{
1441 return single_open(file, pinctrl_groups_show, inode->i_private);
1442}
1443
1444static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1445{
1446 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1447}
1448
1449static int pinctrl_devices_open(struct inode *inode, struct file *file)
1450{
1451 return single_open(file, pinctrl_devices_show, NULL);
1452}
1453
Stephen Warren3eedb432012-02-23 17:04:40 -07001454static int pinctrl_maps_open(struct inode *inode, struct file *file)
1455{
1456 return single_open(file, pinctrl_maps_show, NULL);
1457}
1458
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001459static int pinctrl_open(struct inode *inode, struct file *file)
1460{
1461 return single_open(file, pinctrl_show, NULL);
1462}
1463
Linus Walleij2744e8a2011-05-02 20:50:54 +02001464static const struct file_operations pinctrl_pins_ops = {
1465 .open = pinctrl_pins_open,
1466 .read = seq_read,
1467 .llseek = seq_lseek,
1468 .release = single_release,
1469};
1470
1471static const struct file_operations pinctrl_groups_ops = {
1472 .open = pinctrl_groups_open,
1473 .read = seq_read,
1474 .llseek = seq_lseek,
1475 .release = single_release,
1476};
1477
1478static const struct file_operations pinctrl_gpioranges_ops = {
1479 .open = pinctrl_gpioranges_open,
1480 .read = seq_read,
1481 .llseek = seq_lseek,
1482 .release = single_release,
1483};
1484
1485static const struct file_operations pinctrl_devices_ops = {
1486 .open = pinctrl_devices_open,
1487 .read = seq_read,
1488 .llseek = seq_lseek,
1489 .release = single_release,
1490};
1491
Stephen Warren3eedb432012-02-23 17:04:40 -07001492static const struct file_operations pinctrl_maps_ops = {
1493 .open = pinctrl_maps_open,
1494 .read = seq_read,
1495 .llseek = seq_lseek,
1496 .release = single_release,
1497};
1498
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001499static const struct file_operations pinctrl_ops = {
1500 .open = pinctrl_open,
1501 .read = seq_read,
1502 .llseek = seq_lseek,
1503 .release = single_release,
1504};
1505
Linus Walleij2744e8a2011-05-02 20:50:54 +02001506static struct dentry *debugfs_root;
1507
1508static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1509{
Tony Lindgren02157162012-01-20 08:17:22 -08001510 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001511
Stephen Warren51cd24e2011-12-09 16:59:05 -07001512 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001513 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001514 pctldev->device_root = device_root;
1515
Linus Walleij2744e8a2011-05-02 20:50:54 +02001516 if (IS_ERR(device_root) || !device_root) {
1517 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001518 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001519 return;
1520 }
1521 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1522 device_root, pctldev, &pinctrl_pins_ops);
1523 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1524 device_root, pctldev, &pinctrl_groups_ops);
1525 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1526 device_root, pctldev, &pinctrl_gpioranges_ops);
1527 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001528 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001529}
1530
Tony Lindgren02157162012-01-20 08:17:22 -08001531static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1532{
1533 debugfs_remove_recursive(pctldev->device_root);
1534}
1535
Linus Walleij2744e8a2011-05-02 20:50:54 +02001536static void pinctrl_init_debugfs(void)
1537{
1538 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1539 if (IS_ERR(debugfs_root) || !debugfs_root) {
1540 pr_warn("failed to create debugfs directory\n");
1541 debugfs_root = NULL;
1542 return;
1543 }
1544
1545 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1546 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001547 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1548 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001549 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1550 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001551}
1552
1553#else /* CONFIG_DEBUG_FS */
1554
1555static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1556{
1557}
1558
1559static void pinctrl_init_debugfs(void)
1560{
1561}
1562
Tony Lindgren02157162012-01-20 08:17:22 -08001563static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1564{
1565}
1566
Linus Walleij2744e8a2011-05-02 20:50:54 +02001567#endif
1568
Stephen Warrend26bc492012-03-16 14:54:25 -06001569static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1570{
1571 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1572
1573 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301574 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001575 !ops->get_group_name ||
1576 !ops->get_group_pins)
1577 return -EINVAL;
1578
Stephen Warren57291ce2012-03-23 10:29:46 -06001579 if (ops->dt_node_to_map && !ops->dt_free_map)
1580 return -EINVAL;
1581
Stephen Warrend26bc492012-03-16 14:54:25 -06001582 return 0;
1583}
1584
Linus Walleij2744e8a2011-05-02 20:50:54 +02001585/**
1586 * pinctrl_register() - register a pin controller device
1587 * @pctldesc: descriptor for this pin controller
1588 * @dev: parent device for this pin controller
1589 * @driver_data: private pin controller data for this pin controller
1590 */
1591struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1592 struct device *dev, void *driver_data)
1593{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001594 struct pinctrl_dev *pctldev;
1595 int ret;
1596
Devendra Nagada9aecb2012-06-16 23:43:16 +05301597 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001598 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301599 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001600 return NULL;
1601
Stephen Warren02f5b982012-02-22 14:26:00 -07001602 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001603 if (pctldev == NULL) {
1604 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001605 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001606 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001607
1608 /* Initialize pin control device struct */
1609 pctldev->owner = pctldesc->owner;
1610 pctldev->desc = pctldesc;
1611 pctldev->driver_data = driver_data;
1612 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001613 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001614 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001615
Stephen Warrend26bc492012-03-16 14:54:25 -06001616 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301617 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001618 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001619 goto out_err;
1620 }
1621
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001622 /* If we're implementing pinmuxing, check the ops for sanity */
1623 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301624 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001625 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001626 }
1627
1628 /* If we're implementing pinconfig, check the ops for sanity */
1629 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301630 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001631 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001632 }
1633
Linus Walleij2744e8a2011-05-02 20:50:54 +02001634 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001635 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001636 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1637 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001638 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001639 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1640 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001641 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001642 }
1643
Stephen Warren57b676f2012-03-02 13:05:44 -07001644 mutex_lock(&pinctrl_mutex);
1645
Stephen Warren8b9c1392012-02-19 23:45:42 -07001646 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001647
Stephen Warren6e5e9592012-03-02 13:05:47 -07001648 pctldev->p = pinctrl_get_locked(pctldev->dev);
1649 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001650 pctldev->hog_default =
Stephen Warren6e5e9592012-03-02 13:05:47 -07001651 pinctrl_lookup_state_locked(pctldev->p,
1652 PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001653 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001654 dev_dbg(dev, "failed to lookup the default state\n");
1655 } else {
Julien Delacou840a47b2012-12-10 14:47:33 +01001656 if (pinctrl_select_state_locked(pctldev->p,
1657 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001658 dev_err(dev,
1659 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001660 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001661
1662 pctldev->hog_sleep =
1663 pinctrl_lookup_state_locked(pctldev->p,
1664 PINCTRL_STATE_SLEEP);
1665 if (IS_ERR(pctldev->hog_sleep))
1666 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001667 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001668
1669 mutex_unlock(&pinctrl_mutex);
1670
Stephen Warren2304b472012-02-22 14:26:01 -07001671 pinctrl_init_device_debugfs(pctldev);
1672
Linus Walleij2744e8a2011-05-02 20:50:54 +02001673 return pctldev;
1674
Stephen Warren51cd24e2011-12-09 16:59:05 -07001675out_err:
1676 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001677 return NULL;
1678}
1679EXPORT_SYMBOL_GPL(pinctrl_register);
1680
1681/**
1682 * pinctrl_unregister() - unregister pinmux
1683 * @pctldev: pin controller to unregister
1684 *
1685 * Called by pinmux drivers to unregister a pinmux.
1686 */
1687void pinctrl_unregister(struct pinctrl_dev *pctldev)
1688{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001689 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001690 if (pctldev == NULL)
1691 return;
1692
Tony Lindgren02157162012-01-20 08:17:22 -08001693 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001694
1695 mutex_lock(&pinctrl_mutex);
1696
Stephen Warren6e5e9592012-03-02 13:05:47 -07001697 if (!IS_ERR(pctldev->p))
1698 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001699
Linus Walleij2744e8a2011-05-02 20:50:54 +02001700 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001701 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001702 /* Destroy descriptor tree */
1703 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1704 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001705 /* remove gpio ranges map */
1706 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1707 list_del(&range->node);
1708
Stephen Warren51cd24e2011-12-09 16:59:05 -07001709 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001710
1711 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001712}
1713EXPORT_SYMBOL_GPL(pinctrl_unregister);
1714
1715static int __init pinctrl_init(void)
1716{
1717 pr_info("initialized pinctrl subsystem\n");
1718 pinctrl_init_debugfs();
1719 return 0;
1720}
1721
1722/* init early since many drivers really need to initialized pinmux early */
1723core_initcall(pinctrl_init);