2 * Driver for the ST Microelectronics SPEAr pinmux
4 * Copyright (C) 2012 ST Microelectronics
5 * Viresh Kumar <viresh.linux@gmail.com>
8 * - U300 Pinctl drivers
9 * - Tegra Pinctl drivers
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/err.h>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "pinctrl-spear.h"
29 #define DRIVER_NAME "spear-pinmux"
31 static void muxregs_endisable(struct spear_pmx
*pmx
,
32 struct spear_muxreg
*muxregs
, u8 count
, bool enable
)
34 struct spear_muxreg
*muxreg
;
37 for (j
= 0; j
< count
; j
++) {
40 val
= pmx_readl(pmx
, muxreg
->reg
);
48 val
|= muxreg
->mask
& temp
;
49 pmx_writel(pmx
, val
, muxreg
->reg
);
53 static int set_mode(struct spear_pmx
*pmx
, int mode
)
55 struct spear_pmx_mode
*pmx_mode
= NULL
;
59 if (!pmx
->machdata
->pmx_modes
|| !pmx
->machdata
->npmx_modes
)
62 for (i
= 0; i
< pmx
->machdata
->npmx_modes
; i
++) {
63 if (pmx
->machdata
->pmx_modes
[i
]->mode
== (1 << mode
)) {
64 pmx_mode
= pmx
->machdata
->pmx_modes
[i
];
72 val
= pmx_readl(pmx
, pmx_mode
->reg
);
73 val
&= ~pmx_mode
->mask
;
75 pmx_writel(pmx
, val
, pmx_mode
->reg
);
77 pmx
->machdata
->mode
= pmx_mode
->mode
;
78 dev_info(pmx
->dev
, "Configured Mode: %s with id: %x\n\n",
79 pmx_mode
->name
? pmx_mode
->name
: "no_name",
85 void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup
*gpio_pingroup
,
86 unsigned count
, u16 reg
)
90 for (i
= 0; i
< count
; i
++)
91 for (j
= 0; j
< gpio_pingroup
[i
].nmuxregs
; j
++)
92 gpio_pingroup
[i
].muxregs
[j
].reg
= reg
;
95 void pmx_init_addr(struct spear_pinctrl_machdata
*machdata
, u16 reg
)
97 struct spear_pingroup
*pgroup
;
98 struct spear_modemux
*modemux
;
101 for (group
= 0; group
< machdata
->ngroups
; group
++) {
102 pgroup
= machdata
->groups
[group
];
104 for (i
= 0; i
< pgroup
->nmodemuxs
; i
++) {
105 modemux
= &pgroup
->modemuxs
[i
];
107 for (j
= 0; j
< modemux
->nmuxregs
; j
++)
108 if (modemux
->muxregs
[j
].reg
== 0xFFFF)
109 modemux
->muxregs
[j
].reg
= reg
;
114 static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev
*pctldev
)
116 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
118 return pmx
->machdata
->ngroups
;
121 static const char *spear_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
124 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
126 return pmx
->machdata
->groups
[group
]->name
;
129 static int spear_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
130 unsigned group
, const unsigned **pins
, unsigned *num_pins
)
132 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
134 *pins
= pmx
->machdata
->groups
[group
]->pins
;
135 *num_pins
= pmx
->machdata
->groups
[group
]->npins
;
140 static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
141 struct seq_file
*s
, unsigned offset
)
143 seq_printf(s
, " " DRIVER_NAME
);
146 static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
147 struct device_node
*np_config
,
148 struct pinctrl_map
**map
,
151 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
152 struct device_node
*np
;
153 struct property
*prop
;
154 const char *function
, *group
;
155 int ret
, index
= 0, count
= 0;
157 /* calculate number of maps required */
158 for_each_child_of_node(np_config
, np
) {
159 ret
= of_property_read_string(np
, "st,function", &function
);
163 ret
= of_property_count_strings(np
, "st,pins");
171 dev_err(pmx
->dev
, "No child nodes passed via DT\n");
175 *map
= kzalloc(sizeof(**map
) * count
, GFP_KERNEL
);
179 for_each_child_of_node(np_config
, np
) {
180 of_property_read_string(np
, "st,function", &function
);
181 of_property_for_each_string(np
, "st,pins", prop
, group
) {
182 (*map
)[index
].type
= PIN_MAP_TYPE_MUX_GROUP
;
183 (*map
)[index
].data
.mux
.group
= group
;
184 (*map
)[index
].data
.mux
.function
= function
;
194 static void spear_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
195 struct pinctrl_map
*map
,
201 static const struct pinctrl_ops spear_pinctrl_ops
= {
202 .get_groups_count
= spear_pinctrl_get_groups_cnt
,
203 .get_group_name
= spear_pinctrl_get_group_name
,
204 .get_group_pins
= spear_pinctrl_get_group_pins
,
205 .pin_dbg_show
= spear_pinctrl_pin_dbg_show
,
206 .dt_node_to_map
= spear_pinctrl_dt_node_to_map
,
207 .dt_free_map
= spear_pinctrl_dt_free_map
,
210 static int spear_pinctrl_get_funcs_count(struct pinctrl_dev
*pctldev
)
212 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
214 return pmx
->machdata
->nfunctions
;
217 static const char *spear_pinctrl_get_func_name(struct pinctrl_dev
*pctldev
,
220 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
222 return pmx
->machdata
->functions
[function
]->name
;
225 static int spear_pinctrl_get_func_groups(struct pinctrl_dev
*pctldev
,
226 unsigned function
, const char *const **groups
,
227 unsigned * const ngroups
)
229 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
231 *groups
= pmx
->machdata
->functions
[function
]->groups
;
232 *ngroups
= pmx
->machdata
->functions
[function
]->ngroups
;
237 static int spear_pinctrl_endisable(struct pinctrl_dev
*pctldev
,
238 unsigned function
, unsigned group
, bool enable
)
240 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
241 const struct spear_pingroup
*pgroup
;
242 const struct spear_modemux
*modemux
;
246 pgroup
= pmx
->machdata
->groups
[group
];
248 for (i
= 0; i
< pgroup
->nmodemuxs
; i
++) {
249 modemux
= &pgroup
->modemuxs
[i
];
251 /* SoC have any modes */
252 if (pmx
->machdata
->modes_supported
) {
253 if (!(pmx
->machdata
->mode
& modemux
->modes
))
258 muxregs_endisable(pmx
, modemux
->muxregs
, modemux
->nmuxregs
,
263 dev_err(pmx
->dev
, "pinmux group: %s not supported\n",
271 static int spear_pinctrl_enable(struct pinctrl_dev
*pctldev
, unsigned function
,
274 return spear_pinctrl_endisable(pctldev
, function
, group
, true);
277 static void spear_pinctrl_disable(struct pinctrl_dev
*pctldev
,
278 unsigned function
, unsigned group
)
280 spear_pinctrl_endisable(pctldev
, function
, group
, false);
283 /* gpio with pinmux */
284 static struct spear_gpio_pingroup
*get_gpio_pingroup(struct spear_pmx
*pmx
,
287 struct spear_gpio_pingroup
*gpio_pingroup
;
290 if (!pmx
->machdata
->gpio_pingroups
)
293 for (i
= 0; i
< pmx
->machdata
->ngpio_pingroups
; i
++) {
294 gpio_pingroup
= &pmx
->machdata
->gpio_pingroups
[i
];
296 for (j
= 0; j
< gpio_pingroup
->npins
; j
++) {
297 if (gpio_pingroup
->pins
[j
] == pin
)
298 return gpio_pingroup
;
305 static int gpio_request_endisable(struct pinctrl_dev
*pctldev
,
306 struct pinctrl_gpio_range
*range
, unsigned offset
, bool enable
)
308 struct spear_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
309 struct spear_pinctrl_machdata
*machdata
= pmx
->machdata
;
310 struct spear_gpio_pingroup
*gpio_pingroup
;
313 * Some SoC have configuration options applicable to group of pins,
314 * rather than a single pin.
316 gpio_pingroup
= get_gpio_pingroup(pmx
, offset
);
318 muxregs_endisable(pmx
, gpio_pingroup
->muxregs
,
319 gpio_pingroup
->nmuxregs
, enable
);
322 * SoC may need some extra configurations, or configurations for single
325 if (machdata
->gpio_request_endisable
)
326 machdata
->gpio_request_endisable(pmx
, offset
, enable
);
331 static int gpio_request_enable(struct pinctrl_dev
*pctldev
,
332 struct pinctrl_gpio_range
*range
, unsigned offset
)
334 return gpio_request_endisable(pctldev
, range
, offset
, true);
337 static void gpio_disable_free(struct pinctrl_dev
*pctldev
,
338 struct pinctrl_gpio_range
*range
, unsigned offset
)
340 gpio_request_endisable(pctldev
, range
, offset
, false);
343 static const struct pinmux_ops spear_pinmux_ops
= {
344 .get_functions_count
= spear_pinctrl_get_funcs_count
,
345 .get_function_name
= spear_pinctrl_get_func_name
,
346 .get_function_groups
= spear_pinctrl_get_func_groups
,
347 .enable
= spear_pinctrl_enable
,
348 .disable
= spear_pinctrl_disable
,
349 .gpio_request_enable
= gpio_request_enable
,
350 .gpio_disable_free
= gpio_disable_free
,
353 static struct pinctrl_desc spear_pinctrl_desc
= {
355 .pctlops
= &spear_pinctrl_ops
,
356 .pmxops
= &spear_pinmux_ops
,
357 .owner
= THIS_MODULE
,
360 int spear_pinctrl_probe(struct platform_device
*pdev
,
361 struct spear_pinctrl_machdata
*machdata
)
363 struct device_node
*np
= pdev
->dev
.of_node
;
364 struct resource
*res
;
365 struct spear_pmx
*pmx
;
370 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
374 pmx
= devm_kzalloc(&pdev
->dev
, sizeof(*pmx
), GFP_KERNEL
);
376 dev_err(&pdev
->dev
, "Can't alloc spear_pmx\n");
380 pmx
->vbase
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
382 dev_err(&pdev
->dev
, "Couldn't ioremap at index 0\n");
386 pmx
->dev
= &pdev
->dev
;
387 pmx
->machdata
= machdata
;
389 /* configure mode, if supported by SoC */
390 if (machdata
->modes_supported
) {
393 if (of_property_read_u32(np
, "st,pinmux-mode", &mode
)) {
394 dev_err(&pdev
->dev
, "OF: pinmux mode not passed\n");
398 if (set_mode(pmx
, mode
)) {
399 dev_err(&pdev
->dev
, "OF: Couldn't configure mode: %x\n",
405 platform_set_drvdata(pdev
, pmx
);
407 spear_pinctrl_desc
.pins
= machdata
->pins
;
408 spear_pinctrl_desc
.npins
= machdata
->npins
;
410 pmx
->pctl
= pinctrl_register(&spear_pinctrl_desc
, &pdev
->dev
, pmx
);
412 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
419 int spear_pinctrl_remove(struct platform_device
*pdev
)
421 struct spear_pmx
*pmx
= platform_get_drvdata(pdev
);
423 pinctrl_unregister(pmx
->pctl
);