2 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
3 * Copyright 2017 Linaro Ltd.
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 * published by the Free Software Foundation.
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinconf-generic.h>
16 #include <linux/pinctrl/pinmux.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
21 #include "../pinctrl-utils.h"
22 #include "../pinmux.h"
23 #include "pinctrl-zx.h"
25 #define ZX_PULL_DOWN BIT(0)
26 #define ZX_PULL_UP BIT(1)
27 #define ZX_INPUT_ENABLE BIT(3)
29 #define ZX_DS_MASK (0x7 << ZX_DS_SHIFT)
30 #define ZX_DS_VALUE(x) (((x) << ZX_DS_SHIFT) & ZX_DS_MASK)
31 #define ZX_SLEW BIT(8)
34 struct pinctrl_dev
*pctldev
;
37 void __iomem
*aux_base
;
39 struct zx_pinctrl_soc_info
*info
;
42 static int zx_dt_node_to_map(struct pinctrl_dev
*pctldev
,
43 struct device_node
*np_config
,
44 struct pinctrl_map
**map
, u32
*num_maps
)
46 return pinconf_generic_dt_node_to_map(pctldev
, np_config
, map
,
47 num_maps
, PIN_MAP_TYPE_INVALID
);
50 static const struct pinctrl_ops zx_pinctrl_ops
= {
51 .dt_node_to_map
= zx_dt_node_to_map
,
52 .dt_free_map
= pinctrl_utils_free_map
,
53 .get_groups_count
= pinctrl_generic_get_group_count
,
54 .get_group_name
= pinctrl_generic_get_group_name
,
55 .get_group_pins
= pinctrl_generic_get_group_pins
,
60 static int zx_set_mux(struct pinctrl_dev
*pctldev
, unsigned int func_selector
,
61 unsigned int group_selector
)
63 struct zx_pinctrl
*zpctl
= pinctrl_dev_get_drvdata(pctldev
);
64 struct zx_pinctrl_soc_info
*info
= zpctl
->info
;
65 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ group_selector
;
66 struct zx_pin_data
*data
= pindesc
->drv_data
;
67 struct zx_mux_desc
*mux
;
68 u32 mask
, offset
, bitpos
;
69 struct function_desc
*func
;
73 /* Skip reserved pin */
78 mask
= (1 << data
->width
) - 1;
79 offset
= data
->offset
;
80 bitpos
= data
->bitpos
;
82 func
= pinmux_generic_get_function(pctldev
, func_selector
);
87 if (strcmp(mux
->name
, func
->name
) == 0)
92 /* Found mux value to be written */
95 spin_lock_irqsave(&zpctl
->lock
, flags
);
99 * It's an AON pin, whose mux register offset and bit position
100 * can be caluculated from pin number. Each register covers 16
101 * pins, and each pin occupies 2 bits.
103 u16 aoffset
= pindesc
->number
/ 16 * 4;
104 u16 abitpos
= (pindesc
->number
% 16) * 2;
106 if (mval
& AON_MUX_FLAG
) {
108 * This is a mux value that needs to be written into
109 * AON pinmux register. Write it and then we're done.
111 val
= readl(zpctl
->aux_base
+ aoffset
);
112 val
&= ~(0x3 << abitpos
);
113 val
|= (mval
& 0x3) << abitpos
;
114 writel(val
, zpctl
->aux_base
+ aoffset
);
117 * It's a mux value that needs to be written into TOP
120 val
= readl(zpctl
->base
+ offset
);
121 val
&= ~(mask
<< bitpos
);
122 val
|= (mval
& mask
) << bitpos
;
123 writel(val
, zpctl
->base
+ offset
);
126 * In this case, the AON pinmux register needs to be
127 * set up to select non-AON function.
129 val
= readl(zpctl
->aux_base
+ aoffset
);
130 val
&= ~(0x3 << abitpos
);
131 val
|= NONAON_MVAL
<< abitpos
;
132 writel(val
, zpctl
->aux_base
+ aoffset
);
137 * This is a TOP pin, and we only need to set up TOP pinmux
138 * register and then we're done with it.
140 val
= readl(zpctl
->base
+ offset
);
141 val
&= ~(mask
<< bitpos
);
142 val
|= (mval
& mask
) << bitpos
;
143 writel(val
, zpctl
->base
+ offset
);
146 spin_unlock_irqrestore(&zpctl
->lock
, flags
);
151 static const struct pinmux_ops zx_pinmux_ops
= {
152 .get_functions_count
= pinmux_generic_get_function_count
,
153 .get_function_name
= pinmux_generic_get_function_name
,
154 .get_function_groups
= pinmux_generic_get_function_groups
,
155 .set_mux
= zx_set_mux
,
158 static int zx_pin_config_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
159 unsigned long *config
)
161 struct zx_pinctrl
*zpctl
= pinctrl_dev_get_drvdata(pctldev
);
162 struct zx_pinctrl_soc_info
*info
= zpctl
->info
;
163 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ pin
;
164 struct zx_pin_data
*data
= pindesc
->drv_data
;
165 enum pin_config_param param
= pinconf_to_config_param(*config
);
168 /* Skip reserved pin */
172 val
= readl(zpctl
->aux_base
+ data
->coffset
);
173 val
= val
>> data
->cbitpos
;
176 case PIN_CONFIG_BIAS_PULL_DOWN
:
182 case PIN_CONFIG_BIAS_PULL_UP
:
188 case PIN_CONFIG_INPUT_ENABLE
:
189 val
&= ZX_INPUT_ENABLE
;
194 case PIN_CONFIG_DRIVE_STRENGTH
:
196 val
= val
>> ZX_DS_SHIFT
;
198 case PIN_CONFIG_SLEW_RATE
:
206 *config
= pinconf_to_config_packed(param
, val
);
211 static int zx_pin_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
212 unsigned long *configs
, unsigned int num_configs
)
214 struct zx_pinctrl
*zpctl
= pinctrl_dev_get_drvdata(pctldev
);
215 struct zx_pinctrl_soc_info
*info
= zpctl
->info
;
216 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ pin
;
217 struct zx_pin_data
*data
= pindesc
->drv_data
;
218 enum pin_config_param param
;
222 /* Skip reserved pin */
226 val
= readl(zpctl
->aux_base
+ data
->coffset
);
228 for (i
= 0; i
< num_configs
; i
++) {
229 param
= pinconf_to_config_param(configs
[i
]);
230 arg
= pinconf_to_config_argument(configs
[i
]);
233 case PIN_CONFIG_BIAS_PULL_DOWN
:
234 val
|= ZX_PULL_DOWN
<< data
->cbitpos
;
236 case PIN_CONFIG_BIAS_PULL_UP
:
237 val
|= ZX_PULL_UP
<< data
->cbitpos
;
239 case PIN_CONFIG_INPUT_ENABLE
:
240 val
|= ZX_INPUT_ENABLE
<< data
->cbitpos
;
242 case PIN_CONFIG_DRIVE_STRENGTH
:
243 val
&= ~(ZX_DS_MASK
<< data
->cbitpos
);
244 val
|= ZX_DS_VALUE(arg
) << data
->cbitpos
;
246 case PIN_CONFIG_SLEW_RATE
:
248 val
|= ZX_SLEW
<< data
->cbitpos
;
250 val
&= ~ZX_SLEW
<< data
->cbitpos
;
257 writel(val
, zpctl
->aux_base
+ data
->coffset
);
261 static const struct pinconf_ops zx_pinconf_ops
= {
262 .pin_config_set
= zx_pin_config_set
,
263 .pin_config_get
= zx_pin_config_get
,
267 static int zx_pinctrl_build_state(struct platform_device
*pdev
)
269 struct zx_pinctrl
*zpctl
= platform_get_drvdata(pdev
);
270 struct zx_pinctrl_soc_info
*info
= zpctl
->info
;
271 struct pinctrl_dev
*pctldev
= zpctl
->pctldev
;
272 struct function_desc
*functions
;
274 struct group_desc
*groups
;
278 /* Every single pin composes a group */
279 ngroups
= info
->npins
;
280 groups
= devm_kzalloc(&pdev
->dev
, ngroups
* sizeof(*groups
),
285 for (i
= 0; i
< ngroups
; i
++) {
286 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ i
;
287 struct group_desc
*group
= groups
+ i
;
289 group
->name
= pindesc
->name
;
290 group
->pins
= (int *) &pindesc
->number
;
292 radix_tree_insert(&pctldev
->pin_group_tree
, i
, group
);
295 pctldev
->num_groups
= ngroups
;
297 /* Build function list from pin mux functions */
298 functions
= kcalloc(info
->npins
, sizeof(*functions
), GFP_KERNEL
);
303 for (i
= 0; i
< info
->npins
; i
++) {
304 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ i
;
305 struct zx_pin_data
*data
= pindesc
->drv_data
;
306 struct zx_mux_desc
*mux
;
308 /* Reserved pins do not have a drv_data at all */
312 /* Loop over all muxes for the pin */
315 struct function_desc
*func
= functions
;
317 /* Search function list for given mux */
319 if (strcmp(mux
->name
, func
->name
) == 0) {
320 /* Function exists */
321 func
->num_group_names
++;
329 func
->name
= mux
->name
;
330 func
->num_group_names
= 1;
331 radix_tree_insert(&pctldev
->pin_function_tree
,
339 pctldev
->num_functions
= nfunctions
;
340 functions
= krealloc(functions
, nfunctions
* sizeof(*functions
),
343 /* Find pin groups for every single function */
344 for (i
= 0; i
< info
->npins
; i
++) {
345 const struct pinctrl_pin_desc
*pindesc
= info
->pins
+ i
;
346 struct zx_pin_data
*data
= pindesc
->drv_data
;
347 struct zx_mux_desc
*mux
;
354 struct function_desc
*func
;
358 /* Find function for given mux */
359 for (j
= 0; j
< nfunctions
; j
++)
360 if (strcmp(functions
[j
].name
, mux
->name
) == 0)
363 func
= functions
+ j
;
364 if (!func
->group_names
) {
365 func
->group_names
= devm_kzalloc(&pdev
->dev
,
366 func
->num_group_names
*
367 sizeof(*func
->group_names
),
369 if (!func
->group_names
) {
375 group
= func
->group_names
;
378 *group
= pindesc
->name
;
387 int zx_pinctrl_init(struct platform_device
*pdev
,
388 struct zx_pinctrl_soc_info
*info
)
390 struct pinctrl_desc
*pctldesc
;
391 struct zx_pinctrl
*zpctl
;
392 struct device_node
*np
;
393 struct resource
*res
;
396 zpctl
= devm_kzalloc(&pdev
->dev
, sizeof(*zpctl
), GFP_KERNEL
);
400 spin_lock_init(&zpctl
->lock
);
402 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
403 zpctl
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
404 if (IS_ERR(zpctl
->base
))
405 return PTR_ERR(zpctl
->base
);
407 np
= of_parse_phandle(pdev
->dev
.of_node
, "zte,auxiliary-controller", 0);
409 dev_err(&pdev
->dev
, "failed to find auxiliary controller\n");
413 zpctl
->aux_base
= of_iomap(np
, 0);
414 if (!zpctl
->aux_base
)
417 zpctl
->dev
= &pdev
->dev
;
420 pctldesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pctldesc
), GFP_KERNEL
);
424 pctldesc
->name
= dev_name(&pdev
->dev
);
425 pctldesc
->owner
= THIS_MODULE
;
426 pctldesc
->pins
= info
->pins
;
427 pctldesc
->npins
= info
->npins
;
428 pctldesc
->pctlops
= &zx_pinctrl_ops
;
429 pctldesc
->pmxops
= &zx_pinmux_ops
;
430 pctldesc
->confops
= &zx_pinconf_ops
;
432 zpctl
->pctldev
= devm_pinctrl_register(&pdev
->dev
, pctldesc
, zpctl
);
433 if (IS_ERR(zpctl
->pctldev
)) {
434 ret
= PTR_ERR(zpctl
->pctldev
);
435 dev_err(&pdev
->dev
, "failed to register pinctrl: %d\n", ret
);
439 platform_set_drvdata(pdev
, zpctl
);
441 ret
= zx_pinctrl_build_state(pdev
);
443 dev_err(&pdev
->dev
, "failed to build state: %d\n", ret
);
447 dev_info(&pdev
->dev
, "initialized pinctrl driver\n");