1 // SPDX-License-Identifier: GPL-2.0
3 * Sophgo CV18XX SoCs pinctrl driver.
5 * Copyright (C) 2024 Inochi Amaoto <inochiama@outlook.com>
9 #include <linux/bitfield.h>
10 #include <linux/export.h>
13 #include <linux/platform_device.h>
14 #include <linux/bsearch.h>
15 #include <linux/seq_file.h>
16 #include <linux/spinlock.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
25 #include <dt-bindings/pinctrl/pinctrl-cv18xx.h>
28 #include "../pinctrl-utils.h"
29 #include "../pinconf.h"
30 #include "../pinmux.h"
31 #include "pinctrl-cv18xx.h"
33 struct cv1800_pinctrl
{
35 struct pinctrl_dev
*pctl_dev
;
36 const struct cv1800_pinctrl_data
*data
;
37 struct pinctrl_desc pdesc
;
43 void __iomem
*regs
[2];
46 struct cv1800_pin_mux_config
{
47 struct cv1800_pin
*pin
;
51 static unsigned int cv1800_dt_get_pin(u32 value
)
53 return value
& GENMASK(15, 0);
56 static unsigned int cv1800_dt_get_pin_mux(u32 value
)
58 return (value
>> 16) & GENMASK(7, 0);
61 static unsigned int cv1800_dt_get_pin_mux2(u32 value
)
63 return (value
>> 24) & GENMASK(7, 0);
66 #define cv1800_pinctrl_get_component_addr(pctrl, _comp) \
67 ((pctrl)->regs[(_comp)->area] + (_comp)->offset)
69 static int cv1800_cmp_pin(const void *key
, const void *pivot
)
71 const struct cv1800_pin
*pin
= pivot
;
72 int pin_id
= (long)key
;
75 return pin_id
- pivid
;
78 static int cv1800_set_power_cfg(struct cv1800_pinctrl
*pctrl
,
81 if (domain
>= pctrl
->data
->npd
)
84 if (pctrl
->power_cfg
[domain
] && pctrl
->power_cfg
[domain
] != cfg
)
87 pctrl
->power_cfg
[domain
] = cfg
;
92 static int cv1800_get_power_cfg(struct cv1800_pinctrl
*pctrl
,
95 return pctrl
->power_cfg
[domain
];
98 static struct cv1800_pin
*cv1800_get_pin(struct cv1800_pinctrl
*pctrl
,
101 return bsearch((void *)pin
, pctrl
->data
->pindata
, pctrl
->data
->npins
,
102 sizeof(struct cv1800_pin
), cv1800_cmp_pin
);
105 #define PIN_BGA_ID_OFFSET 8
106 #define PIN_BGA_ID_MASK 0xff
108 static const char *const io_type_desc
[] = {
115 static const char *cv1800_get_power_cfg_desc(struct cv1800_pinctrl
*pctrl
,
118 return pctrl
->data
->pdnames
[domain
];
121 static void cv1800_pctrl_dbg_show(struct pinctrl_dev
*pctldev
,
122 struct seq_file
*seq
, unsigned int pin_id
)
124 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
125 struct cv1800_pin
*pin
= cv1800_get_pin(pctrl
, pin_id
);
126 enum cv1800_pin_io_type type
= cv1800_pin_io_type(pin
);
130 if (pin
->pin
>> PIN_BGA_ID_OFFSET
)
131 seq_printf(seq
, "pos: %c%u ",
132 'A' + (pin
->pin
>> PIN_BGA_ID_OFFSET
) - 1,
133 pin
->pin
& PIN_BGA_ID_MASK
);
135 seq_printf(seq
, "pos: %u ", pin
->pin
);
137 seq_printf(seq
, "power-domain: %s ",
138 cv1800_get_power_cfg_desc(pctrl
, pin
->power_domain
));
139 seq_printf(seq
, "type: %s ", io_type_desc
[type
]);
141 reg
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->mux
);
143 seq_printf(seq
, "mux: 0x%08x ", value
);
145 if (pin
->flags
& CV1800_PIN_HAVE_MUX2
) {
146 reg
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->mux2
);
148 seq_printf(seq
, "mux2: 0x%08x ", value
);
151 if (type
== IO_TYPE_1V8_ONLY
|| type
== IO_TYPE_1V8_OR_3V3
) {
152 reg
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->conf
);
154 seq_printf(seq
, "conf: 0x%08x ", value
);
158 static int cv1800_verify_pinmux_config(const struct cv1800_pin_mux_config
*config
)
160 unsigned int mux
= cv1800_dt_get_pin_mux(config
->config
);
161 unsigned int mux2
= cv1800_dt_get_pin_mux2(config
->config
);
163 if (mux
> config
->pin
->mux
.max
)
166 if (config
->pin
->flags
& CV1800_PIN_HAVE_MUX2
) {
167 if (mux
!= config
->pin
->mux2
.pfunc
)
170 if (mux2
> config
->pin
->mux2
.max
)
173 if (mux2
!= PIN_MUX_INVALD
)
180 static int cv1800_verify_pin_group(const struct cv1800_pin_mux_config
*mux
,
183 enum cv1800_pin_io_type type
;
190 type
= cv1800_pin_io_type(mux
[0].pin
);
191 power_domain
= mux
[0].pin
->power_domain
;
193 for (i
= 0; i
< npins
; i
++) {
194 if (type
!= cv1800_pin_io_type(mux
[i
].pin
) ||
195 power_domain
!= mux
[i
].pin
->power_domain
)
202 static int cv1800_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
203 struct device_node
*np
,
204 struct pinctrl_map
**maps
,
205 unsigned int *num_maps
)
207 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
208 struct device
*dev
= pctrl
->dev
;
209 struct device_node
*child
;
210 struct pinctrl_map
*map
;
211 const char **grpnames
;
217 for_each_available_child_of_node(np
, child
)
220 grpnames
= devm_kcalloc(dev
, ngroups
, sizeof(*grpnames
), GFP_KERNEL
);
224 map
= kcalloc(ngroups
* 2, sizeof(*map
), GFP_KERNEL
);
229 mutex_lock(&pctrl
->mutex
);
230 for_each_available_child_of_node(np
, child
) {
231 int npins
= of_property_count_u32_elems(child
, "pinmux");
233 struct cv1800_pin_mux_config
*pinmuxs
;
238 dev_err(dev
, "invalid pinctrl group %pOFn.%pOFn\n",
244 grpname
= devm_kasprintf(dev
, GFP_KERNEL
, "%pOFn.%pOFn",
251 grpnames
[ngroups
++] = grpname
;
253 pins
= devm_kcalloc(dev
, npins
, sizeof(*pins
), GFP_KERNEL
);
259 pinmuxs
= devm_kcalloc(dev
, npins
, sizeof(*pinmuxs
), GFP_KERNEL
);
265 for (i
= 0; i
< npins
; i
++) {
266 ret
= of_property_read_u32_index(child
, "pinmux",
271 pins
[i
] = cv1800_dt_get_pin(config
);
272 pinmuxs
[i
].config
= config
;
273 pinmuxs
[i
].pin
= cv1800_get_pin(pctrl
, pins
[i
]);
275 if (!pinmuxs
[i
].pin
) {
276 dev_err(dev
, "failed to get pin %d\n", pins
[i
]);
281 ret
= cv1800_verify_pinmux_config(&pinmuxs
[i
]);
283 dev_err(dev
, "group %s pin %d is invalid\n",
289 ret
= cv1800_verify_pin_group(pinmuxs
, npins
);
291 dev_err(dev
, "group %s is invalid\n", grpname
);
295 ret
= of_property_read_u32(child
, "power-source", &power
);
299 if (!(power
== PIN_POWER_STATE_3V3
|| power
== PIN_POWER_STATE_1V8
)) {
300 dev_err(dev
, "group %s have unsupported power: %u\n",
306 ret
= cv1800_set_power_cfg(pctrl
, pinmuxs
[0].pin
->power_domain
,
311 map
[nmaps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
312 map
[nmaps
].data
.mux
.function
= np
->name
;
313 map
[nmaps
].data
.mux
.group
= grpname
;
316 ret
= pinconf_generic_parse_dt_config(child
, pctldev
,
317 &map
[nmaps
].data
.configs
.configs
,
318 &map
[nmaps
].data
.configs
.num_configs
);
320 dev_err(dev
, "failed to parse pin config of group %s: %d\n",
325 ret
= pinctrl_generic_add_group(pctldev
, grpname
,
326 pins
, npins
, pinmuxs
);
328 dev_err(dev
, "failed to add group %s: %d\n", grpname
, ret
);
332 /* don't create a map if there are no pinconf settings */
333 if (map
[nmaps
].data
.configs
.num_configs
== 0)
336 map
[nmaps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
337 map
[nmaps
].data
.configs
.group_or_pin
= grpname
;
341 ret
= pinmux_generic_add_function(pctldev
, np
->name
,
342 grpnames
, ngroups
, NULL
);
344 dev_err(dev
, "error adding function %s: %d\n", np
->name
, ret
);
345 goto function_failed
;
350 mutex_unlock(&pctrl
->mutex
);
357 pinctrl_utils_free_map(pctldev
, map
, nmaps
);
358 mutex_unlock(&pctrl
->mutex
);
362 static const struct pinctrl_ops cv1800_pctrl_ops
= {
363 .get_groups_count
= pinctrl_generic_get_group_count
,
364 .get_group_name
= pinctrl_generic_get_group_name
,
365 .get_group_pins
= pinctrl_generic_get_group_pins
,
366 .pin_dbg_show
= cv1800_pctrl_dbg_show
,
367 .dt_node_to_map
= cv1800_pctrl_dt_node_to_map
,
368 .dt_free_map
= pinctrl_utils_free_map
,
371 static int cv1800_pmx_set_mux(struct pinctrl_dev
*pctldev
,
372 unsigned int fsel
, unsigned int gsel
)
374 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
375 const struct group_desc
*group
;
376 const struct cv1800_pin_mux_config
*configs
;
379 group
= pinctrl_generic_get_group(pctldev
, gsel
);
383 configs
= group
->data
;
385 for (i
= 0; i
< group
->grp
.npins
; i
++) {
386 const struct cv1800_pin
*pin
= configs
[i
].pin
;
387 u32 value
= configs
[i
].config
;
388 void __iomem
*reg_mux
;
389 void __iomem
*reg_mux2
;
394 reg_mux
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->mux
);
395 reg_mux2
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->mux2
);
396 mux
= cv1800_dt_get_pin_mux(value
);
397 mux2
= cv1800_dt_get_pin_mux2(value
);
399 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
400 writel_relaxed(mux
, reg_mux
);
401 if (mux2
!= PIN_MUX_INVALD
)
402 writel_relaxed(mux2
, reg_mux2
);
403 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
409 static const struct pinmux_ops cv1800_pmx_ops
= {
410 .get_functions_count
= pinmux_generic_get_function_count
,
411 .get_function_name
= pinmux_generic_get_function_name
,
412 .get_function_groups
= pinmux_generic_get_function_groups
,
413 .set_mux
= cv1800_pmx_set_mux
,
417 #define PIN_IO_PULLUP BIT(2)
418 #define PIN_IO_PULLDOWN BIT(3)
419 #define PIN_IO_DRIVE GENMASK(7, 5)
420 #define PIN_IO_SCHMITT GENMASK(9, 8)
421 #define PIN_IO_BUS_HOLD BIT(10)
422 #define PIN_IO_OUT_FAST_SLEW BIT(11)
424 static u32
cv1800_pull_down_typical_resistor(struct cv1800_pinctrl
*pctrl
,
425 struct cv1800_pin
*pin
)
427 return pctrl
->data
->vddio_ops
->get_pull_down(pin
, pctrl
->power_cfg
);
430 static u32
cv1800_pull_up_typical_resistor(struct cv1800_pinctrl
*pctrl
,
431 struct cv1800_pin
*pin
)
433 return pctrl
->data
->vddio_ops
->get_pull_up(pin
, pctrl
->power_cfg
);
436 static int cv1800_pinctrl_oc2reg(struct cv1800_pinctrl
*pctrl
,
437 struct cv1800_pin
*pin
, u32 target
)
442 len
= pctrl
->data
->vddio_ops
->get_oc_map(pin
, pctrl
->power_cfg
, &map
);
446 for (i
= 0; i
< len
; i
++) {
447 if (map
[i
] >= target
)
454 static int cv1800_pinctrl_reg2oc(struct cv1800_pinctrl
*pctrl
,
455 struct cv1800_pin
*pin
, u32 reg
)
460 len
= pctrl
->data
->vddio_ops
->get_oc_map(pin
, pctrl
->power_cfg
, &map
);
470 static int cv1800_pinctrl_schmitt2reg(struct cv1800_pinctrl
*pctrl
,
471 struct cv1800_pin
*pin
, u32 target
)
476 len
= pctrl
->data
->vddio_ops
->get_schmitt_map(pin
, pctrl
->power_cfg
,
481 for (i
= 0; i
< len
; i
++) {
482 if (map
[i
] == target
)
489 static int cv1800_pinctrl_reg2schmitt(struct cv1800_pinctrl
*pctrl
,
490 struct cv1800_pin
*pin
, u32 reg
)
495 len
= pctrl
->data
->vddio_ops
->get_schmitt_map(pin
, pctrl
->power_cfg
,
506 static int cv1800_pconf_get(struct pinctrl_dev
*pctldev
,
507 unsigned int pin_id
, unsigned long *config
)
509 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
510 int param
= pinconf_to_config_param(*config
);
511 struct cv1800_pin
*pin
= cv1800_get_pin(pctrl
, pin_id
);
512 enum cv1800_pin_io_type type
;
521 type
= cv1800_pin_io_type(pin
);
522 if (type
== IO_TYPE_ETH
|| type
== IO_TYPE_AUDIO
)
525 value
= readl(cv1800_pinctrl_get_component_addr(pctrl
, &pin
->conf
));
528 case PIN_CONFIG_BIAS_PULL_DOWN
:
529 enabled
= FIELD_GET(PIN_IO_PULLDOWN
, value
);
530 arg
= cv1800_pull_down_typical_resistor(pctrl
, pin
);
532 case PIN_CONFIG_BIAS_PULL_UP
:
533 enabled
= FIELD_GET(PIN_IO_PULLUP
, value
);
534 arg
= cv1800_pull_up_typical_resistor(pctrl
, pin
);
536 case PIN_CONFIG_DRIVE_STRENGTH_UA
:
538 arg
= FIELD_GET(PIN_IO_DRIVE
, value
);
539 ret
= cv1800_pinctrl_reg2oc(pctrl
, pin
, arg
);
544 case PIN_CONFIG_INPUT_SCHMITT_UV
:
545 arg
= FIELD_GET(PIN_IO_SCHMITT
, value
);
546 ret
= cv1800_pinctrl_reg2schmitt(pctrl
, pin
, arg
);
552 case PIN_CONFIG_POWER_SOURCE
:
554 arg
= cv1800_get_power_cfg(pctrl
, pin
->power_domain
);
556 case PIN_CONFIG_SLEW_RATE
:
558 arg
= FIELD_GET(PIN_IO_OUT_FAST_SLEW
, value
);
560 case PIN_CONFIG_BIAS_BUS_HOLD
:
561 arg
= FIELD_GET(PIN_IO_BUS_HOLD
, value
);
568 *config
= pinconf_to_config_packed(param
, arg
);
570 return enabled
? 0 : -EINVAL
;
573 static int cv1800_pinconf_compute_config(struct cv1800_pinctrl
*pctrl
,
574 struct cv1800_pin
*pin
,
575 unsigned long *configs
,
576 unsigned int num_configs
,
581 enum cv1800_pin_io_type type
;
587 type
= cv1800_pin_io_type(pin
);
588 if (type
== IO_TYPE_ETH
|| type
== IO_TYPE_AUDIO
)
591 for (i
= 0; i
< num_configs
; i
++) {
592 int param
= pinconf_to_config_param(configs
[i
]);
593 u32 arg
= pinconf_to_config_argument(configs
[i
]);
596 case PIN_CONFIG_BIAS_PULL_DOWN
:
597 v
&= ~PIN_IO_PULLDOWN
;
598 v
|= FIELD_PREP(PIN_IO_PULLDOWN
, arg
);
600 case PIN_CONFIG_BIAS_PULL_UP
:
602 v
|= FIELD_PREP(PIN_IO_PULLUP
, arg
);
604 case PIN_CONFIG_DRIVE_STRENGTH_UA
:
605 ret
= cv1800_pinctrl_oc2reg(pctrl
, pin
, arg
);
609 v
|= FIELD_PREP(PIN_IO_DRIVE
, ret
);
611 case PIN_CONFIG_INPUT_SCHMITT_UV
:
612 ret
= cv1800_pinctrl_schmitt2reg(pctrl
, pin
, arg
);
615 v
&= ~PIN_IO_SCHMITT
;
616 v
|= FIELD_PREP(PIN_IO_SCHMITT
, ret
);
618 case PIN_CONFIG_POWER_SOURCE
:
619 /* Ignore power source as it is always fixed */
621 case PIN_CONFIG_SLEW_RATE
:
622 v
&= ~PIN_IO_OUT_FAST_SLEW
;
623 v
|= FIELD_PREP(PIN_IO_OUT_FAST_SLEW
, arg
);
625 case PIN_CONFIG_BIAS_BUS_HOLD
:
626 v
&= ~PIN_IO_BUS_HOLD
;
627 v
|= FIELD_PREP(PIN_IO_BUS_HOLD
, arg
);
639 static int cv1800_pin_set_config(struct cv1800_pinctrl
*pctrl
,
643 struct cv1800_pin
*pin
= cv1800_get_pin(pctrl
, pin_id
);
650 addr
= cv1800_pinctrl_get_component_addr(pctrl
, &pin
->conf
);
652 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
654 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
659 static int cv1800_pconf_set(struct pinctrl_dev
*pctldev
,
660 unsigned int pin_id
, unsigned long *configs
,
661 unsigned int num_configs
)
663 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
664 struct cv1800_pin
*pin
= cv1800_get_pin(pctrl
, pin_id
);
670 if (cv1800_pinconf_compute_config(pctrl
, pin
,
671 configs
, num_configs
, &value
))
674 return cv1800_pin_set_config(pctrl
, pin_id
, value
);
677 static int cv1800_pconf_group_set(struct pinctrl_dev
*pctldev
,
679 unsigned long *configs
,
680 unsigned int num_configs
)
682 struct cv1800_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
683 const struct group_desc
*group
;
684 const struct cv1800_pin_mux_config
*pinmuxs
;
688 group
= pinctrl_generic_get_group(pctldev
, gsel
);
692 pinmuxs
= group
->data
;
694 if (cv1800_pinconf_compute_config(pctrl
, pinmuxs
[0].pin
,
695 configs
, num_configs
, &value
))
698 for (i
= 0; i
< group
->grp
.npins
; i
++)
699 cv1800_pin_set_config(pctrl
, group
->grp
.pins
[i
], value
);
704 static const struct pinconf_ops cv1800_pconf_ops
= {
705 .pin_config_get
= cv1800_pconf_get
,
706 .pin_config_set
= cv1800_pconf_set
,
707 .pin_config_group_set
= cv1800_pconf_group_set
,
711 int cv1800_pinctrl_probe(struct platform_device
*pdev
)
713 struct device
*dev
= &pdev
->dev
;
714 struct cv1800_pinctrl
*pctrl
;
715 const struct cv1800_pinctrl_data
*pctrl_data
;
718 pctrl_data
= device_get_match_data(dev
);
722 if (pctrl_data
->npins
== 0 || pctrl_data
->npd
== 0)
723 return dev_err_probe(dev
, -EINVAL
, "invalid pin data\n");
725 pctrl
= devm_kzalloc(dev
, sizeof(*pctrl
), GFP_KERNEL
);
729 pctrl
->power_cfg
= devm_kcalloc(dev
, pctrl_data
->npd
,
730 sizeof(u32
), GFP_KERNEL
);
731 if (!pctrl
->power_cfg
)
734 pctrl
->regs
[0] = devm_platform_ioremap_resource_byname(pdev
, "sys");
735 if (IS_ERR(pctrl
->regs
[0]))
736 return PTR_ERR(pctrl
->regs
[0]);
738 pctrl
->regs
[1] = devm_platform_ioremap_resource_byname(pdev
, "rtc");
739 if (IS_ERR(pctrl
->regs
[1]))
740 return PTR_ERR(pctrl
->regs
[1]);
742 pctrl
->pdesc
.name
= dev_name(dev
);
743 pctrl
->pdesc
.pins
= pctrl_data
->pins
;
744 pctrl
->pdesc
.npins
= pctrl_data
->npins
;
745 pctrl
->pdesc
.pctlops
= &cv1800_pctrl_ops
;
746 pctrl
->pdesc
.pmxops
= &cv1800_pmx_ops
;
747 pctrl
->pdesc
.confops
= &cv1800_pconf_ops
;
748 pctrl
->pdesc
.owner
= THIS_MODULE
;
750 pctrl
->data
= pctrl_data
;
752 raw_spin_lock_init(&pctrl
->lock
);
753 mutex_init(&pctrl
->mutex
);
755 platform_set_drvdata(pdev
, pctrl
);
757 ret
= devm_pinctrl_register_and_init(dev
, &pctrl
->pdesc
,
758 pctrl
, &pctrl
->pctl_dev
);
760 return dev_err_probe(dev
, ret
,
761 "fail to register pinctrl driver\n");
763 return pinctrl_enable(pctrl
->pctl_dev
);
765 EXPORT_SYMBOL_GPL(cv1800_pinctrl_probe
);