blob: 0c9d08d8469471a909665abe3a300282c574f771 [file] [log] [blame]
Linus Walleijae6b4d82011-10-19 18:14:33 +02001/*
2 * Core driver for the pin config portions of the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 *
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 *
9 * License terms: GNU General Public License (GPL) version 2
10 */
11#define pr_fmt(fmt) "pinconfig core: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
20#include <linux/pinctrl/machine.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinconf.h>
23#include "core.h"
24#include "pinconf.h"
25
Stephen Warren2b694252012-02-19 23:45:46 -070026int pinconf_check_ops(struct pinctrl_dev *pctldev)
27{
28 const struct pinconf_ops *ops = pctldev->desc->confops;
29
30 /* We must be able to read out pin status */
31 if (!ops->pin_config_get && !ops->pin_config_group_get)
32 return -EINVAL;
33 /* We have to be able to config the pins in SOME way */
34 if (!ops->pin_config_set && !ops->pin_config_group_set)
35 return -EINVAL;
36 return 0;
37}
38
39static int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020040 unsigned long *config)
41{
42 const struct pinconf_ops *ops = pctldev->desc->confops;
43
44 if (!ops || !ops->pin_config_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070045 dev_err(pctldev->dev, "cannot get pin configuration, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020046 "pin_config_get() function in driver\n");
47 return -EINVAL;
48 }
49
50 return ops->pin_config_get(pctldev, pin, config);
51}
52
53/**
54 * pin_config_get() - get the configuration of a single pin parameter
Stephen Warren43699de2011-12-15 16:57:17 -070055 * @dev_name: name of the pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +020056 * @name: name of the pin to get the config for
57 * @config: the config pointed to by this argument will be filled in with the
58 * current pin state, it can be used directly by drivers as a numeral, or
59 * it can be dereferenced to any struct.
60 */
Stephen Warren43699de2011-12-15 16:57:17 -070061int pin_config_get(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +020062 unsigned long *config)
63{
Stephen Warren43699de2011-12-15 16:57:17 -070064 struct pinctrl_dev *pctldev;
Linus Walleijae6b4d82011-10-19 18:14:33 +020065 int pin;
66
Linus Walleij9dfac4f2012-02-01 18:02:47 +010067 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren43699de2011-12-15 16:57:17 -070068 if (!pctldev)
69 return -EINVAL;
70
Linus Walleijae6b4d82011-10-19 18:14:33 +020071 pin = pin_get_from_name(pctldev, name);
72 if (pin < 0)
73 return pin;
74
75 return pin_config_get_for_pin(pctldev, pin, config);
76}
77EXPORT_SYMBOL(pin_config_get);
78
Stephen Warren2b694252012-02-19 23:45:46 -070079static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020080 unsigned long config)
81{
82 const struct pinconf_ops *ops = pctldev->desc->confops;
83 int ret;
84
85 if (!ops || !ops->pin_config_set) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070086 dev_err(pctldev->dev, "cannot configure pin, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020087 "config function in driver\n");
88 return -EINVAL;
89 }
90
91 ret = ops->pin_config_set(pctldev, pin, config);
92 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070093 dev_err(pctldev->dev,
Linus Walleijae6b4d82011-10-19 18:14:33 +020094 "unable to set pin configuration on pin %d\n", pin);
95 return ret;
96 }
97
98 return 0;
99}
100
101/**
102 * pin_config_set() - set the configuration of a single pin parameter
Stephen Warren43699de2011-12-15 16:57:17 -0700103 * @dev_name: name of pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +0200104 * @name: name of the pin to set the config for
105 * @config: the config in this argument will contain the desired pin state, it
106 * can be used directly by drivers as a numeral, or it can be dereferenced
107 * to any struct.
108 */
Stephen Warren43699de2011-12-15 16:57:17 -0700109int pin_config_set(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200110 unsigned long config)
111{
Stephen Warren43699de2011-12-15 16:57:17 -0700112 struct pinctrl_dev *pctldev;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200113 int pin;
114
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100115 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren43699de2011-12-15 16:57:17 -0700116 if (!pctldev)
117 return -EINVAL;
118
Linus Walleijae6b4d82011-10-19 18:14:33 +0200119 pin = pin_get_from_name(pctldev, name);
120 if (pin < 0)
121 return pin;
122
123 return pin_config_set_for_pin(pctldev, pin, config);
124}
125EXPORT_SYMBOL(pin_config_set);
126
Stephen Warren43699de2011-12-15 16:57:17 -0700127int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200128 unsigned long *config)
129{
Stephen Warren43699de2011-12-15 16:57:17 -0700130 struct pinctrl_dev *pctldev;
131 const struct pinconf_ops *ops;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200132 int selector;
133
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100134 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren43699de2011-12-15 16:57:17 -0700135 if (!pctldev)
136 return -EINVAL;
137 ops = pctldev->desc->confops;
138
Linus Walleijae6b4d82011-10-19 18:14:33 +0200139 if (!ops || !ops->pin_config_group_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700140 dev_err(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 "group, missing group config get function in "
142 "driver\n");
143 return -EINVAL;
144 }
145
146 selector = pinctrl_get_group_selector(pctldev, pin_group);
147 if (selector < 0)
148 return selector;
149
150 return ops->pin_config_group_get(pctldev, selector, config);
151}
152EXPORT_SYMBOL(pin_config_group_get);
153
Stephen Warren43699de2011-12-15 16:57:17 -0700154int pin_config_group_set(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200155 unsigned long config)
156{
Stephen Warren43699de2011-12-15 16:57:17 -0700157 struct pinctrl_dev *pctldev;
158 const struct pinconf_ops *ops;
159 const struct pinctrl_ops *pctlops;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200160 int selector;
161 const unsigned *pins;
162 unsigned num_pins;
163 int ret;
164 int i;
165
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100166 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren43699de2011-12-15 16:57:17 -0700167 if (!pctldev)
168 return -EINVAL;
169 ops = pctldev->desc->confops;
170 pctlops = pctldev->desc->pctlops;
171
Linus Walleijae6b4d82011-10-19 18:14:33 +0200172 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700173 dev_err(pctldev->dev, "cannot configure pin group, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200174 "config function in driver\n");
175 return -EINVAL;
176 }
177
178 selector = pinctrl_get_group_selector(pctldev, pin_group);
179 if (selector < 0)
180 return selector;
181
182 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
183 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700184 dev_err(pctldev->dev, "cannot configure pin group, error "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200185 "getting pins\n");
186 return ret;
187 }
188
189 /*
190 * If the pin controller supports handling entire groups we use that
191 * capability.
192 */
193 if (ops->pin_config_group_set) {
194 ret = ops->pin_config_group_set(pctldev, selector, config);
195 /*
196 * If the pin controller prefer that a certain group be handled
197 * pin-by-pin as well, it returns -EAGAIN.
198 */
199 if (ret != -EAGAIN)
200 return ret;
201 }
202
203 /*
204 * If the controller cannot handle entire groups, we configure each pin
205 * individually.
206 */
207 if (!ops->pin_config_set)
208 return 0;
209
210 for (i = 0; i < num_pins; i++) {
211 ret = ops->pin_config_set(pctldev, pins[i], config);
212 if (ret < 0)
213 return ret;
214 }
215
216 return 0;
217}
218EXPORT_SYMBOL(pin_config_group_set);
219
Linus Walleijae6b4d82011-10-19 18:14:33 +0200220#ifdef CONFIG_DEBUG_FS
221
222static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
223 struct seq_file *s, int pin)
224{
225 const struct pinconf_ops *ops = pctldev->desc->confops;
226
227 if (ops && ops->pin_config_dbg_show)
228 ops->pin_config_dbg_show(pctldev, s, pin);
229}
230
231static int pinconf_pins_show(struct seq_file *s, void *what)
232{
233 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900234 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200235
236 seq_puts(s, "Pin config settings per pin\n");
237 seq_puts(s, "Format: pin (name): pinmux setting array\n");
238
Chanho Park706e8522012-01-03 16:47:50 +0900239 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700240 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200241 struct pin_desc *desc;
242
Chanho Park706e8522012-01-03 16:47:50 +0900243 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200244 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900245 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200246 if (desc == NULL)
247 continue;
248
249 seq_printf(s, "pin %d (%s):", pin,
250 desc->name ? desc->name : "unnamed");
251
252 pinconf_dump_pin(pctldev, s, pin);
253
254 seq_printf(s, "\n");
255 }
256
257 return 0;
258}
259
260static void pinconf_dump_group(struct pinctrl_dev *pctldev,
261 struct seq_file *s, unsigned selector,
262 const char *gname)
263{
264 const struct pinconf_ops *ops = pctldev->desc->confops;
265
266 if (ops && ops->pin_config_group_dbg_show)
267 ops->pin_config_group_dbg_show(pctldev, s, selector);
268}
269
270static int pinconf_groups_show(struct seq_file *s, void *what)
271{
272 struct pinctrl_dev *pctldev = s->private;
273 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
274 const struct pinconf_ops *ops = pctldev->desc->confops;
275 unsigned selector = 0;
276
277 if (!ops || !ops->pin_config_group_get)
278 return 0;
279
280 seq_puts(s, "Pin config settings per pin group\n");
281 seq_puts(s, "Format: group (name): pinmux setting array\n");
282
283 while (pctlops->list_groups(pctldev, selector) >= 0) {
284 const char *gname = pctlops->get_group_name(pctldev, selector);
285
286 seq_printf(s, "%u (%s):", selector, gname);
287 pinconf_dump_group(pctldev, s, selector, gname);
288 selector++;
289 }
290
291 return 0;
292}
293
294static int pinconf_pins_open(struct inode *inode, struct file *file)
295{
296 return single_open(file, pinconf_pins_show, inode->i_private);
297}
298
299static int pinconf_groups_open(struct inode *inode, struct file *file)
300{
301 return single_open(file, pinconf_groups_show, inode->i_private);
302}
303
304static const struct file_operations pinconf_pins_ops = {
305 .open = pinconf_pins_open,
306 .read = seq_read,
307 .llseek = seq_lseek,
308 .release = single_release,
309};
310
311static const struct file_operations pinconf_groups_ops = {
312 .open = pinconf_groups_open,
313 .read = seq_read,
314 .llseek = seq_lseek,
315 .release = single_release,
316};
317
318void pinconf_init_device_debugfs(struct dentry *devroot,
319 struct pinctrl_dev *pctldev)
320{
321 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
322 devroot, pctldev, &pinconf_pins_ops);
323 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
324 devroot, pctldev, &pinconf_groups_ops);
325}
326
327#endif