| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i2c-core.c - a device driver for the iic-bus interface		     */ | 
 | 2 | /* ------------------------------------------------------------------------- */ | 
 | 3 | /*   Copyright (C) 1995-99 Simon G. Vogl | 
 | 4 |  | 
 | 5 |     This program is free software; you can redistribute it and/or modify | 
 | 6 |     it under the terms of the GNU General Public License as published by | 
 | 7 |     the Free Software Foundation; either version 2 of the License, or | 
 | 8 |     (at your option) any later version. | 
 | 9 |  | 
 | 10 |     This program is distributed in the hope that it will be useful, | 
 | 11 |     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 12 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 13 |     GNU General Public License for more details. | 
 | 14 |  | 
 | 15 |     You should have received a copy of the GNU General Public License | 
 | 16 |     along with this program; if not, write to the Free Software | 
| Jean Delvare | 5694f8a | 2012-03-26 21:47:19 +0200 | [diff] [blame] | 17 |     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
 | 18 |     MA 02110-1301 USA.							     */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /* ------------------------------------------------------------------------- */ | 
 | 20 |  | 
| Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 21 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |    All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 23 |    SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 24 |    Jean Delvare <khali@linux-fr.org> | 
 | 25 |    Mux support by Rodolfo Giometti <giometti@enneenne.com> and | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 26 |    Michael Lawnick <michael.lawnick.ext@nsn.com> | 
 | 27 |    OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de> | 
 | 28 |    (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and | 
 | 29 |    (c) 2013  Wolfram Sang <wsa@the-dreams.de> | 
 | 30 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/module.h> | 
 | 33 | #include <linux/kernel.h> | 
| Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 34 | #include <linux/delay.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/errno.h> | 
| Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 36 | #include <linux/gpio.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/slab.h> | 
 | 38 | #include <linux/i2c.h> | 
 | 39 | #include <linux/init.h> | 
 | 40 | #include <linux/idr.h> | 
| Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 41 | #include <linux/mutex.h> | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 42 | #include <linux/of.h> | 
| Grant Likely | 959e85f | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 43 | #include <linux/of_device.h> | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 44 | #include <linux/of_irq.h> | 
| Jean Delvare | b8d6f45 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 45 | #include <linux/completion.h> | 
| Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 46 | #include <linux/hardirq.h> | 
 | 47 | #include <linux/irqflags.h> | 
| Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 48 | #include <linux/rwsem.h> | 
| Mark Brown | 6de468a | 2010-03-02 12:23:46 +0100 | [diff] [blame] | 49 | #include <linux/pm_runtime.h> | 
| Mika Westerberg | 907ddf8 | 2012-11-23 12:23:40 +0100 | [diff] [blame] | 50 | #include <linux/acpi.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <asm/uaccess.h> | 
 | 52 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 53 | #include "i2c-core.h" | 
 | 54 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
| Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 56 | /* core_lock protects i2c_adapter_idr, and guarantees | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 57 |    that device detection, deletion of detected devices, and attach_adapter | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 58 |    calls are serialized */ | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 59 | static DEFINE_MUTEX(core_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static DEFINE_IDR(i2c_adapter_idr); | 
 | 61 |  | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 62 | static struct device_type i2c_client_type; | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 63 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 64 |  | 
 | 65 | /* ------------------------------------------------------------------------- */ | 
 | 66 |  | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 67 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, | 
 | 68 | 						const struct i2c_client *client) | 
 | 69 | { | 
 | 70 | 	while (id->name[0]) { | 
 | 71 | 		if (strcmp(client->name, id->name) == 0) | 
 | 72 | 			return id; | 
 | 73 | 		id++; | 
 | 74 | 	} | 
 | 75 | 	return NULL; | 
 | 76 | } | 
 | 77 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static int i2c_device_match(struct device *dev, struct device_driver *drv) | 
 | 79 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 80 | 	struct i2c_client	*client = i2c_verify_client(dev); | 
 | 81 | 	struct i2c_driver	*driver; | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 82 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 83 | 	if (!client) | 
 | 84 | 		return 0; | 
 | 85 |  | 
| Grant Likely | 959e85f | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 86 | 	/* Attempt an OF style match */ | 
 | 87 | 	if (of_driver_match_device(dev, drv)) | 
 | 88 | 		return 1; | 
 | 89 |  | 
| Mika Westerberg | 907ddf8 | 2012-11-23 12:23:40 +0100 | [diff] [blame] | 90 | 	/* Then ACPI style match */ | 
 | 91 | 	if (acpi_driver_match_device(dev, drv)) | 
 | 92 | 		return 1; | 
 | 93 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 94 | 	driver = to_i2c_driver(drv); | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 95 | 	/* match on an id table if there is one */ | 
 | 96 | 	if (driver->id_table) | 
 | 97 | 		return i2c_match_id(driver->id_table, client) != NULL; | 
 | 98 |  | 
| Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 99 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } | 
 | 101 |  | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 102 |  | 
 | 103 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 104 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 105 | { | 
 | 106 | 	struct i2c_client	*client = to_i2c_client(dev); | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 107 |  | 
| Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 108 | 	if (add_uevent_var(env, "MODALIAS=%s%s", | 
 | 109 | 			   I2C_MODULE_PREFIX, client->name)) | 
 | 110 | 		return -ENOMEM; | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 111 | 	dev_dbg(dev, "uevent\n"); | 
 | 112 | 	return 0; | 
 | 113 | } | 
 | 114 |  | 
| Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 115 | /* i2c bus recovery routines */ | 
 | 116 | static int get_scl_gpio_value(struct i2c_adapter *adap) | 
 | 117 | { | 
 | 118 | 	return gpio_get_value(adap->bus_recovery_info->scl_gpio); | 
 | 119 | } | 
 | 120 |  | 
 | 121 | static void set_scl_gpio_value(struct i2c_adapter *adap, int val) | 
 | 122 | { | 
 | 123 | 	gpio_set_value(adap->bus_recovery_info->scl_gpio, val); | 
 | 124 | } | 
 | 125 |  | 
 | 126 | static int get_sda_gpio_value(struct i2c_adapter *adap) | 
 | 127 | { | 
 | 128 | 	return gpio_get_value(adap->bus_recovery_info->sda_gpio); | 
 | 129 | } | 
 | 130 |  | 
 | 131 | static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) | 
 | 132 | { | 
 | 133 | 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | 
 | 134 | 	struct device *dev = &adap->dev; | 
 | 135 | 	int ret = 0; | 
 | 136 |  | 
 | 137 | 	ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN | | 
 | 138 | 			GPIOF_OUT_INIT_HIGH, "i2c-scl"); | 
 | 139 | 	if (ret) { | 
 | 140 | 		dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); | 
 | 141 | 		return ret; | 
 | 142 | 	} | 
 | 143 |  | 
 | 144 | 	if (bri->get_sda) { | 
 | 145 | 		if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { | 
 | 146 | 			/* work without SDA polling */ | 
 | 147 | 			dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", | 
 | 148 | 					bri->sda_gpio); | 
 | 149 | 			bri->get_sda = NULL; | 
 | 150 | 		} | 
 | 151 | 	} | 
 | 152 |  | 
 | 153 | 	return ret; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) | 
 | 157 | { | 
 | 158 | 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | 
 | 159 |  | 
 | 160 | 	if (bri->get_sda) | 
 | 161 | 		gpio_free(bri->sda_gpio); | 
 | 162 |  | 
 | 163 | 	gpio_free(bri->scl_gpio); | 
 | 164 | } | 
 | 165 |  | 
 | 166 | /* | 
 | 167 |  * We are generating clock pulses. ndelay() determines durating of clk pulses. | 
 | 168 |  * We will generate clock with rate 100 KHz and so duration of both clock levels | 
 | 169 |  * is: delay in ns = (10^6 / 100) / 2 | 
 | 170 |  */ | 
 | 171 | #define RECOVERY_NDELAY		5000 | 
 | 172 | #define RECOVERY_CLK_CNT	9 | 
 | 173 |  | 
 | 174 | static int i2c_generic_recovery(struct i2c_adapter *adap) | 
 | 175 | { | 
 | 176 | 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | 
 | 177 | 	int i = 0, val = 1, ret = 0; | 
 | 178 |  | 
 | 179 | 	if (bri->prepare_recovery) | 
 | 180 | 		bri->prepare_recovery(bri); | 
 | 181 |  | 
 | 182 | 	/* | 
 | 183 | 	 * By this time SCL is high, as we need to give 9 falling-rising edges | 
 | 184 | 	 */ | 
 | 185 | 	while (i++ < RECOVERY_CLK_CNT * 2) { | 
 | 186 | 		if (val) { | 
 | 187 | 			/* Break if SDA is high */ | 
 | 188 | 			if (bri->get_sda && bri->get_sda(adap)) | 
 | 189 | 					break; | 
 | 190 | 			/* SCL shouldn't be low here */ | 
 | 191 | 			if (!bri->get_scl(adap)) { | 
 | 192 | 				dev_err(&adap->dev, | 
 | 193 | 					"SCL is stuck low, exit recovery\n"); | 
 | 194 | 				ret = -EBUSY; | 
 | 195 | 				break; | 
 | 196 | 			} | 
 | 197 | 		} | 
 | 198 |  | 
 | 199 | 		val = !val; | 
 | 200 | 		bri->set_scl(adap, val); | 
 | 201 | 		ndelay(RECOVERY_NDELAY); | 
 | 202 | 	} | 
 | 203 |  | 
 | 204 | 	if (bri->unprepare_recovery) | 
 | 205 | 		bri->unprepare_recovery(bri); | 
 | 206 |  | 
 | 207 | 	return ret; | 
 | 208 | } | 
 | 209 |  | 
 | 210 | int i2c_generic_scl_recovery(struct i2c_adapter *adap) | 
 | 211 | { | 
 | 212 | 	adap->bus_recovery_info->set_scl(adap, 1); | 
 | 213 | 	return i2c_generic_recovery(adap); | 
 | 214 | } | 
 | 215 |  | 
 | 216 | int i2c_generic_gpio_recovery(struct i2c_adapter *adap) | 
 | 217 | { | 
 | 218 | 	int ret; | 
 | 219 |  | 
 | 220 | 	ret = i2c_get_gpios_for_recovery(adap); | 
 | 221 | 	if (ret) | 
 | 222 | 		return ret; | 
 | 223 |  | 
 | 224 | 	ret = i2c_generic_recovery(adap); | 
 | 225 | 	i2c_put_gpios_for_recovery(adap); | 
 | 226 |  | 
 | 227 | 	return ret; | 
 | 228 | } | 
 | 229 |  | 
 | 230 | int i2c_recover_bus(struct i2c_adapter *adap) | 
 | 231 | { | 
 | 232 | 	if (!adap->bus_recovery_info) | 
 | 233 | 		return -EOPNOTSUPP; | 
 | 234 |  | 
 | 235 | 	dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); | 
 | 236 | 	return adap->bus_recovery_info->recover_bus(adap); | 
 | 237 | } | 
 | 238 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | static int i2c_device_probe(struct device *dev) | 
 | 240 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 241 | 	struct i2c_client	*client = i2c_verify_client(dev); | 
 | 242 | 	struct i2c_driver	*driver; | 
| Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 243 | 	int status; | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 244 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 245 | 	if (!client) | 
 | 246 | 		return 0; | 
 | 247 |  | 
 | 248 | 	driver = to_i2c_driver(dev->driver); | 
| Jean Delvare | e045744 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 249 | 	if (!driver->probe || !driver->id_table) | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 250 | 		return -ENODEV; | 
 | 251 | 	client->driver = driver; | 
| Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 252 | 	if (!device_can_wakeup(&client->dev)) | 
 | 253 | 		device_init_wakeup(&client->dev, | 
 | 254 | 					client->flags & I2C_CLIENT_WAKE); | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 255 | 	dev_dbg(dev, "probe\n"); | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 256 |  | 
| Jean Delvare | e045744 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 257 | 	status = driver->probe(client, i2c_match_id(driver->id_table, client)); | 
| Wolfram Sang | e4a7b9b | 2010-05-04 11:09:27 +0200 | [diff] [blame] | 258 | 	if (status) { | 
| Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 259 | 		client->driver = NULL; | 
| Wolfram Sang | e4a7b9b | 2010-05-04 11:09:27 +0200 | [diff] [blame] | 260 | 		i2c_set_clientdata(client, NULL); | 
 | 261 | 	} | 
| Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 262 | 	return status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } | 
 | 264 |  | 
 | 265 | static int i2c_device_remove(struct device *dev) | 
 | 266 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 267 | 	struct i2c_client	*client = i2c_verify_client(dev); | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 268 | 	struct i2c_driver	*driver; | 
 | 269 | 	int			status; | 
 | 270 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 271 | 	if (!client || !dev->driver) | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 272 | 		return 0; | 
 | 273 |  | 
 | 274 | 	driver = to_i2c_driver(dev->driver); | 
 | 275 | 	if (driver->remove) { | 
 | 276 | 		dev_dbg(dev, "remove\n"); | 
 | 277 | 		status = driver->remove(client); | 
 | 278 | 	} else { | 
 | 279 | 		dev->driver = NULL; | 
 | 280 | 		status = 0; | 
 | 281 | 	} | 
