1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/jiffies.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/device.h>
24#include <linux/i2c.h>
25#include <linux/i2c-mux.h>
26
27#include <linux/i2c/pca954x.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#define PCA9541_CONTROL 0x01
45#define PCA9541_ISTAT 0x02
46
47#define PCA9541_CTL_MYBUS (1 << 0)
48#define PCA9541_CTL_NMYBUS (1 << 1)
49#define PCA9541_CTL_BUSON (1 << 2)
50#define PCA9541_CTL_NBUSON (1 << 3)
51#define PCA9541_CTL_BUSINIT (1 << 4)
52#define PCA9541_CTL_TESTON (1 << 6)
53#define PCA9541_CTL_NTESTON (1 << 7)
54
55#define PCA9541_ISTAT_INTIN (1 << 0)
56#define PCA9541_ISTAT_BUSINIT (1 << 1)
57#define PCA9541_ISTAT_BUSOK (1 << 2)
58#define PCA9541_ISTAT_BUSLOST (1 << 3)
59#define PCA9541_ISTAT_MYTEST (1 << 6)
60#define PCA9541_ISTAT_NMYTEST (1 << 7)
61
62#define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
63#define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
64#define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
65#define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
66
67
68#define ARB_TIMEOUT (HZ / 8)
69#define ARB2_TIMEOUT (HZ / 4)
70
71
72#define SELECT_DELAY_SHORT 50
73#define SELECT_DELAY_LONG 1000
74
75struct pca9541 {
76 struct i2c_client *client;
77 unsigned long select_timeout;
78 unsigned long arb_timeout;
79};
80
81static const struct i2c_device_id pca9541_id[] = {
82 {"pca9541", 0},
83 {}
84};
85
86MODULE_DEVICE_TABLE(i2c, pca9541_id);
87
88
89
90
91
92static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
93{
94 struct i2c_adapter *adap = client->adapter;
95 int ret;
96
97 if (adap->algo->master_xfer) {
98 struct i2c_msg msg;
99 char buf[2];
100
101 msg.addr = client->addr;
102 msg.flags = 0;
103 msg.len = 2;
104 buf[0] = command;
105 buf[1] = val;
106 msg.buf = buf;
107 ret = __i2c_transfer(adap, &msg, 1);
108 } else {
109 union i2c_smbus_data data;
110
111 data.byte = val;
112 ret = adap->algo->smbus_xfer(adap, client->addr,
113 client->flags,
114 I2C_SMBUS_WRITE,
115 command,
116 I2C_SMBUS_BYTE_DATA, &data);
117 }
118
119 return ret;
120}
121
122
123
124
125
126static int pca9541_reg_read(struct i2c_client *client, u8 command)
127{
128 struct i2c_adapter *adap = client->adapter;
129 int ret;
130 u8 val;
131
132 if (adap->algo->master_xfer) {
133 struct i2c_msg msg[2] = {
134 {
135 .addr = client->addr,
136 .flags = 0,
137 .len = 1,
138 .buf = &command
139 },
140 {
141 .addr = client->addr,
142 .flags = I2C_M_RD,
143 .len = 1,
144 .buf = &val
145 }
146 };
147 ret = __i2c_transfer(adap, msg, 2);
148 if (ret == 2)
149 ret = val;
150 else if (ret >= 0)
151 ret = -EIO;
152 } else {
153 union i2c_smbus_data data;
154
155 ret = adap->algo->smbus_xfer(adap, client->addr,
156 client->flags,
157 I2C_SMBUS_READ,
158 command,
159 I2C_SMBUS_BYTE_DATA, &data);
160 if (!ret)
161 ret = data.byte;
162 }
163 return ret;
164}
165
166
167
168
169
170
171static void pca9541_release_bus(struct i2c_client *client)
172{
173 int reg;
174
175 reg = pca9541_reg_read(client, PCA9541_CONTROL);
176 if (reg >= 0 && !busoff(reg) && mybus(reg))
177 pca9541_reg_write(client, PCA9541_CONTROL,
178 (reg & PCA9541_CTL_NBUSON) >> 1);
179}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206static const u8 pca9541_control[16] = {
207 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
208};
209
210
211
212
213
214
215
216
217
218static int pca9541_arbitrate(struct i2c_client *client)
219{
220 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
221 struct pca9541 *data = i2c_mux_priv(muxc);
222 int reg;
223
224 reg = pca9541_reg_read(client, PCA9541_CONTROL);
225 if (reg < 0)
226 return reg;
227
228 if (busoff(reg)) {
229 int istat;
230
231
232
233
234 istat = pca9541_reg_read(client, PCA9541_ISTAT);
235 if (!(istat & PCA9541_ISTAT_NMYTEST)
236 || time_is_before_eq_jiffies(data->arb_timeout)) {
237
238
239
240
241 pca9541_reg_write(client,
242 PCA9541_CONTROL,
243 pca9541_control[reg & 0x0f]
244 | PCA9541_CTL_NTESTON);
245 data->select_timeout = SELECT_DELAY_SHORT;
246 } else {
247
248
249
250
251 data->select_timeout = SELECT_DELAY_LONG * 2;
252 }
253 } else if (mybus(reg)) {
254
255
256
257
258 if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
259 pca9541_reg_write(client,
260 PCA9541_CONTROL,
261 reg & ~(PCA9541_CTL_NTESTON
262 | PCA9541_CTL_BUSINIT));
263 return 1;
264 } else {
265
266
267
268
269
270 data->select_timeout = SELECT_DELAY_LONG;
271 if (time_is_before_eq_jiffies(data->arb_timeout)) {
272
273 pca9541_reg_write(client,
274 PCA9541_CONTROL,
275 pca9541_control[reg & 0x0f]
276 | PCA9541_CTL_BUSINIT
277 | PCA9541_CTL_NTESTON);
278 } else {
279
280 if (!(reg & PCA9541_CTL_NTESTON))
281 pca9541_reg_write(client,
282 PCA9541_CONTROL,
283 reg | PCA9541_CTL_NTESTON);
284 }
285 }
286 return 0;
287}
288
289static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
290{
291 struct pca9541 *data = i2c_mux_priv(muxc);
292 struct i2c_client *client = data->client;
293 int ret;
294 unsigned long timeout = jiffies + ARB2_TIMEOUT;
295
296
297 data->arb_timeout = jiffies + ARB_TIMEOUT;
298
299
300 do {
301 ret = pca9541_arbitrate(client);
302 if (ret)
303 return ret < 0 ? ret : 0;
304
305 if (data->select_timeout == SELECT_DELAY_SHORT)
306 udelay(data->select_timeout);
307 else
308 msleep(data->select_timeout / 1000);
309 } while (time_is_after_eq_jiffies(timeout));
310
311 return -ETIMEDOUT;
312}
313
314static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
315{
316 struct pca9541 *data = i2c_mux_priv(muxc);
317 struct i2c_client *client = data->client;
318
319 pca9541_release_bus(client);
320 return 0;
321}
322
323
324
325
326static int pca9541_probe(struct i2c_client *client,
327 const struct i2c_device_id *id)
328{
329 struct i2c_adapter *adap = client->adapter;
330 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
331 struct i2c_mux_core *muxc;
332 struct pca9541 *data;
333 int force;
334 int ret;
335
336 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
337 return -ENODEV;
338
339
340
341
342
343 i2c_lock_adapter(adap);
344 pca9541_release_bus(client);
345 i2c_unlock_adapter(adap);
346
347
348
349 force = 0;
350 if (pdata)
351 force = pdata->modes[0].adap_id;
352 muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data), 0,
353 pca9541_select_chan, pca9541_release_chan);
354 if (!muxc)
355 return -ENOMEM;
356
357 data = i2c_mux_priv(muxc);
358 data->client = client;
359
360 i2c_set_clientdata(client, muxc);
361
362 ret = i2c_mux_add_adapter(muxc, force, 0, 0);
363 if (ret) {
364 dev_err(&client->dev, "failed to register master selector\n");
365 return ret;
366 }
367
368 dev_info(&client->dev, "registered master selector for I2C %s\n",
369 client->name);
370
371 return 0;
372}
373
374static int pca9541_remove(struct i2c_client *client)
375{
376 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
377
378 i2c_mux_del_adapters(muxc);
379 return 0;
380}
381
382static struct i2c_driver pca9541_driver = {
383 .driver = {
384 .name = "pca9541",
385 },
386 .probe = pca9541_probe,
387 .remove = pca9541_remove,
388 .id_table = pca9541_id,
389};
390
391module_i2c_driver(pca9541_driver);
392
393MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
394MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
395MODULE_LICENSE("GPL v2");
396