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/acpi.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/slab.h>
29 #include <linux/sysfs.h>
30 
31 /* multiplexer per channel data */
32 struct i2c_mux_priv {
33 	struct i2c_adapter adap;
34 	struct i2c_algorithm algo;
35 	struct i2c_mux_core *muxc;
36 	u32 chan_id;
37 };
38 
__i2c_mux_master_xfer(struct i2c_adapter * adap,struct i2c_msg msgs[],int num)39 static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
40 				 struct i2c_msg msgs[], int num)
41 {
42 	struct i2c_mux_priv *priv = adap->algo_data;
43 	struct i2c_mux_core *muxc = priv->muxc;
44 	struct i2c_adapter *parent = muxc->parent;
45 	int ret;
46 
47 	/* Switch to the right mux port and perform the transfer. */
48 
49 	ret = muxc->select(muxc, priv->chan_id);
50 	if (ret >= 0)
51 		ret = __i2c_transfer(parent, msgs, num);
52 	if (muxc->deselect)
53 		muxc->deselect(muxc, priv->chan_id);
54 
55 	return ret;
56 }
57 
i2c_mux_master_xfer(struct i2c_adapter * adap,struct i2c_msg msgs[],int num)58 static int 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 
__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)77 static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
78 				u16 addr, unsigned short flags,
79 				char read_write, u8 command,
80 				int size, union i2c_smbus_data *data)
81 {
82 	struct i2c_mux_priv *priv = adap->algo_data;
83 	struct i2c_mux_core *muxc = priv->muxc;
84 	struct i2c_adapter *parent = muxc->parent;
85 	int ret;
86 
87 	/* Select the right mux port and perform the transfer. */
88 
89 	ret = muxc->select(muxc, priv->chan_id);
90 	if (ret >= 0)
91 		ret = __i2c_smbus_xfer(parent, addr, flags,
92 				       read_write, command, size, data);
93 	if (muxc->deselect)
94 		muxc->deselect(muxc, priv->chan_id);
95 
96 	return ret;
97 }
98 
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)99 static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
100 			      u16 addr, unsigned short flags,
101 			      char read_write, u8 command,
102 			      int size, union i2c_smbus_data *data)
103 {
104 	struct i2c_mux_priv *priv = adap->algo_data;
105 	struct i2c_mux_core *muxc = priv->muxc;
106 	struct i2c_adapter *parent = muxc->parent;
107 	int ret;
108 
109 	/* Select the right mux port and perform the transfer. */
110 
111 	ret = muxc->select(muxc, priv->chan_id);
112 	if (ret >= 0)
113 		ret = i2c_smbus_xfer(parent, addr, flags,
114 				     read_write, command, size, data);
115 	if (muxc->deselect)
116 		muxc->deselect(muxc, priv->chan_id);
117 
118 	return ret;
119 }
120 
121 /* Return the parent's functionality */
i2c_mux_functionality(struct i2c_adapter * adap)122 static u32 i2c_mux_functionality(struct i2c_adapter *adap)
123 {
124 	struct i2c_mux_priv *priv = adap->algo_data;
125 	struct i2c_adapter *parent = priv->muxc->parent;
126 
127 	return parent->algo->functionality(parent);
128 }
129 
130 /* Return all parent classes, merged */
i2c_mux_parent_classes(struct i2c_adapter * parent)131 static unsigned int i2c_mux_parent_classes(struct i2c_adapter *parent)
132 {
133 	unsigned int class = 0;
134 
135 	do {
136 		class |= parent->class;
137 		parent = i2c_parent_is_i2c_adapter(parent);
138 	} while (parent);
139 
140 	return class;
141 }
142 
i2c_mux_lock_bus(struct i2c_adapter * adapter,unsigned int flags)143 static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
144 {
145 	struct i2c_mux_priv *priv = adapter->algo_data;
146 	struct i2c_adapter *parent = priv->muxc->parent;
147 
148 	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
149 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
150 		return;
151 	i2c_lock_bus(parent, flags);
152 }
153 
i2c_mux_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)154 static int i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
155 {
156 	struct i2c_mux_priv *priv = adapter->algo_data;
157 	struct i2c_adapter *parent = priv->muxc->parent;
158 
159 	if (!rt_mutex_trylock(&parent->mux_lock))
160 		return 0;	/* mux_lock not locked, failure */
161 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
162 		return 1;	/* we only want mux_lock, success */
163 	if (i2c_trylock_bus(parent, flags))
164 		return 1;	/* parent locked too, success */
165 	rt_mutex_unlock(&parent->mux_lock);
166 	return 0;		/* parent not locked, failure */
167 }
168 
i2c_mux_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)169 static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
170 {
171 	struct i2c_mux_priv *priv = adapter->algo_data;
172 	struct i2c_adapter *parent = priv->muxc->parent;
173 
174 	if (flags & I2C_LOCK_ROOT_ADAPTER)
175 		i2c_unlock_bus(parent, flags);
176 	rt_mutex_unlock(&parent->mux_lock);
177 }
178 
i2c_parent_lock_bus(struct i2c_adapter * adapter,unsigned int flags)179 static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
180 				unsigned int flags)
181 {
182 	struct i2c_mux_priv *priv = adapter->algo_data;
183 	struct i2c_adapter *parent = priv->muxc->parent;
184 
185 	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
186 	i2c_lock_bus(parent, flags);
187 }
188 
i2c_parent_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)189 static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
190 				  unsigned int flags)
191 {
192 	struct i2c_mux_priv *priv = adapter->algo_data;
193 	struct i2c_adapter *parent = priv->muxc->parent;
194 
195 	if (!rt_mutex_trylock(&parent->mux_lock))
196 		return 0;	/* mux_lock not locked, failure */
197 	if (i2c_trylock_bus(parent, flags))
198 		return 1;	/* parent locked too, success */
199 	rt_mutex_unlock(&parent->mux_lock);
200 	return 0;		/* parent not locked, failure */
201 }
202 
i2c_parent_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)203 static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
204 				  unsigned int flags)
205 {
206 	struct i2c_mux_priv *priv = adapter->algo_data;
207 	struct i2c_adapter *parent = priv->muxc->parent;
208 
209 	i2c_unlock_bus(parent, flags);
210 	rt_mutex_unlock(&parent->mux_lock);
211 }
212 
i2c_root_adapter(struct device * dev)213 struct i2c_adapter *i2c_root_adapter(struct device *dev)
214 {
215 	struct device *i2c;
216 	struct i2c_adapter *i2c_root;
217 
218 	/*
219 	 * Walk up the device tree to find an i2c adapter, indicating
220 	 * that this is an i2c client device. Check all ancestors to
221 	 * handle mfd devices etc.
222 	 */
223 	for (i2c = dev; i2c; i2c = i2c->parent) {
224 		if (i2c->type == &i2c_adapter_type)
225 			break;
226 	}
227 	if (!i2c)
228 		return NULL;
229 
230 	/* Continue up the tree to find the root i2c adapter */
231 	i2c_root = to_i2c_adapter(i2c);
232 	while (i2c_parent_is_i2c_adapter(i2c_root))
233 		i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
234 
235 	return i2c_root;
236 }
237 EXPORT_SYMBOL_GPL(i2c_root_adapter);
238 
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))239 struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
240 				   struct device *dev, int max_adapters,
241 				   int sizeof_priv, u32 flags,
242 				   int (*select)(struct i2c_mux_core *, u32),
243 				   int (*deselect)(struct i2c_mux_core *, u32))
244 {
245 	struct i2c_mux_core *muxc;
246 	size_t mux_size;
247 
248 	mux_size = struct_size(muxc, adapter, max_adapters);
249 	muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
250 	if (!muxc)
251 		return NULL;
252 	if (sizeof_priv)
253 		muxc->priv = &muxc->adapter[max_adapters];
254 
255 	muxc->parent = parent;
256 	muxc->dev = dev;
257 	if (flags & I2C_MUX_LOCKED)
258 		muxc->mux_locked = true;
259 	if (flags & I2C_MUX_ARBITRATOR)
260 		muxc->arbitrator = true;
261 	if (flags & I2C_MUX_GATE)
262 		muxc->gate = true;
263 	muxc->select = select;
264 	muxc->deselect = deselect;
265 	muxc->max_adapters = max_adapters;
266 
267 	return muxc;
268 }
269 EXPORT_SYMBOL_GPL(i2c_mux_alloc);
270 
271 static const struct i2c_lock_operations i2c_mux_lock_ops = {
272 	.lock_bus =    i2c_mux_lock_bus,
273 	.trylock_bus = i2c_mux_trylock_bus,
274 	.unlock_bus =  i2c_mux_unlock_bus,
275 };
276 
277 static const struct i2c_lock_operations i2c_parent_lock_ops = {
278 	.lock_bus =    i2c_parent_lock_bus,
279 	.trylock_bus = i2c_parent_trylock_bus,
280 	.unlock_bus =  i2c_parent_unlock_bus,
281 };
282 
i2c_mux_add_adapter(struct i2c_mux_core * muxc,u32 force_nr,u32 chan_id,unsigned int class)283 int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
284 			u32 force_nr, u32 chan_id,
285 			unsigned int class)
286 {
287 	struct i2c_adapter *parent = muxc->parent;
288 	struct i2c_mux_priv *priv;
289 	char symlink_name[20];
290 	int ret;
291 
292 	if (muxc->num_adapters >= muxc->max_adapters) {
293 		dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
294 		return -EINVAL;
295 	}
296 
297 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
298 	if (!priv)
299 		return -ENOMEM;
300 
301 	/* Set up private adapter data */
302 	priv->muxc = muxc;
303 	priv->chan_id = chan_id;
304 
305 	/* Need to do algo dynamically because we don't know ahead
306 	 * of time what sort of physical adapter we'll be dealing with.
307 	 */
308 	if (parent->algo->master_xfer) {
309 		if (muxc->mux_locked)
310 			priv->algo.master_xfer = i2c_mux_master_xfer;
311 		else
312 			priv->algo.master_xfer = __i2c_mux_master_xfer;
313 	}
314 	if (parent->algo->master_xfer_atomic)
315 		priv->algo.master_xfer_atomic = priv->algo.master_xfer;
316 
317 	if (parent->algo->smbus_xfer) {
318 		if (muxc->mux_locked)
319 			priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
320 		else
321 			priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
322 	}
323 	if (parent->algo->smbus_xfer_atomic)
324 		priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer;
325 
326 	priv->algo.functionality = i2c_mux_functionality;
327 
328 	/* Now fill out new adapter structure */
329 	snprintf(priv->adap.name, sizeof(priv->adap.name),
330 		 "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
331 	priv->adap.owner = THIS_MODULE;
332 	priv->adap.algo = &priv->algo;
333 	priv->adap.algo_data = priv;
334 	priv->adap.dev.parent = &parent->dev;
335 	priv->adap.retries = parent->retries;
336 	priv->adap.timeout = parent->timeout;
337 	priv->adap.quirks = parent->quirks;
338 	if (muxc->mux_locked)
339 		priv->adap.lock_ops = &i2c_mux_lock_ops;
340 	else
341 		priv->adap.lock_ops = &i2c_parent_lock_ops;
342 
343 	/* Sanity check on class */
344 	if (i2c_mux_parent_classes(parent) & class)
345 		dev_err(&parent->dev,
346 			"Segment %d behind mux can't share classes with ancestors\n",
347 			chan_id);
348 	else
349 		priv->adap.class = class;
350 
351 	/*
352 	 * Try to populate the mux adapter's of_node, expands to
353 	 * nothing if !CONFIG_OF.
354 	 */
355 	if (muxc->dev->of_node) {
356 		struct device_node *dev_node = muxc->dev->of_node;
357 		struct device_node *mux_node, *child = NULL;
358 		u32 reg;
359 
360 		if (muxc->arbitrator)
361 			mux_node = of_get_child_by_name(dev_node, "i2c-arb");
362 		else if (muxc->gate)
363 			mux_node = of_get_child_by_name(dev_node, "i2c-gate");
364 		else
365 			mux_node = of_get_child_by_name(dev_node, "i2c-mux");
366 
367 		if (mux_node) {
368 			/* A "reg" property indicates an old-style DT entry */
369 			if (!of_property_read_u32(mux_node, "reg", &reg)) {
370 				of_node_put(mux_node);
371 				mux_node = NULL;
372 			}
373 		}
374 
375 		if (!mux_node)
376 			mux_node = of_node_get(dev_node);
377 		else if (muxc->arbitrator || muxc->gate)
378 			child = of_node_get(mux_node);
379 
380 		if (!child) {
381 			for_each_child_of_node(mux_node, child) {
382 				ret = of_property_read_u32(child, "reg", &reg);
383 				if (ret)
384 					continue;
385 				if (chan_id == reg)
386 					break;
387 			}
388 		}
389 
390 		priv->adap.dev.of_node = child;
391 		of_node_put(mux_node);
392 	}
393 
394 	/*
395 	 * Associate the mux channel with an ACPI node.
396 	 */
397 	if (has_acpi_companion(muxc->dev))
398 		acpi_preset_companion(&priv->adap.dev,
399 				      ACPI_COMPANION(muxc->dev),
400 				      chan_id);
401 
402 	if (force_nr) {
403 		priv->adap.nr = force_nr;
404 		ret = i2c_add_numbered_adapter(&priv->adap);
405 		if (ret < 0) {
406 			dev_err(&parent->dev,
407 				"failed to add mux-adapter %u as bus %u (error=%d)\n",
408 				chan_id, force_nr, ret);
409 			goto err_free_priv;
410 		}
411 	} else {
412 		ret = i2c_add_adapter(&priv->adap);
413 		if (ret < 0) {
414 			dev_err(&parent->dev,
415 				"failed to add mux-adapter %u (error=%d)\n",
416 				chan_id, ret);
417 			goto err_free_priv;
418 		}
419 	}
420 
421 	WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
422 			       "mux_device"),
423 	     "can't create symlink to mux device\n");
424 
425 	snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
426 	WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
427 			       symlink_name),
428 	     "can't create symlink to channel %u\n", chan_id);
429 	dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
430 		 i2c_adapter_id(&priv->adap));
431 
432 	muxc->adapter[muxc->num_adapters++] = &priv->adap;
433 	return 0;
434 
435 err_free_priv:
436 	kfree(priv);
437 	return ret;
438 }
439 EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
440 
i2c_mux_del_adapters(struct i2c_mux_core * muxc)441 void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
442 {
443 	char symlink_name[20];
444 
445 	while (muxc->num_adapters) {
446 		struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
447 		struct i2c_mux_priv *priv = adap->algo_data;
448 		struct device_node *np = adap->dev.of_node;
449 
450 		muxc->adapter[muxc->num_adapters] = NULL;
451 
452 		snprintf(symlink_name, sizeof(symlink_name),
453 			 "channel-%u", priv->chan_id);
454 		sysfs_remove_link(&muxc->dev->kobj, symlink_name);
455 
456 		sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
457 		i2c_del_adapter(adap);
458 		of_node_put(np);
459 		kfree(priv);
460 	}
461 }
462 EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
463 
464 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
465 MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
466 MODULE_LICENSE("GPL v2");
467