| Wolfram Sang | e4a7b9b | 2010-05-04 11:09:27 +0200 | [diff] [blame] | 282 | 	if (status == 0) { | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 283 | 		client->driver = NULL; | 
| Wolfram Sang | e4a7b9b | 2010-05-04 11:09:27 +0200 | [diff] [blame] | 284 | 		i2c_set_clientdata(client, NULL); | 
 | 285 | 	} | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 286 | 	return status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } | 
 | 288 |  | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 289 | static void i2c_device_shutdown(struct device *dev) | 
 | 290 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 291 | 	struct i2c_client *client = i2c_verify_client(dev); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 292 | 	struct i2c_driver *driver; | 
 | 293 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 294 | 	if (!client || !dev->driver) | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 295 | 		return; | 
 | 296 | 	driver = to_i2c_driver(dev->driver); | 
 | 297 | 	if (driver->shutdown) | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 298 | 		driver->shutdown(client); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 299 | } | 
 | 300 |  | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 301 | #ifdef CONFIG_PM_SLEEP | 
 | 302 | static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg) | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 303 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 304 | 	struct i2c_client *client = i2c_verify_client(dev); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 305 | 	struct i2c_driver *driver; | 
 | 306 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 307 | 	if (!client || !dev->driver) | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 308 | 		return 0; | 
 | 309 | 	driver = to_i2c_driver(dev->driver); | 
 | 310 | 	if (!driver->suspend) | 
 | 311 | 		return 0; | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 312 | 	return driver->suspend(client, mesg); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 313 | } | 
 | 314 |  | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 315 | static int i2c_legacy_resume(struct device *dev) | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 316 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 317 | 	struct i2c_client *client = i2c_verify_client(dev); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 318 | 	struct i2c_driver *driver; | 
 | 319 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 320 | 	if (!client || !dev->driver) | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 321 | 		return 0; | 
 | 322 | 	driver = to_i2c_driver(dev->driver); | 
 | 323 | 	if (!driver->resume) | 
 | 324 | 		return 0; | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 325 | 	return driver->resume(client); | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 326 | } | 
 | 327 |  | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 328 | static int i2c_device_pm_suspend(struct device *dev) | 
 | 329 | { | 
 | 330 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
 | 331 |  | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 332 | 	if (pm) | 
 | 333 | 		return pm_generic_suspend(dev); | 
 | 334 | 	else | 
 | 335 | 		return i2c_legacy_suspend(dev, PMSG_SUSPEND); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 336 | } | 
 | 337 |  | 
 | 338 | static int i2c_device_pm_resume(struct device *dev) | 
 | 339 | { | 
 | 340 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 341 |  | 
 | 342 | 	if (pm) | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 343 | 		return pm_generic_resume(dev); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 344 | 	else | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 345 | 		return i2c_legacy_resume(dev); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 346 | } | 
 | 347 |  | 
 | 348 | static int i2c_device_pm_freeze(struct device *dev) | 
 | 349 | { | 
 | 350 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
 | 351 |  | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 352 | 	if (pm) | 
 | 353 | 		return pm_generic_freeze(dev); | 
 | 354 | 	else | 
 | 355 | 		return i2c_legacy_suspend(dev, PMSG_FREEZE); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 356 | } | 
 | 357 |  | 
 | 358 | static int i2c_device_pm_thaw(struct device *dev) | 
 | 359 | { | 
 | 360 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
 | 361 |  | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 362 | 	if (pm) | 
 | 363 | 		return pm_generic_thaw(dev); | 
 | 364 | 	else | 
 | 365 | 		return i2c_legacy_resume(dev); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 366 | } | 
 | 367 |  | 
 | 368 | static int i2c_device_pm_poweroff(struct device *dev) | 
 | 369 | { | 
 | 370 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
 | 371 |  | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 372 | 	if (pm) | 
 | 373 | 		return pm_generic_poweroff(dev); | 
 | 374 | 	else | 
 | 375 | 		return i2c_legacy_suspend(dev, PMSG_HIBERNATE); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 376 | } | 
 | 377 |  | 
 | 378 | static int i2c_device_pm_restore(struct device *dev) | 
 | 379 | { | 
 | 380 | 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 381 |  | 
 | 382 | 	if (pm) | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 383 | 		return pm_generic_restore(dev); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 384 | 	else | 
| Mark Brown | d529de2 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 385 | 		return i2c_legacy_resume(dev); | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 386 | } | 
 | 387 | #else /* !CONFIG_PM_SLEEP */ | 
 | 388 | #define i2c_device_pm_suspend	NULL | 
 | 389 | #define i2c_device_pm_resume	NULL | 
 | 390 | #define i2c_device_pm_freeze	NULL | 
 | 391 | #define i2c_device_pm_thaw	NULL | 
 | 392 | #define i2c_device_pm_poweroff	NULL | 
 | 393 | #define i2c_device_pm_restore	NULL | 
 | 394 | #endif /* !CONFIG_PM_SLEEP */ | 
 | 395 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 396 | static void i2c_client_dev_release(struct device *dev) | 
 | 397 | { | 
 | 398 | 	kfree(to_i2c_client(dev)); | 
 | 399 | } | 
 | 400 |  | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 401 | static ssize_t | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 402 | show_name(struct device *dev, struct device_attribute *attr, char *buf) | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 403 | { | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 404 | 	return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? | 
 | 405 | 		       to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 406 | } | 
 | 407 |  | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 408 | static ssize_t | 
 | 409 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 410 | { | 
 | 411 | 	struct i2c_client *client = to_i2c_client(dev); | 
| Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 412 | 	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 413 | } | 
 | 414 |  | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 415 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 416 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); | 
 | 417 |  | 
 | 418 | static struct attribute *i2c_dev_attrs[] = { | 
 | 419 | 	&dev_attr_name.attr, | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 420 | 	/* modalias helps coldplug:  modprobe $(cat .../modalias) */ | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 421 | 	&dev_attr_modalias.attr, | 
 | 422 | 	NULL | 
 | 423 | }; | 
 | 424 |  | 
 | 425 | static struct attribute_group i2c_dev_attr_group = { | 
 | 426 | 	.attrs		= i2c_dev_attrs, | 
 | 427 | }; | 
 | 428 |  | 
 | 429 | static const struct attribute_group *i2c_dev_attr_groups[] = { | 
 | 430 | 	&i2c_dev_attr_group, | 
 | 431 | 	NULL | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 432 | }; | 
 | 433 |  | 
| Tobias Klauser | 0b2c368 | 2010-01-16 20:43:12 +0100 | [diff] [blame] | 434 | static const struct dev_pm_ops i2c_device_pm_ops = { | 
| sonic zhang | 54067ee | 2009-12-14 21:17:30 +0100 | [diff] [blame] | 435 | 	.suspend = i2c_device_pm_suspend, | 
 | 436 | 	.resume = i2c_device_pm_resume, | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 437 | 	.freeze = i2c_device_pm_freeze, | 
 | 438 | 	.thaw = i2c_device_pm_thaw, | 
 | 439 | 	.poweroff = i2c_device_pm_poweroff, | 
 | 440 | 	.restore = i2c_device_pm_restore, | 
 | 441 | 	SET_RUNTIME_PM_OPS( | 
 | 442 | 		pm_generic_runtime_suspend, | 
 | 443 | 		pm_generic_runtime_resume, | 
| Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 444 | 		NULL | 
| Rafael J. Wysocki | 2f60ba7 | 2010-05-10 23:09:30 +0200 | [diff] [blame] | 445 | 	) | 
| sonic zhang | 54067ee | 2009-12-14 21:17:30 +0100 | [diff] [blame] | 446 | }; | 
 | 447 |  | 
| Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 448 | struct bus_type i2c_bus_type = { | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 449 | 	.name		= "i2c", | 
 | 450 | 	.match		= i2c_device_match, | 
 | 451 | 	.probe		= i2c_device_probe, | 
 | 452 | 	.remove		= i2c_device_remove, | 
 | 453 | 	.shutdown	= i2c_device_shutdown, | 
| sonic zhang | 54067ee | 2009-12-14 21:17:30 +0100 | [diff] [blame] | 454 | 	.pm		= &i2c_device_pm_ops, | 
| Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 455 | }; | 
| Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(i2c_bus_type); | 
| Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 457 |  | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 458 | static struct device_type i2c_client_type = { | 
 | 459 | 	.groups		= i2c_dev_attr_groups, | 
 | 460 | 	.uevent		= i2c_device_uevent, | 
 | 461 | 	.release	= i2c_client_dev_release, | 
 | 462 | }; | 
 | 463 |  | 
| David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 464 |  | 
 | 465 | /** | 
 | 466 |  * i2c_verify_client - return parameter as i2c_client, or NULL | 
 | 467 |  * @dev: device, probably from some driver model iterator | 
 | 468 |  * | 
 | 469 |  * When traversing the driver model tree, perhaps using driver model | 
 | 470 |  * iterators like @device_for_each_child(), you can't assume very much | 
 | 471 |  * about the nodes you find.  Use this function to avoid oopses caused | 
 | 472 |  * by wrongly treating some non-I2C device as an i2c_client. | 
 | 473 |  */ | 
 | 474 | struct i2c_client *i2c_verify_client(struct device *dev) | 
 | 475 | { | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 476 | 	return (dev->type == &i2c_client_type) | 
| David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 477 | 			? to_i2c_client(dev) | 
 | 478 | 			: NULL; | 
 | 479 | } | 
 | 480 | EXPORT_SYMBOL(i2c_verify_client); | 
 | 481 |  | 
 | 482 |  | 
| Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 483 | /* This is a permissive address validity check, I2C address map constraints | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 484 |  * are purposely not enforced, except for the general call address. */ | 
| Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 485 | static int i2c_check_client_addr_validity(const struct i2c_client *client) | 
 | 486 | { | 
 | 487 | 	if (client->flags & I2C_CLIENT_TEN) { | 
 | 488 | 		/* 10-bit address, all values are valid */ | 
 | 489 | 		if (client->addr > 0x3ff) | 
 | 490 | 			return -EINVAL; | 
 | 491 | 	} else { | 
 | 492 | 		/* 7-bit address, reject the general call address */ | 
 | 493 | 		if (client->addr == 0x00 || client->addr > 0x7f) | 
 | 494 | 			return -EINVAL; | 
 | 495 | 	} | 
 | 496 | 	return 0; | 
 | 497 | } | 
 | 498 |  | 
| Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 499 | /* And this is a strict address validity check, used when probing. If a | 
 | 500 |  * device uses a reserved address, then it shouldn't be probed. 7-bit | 
 | 501 |  * addressing is assumed, 10-bit address devices are rare and should be | 
 | 502 |  * explicitly enumerated. */ | 
 | 503 | static int i2c_check_addr_validity(unsigned short addr) | 
 | 504 | { | 
 | 505 | 	/* | 
 | 506 | 	 * Reserved addresses per I2C specification: | 
 | 507 | 	 *  0x00       General call address / START byte | 
 | 508 | 	 *  0x01       CBUS address | 
 | 509 | 	 *  0x02       Reserved for different bus format | 
 | 510 | 	 *  0x03       Reserved for future purposes | 
 | 511 | 	 *  0x04-0x07  Hs-mode master code | 
 | 512 | 	 *  0x78-0x7b  10-bit slave addressing | 
 | 513 | 	 *  0x7c-0x7f  Reserved for future purposes | 
 | 514 | 	 */ | 
 | 515 | 	if (addr < 0x08 || addr > 0x77) | 
 | 516 | 		return -EINVAL; | 
 | 517 | 	return 0; | 
 | 518 | } | 
 | 519 |  | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 520 | static int __i2c_check_addr_busy(struct device *dev, void *addrp) | 
 | 521 | { | 
 | 522 | 	struct i2c_client	*client = i2c_verify_client(dev); | 
 | 523 | 	int			addr = *(int *)addrp; | 
 | 524 |  | 
 | 525 | 	if (client && client->addr == addr) | 
 | 526 | 		return -EBUSY; | 
 | 527 | 	return 0; | 
 | 528 | } | 
 | 529 |  | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 530 | /* walk up mux tree */ | 
 | 531 | static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) | 
 | 532 | { | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 533 | 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 534 | 	int result; | 
 | 535 |  | 
 | 536 | 	result = device_for_each_child(&adapter->dev, &addr, | 
 | 537 | 					__i2c_check_addr_busy); | 
 | 538 |  | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 539 | 	if (!result && parent) | 
 | 540 | 		result = i2c_check_mux_parents(parent, addr); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 541 |  | 
 | 542 | 	return result; | 
 | 543 | } | 
 | 544 |  | 
 | 545 | /* recurse down mux tree */ | 
 | 546 | static int i2c_check_mux_children(struct device *dev, void *addrp) | 
 | 547 | { | 
 | 548 | 	int result; | 
 | 549 |  | 
 | 550 | 	if (dev->type == &i2c_adapter_type) | 
 | 551 | 		result = device_for_each_child(dev, addrp, | 
 | 552 | 						i2c_check_mux_children); | 
 | 553 | 	else | 
 | 554 | 		result = __i2c_check_addr_busy(dev, addrp); | 
 | 555 |  | 
 | 556 | 	return result; | 
 | 557 | } | 
 | 558 |  | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 559 | static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) | 
 | 560 | { | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 561 | 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 562 | 	int result = 0; | 
 | 563 |  | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 564 | 	if (parent) | 
 | 565 | 		result = i2c_check_mux_parents(parent, addr); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 566 |  | 
 | 567 | 	if (!result) | 
 | 568 | 		result = device_for_each_child(&adapter->dev, &addr, | 
 | 569 | 						i2c_check_mux_children); | 
 | 570 |  | 
 | 571 | 	return result; | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 572 | } | 
 | 573 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 574 | /** | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 575 |  * i2c_lock_adapter - Get exclusive access to an I2C bus segment | 
 | 576 |  * @adapter: Target I2C bus segment | 
 | 577 |  */ | 
 | 578 | void i2c_lock_adapter(struct i2c_adapter *adapter) | 
 | 579 | { | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 580 | 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); | 
 | 581 |  | 
 | 582 | 	if (parent) | 
 | 583 | 		i2c_lock_adapter(parent); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 584 | 	else | 
 | 585 | 		rt_mutex_lock(&adapter->bus_lock); | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 586 | } | 
 | 587 | EXPORT_SYMBOL_GPL(i2c_lock_adapter); | 
 | 588 |  | 
 | 589 | /** | 
 | 590 |  * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment | 
 | 591 |  * @adapter: Target I2C bus segment | 
 | 592 |  */ | 
 | 593 | static int i2c_trylock_adapter(struct i2c_adapter *adapter) | 
 | 594 | { | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 595 | 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); | 
 | 596 |  | 
 | 597 | 	if (parent) | 
 | 598 | 		return i2c_trylock_adapter(parent); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 599 | 	else | 
 | 600 | 		return rt_mutex_trylock(&adapter->bus_lock); | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 601 | } | 
 | 602 |  | 
 | 603 | /** | 
 | 604 |  * i2c_unlock_adapter - Release exclusive access to an I2C bus segment | 
 | 605 |  * @adapter: Target I2C bus segment | 
 | 606 |  */ | 
 | 607 | void i2c_unlock_adapter(struct i2c_adapter *adapter) | 
 | 608 | { | 
| Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 609 | 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); | 
 | 610 |  | 
 | 611 | 	if (parent) | 
 | 612 | 		i2c_unlock_adapter(parent); | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 613 | 	else | 
 | 614 | 		rt_mutex_unlock(&adapter->bus_lock); | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 615 | } | 
 | 616 | EXPORT_SYMBOL_GPL(i2c_unlock_adapter); | 
 | 617 |  | 
 | 618 | /** | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 619 |  * i2c_new_device - instantiate an i2c device | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 620 |  * @adap: the adapter managing the device | 
 | 621 |  * @info: describes one I2C device; bus_num is ignored | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 622 |  * Context: can sleep | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 623 |  * | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 624 |  * Create an i2c device. Binding is handled through driver model | 
 | 625 |  * probe()/remove() methods.  A driver may be bound to this device when we | 
 | 626 |  * return from this function, or any later moment (e.g. maybe hotplugging will | 
 | 627 |  * load the driver module).  This call is not appropriate for use by mainboard | 
 | 628 |  * initialization logic, which usually runs during an arch_initcall() long | 
 | 629 |  * before any i2c_adapter could exist. | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 630 |  * | 
 | 631 |  * This returns the new i2c client, which may be saved for later use with | 
 | 632 |  * i2c_unregister_device(); or NULL to indicate an error. | 
 | 633 |  */ | 
 | 634 | struct i2c_client * | 
 | 635 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | 
 | 636 | { | 
 | 637 | 	struct i2c_client	*client; | 
 | 638 | 	int			status; | 
 | 639 |  | 
 | 640 | 	client = kzalloc(sizeof *client, GFP_KERNEL); | 
 | 641 | 	if (!client) | 
 | 642 | 		return NULL; | 
 | 643 |  | 
 | 644 | 	client->adapter = adap; | 
 | 645 |  | 
 | 646 | 	client->dev.platform_data = info->platform_data; | 
| David Brownell | 3bbb835 | 2007-10-13 23:56:29 +0200 | [diff] [blame] | 647 |  | 
| Anton Vorontsov | 11f1f2a | 2008-10-22 20:21:33 +0200 | [diff] [blame] | 648 | 	if (info->archdata) | 
 | 649 | 		client->dev.archdata = *info->archdata; | 
 | 650 |  | 
| Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 651 | 	client->flags = info->flags; | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 652 | 	client->addr = info->addr; | 
 | 653 | 	client->irq = info->irq; | 
 | 654 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 655 | 	strlcpy(client->name, info->type, sizeof(client->name)); | 
 | 656 |  | 
| Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 657 | 	/* Check for address validity */ | 
 | 658 | 	status = i2c_check_client_addr_validity(client); | 
 | 659 | 	if (status) { | 
 | 660 | 		dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", | 
 | 661 | 			client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); | 
 | 662 | 		goto out_err_silent; | 
 | 663 | 	} | 
 | 664 |  | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 665 | 	/* Check for address business */ | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 666 | 	status = i2c_check_addr_busy(adap, client->addr); | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 667 | 	if (status) | 
 | 668 | 		goto out_err; | 
 | 669 |  | 
 | 670 | 	client->dev.parent = &client->adapter->dev; | 
 | 671 | 	client->dev.bus = &i2c_bus_type; | 
| Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 672 | 	client->dev.type = &i2c_client_type; | 
| Grant Likely | d12d42f | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 673 | 	client->dev.of_node = info->of_node; | 
| Mika Westerberg | 907ddf8 | 2012-11-23 12:23:40 +0100 | [diff] [blame] | 674 | 	ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle); | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 675 |  | 
| Jean Delvare | cbb4451 | 2011-11-23 11:33:07 +0100 | [diff] [blame] | 676 | 	/* For 10-bit clients, add an arbitrary offset to avoid collisions */ | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 677 | 	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), | 
| Jean Delvare | cbb4451 | 2011-11-23 11:33:07 +0100 | [diff] [blame] | 678 | 		     client->addr | ((client->flags & I2C_CLIENT_TEN) | 
 | 679 | 				     ? 0xa000 : 0)); | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 680 | 	status = device_register(&client->dev); | 
 | 681 | 	if (status) | 
 | 682 | 		goto out_err; | 
 | 683 |  | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 684 | 	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", | 
 | 685 | 		client->name, dev_name(&client->dev)); | 
 | 686 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 687 | 	return client; | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 688 |  | 
 | 689 | out_err: | 
 | 690 | 	dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " | 
 | 691 | 		"(%d)\n", client->name, client->addr, status); | 
| Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 692 | out_err_silent: | 
| Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 693 | 	kfree(client); | 
 | 694 | 	return NULL; | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 695 | } | 
 | 696 | EXPORT_SYMBOL_GPL(i2c_new_device); | 
 | 697 |  | 
 | 698 |  | 
 | 699 | /** | 
 | 700 |  * i2c_unregister_device - reverse effect of i2c_new_device() | 
 | 701 |  * @client: value returned from i2c_new_device() | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 702 |  * Context: can sleep | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 703 |  */ | 
 | 704 | void i2c_unregister_device(struct i2c_client *client) | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 705 | { | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 706 | 	device_unregister(&client->dev); | 
 | 707 | } | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 708 | EXPORT_SYMBOL_GPL(i2c_unregister_device); | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 709 |  | 
 | 710 |  | 
| Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 711 | static const struct i2c_device_id dummy_id[] = { | 
 | 712 | 	{ "dummy", 0 }, | 
 | 713 | 	{ }, | 
 | 714 | }; | 
 | 715 |  | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 716 | static int dummy_probe(struct i2c_client *client, | 
 | 717 | 		       const struct i2c_device_id *id) | 
 | 718 | { | 
 | 719 | 	return 0; | 
 | 720 | } | 
 | 721 |  | 
 | 722 | static int dummy_remove(struct i2c_client *client) | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 723 | { | 
 | 724 | 	return 0; | 
 | 725 | } | 
 | 726 |  | 
 | 727 | static struct i2c_driver dummy_driver = { | 
 | 728 | 	.driver.name	= "dummy", | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 729 | 	.probe		= dummy_probe, | 
 | 730 | 	.remove		= dummy_remove, | 
| Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 731 | 	.id_table	= dummy_id, | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 732 | }; | 
 | 733 |  | 
 | 734 | /** | 
 | 735 |  * i2c_new_dummy - return a new i2c device bound to a dummy driver | 
 | 736 |  * @adapter: the adapter managing the device | 
 | 737 |  * @address: seven bit address to be used | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 738 |  * Context: can sleep | 
 | 739 |  * | 
 | 740 |  * This returns an I2C client bound to the "dummy" driver, intended for use | 
 | 741 |  * with devices that consume multiple addresses.  Examples of such chips | 
 | 742 |  * include various EEPROMS (like 24c04 and 24c08 models). | 
 | 743 |  * | 
 | 744 |  * These dummy devices have two main uses.  First, most I2C and SMBus calls | 
 | 745 |  * except i2c_transfer() need a client handle; the dummy will be that handle. | 
 | 746 |  * And second, this prevents the specified address from being bound to a | 
 | 747 |  * different driver. | 
 | 748 |  * | 
 | 749 |  * This returns the new i2c client, which should be saved for later use with | 
 | 750 |  * i2c_unregister_device(); or NULL to indicate an error. | 
 | 751 |  */ | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 752 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 753 | { | 
 | 754 | 	struct i2c_board_info info = { | 
| Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 755 | 		I2C_BOARD_INFO("dummy", address), | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 756 | 	}; | 
 | 757 |  | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 758 | 	return i2c_new_device(adapter, &info); | 
 | 759 | } | 
 | 760 | EXPORT_SYMBOL_GPL(i2c_new_dummy); | 
 | 761 |  | 
| David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 762 | /* ------------------------------------------------------------------------- */ | 
 | 763 |  | 
| David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 764 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ | 
 | 765 |  | 
| Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 766 | static void i2c_adapter_dev_release(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { | 
| David Brownell | ef2c8321 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 768 | 	struct i2c_adapter *adap = to_i2c_adapter(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | 	complete(&adap->dev_released); | 
 | 770 | } | 
 | 771 |  | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 772 | /* | 
| Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 773 |  * This function is only needed for mutex_lock_nested, so it is never | 
 | 774 |  * called unless locking correctness checking is enabled. Thus we | 
 | 775 |  * make it inline to avoid a compiler warning. That's what gcc ends up | 
 | 776 |  * doing anyway. | 
 | 777 |  */ | 
 | 778 | static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter) | 
 | 779 | { | 
 | 780 | 	unsigned int depth = 0; | 
 | 781 |  | 
 | 782 | 	while ((adapter = i2c_parent_is_i2c_adapter(adapter))) | 
 | 783 | 		depth++; | 
 | 784 |  | 
 | 785 | 	return depth; | 
 | 786 | } | 
 | 787 |  | 
 | 788 | /* | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 789 |  * Let users instantiate I2C devices through sysfs. This can be used when | 
 | 790 |  * platform initialization code doesn't contain the proper data for | 
 | 791 |  * whatever reason. Also useful for drivers that do device detection and | 
 | 792 |  * detection fails, either because the device uses an unexpected address, | 
 | 793 |  * or this is a compatible device with different ID register values. | 
 | 794 |  * | 
 | 795 |  * Parameter checking may look overzealous, but we really don't want | 
 | 796 |  * the user to provide incorrect parameters. | 
 | 797 |  */ | 
 | 798 | static ssize_t | 
 | 799 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | 
 | 800 | 		     const char *buf, size_t count) | 
 | 801 | { | 
 | 802 | 	struct i2c_adapter *adap = to_i2c_adapter(dev); | 
 | 803 | 	struct i2c_board_info info; | 
 | 804 | 	struct i2c_client *client; | 
 | 805 | 	char *blank, end; | 
 | 806 | 	int res; | 
 | 807 |  | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 808 | 	memset(&info, 0, sizeof(struct i2c_board_info)); | 
 | 809 |  | 
 | 810 | 	blank = strchr(buf, ' '); | 
 | 811 | 	if (!blank) { | 
 | 812 | 		dev_err(dev, "%s: Missing parameters\n", "new_device"); | 
 | 813 | 		return -EINVAL; | 
 | 814 | 	} | 
 | 815 | 	if (blank - buf > I2C_NAME_SIZE - 1) { | 
 | 816 | 		dev_err(dev, "%s: Invalid device name\n", "new_device"); | 
 | 817 | 		return -EINVAL; | 
 | 818 | 	} | 
 | 819 | 	memcpy(info.type, buf, blank - buf); | 
 | 820 |  | 
 | 821 | 	/* Parse remaining parameters, reject extra parameters */ | 
 | 822 | 	res = sscanf(++blank, "%hi%c", &info.addr, &end); | 
 | 823 | 	if (res < 1) { | 
 | 824 | 		dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); | 
 | 825 | 		return -EINVAL; | 
 | 826 | 	} | 
 | 827 | 	if (res > 1  && end != '\n') { | 
 | 828 | 		dev_err(dev, "%s: Extra parameters\n", "new_device"); | 
 | 829 | 		return -EINVAL; | 
 | 830 | 	} | 
 | 831 |  | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 832 | 	client = i2c_new_device(adap, &info); | 
 | 833 | 	if (!client) | 
| Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 834 | 		return -EINVAL; | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 835 |  | 
 | 836 | 	/* Keep track of the added device */ | 
| Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 837 | 	mutex_lock(&adap->userspace_clients_lock); | 
| Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 838 | 	list_add_tail(&client->detected, &adap->userspace_clients); | 
| Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 839 | 	mutex_unlock(&adap->userspace_clients_lock); | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 840 | 	dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", | 
 | 841 | 		 info.type, info.addr); | 
 | 842 |  | 
 | 843 | 	return count; | 
 | 844 | } | 
 | 845 |  | 
 | 846 | /* | 
 | 847 |  * And of course let the users delete the devices they instantiated, if | 
 | 848 |  * they got it wrong. This interface can only be used to delete devices | 
 | 849 |  * instantiated by i2c_sysfs_new_device above. This guarantees that we | 
 | 850 |  * don't delete devices to which some kernel code still has references. | 
 | 851 |  * | 
 | 852 |  * Parameter checking may look overzealous, but we really don't want | 
 | 853 |  * the user to delete the wrong device. | 
 | 854 |  */ | 
 | 855 | static ssize_t | 
 | 856 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | 
 | 857 | 			const char *buf, size_t count) | 
 | 858 | { | 
 | 859 | 	struct i2c_adapter *adap = to_i2c_adapter(dev); | 
 | 860 | 	struct i2c_client *client, *next; | 
 | 861 | 	unsigned short addr; | 
 | 862 | 	char end; | 
 | 863 | 	int res; | 
 | 864 |  | 
 | 865 | 	/* Parse parameters, reject extra parameters */ | 
 | 866 | 	res = sscanf(buf, "%hi%c", &addr, &end); | 
 | 867 | 	if (res < 1) { | 
 | 868 | 		dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); | 
 | 869 | 		return -EINVAL; | 
 | 870 | 	} | 
 | 871 | 	if (res > 1  && end != '\n') { | 
 | 872 | 		dev_err(dev, "%s: Extra parameters\n", "delete_device"); | 
 | 873 | 		return -EINVAL; | 
 | 874 | 	} | 
 | 875 |  | 
 | 876 | 	/* Make sure the device was added through sysfs */ | 
 | 877 | 	res = -ENOENT; | 
| Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 878 | 	mutex_lock_nested(&adap->userspace_clients_lock, | 
 | 879 | 			  i2c_adapter_depth(adap)); | 
| Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 880 | 	list_for_each_entry_safe(client, next, &adap->userspace_clients, | 
 | 881 | 				 detected) { | 
 | 882 | 		if (client->addr == addr) { | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 883 | 			dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", | 
 | 884 | 				 "delete_device", client->name, client->addr); | 
 | 885 |  | 
 | 886 | 			list_del(&client->detected); | 
 | 887 | 			i2c_unregister_device(client); | 
 | 888 | 			res = count; | 
 | 889 | 			break; | 
 | 890 | 		} | 
 | 891 | 	} | 
| Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 892 | 	mutex_unlock(&adap->userspace_clients_lock); | 
| Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 893 |  | 
 | 894 | 	if (res < 0) | 
 | 895 | 		dev_err(dev, "%s: Can't find device in list\n", | 
 | 896 | 			"delete_device"); | 
 | 897 | 	return res; | 
 | 898 | } | 
 | 899 |  | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 900 | static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); | 
