2 * linux/drivers/pinctrl/pinctrl-lantiq.c
3 * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
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.
9 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
12 #include <linux/module.h>
13 #include <linux/device.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
19 #include "pinctrl-lantiq.h"
21 static int ltq_get_group_count(struct pinctrl_dev
*pctrldev
)
23 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
24 return info
->num_grps
;
27 static const char *ltq_get_group_name(struct pinctrl_dev
*pctrldev
,
30 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
31 if (selector
>= info
->num_grps
)
33 return info
->grps
[selector
].name
;
36 static int ltq_get_group_pins(struct pinctrl_dev
*pctrldev
,
38 const unsigned **pins
,
41 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
42 if (selector
>= info
->num_grps
)
44 *pins
= info
->grps
[selector
].pins
;
45 *num_pins
= info
->grps
[selector
].npins
;
49 static void ltq_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
50 struct pinctrl_map
*map
, unsigned num_maps
)
54 for (i
= 0; i
< num_maps
; i
++)
55 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
||
56 map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
57 kfree(map
[i
].data
.configs
.configs
);
61 static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
65 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
68 static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
69 struct device_node
*np
,
70 struct pinctrl_map
**map
)
72 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctldev
);
73 struct property
*pins
= of_find_property(np
, "lantiq,pins", NULL
);
74 struct property
*groups
= of_find_property(np
, "lantiq,groups", NULL
);
75 unsigned long configs
[3];
76 unsigned num_configs
= 0;
77 struct property
*prop
;
78 const char *group
, *pin
;
82 if (!pins
&& !groups
) {
83 dev_err(pctldev
->dev
, "%s defines neither pins nor groups\n",
89 dev_err(pctldev
->dev
, "%s defines both pins and groups\n",
94 ret
= of_property_read_string(np
, "lantiq,function", &function
);
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
;
105 for (i
= 0; i
< info
->num_params
; i
++) {
107 int ret
= of_property_read_u32(np
,
108 info
->params
[i
].property
, &val
);
110 configs
[num_configs
++] =
111 LTQ_PINCONF_PACK(info
->params
[i
].param
,
118 of_property_for_each_string(np
, "lantiq,pins", prop
, pin
) {
119 (*map
)->data
.configs
.configs
= kmemdup(configs
,
120 num_configs
* sizeof(unsigned long),
122 (*map
)->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
124 (*map
)->data
.configs
.group_or_pin
= pin
;
125 (*map
)->data
.configs
.num_configs
= num_configs
;
128 of_property_for_each_string(np
, "lantiq,groups", prop
, group
) {
129 (*map
)->data
.configs
.configs
= kmemdup(configs
,
130 num_configs
* sizeof(unsigned long),
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
;
140 static int ltq_pinctrl_dt_subnode_size(struct device_node
*np
)
144 ret
= of_property_count_strings(np
, "lantiq,groups");
146 ret
= of_property_count_strings(np
, "lantiq,pins");
150 static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
151 struct device_node
*np_config
,
152 struct pinctrl_map
**map
,
155 struct pinctrl_map
*tmp
;
156 struct device_node
*np
;
159 for_each_child_of_node(np_config
, np
)
160 max_maps
+= ltq_pinctrl_dt_subnode_size(np
);
161 *map
= kzalloc(max_maps
* sizeof(struct pinctrl_map
) * 2, GFP_KERNEL
);
166 for_each_child_of_node(np_config
, np
)
167 ltq_pinctrl_dt_subnode_to_map(pctldev
, np
, &tmp
);
168 *num_maps
= ((int)(tmp
- *map
));
173 static const struct pinctrl_ops ltq_pctrl_ops
= {
174 .get_groups_count
= ltq_get_group_count
,
175 .get_group_name
= ltq_get_group_name
,
176 .get_group_pins
= ltq_get_group_pins
,
177 .pin_dbg_show
= ltq_pinctrl_pin_dbg_show
,
178 .dt_node_to_map
= ltq_pinctrl_dt_node_to_map
,
179 .dt_free_map
= ltq_pinctrl_dt_free_map
,
182 static int ltq_pmx_func_count(struct pinctrl_dev
*pctrldev
)
184 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
186 return info
->num_funcs
;
189 static const char *ltq_pmx_func_name(struct pinctrl_dev
*pctrldev
,
192 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
194 if (selector
>= info
->num_funcs
)
197 return info
->funcs
[selector
].name
;
200 static int ltq_pmx_get_groups(struct pinctrl_dev
*pctrldev
,
202 const char * const **groups
,
203 unsigned * const num_groups
)
205 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
207 *groups
= info
->funcs
[func
].groups
;
208 *num_groups
= info
->funcs
[func
].num_groups
;
213 /* Return function number. If failure, return negative value. */
214 static int match_mux(const struct ltq_mfp_pin
*mfp
, unsigned mux
)
217 for (i
= 0; i
< LTQ_MAX_MUX
; i
++) {
218 if (mfp
->func
[i
] == mux
)
221 if (i
>= LTQ_MAX_MUX
)
226 /* dont assume .mfp is linearly mapped. find the mfp with the correct .pin */
227 static int match_mfp(const struct ltq_pinmux_info
*info
, int pin
)
230 for (i
= 0; i
< info
->num_mfp
; i
++) {
231 if (info
->mfp
[i
].pin
== pin
)
237 /* check whether current pin configuration is valid. Negative for failure */
238 static int match_group_mux(const struct ltq_pin_group
*grp
,
239 const struct ltq_pinmux_info
*info
,
243 for (i
= 0; i
< grp
->npins
; i
++) {
244 pin
= match_mfp(info
, grp
->pins
[i
]);
246 dev_err(info
->dev
, "could not find mfp for pin %d\n",
250 ret
= match_mux(&info
->mfp
[pin
], mux
);
252 dev_err(info
->dev
, "Can't find mux %d on pin%d\n",
260 static int ltq_pmx_set(struct pinctrl_dev
*pctrldev
,
264 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
265 const struct ltq_pin_group
*pin_grp
= &info
->grps
[group
];
266 int i
, pin
, pin_func
, ret
;
268 if (!pin_grp
->npins
||
269 (match_group_mux(pin_grp
, info
, pin_grp
->mux
) < 0)) {
270 dev_err(info
->dev
, "Failed to set the pin group: %s\n",
271 info
->grps
[group
].name
);
274 for (i
= 0; i
< pin_grp
->npins
; i
++) {
275 pin
= match_mfp(info
, pin_grp
->pins
[i
]);
277 dev_err(info
->dev
, "could not find mfp for pin %d\n",
281 pin_func
= match_mux(&info
->mfp
[pin
], pin_grp
->mux
);
282 ret
= info
->apply_mux(pctrldev
, pin
, pin_func
);
285 "failed to apply mux %d for pin %d\n",
293 static int ltq_pmx_gpio_request_enable(struct pinctrl_dev
*pctrldev
,
294 struct pinctrl_gpio_range
*range
,
297 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
298 int mfp
= match_mfp(info
, pin
);
302 dev_err(info
->dev
, "could not find mfp for pin %d\n", pin
);
306 pin_func
= match_mux(&info
->mfp
[mfp
], 0);
308 dev_err(info
->dev
, "No GPIO function on pin%d\n", mfp
);
312 return info
->apply_mux(pctrldev
, mfp
, pin_func
);
315 static const struct pinmux_ops ltq_pmx_ops
= {
316 .get_functions_count
= ltq_pmx_func_count
,
317 .get_function_name
= ltq_pmx_func_name
,
318 .get_function_groups
= ltq_pmx_get_groups
,
319 .set_mux
= ltq_pmx_set
,
320 .gpio_request_enable
= ltq_pmx_gpio_request_enable
,
324 * allow different socs to register with the generic part of the lanti
327 int ltq_pinctrl_register(struct platform_device
*pdev
,
328 struct ltq_pinmux_info
*info
)
330 struct pinctrl_desc
*desc
;
335 desc
->pctlops
= <q_pctrl_ops
;
336 desc
->pmxops
= <q_pmx_ops
;
337 info
->dev
= &pdev
->dev
;
339 info
->pctrl
= pinctrl_register(desc
, &pdev
->dev
, info
);
341 dev_err(&pdev
->dev
, "failed to register LTQ pinmux driver\n");
344 platform_set_drvdata(pdev
, info
);