1// SPDX-License-Identifier: GPL-2.0
2/*
3 * MMIO register bitfield-controlled multiplexer driver
4 *
5 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
6 */
7
8#include <linux/bitops.h>
9#include <linux/err.h>
10#include <linux/mfd/syscon.h>
11#include <linux/module.h>
12#include <linux/mux/driver.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/property.h>
16#include <linux/regmap.h>
17
18static int mux_mmio_set(struct mux_control *mux, int state)
19{
20 struct regmap_field **fields = mux_chip_priv(mux_chip: mux->chip);
21
22 return regmap_field_write(field: fields[mux_control_get_index(mux)], val: state);
23}
24
25static const struct mux_control_ops mux_mmio_ops = {
26 .set = mux_mmio_set,
27};
28
29static const struct of_device_id mux_mmio_dt_ids[] = {
30 { .compatible = "mmio-mux", },
31 { .compatible = "reg-mux", },
32 { /* sentinel */ }
33};
34MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids);
35
36static int mux_mmio_probe(struct platform_device *pdev)
37{
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;
43 int num_fields;
44 int ret;
45 int i;
46
47 if (of_device_is_compatible(device: np, "mmio-mux")) {
48 regmap = syscon_node_to_regmap(np: np->parent);
49 } else {
50 regmap = device_node_to_regmap(np);
51 /* Fallback to checking the parent node on "real" errors. */
52 if (IS_ERR(ptr: regmap) && regmap != ERR_PTR(error: -EPROBE_DEFER)) {
53 regmap = dev_get_regmap(dev: dev->parent, NULL);
54 if (!regmap)
55 regmap = ERR_PTR(error: -ENODEV);
56 }
57 }
58 if (IS_ERR(ptr: regmap))
59 return dev_err_probe(dev, err: PTR_ERR(ptr: regmap),
60 fmt: "failed to get regmap\n");
61
62 ret = of_property_count_u32_elems(np, propname: "mux-reg-masks");
63 if (ret == 0 || ret % 2)
64 ret = -EINVAL;
65 if (ret < 0) {
66 dev_err(dev, "mux-reg-masks property missing or invalid: %d\n",
67 ret);
68 return ret;
69 }
70 num_fields = ret / 2;
71
72 mux_chip = devm_mux_chip_alloc(dev, controllers: num_fields, sizeof_priv: num_fields *
73 sizeof(*fields));
74 if (IS_ERR(ptr: mux_chip))
75 return PTR_ERR(ptr: mux_chip);
76
77 fields = mux_chip_priv(mux_chip);
78
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;
83 u32 reg, mask;
84 int bits;
85
86 ret = of_property_read_u32_index(np, propname: "mux-reg-masks",
87 index: 2 * i, out_value: &reg);
88 if (!ret)
89 ret = of_property_read_u32_index(np, propname: "mux-reg-masks",
90 index: 2 * i + 1, out_value: &mask);
91 if (ret < 0) {
92 dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n",
93 i, ret);
94 return ret;
95 }
96
97 field.reg = reg;
98 field.msb = fls(x: mask) - 1;
99 field.lsb = ffs(mask) - 1;
100
101 if (mask != GENMASK(field.msb, field.lsb)) {
102 dev_err(dev, "bitfield %d: invalid mask 0x%x\n",
103 i, mask);
104 return -EINVAL;
105 }
106
107 fields[i] = devm_regmap_field_alloc(dev, regmap, reg_field: field);
108 if (IS_ERR(ptr: fields[i])) {
109 ret = PTR_ERR(ptr: fields[i]);
110 dev_err(dev, "bitfield %d: failed allocate: %d\n",
111 i, ret);
112 return ret;
113 }
114
115 bits = 1 + field.msb - field.lsb;
116 mux->states = 1 << bits;
117
118 of_property_read_u32_index(np, propname: "idle-states", index: i,
119 out_value: (u32 *)&idle_state);
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",
123 i, idle_state);
124 return -EINVAL;
125 }
126
127 mux->idle_state = idle_state;
128 }
129 }
130
131 mux_chip->ops = &mux_mmio_ops;
132
133 return devm_mux_chip_register(dev, mux_chip);
134}
135
136static struct platform_driver mux_mmio_driver = {
137 .driver = {
138 .name = "mmio-mux",
139 .of_match_table = mux_mmio_dt_ids,
140 },
141 .probe = mux_mmio_probe,
142};
143module_platform_driver(mux_mmio_driver);
144
145MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
146MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
147MODULE_LICENSE("GPL v2");
148

source code of linux/drivers/mux/mmio.c