| Alexander Sverdlin | e9b526f | 2013-05-17 14:56:35 +0200 | [diff] [blame] | 901 | static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL, | 
 | 902 | 				   i2c_sysfs_delete_device); | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 903 |  | 
 | 904 | static struct attribute *i2c_adapter_attrs[] = { | 
 | 905 | 	&dev_attr_name.attr, | 
 | 906 | 	&dev_attr_new_device.attr, | 
 | 907 | 	&dev_attr_delete_device.attr, | 
 | 908 | 	NULL | 
| David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 909 | }; | 
 | 910 |  | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 911 | static struct attribute_group i2c_adapter_attr_group = { | 
 | 912 | 	.attrs		= i2c_adapter_attrs, | 
 | 913 | }; | 
 | 914 |  | 
 | 915 | static const struct attribute_group *i2c_adapter_attr_groups[] = { | 
 | 916 | 	&i2c_adapter_attr_group, | 
 | 917 | 	NULL | 
 | 918 | }; | 
 | 919 |  | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 920 | struct device_type i2c_adapter_type = { | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 921 | 	.groups		= i2c_adapter_attr_groups, | 
 | 922 | 	.release	= i2c_adapter_dev_release, | 
| David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 923 | }; | 
| Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 924 | EXPORT_SYMBOL_GPL(i2c_adapter_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 |  | 
| Stephen Warren | 643dd09 | 2012-04-17 12:43:33 -0600 | [diff] [blame] | 926 | /** | 
 | 927 |  * i2c_verify_adapter - return parameter as i2c_adapter or NULL | 
 | 928 |  * @dev: device, probably from some driver model iterator | 
 | 929 |  * | 
 | 930 |  * When traversing the driver model tree, perhaps using driver model | 
 | 931 |  * iterators like @device_for_each_child(), you can't assume very much | 
 | 932 |  * about the nodes you find.  Use this function to avoid oopses caused | 
 | 933 |  * by wrongly treating some non-I2C device as an i2c_adapter. | 
 | 934 |  */ | 
 | 935 | struct i2c_adapter *i2c_verify_adapter(struct device *dev) | 
 | 936 | { | 
 | 937 | 	return (dev->type == &i2c_adapter_type) | 
 | 938 | 			? to_i2c_adapter(dev) | 
 | 939 | 			: NULL; | 
 | 940 | } | 
 | 941 | EXPORT_SYMBOL(i2c_verify_adapter); | 
 | 942 |  | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 943 | #ifdef CONFIG_I2C_COMPAT | 
 | 944 | static struct class_compat *i2c_adapter_compat_class; | 
 | 945 | #endif | 
 | 946 |  | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 947 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) | 
 | 948 | { | 
 | 949 | 	struct i2c_devinfo	*devinfo; | 
 | 950 |  | 
| Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 951 | 	down_read(&__i2c_board_lock); | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 952 | 	list_for_each_entry(devinfo, &__i2c_board_list, list) { | 
 | 953 | 		if (devinfo->busnum == adapter->nr | 
 | 954 | 				&& !i2c_new_device(adapter, | 
 | 955 | 						&devinfo->board_info)) | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 956 | 			dev_err(&adapter->dev, | 
 | 957 | 				"Can't create device at 0x%02x\n", | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 958 | 				devinfo->board_info.addr); | 
 | 959 | 	} | 
| Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 960 | 	up_read(&__i2c_board_lock); | 
| David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 961 | } | 
 | 962 |  | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 963 | /* OF support code */ | 
 | 964 |  | 
 | 965 | #if IS_ENABLED(CONFIG_OF) | 
 | 966 | static void of_i2c_register_devices(struct i2c_adapter *adap) | 
 | 967 | { | 
 | 968 | 	void *result; | 
 | 969 | 	struct device_node *node; | 
 | 970 |  | 
 | 971 | 	/* Only register child devices if the adapter has a node pointer set */ | 
 | 972 | 	if (!adap->dev.of_node) | 
 | 973 | 		return; | 
 | 974 |  | 
 | 975 | 	dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); | 
 | 976 |  | 
 | 977 | 	for_each_available_child_of_node(adap->dev.of_node, node) { | 
 | 978 | 		struct i2c_board_info info = {}; | 
 | 979 | 		struct dev_archdata dev_ad = {}; | 
 | 980 | 		const __be32 *addr; | 
 | 981 | 		int len; | 
 | 982 |  | 
 | 983 | 		dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name); | 
 | 984 |  | 
 | 985 | 		if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) { | 
 | 986 | 			dev_err(&adap->dev, "of_i2c: modalias failure on %s\n", | 
 | 987 | 				node->full_name); | 
 | 988 | 			continue; | 
 | 989 | 		} | 
 | 990 |  | 
 | 991 | 		addr = of_get_property(node, "reg", &len); | 
 | 992 | 		if (!addr || (len < sizeof(int))) { | 
 | 993 | 			dev_err(&adap->dev, "of_i2c: invalid reg on %s\n", | 
 | 994 | 				node->full_name); | 
 | 995 | 			continue; | 
 | 996 | 		} | 
 | 997 |  | 
 | 998 | 		info.addr = be32_to_cpup(addr); | 
 | 999 | 		if (info.addr > (1 << 10) - 1) { | 
 | 1000 | 			dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n", | 
 | 1001 | 				info.addr, node->full_name); | 
 | 1002 | 			continue; | 
 | 1003 | 		} | 
 | 1004 |  | 
 | 1005 | 		info.irq = irq_of_parse_and_map(node, 0); | 
 | 1006 | 		info.of_node = of_node_get(node); | 
 | 1007 | 		info.archdata = &dev_ad; | 
 | 1008 |  | 
 | 1009 | 		if (of_get_property(node, "wakeup-source", NULL)) | 
 | 1010 | 			info.flags |= I2C_CLIENT_WAKE; | 
 | 1011 |  | 
 | 1012 | 		request_module("%s%s", I2C_MODULE_PREFIX, info.type); | 
 | 1013 |  | 
 | 1014 | 		result = i2c_new_device(adap, &info); | 
 | 1015 | 		if (result == NULL) { | 
 | 1016 | 			dev_err(&adap->dev, "of_i2c: Failure registering %s\n", | 
 | 1017 | 				node->full_name); | 
 | 1018 | 			of_node_put(node); | 
 | 1019 | 			irq_dispose_mapping(info.irq); | 
 | 1020 | 			continue; | 
 | 1021 | 		} | 
 | 1022 | 	} | 
 | 1023 | } | 
 | 1024 |  | 
 | 1025 | static int of_dev_node_match(struct device *dev, void *data) | 
 | 1026 | { | 
 | 1027 | 	return dev->of_node == data; | 
 | 1028 | } | 
 | 1029 |  | 
 | 1030 | /* must call put_device() when done with returned i2c_client device */ | 
 | 1031 | struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) | 
 | 1032 | { | 
 | 1033 | 	struct device *dev; | 
 | 1034 |  | 
 | 1035 | 	dev = bus_find_device(&i2c_bus_type, NULL, node, | 
 | 1036 | 					 of_dev_node_match); | 
 | 1037 | 	if (!dev) | 
 | 1038 | 		return NULL; | 
 | 1039 |  | 
 | 1040 | 	return i2c_verify_client(dev); | 
 | 1041 | } | 
 | 1042 | EXPORT_SYMBOL(of_find_i2c_device_by_node); | 
 | 1043 |  | 
 | 1044 | /* must call put_device() when done with returned i2c_adapter device */ | 
 | 1045 | struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) | 
 | 1046 | { | 
 | 1047 | 	struct device *dev; | 
 | 1048 |  | 
 | 1049 | 	dev = bus_find_device(&i2c_bus_type, NULL, node, | 
 | 1050 | 					 of_dev_node_match); | 
 | 1051 | 	if (!dev) | 
 | 1052 | 		return NULL; | 
 | 1053 |  | 
 | 1054 | 	return i2c_verify_adapter(dev); | 
 | 1055 | } | 
 | 1056 | EXPORT_SYMBOL(of_find_i2c_adapter_by_node); | 
 | 1057 | #else | 
 | 1058 | static void of_i2c_register_devices(struct i2c_adapter *adap) { } | 
 | 1059 | #endif /* CONFIG_OF */ | 
 | 1060 |  | 
| Mika Westerberg | 55e71ed | 2013-08-21 17:28:23 +0300 | [diff] [blame] | 1061 | /* ACPI support code */ | 
 | 1062 |  | 
 | 1063 | #if IS_ENABLED(CONFIG_ACPI) | 
 | 1064 | static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data) | 
 | 1065 | { | 
 | 1066 | 	struct i2c_board_info *info = data; | 
 | 1067 |  | 
 | 1068 | 	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { | 
 | 1069 | 		struct acpi_resource_i2c_serialbus *sb; | 
 | 1070 |  | 
 | 1071 | 		sb = &ares->data.i2c_serial_bus; | 
 | 1072 | 		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) { | 
 | 1073 | 			info->addr = sb->slave_address; | 
 | 1074 | 			if (sb->access_mode == ACPI_I2C_10BIT_MODE) | 
 | 1075 | 				info->flags |= I2C_CLIENT_TEN; | 
 | 1076 | 		} | 
 | 1077 | 	} else if (info->irq < 0) { | 
 | 1078 | 		struct resource r; | 
 | 1079 |  | 
 | 1080 | 		if (acpi_dev_resource_interrupt(ares, 0, &r)) | 
 | 1081 | 			info->irq = r.start; | 
 | 1082 | 	} | 
 | 1083 |  | 
 | 1084 | 	/* Tell the ACPI core to skip this resource */ | 
 | 1085 | 	return 1; | 
 | 1086 | } | 
 | 1087 |  | 
 | 1088 | static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level, | 
 | 1089 | 				       void *data, void **return_value) | 
 | 1090 | { | 
 | 1091 | 	struct i2c_adapter *adapter = data; | 
 | 1092 | 	struct list_head resource_list; | 
 | 1093 | 	struct i2c_board_info info; | 
 | 1094 | 	struct acpi_device *adev; | 
 | 1095 | 	int ret; | 
 | 1096 |  | 
 | 1097 | 	if (acpi_bus_get_device(handle, &adev)) | 
 | 1098 | 		return AE_OK; | 
 | 1099 | 	if (acpi_bus_get_status(adev) || !adev->status.present) | 
 | 1100 | 		return AE_OK; | 
 | 1101 |  | 
 | 1102 | 	memset(&info, 0, sizeof(info)); | 
 | 1103 | 	info.acpi_node.handle = handle; | 
 | 1104 | 	info.irq = -1; | 
 | 1105 |  | 
 | 1106 | 	INIT_LIST_HEAD(&resource_list); | 
 | 1107 | 	ret = acpi_dev_get_resources(adev, &resource_list, | 
 | 1108 | 				     acpi_i2c_add_resource, &info); | 
 | 1109 | 	acpi_dev_free_resource_list(&resource_list); | 
 | 1110 |  | 
 | 1111 | 	if (ret < 0 || !info.addr) | 
 | 1112 | 		return AE_OK; | 
 | 1113 |  | 
 | 1114 | 	strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type)); | 
 | 1115 | 	if (!i2c_new_device(adapter, &info)) { | 
 | 1116 | 		dev_err(&adapter->dev, | 
 | 1117 | 			"failed to add I2C device %s from ACPI\n", | 
 | 1118 | 			dev_name(&adev->dev)); | 
 | 1119 | 	} | 
 | 1120 |  | 
 | 1121 | 	return AE_OK; | 
 | 1122 | } | 
 | 1123 |  | 
 | 1124 | /** | 
 | 1125 |  * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter | 
 | 1126 |  * @adap: pointer to adapter | 
 | 1127 |  * | 
 | 1128 |  * Enumerate all I2C slave devices behind this adapter by walking the ACPI | 
 | 1129 |  * namespace. When a device is found it will be added to the Linux device | 
 | 1130 |  * model and bound to the corresponding ACPI handle. | 
 | 1131 |  */ | 
 | 1132 | static void acpi_i2c_register_devices(struct i2c_adapter *adap) | 
 | 1133 | { | 
 | 1134 | 	acpi_handle handle; | 
 | 1135 | 	acpi_status status; | 
 | 1136 |  | 
 | 1137 | 	handle = ACPI_HANDLE(adap->dev.parent); | 
 | 1138 | 	if (!handle) | 
 | 1139 | 		return; | 
 | 1140 |  | 
 | 1141 | 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, | 
 | 1142 | 				     acpi_i2c_add_device, NULL, | 
 | 1143 | 				     adap, NULL); | 
 | 1144 | 	if (ACPI_FAILURE(status)) | 
 | 1145 | 		dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); | 
 | 1146 | } | 
 | 1147 | #else | 
 | 1148 | static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {} | 
 | 1149 | #endif /* CONFIG_ACPI */ | 
 | 1150 |  | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1151 | static int i2c_do_add_adapter(struct i2c_driver *driver, | 
 | 1152 | 			      struct i2c_adapter *adap) | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1153 | { | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1154 | 	/* Detect supported devices on that bus, and instantiate them */ | 
 | 1155 | 	i2c_detect(adap, driver); | 
 | 1156 |  | 
 | 1157 | 	/* Let legacy drivers scan this bus for matching devices */ | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1158 | 	if (driver->attach_adapter) { | 
| Jean Delvare | a920ff4 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 1159 | 		dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n", | 
 | 1160 | 			 driver->driver.name); | 
| Jean Delvare | fe6fc25 | 2011-03-20 14:50:53 +0100 | [diff] [blame] | 1161 | 		dev_warn(&adap->dev, "Please use another way to instantiate " | 
 | 1162 | 			 "your i2c_client\n"); | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1163 | 		/* We ignore the return code; if it fails, too bad */ | 
 | 1164 | 		driver->attach_adapter(adap); | 
 | 1165 | 	} | 
 | 1166 | 	return 0; | 
 | 1167 | } | 
 | 1168 |  | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1169 | static int __process_new_adapter(struct device_driver *d, void *data) | 
 | 1170 | { | 
 | 1171 | 	return i2c_do_add_adapter(to_i2c_driver(d), data); | 
 | 1172 | } | 
 | 1173 |  | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1174 | static int i2c_register_adapter(struct i2c_adapter *adap) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | { | 
| Jean Delvare | d670328 | 2010-08-11 18:20:59 +0200 | [diff] [blame] | 1176 | 	int res = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 |  | 
| David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1178 | 	/* Can't register until after driver model init */ | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1179 | 	if (unlikely(WARN_ON(!i2c_bus_type.p))) { | 
 | 1180 | 		res = -EAGAIN; | 
 | 1181 | 		goto out_list; | 
 | 1182 | 	} | 
| David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1183 |  | 
| Jean Delvare | 2236baa | 2010-11-15 22:40:38 +0100 | [diff] [blame] | 1184 | 	/* Sanity checks */ | 
 | 1185 | 	if (unlikely(adap->name[0] == '\0')) { | 
 | 1186 | 		pr_err("i2c-core: Attempt to register an adapter with " | 
 | 1187 | 		       "no name!\n"); | 
 | 1188 | 		return -EINVAL; | 
 | 1189 | 	} | 
 | 1190 | 	if (unlikely(!adap->algo)) { | 
 | 1191 | 		pr_err("i2c-core: Attempt to register adapter '%s' with " | 
 | 1192 | 		       "no algo!\n", adap->name); | 
 | 1193 | 		return -EINVAL; | 
 | 1194 | 	} | 
 | 1195 |  | 
| Mika Kuoppala | 194684e | 2009-12-06 17:06:22 +0100 | [diff] [blame] | 1196 | 	rt_mutex_init(&adap->bus_lock); | 
| Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1197 | 	mutex_init(&adap->userspace_clients_lock); | 
| Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1198 | 	INIT_LIST_HEAD(&adap->userspace_clients); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 |  | 
| Jean Delvare | 8fcfef6 | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 1200 | 	/* Set default timeout to 1 second if not already set */ | 
 | 1201 | 	if (adap->timeout == 0) | 
 | 1202 | 		adap->timeout = HZ; | 
 | 1203 |  | 
| Kay Sievers | 27d9c18 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 1204 | 	dev_set_name(&adap->dev, "i2c-%d", adap->nr); | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1205 | 	adap->dev.bus = &i2c_bus_type; | 
 | 1206 | 	adap->dev.type = &i2c_adapter_type; | 
| Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1207 | 	res = device_register(&adap->dev); | 
 | 1208 | 	if (res) | 
 | 1209 | 		goto out_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 |  | 
| Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 1211 | 	dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); | 
 | 1212 |  | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1213 | #ifdef CONFIG_I2C_COMPAT | 
 | 1214 | 	res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, | 
 | 1215 | 				       adap->dev.parent); | 
 | 1216 | 	if (res) | 
 | 1217 | 		dev_warn(&adap->dev, | 
 | 1218 | 			 "Failed to create compatibility class link\n"); | 
 | 1219 | #endif | 
 | 1220 |  | 
| Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 1221 | 	/* bus recovery specific initialization */ | 
 | 1222 | 	if (adap->bus_recovery_info) { | 
 | 1223 | 		struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | 
 | 1224 |  | 
 | 1225 | 		if (!bri->recover_bus) { | 
 | 1226 | 			dev_err(&adap->dev, "No recover_bus() found, not using recovery\n"); | 
 | 1227 | 			adap->bus_recovery_info = NULL; | 
 | 1228 | 			goto exit_recovery; | 
 | 1229 | 		} | 
 | 1230 |  | 
 | 1231 | 		/* Generic GPIO recovery */ | 
 | 1232 | 		if (bri->recover_bus == i2c_generic_gpio_recovery) { | 
 | 1233 | 			if (!gpio_is_valid(bri->scl_gpio)) { | 
 | 1234 | 				dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n"); | 
 | 1235 | 				adap->bus_recovery_info = NULL; | 
 | 1236 | 				goto exit_recovery; | 
 | 1237 | 			} | 
 | 1238 |  | 
 | 1239 | 			if (gpio_is_valid(bri->sda_gpio)) | 
 | 1240 | 				bri->get_sda = get_sda_gpio_value; | 
 | 1241 | 			else | 
 | 1242 | 				bri->get_sda = NULL; | 
 | 1243 |  | 
 | 1244 | 			bri->get_scl = get_scl_gpio_value; | 
 | 1245 | 			bri->set_scl = set_scl_gpio_value; | 
 | 1246 | 		} else if (!bri->set_scl || !bri->get_scl) { | 
 | 1247 | 			/* Generic SCL recovery */ | 
 | 1248 | 			dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n"); | 
 | 1249 | 			adap->bus_recovery_info = NULL; | 
 | 1250 | 		} | 
 | 1251 | 	} | 
 | 1252 |  | 
 | 1253 | exit_recovery: | 
| Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1254 | 	/* create pre-declared device nodes */ | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1255 | 	of_i2c_register_devices(adap); | 
| Mika Westerberg | 55e71ed | 2013-08-21 17:28:23 +0300 | [diff] [blame] | 1256 | 	acpi_i2c_register_devices(adap); | 
| Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1257 |  | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1258 | 	if (adap->nr < __i2c_first_dynamic_bus_num) | 
 | 1259 | 		i2c_scan_static_board_info(adap); | 
 | 1260 |  | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1261 | 	/* Notify drivers */ | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1262 | 	mutex_lock(&core_lock); | 
