blob: 4d0cc1889dd9dcb11d88f6f7647264e7ea632e99 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin muxing portions of the pin control subsystem
3 *
Linus Walleije93bcee2012-02-09 07:23:28 +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 Warren7ecdb162012-03-02 13:05:45 -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) "pinmux core: " fmt
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
21#include <linux/radix-tree.h>
22#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij97607d12011-11-29 12:52:39 +010024#include <linux/string.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020025#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/pinctrl/machine.h>
28#include <linux/pinctrl/pinmux.h>
29#include "core.h"
Linus Walleijbefe5bd2012-02-09 19:47:48 +010030#include "pinmux.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031
Stephen Warren03665e02012-02-19 23:45:45 -070032int pinmux_check_ops(struct pinctrl_dev *pctldev)
33{
34 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Dong Aishenga1d31f72012-04-06 20:18:09 +080035 unsigned nfuncs;
Stephen Warren03665e02012-02-19 23:45:45 -070036 unsigned selector = 0;
37
38 /* Check that we implement required operations */
Dong Aishenga1d31f72012-04-06 20:18:09 +080039 if (!ops ||
40 !ops->get_functions_count ||
Stephen Warren03665e02012-02-19 23:45:45 -070041 !ops->get_function_name ||
42 !ops->get_function_groups ||
Linus Walleij03e9f0c2014-09-03 13:02:56 +020043 !ops->set_mux) {
John Crispinad6e1102012-04-26 16:47:11 +020044 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
Stephen Warren03665e02012-02-19 23:45:45 -070045 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020046 }
Stephen Warren03665e02012-02-19 23:45:45 -070047 /* Check that all functions registered have names */
Dong Aishenga1d31f72012-04-06 20:18:09 +080048 nfuncs = ops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053049 while (selector < nfuncs) {
Stephen Warren03665e02012-02-19 23:45:45 -070050 const char *fname = ops->get_function_name(pctldev,
51 selector);
52 if (!fname) {
Dong Aishenga1d31f72012-04-06 20:18:09 +080053 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
Stephen Warren03665e02012-02-19 23:45:45 -070054 selector);
55 return -EINVAL;
56 }
57 selector++;
58 }
59
60 return 0;
61}
62
Masahiro Yamada3f713b72017-08-04 11:22:31 +090063int pinmux_validate_map(const struct pinctrl_map *map, int i)
Stephen Warren1e2082b2012-03-02 13:05:48 -070064{
65 if (!map->data.mux.function) {
66 pr_err("failed to register map %s (%d): no function given\n",
67 map->name, i);
68 return -EINVAL;
69 }
70
71 return 0;
72}
73
Linus Walleij2744e8a2011-05-02 20:50:54 +020074/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020075 * pin_request() - request a single pin to be muxed in, typically for GPIO
76 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -070077 * @owner: a representation of the owner of this pin; typically the device
78 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +020079 * @gpio_range: the range matching the GPIO pin if this is a request for a
80 * single GPIO pin
81 */
82static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -070083 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +020084 struct pinctrl_gpio_range *gpio_range)
85{
86 struct pin_desc *desc;
87 const struct pinmux_ops *ops = pctldev->desc->pmxops;
88 int status = -EINVAL;
89
Linus Walleij2744e8a2011-05-02 20:50:54 +020090 desc = pin_desc_get(pctldev, pin);
91 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070092 dev_err(pctldev->dev,
Stephen Warrend4705312012-05-01 11:14:15 -060093 "pin %d is not registered so it cannot be requested\n",
94 pin);
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 goto out;
96 }
97
Dong Aishengd0bd8df2012-04-17 15:00:45 +080098 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
99 pin, desc->name, owner);
100
Vladimir Zapolskiyb1eb8fa2016-12-25 02:59:28 +0200101 if ((!gpio_range || ops->strict) &&
102 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
103 dev_err(pctldev->dev,
104 "pin %s already requested by %s; cannot claim for %s\n",
105 desc->name, desc->mux_owner, owner);
106 goto out;
107 }
Stephen Warren652162d2012-03-05 17:22:15 -0700108
Vladimir Zapolskiyb1eb8fa2016-12-25 02:59:28 +0200109 if ((gpio_range || ops->strict) && desc->gpio_owner) {
110 dev_err(pctldev->dev,
111 "pin %s already requested by %s; cannot claim for %s\n",
112 desc->name, desc->gpio_owner, owner);
113 goto out;
114 }
115
116 if (gpio_range) {
Stephen Warren652162d2012-03-05 17:22:15 -0700117 desc->gpio_owner = owner;
118 } else {
Stephen Warren652162d2012-03-05 17:22:15 -0700119 desc->mux_usecount++;
120 if (desc->mux_usecount > 1)
121 return 0;
122
123 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200124 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700125
Linus Walleij2744e8a2011-05-02 20:50:54 +0200126 /* Let each pin increase references to this module */
127 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700128 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200129 "could not increase module refcount for pin %d\n",
130 pin);
131 status = -EINVAL;
132 goto out_free_pin;
133 }
134
135 /*
136 * If there is no kind of request function for the pin we just assume
137 * we got it by default and proceed.
138 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600139 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140 /* This requests and enables a single GPIO pin */
141 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
142 else if (ops->request)
143 status = ops->request(pctldev, pin);
144 else
145 status = 0;
146
Stephen Warren0e3db1732012-03-02 13:05:46 -0700147 if (status) {
Stephen Warrend4705312012-05-01 11:14:15 -0600148 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700149 module_put(pctldev->owner);
150 }
151
Linus Walleij2744e8a2011-05-02 20:50:54 +0200152out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700153 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700154 if (gpio_range) {
155 desc->gpio_owner = NULL;
156 } else {
157 desc->mux_usecount--;
158 if (!desc->mux_usecount)
159 desc->mux_owner = NULL;
160 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700161 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200162out:
163 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700164 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warrend4705312012-05-01 11:14:15 -0600165 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200166
167 return status;
168}
169
170/**
171 * pin_free() - release a single muxed in pin so something else can be muxed
172 * @pctldev: pin controller device handling this pin
173 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600174 * @gpio_range: the range matching the GPIO pin if this is a request for a
175 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100176 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700177 * This function returns a pointer to the previous owner. This is used
178 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100179 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200180 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600181static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
182 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183{
184 const struct pinmux_ops *ops = pctldev->desc->pmxops;
185 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700186 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187
188 desc = pin_desc_get(pctldev, pin);
189 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700190 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200191 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600192 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193 }
194
Stephen Warren652162d2012-03-05 17:22:15 -0700195 if (!gpio_range) {
Richard Genoud740924a2013-03-21 12:21:47 +0100196 /*
197 * A pin should not be freed more times than allocated.
198 */
199 if (WARN_ON(!desc->mux_usecount))
200 return NULL;
Stephen Warren652162d2012-03-05 17:22:15 -0700201 desc->mux_usecount--;
202 if (desc->mux_usecount)
203 return NULL;
204 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700205
Stephen Warren3712a3c2011-10-21 12:25:53 -0600206 /*
207 * If there is no kind of request function for the pin we just assume
208 * we got it by default and proceed.
209 */
210 if (gpio_range && ops->gpio_disable_free)
211 ops->gpio_disable_free(pctldev, gpio_range, pin);
212 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213 ops->free(pctldev, pin);
214
Stephen Warren652162d2012-03-05 17:22:15 -0700215 if (gpio_range) {
216 owner = desc->gpio_owner;
217 desc->gpio_owner = NULL;
218 } else {
219 owner = desc->mux_owner;
220 desc->mux_owner = NULL;
221 desc->mux_setting = NULL;
222 }
223
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600225
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700226 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200227}
228
229/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100230 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
231 * @pctldev: pin controller device affected
232 * @pin: the pin to mux in for GPIO
233 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200234 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100235int pinmux_request_gpio(struct pinctrl_dev *pctldev,
236 struct pinctrl_gpio_range *range,
237 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700239 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241
242 /* Conjure some name stating what chip and pin this is taken by */
Thomas Petazzoni23a895a2012-09-13 21:48:14 +0200243 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700244 if (!owner)
Masahiro Yamada1fb1f052016-05-25 20:14:13 +0900245 return -ENOMEM;
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600246
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700247 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600248 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700249 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600250
251 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200252}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200253
254/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100255 * pinmux_free_gpio() - release a pin from GPIO muxing
256 * @pctldev: the pin controller device for the pin
257 * @pin: the affected currently GPIO-muxed in pin
258 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100260void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
261 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700263 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200264
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700265 owner = pin_free(pctldev, pin, range);
266 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200268
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100269/**
270 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
271 * @pctldev: the pin controller handling this pin
272 * @range: applicable GPIO range
273 * @pin: the affected GPIO pin in this controller
274 * @input: true if we set the pin as input, false for output
275 */
276int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
277 struct pinctrl_gpio_range *range,
278 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100279{
Linus Walleij542e7042011-11-14 10:06:22 +0100280 const struct pinmux_ops *ops;
281 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100282
283 ops = pctldev->desc->pmxops;
284
Linus Walleij542e7042011-11-14 10:06:22 +0100285 if (ops->gpio_set_direction)
286 ret = ops->gpio_set_direction(pctldev, range, pin, input);
287 else
288 ret = 0;
289
290 return ret;
291}
292
Stephen Warren7ecdb162012-03-02 13:05:45 -0700293static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
294 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295{
296 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530297 unsigned nfuncs = ops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200298 unsigned selector = 0;
299
300 /* See if this pctldev has this function */
Viresh Kumard1e90e92012-03-30 11:25:40 +0530301 while (selector < nfuncs) {
Masahiro Yamada163dc9f2015-08-01 13:22:38 +0900302 const char *fname = ops->get_function_name(pctldev, selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200303
Stephen Warren7ecdb162012-03-02 13:05:45 -0700304 if (!strcmp(function, fname))
305 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 selector++;
308 }
309
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310 return -EINVAL;
311}
312
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900313int pinmux_map_to_setting(const struct pinctrl_map *map,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700314 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200315{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700316 struct pinctrl_dev *pctldev = setting->pctldev;
317 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700318 char const * const *groups;
319 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700321 const char *group;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322
Dong Aishengad8bb722012-04-10 12:41:34 +0800323 if (!pmxops) {
324 dev_err(pctldev->dev, "does not support mux function\n");
325 return -EINVAL;
326 }
327
John Crispin15f70e12012-04-23 19:01:58 +0200328 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
John Crispinad6e1102012-04-26 16:47:11 +0200329 if (ret < 0) {
330 dev_err(pctldev->dev, "invalid function %s in map table\n",
331 map->data.mux.function);
John Crispin15f70e12012-04-23 19:01:58 +0200332 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200333 }
John Crispin15f70e12012-04-23 19:01:58 +0200334 setting->data.mux.func = ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200335
Stephen Warren1e2082b2012-03-02 13:05:48 -0700336 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700337 &groups, &num_groups);
John Crispinad6e1102012-04-26 16:47:11 +0200338 if (ret < 0) {
339 dev_err(pctldev->dev, "can't query groups for function %s\n",
340 map->data.mux.function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200341 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200342 }
343 if (!num_groups) {
344 dev_err(pctldev->dev,
345 "function %s can't be selected on any group\n",
346 map->data.mux.function);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700347 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +0200348 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700349 if (map->data.mux.group) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700350 group = map->data.mux.group;
Andy Shevchenkodff43592016-03-17 14:22:20 -0700351 ret = match_string(groups, num_groups, group);
352 if (ret < 0) {
John Crispinad6e1102012-04-26 16:47:11 +0200353 dev_err(pctldev->dev,
354 "invalid group \"%s\" for function \"%s\"\n",
355 group, map->data.mux.function);
Andy Shevchenkodff43592016-03-17 14:22:20 -0700356 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200357 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700358 } else {
359 group = groups[0];
360 }
361
John Crispin15f70e12012-04-23 19:01:58 +0200362 ret = pinctrl_get_group_selector(pctldev, group);
John Crispinad6e1102012-04-26 16:47:11 +0200363 if (ret < 0) {
364 dev_err(pctldev->dev, "invalid group %s in map table\n",
365 map->data.mux.group);
John Crispin15f70e12012-04-23 19:01:58 +0200366 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200367 }
John Crispin15f70e12012-04-23 19:01:58 +0200368 setting->data.mux.group = ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700369
Linus Walleij2744e8a2011-05-02 20:50:54 +0200370 return 0;
371}
372
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900373void pinmux_free_setting(const struct pinctrl_setting *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100374{
Linus Walleij1a789582012-10-17 20:51:54 +0200375 /* This function is currently unused */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100376}
377
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900378int pinmux_enable_setting(const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200379{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700380 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700381 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100382 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200383 int ret = 0;
384 const unsigned *pins = NULL;
385 unsigned num_pins = 0;
Stephen Warrenba110d92012-03-02 13:05:49 -0700386 int i;
387 struct pin_desc *desc;
388
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200389 if (pctlops->get_group_pins)
390 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
391 &pins, &num_pins);
392
Stephen Warrenba110d92012-03-02 13:05:49 -0700393 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200394 const char *gname;
395
Stephen Warrenba110d92012-03-02 13:05:49 -0700396 /* errors only affect debug data, so just warn */
Linus Walleij1c8e7942013-08-14 18:23:33 +0200397 gname = pctlops->get_group_name(pctldev,
398 setting->data.mux.group);
Stephen Warrenba110d92012-03-02 13:05:49 -0700399 dev_warn(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200400 "could not get pins for group %s\n",
401 gname);
Stephen Warrenba110d92012-03-02 13:05:49 -0700402 num_pins = 0;
403 }
404
Linus Walleij1a789582012-10-17 20:51:54 +0200405 /* Try to allocate all pins in this group, one by one */
406 for (i = 0; i < num_pins; i++) {
407 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
408 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200409 const char *gname;
410 const char *pname;
411
412 desc = pin_desc_get(pctldev, pins[i]);
413 pname = desc ? desc->name : "non-existing";
414 gname = pctlops->get_group_name(pctldev,
415 setting->data.mux.group);
Linus Walleij1a789582012-10-17 20:51:54 +0200416 dev_err(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200417 "could not request pin %d (%s) from group %s "
418 " on device %s\n",
419 pins[i], pname, gname,
420 pinctrl_dev_get_name(pctldev));
Axel Line38d4572012-11-10 21:53:20 +0800421 goto err_pin_request;
Linus Walleij1a789582012-10-17 20:51:54 +0200422 }
423 }
424
425 /* Now that we have acquired the pins, encode the mux setting */
Stephen Warrenba110d92012-03-02 13:05:49 -0700426 for (i = 0; i < num_pins; i++) {
427 desc = pin_desc_get(pctldev, pins[i]);
428 if (desc == NULL) {
429 dev_warn(pctldev->dev,
430 "could not get pin desc for pin %d\n",
431 pins[i]);
432 continue;
433 }
434 desc->mux_setting = &(setting->data.mux);
435 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200436
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200437 ret = ops->set_mux(pctldev, setting->data.mux.func,
438 setting->data.mux.group);
Axel Line38d4572012-11-10 21:53:20 +0800439
440 if (ret)
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200441 goto err_set_mux;
Axel Line38d4572012-11-10 21:53:20 +0800442
443 return 0;
444
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200445err_set_mux:
Axel Line38d4572012-11-10 21:53:20 +0800446 for (i = 0; i < num_pins; i++) {
447 desc = pin_desc_get(pctldev, pins[i]);
448 if (desc)
449 desc->mux_setting = NULL;
450 }
451err_pin_request:
452 /* On error release all taken pins */
453 while (--i >= 0)
454 pin_free(pctldev, pins[i], NULL);
455
456 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200457}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200458
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900459void pinmux_disable_setting(const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200460{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700461 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700462 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200463 int ret = 0;
464 const unsigned *pins = NULL;
465 unsigned num_pins = 0;
Stephen Warrenba110d92012-03-02 13:05:49 -0700466 int i;
467 struct pin_desc *desc;
468
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200469 if (pctlops->get_group_pins)
470 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
471 &pins, &num_pins);
Stephen Warrenba110d92012-03-02 13:05:49 -0700472 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200473 const char *gname;
474
Stephen Warrenba110d92012-03-02 13:05:49 -0700475 /* errors only affect debug data, so just warn */
Linus Walleij1c8e7942013-08-14 18:23:33 +0200476 gname = pctlops->get_group_name(pctldev,
477 setting->data.mux.group);
Stephen Warrenba110d92012-03-02 13:05:49 -0700478 dev_warn(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200479 "could not get pins for group %s\n",
480 gname);
Stephen Warrenba110d92012-03-02 13:05:49 -0700481 num_pins = 0;
482 }
483
Linus Walleij1a789582012-10-17 20:51:54 +0200484 /* Flag the descs that no setting is active */
Stephen Warrenba110d92012-03-02 13:05:49 -0700485 for (i = 0; i < num_pins; i++) {
486 desc = pin_desc_get(pctldev, pins[i]);
487 if (desc == NULL) {
488 dev_warn(pctldev->dev,
489 "could not get pin desc for pin %d\n",
490 pins[i]);
491 continue;
492 }
Sonic Zhang744f0a92013-08-14 13:26:43 +0800493 if (desc->mux_setting == &(setting->data.mux)) {
Sonic Zhang744f0a92013-08-14 13:26:43 +0800494 pin_free(pctldev, pins[i], NULL);
Linus Walleij1c8e7942013-08-14 18:23:33 +0200495 } else {
496 const char *gname;
Linus Walleij1c8e7942013-08-14 18:23:33 +0200497
Linus Walleij1c8e7942013-08-14 18:23:33 +0200498 gname = pctlops->get_group_name(pctldev,
499 setting->data.mux.group);
500 dev_warn(pctldev->dev,
501 "not freeing pin %d (%s) as part of "
502 "deactivating group %s - it is already "
503 "used for some other setting",
Michael Opdenacker808e6572013-10-29 04:50:45 +0100504 pins[i], desc->name, gname);
Sonic Zhang744f0a92013-08-14 13:26:43 +0800505 }
Stephen Warrenba110d92012-03-02 13:05:49 -0700506 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200507}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508
Linus Walleij2744e8a2011-05-02 20:50:54 +0200509#ifdef CONFIG_DEBUG_FS
510
511/* Called from pincontrol core */
512static int pinmux_functions_show(struct seq_file *s, void *what)
513{
514 struct pinctrl_dev *pctldev = s->private;
515 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Dong Aishengad8bb722012-04-10 12:41:34 +0800516 unsigned nfuncs;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200517 unsigned func_selector = 0;
518
Dong Aishengad8bb722012-04-10 12:41:34 +0800519 if (!pmxops)
520 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700521
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200522 mutex_lock(&pctldev->mutex);
Dong Aishengad8bb722012-04-10 12:41:34 +0800523 nfuncs = pmxops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +0530524 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200525 const char *func = pmxops->get_function_name(pctldev,
526 func_selector);
527 const char * const *groups;
528 unsigned num_groups;
529 int ret;
530 int i;
531
532 ret = pmxops->get_function_groups(pctldev, func_selector,
533 &groups, &num_groups);
Ludovic Desroches9d7ebbb2015-06-08 17:16:37 +0200534 if (ret) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200535 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
536 func);
Ludovic Desroches9d7ebbb2015-06-08 17:16:37 +0200537 func_selector++;
538 continue;
539 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200540
541 seq_printf(s, "function: %s, groups = [ ", func);
542 for (i = 0; i < num_groups; i++)
543 seq_printf(s, "%s ", groups[i]);
544 seq_puts(s, "]\n");
545
546 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200547 }
548
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200549 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700550
Linus Walleij2744e8a2011-05-02 20:50:54 +0200551 return 0;
552}
553
554static int pinmux_pins_show(struct seq_file *s, void *what)
555{
556 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700557 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
558 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900559 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200560
Dong Aishengad8bb722012-04-10 12:41:34 +0800561 if (!pmxops)
562 return 0;
563
Linus Walleij2744e8a2011-05-02 20:50:54 +0200564 seq_puts(s, "Pinmux settings per pin\n");
Linus Walleij81508852015-05-28 10:11:19 +0200565 if (pmxops->strict)
566 seq_puts(s,
567 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
568 else
569 seq_puts(s,
570 "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200571
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200572 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700573
Chanho Park706e8522012-01-03 16:47:50 +0900574 /* The pin number can be retrived from the pin controller descriptor */
575 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200576 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100577 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200578
Chanho Park706e8522012-01-03 16:47:50 +0900579 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200580 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900581 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200582 if (desc == NULL)
583 continue;
584
Stephen Warren652162d2012-03-05 17:22:15 -0700585 if (desc->mux_owner &&
586 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100587 is_hog = true;
588
Linus Walleij81508852015-05-28 10:11:19 +0200589 if (pmxops->strict) {
590 if (desc->mux_owner)
591 seq_printf(s, "pin %d (%s): device %s%s",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900592 pin, desc->name, desc->mux_owner,
Linus Walleij81508852015-05-28 10:11:19 +0200593 is_hog ? " (HOG)" : "");
594 else if (desc->gpio_owner)
595 seq_printf(s, "pin %d (%s): GPIO %s",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900596 pin, desc->name, desc->gpio_owner);
Linus Walleij81508852015-05-28 10:11:19 +0200597 else
598 seq_printf(s, "pin %d (%s): UNCLAIMED",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900599 pin, desc->name);
Linus Walleij81508852015-05-28 10:11:19 +0200600 } else {
601 /* For non-strict controllers */
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900602 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
Linus Walleij81508852015-05-28 10:11:19 +0200603 desc->mux_owner ? desc->mux_owner
604 : "(MUX UNCLAIMED)",
605 desc->gpio_owner ? desc->gpio_owner
606 : "(GPIO UNCLAIMED)",
607 is_hog ? " (HOG)" : "");
608 }
Stephen Warrenba110d92012-03-02 13:05:49 -0700609
Linus Walleij81508852015-05-28 10:11:19 +0200610 /* If mux: print function+group claiming the pin */
Stephen Warrenba110d92012-03-02 13:05:49 -0700611 if (desc->mux_setting)
612 seq_printf(s, " function %s group %s\n",
613 pmxops->get_function_name(pctldev,
614 desc->mux_setting->func),
615 pctlops->get_group_name(pctldev,
616 desc->mux_setting->group));
617 else
Markus Elfringffd10c22018-01-13 11:33:47 +0100618 seq_putc(s, '\n');
Linus Walleij2744e8a2011-05-02 20:50:54 +0200619 }
620
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200621 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700622
Linus Walleij2744e8a2011-05-02 20:50:54 +0200623 return 0;
624}
625
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900626void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700627{
628 seq_printf(s, "group %s\nfunction %s\n",
629 map->data.mux.group ? map->data.mux.group : "(default)",
630 map->data.mux.function);
631}
632
633void pinmux_show_setting(struct seq_file *s,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900634 const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200635{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700636 struct pinctrl_dev *pctldev = setting->pctldev;
637 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
638 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200639
Stephen Warren1e2082b2012-03-02 13:05:48 -0700640 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
641 pctlops->get_group_name(pctldev, setting->data.mux.group),
642 setting->data.mux.group,
643 pmxops->get_function_name(pctldev, setting->data.mux.func),
644 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200645}
646
Yangtao Li0819dc72018-11-30 11:36:17 -0500647DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
648DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200649
Linus Walleij2744e8a2011-05-02 20:50:54 +0200650void pinmux_init_device_debugfs(struct dentry *devroot,
651 struct pinctrl_dev *pctldev)
652{
653 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500654 devroot, pctldev, &pinmux_functions_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200655 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500656 devroot, pctldev, &pinmux_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200657}
658
659#endif /* CONFIG_DEBUG_FS */
Tony Lindgrena76edc82016-12-27 09:20:01 -0800660
661#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
662
663/**
664 * pinmux_generic_get_function_count() - returns number of functions
665 * @pctldev: pin controller device
666 */
667int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
668{
669 return pctldev->num_functions;
670}
671EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
672
673/**
674 * pinmux_generic_get_function_name() - returns the function name
675 * @pctldev: pin controller device
676 * @selector: function number
677 */
678const char *
679pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
680 unsigned int selector)
681{
682 struct function_desc *function;
683
684 function = radix_tree_lookup(&pctldev->pin_function_tree,
685 selector);
686 if (!function)
687 return NULL;
688
689 return function->name;
690}
691EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
692
693/**
694 * pinmux_generic_get_function_groups() - gets the function groups
695 * @pctldev: pin controller device
696 * @selector: function number
697 * @groups: array of pin groups
698 * @num_groups: number of pin groups
699 */
700int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
701 unsigned int selector,
702 const char * const **groups,
703 unsigned * const num_groups)
704{
705 struct function_desc *function;
706
707 function = radix_tree_lookup(&pctldev->pin_function_tree,
708 selector);
709 if (!function) {
710 dev_err(pctldev->dev, "%s could not find function%i\n",
711 __func__, selector);
712 return -EINVAL;
713 }
714 *groups = function->group_names;
715 *num_groups = function->num_group_names;
716
717 return 0;
718}
719EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
720
721/**
722 * pinmux_generic_get_function() - returns a function based on the number
723 * @pctldev: pin controller device
724 * @group_selector: function number
725 */
726struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
727 unsigned int selector)
728{
729 struct function_desc *function;
730
731 function = radix_tree_lookup(&pctldev->pin_function_tree,
732 selector);
733 if (!function)
734 return NULL;
735
736 return function;
737}
738EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
739
740/**
Geert Uytterhoeven6bffa7e2017-04-03 10:32:42 +0200741 * pinmux_generic_add_function() - adds a function group
Tony Lindgrena76edc82016-12-27 09:20:01 -0800742 * @pctldev: pin controller device
743 * @name: name of the function
744 * @groups: array of pin groups
745 * @num_groups: number of pin groups
746 * @data: pin controller driver specific data
747 */
748int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
749 const char *name,
750 const char **groups,
751 const unsigned int num_groups,
752 void *data)
753{
754 struct function_desc *function;
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700755 int selector;
756
757 if (!name)
758 return -EINVAL;
759
760 selector = pinmux_func_name_to_selector(pctldev, name);
761 if (selector >= 0)
762 return selector;
763
764 selector = pctldev->num_functions;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800765
766 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
767 if (!function)
768 return -ENOMEM;
769
770 function->name = name;
771 function->group_names = groups;
772 function->num_group_names = num_groups;
773 function->data = data;
774
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700775 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
Tony Lindgrena76edc82016-12-27 09:20:01 -0800776
777 pctldev->num_functions++;
778
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700779 return selector;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800780}
781EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
782
783/**
784 * pinmux_generic_remove_function() - removes a numbered function
785 * @pctldev: pin controller device
786 * @selector: function number
787 *
788 * Note that the caller must take care of locking.
789 */
790int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
791 unsigned int selector)
792{
793 struct function_desc *function;
794
795 function = radix_tree_lookup(&pctldev->pin_function_tree,
796 selector);
797 if (!function)
798 return -ENOENT;
799
800 radix_tree_delete(&pctldev->pin_function_tree, selector);
801 devm_kfree(pctldev->dev, function);
802
803 pctldev->num_functions--;
804
805 return 0;
806}
807EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
808
809/**
810 * pinmux_generic_free_functions() - removes all functions
811 * @pctldev: pin controller device
812 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700813 * Note that the caller must take care of locking. The pinctrl
814 * functions are allocated with devm_kzalloc() so no need to free
815 * them here.
Tony Lindgrena76edc82016-12-27 09:20:01 -0800816 */
817void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
818{
819 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900820 void __rcu **slot;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800821
822 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700823 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
Tony Lindgrena76edc82016-12-27 09:20:01 -0800824
825 pctldev->num_functions = 0;
826}
827
828#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */