1 // SPDX-License-Identifier: GPL-2.0
3 * MMIO register bitfield-controlled multiplexer driver
5 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
8 #include <linux/bitops.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/mux/driver.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
18 static int mux_mmio_set(struct mux_control
*mux
, int state
)
20 struct regmap_field
**fields
= mux_chip_priv(mux
->chip
);
22 return regmap_field_write(fields
[mux_control_get_index(mux
)], state
);
25 static const struct mux_control_ops mux_mmio_ops
= {
29 static const struct of_device_id mux_mmio_dt_ids
[] = {
30 { .compatible
= "mmio-mux", },
31 { .compatible
= "reg-mux", },
34 MODULE_DEVICE_TABLE(of
, mux_mmio_dt_ids
);
36 static int mux_mmio_probe(struct platform_device
*pdev
)
38 struct device
*dev
= &pdev
->dev
;
39 struct device_node
*np
= dev
->of_node
;
40 struct regmap_field
**fields
;
41 struct mux_chip
*mux_chip
;
42 struct regmap
*regmap
;
47 if (of_device_is_compatible(np
, "mmio-mux")) {
48 regmap
= syscon_node_to_regmap(np
->parent
);
50 regmap
= device_node_to_regmap(np
);
51 /* Fallback to checking the parent node on "real" errors. */
52 if (IS_ERR(regmap
) && regmap
!= ERR_PTR(-EPROBE_DEFER
)) {
53 regmap
= dev_get_regmap(dev
->parent
, NULL
);
55 regmap
= ERR_PTR(-ENODEV
);
59 return dev_err_probe(dev
, PTR_ERR(regmap
),
60 "failed to get regmap\n");
62 ret
= of_property_count_u32_elems(np
, "mux-reg-masks");
63 if (ret
== 0 || ret
% 2)
66 dev_err(dev
, "mux-reg-masks property missing or invalid: %d\n",
72 mux_chip
= devm_mux_chip_alloc(dev
, num_fields
, num_fields
*
75 return PTR_ERR(mux_chip
);
77 fields
= mux_chip_priv(mux_chip
);
79 for (i
= 0; i
< num_fields
; i
++) {
80 struct mux_control
*mux
= &mux_chip
->mux
[i
];
81 struct reg_field field
;
82 s32 idle_state
= MUX_IDLE_AS_IS
;
86 ret
= of_property_read_u32_index(np
, "mux-reg-masks",
89 ret
= of_property_read_u32_index(np
, "mux-reg-masks",
92 dev_err(dev
, "bitfield %d: failed to read mux-reg-masks property: %d\n",
98 field
.msb
= fls(mask
) - 1;
99 field
.lsb
= ffs(mask
) - 1;
101 if (mask
!= GENMASK(field
.msb
, field
.lsb
)) {
102 dev_err(dev
, "bitfield %d: invalid mask 0x%x\n",
107 fields
[i
] = devm_regmap_field_alloc(dev
, regmap
, field
);
108 if (IS_ERR(fields
[i
])) {
109 ret
= PTR_ERR(fields
[i
]);
110 dev_err(dev
, "bitfield %d: failed allocate: %d\n",
115 bits
= 1 + field
.msb
- field
.lsb
;
116 mux
->states
= 1 << bits
;
118 of_property_read_u32_index(np
, "idle-states", i
,
120 if (idle_state
!= MUX_IDLE_AS_IS
) {
121 if (idle_state
< 0 || idle_state
>= mux
->states
) {
122 dev_err(dev
, "bitfield: %d: out of range idle state %d\n",
127 mux
->idle_state
= idle_state
;
131 mux_chip
->ops
= &mux_mmio_ops
;
133 return devm_mux_chip_register(dev
, mux_chip
);
136 static struct platform_driver mux_mmio_driver
= {
139 .of_match_table
= mux_mmio_dt_ids
,
141 .probe
= mux_mmio_probe
,
143 module_platform_driver(mux_mmio_driver
);
145 MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
146 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
147 MODULE_LICENSE("GPL v2");