| Jean Delvare | d670328 | 2010-08-11 18:20:59 +0200 | [diff] [blame] | 1263 | 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1264 | 	mutex_unlock(&core_lock); | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1265 |  | 
 | 1266 | 	return 0; | 
| Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1267 |  | 
| Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1268 | out_list: | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1269 | 	mutex_lock(&core_lock); | 
| Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1270 | 	idr_remove(&i2c_adapter_idr, adap->nr); | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1271 | 	mutex_unlock(&core_lock); | 
 | 1272 | 	return res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } | 
 | 1274 |  | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1275 | /** | 
| Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1276 |  * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 | 
 | 1277 |  * @adap: the adapter to register (with adap->nr initialized) | 
 | 1278 |  * Context: can sleep | 
 | 1279 |  * | 
 | 1280 |  * See i2c_add_numbered_adapter() for details. | 
 | 1281 |  */ | 
 | 1282 | static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) | 
 | 1283 | { | 
 | 1284 | 	int	id; | 
 | 1285 |  | 
 | 1286 | 	mutex_lock(&core_lock); | 
 | 1287 | 	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | 
 | 1288 | 		       GFP_KERNEL); | 
 | 1289 | 	mutex_unlock(&core_lock); | 
 | 1290 | 	if (id < 0) | 
 | 1291 | 		return id == -ENOSPC ? -EBUSY : id; | 
 | 1292 |  | 
 | 1293 | 	return i2c_register_adapter(adap); | 
 | 1294 | } | 
 | 1295 |  | 
 | 1296 | /** | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1297 |  * i2c_add_adapter - declare i2c adapter, use dynamic bus number | 
 | 1298 |  * @adapter: the adapter to add | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1299 |  * Context: can sleep | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1300 |  * | 
 | 1301 |  * This routine is used to declare an I2C adapter when its bus number | 
| Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1302 |  * doesn't matter or when its bus number is specified by an dt alias. | 
 | 1303 |  * Examples of bases when the bus number doesn't matter: I2C adapters | 
 | 1304 |  * dynamically added by USB links or PCI plugin cards. | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1305 |  * | 
 | 1306 |  * When this returns zero, a new bus number was allocated and stored | 
 | 1307 |  * in adap->nr, and the specified adapter became available for clients. | 
 | 1308 |  * Otherwise, a negative errno value is returned. | 
 | 1309 |  */ | 
 | 1310 | int i2c_add_adapter(struct i2c_adapter *adapter) | 
 | 1311 | { | 
| Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1312 | 	struct device *dev = &adapter->dev; | 
| Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1313 | 	int id; | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1314 |  | 
| Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1315 | 	if (dev->of_node) { | 
 | 1316 | 		id = of_alias_get_id(dev->of_node, "i2c"); | 
 | 1317 | 		if (id >= 0) { | 
 | 1318 | 			adapter->nr = id; | 
 | 1319 | 			return __i2c_add_numbered_adapter(adapter); | 
 | 1320 | 		} | 
 | 1321 | 	} | 
 | 1322 |  | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1323 | 	mutex_lock(&core_lock); | 
| Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1324 | 	id = idr_alloc(&i2c_adapter_idr, adapter, | 
 | 1325 | 		       __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1326 | 	mutex_unlock(&core_lock); | 
| Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1327 | 	if (id < 0) | 
 | 1328 | 		return id; | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1329 |  | 
 | 1330 | 	adapter->nr = id; | 
| Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1331 |  | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1332 | 	return i2c_register_adapter(adapter); | 
 | 1333 | } | 
 | 1334 | EXPORT_SYMBOL(i2c_add_adapter); | 
 | 1335 |  | 
 | 1336 | /** | 
 | 1337 |  * i2c_add_numbered_adapter - declare i2c adapter, use static bus number | 
 | 1338 |  * @adap: the adapter to register (with adap->nr initialized) | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1339 |  * Context: can sleep | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1340 |  * | 
 | 1341 |  * This routine is used to declare an I2C adapter when its bus number | 
| Randy Dunlap | 8c07e46 | 2008-03-23 20:28:20 +0100 | [diff] [blame] | 1342 |  * matters.  For example, use it for I2C adapters from system-on-chip CPUs, | 
 | 1343 |  * or otherwise built in to the system's mainboard, and where i2c_board_info | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1344 |  * is used to properly configure I2C devices. | 
 | 1345 |  * | 
| Grant Likely | 488bf31 | 2011-07-25 17:49:43 +0200 | [diff] [blame] | 1346 |  * If the requested bus number is set to -1, then this function will behave | 
 | 1347 |  * identically to i2c_add_adapter, and will dynamically assign a bus number. | 
 | 1348 |  * | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1349 |  * If no devices have pre-been declared for this bus, then be sure to | 
 | 1350 |  * register the adapter before any dynamically allocated ones.  Otherwise | 
 | 1351 |  * the required bus ID may not be available. | 
 | 1352 |  * | 
 | 1353 |  * When this returns zero, the specified adapter became available for | 
 | 1354 |  * clients using the bus number provided in adap->nr.  Also, the table | 
 | 1355 |  * of I2C devices pre-declared using i2c_register_board_info() is scanned, | 
 | 1356 |  * and the appropriate driver model device nodes are created.  Otherwise, a | 
 | 1357 |  * negative errno value is returned. | 
 | 1358 |  */ | 
 | 1359 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | 
 | 1360 | { | 
| Grant Likely | 488bf31 | 2011-07-25 17:49:43 +0200 | [diff] [blame] | 1361 | 	if (adap->nr == -1) /* -1 means dynamically assign bus id */ | 
 | 1362 | 		return i2c_add_adapter(adap); | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1363 |  | 
| Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1364 | 	return __i2c_add_numbered_adapter(adap); | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1365 | } | 
 | 1366 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | 
 | 1367 |  | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1368 | static void i2c_do_del_adapter(struct i2c_driver *driver, | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1369 | 			      struct i2c_adapter *adapter) | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1370 | { | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1371 | 	struct i2c_client *client, *_n; | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1372 |  | 
| Jean Delvare | acec211 | 2009-03-28 21:34:40 +0100 | [diff] [blame] | 1373 | 	/* Remove the devices we created ourselves as the result of hardware | 
 | 1374 | 	 * probing (using a driver's detect method) */ | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1375 | 	list_for_each_entry_safe(client, _n, &driver->clients, detected) { | 
 | 1376 | 		if (client->adapter == adapter) { | 
 | 1377 | 			dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | 
 | 1378 | 				client->name, client->addr); | 
 | 1379 | 			list_del(&client->detected); | 
 | 1380 | 			i2c_unregister_device(client); | 
 | 1381 | 		} | 
 | 1382 | 	} | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1383 | } | 
 | 1384 |  | 
| Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1385 | static int __unregister_client(struct device *dev, void *dummy) | 
 | 1386 | { | 
 | 1387 | 	struct i2c_client *client = i2c_verify_client(dev); | 
| Jean Delvare | 5219bf8 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 1388 | 	if (client && strcmp(client->name, "dummy")) | 
 | 1389 | 		i2c_unregister_device(client); | 
 | 1390 | 	return 0; | 
 | 1391 | } | 
 | 1392 |  | 
 | 1393 | static int __unregister_dummy(struct device *dev, void *dummy) | 
 | 1394 | { | 
 | 1395 | 	struct i2c_client *client = i2c_verify_client(dev); | 
| Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1396 | 	if (client) | 
 | 1397 | 		i2c_unregister_device(client); | 
 | 1398 | 	return 0; | 
 | 1399 | } | 
 | 1400 |  | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1401 | static int __process_removed_adapter(struct device_driver *d, void *data) | 
 | 1402 | { | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1403 | 	i2c_do_del_adapter(to_i2c_driver(d), data); | 
 | 1404 | 	return 0; | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1405 | } | 
 | 1406 |  | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1407 | /** | 
 | 1408 |  * i2c_del_adapter - unregister I2C adapter | 
 | 1409 |  * @adap: the adapter being unregistered | 
 | 1410 |  * Context: can sleep | 
 | 1411 |  * | 
 | 1412 |  * This unregisters an I2C adapter which was previously registered | 
 | 1413 |  * by @i2c_add_adapter or @i2c_add_numbered_adapter. | 
 | 1414 |  */ | 
