blob: 6cdbd9ccf2f048bf80744061660e9df62f67d0c1 [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij2744e8a2011-05-02 20:50:54 +02002/*
3 * Core driver for the pin muxing portions of the pin control subsystem
4 *
Linus Walleije93bcee2012-02-09 07:23:28 +01005 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02006 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
Stephen Warren7ecdb162012-03-02 13:05:45 -070011 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 */
13#define pr_fmt(fmt) "pinmux core: " fmt
14
Drew Fustini6199f6be2021-03-01 21:30:57 -080015#include <linux/ctype.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020016#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/**
Stefan Wahren472a61e2019-08-14 14:00:35 +030075 * pinmux_can_be_used_for_gpio() - check if a specific pin
76 * is either muxed to a different function or used as gpio.
77 *
Lee Jonesd3403512020-07-13 15:49:17 +010078 * @pctldev: the associated pin controller device
Stefan Wahren472a61e2019-08-14 14:00:35 +030079 * @pin: the pin number in the global pin space
80 *
81 * Controllers not defined as strict will always return true,
82 * menaning that the gpio can be used.
83 */
84bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
85{
86 struct pin_desc *desc = pin_desc_get(pctldev, pin);
87 const struct pinmux_ops *ops = pctldev->desc->pmxops;
88
89 /* Can't inspect pin, assume it can be used */
Alexandre Torgue6ba2fd32019-12-04 15:41:06 +010090 if (!desc || !ops)
Stefan Wahren472a61e2019-08-14 14:00:35 +030091 return true;
92
93 if (ops->strict && desc->mux_usecount)
94 return false;
95
96 return !(ops->strict && !!desc->gpio_owner);
97}
98
99/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100 * pin_request() - request a single pin to be muxed in, typically for GPIO
Lee Jonesd3403512020-07-13 15:49:17 +0100101 * @pctldev: the associated pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102 * @pin: the pin number in the global pin space
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700103 * @owner: a representation of the owner of this pin; typically the device
104 * name that controls its mux function, or the requested GPIO name
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105 * @gpio_range: the range matching the GPIO pin if this is a request for a
106 * single GPIO pin
107 */
108static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700109 int pin, const char *owner,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 struct pinctrl_gpio_range *gpio_range)
111{
112 struct pin_desc *desc;
113 const struct pinmux_ops *ops = pctldev->desc->pmxops;
114 int status = -EINVAL;
115
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116 desc = pin_desc_get(pctldev, pin);
117 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700118 dev_err(pctldev->dev,
Stephen Warrend4705312012-05-01 11:14:15 -0600119 "pin %d is not registered so it cannot be requested\n",
120 pin);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121 goto out;
122 }
123
Dong Aishengd0bd8df2012-04-17 15:00:45 +0800124 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
125 pin, desc->name, owner);
126
Vladimir Zapolskiyb1eb8fa2016-12-25 02:59:28 +0200127 if ((!gpio_range || ops->strict) &&
128 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
129 dev_err(pctldev->dev,
130 "pin %s already requested by %s; cannot claim for %s\n",
131 desc->name, desc->mux_owner, owner);
132 goto out;
133 }
Stephen Warren652162d2012-03-05 17:22:15 -0700134
Vladimir Zapolskiyb1eb8fa2016-12-25 02:59:28 +0200135 if ((gpio_range || ops->strict) && desc->gpio_owner) {
136 dev_err(pctldev->dev,
137 "pin %s already requested by %s; cannot claim for %s\n",
138 desc->name, desc->gpio_owner, owner);
139 goto out;
140 }
141
142 if (gpio_range) {
Stephen Warren652162d2012-03-05 17:22:15 -0700143 desc->gpio_owner = owner;
144 } else {
Stephen Warren652162d2012-03-05 17:22:15 -0700145 desc->mux_usecount++;
146 if (desc->mux_usecount > 1)
147 return 0;
148
149 desc->mux_owner = owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200150 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700151
Linus Walleij2744e8a2011-05-02 20:50:54 +0200152 /* Let each pin increase references to this module */
153 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700154 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200155 "could not increase module refcount for pin %d\n",
156 pin);
157 status = -EINVAL;
158 goto out_free_pin;
159 }
160
161 /*
162 * If there is no kind of request function for the pin we just assume
163 * we got it by default and proceed.
164 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600165 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200166 /* This requests and enables a single GPIO pin */
167 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
168 else if (ops->request)
169 status = ops->request(pctldev, pin);
170 else
171 status = 0;
172
Stephen Warren0e3db1732012-03-02 13:05:46 -0700173 if (status) {
Stephen Warrend4705312012-05-01 11:14:15 -0600174 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
Stephen Warren0e3db1732012-03-02 13:05:46 -0700175 module_put(pctldev->owner);
176 }
177
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178out_free_pin:
Stephen Warren0e3db1732012-03-02 13:05:46 -0700179 if (status) {
Stephen Warren652162d2012-03-05 17:22:15 -0700180 if (gpio_range) {
181 desc->gpio_owner = NULL;
182 } else {
183 desc->mux_usecount--;
184 if (!desc->mux_usecount)
185 desc->mux_owner = NULL;
186 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700187 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188out:
189 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700190 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Stephen Warrend4705312012-05-01 11:14:15 -0600191 pin, owner, status);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192
193 return status;
194}
195
196/**
197 * pin_free() - release a single muxed in pin so something else can be muxed
198 * @pctldev: pin controller device handling this pin
199 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600200 * @gpio_range: the range matching the GPIO pin if this is a request for a
201 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100202 *
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700203 * This function returns a pointer to the previous owner. This is used
204 * for callers that dynamically allocate an owner name so it can be freed
Linus Walleij336cdba02011-11-10 09:27:41 +0100205 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600207static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
208 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200209{
210 const struct pinmux_ops *ops = pctldev->desc->pmxops;
211 struct pin_desc *desc;
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700212 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213
214 desc = pin_desc_get(pctldev, pin);
215 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700216 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600218 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200219 }
220
Stephen Warren652162d2012-03-05 17:22:15 -0700221 if (!gpio_range) {
Richard Genoud740924a2013-03-21 12:21:47 +0100222 /*
223 * A pin should not be freed more times than allocated.
224 */
225 if (WARN_ON(!desc->mux_usecount))
226 return NULL;
Stephen Warren652162d2012-03-05 17:22:15 -0700227 desc->mux_usecount--;
228 if (desc->mux_usecount)
229 return NULL;
230 }
Stephen Warren0e3db1732012-03-02 13:05:46 -0700231
Stephen Warren3712a3c2011-10-21 12:25:53 -0600232 /*
233 * If there is no kind of request function for the pin we just assume
234 * we got it by default and proceed.
235 */
236 if (gpio_range && ops->gpio_disable_free)
237 ops->gpio_disable_free(pctldev, gpio_range, pin);
238 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 ops->free(pctldev, pin);
240
Stephen Warren652162d2012-03-05 17:22:15 -0700241 if (gpio_range) {
242 owner = desc->gpio_owner;
243 desc->gpio_owner = NULL;
244 } else {
245 owner = desc->mux_owner;
246 desc->mux_owner = NULL;
247 desc->mux_setting = NULL;
248 }
249
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600251
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700252 return owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200253}
254
255/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100256 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
257 * @pctldev: pin controller device affected
258 * @pin: the pin to mux in for GPIO
259 * @range: the applicable GPIO range
Lee Jonesd3403512020-07-13 15:49:17 +0100260 * @gpio: number of requested GPIO
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100262int pinmux_request_gpio(struct pinctrl_dev *pctldev,
263 struct pinctrl_gpio_range *range,
264 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700266 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200268
269 /* Conjure some name stating what chip and pin this is taken by */
Thomas Petazzoni23a895a2012-09-13 21:48:14 +0200270 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700271 if (!owner)
Masahiro Yamada1fb1f052016-05-25 20:14:13 +0900272 return -ENOMEM;
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600273
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700274 ret = pin_request(pctldev, pin, owner, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600275 if (ret < 0)
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700276 kfree(owner);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600277
278 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200279}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200280
281/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100282 * pinmux_free_gpio() - release a pin from GPIO muxing
283 * @pctldev: the pin controller device for the pin
284 * @pin: the affected currently GPIO-muxed in pin
285 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200286 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100287void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
288 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200289{
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700290 const char *owner;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200291
Stephen Warren3cc70ed2012-02-19 23:45:44 -0700292 owner = pin_free(pctldev, pin, range);
293 kfree(owner);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200294}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100296/**
297 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
298 * @pctldev: the pin controller handling this pin
299 * @range: applicable GPIO range
300 * @pin: the affected GPIO pin in this controller
301 * @input: true if we set the pin as input, false for output
302 */
303int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range,
305 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100306{
Linus Walleij542e7042011-11-14 10:06:22 +0100307 const struct pinmux_ops *ops;
308 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100309
310 ops = pctldev->desc->pmxops;
311
Linus Walleij542e7042011-11-14 10:06:22 +0100312 if (ops->gpio_set_direction)
313 ret = ops->gpio_set_direction(pctldev, range, pin, input);
314 else
315 ret = 0;
316
317 return ret;
318}
319
Stephen Warren7ecdb162012-03-02 13:05:45 -0700320static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
321 const char *function)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322{
323 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530324 unsigned nfuncs = ops->get_functions_count(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325 unsigned selector = 0;
326
327 /* See if this pctldev has this function */
Viresh Kumard1e90e92012-03-30 11:25:40 +0530328 while (selector < nfuncs) {
Masahiro Yamada163dc9f2015-08-01 13:22:38 +0900329 const char *fname = ops->get_function_name(pctldev, selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200330
Stephen Warren7ecdb162012-03-02 13:05:45 -0700331 if (!strcmp(function, fname))
332 return selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334 selector++;
335 }
336
Linus Walleij2744e8a2011-05-02 20:50:54 +0200337 return -EINVAL;
338}
339
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900340int pinmux_map_to_setting(const struct pinctrl_map *map,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700341 struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200342{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700343 struct pinctrl_dev *pctldev = setting->pctldev;
344 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700345 char const * const *groups;
346 unsigned num_groups;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347 int ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700348 const char *group;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200349
Dong Aishengad8bb722012-04-10 12:41:34 +0800350 if (!pmxops) {
351 dev_err(pctldev->dev, "does not support mux function\n");
352 return -EINVAL;
353 }
354
John Crispin15f70e12012-04-23 19:01:58 +0200355 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
John Crispinad6e1102012-04-26 16:47:11 +0200356 if (ret < 0) {
357 dev_err(pctldev->dev, "invalid function %s in map table\n",
358 map->data.mux.function);
John Crispin15f70e12012-04-23 19:01:58 +0200359 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200360 }
John Crispin15f70e12012-04-23 19:01:58 +0200361 setting->data.mux.func = ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200362
Stephen Warren1e2082b2012-03-02 13:05:48 -0700363 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
Stephen Warren7ecdb162012-03-02 13:05:45 -0700364 &groups, &num_groups);
John Crispinad6e1102012-04-26 16:47:11 +0200365 if (ret < 0) {
366 dev_err(pctldev->dev, "can't query groups for function %s\n",
367 map->data.mux.function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200368 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200369 }
370 if (!num_groups) {
371 dev_err(pctldev->dev,
372 "function %s can't be selected on any group\n",
373 map->data.mux.function);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700374 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +0200375 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700376 if (map->data.mux.group) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700377 group = map->data.mux.group;
Andy Shevchenkodff43592016-03-17 14:22:20 -0700378 ret = match_string(groups, num_groups, group);
379 if (ret < 0) {
John Crispinad6e1102012-04-26 16:47:11 +0200380 dev_err(pctldev->dev,
381 "invalid group \"%s\" for function \"%s\"\n",
382 group, map->data.mux.function);
Andy Shevchenkodff43592016-03-17 14:22:20 -0700383 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200384 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700385 } else {
386 group = groups[0];
387 }
388
John Crispin15f70e12012-04-23 19:01:58 +0200389 ret = pinctrl_get_group_selector(pctldev, group);
John Crispinad6e1102012-04-26 16:47:11 +0200390 if (ret < 0) {
391 dev_err(pctldev->dev, "invalid group %s in map table\n",
392 map->data.mux.group);
John Crispin15f70e12012-04-23 19:01:58 +0200393 return ret;
John Crispinad6e1102012-04-26 16:47:11 +0200394 }
John Crispin15f70e12012-04-23 19:01:58 +0200395 setting->data.mux.group = ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700396
Linus Walleij2744e8a2011-05-02 20:50:54 +0200397 return 0;
398}
399
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900400void pinmux_free_setting(const struct pinctrl_setting *setting)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100401{
Linus Walleij1a789582012-10-17 20:51:54 +0200402 /* This function is currently unused */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100403}
404
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900405int pinmux_enable_setting(const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200406{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700407 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700408 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100409 const struct pinmux_ops *ops = pctldev->desc->pmxops;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200410 int ret = 0;
411 const unsigned *pins = NULL;
412 unsigned num_pins = 0;
Stephen Warrenba110d92012-03-02 13:05:49 -0700413 int i;
414 struct pin_desc *desc;
415
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200416 if (pctlops->get_group_pins)
417 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
418 &pins, &num_pins);
419
Stephen Warrenba110d92012-03-02 13:05:49 -0700420 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200421 const char *gname;
422
Stephen Warrenba110d92012-03-02 13:05:49 -0700423 /* errors only affect debug data, so just warn */
Linus Walleij1c8e7942013-08-14 18:23:33 +0200424 gname = pctlops->get_group_name(pctldev,
425 setting->data.mux.group);
Stephen Warrenba110d92012-03-02 13:05:49 -0700426 dev_warn(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200427 "could not get pins for group %s\n",
428 gname);
Stephen Warrenba110d92012-03-02 13:05:49 -0700429 num_pins = 0;
430 }
431
Linus Walleij1a789582012-10-17 20:51:54 +0200432 /* Try to allocate all pins in this group, one by one */
433 for (i = 0; i < num_pins; i++) {
434 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
435 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200436 const char *gname;
437 const char *pname;
438
439 desc = pin_desc_get(pctldev, pins[i]);
440 pname = desc ? desc->name : "non-existing";
441 gname = pctlops->get_group_name(pctldev,
442 setting->data.mux.group);
Linus Walleij1a789582012-10-17 20:51:54 +0200443 dev_err(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200444 "could not request pin %d (%s) from group %s "
445 " on device %s\n",
446 pins[i], pname, gname,
447 pinctrl_dev_get_name(pctldev));
Axel Line38d4572012-11-10 21:53:20 +0800448 goto err_pin_request;
Linus Walleij1a789582012-10-17 20:51:54 +0200449 }
450 }
451
452 /* Now that we have acquired the pins, encode the mux setting */
Stephen Warrenba110d92012-03-02 13:05:49 -0700453 for (i = 0; i < num_pins; i++) {
454 desc = pin_desc_get(pctldev, pins[i]);
455 if (desc == NULL) {
456 dev_warn(pctldev->dev,
457 "could not get pin desc for pin %d\n",
458 pins[i]);
459 continue;
460 }
461 desc->mux_setting = &(setting->data.mux);
462 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200463
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200464 ret = ops->set_mux(pctldev, setting->data.mux.func,
465 setting->data.mux.group);
Axel Line38d4572012-11-10 21:53:20 +0800466
467 if (ret)
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200468 goto err_set_mux;
Axel Line38d4572012-11-10 21:53:20 +0800469
470 return 0;
471
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200472err_set_mux:
Axel Line38d4572012-11-10 21:53:20 +0800473 for (i = 0; i < num_pins; i++) {
474 desc = pin_desc_get(pctldev, pins[i]);
475 if (desc)
476 desc->mux_setting = NULL;
477 }
478err_pin_request:
479 /* On error release all taken pins */
480 while (--i >= 0)
481 pin_free(pctldev, pins[i], NULL);
482
483 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200485
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900486void pinmux_disable_setting(const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200487{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700488 struct pinctrl_dev *pctldev = setting->pctldev;
Stephen Warrenba110d92012-03-02 13:05:49 -0700489 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200490 int ret = 0;
491 const unsigned *pins = NULL;
492 unsigned num_pins = 0;
Stephen Warrenba110d92012-03-02 13:05:49 -0700493 int i;
494 struct pin_desc *desc;
495
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200496 if (pctlops->get_group_pins)
497 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
498 &pins, &num_pins);
Stephen Warrenba110d92012-03-02 13:05:49 -0700499 if (ret) {
Linus Walleij1c8e7942013-08-14 18:23:33 +0200500 const char *gname;
501
Stephen Warrenba110d92012-03-02 13:05:49 -0700502 /* errors only affect debug data, so just warn */
Linus Walleij1c8e7942013-08-14 18:23:33 +0200503 gname = pctlops->get_group_name(pctldev,
504 setting->data.mux.group);
Stephen Warrenba110d92012-03-02 13:05:49 -0700505 dev_warn(pctldev->dev,
Linus Walleij1c8e7942013-08-14 18:23:33 +0200506 "could not get pins for group %s\n",
507 gname);
Stephen Warrenba110d92012-03-02 13:05:49 -0700508 num_pins = 0;
509 }
510
Linus Walleij1a789582012-10-17 20:51:54 +0200511 /* Flag the descs that no setting is active */
Stephen Warrenba110d92012-03-02 13:05:49 -0700512 for (i = 0; i < num_pins; i++) {
513 desc = pin_desc_get(pctldev, pins[i]);
514 if (desc == NULL) {
515 dev_warn(pctldev->dev,
516 "could not get pin desc for pin %d\n",
517 pins[i]);
518 continue;
519 }
Sonic Zhang744f0a92013-08-14 13:26:43 +0800520 if (desc->mux_setting == &(setting->data.mux)) {
Sonic Zhang744f0a92013-08-14 13:26:43 +0800521 pin_free(pctldev, pins[i], NULL);
Linus Walleij1c8e7942013-08-14 18:23:33 +0200522 } else {
523 const char *gname;
Linus Walleij1c8e7942013-08-14 18:23:33 +0200524
Linus Walleij1c8e7942013-08-14 18:23:33 +0200525 gname = pctlops->get_group_name(pctldev,
526 setting->data.mux.group);
527 dev_warn(pctldev->dev,
528 "not freeing pin %d (%s) as part of "
529 "deactivating group %s - it is already "
530 "used for some other setting",
Michael Opdenacker808e6572013-10-29 04:50:45 +0100531 pins[i], desc->name, gname);
Sonic Zhang744f0a92013-08-14 13:26:43 +0800532 }
Stephen Warrenba110d92012-03-02 13:05:49 -0700533 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200534}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200535
Linus Walleij2744e8a2011-05-02 20:50:54 +0200536#ifdef CONFIG_DEBUG_FS
537
538/* Called from pincontrol core */
539static int pinmux_functions_show(struct seq_file *s, void *what)
540{
541 struct pinctrl_dev *pctldev = s->private;
542 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Dong Aishengad8bb722012-04-10 12:41:34 +0800543 unsigned nfuncs;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200544 unsigned func_selector = 0;
545
Dong Aishengad8bb722012-04-10 12:41:34 +0800546 if (!pmxops)
547 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700548
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200549 mutex_lock(&pctldev->mutex);
Dong Aishengad8bb722012-04-10 12:41:34 +0800550 nfuncs = pmxops->get_functions_count(pctldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +0530551 while (func_selector < nfuncs) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200552 const char *func = pmxops->get_function_name(pctldev,
553 func_selector);
554 const char * const *groups;
555 unsigned num_groups;
556 int ret;
557 int i;
558
559 ret = pmxops->get_function_groups(pctldev, func_selector,
560 &groups, &num_groups);
Ludovic Desroches9d7ebbb2015-06-08 17:16:37 +0200561 if (ret) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200562 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
563 func);
Ludovic Desroches9d7ebbb2015-06-08 17:16:37 +0200564 func_selector++;
565 continue;
566 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200567
Drew Fustini3bbf9b82021-01-23 12:22:14 -0800568 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200569 for (i = 0; i < num_groups; i++)
570 seq_printf(s, "%s ", groups[i]);
571 seq_puts(s, "]\n");
572
573 func_selector++;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200574 }
575
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200576 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700577
Linus Walleij2744e8a2011-05-02 20:50:54 +0200578 return 0;
579}
580
581static int pinmux_pins_show(struct seq_file *s, void *what)
582{
583 struct pinctrl_dev *pctldev = s->private;
Stephen Warrenba110d92012-03-02 13:05:49 -0700584 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
585 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
Chanho Park706e8522012-01-03 16:47:50 +0900586 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200587
Dong Aishengad8bb722012-04-10 12:41:34 +0800588 if (!pmxops)
589 return 0;
590
Linus Walleij2744e8a2011-05-02 20:50:54 +0200591 seq_puts(s, "Pinmux settings per pin\n");
Linus Walleij81508852015-05-28 10:11:19 +0200592 if (pmxops->strict)
593 seq_puts(s,
594 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
595 else
596 seq_puts(s,
597 "Format: pin (name): mux_owner gpio_owner hog?\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200598
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200599 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700600
Chanho Park706e8522012-01-03 16:47:50 +0900601 /* The pin number can be retrived from the pin controller descriptor */
602 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200603 struct pin_desc *desc;
Linus Walleij1cf94c42012-02-24 06:53:04 +0100604 bool is_hog = false;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200605
Chanho Park706e8522012-01-03 16:47:50 +0900606 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200607 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900608 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200609 if (desc == NULL)
610 continue;
611
Stephen Warren652162d2012-03-05 17:22:15 -0700612 if (desc->mux_owner &&
613 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
Linus Walleij1cf94c42012-02-24 06:53:04 +0100614 is_hog = true;
615
Linus Walleij81508852015-05-28 10:11:19 +0200616 if (pmxops->strict) {
617 if (desc->mux_owner)
618 seq_printf(s, "pin %d (%s): device %s%s",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900619 pin, desc->name, desc->mux_owner,
Linus Walleij81508852015-05-28 10:11:19 +0200620 is_hog ? " (HOG)" : "");
621 else if (desc->gpio_owner)
622 seq_printf(s, "pin %d (%s): GPIO %s",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900623 pin, desc->name, desc->gpio_owner);
Linus Walleij81508852015-05-28 10:11:19 +0200624 else
625 seq_printf(s, "pin %d (%s): UNCLAIMED",
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900626 pin, desc->name);
Linus Walleij81508852015-05-28 10:11:19 +0200627 } else {
628 /* For non-strict controllers */
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900629 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
Linus Walleij81508852015-05-28 10:11:19 +0200630 desc->mux_owner ? desc->mux_owner
631 : "(MUX UNCLAIMED)",
632 desc->gpio_owner ? desc->gpio_owner
633 : "(GPIO UNCLAIMED)",
634 is_hog ? " (HOG)" : "");
635 }
Stephen Warrenba110d92012-03-02 13:05:49 -0700636
Linus Walleij81508852015-05-28 10:11:19 +0200637 /* If mux: print function+group claiming the pin */
Stephen Warrenba110d92012-03-02 13:05:49 -0700638 if (desc->mux_setting)
639 seq_printf(s, " function %s group %s\n",
640 pmxops->get_function_name(pctldev,
641 desc->mux_setting->func),
642 pctlops->get_group_name(pctldev,
643 desc->mux_setting->group));
644 else
Markus Elfringffd10c22018-01-13 11:33:47 +0100645 seq_putc(s, '\n');
Linus Walleij2744e8a2011-05-02 20:50:54 +0200646 }
647
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200648 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700649
Linus Walleij2744e8a2011-05-02 20:50:54 +0200650 return 0;
651}
652
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900653void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700654{
655 seq_printf(s, "group %s\nfunction %s\n",
656 map->data.mux.group ? map->data.mux.group : "(default)",
657 map->data.mux.function);
658}
659
660void pinmux_show_setting(struct seq_file *s,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900661 const struct pinctrl_setting *setting)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200662{
Stephen Warren7ecdb162012-03-02 13:05:45 -0700663 struct pinctrl_dev *pctldev = setting->pctldev;
664 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
665 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200666
Stephen Warren1e2082b2012-03-02 13:05:48 -0700667 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
668 pctlops->get_group_name(pctldev, setting->data.mux.group),
669 setting->data.mux.group,
670 pmxops->get_function_name(pctldev, setting->data.mux.func),
671 setting->data.mux.func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200672}
673
Yangtao Li0819dc72018-11-30 11:36:17 -0500674DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
675DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200676
Drew Fustini6199f6be2021-03-01 21:30:57 -0800677#define PINMUX_SELECT_MAX 128
678static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
679 size_t len, loff_t *ppos)
680{
681 struct seq_file *sfile = file->private_data;
682 struct pinctrl_dev *pctldev = sfile->private;
683 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
684 const char *const *groups;
685 char *buf, *gname, *fname;
686 unsigned int num_groups;
687 int fsel, gsel, ret;
688
689 if (len > PINMUX_SELECT_MAX)
690 return -ENOMEM;
691
692 buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
693 if (!buf)
694 return -ENOMEM;
695
696 ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
697 if (ret < 0)
698 goto exit_free_buf;
699 buf[len-1] = '\0';
700
701 /* remove leading and trailing spaces of input buffer */
702 gname = strstrip(buf);
703 if (*gname == '\0') {
704 ret = -EINVAL;
705 goto exit_free_buf;
706 }
707
708 /* find a separator which is a spacelike character */
709 for (fname = gname; !isspace(*fname); fname++) {
710 if (*fname == '\0') {
711 ret = -EINVAL;
712 goto exit_free_buf;
713 }
714 }
715 *fname = '\0';
716
717 /* drop extra spaces between function and group names */
718 fname = skip_spaces(fname + 1);
719 if (*fname == '\0') {
720 ret = -EINVAL;
721 goto exit_free_buf;
722 }
723
724 ret = pinmux_func_name_to_selector(pctldev, fname);
725 if (ret < 0) {
726 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
727 goto exit_free_buf;
728 }
729 fsel = ret;
730
731 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
732 if (ret) {
733 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
734 goto exit_free_buf;
735 }
736
737 ret = match_string(groups, num_groups, gname);
738 if (ret < 0) {
739 dev_err(pctldev->dev, "invalid group %s", gname);
740 goto exit_free_buf;
741 }
742
743 ret = pinctrl_get_group_selector(pctldev, gname);
744 if (ret < 0) {
745 dev_err(pctldev->dev, "failed to get group selector for %s", gname);
746 goto exit_free_buf;
747 }
748 gsel = ret;
749
750 ret = pmxops->set_mux(pctldev, fsel, gsel);
751 if (ret) {
752 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
753 goto exit_free_buf;
754 }
755 ret = len;
756
757exit_free_buf:
758 kfree(buf);
759
760 return ret;
761}
762
763static int pinmux_select_open(struct inode *inode, struct file *file)
764{
765 return single_open(file, NULL, inode->i_private);
766}
767
768static const struct file_operations pinmux_select_ops = {
769 .owner = THIS_MODULE,
770 .open = pinmux_select_open,
771 .write = pinmux_select,
772 .llseek = no_llseek,
773 .release = single_release,
774};
775
Linus Walleij2744e8a2011-05-02 20:50:54 +0200776void pinmux_init_device_debugfs(struct dentry *devroot,
777 struct pinctrl_dev *pctldev)
778{
Drew Fustini47473812021-03-01 21:30:56 -0800779 debugfs_create_file("pinmux-functions", 0444,
Yangtao Li0819dc72018-11-30 11:36:17 -0500780 devroot, pctldev, &pinmux_functions_fops);
Drew Fustini47473812021-03-01 21:30:56 -0800781 debugfs_create_file("pinmux-pins", 0444,
Yangtao Li0819dc72018-11-30 11:36:17 -0500782 devroot, pctldev, &pinmux_pins_fops);
Drew Fustini6199f6be2021-03-01 21:30:57 -0800783 debugfs_create_file("pinmux-select", 0200,
784 devroot, pctldev, &pinmux_select_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200785}
786
787#endif /* CONFIG_DEBUG_FS */
Tony Lindgrena76edc82016-12-27 09:20:01 -0800788
789#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
790
791/**
792 * pinmux_generic_get_function_count() - returns number of functions
793 * @pctldev: pin controller device
794 */
795int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
796{
797 return pctldev->num_functions;
798}
799EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
800
801/**
802 * pinmux_generic_get_function_name() - returns the function name
803 * @pctldev: pin controller device
804 * @selector: function number
805 */
806const char *
807pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
808 unsigned int selector)
809{
810 struct function_desc *function;
811
812 function = radix_tree_lookup(&pctldev->pin_function_tree,
813 selector);
814 if (!function)
815 return NULL;
816
817 return function->name;
818}
819EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
820
821/**
822 * pinmux_generic_get_function_groups() - gets the function groups
823 * @pctldev: pin controller device
824 * @selector: function number
825 * @groups: array of pin groups
826 * @num_groups: number of pin groups
827 */
828int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
829 unsigned int selector,
830 const char * const **groups,
831 unsigned * const num_groups)
832{
833 struct function_desc *function;
834
835 function = radix_tree_lookup(&pctldev->pin_function_tree,
836 selector);
837 if (!function) {
838 dev_err(pctldev->dev, "%s could not find function%i\n",
839 __func__, selector);
840 return -EINVAL;
841 }
842 *groups = function->group_names;
843 *num_groups = function->num_group_names;
844
845 return 0;
846}
847EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
848
849/**
850 * pinmux_generic_get_function() - returns a function based on the number
851 * @pctldev: pin controller device
Lee Jonesd3403512020-07-13 15:49:17 +0100852 * @selector: function number
Tony Lindgrena76edc82016-12-27 09:20:01 -0800853 */
854struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
855 unsigned int selector)
856{
857 struct function_desc *function;
858
859 function = radix_tree_lookup(&pctldev->pin_function_tree,
860 selector);
861 if (!function)
862 return NULL;
863
864 return function;
865}
866EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
867
868/**
Geert Uytterhoeven6bffa7e2017-04-03 10:32:42 +0200869 * pinmux_generic_add_function() - adds a function group
Tony Lindgrena76edc82016-12-27 09:20:01 -0800870 * @pctldev: pin controller device
871 * @name: name of the function
872 * @groups: array of pin groups
873 * @num_groups: number of pin groups
874 * @data: pin controller driver specific data
875 */
876int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
877 const char *name,
878 const char **groups,
879 const unsigned int num_groups,
880 void *data)
881{
882 struct function_desc *function;
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700883 int selector;
884
885 if (!name)
886 return -EINVAL;
887
888 selector = pinmux_func_name_to_selector(pctldev, name);
889 if (selector >= 0)
890 return selector;
891
892 selector = pctldev->num_functions;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800893
894 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
895 if (!function)
896 return -ENOMEM;
897
898 function->name = name;
899 function->group_names = groups;
900 function->num_group_names = num_groups;
901 function->data = data;
902
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700903 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
Tony Lindgrena76edc82016-12-27 09:20:01 -0800904
905 pctldev->num_functions++;
906
Tony Lindgrenf913cfc2018-07-05 02:10:15 -0700907 return selector;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800908}
909EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
910
911/**
912 * pinmux_generic_remove_function() - removes a numbered function
913 * @pctldev: pin controller device
914 * @selector: function number
915 *
916 * Note that the caller must take care of locking.
917 */
918int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
919 unsigned int selector)
920{
921 struct function_desc *function;
922
923 function = radix_tree_lookup(&pctldev->pin_function_tree,
924 selector);
925 if (!function)
926 return -ENOENT;
927
928 radix_tree_delete(&pctldev->pin_function_tree, selector);
929 devm_kfree(pctldev->dev, function);
930
931 pctldev->num_functions--;
932
933 return 0;
934}
935EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
936
937/**
938 * pinmux_generic_free_functions() - removes all functions
939 * @pctldev: pin controller device
940 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700941 * Note that the caller must take care of locking. The pinctrl
942 * functions are allocated with devm_kzalloc() so no need to free
943 * them here.
Tony Lindgrena76edc82016-12-27 09:20:01 -0800944 */
945void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
946{
947 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900948 void __rcu **slot;
Tony Lindgrena76edc82016-12-27 09:20:01 -0800949
950 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700951 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
Tony Lindgrena76edc82016-12-27 09:20:01 -0800952
953 pctldev->num_functions = 0;
954}
955
956#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */