rtc: pcf8523: don't return invalid date when battery is low
[linux-2.6-block.git] / drivers / pinctrl / pinctrl-lantiq.c
CommitLineData
3f8c50c9
JC
1/*
2 * linux/drivers/pinctrl/pinctrl-lantiq.c
3 * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * publishhed by the Free Software Foundation.
8 *
55e40950 9 * Copyright (C) 2012 John Crispin <john@phrozen.org>
3f8c50c9
JC
10 */
11
12#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18
19#include "pinctrl-lantiq.h"
20
21static int ltq_get_group_count(struct pinctrl_dev *pctrldev)
22{
23 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
24 return info->num_grps;
25}
26
27static const char *ltq_get_group_name(struct pinctrl_dev *pctrldev,
28 unsigned selector)
29{
30 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
31 if (selector >= info->num_grps)
32 return NULL;
33 return info->grps[selector].name;
34}
35
36static int ltq_get_group_pins(struct pinctrl_dev *pctrldev,
37 unsigned selector,
38 const unsigned **pins,
39 unsigned *num_pins)
40{
41 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
42 if (selector >= info->num_grps)
43 return -EINVAL;
44 *pins = info->grps[selector].pins;
45 *num_pins = info->grps[selector].npins;
46 return 0;
47}
48
89377aa5
AL
49static void ltq_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
50 struct pinctrl_map *map, unsigned num_maps)
3f8c50c9
JC
51{
52 int i;
53
54 for (i = 0; i < num_maps; i++)
1dda2fa6
LP
55 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN ||
56 map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
3f8c50c9
JC
57 kfree(map[i].data.configs.configs);
58 kfree(map);
59}
60
61static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
62 struct seq_file *s,
63 unsigned offset)
64{
65 seq_printf(s, " %s", dev_name(pctldev->dev));
66}
67
3a6b04ca 68static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
3f8c50c9
JC
69 struct device_node *np,
70 struct pinctrl_map **map)
71{
72 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctldev);
3a6b04ca
JC
73 struct property *pins = of_find_property(np, "lantiq,pins", NULL);
74 struct property *groups = of_find_property(np, "lantiq,groups", NULL);
3f8c50c9
JC
75 unsigned long configs[3];
76 unsigned num_configs = 0;
77 struct property *prop;
78 const char *group, *pin;
79 const char *function;
80 int ret, i;
81
3a6b04ca 82 if (!pins && !groups) {
94f4e54c
RH
83 dev_err(pctldev->dev, "%pOFn defines neither pins nor groups\n",
84 np);
3a6b04ca
JC
85 return;
86 }
87
88 if (pins && groups) {
94f4e54c
RH
89 dev_err(pctldev->dev, "%pOFn defines both pins and groups\n",
90 np);
3a6b04ca
JC
91 return;
92 }
93
3f8c50c9 94 ret = of_property_read_string(np, "lantiq,function", &function);
3a6b04ca 95 if (groups && !ret) {
3f8c50c9
JC
96 of_property_for_each_string(np, "lantiq,groups", prop, group) {
97 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
98 (*map)->name = function;
99 (*map)->data.mux.group = group;
100 (*map)->data.mux.function = function;
101 (*map)++;
102 }
3f8c50c9
JC
103 }
104
105 for (i = 0; i < info->num_params; i++) {
106 u32 val;
107 int ret = of_property_read_u32(np,
108 info->params[i].property, &val);
109 if (!ret)
110 configs[num_configs++] =
111 LTQ_PINCONF_PACK(info->params[i].param,
112 val);
113 }
114
115 if (!num_configs)
3a6b04ca 116 return;
3f8c50c9
JC
117
118 of_property_for_each_string(np, "lantiq,pins", prop, pin) {
119 (*map)->data.configs.configs = kmemdup(configs,
120 num_configs * sizeof(unsigned long),
121 GFP_KERNEL);
122 (*map)->type = PIN_MAP_TYPE_CONFIGS_PIN;
123 (*map)->name = pin;
124 (*map)->data.configs.group_or_pin = pin;
125 (*map)->data.configs.num_configs = num_configs;
126 (*map)++;
127 }
3a6b04ca
JC
128 of_property_for_each_string(np, "lantiq,groups", prop, group) {
129 (*map)->data.configs.configs = kmemdup(configs,
130 num_configs * sizeof(unsigned long),
131 GFP_KERNEL);
132 (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
133 (*map)->name = group;
134 (*map)->data.configs.group_or_pin = group;
135 (*map)->data.configs.num_configs = num_configs;
136 (*map)++;
137 }
3f8c50c9
JC
138}
139
140static int ltq_pinctrl_dt_subnode_size(struct device_node *np)
141{
142 int ret;
143
144 ret = of_property_count_strings(np, "lantiq,groups");
145 if (ret < 0)
146 ret = of_property_count_strings(np, "lantiq,pins");
147 return ret;
148}
149
89377aa5
AL
150static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
151 struct device_node *np_config,
152 struct pinctrl_map **map,
153 unsigned *num_maps)
3f8c50c9
JC
154{
155 struct pinctrl_map *tmp;
156 struct device_node *np;
3a6b04ca 157 int max_maps = 0;
3f8c50c9 158
3f8c50c9 159 for_each_child_of_node(np_config, np)
3a6b04ca 160 max_maps += ltq_pinctrl_dt_subnode_size(np);
6396bb22
KC
161 *map = kzalloc(array3_size(max_maps, sizeof(struct pinctrl_map), 2),
162 GFP_KERNEL);
3f8c50c9
JC
163 if (!*map)
164 return -ENOMEM;
165 tmp = *map;
166
3a6b04ca
JC
167 for_each_child_of_node(np_config, np)
168 ltq_pinctrl_dt_subnode_to_map(pctldev, np, &tmp);
169 *num_maps = ((int)(tmp - *map));
170
3f8c50c9
JC
171 return 0;
172}
173
022ab148 174static const struct pinctrl_ops ltq_pctrl_ops = {
3f8c50c9
JC
175 .get_groups_count = ltq_get_group_count,
176 .get_group_name = ltq_get_group_name,
177 .get_group_pins = ltq_get_group_pins,
178 .pin_dbg_show = ltq_pinctrl_pin_dbg_show,
179 .dt_node_to_map = ltq_pinctrl_dt_node_to_map,
180 .dt_free_map = ltq_pinctrl_dt_free_map,
181};
182
183static int ltq_pmx_func_count(struct pinctrl_dev *pctrldev)
184{
185 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
186
187 return info->num_funcs;
188}
189
190static const char *ltq_pmx_func_name(struct pinctrl_dev *pctrldev,
191 unsigned selector)
192{
193 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
194
195 if (selector >= info->num_funcs)
196 return NULL;
197
198 return info->funcs[selector].name;
199}
200
201static int ltq_pmx_get_groups(struct pinctrl_dev *pctrldev,
202 unsigned func,
203 const char * const **groups,
204 unsigned * const num_groups)
205{
206 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
207
208 *groups = info->funcs[func].groups;
209 *num_groups = info->funcs[func].num_groups;
210
211 return 0;
212}
213
214/* Return function number. If failure, return negative value. */
215static int match_mux(const struct ltq_mfp_pin *mfp, unsigned mux)
216{
217 int i;
218 for (i = 0; i < LTQ_MAX_MUX; i++) {
219 if (mfp->func[i] == mux)
220 break;
221 }
222 if (i >= LTQ_MAX_MUX)
223 return -EINVAL;
224 return i;
225}
226
227/* dont assume .mfp is linearly mapped. find the mfp with the correct .pin */
228static int match_mfp(const struct ltq_pinmux_info *info, int pin)
229{
230 int i;
231 for (i = 0; i < info->num_mfp; i++) {
232 if (info->mfp[i].pin == pin)
233 return i;
234 }
235 return -1;
236}
237
238/* check whether current pin configuration is valid. Negative for failure */
239static int match_group_mux(const struct ltq_pin_group *grp,
240 const struct ltq_pinmux_info *info,
241 unsigned mux)
242{
243 int i, pin, ret = 0;
244 for (i = 0; i < grp->npins; i++) {
245 pin = match_mfp(info, grp->pins[i]);
246 if (pin < 0) {
247 dev_err(info->dev, "could not find mfp for pin %d\n",
248 grp->pins[i]);
249 return -EINVAL;
250 }
251 ret = match_mux(&info->mfp[pin], mux);
252 if (ret < 0) {
253 dev_err(info->dev, "Can't find mux %d on pin%d\n",
254 mux, pin);
255 break;
256 }
257 }
258 return ret;
259}
260
03e9f0ca
LW
261static int ltq_pmx_set(struct pinctrl_dev *pctrldev,
262 unsigned func,
263 unsigned group)
3f8c50c9
JC
264{
265 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
266 const struct ltq_pin_group *pin_grp = &info->grps[group];
267 int i, pin, pin_func, ret;
268
269 if (!pin_grp->npins ||
270 (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
271 dev_err(info->dev, "Failed to set the pin group: %s\n",
272 info->grps[group].name);
273 return -EINVAL;
274 }
275 for (i = 0; i < pin_grp->npins; i++) {
276 pin = match_mfp(info, pin_grp->pins[i]);
277 if (pin < 0) {
278 dev_err(info->dev, "could not find mfp for pin %d\n",
279 pin_grp->pins[i]);
280 return -EINVAL;
281 }
282 pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
283 ret = info->apply_mux(pctrldev, pin, pin_func);
284 if (ret) {
285 dev_err(info->dev,
286 "failed to apply mux %d for pin %d\n",
287 pin_func, pin);
288 return ret;
289 }
290 }
291 return 0;
292}
293
3f8c50c9
JC
294static int ltq_pmx_gpio_request_enable(struct pinctrl_dev *pctrldev,
295 struct pinctrl_gpio_range *range,
296 unsigned pin)
297{
298 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
d8a7c1f1 299 int mfp = match_mfp(info, pin);
3f8c50c9
JC
300 int pin_func;
301
302 if (mfp < 0) {
303 dev_err(info->dev, "could not find mfp for pin %d\n", pin);
304 return -EINVAL;
305 }
306
307 pin_func = match_mux(&info->mfp[mfp], 0);
308 if (pin_func < 0) {
309 dev_err(info->dev, "No GPIO function on pin%d\n", mfp);
310 return -EINVAL;
311 }
312
313 return info->apply_mux(pctrldev, mfp, pin_func);
314}
315
022ab148 316static const struct pinmux_ops ltq_pmx_ops = {
3f8c50c9
JC
317 .get_functions_count = ltq_pmx_func_count,
318 .get_function_name = ltq_pmx_func_name,
319 .get_function_groups = ltq_pmx_get_groups,
03e9f0ca 320 .set_mux = ltq_pmx_set,
3f8c50c9
JC
321 .gpio_request_enable = ltq_pmx_gpio_request_enable,
322};
323
324/*
325 * allow different socs to register with the generic part of the lanti
326 * pinctrl code
327 */
328int ltq_pinctrl_register(struct platform_device *pdev,
329 struct ltq_pinmux_info *info)
330{
331 struct pinctrl_desc *desc;
332
333 if (!info)
334 return -EINVAL;
335 desc = info->desc;
336 desc->pctlops = &ltq_pctrl_ops;
337 desc->pmxops = &ltq_pmx_ops;
338 info->dev = &pdev->dev;
339
280132d1 340 info->pctrl = devm_pinctrl_register(&pdev->dev, desc, info);
323de9ef 341 if (IS_ERR(info->pctrl)) {
3f8c50c9 342 dev_err(&pdev->dev, "failed to register LTQ pinmux driver\n");
323de9ef 343 return PTR_ERR(info->pctrl);
3f8c50c9
JC
344 }
345 platform_set_drvdata(pdev, info);
346 return 0;
347}