| Lars-Peter Clausen | 7154630 | 2013-03-09 08:16:47 +0000 | [diff] [blame] | 1415 | void i2c_del_adapter(struct i2c_adapter *adap) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | { | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1417 | 	struct i2c_adapter *found; | 
| Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1418 | 	struct i2c_client *client, *next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 |  | 
 | 1420 | 	/* First make sure that this adapter was ever added */ | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1421 | 	mutex_lock(&core_lock); | 
 | 1422 | 	found = idr_find(&i2c_adapter_idr, adap->nr); | 
 | 1423 | 	mutex_unlock(&core_lock); | 
 | 1424 | 	if (found != adap) { | 
| Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 1425 | 		pr_debug("i2c-core: attempting to delete unregistered " | 
 | 1426 | 			 "adapter [%s]\n", adap->name); | 
| Lars-Peter Clausen | 7154630 | 2013-03-09 08:16:47 +0000 | [diff] [blame] | 1427 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | 	} | 
 | 1429 |  | 
| Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1430 | 	/* Tell drivers about this removal */ | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1431 | 	mutex_lock(&core_lock); | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1432 | 	bus_for_each_drv(&i2c_bus_type, NULL, adap, | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1433 | 			       __process_removed_adapter); | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1434 | 	mutex_unlock(&core_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 |  | 
| Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1436 | 	/* Remove devices instantiated from sysfs */ | 
| Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 1437 | 	mutex_lock_nested(&adap->userspace_clients_lock, | 
 | 1438 | 			  i2c_adapter_depth(adap)); | 
| Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1439 | 	list_for_each_entry_safe(client, next, &adap->userspace_clients, | 
 | 1440 | 				 detected) { | 
 | 1441 | 		dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, | 
 | 1442 | 			client->addr); | 
 | 1443 | 		list_del(&client->detected); | 
 | 1444 | 		i2c_unregister_device(client); | 
| Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1445 | 	} | 
| Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1446 | 	mutex_unlock(&adap->userspace_clients_lock); | 
| Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1447 |  | 
| Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1448 | 	/* Detach any active clients. This can't fail, thus we do not | 
| Jean Delvare | 5219bf8 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 1449 | 	 * check the returned value. This is a two-pass process, because | 
 | 1450 | 	 * we can't remove the dummy devices during the first pass: they | 
 | 1451 | 	 * could have been instantiated by real devices wishing to clean | 
 | 1452 | 	 * them up properly, so we give them a chance to do that first. */ | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1453 | 	device_for_each_child(&adap->dev, NULL, __unregister_client); | 
 | 1454 | 	device_for_each_child(&adap->dev, NULL, __unregister_dummy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 |  | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1456 | #ifdef CONFIG_I2C_COMPAT | 
 | 1457 | 	class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, | 
 | 1458 | 				 adap->dev.parent); | 
 | 1459 | #endif | 
 | 1460 |  | 
| Thadeu Lima de Souza Cascardo | c556752 | 2010-01-16 20:43:13 +0100 | [diff] [blame] | 1461 | 	/* device name is gone after device_unregister */ | 
 | 1462 | 	dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); | 
 | 1463 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | 	/* clean up the sysfs representation */ | 
 | 1465 | 	init_completion(&adap->dev_released); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | 	device_unregister(&adap->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 |  | 
 | 1468 | 	/* wait for sysfs to drop all references */ | 
 | 1469 | 	wait_for_completion(&adap->dev_released); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 |  | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1471 | 	/* free bus id */ | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1472 | 	mutex_lock(&core_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | 	idr_remove(&i2c_adapter_idr, adap->nr); | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1474 | 	mutex_unlock(&core_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 |  | 
| Jean Delvare | bd4bc3db | 2008-07-16 19:30:05 +0200 | [diff] [blame] | 1476 | 	/* Clear the device structure in case this adapter is ever going to be | 
 | 1477 | 	   added again */ | 
 | 1478 | 	memset(&adap->dev, 0, sizeof(adap->dev)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1480 | EXPORT_SYMBOL(i2c_del_adapter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 |  | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1482 | /* ------------------------------------------------------------------------- */ | 
 | 1483 |  | 
| Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1484 | int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *)) | 
 | 1485 | { | 
 | 1486 | 	int res; | 
 | 1487 |  | 
 | 1488 | 	mutex_lock(&core_lock); | 
 | 1489 | 	res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); | 
 | 1490 | 	mutex_unlock(&core_lock); | 
 | 1491 |  | 
 | 1492 | 	return res; | 
 | 1493 | } | 
 | 1494 | EXPORT_SYMBOL_GPL(i2c_for_each_dev); | 
 | 1495 |  | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1496 | static int __process_new_driver(struct device *dev, void *data) | 
| Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1497 | { | 
| Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1498 | 	if (dev->type != &i2c_adapter_type) | 
 | 1499 | 		return 0; | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1500 | 	return i2c_do_add_adapter(data, to_i2c_adapter(dev)); | 
| Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1501 | } | 
 | 1502 |  | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1503 | /* | 
 | 1504 |  * An i2c_driver is used with one or more i2c_client (device) nodes to access | 
| Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1505 |  * i2c slave chips, on a bus instance associated with some i2c_adapter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 |  */ | 
 | 1507 |  | 
| Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1508 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | { | 
| Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1510 | 	int res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 |  | 
| David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1512 | 	/* Can't register until after driver model init */ | 
 | 1513 | 	if (unlikely(WARN_ON(!i2c_bus_type.p))) | 
 | 1514 | 		return -EAGAIN; | 
 | 1515 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | 	/* add the driver to the list of i2c drivers in the driver core */ | 
| Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1517 | 	driver->driver.owner = owner; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | 	driver->driver.bus = &i2c_bus_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 |  | 
| Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1520 | 	/* When registration returns, the driver core | 
| David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1521 | 	 * will have called probe() for all matching-but-unbound devices. | 
 | 1522 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | 	res = driver_register(&driver->driver); | 
 | 1524 | 	if (res) | 
| Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1525 | 		return res; | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1526 |  | 
| Mark Brown | f4e8db3 | 2011-01-14 22:03:50 +0100 | [diff] [blame] | 1527 | 	/* Drivers should switch to dev_pm_ops instead. */ | 
 | 1528 | 	if (driver->suspend) | 
 | 1529 | 		pr_warn("i2c-core: driver [%s] using legacy suspend method\n", | 
 | 1530 | 			driver->driver.name); | 
 | 1531 | 	if (driver->resume) | 
 | 1532 | 		pr_warn("i2c-core: driver [%s] using legacy resume method\n", | 
 | 1533 | 			driver->driver.name); | 
 | 1534 |  | 
| Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 1535 | 	pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 |  | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1537 | 	INIT_LIST_HEAD(&driver->clients); | 
 | 1538 | 	/* Walk the adapters that are already present */ | 
| Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1539 | 	i2c_for_each_dev(driver, __process_new_driver); | 
| Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1540 |  | 
| Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1541 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } | 
| Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1543 | EXPORT_SYMBOL(i2c_register_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 |  | 
| Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1545 | static int __process_removed_driver(struct device *dev, void *data) | 
| Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1546 | { | 
| Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1547 | 	if (dev->type == &i2c_adapter_type) | 
 | 1548 | 		i2c_do_del_adapter(data, to_i2c_adapter(dev)); | 
 | 1549 | 	return 0; | 
| Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1550 | } | 
 | 1551 |  | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1552 | /** | 
 | 1553 |  * i2c_del_driver - unregister I2C driver | 
 | 1554 |  * @driver: the driver being unregistered | 
| David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1555 |  * Context: can sleep | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1556 |  */ | 
| Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 1557 | void i2c_del_driver(struct i2c_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | { | 
| Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1559 | 	i2c_for_each_dev(driver, __process_removed_driver); | 
| David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1560 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | 	driver_unregister(&driver->driver); | 
| Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 1562 | 	pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1564 | EXPORT_SYMBOL(i2c_del_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 |  | 
| David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1566 | /* ------------------------------------------------------------------------- */ | 
 | 1567 |  | 
| Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 1568 | /** | 
 | 1569 |  * i2c_use_client - increments the reference count of the i2c client structure | 
 | 1570 |  * @client: the client being referenced | 
 | 1571 |  * | 
 | 1572 |  * Each live reference to a client should be refcounted. The driver model does | 
 | 1573 |  * that automatically as part of driver binding, so that most drivers don't | 
 | 1574 |  * need to do this explicitly: they hold a reference until they're unbound | 
 | 1575 |  * from the device. | 
 | 1576 |  * | 
 | 1577 |  * A pointer to the client with the incremented reference counter is returned. | 
 | 1578 |  */ | 
 | 1579 | struct i2c_client *i2c_use_client(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | { | 
| David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1581 | 	if (client && get_device(&client->dev)) | 
 | 1582 | 		return client; | 
 | 1583 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1585 | EXPORT_SYMBOL(i2c_use_client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 |  | 
| Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 1587 | /** | 
 | 1588 |  * i2c_release_client - release a use of the i2c client structure | 
 | 1589 |  * @client: the client being no longer referenced | 
 | 1590 |  * | 
 | 1591 |  * Must be called when a user of a client is finished with it. | 
 | 1592 |  */ | 
 | 1593 | void i2c_release_client(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | { | 
| David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1595 | 	if (client) | 
 | 1596 | 		put_device(&client->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1598 | EXPORT_SYMBOL(i2c_release_client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 |  | 
| David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1600 | struct i2c_cmd_arg { | 
 | 1601 | 	unsigned	cmd; | 
 | 1602 | 	void		*arg; | 
 | 1603 | }; | 
 | 1604 |  | 
 | 1605 | static int i2c_cmd(struct device *dev, void *_arg) | 
 | 1606 | { | 
 | 1607 | 	struct i2c_client	*client = i2c_verify_client(dev); | 
 | 1608 | 	struct i2c_cmd_arg	*arg = _arg; | 
 | 1609 |  | 
 | 1610 | 	if (client && client->driver && client->driver->command) | 
 | 1611 | 		client->driver->command(client, arg->cmd, arg->arg); | 
 | 1612 | 	return 0; | 
 | 1613 | } | 
 | 1614 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) | 
 | 1616 | { | 
| David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1617 | 	struct i2c_cmd_arg	cmd_arg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 |  | 
| David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1619 | 	cmd_arg.cmd = cmd; | 
 | 1620 | 	cmd_arg.arg = arg; | 
 | 1621 | 	device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1623 | EXPORT_SYMBOL(i2c_clients_command); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 |  | 
 | 1625 | static int __init i2c_init(void) | 
 | 1626 | { | 
 | 1627 | 	int retval; | 
 | 1628 |  | 
 | 1629 | 	retval = bus_register(&i2c_bus_type); | 
 | 1630 | 	if (retval) | 
 | 1631 | 		return retval; | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1632 | #ifdef CONFIG_I2C_COMPAT | 
 | 1633 | 	i2c_adapter_compat_class = class_compat_register("i2c-adapter"); | 
 | 1634 | 	if (!i2c_adapter_compat_class) { | 
 | 1635 | 		retval = -ENOMEM; | 
 | 1636 | 		goto bus_err; | 
 | 1637 | 	} | 
 | 1638 | #endif | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1639 | 	retval = i2c_add_driver(&dummy_driver); | 
 | 1640 | 	if (retval) | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1641 | 		goto class_err; | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1642 | 	return 0; | 
 | 1643 |  | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1644 | class_err: | 
 | 1645 | #ifdef CONFIG_I2C_COMPAT | 
 | 1646 | 	class_compat_unregister(i2c_adapter_compat_class); | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1647 | bus_err: | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1648 | #endif | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1649 | 	bus_unregister(&i2c_bus_type); | 
 | 1650 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } | 
 | 1652 |  | 
 | 1653 | static void __exit i2c_exit(void) | 
 | 1654 | { | 
| David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1655 | 	i2c_del_driver(&dummy_driver); | 
| Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1656 | #ifdef CONFIG_I2C_COMPAT | 
 | 1657 | 	class_compat_unregister(i2c_adapter_compat_class); | 
 | 1658 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | 	bus_unregister(&i2c_bus_type); | 
 | 1660 | } | 
 | 1661 |  | 
| David Brownell | a10f9e7 | 2008-10-14 17:30:06 +0200 | [diff] [blame] | 1662 | /* We must initialize early, because some subsystems register i2c drivers | 
 | 1663 |  * in subsys_initcall() code, but are linked (and initialized) before i2c. | 
 | 1664 |  */ | 
 | 1665 | postcore_initcall(i2c_init); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | module_exit(i2c_exit); | 
 | 1667 |  | 
 | 1668 | /* ---------------------------------------------------- | 
 | 1669 |  * the functional interface to the i2c busses. | 
 | 1670 |  * ---------------------------------------------------- | 
 | 1671 |  */ | 
 | 1672 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1673 | /** | 
| Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 1674 |  * __i2c_transfer - unlocked flavor of i2c_transfer | 
 | 1675 |  * @adap: Handle to I2C bus | 
 | 1676 |  * @msgs: One or more messages to execute before STOP is issued to | 
 | 1677 |  *	terminate the operation; each message begins with a START. | 
 | 1678 |  * @num: Number of messages to be executed. | 
 | 1679 |  * | 
 | 1680 |  * Returns negative errno, else the number of messages executed. | 
 | 1681 |  * | 
 | 1682 |  * Adapter lock must be held when calling this function. No debug logging | 
 | 1683 |  * takes place. adap->algo->master_xfer existence isn't checked. | 
 | 1684 |  */ | 
 | 1685 | int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) | 
 | 1686 | { | 
 | 1687 | 	unsigned long orig_jiffies; | 
 | 1688 | 	int ret, try; | 
 | 1689 |  | 
 | 1690 | 	/* Retry automatically on arbitration loss */ | 
 | 1691 | 	orig_jiffies = jiffies; | 
 | 1692 | 	for (ret = 0, try = 0; try <= adap->retries; try++) { | 
 | 1693 | 		ret = adap->algo->master_xfer(adap, msgs, num); | 
 | 1694 | 		if (ret != -EAGAIN) | 
 | 1695 | 			break; | 
 | 1696 | 		if (time_after(jiffies, orig_jiffies + adap->timeout)) | 
 | 1697 | 			break; | 
 | 1698 | 	} | 
 | 1699 |  | 
 | 1700 | 	return ret; | 
 | 1701 | } | 
 | 1702 | EXPORT_SYMBOL(__i2c_transfer); | 
 | 1703 |  | 
 | 1704 | /** | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1705 |  * i2c_transfer - execute a single or combined I2C message | 
 | 1706 |  * @adap: Handle to I2C bus | 
 | 1707 |  * @msgs: One or more messages to execute before STOP is issued to | 
 | 1708 |  *	terminate the operation; each message begins with a START. | 
 | 1709 |  * @num: Number of messages to be executed. | 
 | 1710 |  * | 
 | 1711 |  * Returns negative errno, else the number of messages executed. | 
 | 1712 |  * | 
 | 1713 |  * Note that there is no requirement that each message be sent to | 
 | 1714 |  * the same slave address, although that is the most common model. | 
 | 1715 |  */ | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1716 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { | 
| Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 1718 | 	int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1720 | 	/* REVISIT the fault reporting model here is weak: | 
 | 1721 | 	 * | 
 | 1722 | 	 *  - When we get an error after receiving N bytes from a slave, | 
 | 1723 | 	 *    there is no way to report "N". | 
 | 1724 | 	 * | 
 | 1725 | 	 *  - When we get a NAK after transmitting N bytes to a slave, | 
 | 1726 | 	 *    there is no way to report "N" ... or to let the master | 
 | 1727 | 	 *    continue executing the rest of this combined message, if | 
 | 1728 | 	 *    that's the appropriate response. | 
 | 1729 | 	 * | 
 | 1730 | 	 *  - When for example "num" is two and we successfully complete | 
 | 1731 | 	 *    the first message but get an error part way through the | 
 | 1732 | 	 *    second, it's unclear whether that should be reported as | 
 | 1733 | 	 *    one (discarding status on the second message) or errno | 
 | 1734 | 	 *    (discarding status on the first one). | 
 | 1735 | 	 */ | 
 | 1736 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | 	if (adap->algo->master_xfer) { | 
 | 1738 | #ifdef DEBUG | 
 | 1739 | 		for (ret = 0; ret < num; ret++) { | 
 | 1740 | 			dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " | 
| Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1741 | 				"len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) | 
 | 1742 | 				? 'R' : 'W', msgs[ret].addr, msgs[ret].len, | 
 | 1743 | 				(msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | 		} | 
 | 1745 | #endif | 
 | 1746 |  | 
| Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1747 | 		if (in_atomic() || irqs_disabled()) { | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 1748 | 			ret = i2c_trylock_adapter(adap); | 
| Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1749 | 			if (!ret) | 
 | 1750 | 				/* I2C activity is ongoing. */ | 
 | 1751 | 				return -EAGAIN; | 
 | 1752 | 		} else { | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 1753 | 			i2c_lock_adapter(adap); | 
| Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1754 | 		} | 
 | 1755 |  | 
| Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 1756 | 		ret = __i2c_transfer(adap, msgs, num); | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 1757 | 		i2c_unlock_adapter(adap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 |  | 
 | 1759 | 		return ret; | 
 | 1760 | 	} else { | 
 | 1761 | 		dev_dbg(&adap->dev, "I2C level transfers not supported\n"); | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1762 | 		return -EOPNOTSUPP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | 	} | 
 | 1764 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1765 | EXPORT_SYMBOL(i2c_transfer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1767 | /** | 
 | 1768 |  * i2c_master_send - issue a single I2C message in master transmit mode | 
 | 1769 |  * @client: Handle to slave device | 
 | 1770 |  * @buf: Data that will be written to the slave | 
| Zhangfei Gao | 0c43ea5 | 2010-03-02 12:23:49 +0100 | [diff] [blame] | 1771 |  * @count: How many bytes to write, must be less than 64k since msg.len is u16 | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1772 |  * | 
 | 1773 |  * Returns negative errno, or else the number of bytes written. | 
 | 1774 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 1775 | int i2c_master_send(const struct i2c_client *client, const char *buf, int count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { | 
 | 1777 | 	int ret; | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 1778 | 	struct i2c_adapter *adap = client->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | 	struct i2c_msg msg; | 
 | 1780 |  | 
| Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1781 | 	msg.addr = client->addr; | 
 | 1782 | 	msg.flags = client->flags & I2C_M_TEN; | 
 | 1783 | 	msg.len = count; | 
 | 1784 | 	msg.buf = (char *)buf; | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1785 |  | 
| Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1786 | 	ret = i2c_transfer(adap, &msg, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 |  | 
| Wolfram Sang | 834aa6f | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 1788 | 	/* | 
 | 1789 | 	 * If everything went ok (i.e. 1 msg transmitted), return #bytes | 
 | 1790 | 	 * transmitted, else error code. | 
 | 1791 | 	 */ | 
| Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1792 | 	return (ret == 1) ? count : ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1794 | EXPORT_SYMBOL(i2c_master_send); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1796 | /** | 
 | 1797 |  * i2c_master_recv - issue a single I2C message in master receive mode | 
 | 1798 |  * @client: Handle to slave device | 
 | 1799 |  * @buf: Where to store data read from slave | 
| Zhangfei Gao | 0c43ea5 | 2010-03-02 12:23:49 +0100 | [diff] [blame] | 1800 |  * @count: How many bytes to read, must be less than 64k since msg.len is u16 | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1801 |  * | 
 | 1802 |  * Returns negative errno, or else the number of bytes read. | 
 | 1803 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 1804 | int i2c_master_recv(const struct i2c_client *client, char *buf, int count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | { | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 1806 | 	struct i2c_adapter *adap = client->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | 	struct i2c_msg msg; | 
 | 1808 | 	int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 |  | 
| Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1810 | 	msg.addr = client->addr; | 
 | 1811 | 	msg.flags = client->flags & I2C_M_TEN; | 
 | 1812 | 	msg.flags |= I2C_M_RD; | 
 | 1813 | 	msg.len = count; | 
 | 1814 | 	msg.buf = buf; | 
 | 1815 |  | 
 | 1816 | 	ret = i2c_transfer(adap, &msg, 1); | 
 | 1817 |  | 
| Wolfram Sang | 834aa6f | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 1818 | 	/* | 
 | 1819 | 	 * If everything went ok (i.e. 1 msg received), return #bytes received, | 
 | 1820 | 	 * else error code. | 
 | 1821 | 	 */ | 
| Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1822 | 	return (ret == 1) ? count : ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1824 | EXPORT_SYMBOL(i2c_master_recv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | /* ---------------------------------------------------- | 
 | 1827 |  * the i2c address scanning function | 
 | 1828 |  * Will not work for 10-bit addresses! | 
 | 1829 |  * ---------------------------------------------------- | 
 | 1830 |  */ | 
| Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 1831 |  | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1832 | /* | 
 | 1833 |  * Legacy default probe function, mostly relevant for SMBus. The default | 
 | 1834 |  * probe method is a quick write, but it is known to corrupt the 24RF08 | 
 | 1835 |  * EEPROMs due to a state machine bug, and could also irreversibly | 
 | 1836 |  * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, | 
 | 1837 |  * we use a short byte read instead. Also, some bus drivers don't implement | 
 | 1838 |  * quick write, so we fallback to a byte read in that case too. | 
 | 1839 |  * On x86, there is another special case for FSC hardware monitoring chips, | 
 | 1840 |  * which want regular byte reads (address 0x73.) Fortunately, these are the | 
 | 1841 |  * only known chips using this I2C address on PC hardware. | 
 | 1842 |  * Returns 1 if probe succeeded, 0 if not. | 
 | 1843 |  */ | 
 | 1844 | static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) | 
 | 1845 | { | 
 | 1846 | 	int err; | 
 | 1847 | 	union i2c_smbus_data dummy; | 
 | 1848 |  | 
 | 1849 | #ifdef CONFIG_X86 | 
 | 1850 | 	if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) | 
 | 1851 | 	 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) | 
 | 1852 | 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | 
 | 1853 | 				     I2C_SMBUS_BYTE_DATA, &dummy); | 
 | 1854 | 	else | 
 | 1855 | #endif | 
| Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 1856 | 	if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) | 
 | 1857 | 	 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1858 | 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, | 
 | 1859 | 				     I2C_SMBUS_QUICK, NULL); | 
| Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 1860 | 	else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) | 
 | 1861 | 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | 
 | 1862 | 				     I2C_SMBUS_BYTE, &dummy); | 
 | 1863 | 	else { | 
| Andy Lutomirski | d63a9e8 | 2013-07-17 13:27:59 -0700 | [diff] [blame] | 1864 | 		dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", | 
 | 1865 | 			 addr); | 
| Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 1866 | 		err = -EOPNOTSUPP; | 
 | 1867 | 	} | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1868 |  | 
 | 1869 | 	return err >= 0; | 
 | 1870 | } | 
 | 1871 |  | 
| Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 1872 | static int i2c_detect_address(struct i2c_client *temp_client, | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1873 | 			      struct i2c_driver *driver) | 
 | 1874 | { | 
 | 1875 | 	struct i2c_board_info info; | 
 | 1876 | 	struct i2c_adapter *adapter = temp_client->adapter; | 
 | 1877 | 	int addr = temp_client->addr; | 
 | 1878 | 	int err; | 
 | 1879 |  | 
 | 1880 | 	/* Make sure the address is valid */ | 
| Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 1881 | 	err = i2c_check_addr_validity(addr); | 
 | 1882 | 	if (err) { | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1883 | 		dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", | 
 | 1884 | 			 addr); | 
| Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 1885 | 		return err; | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1886 | 	} | 
 | 1887 |  | 
 | 1888 | 	/* Skip if already in use */ | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 1889 | 	if (i2c_check_addr_busy(adapter, addr)) | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1890 | 		return 0; | 
 | 1891 |  | 
| Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 1892 | 	/* Make sure there is something at this address */ | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1893 | 	if (!i2c_default_probe(adapter, addr)) | 
 | 1894 | 		return 0; | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1895 |  | 
 | 1896 | 	/* Finally call the custom detection function */ | 
 | 1897 | 	memset(&info, 0, sizeof(struct i2c_board_info)); | 
 | 1898 | 	info.addr = addr; | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1899 | 	err = driver->detect(temp_client, &info); | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1900 | 	if (err) { | 
 | 1901 | 		/* -ENODEV is returned if the detection fails. We catch it | 
 | 1902 | 		   here as this isn't an error. */ | 
 | 1903 | 		return err == -ENODEV ? 0 : err; | 
 | 1904 | 	} | 
 | 1905 |  | 
 | 1906 | 	/* Consistency check */ | 
 | 1907 | 	if (info.type[0] == '\0') { | 
 | 1908 | 		dev_err(&adapter->dev, "%s detection function provided " | 
 | 1909 | 			"no name for 0x%x\n", driver->driver.name, | 
 | 1910 | 			addr); | 
 | 1911 | 	} else { | 
 | 1912 | 		struct i2c_client *client; | 
 | 1913 |  | 
 | 1914 | 		/* Detection succeeded, instantiate the device */ | 
 | 1915 | 		dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", | 
 | 1916 | 			info.type, info.addr); | 
 | 1917 | 		client = i2c_new_device(adapter, &info); | 
 | 1918 | 		if (client) | 
 | 1919 | 			list_add_tail(&client->detected, &driver->clients); | 
 | 1920 | 		else | 
 | 1921 | 			dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", | 
 | 1922 | 				info.type, info.addr); | 
 | 1923 | 	} | 
 | 1924 | 	return 0; | 
 | 1925 | } | 
 | 1926 |  | 
 | 1927 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | 
 | 1928 | { | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1929 | 	const unsigned short *address_list; | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1930 | 	struct i2c_client *temp_client; | 
 | 1931 | 	int i, err = 0; | 
 | 1932 | 	int adap_id = i2c_adapter_id(adapter); | 
 | 1933 |  | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1934 | 	address_list = driver->address_list; | 
 | 1935 | 	if (!driver->detect || !address_list) | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1936 | 		return 0; | 
 | 1937 |  | 
| Jean Delvare | 51b54ba | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 1938 | 	/* Stop here if the classes do not match */ | 
 | 1939 | 	if (!(adapter->class & driver->class)) | 
 | 1940 | 		return 0; | 
 | 1941 |  | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1942 | 	/* Set up a temporary client to help detect callback */ | 
 | 1943 | 	temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | 
 | 1944 | 	if (!temp_client) | 
 | 1945 | 		return -ENOMEM; | 
 | 1946 | 	temp_client->adapter = adapter; | 
 | 1947 |  | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1948 | 	for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1949 | 		dev_dbg(&adapter->dev, "found normal entry for adapter %d, " | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1950 | 			"addr 0x%02x\n", adap_id, address_list[i]); | 
 | 1951 | 		temp_client->addr = address_list[i]; | 
| Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 1952 | 		err = i2c_detect_address(temp_client, driver); | 
| Jean Delvare | 51b54ba | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 1953 | 		if (unlikely(err)) | 
 | 1954 | 			break; | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1955 | 	} | 
 | 1956 |  | 
| Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1957 | 	kfree(temp_client); | 
 | 1958 | 	return err; | 
 | 1959 | } | 
 | 1960 |  | 
| Jean Delvare | d44f19d | 2010-08-11 18:20:57 +0200 | [diff] [blame] | 1961 | int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) | 
 | 1962 | { | 
 | 1963 | 	return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | 
 | 1964 | 			      I2C_SMBUS_QUICK, NULL) >= 0; | 
 | 1965 | } | 
 | 1966 | EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); | 
 | 1967 |  | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1968 | struct i2c_client * | 
 | 1969 | i2c_new_probed_device(struct i2c_adapter *adap, | 
 | 1970 | 		      struct i2c_board_info *info, | 
| Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 1971 | 		      unsigned short const *addr_list, | 
 | 1972 | 		      int (*probe)(struct i2c_adapter *, unsigned short addr)) | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1973 | { | 
 | 1974 | 	int i; | 
 | 1975 |  | 
| Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 1976 | 	if (!probe) | 
| Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 1977 | 		probe = i2c_default_probe; | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1978 |  | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1979 | 	for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { | 
 | 1980 | 		/* Check address validity */ | 
| Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 1981 | 		if (i2c_check_addr_validity(addr_list[i]) < 0) { | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1982 | 			dev_warn(&adap->dev, "Invalid 7-bit address " | 
 | 1983 | 				 "0x%02x\n", addr_list[i]); | 
 | 1984 | 			continue; | 
 | 1985 | 		} | 
 | 1986 |  | 
 | 1987 | 		/* Check address availability */ | 
| Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 1988 | 		if (i2c_check_addr_busy(adap, addr_list[i])) { | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1989 | 			dev_dbg(&adap->dev, "Address 0x%02x already in " | 
 | 1990 | 				"use, not probing\n", addr_list[i]); | 
 | 1991 | 			continue; | 
 | 1992 | 		} | 
 | 1993 |  | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1994 | 		/* Test address responsiveness */ | 
| Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 1995 | 		if (probe(adap, addr_list[i])) | 
| Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 1996 | 			break; | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1997 | 	} | 
| Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1998 |  | 
 | 1999 | 	if (addr_list[i] == I2C_CLIENT_END) { | 
 | 2000 | 		dev_dbg(&adap->dev, "Probing failed, no device found\n"); | 
 | 2001 | 		return NULL; | 
 | 2002 | 	} | 
 | 2003 |  | 
 | 2004 | 	info->addr = addr_list[i]; | 
 | 2005 | 	return i2c_new_device(adap, info); | 
 | 2006 | } | 
 | 2007 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); | 
 | 2008 |  | 
