1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Realtek DHC pin controller driver
5 * Copyright (c) 2023 Realtek Semiconductor Corp.
8 #include <linux/bitops.h>
10 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/pinctrl/machine.h>
14 #include <linux/pinctrl/pinconf.h>
15 #include <linux/pinctrl/pinconf-generic.h>
16 #include <linux/pinctrl/pinctrl.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/platform_device.h>
19 #include <linux/seq_file.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
23 #include "../pinctrl-utils.h"
24 #include "pinctrl-rtd.h"
28 struct pinctrl_dev
*pcdev
;
30 struct pinctrl_desc desc
;
31 const struct rtd_pinctrl_desc
*info
;
32 struct regmap
*regmap_pinctrl
;
35 /* custom pinconf parameters */
36 #define RTD_DRIVE_STRENGH_P (PIN_CONFIG_END + 1)
37 #define RTD_DRIVE_STRENGH_N (PIN_CONFIG_END + 2)
38 #define RTD_DUTY_CYCLE (PIN_CONFIG_END + 3)
40 static const struct pinconf_generic_params rtd_custom_bindings
[] = {
41 {"realtek,drive-strength-p", RTD_DRIVE_STRENGH_P
, 0},
42 {"realtek,drive-strength-n", RTD_DRIVE_STRENGH_N
, 0},
43 {"realtek,duty-cycle", RTD_DUTY_CYCLE
, 0},
46 static int rtd_pinctrl_get_groups_count(struct pinctrl_dev
*pcdev
)
48 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
50 return data
->info
->num_groups
;
53 static const char *rtd_pinctrl_get_group_name(struct pinctrl_dev
*pcdev
,
54 unsigned int selector
)
56 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
58 return data
->info
->groups
[selector
].name
;
61 static int rtd_pinctrl_get_group_pins(struct pinctrl_dev
*pcdev
,
62 unsigned int selector
,
63 const unsigned int **pins
,
64 unsigned int *num_pins
)
66 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
68 *pins
= data
->info
->groups
[selector
].pins
;
69 *num_pins
= data
->info
->groups
[selector
].num_pins
;
74 static void rtd_pinctrl_dbg_show(struct pinctrl_dev
*pcdev
,
78 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
79 const struct rtd_pin_desc
*mux
= &data
->info
->muxes
[offset
];
80 const struct rtd_pin_mux_desc
*func
;
87 seq_puts(s
, "[not defined]");
90 val
= readl_relaxed(data
->base
+ mux
->mux_offset
);
95 func
= &mux
->functions
[0];
96 seq_puts(s
, "function: ");
98 if (func
->mux_value
== pin_val
) {
100 seq_printf(s
, "[%s] ", func
->name
);
102 seq_printf(s
, "%s ", func
->name
);
107 seq_puts(s
, "[not defined]");
110 static const struct pinctrl_ops rtd_pinctrl_ops
= {
111 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
112 .dt_free_map
= pinctrl_utils_free_map
,
113 .get_groups_count
= rtd_pinctrl_get_groups_count
,
114 .get_group_name
= rtd_pinctrl_get_group_name
,
115 .get_group_pins
= rtd_pinctrl_get_group_pins
,
116 .pin_dbg_show
= rtd_pinctrl_dbg_show
,
119 static int rtd_pinctrl_get_functions_count(struct pinctrl_dev
*pcdev
)
121 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
123 return data
->info
->num_functions
;
126 static const char *rtd_pinctrl_get_function_name(struct pinctrl_dev
*pcdev
,
127 unsigned int selector
)
129 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
131 return data
->info
->functions
[selector
].name
;
134 static int rtd_pinctrl_get_function_groups(struct pinctrl_dev
*pcdev
,
135 unsigned int selector
,
136 const char * const **groups
,
137 unsigned int * const num_groups
)
139 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
141 *groups
= data
->info
->functions
[selector
].groups
;
142 *num_groups
= data
->info
->functions
[selector
].num_groups
;
147 static const struct rtd_pin_desc
*rtd_pinctrl_find_mux(struct rtd_pinctrl
*data
, unsigned int pin
)
149 if (data
->info
->muxes
[pin
].name
)
150 return &data
->info
->muxes
[pin
];
155 static int rtd_pinctrl_set_one_mux(struct pinctrl_dev
*pcdev
,
156 unsigned int pin
, const char *func_name
)
158 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
159 const struct rtd_pin_desc
*mux
;
163 mux
= rtd_pinctrl_find_mux(data
, pin
);
167 if (!mux
->functions
) {
169 dev_err(pcdev
->dev
, "NULL pin has no functions\n");
171 dev_err(pcdev
->dev
, "No functions available for pin %s\n", mux
->name
);
175 for (i
= 0; mux
->functions
[i
].name
; i
++) {
176 if (strcmp(mux
->functions
[i
].name
, func_name
) != 0)
178 ret
= regmap_update_bits(data
->regmap_pinctrl
, mux
->mux_offset
, mux
->mux_mask
,
179 mux
->functions
[i
].mux_value
);
184 dev_err(pcdev
->dev
, "NULL pin provided for function %s\n", func_name
);
188 dev_err(pcdev
->dev
, "No function %s available for pin %s\n", func_name
, mux
->name
);
193 static int rtd_pinctrl_set_mux(struct pinctrl_dev
*pcdev
,
194 unsigned int function
, unsigned int group
)
196 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
197 const unsigned int *pins
;
198 unsigned int num_pins
;
199 const char *func_name
;
200 const char *group_name
;
203 func_name
= data
->info
->functions
[function
].name
;
204 group_name
= data
->info
->groups
[group
].name
;
206 ret
= rtd_pinctrl_get_group_pins(pcdev
, group
, &pins
, &num_pins
);
208 dev_err(pcdev
->dev
, "Getting pins for group %s failed\n", group_name
);
212 for (i
= 0; i
< num_pins
; i
++) {
213 ret
= rtd_pinctrl_set_one_mux(pcdev
, pins
[i
], func_name
);
221 static int rtd_pinctrl_gpio_request_enable(struct pinctrl_dev
*pcdev
,
222 struct pinctrl_gpio_range
*range
,
225 return rtd_pinctrl_set_one_mux(pcdev
, offset
, "gpio");
228 static const struct pinmux_ops rtd_pinmux_ops
= {
229 .get_functions_count
= rtd_pinctrl_get_functions_count
,
230 .get_function_name
= rtd_pinctrl_get_function_name
,
231 .get_function_groups
= rtd_pinctrl_get_function_groups
,
232 .set_mux
= rtd_pinctrl_set_mux
,
233 .gpio_request_enable
= rtd_pinctrl_gpio_request_enable
,
236 static const struct pinctrl_pin_desc
237 *rtd_pinctrl_get_pin_by_number(struct rtd_pinctrl
*data
, int number
)
241 for (i
= 0; i
< data
->info
->num_pins
; i
++) {
242 if (data
->info
->pins
[i
].number
== number
)
243 return &data
->info
->pins
[i
];
249 static const struct rtd_pin_config_desc
250 *rtd_pinctrl_find_config(struct rtd_pinctrl
*data
, unsigned int pin
)
252 if (data
->info
->configs
[pin
].name
)
253 return &data
->info
->configs
[pin
];
258 static const struct rtd_pin_sconfig_desc
*rtd_pinctrl_find_sconfig(struct rtd_pinctrl
*data
,
262 const struct pinctrl_pin_desc
*pin_desc
;
263 const char *pin_name
;
265 pin_desc
= rtd_pinctrl_get_pin_by_number(data
, pin
);
269 pin_name
= pin_desc
->name
;
271 for (i
= 0; i
< data
->info
->num_sconfigs
; i
++) {
272 if (strcmp(data
->info
->sconfigs
[i
].name
, pin_name
) == 0)
273 return &data
->info
->sconfigs
[i
];
279 static int rtd_pconf_parse_conf(struct rtd_pinctrl
*data
,
281 enum pin_config_param param
,
282 enum pin_config_param arg
)
284 const struct rtd_pin_config_desc
*config_desc
;
285 const struct rtd_pin_sconfig_desc
*sconfig_desc
;
290 u32 pulsel_off
, pulen_off
, smt_off
, curr_off
, pow_off
, reg_off
, p_off
, n_off
;
291 const char *name
= data
->info
->pins
[pinnr
].name
;
294 config_desc
= rtd_pinctrl_find_config(data
, pinnr
);
296 dev_err(data
->dev
, "Not support pin config for pin: %s\n", name
);
299 switch ((u32
)param
) {
300 case PIN_CONFIG_INPUT_SCHMITT
:
301 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
302 if (config_desc
->smt_offset
== NA
) {
303 dev_err(data
->dev
, "Not support input schmitt for pin: %s\n", name
);
306 smt_off
= config_desc
->base_bit
+ config_desc
->smt_offset
;
307 reg_off
= config_desc
->reg_offset
;
311 val
= set_val
? BIT(smt_off
) : 0;
314 case PIN_CONFIG_DRIVE_PUSH_PULL
:
315 if (config_desc
->pud_en_offset
== NA
) {
316 dev_err(data
->dev
, "Not support push pull for pin: %s\n", name
);
319 pulen_off
= config_desc
->base_bit
+ config_desc
->pud_en_offset
;
320 reg_off
= config_desc
->reg_offset
;
322 mask
= BIT(pulen_off
);
326 case PIN_CONFIG_BIAS_DISABLE
:
327 if (config_desc
->pud_en_offset
== NA
) {
328 dev_err(data
->dev
, "Not support bias disable for pin: %s\n", name
);
331 pulen_off
= config_desc
->base_bit
+ config_desc
->pud_en_offset
;
332 reg_off
= config_desc
->reg_offset
;
334 mask
= BIT(pulen_off
);
338 case PIN_CONFIG_BIAS_PULL_UP
:
339 if (config_desc
->pud_en_offset
== NA
) {
340 dev_err(data
->dev
, "Not support bias pull up for pin:%s\n", name
);
343 pulen_off
= config_desc
->base_bit
+ config_desc
->pud_en_offset
;
344 pulsel_off
= config_desc
->base_bit
+ config_desc
->pud_sel_offset
;
345 reg_off
= config_desc
->reg_offset
;
347 mask
= BIT(pulen_off
) | BIT(pulsel_off
);
351 case PIN_CONFIG_BIAS_PULL_DOWN
:
352 if (config_desc
->pud_en_offset
== NA
) {
353 dev_err(data
->dev
, "Not support bias pull down for pin: %s\n", name
);
356 pulen_off
= config_desc
->base_bit
+ config_desc
->pud_en_offset
;
357 pulsel_off
= config_desc
->base_bit
+ config_desc
->pud_sel_offset
;
358 reg_off
= config_desc
->reg_offset
;
360 mask
= BIT(pulen_off
) | BIT(pulsel_off
);
361 val
= BIT(pulen_off
);
364 case PIN_CONFIG_DRIVE_STRENGTH
:
365 curr_off
= config_desc
->base_bit
+ config_desc
->curr_offset
;
366 reg_off
= config_desc
->reg_offset
;
369 switch (config_desc
->curr_type
) {
373 else if (strength
== 8)
381 else if (strength
== 4)
387 dev_err(data
->dev
, "Not support drive strength for pin: %s\n", name
);
392 mask
= BIT(curr_off
);
395 case PIN_CONFIG_POWER_SOURCE
:
396 if (config_desc
->power_offset
== NA
) {
397 dev_err(data
->dev
, "Not support power source for pin: %s\n", name
);
400 reg_off
= config_desc
->reg_offset
;
401 pow_off
= config_desc
->base_bit
+ config_desc
->power_offset
;
408 val
= set_val
? mask
: 0;
411 case RTD_DRIVE_STRENGH_P
:
412 sconfig_desc
= rtd_pinctrl_find_sconfig(data
, pinnr
);
414 dev_err(data
->dev
, "Not support P driving for pin: %s\n", name
);
418 reg_off
= sconfig_desc
->reg_offset
;
419 p_off
= sconfig_desc
->pdrive_offset
;
424 mask
= GENMASK(p_off
+ sconfig_desc
->pdrive_maskbits
- 1, p_off
);
425 val
= set_val
<< p_off
;
428 case RTD_DRIVE_STRENGH_N
:
429 sconfig_desc
= rtd_pinctrl_find_sconfig(data
, pinnr
);
431 dev_err(data
->dev
, "Not support N driving for pin: %s\n", name
);
435 reg_off
= sconfig_desc
->reg_offset
;
436 n_off
= sconfig_desc
->ndrive_offset
;
441 mask
= GENMASK(n_off
+ sconfig_desc
->ndrive_maskbits
- 1, n_off
);
442 val
= set_val
<< n_off
;
446 sconfig_desc
= rtd_pinctrl_find_sconfig(data
, pinnr
);
447 if (!sconfig_desc
|| sconfig_desc
->dcycle_offset
== NA
) {
448 dev_err(data
->dev
, "Not support duty cycle for pin: %s\n", name
);
452 reg_off
= config_desc
->reg_offset
;
453 mask
= GENMASK(sconfig_desc
->dcycle_offset
+
454 sconfig_desc
->dcycle_maskbits
- 1, sconfig_desc
->dcycle_offset
);
455 val
= set_val
<< sconfig_desc
->dcycle_offset
;
459 dev_err(data
->dev
, "unsupported pinconf: %d\n", (u32
)param
);
463 ret
= regmap_update_bits(data
->regmap_pinctrl
, reg_off
, mask
, val
);
465 dev_err(data
->dev
, "could not update pinconf(%d) for pin(%s)\n", (u32
)param
, name
);
470 static int rtd_pin_config_get(struct pinctrl_dev
*pcdev
, unsigned int pinnr
,
471 unsigned long *config
)
473 unsigned int param
= pinconf_to_config_param(*config
);
474 unsigned int arg
= 0;
481 *config
= pinconf_to_config_packed(param
, arg
);
485 static int rtd_pin_config_set(struct pinctrl_dev
*pcdev
, unsigned int pinnr
,
486 unsigned long *configs
, unsigned int num_configs
)
488 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
492 for (i
= 0; i
< num_configs
; i
++) {
493 ret
= rtd_pconf_parse_conf(data
, pinnr
,
494 pinconf_to_config_param(configs
[i
]),
495 pinconf_to_config_argument(configs
[i
]));
503 static int rtd_pin_config_group_set(struct pinctrl_dev
*pcdev
, unsigned int group
,
504 unsigned long *configs
, unsigned int num_configs
)
506 struct rtd_pinctrl
*data
= pinctrl_dev_get_drvdata(pcdev
);
507 const unsigned int *pins
;
508 unsigned int num_pins
;
509 const char *group_name
;
512 group_name
= data
->info
->groups
[group
].name
;
514 ret
= rtd_pinctrl_get_group_pins(pcdev
, group
, &pins
, &num_pins
);
516 dev_err(pcdev
->dev
, "Getting pins for group %s failed\n", group_name
);
520 for (i
= 0; i
< num_pins
; i
++) {
521 ret
= rtd_pin_config_set(pcdev
, pins
[i
], configs
, num_configs
);
529 static const struct pinconf_ops rtd_pinconf_ops
= {
531 .pin_config_get
= rtd_pin_config_get
,
532 .pin_config_set
= rtd_pin_config_set
,
533 .pin_config_group_set
= rtd_pin_config_group_set
,
536 static const struct regmap_config rtd_pinctrl_regmap_config
= {
540 .use_relaxed_mmio
= true,
543 int rtd_pinctrl_probe(struct platform_device
*pdev
, const struct rtd_pinctrl_desc
*desc
)
545 struct rtd_pinctrl
*data
;
548 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
552 data
->base
= of_iomap(pdev
->dev
.of_node
, 0);
556 data
->dev
= &pdev
->dev
;
558 data
->desc
.name
= dev_name(&pdev
->dev
);
559 data
->desc
.pins
= data
->info
->pins
;
560 data
->desc
.npins
= data
->info
->num_pins
;
561 data
->desc
.pctlops
= &rtd_pinctrl_ops
;
562 data
->desc
.pmxops
= &rtd_pinmux_ops
;
563 data
->desc
.confops
= &rtd_pinconf_ops
;
564 data
->desc
.custom_params
= rtd_custom_bindings
;
565 data
->desc
.num_custom_params
= ARRAY_SIZE(rtd_custom_bindings
);
566 data
->desc
.owner
= THIS_MODULE
;
567 data
->regmap_pinctrl
= devm_regmap_init_mmio(data
->dev
, data
->base
,
568 &rtd_pinctrl_regmap_config
);
570 if (IS_ERR(data
->regmap_pinctrl
)) {
571 dev_err(data
->dev
, "failed to init regmap: %ld\n",
572 PTR_ERR(data
->regmap_pinctrl
));
573 ret
= PTR_ERR(data
->regmap_pinctrl
);
577 data
->pcdev
= pinctrl_register(&data
->desc
, &pdev
->dev
, data
);
578 if (IS_ERR(data
->pcdev
)) {
579 ret
= PTR_ERR(data
->pcdev
);
583 platform_set_drvdata(pdev
, data
);
585 dev_dbg(&pdev
->dev
, "probed\n");
593 EXPORT_SYMBOL(rtd_pinctrl_probe
);
595 MODULE_DESCRIPTION("Realtek DHC SoC pinctrl driver");
596 MODULE_LICENSE("GPL v2");