blob: 9195eefe258a5181f19121830eafb20d80c7b29c [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
26int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
27 unsigned long *config)
28{
29 const struct pinconf_ops *ops = pctldev->desc->confops;
30
31 if (!ops || !ops->pin_config_get) {
32 dev_err(&pctldev->dev, "cannot get pin configuration, missing "
33 "pin_config_get() function in driver\n");
34 return -EINVAL;
35 }
36
37 return ops->pin_config_get(pctldev, pin, config);
38}
39
40/**
41 * pin_config_get() - get the configuration of a single pin parameter
42 * @pctldev: pin controller device for this pin
43 * @name: name of the pin to get the config for
44 * @config: the config pointed to by this argument will be filled in with the
45 * current pin state, it can be used directly by drivers as a numeral, or
46 * it can be dereferenced to any struct.
47 */
48int pin_config_get(struct pinctrl_dev *pctldev, const char *name,
49 unsigned long *config)
50{
51 int pin;
52
53 pin = pin_get_from_name(pctldev, name);
54 if (pin < 0)
55 return pin;
56
57 return pin_config_get_for_pin(pctldev, pin, config);
58}
59EXPORT_SYMBOL(pin_config_get);
60
61int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
62 unsigned long config)
63{
64 const struct pinconf_ops *ops = pctldev->desc->confops;
65 int ret;
66
67 if (!ops || !ops->pin_config_set) {
68 dev_err(&pctldev->dev, "cannot configure pin, missing "
69 "config function in driver\n");
70 return -EINVAL;
71 }
72
73 ret = ops->pin_config_set(pctldev, pin, config);
74 if (ret) {
75 dev_err(&pctldev->dev,
76 "unable to set pin configuration on pin %d\n", pin);
77 return ret;
78 }
79
80 return 0;
81}
82
83/**
84 * pin_config_set() - set the configuration of a single pin parameter
85 * @pctldev: pin controller device for this pin
86 * @name: name of the pin to set the config for
87 * @config: the config in this argument will contain the desired pin state, it
88 * can be used directly by drivers as a numeral, or it can be dereferenced
89 * to any struct.
90 */
91int pin_config_set(struct pinctrl_dev *pctldev, const char *name,
92 unsigned long config)
93{
94 int pin;
95
96 pin = pin_get_from_name(pctldev, name);
97 if (pin < 0)
98 return pin;
99
100 return pin_config_set_for_pin(pctldev, pin, config);
101}
102EXPORT_SYMBOL(pin_config_set);
103
104int pin_config_group_get(struct pinctrl_dev *pctldev, const char *pin_group,
105 unsigned long *config)
106{
107 const struct pinconf_ops *ops = pctldev->desc->confops;
108 int selector;
109
110 if (!ops || !ops->pin_config_group_get) {
111 dev_err(&pctldev->dev, "cannot get configuration for pin "
112 "group, missing group config get function in "
113 "driver\n");
114 return -EINVAL;
115 }
116
117 selector = pinctrl_get_group_selector(pctldev, pin_group);
118 if (selector < 0)
119 return selector;
120
121 return ops->pin_config_group_get(pctldev, selector, config);
122}
123EXPORT_SYMBOL(pin_config_group_get);
124
125
126int pin_config_group_set(struct pinctrl_dev *pctldev, const char *pin_group,
127 unsigned long config)
128{
129 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
130 const struct pinconf_ops *ops = pctldev->desc->confops;
131 int selector;
132 const unsigned *pins;
133 unsigned num_pins;
134 int ret;
135 int i;
136
137 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
138 dev_err(&pctldev->dev, "cannot configure pin group, missing "
139 "config function in driver\n");
140 return -EINVAL;
141 }
142
143 selector = pinctrl_get_group_selector(pctldev, pin_group);
144 if (selector < 0)
145 return selector;
146
147 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
148 if (ret) {
149 dev_err(&pctldev->dev, "cannot configure pin group, error "
150 "getting pins\n");
151 return ret;
152 }
153
154 /*
155 * If the pin controller supports handling entire groups we use that
156 * capability.
157 */
158 if (ops->pin_config_group_set) {
159 ret = ops->pin_config_group_set(pctldev, selector, config);
160 /*
161 * If the pin controller prefer that a certain group be handled
162 * pin-by-pin as well, it returns -EAGAIN.
163 */
164 if (ret != -EAGAIN)
165 return ret;
166 }
167
168 /*
169 * If the controller cannot handle entire groups, we configure each pin
170 * individually.
171 */
172 if (!ops->pin_config_set)
173 return 0;
174
175 for (i = 0; i < num_pins; i++) {
176 ret = ops->pin_config_set(pctldev, pins[i], config);
177 if (ret < 0)
178 return ret;
179 }
180
181 return 0;
182}
183EXPORT_SYMBOL(pin_config_group_set);
184
185int pinconf_check_ops(const struct pinconf_ops *ops)
186{
187 /* We must be able to read out pin status */
188 if (!ops->pin_config_get && !ops->pin_config_group_get)
189 return -EINVAL;
190 /* We have to be able to config the pins in SOME way */
191 if (!ops->pin_config_set && !ops->pin_config_group_set)
192 return -EINVAL;
193 return 0;
194}
195
196#ifdef CONFIG_DEBUG_FS
197
198static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
199 struct seq_file *s, int pin)
200{
201 const struct pinconf_ops *ops = pctldev->desc->confops;
202
203 if (ops && ops->pin_config_dbg_show)
204 ops->pin_config_dbg_show(pctldev, s, pin);
205}
206
207static int pinconf_pins_show(struct seq_file *s, void *what)
208{
209 struct pinctrl_dev *pctldev = s->private;
210 unsigned pin;
211
212 seq_puts(s, "Pin config settings per pin\n");
213 seq_puts(s, "Format: pin (name): pinmux setting array\n");
214
215 /* The highest pin number need to be included in the loop, thus <= */
216 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
217 struct pin_desc *desc;
218
219 desc = pin_desc_get(pctldev, pin);
220 /* Pin space may be sparse */
221 if (desc == NULL)
222 continue;
223
224 seq_printf(s, "pin %d (%s):", pin,
225 desc->name ? desc->name : "unnamed");
226
227 pinconf_dump_pin(pctldev, s, pin);
228
229 seq_printf(s, "\n");
230 }
231
232 return 0;
233}
234
235static void pinconf_dump_group(struct pinctrl_dev *pctldev,
236 struct seq_file *s, unsigned selector,
237 const char *gname)
238{
239 const struct pinconf_ops *ops = pctldev->desc->confops;
240
241 if (ops && ops->pin_config_group_dbg_show)
242 ops->pin_config_group_dbg_show(pctldev, s, selector);
243}
244
245static int pinconf_groups_show(struct seq_file *s, void *what)
246{
247 struct pinctrl_dev *pctldev = s->private;
248 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
249 const struct pinconf_ops *ops = pctldev->desc->confops;
250 unsigned selector = 0;
251
252 if (!ops || !ops->pin_config_group_get)
253 return 0;
254
255 seq_puts(s, "Pin config settings per pin group\n");
256 seq_puts(s, "Format: group (name): pinmux setting array\n");
257
258 while (pctlops->list_groups(pctldev, selector) >= 0) {
259 const char *gname = pctlops->get_group_name(pctldev, selector);
260
261 seq_printf(s, "%u (%s):", selector, gname);
262 pinconf_dump_group(pctldev, s, selector, gname);
263 selector++;
264 }
265
266 return 0;
267}
268
269static int pinconf_pins_open(struct inode *inode, struct file *file)
270{
271 return single_open(file, pinconf_pins_show, inode->i_private);
272}
273
274static int pinconf_groups_open(struct inode *inode, struct file *file)
275{
276 return single_open(file, pinconf_groups_show, inode->i_private);
277}
278
279static const struct file_operations pinconf_pins_ops = {
280 .open = pinconf_pins_open,
281 .read = seq_read,
282 .llseek = seq_lseek,
283 .release = single_release,
284};
285
286static const struct file_operations pinconf_groups_ops = {
287 .open = pinconf_groups_open,
288 .read = seq_read,
289 .llseek = seq_lseek,
290 .release = single_release,
291};
292
293void pinconf_init_device_debugfs(struct dentry *devroot,
294 struct pinctrl_dev *pctldev)
295{
296 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
297 devroot, pctldev, &pinconf_pins_ops);
298 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
299 devroot, pctldev, &pinconf_groups_ops);
300}
301
302#endif