1 /*
2  * Multiplexed I2C bus driver.
3  *
4  * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5  * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
6  * Copyright (c) 2009-2010 NSN GmbH & Co KG <michael.lawnick.ext@nsn.com>
7  *
8  * Simplifies access to complex multiplexed I2C bus topologies, by presenting
9  * each multiplexed bus segment as an additional I2C adapter.
10  * Supports multi-level mux'ing (mux behind a mux).
11  *
12  * Based on:
13  *	i2c-virt.c from Kumar Gala <galak@kernel.crashing.org>
14  *	i2c-virtual.c from Ken Harrenstien, Copyright (c) 2004 Google, Inc.
15  *	i2c-virtual.c from Brian Kuschak <bkuschak@yahoo.com>
16  *
17  * This file is licensed under the terms of the GNU General Public
18  * License version 2. This program is licensed "as is" without any
19  * warranty of any kind, whether express or implied.
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-mux.h>
27 #include <linux/of.h>
28 
29 /* multiplexer per channel data */
30 struct i2c_mux_priv {
31 	struct i2c_adapter adap;
32 	struct i2c_algorithm algo;
33 	struct i2c_mux_core *muxc;
34 	u32	chan_id;
35 };
36 
37 static int
__i2c_mux_master_xfer(struct i2c_adapter * adap,struct i2c_msg msgs[],int num)38 __i2c_mux_master_xfer(struct i2c_adapter *adap,
39     struct i2c_msg msgs[], int num)
40 {
41 	struct i2c_mux_priv *priv = adap->algo_data;
42 	struct i2c_mux_core *muxc = priv->muxc;
43 	struct i2c_adapter *parent = muxc->parent;
44 	int ret;
45 
46 	/* Switch to the right mux port and perform the transfer. */
47 
48 	ret = muxc->select(muxc, priv->chan_id);
49 	if (ret >= 0)
50 		ret = __i2c_transfer(parent, msgs, num);
51 	if (muxc->deselect)
52 		muxc->deselect(muxc, priv->chan_id);
53 
54 	return ret;
55 }
56 
57 static int
i2c_mux_master_xfer(struct i2c_adapter * adap,struct i2c_msg msgs[],int num)58 i2c_mux_master_xfer(struct i2c_adapter *adap,
59     struct i2c_msg msgs[], int num)
60 {
61 	struct i2c_mux_priv *priv = adap->algo_data;
62 	struct i2c_mux_core *muxc = priv->muxc;
63 	struct i2c_adapter *parent = muxc->parent;
64 	int ret;
65 
66 	/* Switch to the right mux port and perform the transfer. */
67 
68 	ret = muxc->select(muxc, priv->chan_id);
69 	if (ret >= 0)
70 		ret = i2c_transfer(parent, msgs, num);
71 	if (muxc->deselect)
72 		muxc->deselect(muxc, priv->chan_id);
73 
74 	return ret;
75 }
76 
77 static int
__i2c_mux_smbus_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)78 __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
79     u16 addr, unsigned short flags,
80     char read_write, u8 command,
81     int size, union i2c_smbus_data *data)
82 {
83 	struct i2c_mux_priv *priv = adap->algo_data;
84 	struct i2c_mux_core *muxc = priv->muxc;
85 	struct i2c_adapter *parent = muxc->parent;
86 	int ret;
87 
88 	/* Select the right mux port and perform the transfer. */
89 
90 	ret = muxc->select(muxc, priv->chan_id);
91 	if (ret >= 0)
92 		ret = parent->algo->smbus_xfer(parent, addr, flags,
93 		    read_write, command, size, data);
94 	if (muxc->deselect)
95 		muxc->deselect(muxc, priv->chan_id);
96 
97 	return ret;
98 }
99 
100 static int
i2c_mux_smbus_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)101 i2c_mux_smbus_xfer(struct i2c_adapter *adap,
102     u16 addr, unsigned short flags,
103     char read_write, u8 command,
104     int size, union i2c_smbus_data *data)
105 {
106 	struct i2c_mux_priv *priv = adap->algo_data;
107 	struct i2c_mux_core *muxc = priv->muxc;
108 	struct i2c_adapter *parent = muxc->parent;
109 	int ret;
110 
111 	/* Select the right mux port and perform the transfer. */
112 
113 	ret = muxc->select(muxc, priv->chan_id);
114 	if (ret >= 0)
115 		ret = i2c_smbus_xfer(parent, addr, flags,
116 		    read_write, command, size, data);
117 	if (muxc->deselect)
118 		muxc->deselect(muxc, priv->chan_id);
119 
120 	return ret;
121 }
122 
123 /* Return the parent's functionality */
124 static	u32
i2c_mux_functionality(struct i2c_adapter * adap)125 i2c_mux_functionality(struct i2c_adapter *adap)
126 {
127 	struct i2c_mux_priv *priv = adap->algo_data;
128 	struct i2c_adapter *parent = priv->muxc->parent;
129 
130 	return parent->algo->functionality(parent);
131 }
132 
133 /* Return all parent classes, merged */
134 static unsigned int
i2c_mux_parent_classes(struct i2c_adapter * parent)135 i2c_mux_parent_classes(struct i2c_adapter *parent)
136 {
137 	unsigned int class = 0;
138 
139 	do {
140 		class |= parent->class;
141 		parent = i2c_parent_is_i2c_adapter(parent);
142 	} while (parent);
143 
144 	return class;
145 }
146 
147 static void
i2c_mux_lock_bus(struct i2c_adapter * adapter,unsigned int flags)148 i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
149 {
150 	struct i2c_mux_priv *priv = adapter->algo_data;
151 	struct i2c_adapter *parent = priv->muxc->parent;
152 
153 	rt_mutex_lock(&parent->mux_lock);
154 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
155 		return;
156 	i2c_lock_bus(parent, flags);
157 }
158 
159 static int
i2c_mux_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)160 i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
161 {
162 	struct i2c_mux_priv *priv = adapter->algo_data;
163 	struct i2c_adapter *parent = priv->muxc->parent;
164 
165 	if (!rt_mutex_trylock(&parent->mux_lock))
166 		return 0;		/* mux_lock not locked, failure */
167 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
168 		return 1;		/* we only want mux_lock, success */
169 	if (parent->trylock_bus(parent, flags))
170 		return 1;		/* parent locked too, success */
171 	rt_mutex_unlock(&parent->mux_lock);
172 	return 0;			/* parent not locked, failure */
173 }
174 
175 static void
i2c_mux_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)176 i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
177 {
178 	struct i2c_mux_priv *priv = adapter->algo_data;
179 	struct i2c_adapter *parent = priv->muxc->parent;
180 
181 	if (flags & I2C_LOCK_ROOT_ADAPTER)
182 		i2c_unlock_bus(parent, flags);
183 	rt_mutex_unlock(&parent->mux_lock);
184 }
185 
186 static void
i2c_parent_lock_bus(struct i2c_adapter * adapter,unsigned int flags)187 i2c_parent_lock_bus(struct i2c_adapter *adapter,
188     unsigned int flags)
189 {
190 	struct i2c_mux_priv *priv = adapter->algo_data;
191 	struct i2c_adapter *parent = priv->muxc->parent;
192 
193 	rt_mutex_lock(&parent->mux_lock);
194 	i2c_lock_bus(parent, flags);
195 }
196 
197 static int
i2c_parent_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)198 i2c_parent_trylock_bus(struct i2c_adapter *adapter,
199     unsigned int flags)
200 {
201 	struct i2c_mux_priv *priv = adapter->algo_data;
202 	struct i2c_adapter *parent = priv->muxc->parent;
203 
204 	if (!rt_mutex_trylock(&parent->mux_lock))
205 		return 0;		/* mux_lock not locked, failure */
206 	if (parent->trylock_bus(parent, flags))
207 		return 1;		/* parent locked too, success */
208 	rt_mutex_unlock(&parent->mux_lock);
209 	return 0;			/* parent not locked, failure */
210 }
211 
212 static void
i2c_parent_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)213 i2c_parent_unlock_bus(struct i2c_adapter *adapter,
214     unsigned int flags)
215 {
216 	struct i2c_mux_priv *priv = adapter->algo_data;
217 	struct i2c_adapter *parent = priv->muxc->parent;
218 
219 	i2c_unlock_bus(parent, flags);
220 	rt_mutex_unlock(&parent->mux_lock);
221 }
222 
223 struct i2c_adapter *
i2c_root_adapter(struct device * dev)224 i2c_root_adapter(struct device *dev)
225 {
226 	struct device *i2c;
227 	struct i2c_adapter *i2c_root;
228 
229 	/*
230 	 * Walk up the device tree to find an i2c adapter, indicating
231 	 * that this is an i2c client device. Check all ancestors to
232 	 * handle mfd devices etc.
233 	 */
234 	for (i2c = dev; i2c; i2c = i2c->parent) {
235 		if (i2c->type == &i2c_adapter_type)
236 			break;
237 	}
238 	if (!i2c)
239 		return NULL;
240 
241 	/* Continue up the tree to find the root i2c adapter */
242 	i2c_root = to_i2c_adapter(i2c);
243 	while (i2c_parent_is_i2c_adapter(i2c_root))
244 		i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
245 
246 	return i2c_root;
247 }
248 EXPORT_SYMBOL_GPL(i2c_root_adapter);
249 
250 struct i2c_mux_core *
i2c_mux_alloc(struct i2c_adapter * parent,struct device * dev,int max_adapters,int sizeof_priv,u32 flags,int (* select)(struct i2c_mux_core *,u32),int (* deselect)(struct i2c_mux_core *,u32))251 i2c_mux_alloc(struct i2c_adapter *parent,
252     struct device *dev, int max_adapters,
253     int sizeof_priv, u32 flags,
254     int (*select) (struct i2c_mux_core *, u32),
255     int (*deselect) (struct i2c_mux_core *, u32))
256 {
257 	struct i2c_mux_core *muxc;
258 
259 	muxc = devm_kzalloc(dev, sizeof(*muxc)
260 	    + max_adapters * sizeof(muxc->adapter[0])
261 	    + sizeof_priv, GFP_KERNEL);
262 	if (!muxc)
263 		return NULL;
264 	if (sizeof_priv)
265 		muxc->priv = &muxc->adapter[max_adapters];
266 
267 	muxc->parent = parent;
268 	muxc->dev = dev;
269 	if (flags & I2C_MUX_LOCKED)
270 		muxc->mux_locked = true;
271 	muxc->select = select;
272 	muxc->deselect = deselect;
273 	muxc->max_adapters = max_adapters;
274 
275 	return muxc;
276 }
277 EXPORT_SYMBOL_GPL(i2c_mux_alloc);
278 
279 int
i2c_mux_add_adapter(struct i2c_mux_core * muxc,u32 force_nr,u32 chan_id,unsigned int class)280 i2c_mux_add_adapter(struct i2c_mux_core *muxc,
281     u32 force_nr, u32 chan_id,
282     unsigned int class)
283 {
284 	struct i2c_adapter *parent = muxc->parent;
285 	struct i2c_mux_priv *priv;
286 	int ret;
287 
288 	if (muxc->num_adapters >= muxc->max_adapters) {
289 		dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
290 		return -EINVAL;
291 	}
292 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
293 	if (!priv)
294 		return -ENOMEM;
295 
296 	/* Set up private adapter data */
297 	priv->muxc = muxc;
298 	priv->chan_id = chan_id;
299 
300 	/*
301 	 * Need to do algo dynamically because we don't know ahead of time
302 	 * what sort of physical adapter we'll be dealing with.
303 	 */
304 	if (parent->algo->master_xfer) {
305 		if (muxc->mux_locked)
306 			priv->algo.master_xfer = i2c_mux_master_xfer;
307 		else
308 			priv->algo.master_xfer = __i2c_mux_master_xfer;
309 	}
310 	if (parent->algo->smbus_xfer) {
311 		if (muxc->mux_locked)
312 			priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
313 		else
314 			priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
315 	}
316 	priv->algo.functionality = i2c_mux_functionality;
317 
318 	/* Now fill out new adapter structure */
319 	snprintf(priv->adap.name, sizeof(priv->adap.name),
320 	    "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
321 	priv->adap.owner = THIS_MODULE;
322 	priv->adap.algo = &priv->algo;
323 	priv->adap.algo_data = priv;
324 	priv->adap.dev.parent = &parent->dev;
325 	priv->adap.retries = parent->retries;
326 	priv->adap.timeout = parent->timeout;
327 	if (muxc->mux_locked) {
328 		priv->adap.lock_bus = i2c_mux_lock_bus;
329 		priv->adap.trylock_bus = i2c_mux_trylock_bus;
330 		priv->adap.unlock_bus = i2c_mux_unlock_bus;
331 	} else {
332 		priv->adap.lock_bus = i2c_parent_lock_bus;
333 		priv->adap.trylock_bus = i2c_parent_trylock_bus;
334 		priv->adap.unlock_bus = i2c_parent_unlock_bus;
335 	}
336 
337 	/* Sanity check on class */
338 	if (i2c_mux_parent_classes(parent) & class)
339 		dev_err(&parent->dev,
340 		    "Segment %d behind mux can't share classes with ancestors\n",
341 		    chan_id);
342 	else
343 		priv->adap.class = class;
344 
345 	if (force_nr) {
346 		priv->adap.nr = force_nr;
347 		ret = i2c_add_numbered_adapter(&priv->adap);
348 	} else {
349 		ret = i2c_add_adapter(&priv->adap);
350 	}
351 	if (ret < 0) {
352 		dev_err(&parent->dev,
353 		    "failed to add mux-adapter (error=%d)\n",
354 		    ret);
355 		kfree(priv);
356 		return ret;
357 	}
358 	dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
359 	    i2c_adapter_id(&priv->adap));
360 
361 	muxc->adapter[muxc->num_adapters++] = &priv->adap;
362 	return 0;
363 }
364 EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
365 
366 void
i2c_mux_del_adapters(struct i2c_mux_core * muxc)367 i2c_mux_del_adapters(struct i2c_mux_core *muxc)
368 {
369 	while (muxc->num_adapters) {
370 		struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
371 		struct i2c_mux_priv *priv = adap->algo_data;
372 
373 		muxc->adapter[muxc->num_adapters] = NULL;
374 
375 		i2c_del_adapter(adap);
376 		kfree(priv);
377 	}
378 }
379 EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
380 
381 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
382 MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
383 MODULE_LICENSE("GPL v2");
384