GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / mux / mmio.c
1 /*
2  * MMIO register bitfield-controlled multiplexer driver
3  *
4  * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/bitops.h>
12 #include <linux/err.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/mux/driver.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/regmap.h>
20
21 static int mux_mmio_set(struct mux_control *mux, int state)
22 {
23         struct regmap_field **fields = mux_chip_priv(mux->chip);
24
25         return regmap_field_write(fields[mux_control_get_index(mux)], state);
26 }
27
28 static const struct mux_control_ops mux_mmio_ops = {
29         .set = mux_mmio_set,
30 };
31
32 static const struct of_device_id mux_mmio_dt_ids[] = {
33         { .compatible = "mmio-mux", },
34         { /* sentinel */ }
35 };
36 MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids);
37
38 static int mux_mmio_probe(struct platform_device *pdev)
39 {
40         struct device *dev = &pdev->dev;
41         struct device_node *np = dev->of_node;
42         struct regmap_field **fields;
43         struct mux_chip *mux_chip;
44         struct regmap *regmap;
45         int num_fields;
46         int ret;
47         int i;
48
49         regmap = syscon_node_to_regmap(np->parent);
50         if (IS_ERR(regmap)) {
51                 ret = PTR_ERR(regmap);
52                 dev_err(dev, "failed to get regmap: %d\n", ret);
53                 return ret;
54         }
55
56         ret = of_property_count_u32_elems(np, "mux-reg-masks");
57         if (ret == 0 || ret % 2)
58                 ret = -EINVAL;
59         if (ret < 0) {
60                 dev_err(dev, "mux-reg-masks property missing or invalid: %d\n",
61                         ret);
62                 return ret;
63         }
64         num_fields = ret / 2;
65
66         mux_chip = devm_mux_chip_alloc(dev, num_fields, num_fields *
67                                        sizeof(*fields));
68         if (IS_ERR(mux_chip))
69                 return PTR_ERR(mux_chip);
70
71         fields = mux_chip_priv(mux_chip);
72
73         for (i = 0; i < num_fields; i++) {
74                 struct mux_control *mux = &mux_chip->mux[i];
75                 struct reg_field field;
76                 s32 idle_state = MUX_IDLE_AS_IS;
77                 u32 reg, mask;
78                 int bits;
79
80                 ret = of_property_read_u32_index(np, "mux-reg-masks",
81                                                  2 * i, &reg);
82                 if (!ret)
83                         ret = of_property_read_u32_index(np, "mux-reg-masks",
84                                                          2 * i + 1, &mask);
85                 if (ret < 0) {
86                         dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n",
87                                 i, ret);
88                         return ret;
89                 }
90
91                 field.reg = reg;
92                 field.msb = fls(mask) - 1;
93                 field.lsb = ffs(mask) - 1;
94
95                 if (mask != GENMASK(field.msb, field.lsb)) {
96                         dev_err(dev, "bitfield %d: invalid mask 0x%x\n",
97                                 i, mask);
98                         return -EINVAL;
99                 }
100
101                 fields[i] = devm_regmap_field_alloc(dev, regmap, field);
102                 if (IS_ERR(fields[i])) {
103                         ret = PTR_ERR(fields[i]);
104                         dev_err(dev, "bitfield %d: failed allocate: %d\n",
105                                 i, ret);
106                         return ret;
107                 }
108
109                 bits = 1 + field.msb - field.lsb;
110                 mux->states = 1 << bits;
111
112                 of_property_read_u32_index(np, "idle-states", i,
113                                            (u32 *)&idle_state);
114                 if (idle_state != MUX_IDLE_AS_IS) {
115                         if (idle_state < 0 || idle_state >= mux->states) {
116                                 dev_err(dev, "bitfield: %d: out of range idle state %d\n",
117                                         i, idle_state);
118                                 return -EINVAL;
119                         }
120
121                         mux->idle_state = idle_state;
122                 }
123         }
124
125         mux_chip->ops = &mux_mmio_ops;
126
127         return devm_mux_chip_register(dev, mux_chip);
128 }
129
130 static struct platform_driver mux_mmio_driver = {
131         .driver = {
132                 .name = "mmio-mux",
133                 .of_match_table = of_match_ptr(mux_mmio_dt_ids),
134         },
135         .probe = mux_mmio_probe,
136 };
137 module_platform_driver(mux_mmio_driver);
138
139 MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
140 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
141 MODULE_LICENSE("GPL v2");