| Jean Delvare | d735b34 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 2009 | struct i2c_adapter *i2c_get_adapter(int nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | 	struct i2c_adapter *adapter; | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2012 |  | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 2013 | 	mutex_lock(&core_lock); | 
| Jean Delvare | d735b34 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 2014 | 	adapter = idr_find(&i2c_adapter_idr, nr); | 
| Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 2015 | 	if (adapter && !try_module_get(adapter->owner)) | 
 | 2016 | 		adapter = NULL; | 
 | 2017 |  | 
| Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 2018 | 	mutex_unlock(&core_lock); | 
| Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 2019 | 	return adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2021 | EXPORT_SYMBOL(i2c_get_adapter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 |  | 
 | 2023 | void i2c_put_adapter(struct i2c_adapter *adap) | 
 | 2024 | { | 
| Sebastian Hesselbarth | c66c4cc | 2013-08-01 14:10:46 +0200 | [diff] [blame] | 2025 | 	if (adap) | 
 | 2026 | 		module_put(adap->owner); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2028 | EXPORT_SYMBOL(i2c_put_adapter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 |  | 
 | 2030 | /* The SMBus parts */ | 
 | 2031 |  | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2032 | #define POLY    (0x1070U << 3) | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2033 | static u8 crc8(u16 data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { | 
 | 2035 | 	int i; | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2036 |  | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2037 | 	for (i = 0; i < 8; i++) { | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2038 | 		if (data & 0x8000) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | 			data = data ^ POLY; | 
 | 2040 | 		data = data << 1; | 
 | 2041 | 	} | 
 | 2042 | 	return (u8)(data >> 8); | 
 | 2043 | } | 
 | 2044 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2045 | /* Incremental CRC8 over count bytes in the array pointed to by p */ | 
 | 2046 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { | 
 | 2048 | 	int i; | 
 | 2049 |  | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2050 | 	for (i = 0; i < count; i++) | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2051 | 		crc = crc8((crc ^ p[i]) << 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | 	return crc; | 
 | 2053 | } | 
 | 2054 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2055 | /* Assume a 7-bit address, which is reasonable for SMBus */ | 
 | 2056 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | { | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2058 | 	/* The address will be sent first */ | 
 | 2059 | 	u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); | 
 | 2060 | 	pec = i2c_smbus_pec(pec, &addr, 1); | 
 | 2061 |  | 
 | 2062 | 	/* The data buffer follows */ | 
 | 2063 | 	return i2c_smbus_pec(pec, msg->buf, msg->len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } | 
 | 2065 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2066 | /* Used for write only transactions */ | 
 | 2067 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | { | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2069 | 	msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); | 
 | 2070 | 	msg->len++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } | 
 | 2072 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2073 | /* Return <0 on CRC error | 
 | 2074 |    If there was a write before this read (most cases) we need to take the | 
 | 2075 |    partial CRC from the write part into account. | 
 | 2076 |    Note that this function does modify the message (we need to decrease the | 
 | 2077 |    message length to hide the CRC byte from the caller). */ | 
 | 2078 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2080 | 	u8 rpec = msg->buf[--msg->len]; | 
 | 2081 | 	cpec = i2c_smbus_msg_pec(cpec, msg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | 	if (rpec != cpec) { | 
 | 2084 | 		pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", | 
 | 2085 | 			rpec, cpec); | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2086 | 		return -EBADMSG; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | 	} | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2088 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | } | 
 | 2090 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2091 | /** | 
 | 2092 |  * i2c_smbus_read_byte - SMBus "receive byte" protocol | 
 | 2093 |  * @client: Handle to slave device | 
 | 2094 |  * | 
 | 2095 |  * This executes the SMBus "receive byte" protocol, returning negative errno | 
 | 2096 |  * else the byte received from the device. | 
 | 2097 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2098 | s32 i2c_smbus_read_byte(const struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | { | 
 | 2100 | 	union i2c_smbus_data data; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2101 | 	int status; | 
 | 2102 |  | 
 | 2103 | 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2104 | 				I2C_SMBUS_READ, 0, | 
 | 2105 | 				I2C_SMBUS_BYTE, &data); | 
 | 2106 | 	return (status < 0) ? status : data.byte; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2108 | EXPORT_SYMBOL(i2c_smbus_read_byte); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2110 | /** | 
 | 2111 |  * i2c_smbus_write_byte - SMBus "send byte" protocol | 
 | 2112 |  * @client: Handle to slave device | 
 | 2113 |  * @value: Byte to be sent | 
 | 2114 |  * | 
 | 2115 |  * This executes the SMBus "send byte" protocol, returning negative errno | 
 | 2116 |  * else zero on success. | 
 | 2117 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2118 | s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | { | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2120 | 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2121 | 	                      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2123 | EXPORT_SYMBOL(i2c_smbus_write_byte); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2125 | /** | 
 | 2126 |  * i2c_smbus_read_byte_data - SMBus "read byte" protocol | 
 | 2127 |  * @client: Handle to slave device | 
 | 2128 |  * @command: Byte interpreted by slave | 
 | 2129 |  * | 
 | 2130 |  * This executes the SMBus "read byte" protocol, returning negative errno | 
 | 2131 |  * else a data byte received from the device. | 
 | 2132 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2133 | s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | { | 
 | 2135 | 	union i2c_smbus_data data; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2136 | 	int status; | 
 | 2137 |  | 
 | 2138 | 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2139 | 				I2C_SMBUS_READ, command, | 
 | 2140 | 				I2C_SMBUS_BYTE_DATA, &data); | 
 | 2141 | 	return (status < 0) ? status : data.byte; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2143 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2145 | /** | 
 | 2146 |  * i2c_smbus_write_byte_data - SMBus "write byte" protocol | 
 | 2147 |  * @client: Handle to slave device | 
 | 2148 |  * @command: Byte interpreted by slave | 
 | 2149 |  * @value: Byte being written | 
 | 2150 |  * | 
 | 2151 |  * This executes the SMBus "write byte" protocol, returning negative errno | 
 | 2152 |  * else zero on success. | 
 | 2153 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2154 | s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, | 
 | 2155 | 			      u8 value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { | 
 | 2157 | 	union i2c_smbus_data data; | 
 | 2158 | 	data.byte = value; | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2159 | 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2160 | 			      I2C_SMBUS_WRITE, command, | 
 | 2161 | 			      I2C_SMBUS_BYTE_DATA, &data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2163 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2165 | /** | 
 | 2166 |  * i2c_smbus_read_word_data - SMBus "read word" protocol | 
 | 2167 |  * @client: Handle to slave device | 
 | 2168 |  * @command: Byte interpreted by slave | 
 | 2169 |  * | 
 | 2170 |  * This executes the SMBus "read word" protocol, returning negative errno | 
 | 2171 |  * else a 16-bit unsigned "word" received from the device. | 
 | 2172 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2173 | s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | { | 
 | 2175 | 	union i2c_smbus_data data; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2176 | 	int status; | 
 | 2177 |  | 
 | 2178 | 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2179 | 				I2C_SMBUS_READ, command, | 
 | 2180 | 				I2C_SMBUS_WORD_DATA, &data); | 
 | 2181 | 	return (status < 0) ? status : data.word; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2183 | EXPORT_SYMBOL(i2c_smbus_read_word_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2185 | /** | 
 | 2186 |  * i2c_smbus_write_word_data - SMBus "write word" protocol | 
 | 2187 |  * @client: Handle to slave device | 
 | 2188 |  * @command: Byte interpreted by slave | 
 | 2189 |  * @value: 16-bit "word" being written | 
 | 2190 |  * | 
 | 2191 |  * This executes the SMBus "write word" protocol, returning negative errno | 
 | 2192 |  * else zero on success. | 
 | 2193 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2194 | s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, | 
 | 2195 | 			      u16 value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | { | 
 | 2197 | 	union i2c_smbus_data data; | 
 | 2198 | 	data.word = value; | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2199 | 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2200 | 			      I2C_SMBUS_WRITE, command, | 
 | 2201 | 			      I2C_SMBUS_WORD_DATA, &data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2203 | EXPORT_SYMBOL(i2c_smbus_write_word_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 |  | 
| David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2205 | /** | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2206 |  * i2c_smbus_read_block_data - SMBus "block read" protocol | 
| David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2207 |  * @client: Handle to slave device | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2208 |  * @command: Byte interpreted by slave | 
| David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2209 |  * @values: Byte array into which data will be read; big enough to hold | 
 | 2210 |  *	the data returned by the slave.  SMBus allows at most 32 bytes. | 
 | 2211 |  * | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2212 |  * This executes the SMBus "block read" protocol, returning negative errno | 
 | 2213 |  * else the number of data bytes in the slave's response. | 
| David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2214 |  * | 
 | 2215 |  * Note that using this function requires that the client's adapter support | 
 | 2216 |  * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers | 
 | 2217 |  * support this; its emulation through I2C messaging relies on a specific | 
 | 2218 |  * mechanism (I2C_M_RECV_LEN) which may not be implemented. | 
 | 2219 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2220 | s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, | 
| Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2221 | 			      u8 *values) | 
 | 2222 | { | 
 | 2223 | 	union i2c_smbus_data data; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2224 | 	int status; | 
| Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2225 |  | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2226 | 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2227 | 				I2C_SMBUS_READ, command, | 
 | 2228 | 				I2C_SMBUS_BLOCK_DATA, &data); | 
 | 2229 | 	if (status) | 
 | 2230 | 		return status; | 
| Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2231 |  | 
 | 2232 | 	memcpy(values, &data.block[1], data.block[0]); | 
 | 2233 | 	return data.block[0]; | 
 | 2234 | } | 
 | 2235 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | 
 | 2236 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2237 | /** | 
 | 2238 |  * i2c_smbus_write_block_data - SMBus "block write" protocol | 
 | 2239 |  * @client: Handle to slave device | 
 | 2240 |  * @command: Byte interpreted by slave | 
 | 2241 |  * @length: Size of data block; SMBus allows at most 32 bytes | 
 | 2242 |  * @values: Byte array which will be written. | 
 | 2243 |  * | 
 | 2244 |  * This executes the SMBus "block write" protocol, returning negative errno | 
 | 2245 |  * else zero on success. | 
 | 2246 |  */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2247 | s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, | 
| Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 2248 | 			       u8 length, const u8 *values) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | { | 
 | 2250 | 	union i2c_smbus_data data; | 
| Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2251 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | 	if (length > I2C_SMBUS_BLOCK_MAX) | 
 | 2253 | 		length = I2C_SMBUS_BLOCK_MAX; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | 	data.block[0] = length; | 
| Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2255 | 	memcpy(&data.block[1], values, length); | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2256 | 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2257 | 			      I2C_SMBUS_WRITE, command, | 
 | 2258 | 			      I2C_SMBUS_BLOCK_DATA, &data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2260 | EXPORT_SYMBOL(i2c_smbus_write_block_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 |  | 
 | 2262 | /* Returns the number of read bytes */ | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2263 | s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, | 
| Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2264 | 				  u8 length, u8 *values) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | { | 
 | 2266 | 	union i2c_smbus_data data; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2267 | 	int status; | 
| Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2268 |  | 
| Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2269 | 	if (length > I2C_SMBUS_BLOCK_MAX) | 
 | 2270 | 		length = I2C_SMBUS_BLOCK_MAX; | 
 | 2271 | 	data.block[0] = length; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2272 | 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2273 | 				I2C_SMBUS_READ, command, | 
 | 2274 | 				I2C_SMBUS_I2C_BLOCK_DATA, &data); | 
 | 2275 | 	if (status < 0) | 
 | 2276 | 		return status; | 
| Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2277 |  | 
 | 2278 | 	memcpy(values, &data.block[1], data.block[0]); | 
 | 2279 | 	return data.block[0]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2281 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 |  | 
| Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2283 | s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, | 
| Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 2284 | 				   u8 length, const u8 *values) | 
| Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 2285 | { | 
 | 2286 | 	union i2c_smbus_data data; | 
 | 2287 |  | 
 | 2288 | 	if (length > I2C_SMBUS_BLOCK_MAX) | 
 | 2289 | 		length = I2C_SMBUS_BLOCK_MAX; | 
 | 2290 | 	data.block[0] = length; | 
 | 2291 | 	memcpy(data.block + 1, values, length); | 
 | 2292 | 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
 | 2293 | 			      I2C_SMBUS_WRITE, command, | 
 | 2294 | 			      I2C_SMBUS_I2C_BLOCK_DATA, &data); | 
 | 2295 | } | 
