]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/i2c/muxes/i2c-mux-reg.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-hirsute-kernel.git] / drivers / i2c / muxes / i2c-mux-reg.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
b3fdd327
YS
2/*
3 * I2C multiplexer using a single register
4 *
5 * Copyright 2015 Freescale Semiconductor
6 * York Sun <yorksun@freescale.com>
b3fdd327
YS
7 */
8
9#include <linux/i2c.h>
10#include <linux/i2c-mux.h>
11#include <linux/init.h>
12#include <linux/io.h>
13#include <linux/module.h>
14#include <linux/of_address.h>
15#include <linux/platform_data/i2c-mux-reg.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18
19struct regmux {
b3fdd327
YS
20 struct i2c_mux_reg_platform_data data;
21};
22
23static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
24{
25 if (!mux->data.reg)
26 return -EINVAL;
27
5a73882f
YS
28 /*
29 * Write to the register, followed by a read to ensure the write is
30 * completed on a "posted" bus, for example PCI or write buffers.
31 * The endianness of reading doesn't matter and the return data
32 * is not used.
33 */
b3fdd327
YS
34 switch (mux->data.reg_size) {
35 case 4:
5a73882f 36 if (mux->data.little_endian)
b3fdd327 37 iowrite32(chan_id, mux->data.reg);
5a73882f 38 else
b3fdd327 39 iowrite32be(chan_id, mux->data.reg);
5a73882f
YS
40 if (!mux->data.write_only)
41 ioread32(mux->data.reg);
b3fdd327
YS
42 break;
43 case 2:
5a73882f 44 if (mux->data.little_endian)
b3fdd327 45 iowrite16(chan_id, mux->data.reg);
5a73882f 46 else
b3fdd327 47 iowrite16be(chan_id, mux->data.reg);
5a73882f
YS
48 if (!mux->data.write_only)
49 ioread16(mux->data.reg);
b3fdd327
YS
50 break;
51 case 1:
52 iowrite8(chan_id, mux->data.reg);
53 if (!mux->data.write_only)
54 ioread8(mux->data.reg);
55 break;
b3fdd327
YS
56 }
57
58 return 0;
59}
60
193304ae 61static int i2c_mux_reg_select(struct i2c_mux_core *muxc, u32 chan)
b3fdd327 62{
193304ae 63 struct regmux *mux = i2c_mux_priv(muxc);
b3fdd327
YS
64
65 return i2c_mux_reg_set(mux, chan);
66}
67
193304ae 68static int i2c_mux_reg_deselect(struct i2c_mux_core *muxc, u32 chan)
b3fdd327 69{
193304ae 70 struct regmux *mux = i2c_mux_priv(muxc);
b3fdd327
YS
71
72 if (mux->data.idle_in_use)
73 return i2c_mux_reg_set(mux, mux->data.idle);
74
75 return 0;
76}
77
78#ifdef CONFIG_OF
79static int i2c_mux_reg_probe_dt(struct regmux *mux,
193304ae 80 struct platform_device *pdev)
b3fdd327
YS
81{
82 struct device_node *np = pdev->dev.of_node;
83 struct device_node *adapter_np, *child;
84 struct i2c_adapter *adapter;
85 struct resource res;
86 unsigned *values;
87 int i = 0;
88
89 if (!np)
90 return -ENODEV;
91
92 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
93 if (!adapter_np) {
94 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
95 return -ENODEV;
96 }
97 adapter = of_find_i2c_adapter_by_node(adapter_np);
bdbf4a29 98 of_node_put(adapter_np);
b3fdd327
YS
99 if (!adapter)
100 return -EPROBE_DEFER;
101
b3fdd327
YS
102 mux->data.parent = i2c_adapter_id(adapter);
103 put_device(&adapter->dev);
104
105 mux->data.n_values = of_get_child_count(np);
3997fb74 106 if (of_property_read_bool(np, "little-endian")) {
b3fdd327 107 mux->data.little_endian = true;
3997fb74 108 } else if (of_property_read_bool(np, "big-endian")) {
b3fdd327
YS
109 mux->data.little_endian = false;
110 } else {
111#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
112 defined(__LITTLE_ENDIAN)
113 mux->data.little_endian = true;
114#elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
115 defined(__BIG_ENDIAN)
116 mux->data.little_endian = false;
117#else
118#error Endianness not defined?
119#endif
120 }
3997fb74 121 mux->data.write_only = of_property_read_bool(np, "write-only");
b3fdd327 122
a86854d0
KC
123 values = devm_kcalloc(&pdev->dev,
124 mux->data.n_values, sizeof(*mux->data.values),
b3fdd327 125 GFP_KERNEL);
b30c08a2 126 if (!values)
b3fdd327 127 return -ENOMEM;
b3fdd327
YS
128
129 for_each_child_of_node(np, child) {
130 of_property_read_u32(child, "reg", values + i);
131 i++;
132 }
133 mux->data.values = values;
134
135 if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
136 mux->data.idle_in_use = true;
137
138 /* map address from "reg" if exists */
22ebf00e 139 if (of_address_to_resource(np, 0, &res) == 0) {
b3fdd327 140 mux->data.reg_size = resource_size(&res);
b3fdd327
YS
141 mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
142 if (IS_ERR(mux->data.reg))
143 return PTR_ERR(mux->data.reg);
144 }
145
146 return 0;
147}
148#else
a05a34e7 149static int i2c_mux_reg_probe_dt(struct regmux *mux,
193304ae 150 struct platform_device *pdev)
b3fdd327
YS
151{
152 return 0;
153}
154#endif
155
156static int i2c_mux_reg_probe(struct platform_device *pdev)
157{
193304ae 158 struct i2c_mux_core *muxc;
b3fdd327
YS
159 struct regmux *mux;
160 struct i2c_adapter *parent;
161 struct resource *res;
b3fdd327
YS
162 unsigned int class;
163 int i, ret, nr;
164
165 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
166 if (!mux)
167 return -ENOMEM;
168
b3fdd327
YS
169 if (dev_get_platdata(&pdev->dev)) {
170 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
171 sizeof(mux->data));
b3fdd327
YS
172 } else {
173 ret = i2c_mux_reg_probe_dt(mux, pdev);
ac5b85de
TB
174 if (ret == -EPROBE_DEFER)
175 return ret;
176
b3fdd327
YS
177 if (ret < 0) {
178 dev_err(&pdev->dev, "Error parsing device tree");
179 return ret;
180 }
181 }
182
193304ae
PR
183 parent = i2c_get_adapter(mux->data.parent);
184 if (!parent)
185 return -EPROBE_DEFER;
186
b3fdd327
YS
187 if (!mux->data.reg) {
188 dev_info(&pdev->dev,
189 "Register not set, using platform resource\n");
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 mux->data.reg_size = resource_size(res);
b3fdd327 192 mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
68118e0e
PR
193 if (IS_ERR(mux->data.reg)) {
194 ret = PTR_ERR(mux->data.reg);
195 goto err_put_parent;
196 }
b3fdd327
YS
197 }
198
fce388af
WS
199 if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
200 mux->data.reg_size != 1) {
201 dev_err(&pdev->dev, "Invalid register size\n");
68118e0e
PR
202 ret = -EINVAL;
203 goto err_put_parent;
fce388af
WS
204 }
205
193304ae
PR
206 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
207 i2c_mux_reg_select, NULL);
68118e0e
PR
208 if (!muxc) {
209 ret = -ENOMEM;
210 goto err_put_parent;
211 }
193304ae
PR
212 muxc->priv = mux;
213
214 platform_set_drvdata(pdev, muxc);
b3fdd327
YS
215
216 if (mux->data.idle_in_use)
193304ae 217 muxc->deselect = i2c_mux_reg_deselect;
b3fdd327
YS
218
219 for (i = 0; i < mux->data.n_values; i++) {
220 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
221 class = mux->data.classes ? mux->data.classes[i] : 0;
222
193304ae 223 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
f0892361 224 if (ret)
a36d4637 225 goto err_del_mux_adapters;
b3fdd327
YS
226 }
227
228 dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
193304ae 229 mux->data.n_values, muxc->parent->name);
b3fdd327
YS
230
231 return 0;
232
a36d4637 233err_del_mux_adapters:
193304ae 234 i2c_mux_del_adapters(muxc);
68118e0e
PR
235err_put_parent:
236 i2c_put_adapter(parent);
b3fdd327
YS
237
238 return ret;
239}
240
241static int i2c_mux_reg_remove(struct platform_device *pdev)
242{
193304ae 243 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
b3fdd327 244
193304ae
PR
245 i2c_mux_del_adapters(muxc);
246 i2c_put_adapter(muxc->parent);
b3fdd327
YS
247
248 return 0;
249}
250
251static const struct of_device_id i2c_mux_reg_of_match[] = {
252 { .compatible = "i2c-mux-reg", },
253 {},
254};
255MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
256
257static struct platform_driver i2c_mux_reg_driver = {
258 .probe = i2c_mux_reg_probe,
259 .remove = i2c_mux_reg_remove,
260 .driver = {
261 .name = "i2c-mux-reg",
9f05e621 262 .of_match_table = of_match_ptr(i2c_mux_reg_of_match),
b3fdd327
YS
263 },
264};
265
266module_platform_driver(i2c_mux_reg_driver);
267
268MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
269MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
270MODULE_LICENSE("GPL");
271MODULE_ALIAS("platform:i2c-mux-reg");