blob: 50e1fb4aedf58c77e4c2f076503206fd3e06a826 [file] [log] [blame]
Guenter Roeckae63b132010-10-24 18:16:58 +02001/*
2 * I2C multiplexer driver for PCA9541 bus master selector
3 *
4 * Copyright (c) 2010 Ericsson AB.
5 *
Guenter Roeckb1041532013-02-26 06:03:52 +00006 * Author: Guenter Roeck <linux@roeck-us.net>
Guenter Roeckae63b132010-10-24 18:16:58 +02007 *
8 * Derived from:
9 * pca954x.c
10 *
11 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
12 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
13 *
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
17 */
18
Guenter Roeckae63b132010-10-24 18:16:58 +020019#include <linux/delay.h>
Guenter Roeckae63b132010-10-24 18:16:58 +020020#include <linux/device.h>
21#include <linux/i2c.h>
22#include <linux/i2c-mux.h>
Wolfram Sang885e42d2017-08-14 10:23:24 +020023#include <linux/jiffies.h>
24#include <linux/module.h>
25#include <linux/slab.h>
Guenter Roeckae63b132010-10-24 18:16:58 +020026
27/*
28 * The PCA9541 is a bus master selector. It supports two I2C masters connected
29 * to a single slave bus.
30 *
31 * Before each bus transaction, a master has to acquire bus ownership. After the
32 * transaction is complete, bus ownership has to be released. This fits well
33 * into the I2C multiplexer framework, which provides select and release
34 * functions for this purpose. For this reason, this driver is modeled as
35 * single-channel I2C bus multiplexer.
36 *
37 * This driver assumes that the two bus masters are controlled by two different
38 * hosts. If a single host controls both masters, platform code has to ensure
39 * that only one of the masters is instantiated at any given time.
40 */
41
42#define PCA9541_CONTROL 0x01
43#define PCA9541_ISTAT 0x02
44
45#define PCA9541_CTL_MYBUS (1 << 0)
46#define PCA9541_CTL_NMYBUS (1 << 1)
47#define PCA9541_CTL_BUSON (1 << 2)
48#define PCA9541_CTL_NBUSON (1 << 3)
49#define PCA9541_CTL_BUSINIT (1 << 4)
50#define PCA9541_CTL_TESTON (1 << 6)
51#define PCA9541_CTL_NTESTON (1 << 7)
52
53#define PCA9541_ISTAT_INTIN (1 << 0)
54#define PCA9541_ISTAT_BUSINIT (1 << 1)
55#define PCA9541_ISTAT_BUSOK (1 << 2)
56#define PCA9541_ISTAT_BUSLOST (1 << 3)
57#define PCA9541_ISTAT_MYTEST (1 << 6)
58#define PCA9541_ISTAT_NMYTEST (1 << 7)
59
60#define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
61#define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
62#define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
63#define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
64
65/* arbitration timeouts, in jiffies */
66#define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */
67#define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */
68
69/* arbitration retry delays, in us */
70#define SELECT_DELAY_SHORT 50
71#define SELECT_DELAY_LONG 1000
72
73struct pca9541 {
Peter Rosinab88b972016-04-20 08:40:02 +020074 struct i2c_client *client;
Guenter Roeckae63b132010-10-24 18:16:58 +020075 unsigned long select_timeout;
76 unsigned long arb_timeout;
77};
78
79static const struct i2c_device_id pca9541_id[] = {
80 {"pca9541", 0},
81 {}
82};
83
84MODULE_DEVICE_TABLE(i2c, pca9541_id);
85
Peter Rosina1cbf332016-07-09 20:30:58 +020086#ifdef CONFIG_OF
87static const struct of_device_id pca9541_of_match[] = {
88 { .compatible = "nxp,pca9541" },
89 {}
90};
Javier Martinez Canillase278d642017-01-16 10:54:55 -030091MODULE_DEVICE_TABLE(of, pca9541_of_match);
Peter Rosina1cbf332016-07-09 20:30:58 +020092#endif
93
Guenter Roeckae63b132010-10-24 18:16:58 +020094/*
95 * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
96 * as they will try to lock the adapter a second time.
97 */
98static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
99{
100 struct i2c_adapter *adap = client->adapter;
Peter Rosina5306b82018-06-20 10:51:55 +0200101 union i2c_smbus_data data = { .byte = val };
Guenter Roeckae63b132010-10-24 18:16:58 +0200102
Peter Rosina5306b82018-06-20 10:51:55 +0200103 return __i2c_smbus_xfer(adap, client->addr, client->flags,
104 I2C_SMBUS_WRITE, command,
105 I2C_SMBUS_BYTE_DATA, &data);
Guenter Roeckae63b132010-10-24 18:16:58 +0200106}
107
108/*
109 * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
110 * as they will try to lock adapter a second time.
111 */
112static int pca9541_reg_read(struct i2c_client *client, u8 command)
113{
114 struct i2c_adapter *adap = client->adapter;
Peter Rosina5306b82018-06-20 10:51:55 +0200115 union i2c_smbus_data data;
Guenter Roeckae63b132010-10-24 18:16:58 +0200116 int ret;
Guenter Roeckae63b132010-10-24 18:16:58 +0200117
Peter Rosina5306b82018-06-20 10:51:55 +0200118 ret = __i2c_smbus_xfer(adap, client->addr, client->flags,
119 I2C_SMBUS_READ, command,
120 I2C_SMBUS_BYTE_DATA, &data);
Guenter Roeckae63b132010-10-24 18:16:58 +0200121
Peter Rosina5306b82018-06-20 10:51:55 +0200122 return ret ?: data.byte;
Guenter Roeckae63b132010-10-24 18:16:58 +0200123}
124
125/*
126 * Arbitration management functions
127 */
128
129/* Release bus. Also reset NTESTON and BUSINIT if it was set. */
130static void pca9541_release_bus(struct i2c_client *client)
131{
132 int reg;
133
134 reg = pca9541_reg_read(client, PCA9541_CONTROL);
135 if (reg >= 0 && !busoff(reg) && mybus(reg))
136 pca9541_reg_write(client, PCA9541_CONTROL,
137 (reg & PCA9541_CTL_NBUSON) >> 1);
138}
139
140/*
141 * Arbitration is defined as a two-step process. A bus master can only activate
142 * the slave bus if it owns it; otherwise it has to request ownership first.
143 * This multi-step process ensures that access contention is resolved
144 * gracefully.
145 *
146 * Bus Ownership Other master Action
147 * state requested access
148 * ----------------------------------------------------
149 * off - yes wait for arbitration timeout or
150 * for other master to drop request
151 * off no no take ownership
152 * off yes no turn on bus
153 * on yes - done
154 * on no - wait for arbitration timeout or
155 * for other master to release bus
156 *
157 * The main contention point occurs if the slave bus is off and both masters
158 * request ownership at the same time. In this case, one master will turn on
159 * the slave bus, believing that it owns it. The other master will request
160 * bus ownership. Result is that the bus is turned on, and master which did
161 * _not_ own the slave bus before ends up owning it.
162 */
163
164/* Control commands per PCA9541 datasheet */
165static const u8 pca9541_control[16] = {
166 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
167};
168
169/*
170 * Channel arbitration
171 *
172 * Return values:
173 * <0: error
174 * 0 : bus not acquired
175 * 1 : bus acquired
176 */
177static int pca9541_arbitrate(struct i2c_client *client)
178{
Peter Rosinab88b972016-04-20 08:40:02 +0200179 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
180 struct pca9541 *data = i2c_mux_priv(muxc);
Guenter Roeckae63b132010-10-24 18:16:58 +0200181 int reg;
182
183 reg = pca9541_reg_read(client, PCA9541_CONTROL);
184 if (reg < 0)
185 return reg;
186
187 if (busoff(reg)) {
188 int istat;
189 /*
190 * Bus is off. Request ownership or turn it on unless
191 * other master requested ownership.
192 */
193 istat = pca9541_reg_read(client, PCA9541_ISTAT);
194 if (!(istat & PCA9541_ISTAT_NMYTEST)
195 || time_is_before_eq_jiffies(data->arb_timeout)) {
196 /*
197 * Other master did not request ownership,
198 * or arbitration timeout expired. Take the bus.
199 */
200 pca9541_reg_write(client,
201 PCA9541_CONTROL,
202 pca9541_control[reg & 0x0f]
203 | PCA9541_CTL_NTESTON);
204 data->select_timeout = SELECT_DELAY_SHORT;
205 } else {
206 /*
207 * Other master requested ownership.
208 * Set extra long timeout to give it time to acquire it.
209 */
210 data->select_timeout = SELECT_DELAY_LONG * 2;
211 }
212 } else if (mybus(reg)) {
213 /*
214 * Bus is on, and we own it. We are done with acquisition.
215 * Reset NTESTON and BUSINIT, then return success.
216 */
217 if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
218 pca9541_reg_write(client,
219 PCA9541_CONTROL,
220 reg & ~(PCA9541_CTL_NTESTON
221 | PCA9541_CTL_BUSINIT));
222 return 1;
223 } else {
224 /*
225 * Other master owns the bus.
226 * If arbitration timeout has expired, force ownership.
227 * Otherwise request it.
228 */
229 data->select_timeout = SELECT_DELAY_LONG;
230 if (time_is_before_eq_jiffies(data->arb_timeout)) {
231 /* Time is up, take the bus and reset it. */
232 pca9541_reg_write(client,
233 PCA9541_CONTROL,
234 pca9541_control[reg & 0x0f]
235 | PCA9541_CTL_BUSINIT
236 | PCA9541_CTL_NTESTON);
237 } else {
238 /* Request bus ownership if needed */
239 if (!(reg & PCA9541_CTL_NTESTON))
240 pca9541_reg_write(client,
241 PCA9541_CONTROL,
242 reg | PCA9541_CTL_NTESTON);
243 }
244 }
245 return 0;
246}
247
Peter Rosinab88b972016-04-20 08:40:02 +0200248static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
Guenter Roeckae63b132010-10-24 18:16:58 +0200249{
Peter Rosinab88b972016-04-20 08:40:02 +0200250 struct pca9541 *data = i2c_mux_priv(muxc);
251 struct i2c_client *client = data->client;
Guenter Roeckae63b132010-10-24 18:16:58 +0200252 int ret;
253 unsigned long timeout = jiffies + ARB2_TIMEOUT;
254 /* give up after this time */
255
256 data->arb_timeout = jiffies + ARB_TIMEOUT;
257 /* force bus ownership after this time */
258
259 do {
260 ret = pca9541_arbitrate(client);
261 if (ret)
262 return ret < 0 ? ret : 0;
263
264 if (data->select_timeout == SELECT_DELAY_SHORT)
265 udelay(data->select_timeout);
266 else
267 msleep(data->select_timeout / 1000);
268 } while (time_is_after_eq_jiffies(timeout));
269
270 return -ETIMEDOUT;
271}
272
Peter Rosinab88b972016-04-20 08:40:02 +0200273static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
Guenter Roeckae63b132010-10-24 18:16:58 +0200274{
Peter Rosinab88b972016-04-20 08:40:02 +0200275 struct pca9541 *data = i2c_mux_priv(muxc);
276 struct i2c_client *client = data->client;
277
Guenter Roeckae63b132010-10-24 18:16:58 +0200278 pca9541_release_bus(client);
279 return 0;
280}
281
282/*
283 * I2C init/probing/exit functions
284 */
285static int pca9541_probe(struct i2c_client *client,
286 const struct i2c_device_id *id)
287{
288 struct i2c_adapter *adap = client->adapter;
Peter Rosinab88b972016-04-20 08:40:02 +0200289 struct i2c_mux_core *muxc;
Guenter Roeckae63b132010-10-24 18:16:58 +0200290 struct pca9541 *data;
Peter Rosinab88b972016-04-20 08:40:02 +0200291 int ret;
Guenter Roeckae63b132010-10-24 18:16:58 +0200292
293 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
Peter Rosinab88b972016-04-20 08:40:02 +0200294 return -ENODEV;
Guenter Roeckae63b132010-10-24 18:16:58 +0200295
296 /*
297 * I2C accesses are unprotected here.
Peter Rosinf06c97d2018-06-20 07:17:55 +0200298 * We have to lock the I2C segment before releasing the bus.
Guenter Roeckae63b132010-10-24 18:16:58 +0200299 */
Peter Rosinf06c97d2018-06-20 07:17:55 +0200300 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Guenter Roeckae63b132010-10-24 18:16:58 +0200301 pca9541_release_bus(client);
Peter Rosinf06c97d2018-06-20 07:17:55 +0200302 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Guenter Roeckae63b132010-10-24 18:16:58 +0200303
304 /* Create mux adapter */
305
Peter Rosin408395902016-08-11 16:49:54 +0200306 muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
307 I2C_MUX_ARBITRATOR,
Peter Rosinab88b972016-04-20 08:40:02 +0200308 pca9541_select_chan, pca9541_release_chan);
309 if (!muxc)
310 return -ENOMEM;
Guenter Roeckae63b132010-10-24 18:16:58 +0200311
Peter Rosinab88b972016-04-20 08:40:02 +0200312 data = i2c_mux_priv(muxc);
313 data->client = client;
314
315 i2c_set_clientdata(client, muxc);
316
Robert Shearman590085f2019-02-28 11:43:42 +0000317 ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
Peter Rosin664d9bb2017-04-03 10:14:19 +0200318 if (ret)
Peter Rosinab88b972016-04-20 08:40:02 +0200319 return ret;
Guenter Roeckae63b132010-10-24 18:16:58 +0200320
321 dev_info(&client->dev, "registered master selector for I2C %s\n",
322 client->name);
323
324 return 0;
Guenter Roeckae63b132010-10-24 18:16:58 +0200325}
326
327static int pca9541_remove(struct i2c_client *client)
328{
Peter Rosinab88b972016-04-20 08:40:02 +0200329 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Guenter Roeckae63b132010-10-24 18:16:58 +0200330
Peter Rosinab88b972016-04-20 08:40:02 +0200331 i2c_mux_del_adapters(muxc);
Guenter Roeckae63b132010-10-24 18:16:58 +0200332 return 0;
333}
334
335static struct i2c_driver pca9541_driver = {
336 .driver = {
337 .name = "pca9541",
Peter Rosina1cbf332016-07-09 20:30:58 +0200338 .of_match_table = of_match_ptr(pca9541_of_match),
Guenter Roeckae63b132010-10-24 18:16:58 +0200339 },
340 .probe = pca9541_probe,
341 .remove = pca9541_remove,
342 .id_table = pca9541_id,
343};
344
Axel Linde054972012-03-26 21:47:19 +0200345module_i2c_driver(pca9541_driver);
Guenter Roeckae63b132010-10-24 18:16:58 +0200346
Guenter Roeck83a638d2012-07-24 14:13:56 +0200347MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
Guenter Roeckae63b132010-10-24 18:16:58 +0200348MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
349MODULE_LICENSE("GPL v2");