| David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2296 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); | 
| Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 2297 |  | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2298 | /* Simulate a SMBus command using the i2c protocol | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 |    No checking of parameters is done!  */ | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2300 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, | 
 | 2301 | 				   unsigned short flags, | 
 | 2302 | 				   char read_write, u8 command, int size, | 
 | 2303 | 				   union i2c_smbus_data *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | { | 
 | 2305 | 	/* So we need to generate a series of msgs. In the case of writing, we | 
 | 2306 | 	  need to use only one message; when reading, we need two. We initialize | 
 | 2307 | 	  most things with sane defaults, to keep the code below somewhat | 
 | 2308 | 	  simpler. */ | 
| Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 2309 | 	unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; | 
 | 2310 | 	unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2311 | 	int num = read_write == I2C_SMBUS_READ ? 2 : 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | 	int i; | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2313 | 	u8 partial_pec = 0; | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2314 | 	int status; | 
| Shubhrajyoti D | 230da09 | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 2315 | 	struct i2c_msg msg[2] = { | 
 | 2316 | 		{ | 
 | 2317 | 			.addr = addr, | 
 | 2318 | 			.flags = flags, | 
 | 2319 | 			.len = 1, | 
 | 2320 | 			.buf = msgbuf0, | 
 | 2321 | 		}, { | 
 | 2322 | 			.addr = addr, | 
 | 2323 | 			.flags = flags | I2C_M_RD, | 
 | 2324 | 			.len = 0, | 
 | 2325 | 			.buf = msgbuf1, | 
 | 2326 | 		}, | 
 | 2327 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 |  | 
 | 2329 | 	msgbuf0[0] = command; | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2330 | 	switch (size) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | 	case I2C_SMBUS_QUICK: | 
 | 2332 | 		msg[0].len = 0; | 
 | 2333 | 		/* Special case: The read/write field is used as data */ | 
| Roel Kluin | f29d2e0 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 2334 | 		msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? | 
 | 2335 | 					I2C_M_RD : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | 		num = 1; | 
 | 2337 | 		break; | 
 | 2338 | 	case I2C_SMBUS_BYTE: | 
 | 2339 | 		if (read_write == I2C_SMBUS_READ) { | 
 | 2340 | 			/* Special case: only a read! */ | 
 | 2341 | 			msg[0].flags = I2C_M_RD | flags; | 
 | 2342 | 			num = 1; | 
 | 2343 | 		} | 
 | 2344 | 		break; | 
 | 2345 | 	case I2C_SMBUS_BYTE_DATA: | 
 | 2346 | 		if (read_write == I2C_SMBUS_READ) | 
 | 2347 | 			msg[1].len = 1; | 
 | 2348 | 		else { | 
 | 2349 | 			msg[0].len = 2; | 
 | 2350 | 			msgbuf0[1] = data->byte; | 
 | 2351 | 		} | 
 | 2352 | 		break; | 
 | 2353 | 	case I2C_SMBUS_WORD_DATA: | 
 | 2354 | 		if (read_write == I2C_SMBUS_READ) | 
 | 2355 | 			msg[1].len = 2; | 
 | 2356 | 		else { | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2357 | 			msg[0].len = 3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | 			msgbuf0[1] = data->word & 0xff; | 
| Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 2359 | 			msgbuf0[2] = data->word >> 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | 		} | 
 | 2361 | 		break; | 
 | 2362 | 	case I2C_SMBUS_PROC_CALL: | 
 | 2363 | 		num = 2; /* Special case */ | 
 | 2364 | 		read_write = I2C_SMBUS_READ; | 
 | 2365 | 		msg[0].len = 3; | 
 | 2366 | 		msg[1].len = 2; | 
 | 2367 | 		msgbuf0[1] = data->word & 0xff; | 
| Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 2368 | 		msgbuf0[2] = data->word >> 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | 		break; | 
 | 2370 | 	case I2C_SMBUS_BLOCK_DATA: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | 		if (read_write == I2C_SMBUS_READ) { | 
| Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2372 | 			msg[1].flags |= I2C_M_RECV_LEN; | 
 | 2373 | 			msg[1].len = 1; /* block length will be added by | 
 | 2374 | 					   the underlying bus driver */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | 		} else { | 
 | 2376 | 			msg[0].len = data->block[0] + 2; | 
 | 2377 | 			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2378 | 				dev_err(&adapter->dev, | 
 | 2379 | 					"Invalid block write size %d\n", | 
 | 2380 | 					data->block[0]); | 
 | 2381 | 				return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | 			} | 
| Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 2383 | 			for (i = 1; i < msg[0].len; i++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | 				msgbuf0[i] = data->block[i-1]; | 
 | 2385 | 		} | 
 | 2386 | 		break; | 
 | 2387 | 	case I2C_SMBUS_BLOCK_PROC_CALL: | 
| Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2388 | 		num = 2; /* Another special case */ | 
 | 2389 | 		read_write = I2C_SMBUS_READ; | 
 | 2390 | 		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2391 | 			dev_err(&adapter->dev, | 
 | 2392 | 				"Invalid block write size %d\n", | 
| Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2393 | 				data->block[0]); | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2394 | 			return -EINVAL; | 
| Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2395 | 		} | 
 | 2396 | 		msg[0].len = data->block[0] + 2; | 
 | 2397 | 		for (i = 1; i < msg[0].len; i++) | 
 | 2398 | 			msgbuf0[i] = data->block[i-1]; | 
 | 2399 | 		msg[1].flags |= I2C_M_RECV_LEN; | 
 | 2400 | 		msg[1].len = 1; /* block length will be added by | 
 | 2401 | 				   the underlying bus driver */ | 
 | 2402 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | 	case I2C_SMBUS_I2C_BLOCK_DATA: | 
 | 2404 | 		if (read_write == I2C_SMBUS_READ) { | 
| Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2405 | 			msg[1].len = data->block[0]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | 		} else { | 
 | 2407 | 			msg[0].len = data->block[0] + 1; | 
| Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 2408 | 			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2409 | 				dev_err(&adapter->dev, | 
 | 2410 | 					"Invalid block write size %d\n", | 
 | 2411 | 					data->block[0]); | 
 | 2412 | 				return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | 			} | 
 | 2414 | 			for (i = 1; i <= data->block[0]; i++) | 
 | 2415 | 				msgbuf0[i] = data->block[i]; | 
 | 2416 | 		} | 
 | 2417 | 		break; | 
 | 2418 | 	default: | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2419 | 		dev_err(&adapter->dev, "Unsupported transaction %d\n", size); | 
 | 2420 | 		return -EOPNOTSUPP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | 	} | 
 | 2422 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2423 | 	i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK | 
 | 2424 | 				      && size != I2C_SMBUS_I2C_BLOCK_DATA); | 
 | 2425 | 	if (i) { | 
 | 2426 | 		/* Compute PEC if first message is a write */ | 
 | 2427 | 		if (!(msg[0].flags & I2C_M_RD)) { | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2428 | 			if (num == 1) /* Write only */ | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2429 | 				i2c_smbus_add_pec(&msg[0]); | 
 | 2430 | 			else /* Write followed by read */ | 
 | 2431 | 				partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | 
 | 2432 | 		} | 
 | 2433 | 		/* Ask for PEC if last message is a read */ | 
 | 2434 | 		if (msg[num-1].flags & I2C_M_RD) | 
| David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2435 | 			msg[num-1].len++; | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2436 | 	} | 
 | 2437 |  | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2438 | 	status = i2c_transfer(adapter, msg, num); | 
 | 2439 | 	if (status < 0) | 
 | 2440 | 		return status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 |  | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2442 | 	/* Check PEC if last message is a read */ | 
 | 2443 | 	if (i && (msg[num-1].flags & I2C_M_RD)) { | 
| David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2444 | 		status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); | 
 | 2445 | 		if (status < 0) | 
 | 2446 | 			return status; | 
| Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2447 | 	} | 
 | 2448 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | 	if (read_write == I2C_SMBUS_READ) | 
| Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2450 | 		switch (size) { | 
 | 2451 | 		case I2C_SMBUS_BYTE: | 
 | 2452 | 			data->byte = msgbuf0[0]; | 
 | 2453 | 			break; | 
 | 2454 | 		case I2C_SMBUS_BYTE_DATA: | 
 | 2455 | 			data->byte = msgbuf1[0]; | 
 | 2456 | 			break; | 
 | 2457 | 		case I2C_SMBUS_WORD_DATA: | 
 | 2458 | 		case I2C_SMBUS_PROC_CALL: | 
 | 2459 | 			data->word = msgbuf1[0] | (msgbuf1[1] << 8); | 
 | 2460 | 			break; | 
 | 2461 | 		case I2C_SMBUS_I2C_BLOCK_DATA: | 
 | 2462 | 			for (i = 0; i < data->block[0]; i++) | 
 | 2463 | 				data->block[i+1] = msgbuf1[i]; | 
 | 2464 | 			break; | 
 | 2465 | 		case I2C_SMBUS_BLOCK_DATA: | 
 | 2466 | 		case I2C_SMBUS_BLOCK_PROC_CALL: | 
 | 2467 | 			for (i = 0; i < msgbuf1[0] + 1; i++) | 
 | 2468 | 				data->block[i] = msgbuf1[i]; | 
 | 2469 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | 		} | 
 | 2471 | 	return 0; | 
 | 2472 | } | 
 | 2473 |  | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2474 | /** | 
 | 2475 |  * i2c_smbus_xfer - execute SMBus protocol operations | 
 | 2476 |  * @adapter: Handle to I2C bus | 
 | 2477 |  * @addr: Address of SMBus slave on that bus | 
 | 2478 |  * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) | 
 | 2479 |  * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE | 
 | 2480 |  * @command: Byte interpreted by slave, for protocols which use such bytes | 
 | 2481 |  * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL | 
 | 2482 |  * @data: Data to be read or written | 
 | 2483 |  * | 
 | 2484 |  * This executes an SMBus protocol operation, and returns a negative | 
 | 2485 |  * errno code else zero on success. | 
 | 2486 |  */ | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2487 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, | 
| David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2488 | 		   char read_write, u8 command, int protocol, | 
| Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2489 | 		   union i2c_smbus_data *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | { | 
| Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 2491 | 	unsigned long orig_jiffies; | 
 | 2492 | 	int try; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | 	s32 res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 |  | 
| Laurent Pinchart | d47726c | 2012-07-24 14:13:59 +0200 | [diff] [blame] | 2495 | 	flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 |  | 
 | 2497 | 	if (adapter->algo->smbus_xfer) { | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2498 | 		i2c_lock_adapter(adapter); | 
| Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 2499 |  | 
 | 2500 | 		/* Retry automatically on arbitration loss */ | 
 | 2501 | 		orig_jiffies = jiffies; | 
 | 2502 | 		for (res = 0, try = 0; try <= adapter->retries; try++) { | 
 | 2503 | 			res = adapter->algo->smbus_xfer(adapter, addr, flags, | 
 | 2504 | 							read_write, command, | 
 | 2505 | 							protocol, data); | 
 | 2506 | 			if (res != -EAGAIN) | 
 | 2507 | 				break; | 
 | 2508 | 			if (time_after(jiffies, | 
 | 2509 | 				       orig_jiffies + adapter->timeout)) | 
 | 2510 | 				break; | 
 | 2511 | 		} | 
| Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2512 | 		i2c_unlock_adapter(adapter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 |  | 
| Laurent Pinchart | 72fc2c7 | 2012-07-24 14:13:59 +0200 | [diff] [blame] | 2514 | 		if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) | 
 | 2515 | 			return res; | 
 | 2516 | 		/* | 
 | 2517 | 		 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't | 
 | 2518 | 		 * implement native support for the SMBus operation. | 
 | 2519 | 		 */ | 
 | 2520 | 	} | 
 | 2521 |  | 
 | 2522 | 	return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, | 
 | 2523 | 				       command, protocol, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | EXPORT_SYMBOL(i2c_smbus_xfer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 |  | 
 | 2527 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | 
 | 2528 | MODULE_DESCRIPTION("I2C-Bus main module"); | 
 | 2529 | MODULE_LICENSE("GPL"); |