1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/pinctrl/pinctrl-lantiq.c
4 * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
6 * Copyright (C) 2012 John Crispin <john@phrozen.org>
9 #include <linux/module.h>
10 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
16 #include "pinctrl-lantiq.h"
18 static int ltq_get_group_count(struct pinctrl_dev
*pctrldev
)
20 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
21 return info
->num_grps
;
24 static const char *ltq_get_group_name(struct pinctrl_dev
*pctrldev
,
27 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
28 if (selector
>= info
->num_grps
)
30 return info
->grps
[selector
].name
;
33 static int ltq_get_group_pins(struct pinctrl_dev
*pctrldev
,
35 const unsigned **pins
,
38 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
39 if (selector
>= info
->num_grps
)
41 *pins
= info
->grps
[selector
].pins
;
42 *num_pins
= info
->grps
[selector
].npins
;
46 static void ltq_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
47 struct pinctrl_map
*map
, unsigned num_maps
)
51 for (i
= 0; i
< num_maps
; i
++)
52 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
||
53 map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
54 kfree(map
[i
].data
.configs
.configs
);
58 static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
62 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
65 static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
66 struct device_node
*np
,
67 struct pinctrl_map
**map
)
69 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctldev
);
70 struct property
*pins
= of_find_property(np
, "lantiq,pins", NULL
);
71 struct property
*groups
= of_find_property(np
, "lantiq,groups", NULL
);
72 unsigned long configs
[3];
73 unsigned num_configs
= 0;
74 struct property
*prop
;
75 const char *group
, *pin
;
79 if (!pins
&& !groups
) {
80 dev_err(pctldev
->dev
, "%pOFn defines neither pins nor groups\n",
86 dev_err(pctldev
->dev
, "%pOFn defines both pins and groups\n",
91 ret
= of_property_read_string(np
, "lantiq,function", &function
);
93 of_property_for_each_string(np
, "lantiq,groups", prop
, group
) {
94 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
95 (*map
)->name
= function
;
96 (*map
)->data
.mux
.group
= group
;
97 (*map
)->data
.mux
.function
= function
;
102 for (i
= 0; i
< info
->num_params
; i
++) {
104 int ret
= of_property_read_u32(np
,
105 info
->params
[i
].property
, &val
);
107 configs
[num_configs
++] =
108 LTQ_PINCONF_PACK(info
->params
[i
].param
,
115 of_property_for_each_string(np
, "lantiq,pins", prop
, pin
) {
116 (*map
)->data
.configs
.configs
= kmemdup(configs
,
117 num_configs
* sizeof(unsigned long),
119 (*map
)->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
121 (*map
)->data
.configs
.group_or_pin
= pin
;
122 (*map
)->data
.configs
.num_configs
= num_configs
;
125 of_property_for_each_string(np
, "lantiq,groups", prop
, group
) {
126 (*map
)->data
.configs
.configs
= kmemdup(configs
,
127 num_configs
* sizeof(unsigned long),
129 (*map
)->type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
130 (*map
)->name
= group
;
131 (*map
)->data
.configs
.group_or_pin
= group
;
132 (*map
)->data
.configs
.num_configs
= num_configs
;
137 static int ltq_pinctrl_dt_subnode_size(struct device_node
*np
)
141 ret
= of_property_count_strings(np
, "lantiq,groups");
143 ret
= of_property_count_strings(np
, "lantiq,pins");
147 static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
148 struct device_node
*np_config
,
149 struct pinctrl_map
**map
,
152 struct pinctrl_map
*tmp
;
153 struct device_node
*np
;
156 for_each_child_of_node(np_config
, np
)
157 max_maps
+= ltq_pinctrl_dt_subnode_size(np
);
158 *map
= kzalloc(array3_size(max_maps
, sizeof(struct pinctrl_map
), 2),
164 for_each_child_of_node(np_config
, np
)
165 ltq_pinctrl_dt_subnode_to_map(pctldev
, np
, &tmp
);
166 *num_maps
= ((int)(tmp
- *map
));
171 static const struct pinctrl_ops ltq_pctrl_ops
= {
172 .get_groups_count
= ltq_get_group_count
,
173 .get_group_name
= ltq_get_group_name
,
174 .get_group_pins
= ltq_get_group_pins
,
175 .pin_dbg_show
= ltq_pinctrl_pin_dbg_show
,
176 .dt_node_to_map
= ltq_pinctrl_dt_node_to_map
,
177 .dt_free_map
= ltq_pinctrl_dt_free_map
,
180 static int ltq_pmx_func_count(struct pinctrl_dev
*pctrldev
)
182 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
184 return info
->num_funcs
;
187 static const char *ltq_pmx_func_name(struct pinctrl_dev
*pctrldev
,
190 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
192 if (selector
>= info
->num_funcs
)
195 return info
->funcs
[selector
].name
;
198 static int ltq_pmx_get_groups(struct pinctrl_dev
*pctrldev
,
200 const char * const **groups
,
201 unsigned * const num_groups
)
203 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
205 *groups
= info
->funcs
[func
].groups
;
206 *num_groups
= info
->funcs
[func
].num_groups
;
211 /* Return function number. If failure, return negative value. */
212 static int match_mux(const struct ltq_mfp_pin
*mfp
, unsigned mux
)
215 for (i
= 0; i
< LTQ_MAX_MUX
; i
++) {
216 if (mfp
->func
[i
] == mux
)
219 if (i
>= LTQ_MAX_MUX
)
224 /* dont assume .mfp is linearly mapped. find the mfp with the correct .pin */
225 static int match_mfp(const struct ltq_pinmux_info
*info
, int pin
)
228 for (i
= 0; i
< info
->num_mfp
; i
++) {
229 if (info
->mfp
[i
].pin
== pin
)
235 /* check whether current pin configuration is valid. Negative for failure */
236 static int match_group_mux(const struct ltq_pin_group
*grp
,
237 const struct ltq_pinmux_info
*info
,
241 for (i
= 0; i
< grp
->npins
; i
++) {
242 pin
= match_mfp(info
, grp
->pins
[i
]);
244 dev_err(info
->dev
, "could not find mfp for pin %d\n",
248 ret
= match_mux(&info
->mfp
[pin
], mux
);
250 dev_err(info
->dev
, "Can't find mux %d on pin%d\n",
258 static int ltq_pmx_set(struct pinctrl_dev
*pctrldev
,
262 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
263 const struct ltq_pin_group
*pin_grp
= &info
->grps
[group
];
264 int i
, pin
, pin_func
, ret
;
266 if (!pin_grp
->npins
||
267 (match_group_mux(pin_grp
, info
, pin_grp
->mux
) < 0)) {
268 dev_err(info
->dev
, "Failed to set the pin group: %s\n",
269 info
->grps
[group
].name
);
272 for (i
= 0; i
< pin_grp
->npins
; i
++) {
273 pin
= match_mfp(info
, pin_grp
->pins
[i
]);
275 dev_err(info
->dev
, "could not find mfp for pin %d\n",
279 pin_func
= match_mux(&info
->mfp
[pin
], pin_grp
->mux
);
280 ret
= info
->apply_mux(pctrldev
, pin
, pin_func
);
283 "failed to apply mux %d for pin %d\n",
291 static int ltq_pmx_gpio_request_enable(struct pinctrl_dev
*pctrldev
,
292 struct pinctrl_gpio_range
*range
,
295 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
296 int mfp
= match_mfp(info
, pin
);
300 dev_err(info
->dev
, "could not find mfp for pin %d\n", pin
);
304 pin_func
= match_mux(&info
->mfp
[mfp
], 0);
306 dev_err(info
->dev
, "No GPIO function on pin%d\n", mfp
);
310 return info
->apply_mux(pctrldev
, mfp
, pin_func
);
313 static const struct pinmux_ops ltq_pmx_ops
= {
314 .get_functions_count
= ltq_pmx_func_count
,
315 .get_function_name
= ltq_pmx_func_name
,
316 .get_function_groups
= ltq_pmx_get_groups
,
317 .set_mux
= ltq_pmx_set
,
318 .gpio_request_enable
= ltq_pmx_gpio_request_enable
,
322 * allow different socs to register with the generic part of the lanti
325 int ltq_pinctrl_register(struct platform_device
*pdev
,
326 struct ltq_pinmux_info
*info
)
328 struct pinctrl_desc
*desc
;
333 desc
->pctlops
= <q_pctrl_ops
;
334 desc
->pmxops
= <q_pmx_ops
;
335 info
->dev
= &pdev
->dev
;
337 info
->pctrl
= devm_pinctrl_register(&pdev
->dev
, desc
, info
);
338 if (IS_ERR(info
->pctrl
)) {
339 dev_err(&pdev
->dev
, "failed to register LTQ pinmux driver\n");
340 return PTR_ERR(info
->pctrl
);
342 platform_set_drvdata(pdev
, info
);