xref: /OK3568_Linux_fs/kernel/drivers/i2c/i2c-core-base.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Linux I2C core
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 1995-99 Simon G. Vogl
6*4882a593Smuzhiyun  *   With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
7*4882a593Smuzhiyun  *   Mux support by Rodolfo Giometti <giometti@enneenne.com> and
8*4882a593Smuzhiyun  *   Michael Lawnick <michael.lawnick.ext@nsn.com>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org>
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define pr_fmt(fmt) "i2c-core: " fmt
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <dt-bindings/i2c/i2c.h>
16*4882a593Smuzhiyun #include <linux/acpi.h>
17*4882a593Smuzhiyun #include <linux/clk/clk-conf.h>
18*4882a593Smuzhiyun #include <linux/completion.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun #include <linux/err.h>
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
23*4882a593Smuzhiyun #include <linux/i2c.h>
24*4882a593Smuzhiyun #include <linux/i2c-smbus.h>
25*4882a593Smuzhiyun #include <linux/idr.h>
26*4882a593Smuzhiyun #include <linux/init.h>
27*4882a593Smuzhiyun #include <linux/interrupt.h>
28*4882a593Smuzhiyun #include <linux/irqflags.h>
29*4882a593Smuzhiyun #include <linux/jump_label.h>
30*4882a593Smuzhiyun #include <linux/kernel.h>
31*4882a593Smuzhiyun #include <linux/module.h>
32*4882a593Smuzhiyun #include <linux/mutex.h>
33*4882a593Smuzhiyun #include <linux/of_device.h>
34*4882a593Smuzhiyun #include <linux/of.h>
35*4882a593Smuzhiyun #include <linux/of_irq.h>
36*4882a593Smuzhiyun #include <linux/pinctrl/consumer.h>
37*4882a593Smuzhiyun #include <linux/pm_domain.h>
38*4882a593Smuzhiyun #include <linux/pm_runtime.h>
39*4882a593Smuzhiyun #include <linux/pm_wakeirq.h>
40*4882a593Smuzhiyun #include <linux/property.h>
41*4882a593Smuzhiyun #include <linux/rwsem.h>
42*4882a593Smuzhiyun #include <linux/slab.h>
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include "i2c-core.h"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
47*4882a593Smuzhiyun #include <trace/events/i2c.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define I2C_ADDR_OFFSET_TEN_BIT	0xa000
50*4882a593Smuzhiyun #define I2C_ADDR_OFFSET_SLAVE	0x1000
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define I2C_ADDR_7BITS_MAX	0x77
53*4882a593Smuzhiyun #define I2C_ADDR_7BITS_COUNT	(I2C_ADDR_7BITS_MAX + 1)
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #define I2C_ADDR_DEVICE_ID	0x7c
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * core_lock protects i2c_adapter_idr, and guarantees that device detection,
59*4882a593Smuzhiyun  * deletion of detected devices are serialized
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun static DEFINE_MUTEX(core_lock);
62*4882a593Smuzhiyun static DEFINE_IDR(i2c_adapter_idr);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr);
65*4882a593Smuzhiyun static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
68*4882a593Smuzhiyun static bool is_registered;
69*4882a593Smuzhiyun 
i2c_transfer_trace_reg(void)70*4882a593Smuzhiyun int i2c_transfer_trace_reg(void)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	static_branch_inc(&i2c_trace_msg_key);
73*4882a593Smuzhiyun 	return 0;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
i2c_transfer_trace_unreg(void)76*4882a593Smuzhiyun void i2c_transfer_trace_unreg(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	static_branch_dec(&i2c_trace_msg_key);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
i2c_match_id(const struct i2c_device_id * id,const struct i2c_client * client)81*4882a593Smuzhiyun const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
82*4882a593Smuzhiyun 						const struct i2c_client *client)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	if (!(id && client))
85*4882a593Smuzhiyun 		return NULL;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	while (id->name[0]) {
88*4882a593Smuzhiyun 		if (strcmp(client->name, id->name) == 0)
89*4882a593Smuzhiyun 			return id;
90*4882a593Smuzhiyun 		id++;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 	return NULL;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_match_id);
95*4882a593Smuzhiyun 
i2c_device_match(struct device * dev,struct device_driver * drv)96*4882a593Smuzhiyun static int i2c_device_match(struct device *dev, struct device_driver *drv)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct i2c_client	*client = i2c_verify_client(dev);
99*4882a593Smuzhiyun 	struct i2c_driver	*driver;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Attempt an OF style match */
103*4882a593Smuzhiyun 	if (i2c_of_match_device(drv->of_match_table, client))
104*4882a593Smuzhiyun 		return 1;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/* Then ACPI style match */
107*4882a593Smuzhiyun 	if (acpi_driver_match_device(dev, drv))
108*4882a593Smuzhiyun 		return 1;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	driver = to_i2c_driver(drv);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* Finally an I2C match */
113*4882a593Smuzhiyun 	if (i2c_match_id(driver->id_table, client))
114*4882a593Smuzhiyun 		return 1;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
i2c_device_uevent(struct device * dev,struct kobj_uevent_env * env)119*4882a593Smuzhiyun static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
122*4882a593Smuzhiyun 	int rc;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	rc = of_device_uevent_modalias(dev, env);
125*4882a593Smuzhiyun 	if (rc != -ENODEV)
126*4882a593Smuzhiyun 		return rc;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	rc = acpi_device_uevent_modalias(dev, env);
129*4882a593Smuzhiyun 	if (rc != -ENODEV)
130*4882a593Smuzhiyun 		return rc;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /* i2c bus recovery routines */
get_scl_gpio_value(struct i2c_adapter * adap)136*4882a593Smuzhiyun static int get_scl_gpio_value(struct i2c_adapter *adap)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
set_scl_gpio_value(struct i2c_adapter * adap,int val)141*4882a593Smuzhiyun static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
get_sda_gpio_value(struct i2c_adapter * adap)146*4882a593Smuzhiyun static int get_sda_gpio_value(struct i2c_adapter *adap)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
set_sda_gpio_value(struct i2c_adapter * adap,int val)151*4882a593Smuzhiyun static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
i2c_generic_bus_free(struct i2c_adapter * adap)156*4882a593Smuzhiyun static int i2c_generic_bus_free(struct i2c_adapter *adap)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
159*4882a593Smuzhiyun 	int ret = -EOPNOTSUPP;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	if (bri->get_bus_free)
162*4882a593Smuzhiyun 		ret = bri->get_bus_free(adap);
163*4882a593Smuzhiyun 	else if (bri->get_sda)
164*4882a593Smuzhiyun 		ret = bri->get_sda(adap);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	if (ret < 0)
167*4882a593Smuzhiyun 		return ret;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return ret ? 0 : -EBUSY;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /*
173*4882a593Smuzhiyun  * We are generating clock pulses. ndelay() determines durating of clk pulses.
174*4882a593Smuzhiyun  * We will generate clock with rate 100 KHz and so duration of both clock levels
175*4882a593Smuzhiyun  * is: delay in ns = (10^6 / 100) / 2
176*4882a593Smuzhiyun  */
177*4882a593Smuzhiyun #define RECOVERY_NDELAY		5000
178*4882a593Smuzhiyun #define RECOVERY_CLK_CNT	9
179*4882a593Smuzhiyun 
i2c_generic_scl_recovery(struct i2c_adapter * adap)180*4882a593Smuzhiyun int i2c_generic_scl_recovery(struct i2c_adapter *adap)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
183*4882a593Smuzhiyun 	int i = 0, scl = 1, ret = 0;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (bri->prepare_recovery)
186*4882a593Smuzhiyun 		bri->prepare_recovery(adap);
187*4882a593Smuzhiyun 	if (bri->pinctrl)
188*4882a593Smuzhiyun 		pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	/*
191*4882a593Smuzhiyun 	 * If we can set SDA, we will always create a STOP to ensure additional
192*4882a593Smuzhiyun 	 * pulses will do no harm. This is achieved by letting SDA follow SCL
193*4882a593Smuzhiyun 	 * half a cycle later. Check the 'incomplete_write_byte' fault injector
194*4882a593Smuzhiyun 	 * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
195*4882a593Smuzhiyun 	 * here for simplicity.
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	bri->set_scl(adap, scl);
198*4882a593Smuzhiyun 	ndelay(RECOVERY_NDELAY);
199*4882a593Smuzhiyun 	if (bri->set_sda)
200*4882a593Smuzhiyun 		bri->set_sda(adap, scl);
201*4882a593Smuzhiyun 	ndelay(RECOVERY_NDELAY / 2);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/*
204*4882a593Smuzhiyun 	 * By this time SCL is high, as we need to give 9 falling-rising edges
205*4882a593Smuzhiyun 	 */
206*4882a593Smuzhiyun 	while (i++ < RECOVERY_CLK_CNT * 2) {
207*4882a593Smuzhiyun 		if (scl) {
208*4882a593Smuzhiyun 			/* SCL shouldn't be low here */
209*4882a593Smuzhiyun 			if (!bri->get_scl(adap)) {
210*4882a593Smuzhiyun 				dev_err(&adap->dev,
211*4882a593Smuzhiyun 					"SCL is stuck low, exit recovery\n");
212*4882a593Smuzhiyun 				ret = -EBUSY;
213*4882a593Smuzhiyun 				break;
214*4882a593Smuzhiyun 			}
215*4882a593Smuzhiyun 		}
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 		scl = !scl;
218*4882a593Smuzhiyun 		bri->set_scl(adap, scl);
219*4882a593Smuzhiyun 		/* Creating STOP again, see above */
220*4882a593Smuzhiyun 		if (scl)  {
221*4882a593Smuzhiyun 			/* Honour minimum tsu:sto */
222*4882a593Smuzhiyun 			ndelay(RECOVERY_NDELAY);
223*4882a593Smuzhiyun 		} else {
224*4882a593Smuzhiyun 			/* Honour minimum tf and thd:dat */
225*4882a593Smuzhiyun 			ndelay(RECOVERY_NDELAY / 2);
226*4882a593Smuzhiyun 		}
227*4882a593Smuzhiyun 		if (bri->set_sda)
228*4882a593Smuzhiyun 			bri->set_sda(adap, scl);
229*4882a593Smuzhiyun 		ndelay(RECOVERY_NDELAY / 2);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		if (scl) {
232*4882a593Smuzhiyun 			ret = i2c_generic_bus_free(adap);
233*4882a593Smuzhiyun 			if (ret == 0)
234*4882a593Smuzhiyun 				break;
235*4882a593Smuzhiyun 		}
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/* If we can't check bus status, assume recovery worked */
239*4882a593Smuzhiyun 	if (ret == -EOPNOTSUPP)
240*4882a593Smuzhiyun 		ret = 0;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (bri->unprepare_recovery)
243*4882a593Smuzhiyun 		bri->unprepare_recovery(adap);
244*4882a593Smuzhiyun 	if (bri->pinctrl)
245*4882a593Smuzhiyun 		pinctrl_select_state(bri->pinctrl, bri->pins_default);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return ret;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
250*4882a593Smuzhiyun 
i2c_recover_bus(struct i2c_adapter * adap)251*4882a593Smuzhiyun int i2c_recover_bus(struct i2c_adapter *adap)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	if (!adap->bus_recovery_info)
254*4882a593Smuzhiyun 		return -EOPNOTSUPP;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
257*4882a593Smuzhiyun 	return adap->bus_recovery_info->recover_bus(adap);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_recover_bus);
260*4882a593Smuzhiyun 
i2c_gpio_init_pinctrl_recovery(struct i2c_adapter * adap)261*4882a593Smuzhiyun static void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
264*4882a593Smuzhiyun 	struct device *dev = &adap->dev;
265*4882a593Smuzhiyun 	struct pinctrl *p = bri->pinctrl;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/*
268*4882a593Smuzhiyun 	 * we can't change states without pinctrl, so remove the states if
269*4882a593Smuzhiyun 	 * populated
270*4882a593Smuzhiyun 	 */
271*4882a593Smuzhiyun 	if (!p) {
272*4882a593Smuzhiyun 		bri->pins_default = NULL;
273*4882a593Smuzhiyun 		bri->pins_gpio = NULL;
274*4882a593Smuzhiyun 		return;
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (!bri->pins_default) {
278*4882a593Smuzhiyun 		bri->pins_default = pinctrl_lookup_state(p,
279*4882a593Smuzhiyun 							 PINCTRL_STATE_DEFAULT);
280*4882a593Smuzhiyun 		if (IS_ERR(bri->pins_default)) {
281*4882a593Smuzhiyun 			dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n");
282*4882a593Smuzhiyun 			bri->pins_default = NULL;
283*4882a593Smuzhiyun 		}
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 	if (!bri->pins_gpio) {
286*4882a593Smuzhiyun 		bri->pins_gpio = pinctrl_lookup_state(p, "gpio");
287*4882a593Smuzhiyun 		if (IS_ERR(bri->pins_gpio))
288*4882a593Smuzhiyun 			bri->pins_gpio = pinctrl_lookup_state(p, "recovery");
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		if (IS_ERR(bri->pins_gpio)) {
291*4882a593Smuzhiyun 			dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n");
292*4882a593Smuzhiyun 			bri->pins_gpio = NULL;
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* for pinctrl state changes, we need all the information */
297*4882a593Smuzhiyun 	if (bri->pins_default && bri->pins_gpio) {
298*4882a593Smuzhiyun 		dev_info(dev, "using pinctrl states for GPIO recovery");
299*4882a593Smuzhiyun 	} else {
300*4882a593Smuzhiyun 		bri->pinctrl = NULL;
301*4882a593Smuzhiyun 		bri->pins_default = NULL;
302*4882a593Smuzhiyun 		bri->pins_gpio = NULL;
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
i2c_gpio_init_generic_recovery(struct i2c_adapter * adap)306*4882a593Smuzhiyun static int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
309*4882a593Smuzhiyun 	struct device *dev = &adap->dev;
310*4882a593Smuzhiyun 	struct gpio_desc *gpiod;
311*4882a593Smuzhiyun 	int ret = 0;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/*
314*4882a593Smuzhiyun 	 * don't touch the recovery information if the driver is not using
315*4882a593Smuzhiyun 	 * generic SCL recovery
316*4882a593Smuzhiyun 	 */
317*4882a593Smuzhiyun 	if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery)
318*4882a593Smuzhiyun 		return 0;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/*
321*4882a593Smuzhiyun 	 * pins might be taken as GPIO, so we should inform pinctrl about
322*4882a593Smuzhiyun 	 * this and move the state to GPIO
323*4882a593Smuzhiyun 	 */
324*4882a593Smuzhiyun 	if (bri->pinctrl)
325*4882a593Smuzhiyun 		pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/*
328*4882a593Smuzhiyun 	 * if there is incomplete or no recovery information, see if generic
329*4882a593Smuzhiyun 	 * GPIO recovery is available
330*4882a593Smuzhiyun 	 */
331*4882a593Smuzhiyun 	if (!bri->scl_gpiod) {
332*4882a593Smuzhiyun 		gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
333*4882a593Smuzhiyun 		if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
334*4882a593Smuzhiyun 			ret  = -EPROBE_DEFER;
335*4882a593Smuzhiyun 			goto cleanup_pinctrl_state;
336*4882a593Smuzhiyun 		}
337*4882a593Smuzhiyun 		if (!IS_ERR(gpiod)) {
338*4882a593Smuzhiyun 			bri->scl_gpiod = gpiod;
339*4882a593Smuzhiyun 			bri->recover_bus = i2c_generic_scl_recovery;
340*4882a593Smuzhiyun 			dev_info(dev, "using generic GPIOs for recovery\n");
341*4882a593Smuzhiyun 		}
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	/* SDA GPIOD line is optional, so we care about DEFER only */
345*4882a593Smuzhiyun 	if (!bri->sda_gpiod) {
346*4882a593Smuzhiyun 		/*
347*4882a593Smuzhiyun 		 * We have SCL. Pull SCL low and wait a bit so that SDA glitches
348*4882a593Smuzhiyun 		 * have no effect.
349*4882a593Smuzhiyun 		 */
350*4882a593Smuzhiyun 		gpiod_direction_output(bri->scl_gpiod, 0);
351*4882a593Smuzhiyun 		udelay(10);
352*4882a593Smuzhiyun 		gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		/* Wait a bit in case of a SDA glitch, and then release SCL. */
355*4882a593Smuzhiyun 		udelay(10);
356*4882a593Smuzhiyun 		gpiod_direction_output(bri->scl_gpiod, 1);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
359*4882a593Smuzhiyun 			ret = -EPROBE_DEFER;
360*4882a593Smuzhiyun 			goto cleanup_pinctrl_state;
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 		if (!IS_ERR(gpiod))
363*4882a593Smuzhiyun 			bri->sda_gpiod = gpiod;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun cleanup_pinctrl_state:
367*4882a593Smuzhiyun 	/* change the state of the pins back to their default state */
368*4882a593Smuzhiyun 	if (bri->pinctrl)
369*4882a593Smuzhiyun 		pinctrl_select_state(bri->pinctrl, bri->pins_default);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	return ret;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
i2c_gpio_init_recovery(struct i2c_adapter * adap)374*4882a593Smuzhiyun static int i2c_gpio_init_recovery(struct i2c_adapter *adap)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	i2c_gpio_init_pinctrl_recovery(adap);
377*4882a593Smuzhiyun 	return i2c_gpio_init_generic_recovery(adap);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
i2c_init_recovery(struct i2c_adapter * adap)380*4882a593Smuzhiyun static int i2c_init_recovery(struct i2c_adapter *adap)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
383*4882a593Smuzhiyun 	char *err_str, *err_level = KERN_ERR;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (!bri)
386*4882a593Smuzhiyun 		return 0;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER)
389*4882a593Smuzhiyun 		return -EPROBE_DEFER;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (!bri->recover_bus) {
392*4882a593Smuzhiyun 		err_str = "no suitable method provided";
393*4882a593Smuzhiyun 		err_level = KERN_DEBUG;
394*4882a593Smuzhiyun 		goto err;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
398*4882a593Smuzhiyun 		bri->get_scl = get_scl_gpio_value;
399*4882a593Smuzhiyun 		bri->set_scl = set_scl_gpio_value;
400*4882a593Smuzhiyun 		if (bri->sda_gpiod) {
401*4882a593Smuzhiyun 			bri->get_sda = get_sda_gpio_value;
402*4882a593Smuzhiyun 			/* FIXME: add proper flag instead of '0' once available */
403*4882a593Smuzhiyun 			if (gpiod_get_direction(bri->sda_gpiod) == 0)
404*4882a593Smuzhiyun 				bri->set_sda = set_sda_gpio_value;
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 	} else if (bri->recover_bus == i2c_generic_scl_recovery) {
407*4882a593Smuzhiyun 		/* Generic SCL recovery */
408*4882a593Smuzhiyun 		if (!bri->set_scl || !bri->get_scl) {
409*4882a593Smuzhiyun 			err_str = "no {get|set}_scl() found";
410*4882a593Smuzhiyun 			goto err;
411*4882a593Smuzhiyun 		}
412*4882a593Smuzhiyun 		if (!bri->set_sda && !bri->get_sda) {
413*4882a593Smuzhiyun 			err_str = "either get_sda() or set_sda() needed";
414*4882a593Smuzhiyun 			goto err;
415*4882a593Smuzhiyun 		}
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return 0;
419*4882a593Smuzhiyun  err:
420*4882a593Smuzhiyun 	dev_printk(err_level, &adap->dev, "Not using recovery: %s\n", err_str);
421*4882a593Smuzhiyun 	adap->bus_recovery_info = NULL;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	return -EINVAL;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
i2c_smbus_host_notify_to_irq(const struct i2c_client * client)426*4882a593Smuzhiyun static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	struct i2c_adapter *adap = client->adapter;
429*4882a593Smuzhiyun 	unsigned int irq;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	if (!adap->host_notify_domain)
432*4882a593Smuzhiyun 		return -ENXIO;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_TEN)
435*4882a593Smuzhiyun 		return -EINVAL;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	irq = irq_create_mapping(adap->host_notify_domain, client->addr);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	return irq > 0 ? irq : -ENXIO;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
i2c_device_probe(struct device * dev)442*4882a593Smuzhiyun static int i2c_device_probe(struct device *dev)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	struct i2c_client	*client = i2c_verify_client(dev);
445*4882a593Smuzhiyun 	struct i2c_driver	*driver;
446*4882a593Smuzhiyun 	int status;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	if (!client)
449*4882a593Smuzhiyun 		return 0;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	client->irq = client->init_irq;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	if (!client->irq) {
454*4882a593Smuzhiyun 		int irq = -ENOENT;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
457*4882a593Smuzhiyun 			dev_dbg(dev, "Using Host Notify IRQ\n");
458*4882a593Smuzhiyun 			/* Keep adapter active when Host Notify is required */
459*4882a593Smuzhiyun 			pm_runtime_get_sync(&client->adapter->dev);
460*4882a593Smuzhiyun 			irq = i2c_smbus_host_notify_to_irq(client);
461*4882a593Smuzhiyun 		} else if (dev->of_node) {
462*4882a593Smuzhiyun 			irq = of_irq_get_byname(dev->of_node, "irq");
463*4882a593Smuzhiyun 			if (irq == -EINVAL || irq == -ENODATA)
464*4882a593Smuzhiyun 				irq = of_irq_get(dev->of_node, 0);
465*4882a593Smuzhiyun 		} else if (ACPI_COMPANION(dev)) {
466*4882a593Smuzhiyun 			irq = i2c_acpi_get_irq(client);
467*4882a593Smuzhiyun 		}
468*4882a593Smuzhiyun 		if (irq == -EPROBE_DEFER) {
469*4882a593Smuzhiyun 			status = irq;
470*4882a593Smuzhiyun 			goto put_sync_adapter;
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 		if (irq < 0)
474*4882a593Smuzhiyun 			irq = 0;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		client->irq = irq;
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	driver = to_i2c_driver(dev->driver);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/*
482*4882a593Smuzhiyun 	 * An I2C ID table is not mandatory, if and only if, a suitable OF
483*4882a593Smuzhiyun 	 * or ACPI ID table is supplied for the probing device.
484*4882a593Smuzhiyun 	 */
485*4882a593Smuzhiyun 	if (!driver->id_table &&
486*4882a593Smuzhiyun 	    !acpi_driver_match_device(dev, dev->driver) &&
487*4882a593Smuzhiyun 	    !i2c_of_match_device(dev->driver->of_match_table, client)) {
488*4882a593Smuzhiyun 		status = -ENODEV;
489*4882a593Smuzhiyun 		goto put_sync_adapter;
490*4882a593Smuzhiyun 	}
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_WAKE) {
493*4882a593Smuzhiyun 		int wakeirq;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
496*4882a593Smuzhiyun 		if (wakeirq == -EPROBE_DEFER) {
497*4882a593Smuzhiyun 			status = wakeirq;
498*4882a593Smuzhiyun 			goto put_sync_adapter;
499*4882a593Smuzhiyun 		}
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 		device_init_wakeup(&client->dev, true);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		if (wakeirq > 0 && wakeirq != client->irq)
504*4882a593Smuzhiyun 			status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
505*4882a593Smuzhiyun 		else if (client->irq > 0)
506*4882a593Smuzhiyun 			status = dev_pm_set_wake_irq(dev, client->irq);
507*4882a593Smuzhiyun 		else
508*4882a593Smuzhiyun 			status = 0;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 		if (status)
511*4882a593Smuzhiyun 			dev_warn(&client->dev, "failed to set up wakeup irq\n");
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	dev_dbg(dev, "probe\n");
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	status = of_clk_set_defaults(dev->of_node, false);
517*4882a593Smuzhiyun 	if (status < 0)
518*4882a593Smuzhiyun 		goto err_clear_wakeup_irq;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	status = dev_pm_domain_attach(&client->dev, true);
521*4882a593Smuzhiyun 	if (status)
522*4882a593Smuzhiyun 		goto err_clear_wakeup_irq;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	/*
525*4882a593Smuzhiyun 	 * When there are no more users of probe(),
526*4882a593Smuzhiyun 	 * rename probe_new to probe.
527*4882a593Smuzhiyun 	 */
528*4882a593Smuzhiyun 	if (driver->probe_new)
529*4882a593Smuzhiyun 		status = driver->probe_new(client);
530*4882a593Smuzhiyun 	else if (driver->probe)
531*4882a593Smuzhiyun 		status = driver->probe(client,
532*4882a593Smuzhiyun 				       i2c_match_id(driver->id_table, client));
533*4882a593Smuzhiyun 	else
534*4882a593Smuzhiyun 		status = -EINVAL;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (status)
537*4882a593Smuzhiyun 		goto err_detach_pm_domain;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	return 0;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun err_detach_pm_domain:
542*4882a593Smuzhiyun 	dev_pm_domain_detach(&client->dev, true);
543*4882a593Smuzhiyun err_clear_wakeup_irq:
544*4882a593Smuzhiyun 	dev_pm_clear_wake_irq(&client->dev);
545*4882a593Smuzhiyun 	device_init_wakeup(&client->dev, false);
546*4882a593Smuzhiyun put_sync_adapter:
547*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_HOST_NOTIFY)
548*4882a593Smuzhiyun 		pm_runtime_put_sync(&client->adapter->dev);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	return status;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
i2c_device_remove(struct device * dev)553*4882a593Smuzhiyun static int i2c_device_remove(struct device *dev)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	struct i2c_client	*client = i2c_verify_client(dev);
556*4882a593Smuzhiyun 	struct i2c_driver	*driver;
557*4882a593Smuzhiyun 	int status = 0;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	if (!client || !dev->driver)
560*4882a593Smuzhiyun 		return 0;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	driver = to_i2c_driver(dev->driver);
563*4882a593Smuzhiyun 	if (driver->remove) {
564*4882a593Smuzhiyun 		dev_dbg(dev, "remove\n");
565*4882a593Smuzhiyun 		status = driver->remove(client);
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	dev_pm_domain_detach(&client->dev, true);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	dev_pm_clear_wake_irq(&client->dev);
571*4882a593Smuzhiyun 	device_init_wakeup(&client->dev, false);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	client->irq = 0;
574*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_HOST_NOTIFY)
575*4882a593Smuzhiyun 		pm_runtime_put(&client->adapter->dev);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	return status;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
i2c_device_shutdown(struct device * dev)580*4882a593Smuzhiyun static void i2c_device_shutdown(struct device *dev)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	struct i2c_client *client = i2c_verify_client(dev);
583*4882a593Smuzhiyun 	struct i2c_driver *driver;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	if (!client || !dev->driver)
586*4882a593Smuzhiyun 		return;
587*4882a593Smuzhiyun 	driver = to_i2c_driver(dev->driver);
588*4882a593Smuzhiyun 	if (driver->shutdown)
589*4882a593Smuzhiyun 		driver->shutdown(client);
590*4882a593Smuzhiyun 	else if (client->irq > 0)
591*4882a593Smuzhiyun 		disable_irq(client->irq);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
i2c_client_dev_release(struct device * dev)594*4882a593Smuzhiyun static void i2c_client_dev_release(struct device *dev)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	kfree(to_i2c_client(dev));
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun static ssize_t
name_show(struct device * dev,struct device_attribute * attr,char * buf)600*4882a593Smuzhiyun name_show(struct device *dev, struct device_attribute *attr, char *buf)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
603*4882a593Smuzhiyun 		       to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun static DEVICE_ATTR_RO(name);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun static ssize_t
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)608*4882a593Smuzhiyun modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
611*4882a593Smuzhiyun 	int len;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	len = of_device_modalias(dev, buf, PAGE_SIZE);
614*4882a593Smuzhiyun 	if (len != -ENODEV)
615*4882a593Smuzhiyun 		return len;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
618*4882a593Smuzhiyun 	if (len != -ENODEV)
619*4882a593Smuzhiyun 		return len;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun static DEVICE_ATTR_RO(modalias);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun static struct attribute *i2c_dev_attrs[] = {
626*4882a593Smuzhiyun 	&dev_attr_name.attr,
627*4882a593Smuzhiyun 	/* modalias helps coldplug:  modprobe $(cat .../modalias) */
628*4882a593Smuzhiyun 	&dev_attr_modalias.attr,
629*4882a593Smuzhiyun 	NULL
630*4882a593Smuzhiyun };
631*4882a593Smuzhiyun ATTRIBUTE_GROUPS(i2c_dev);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun struct bus_type i2c_bus_type = {
634*4882a593Smuzhiyun 	.name		= "i2c",
635*4882a593Smuzhiyun 	.match		= i2c_device_match,
636*4882a593Smuzhiyun 	.probe		= i2c_device_probe,
637*4882a593Smuzhiyun 	.remove		= i2c_device_remove,
638*4882a593Smuzhiyun 	.shutdown	= i2c_device_shutdown,
639*4882a593Smuzhiyun };
640*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_bus_type);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun struct device_type i2c_client_type = {
643*4882a593Smuzhiyun 	.groups		= i2c_dev_groups,
644*4882a593Smuzhiyun 	.uevent		= i2c_device_uevent,
645*4882a593Smuzhiyun 	.release	= i2c_client_dev_release,
646*4882a593Smuzhiyun };
647*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_client_type);
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun /**
651*4882a593Smuzhiyun  * i2c_verify_client - return parameter as i2c_client, or NULL
652*4882a593Smuzhiyun  * @dev: device, probably from some driver model iterator
653*4882a593Smuzhiyun  *
654*4882a593Smuzhiyun  * When traversing the driver model tree, perhaps using driver model
655*4882a593Smuzhiyun  * iterators like @device_for_each_child(), you can't assume very much
656*4882a593Smuzhiyun  * about the nodes you find.  Use this function to avoid oopses caused
657*4882a593Smuzhiyun  * by wrongly treating some non-I2C device as an i2c_client.
658*4882a593Smuzhiyun  */
i2c_verify_client(struct device * dev)659*4882a593Smuzhiyun struct i2c_client *i2c_verify_client(struct device *dev)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	return (dev->type == &i2c_client_type)
662*4882a593Smuzhiyun 			? to_i2c_client(dev)
663*4882a593Smuzhiyun 			: NULL;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_verify_client);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /* Return a unique address which takes the flags of the client into account */
i2c_encode_flags_to_addr(struct i2c_client * client)669*4882a593Smuzhiyun static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	unsigned short addr = client->addr;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	/* For some client flags, add an arbitrary offset to avoid collisions */
674*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_TEN)
675*4882a593Smuzhiyun 		addr |= I2C_ADDR_OFFSET_TEN_BIT;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (client->flags & I2C_CLIENT_SLAVE)
678*4882a593Smuzhiyun 		addr |= I2C_ADDR_OFFSET_SLAVE;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	return addr;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun /* This is a permissive address validity check, I2C address map constraints
684*4882a593Smuzhiyun  * are purposely not enforced, except for the general call address. */
i2c_check_addr_validity(unsigned int addr,unsigned short flags)685*4882a593Smuzhiyun static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	if (flags & I2C_CLIENT_TEN) {
688*4882a593Smuzhiyun 		/* 10-bit address, all values are valid */
689*4882a593Smuzhiyun 		if (addr > 0x3ff)
690*4882a593Smuzhiyun 			return -EINVAL;
691*4882a593Smuzhiyun 	} else {
692*4882a593Smuzhiyun 		/* 7-bit address, reject the general call address */
693*4882a593Smuzhiyun 		if (addr == 0x00 || addr > 0x7f)
694*4882a593Smuzhiyun 			return -EINVAL;
695*4882a593Smuzhiyun 	}
696*4882a593Smuzhiyun 	return 0;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun /* And this is a strict address validity check, used when probing. If a
700*4882a593Smuzhiyun  * device uses a reserved address, then it shouldn't be probed. 7-bit
701*4882a593Smuzhiyun  * addressing is assumed, 10-bit address devices are rare and should be
702*4882a593Smuzhiyun  * explicitly enumerated. */
i2c_check_7bit_addr_validity_strict(unsigned short addr)703*4882a593Smuzhiyun int i2c_check_7bit_addr_validity_strict(unsigned short addr)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun 	/*
706*4882a593Smuzhiyun 	 * Reserved addresses per I2C specification:
707*4882a593Smuzhiyun 	 *  0x00       General call address / START byte
708*4882a593Smuzhiyun 	 *  0x01       CBUS address
709*4882a593Smuzhiyun 	 *  0x02       Reserved for different bus format
710*4882a593Smuzhiyun 	 *  0x03       Reserved for future purposes
711*4882a593Smuzhiyun 	 *  0x04-0x07  Hs-mode master code
712*4882a593Smuzhiyun 	 *  0x78-0x7b  10-bit slave addressing
713*4882a593Smuzhiyun 	 *  0x7c-0x7f  Reserved for future purposes
714*4882a593Smuzhiyun 	 */
715*4882a593Smuzhiyun 	if (addr < 0x08 || addr > 0x77)
716*4882a593Smuzhiyun 		return -EINVAL;
717*4882a593Smuzhiyun 	return 0;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
__i2c_check_addr_busy(struct device * dev,void * addrp)720*4882a593Smuzhiyun static int __i2c_check_addr_busy(struct device *dev, void *addrp)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct i2c_client	*client = i2c_verify_client(dev);
723*4882a593Smuzhiyun 	int			addr = *(int *)addrp;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	if (client && i2c_encode_flags_to_addr(client) == addr)
726*4882a593Smuzhiyun 		return -EBUSY;
727*4882a593Smuzhiyun 	return 0;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /* walk up mux tree */
i2c_check_mux_parents(struct i2c_adapter * adapter,int addr)731*4882a593Smuzhiyun static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
734*4882a593Smuzhiyun 	int result;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	result = device_for_each_child(&adapter->dev, &addr,
737*4882a593Smuzhiyun 					__i2c_check_addr_busy);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	if (!result && parent)
740*4882a593Smuzhiyun 		result = i2c_check_mux_parents(parent, addr);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	return result;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun /* recurse down mux tree */
i2c_check_mux_children(struct device * dev,void * addrp)746*4882a593Smuzhiyun static int i2c_check_mux_children(struct device *dev, void *addrp)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	int result;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (dev->type == &i2c_adapter_type)
751*4882a593Smuzhiyun 		result = device_for_each_child(dev, addrp,
752*4882a593Smuzhiyun 						i2c_check_mux_children);
753*4882a593Smuzhiyun 	else
754*4882a593Smuzhiyun 		result = __i2c_check_addr_busy(dev, addrp);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	return result;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
i2c_check_addr_busy(struct i2c_adapter * adapter,int addr)759*4882a593Smuzhiyun static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
762*4882a593Smuzhiyun 	int result = 0;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	if (parent)
765*4882a593Smuzhiyun 		result = i2c_check_mux_parents(parent, addr);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (!result)
768*4882a593Smuzhiyun 		result = device_for_each_child(&adapter->dev, &addr,
769*4882a593Smuzhiyun 						i2c_check_mux_children);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	return result;
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun /**
775*4882a593Smuzhiyun  * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
776*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
777*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
778*4882a593Smuzhiyun  *	locks only this branch in the adapter tree
779*4882a593Smuzhiyun  */
i2c_adapter_lock_bus(struct i2c_adapter * adapter,unsigned int flags)780*4882a593Smuzhiyun static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
781*4882a593Smuzhiyun 				 unsigned int flags)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun 	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun /**
787*4882a593Smuzhiyun  * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
788*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
789*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
790*4882a593Smuzhiyun  *	trylocks only this branch in the adapter tree
791*4882a593Smuzhiyun  */
i2c_adapter_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)792*4882a593Smuzhiyun static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
793*4882a593Smuzhiyun 				   unsigned int flags)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	return rt_mutex_trylock(&adapter->bus_lock);
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /**
799*4882a593Smuzhiyun  * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
800*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
801*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
802*4882a593Smuzhiyun  *	unlocks only this branch in the adapter tree
803*4882a593Smuzhiyun  */
i2c_adapter_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)804*4882a593Smuzhiyun static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
805*4882a593Smuzhiyun 				   unsigned int flags)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	rt_mutex_unlock(&adapter->bus_lock);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
i2c_dev_set_name(struct i2c_adapter * adap,struct i2c_client * client,struct i2c_board_info const * info,int status)810*4882a593Smuzhiyun static void i2c_dev_set_name(struct i2c_adapter *adap,
811*4882a593Smuzhiyun 			     struct i2c_client *client,
812*4882a593Smuzhiyun 			     struct i2c_board_info const *info,
813*4882a593Smuzhiyun 			     int status)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	if (info && info->dev_name) {
818*4882a593Smuzhiyun 		dev_set_name(&client->dev, "i2c-%s", info->dev_name);
819*4882a593Smuzhiyun 		return;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (adev) {
823*4882a593Smuzhiyun 		dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
824*4882a593Smuzhiyun 		return;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	if (status == 0)
828*4882a593Smuzhiyun 		dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
829*4882a593Smuzhiyun 			i2c_encode_flags_to_addr(client));
830*4882a593Smuzhiyun 	else
831*4882a593Smuzhiyun 		dev_set_name(&client->dev, "%d-%04x-%01x", i2c_adapter_id(adap),
832*4882a593Smuzhiyun 			i2c_encode_flags_to_addr(client), status);
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
i2c_dev_irq_from_resources(const struct resource * resources,unsigned int num_resources)835*4882a593Smuzhiyun int i2c_dev_irq_from_resources(const struct resource *resources,
836*4882a593Smuzhiyun 			       unsigned int num_resources)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun 	struct irq_data *irqd;
839*4882a593Smuzhiyun 	int i;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	for (i = 0; i < num_resources; i++) {
842*4882a593Smuzhiyun 		const struct resource *r = &resources[i];
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 		if (resource_type(r) != IORESOURCE_IRQ)
845*4882a593Smuzhiyun 			continue;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 		if (r->flags & IORESOURCE_BITS) {
848*4882a593Smuzhiyun 			irqd = irq_get_irq_data(r->start);
849*4882a593Smuzhiyun 			if (!irqd)
850*4882a593Smuzhiyun 				break;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 			irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
853*4882a593Smuzhiyun 		}
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 		return r->start;
856*4882a593Smuzhiyun 	}
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	return 0;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun /**
862*4882a593Smuzhiyun  * i2c_new_client_device - instantiate an i2c device
863*4882a593Smuzhiyun  * @adap: the adapter managing the device
864*4882a593Smuzhiyun  * @info: describes one I2C device; bus_num is ignored
865*4882a593Smuzhiyun  * Context: can sleep
866*4882a593Smuzhiyun  *
867*4882a593Smuzhiyun  * Create an i2c device. Binding is handled through driver model
868*4882a593Smuzhiyun  * probe()/remove() methods.  A driver may be bound to this device when we
869*4882a593Smuzhiyun  * return from this function, or any later moment (e.g. maybe hotplugging will
870*4882a593Smuzhiyun  * load the driver module).  This call is not appropriate for use by mainboard
871*4882a593Smuzhiyun  * initialization logic, which usually runs during an arch_initcall() long
872*4882a593Smuzhiyun  * before any i2c_adapter could exist.
873*4882a593Smuzhiyun  *
874*4882a593Smuzhiyun  * This returns the new i2c client, which may be saved for later use with
875*4882a593Smuzhiyun  * i2c_unregister_device(); or an ERR_PTR to describe the error.
876*4882a593Smuzhiyun  */
877*4882a593Smuzhiyun struct i2c_client *
i2c_new_client_device(struct i2c_adapter * adap,struct i2c_board_info const * info)878*4882a593Smuzhiyun i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	struct i2c_client	*client;
881*4882a593Smuzhiyun 	int			status;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	client = kzalloc(sizeof *client, GFP_KERNEL);
884*4882a593Smuzhiyun 	if (!client)
885*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	client->adapter = adap;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	client->dev.platform_data = info->platform_data;
890*4882a593Smuzhiyun 	client->flags = info->flags;
891*4882a593Smuzhiyun 	client->addr = info->addr;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	client->init_irq = info->irq;
894*4882a593Smuzhiyun 	if (!client->init_irq)
895*4882a593Smuzhiyun 		client->init_irq = i2c_dev_irq_from_resources(info->resources,
896*4882a593Smuzhiyun 							 info->num_resources);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	strlcpy(client->name, info->type, sizeof(client->name));
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	status = i2c_check_addr_validity(client->addr, client->flags);
901*4882a593Smuzhiyun 	if (status) {
902*4882a593Smuzhiyun 		dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
903*4882a593Smuzhiyun 			client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
904*4882a593Smuzhiyun 		goto out_err_silent;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/* Check for address business */
908*4882a593Smuzhiyun 	status = i2c_check_addr_ex(adap, i2c_encode_flags_to_addr(client));
909*4882a593Smuzhiyun 	if (status)
910*4882a593Smuzhiyun 		dev_err(&adap->dev,
911*4882a593Smuzhiyun 			"%d i2c clients have been registered at 0x%02x",
912*4882a593Smuzhiyun 			status, client->addr);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	client->dev.parent = &client->adapter->dev;
915*4882a593Smuzhiyun 	client->dev.bus = &i2c_bus_type;
916*4882a593Smuzhiyun 	client->dev.type = &i2c_client_type;
917*4882a593Smuzhiyun 	client->dev.of_node = of_node_get(info->of_node);
918*4882a593Smuzhiyun 	client->dev.fwnode = info->fwnode;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	i2c_dev_set_name(adap, client, info, status);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	if (info->properties) {
923*4882a593Smuzhiyun 		status = device_add_properties(&client->dev, info->properties);
924*4882a593Smuzhiyun 		if (status) {
925*4882a593Smuzhiyun 			dev_err(&adap->dev,
926*4882a593Smuzhiyun 				"Failed to add properties to client %s: %d\n",
927*4882a593Smuzhiyun 				client->name, status);
928*4882a593Smuzhiyun 			goto out_err_put_of_node;
929*4882a593Smuzhiyun 		}
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	status = device_register(&client->dev);
933*4882a593Smuzhiyun 	if (status)
934*4882a593Smuzhiyun 		goto out_free_props;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
937*4882a593Smuzhiyun 		client->name, dev_name(&client->dev));
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	return client;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun out_free_props:
942*4882a593Smuzhiyun 	if (info->properties)
943*4882a593Smuzhiyun 		device_remove_properties(&client->dev);
944*4882a593Smuzhiyun out_err_put_of_node:
945*4882a593Smuzhiyun 	of_node_put(info->of_node);
946*4882a593Smuzhiyun out_err_silent:
947*4882a593Smuzhiyun 	kfree(client);
948*4882a593Smuzhiyun 	return ERR_PTR(status);
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_new_client_device);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun /**
953*4882a593Smuzhiyun  * i2c_unregister_device - reverse effect of i2c_new_*_device()
954*4882a593Smuzhiyun  * @client: value returned from i2c_new_*_device()
955*4882a593Smuzhiyun  * Context: can sleep
956*4882a593Smuzhiyun  */
i2c_unregister_device(struct i2c_client * client)957*4882a593Smuzhiyun void i2c_unregister_device(struct i2c_client *client)
958*4882a593Smuzhiyun {
959*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(client))
960*4882a593Smuzhiyun 		return;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	if (client->dev.of_node) {
963*4882a593Smuzhiyun 		of_node_clear_flag(client->dev.of_node, OF_POPULATED);
964*4882a593Smuzhiyun 		of_node_put(client->dev.of_node);
965*4882a593Smuzhiyun 	}
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	if (ACPI_COMPANION(&client->dev))
968*4882a593Smuzhiyun 		acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
969*4882a593Smuzhiyun 	device_unregister(&client->dev);
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_unregister_device);
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun static const struct i2c_device_id dummy_id[] = {
975*4882a593Smuzhiyun 	{ "dummy", 0 },
976*4882a593Smuzhiyun 	{ },
977*4882a593Smuzhiyun };
978*4882a593Smuzhiyun 
dummy_probe(struct i2c_client * client,const struct i2c_device_id * id)979*4882a593Smuzhiyun static int dummy_probe(struct i2c_client *client,
980*4882a593Smuzhiyun 		       const struct i2c_device_id *id)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun 	return 0;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun 
dummy_remove(struct i2c_client * client)985*4882a593Smuzhiyun static int dummy_remove(struct i2c_client *client)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	return 0;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun static struct i2c_driver dummy_driver = {
991*4882a593Smuzhiyun 	.driver.name	= "dummy",
992*4882a593Smuzhiyun 	.probe		= dummy_probe,
993*4882a593Smuzhiyun 	.remove		= dummy_remove,
994*4882a593Smuzhiyun 	.id_table	= dummy_id,
995*4882a593Smuzhiyun };
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun /**
998*4882a593Smuzhiyun  * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
999*4882a593Smuzhiyun  * @adapter: the adapter managing the device
1000*4882a593Smuzhiyun  * @address: seven bit address to be used
1001*4882a593Smuzhiyun  * Context: can sleep
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  * This returns an I2C client bound to the "dummy" driver, intended for use
1004*4882a593Smuzhiyun  * with devices that consume multiple addresses.  Examples of such chips
1005*4882a593Smuzhiyun  * include various EEPROMS (like 24c04 and 24c08 models).
1006*4882a593Smuzhiyun  *
1007*4882a593Smuzhiyun  * These dummy devices have two main uses.  First, most I2C and SMBus calls
1008*4882a593Smuzhiyun  * except i2c_transfer() need a client handle; the dummy will be that handle.
1009*4882a593Smuzhiyun  * And second, this prevents the specified address from being bound to a
1010*4882a593Smuzhiyun  * different driver.
1011*4882a593Smuzhiyun  *
1012*4882a593Smuzhiyun  * This returns the new i2c client, which should be saved for later use with
1013*4882a593Smuzhiyun  * i2c_unregister_device(); or an ERR_PTR to describe the error.
1014*4882a593Smuzhiyun  */
i2c_new_dummy_device(struct i2c_adapter * adapter,u16 address)1015*4882a593Smuzhiyun struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	struct i2c_board_info info = {
1018*4882a593Smuzhiyun 		I2C_BOARD_INFO("dummy", address),
1019*4882a593Smuzhiyun 	};
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	return i2c_new_client_device(adapter, &info);
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun struct i2c_dummy_devres {
1026*4882a593Smuzhiyun 	struct i2c_client *client;
1027*4882a593Smuzhiyun };
1028*4882a593Smuzhiyun 
devm_i2c_release_dummy(struct device * dev,void * res)1029*4882a593Smuzhiyun static void devm_i2c_release_dummy(struct device *dev, void *res)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun 	struct i2c_dummy_devres *this = res;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	i2c_unregister_device(this->client);
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun /**
1037*4882a593Smuzhiyun  * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1038*4882a593Smuzhiyun  * @dev: device the managed resource is bound to
1039*4882a593Smuzhiyun  * @adapter: the adapter managing the device
1040*4882a593Smuzhiyun  * @address: seven bit address to be used
1041*4882a593Smuzhiyun  * Context: can sleep
1042*4882a593Smuzhiyun  *
1043*4882a593Smuzhiyun  * This is the device-managed version of @i2c_new_dummy_device. It returns the
1044*4882a593Smuzhiyun  * new i2c client or an ERR_PTR in case of an error.
1045*4882a593Smuzhiyun  */
devm_i2c_new_dummy_device(struct device * dev,struct i2c_adapter * adapter,u16 address)1046*4882a593Smuzhiyun struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
1047*4882a593Smuzhiyun 					     struct i2c_adapter *adapter,
1048*4882a593Smuzhiyun 					     u16 address)
1049*4882a593Smuzhiyun {
1050*4882a593Smuzhiyun 	struct i2c_dummy_devres *dr;
1051*4882a593Smuzhiyun 	struct i2c_client *client;
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
1054*4882a593Smuzhiyun 	if (!dr)
1055*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	client = i2c_new_dummy_device(adapter, address);
1058*4882a593Smuzhiyun 	if (IS_ERR(client)) {
1059*4882a593Smuzhiyun 		devres_free(dr);
1060*4882a593Smuzhiyun 	} else {
1061*4882a593Smuzhiyun 		dr->client = client;
1062*4882a593Smuzhiyun 		devres_add(dev, dr);
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	return client;
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun /**
1070*4882a593Smuzhiyun  * i2c_new_ancillary_device - Helper to get the instantiated secondary address
1071*4882a593Smuzhiyun  * and create the associated device
1072*4882a593Smuzhiyun  * @client: Handle to the primary client
1073*4882a593Smuzhiyun  * @name: Handle to specify which secondary address to get
1074*4882a593Smuzhiyun  * @default_addr: Used as a fallback if no secondary address was specified
1075*4882a593Smuzhiyun  * Context: can sleep
1076*4882a593Smuzhiyun  *
1077*4882a593Smuzhiyun  * I2C clients can be composed of multiple I2C slaves bound together in a single
1078*4882a593Smuzhiyun  * component. The I2C client driver then binds to the master I2C slave and needs
1079*4882a593Smuzhiyun  * to create I2C dummy clients to communicate with all the other slaves.
1080*4882a593Smuzhiyun  *
1081*4882a593Smuzhiyun  * This function creates and returns an I2C dummy client whose I2C address is
1082*4882a593Smuzhiyun  * retrieved from the platform firmware based on the given slave name. If no
1083*4882a593Smuzhiyun  * address is specified by the firmware default_addr is used.
1084*4882a593Smuzhiyun  *
1085*4882a593Smuzhiyun  * On DT-based platforms the address is retrieved from the "reg" property entry
1086*4882a593Smuzhiyun  * cell whose "reg-names" value matches the slave name.
1087*4882a593Smuzhiyun  *
1088*4882a593Smuzhiyun  * This returns the new i2c client, which should be saved for later use with
1089*4882a593Smuzhiyun  * i2c_unregister_device(); or an ERR_PTR to describe the error.
1090*4882a593Smuzhiyun  */
i2c_new_ancillary_device(struct i2c_client * client,const char * name,u16 default_addr)1091*4882a593Smuzhiyun struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
1092*4882a593Smuzhiyun 						const char *name,
1093*4882a593Smuzhiyun 						u16 default_addr)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	struct device_node *np = client->dev.of_node;
1096*4882a593Smuzhiyun 	u32 addr = default_addr;
1097*4882a593Smuzhiyun 	int i;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	if (np) {
1100*4882a593Smuzhiyun 		i = of_property_match_string(np, "reg-names", name);
1101*4882a593Smuzhiyun 		if (i >= 0)
1102*4882a593Smuzhiyun 			of_property_read_u32_index(np, "reg", i, &addr);
1103*4882a593Smuzhiyun 	}
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1106*4882a593Smuzhiyun 	return i2c_new_dummy_device(client->adapter, addr);
1107*4882a593Smuzhiyun }
1108*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1113*4882a593Smuzhiyun 
i2c_adapter_dev_release(struct device * dev)1114*4882a593Smuzhiyun static void i2c_adapter_dev_release(struct device *dev)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun 	struct i2c_adapter *adap = to_i2c_adapter(dev);
1117*4882a593Smuzhiyun 	complete(&adap->dev_released);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
i2c_adapter_depth(struct i2c_adapter * adapter)1120*4882a593Smuzhiyun unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	unsigned int depth = 0;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1125*4882a593Smuzhiyun 		depth++;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1128*4882a593Smuzhiyun 		  "adapter depth exceeds lockdep subclass limit\n");
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	return depth;
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_adapter_depth);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun /*
1135*4882a593Smuzhiyun  * Let users instantiate I2C devices through sysfs. This can be used when
1136*4882a593Smuzhiyun  * platform initialization code doesn't contain the proper data for
1137*4882a593Smuzhiyun  * whatever reason. Also useful for drivers that do device detection and
1138*4882a593Smuzhiyun  * detection fails, either because the device uses an unexpected address,
1139*4882a593Smuzhiyun  * or this is a compatible device with different ID register values.
1140*4882a593Smuzhiyun  *
1141*4882a593Smuzhiyun  * Parameter checking may look overzealous, but we really don't want
1142*4882a593Smuzhiyun  * the user to provide incorrect parameters.
1143*4882a593Smuzhiyun  */
1144*4882a593Smuzhiyun static ssize_t
new_device_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1145*4882a593Smuzhiyun new_device_store(struct device *dev, struct device_attribute *attr,
1146*4882a593Smuzhiyun 		 const char *buf, size_t count)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	struct i2c_adapter *adap = to_i2c_adapter(dev);
1149*4882a593Smuzhiyun 	struct i2c_board_info info;
1150*4882a593Smuzhiyun 	struct i2c_client *client;
1151*4882a593Smuzhiyun 	char *blank, end;
1152*4882a593Smuzhiyun 	int res;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	memset(&info, 0, sizeof(struct i2c_board_info));
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	blank = strchr(buf, ' ');
1157*4882a593Smuzhiyun 	if (!blank) {
1158*4882a593Smuzhiyun 		dev_err(dev, "%s: Missing parameters\n", "new_device");
1159*4882a593Smuzhiyun 		return -EINVAL;
1160*4882a593Smuzhiyun 	}
1161*4882a593Smuzhiyun 	if (blank - buf > I2C_NAME_SIZE - 1) {
1162*4882a593Smuzhiyun 		dev_err(dev, "%s: Invalid device name\n", "new_device");
1163*4882a593Smuzhiyun 		return -EINVAL;
1164*4882a593Smuzhiyun 	}
1165*4882a593Smuzhiyun 	memcpy(info.type, buf, blank - buf);
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	/* Parse remaining parameters, reject extra parameters */
1168*4882a593Smuzhiyun 	res = sscanf(++blank, "%hi%c", &info.addr, &end);
1169*4882a593Smuzhiyun 	if (res < 1) {
1170*4882a593Smuzhiyun 		dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1171*4882a593Smuzhiyun 		return -EINVAL;
1172*4882a593Smuzhiyun 	}
1173*4882a593Smuzhiyun 	if (res > 1  && end != '\n') {
1174*4882a593Smuzhiyun 		dev_err(dev, "%s: Extra parameters\n", "new_device");
1175*4882a593Smuzhiyun 		return -EINVAL;
1176*4882a593Smuzhiyun 	}
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1179*4882a593Smuzhiyun 		info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1180*4882a593Smuzhiyun 		info.flags |= I2C_CLIENT_TEN;
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1184*4882a593Smuzhiyun 		info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1185*4882a593Smuzhiyun 		info.flags |= I2C_CLIENT_SLAVE;
1186*4882a593Smuzhiyun 	}
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	client = i2c_new_client_device(adap, &info);
1189*4882a593Smuzhiyun 	if (IS_ERR(client))
1190*4882a593Smuzhiyun 		return PTR_ERR(client);
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	/* Keep track of the added device */
1193*4882a593Smuzhiyun 	mutex_lock(&adap->userspace_clients_lock);
1194*4882a593Smuzhiyun 	list_add_tail(&client->detected, &adap->userspace_clients);
1195*4882a593Smuzhiyun 	mutex_unlock(&adap->userspace_clients_lock);
1196*4882a593Smuzhiyun 	dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1197*4882a593Smuzhiyun 		 info.type, info.addr);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	return count;
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun static DEVICE_ATTR_WO(new_device);
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun /*
1204*4882a593Smuzhiyun  * And of course let the users delete the devices they instantiated, if
1205*4882a593Smuzhiyun  * they got it wrong. This interface can only be used to delete devices
1206*4882a593Smuzhiyun  * instantiated by i2c_sysfs_new_device above. This guarantees that we
1207*4882a593Smuzhiyun  * don't delete devices to which some kernel code still has references.
1208*4882a593Smuzhiyun  *
1209*4882a593Smuzhiyun  * Parameter checking may look overzealous, but we really don't want
1210*4882a593Smuzhiyun  * the user to delete the wrong device.
1211*4882a593Smuzhiyun  */
1212*4882a593Smuzhiyun static ssize_t
delete_device_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1213*4882a593Smuzhiyun delete_device_store(struct device *dev, struct device_attribute *attr,
1214*4882a593Smuzhiyun 		    const char *buf, size_t count)
1215*4882a593Smuzhiyun {
1216*4882a593Smuzhiyun 	struct i2c_adapter *adap = to_i2c_adapter(dev);
1217*4882a593Smuzhiyun 	struct i2c_client *client, *next;
1218*4882a593Smuzhiyun 	unsigned short addr;
1219*4882a593Smuzhiyun 	char end;
1220*4882a593Smuzhiyun 	int res;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	/* Parse parameters, reject extra parameters */
1223*4882a593Smuzhiyun 	res = sscanf(buf, "%hi%c", &addr, &end);
1224*4882a593Smuzhiyun 	if (res < 1) {
1225*4882a593Smuzhiyun 		dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1226*4882a593Smuzhiyun 		return -EINVAL;
1227*4882a593Smuzhiyun 	}
1228*4882a593Smuzhiyun 	if (res > 1  && end != '\n') {
1229*4882a593Smuzhiyun 		dev_err(dev, "%s: Extra parameters\n", "delete_device");
1230*4882a593Smuzhiyun 		return -EINVAL;
1231*4882a593Smuzhiyun 	}
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 	/* Make sure the device was added through sysfs */
1234*4882a593Smuzhiyun 	res = -ENOENT;
1235*4882a593Smuzhiyun 	mutex_lock_nested(&adap->userspace_clients_lock,
1236*4882a593Smuzhiyun 			  i2c_adapter_depth(adap));
1237*4882a593Smuzhiyun 	list_for_each_entry_safe(client, next, &adap->userspace_clients,
1238*4882a593Smuzhiyun 				 detected) {
1239*4882a593Smuzhiyun 		if (i2c_encode_flags_to_addr(client) == addr) {
1240*4882a593Smuzhiyun 			dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1241*4882a593Smuzhiyun 				 "delete_device", client->name, client->addr);
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 			list_del(&client->detected);
1244*4882a593Smuzhiyun 			i2c_unregister_device(client);
1245*4882a593Smuzhiyun 			res = count;
1246*4882a593Smuzhiyun 			break;
1247*4882a593Smuzhiyun 		}
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 	mutex_unlock(&adap->userspace_clients_lock);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	if (res < 0)
1252*4882a593Smuzhiyun 		dev_err(dev, "%s: Can't find device in list\n",
1253*4882a593Smuzhiyun 			"delete_device");
1254*4882a593Smuzhiyun 	return res;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1257*4882a593Smuzhiyun 				  delete_device_store);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun static struct attribute *i2c_adapter_attrs[] = {
1260*4882a593Smuzhiyun 	&dev_attr_name.attr,
1261*4882a593Smuzhiyun 	&dev_attr_new_device.attr,
1262*4882a593Smuzhiyun 	&dev_attr_delete_device.attr,
1263*4882a593Smuzhiyun 	NULL
1264*4882a593Smuzhiyun };
1265*4882a593Smuzhiyun ATTRIBUTE_GROUPS(i2c_adapter);
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun struct device_type i2c_adapter_type = {
1268*4882a593Smuzhiyun 	.groups		= i2c_adapter_groups,
1269*4882a593Smuzhiyun 	.release	= i2c_adapter_dev_release,
1270*4882a593Smuzhiyun };
1271*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_adapter_type);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun /**
1274*4882a593Smuzhiyun  * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1275*4882a593Smuzhiyun  * @dev: device, probably from some driver model iterator
1276*4882a593Smuzhiyun  *
1277*4882a593Smuzhiyun  * When traversing the driver model tree, perhaps using driver model
1278*4882a593Smuzhiyun  * iterators like @device_for_each_child(), you can't assume very much
1279*4882a593Smuzhiyun  * about the nodes you find.  Use this function to avoid oopses caused
1280*4882a593Smuzhiyun  * by wrongly treating some non-I2C device as an i2c_adapter.
1281*4882a593Smuzhiyun  */
i2c_verify_adapter(struct device * dev)1282*4882a593Smuzhiyun struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun 	return (dev->type == &i2c_adapter_type)
1285*4882a593Smuzhiyun 			? to_i2c_adapter(dev)
1286*4882a593Smuzhiyun 			: NULL;
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_verify_adapter);
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1291*4882a593Smuzhiyun static struct class_compat *i2c_adapter_compat_class;
1292*4882a593Smuzhiyun #endif
1293*4882a593Smuzhiyun 
i2c_scan_static_board_info(struct i2c_adapter * adapter)1294*4882a593Smuzhiyun static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	struct i2c_devinfo	*devinfo;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	down_read(&__i2c_board_lock);
1299*4882a593Smuzhiyun 	list_for_each_entry(devinfo, &__i2c_board_list, list) {
1300*4882a593Smuzhiyun 		if (devinfo->busnum == adapter->nr &&
1301*4882a593Smuzhiyun 		    IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
1302*4882a593Smuzhiyun 			dev_err(&adapter->dev,
1303*4882a593Smuzhiyun 				"Can't create device at 0x%02x\n",
1304*4882a593Smuzhiyun 				devinfo->board_info.addr);
1305*4882a593Smuzhiyun 	}
1306*4882a593Smuzhiyun 	up_read(&__i2c_board_lock);
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun 
i2c_do_add_adapter(struct i2c_driver * driver,struct i2c_adapter * adap)1309*4882a593Smuzhiyun static int i2c_do_add_adapter(struct i2c_driver *driver,
1310*4882a593Smuzhiyun 			      struct i2c_adapter *adap)
1311*4882a593Smuzhiyun {
1312*4882a593Smuzhiyun 	/* Detect supported devices on that bus, and instantiate them */
1313*4882a593Smuzhiyun 	i2c_detect(adap, driver);
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	return 0;
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun 
__process_new_adapter(struct device_driver * d,void * data)1318*4882a593Smuzhiyun static int __process_new_adapter(struct device_driver *d, void *data)
1319*4882a593Smuzhiyun {
1320*4882a593Smuzhiyun 	return i2c_do_add_adapter(to_i2c_driver(d), data);
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1324*4882a593Smuzhiyun 	.lock_bus =    i2c_adapter_lock_bus,
1325*4882a593Smuzhiyun 	.trylock_bus = i2c_adapter_trylock_bus,
1326*4882a593Smuzhiyun 	.unlock_bus =  i2c_adapter_unlock_bus,
1327*4882a593Smuzhiyun };
1328*4882a593Smuzhiyun 
i2c_host_notify_irq_teardown(struct i2c_adapter * adap)1329*4882a593Smuzhiyun static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1330*4882a593Smuzhiyun {
1331*4882a593Smuzhiyun 	struct irq_domain *domain = adap->host_notify_domain;
1332*4882a593Smuzhiyun 	irq_hw_number_t hwirq;
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	if (!domain)
1335*4882a593Smuzhiyun 		return;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1338*4882a593Smuzhiyun 		irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	irq_domain_remove(domain);
1341*4882a593Smuzhiyun 	adap->host_notify_domain = NULL;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun 
i2c_host_notify_irq_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t hw_irq_num)1344*4882a593Smuzhiyun static int i2c_host_notify_irq_map(struct irq_domain *h,
1345*4882a593Smuzhiyun 					  unsigned int virq,
1346*4882a593Smuzhiyun 					  irq_hw_number_t hw_irq_num)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	return 0;
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1354*4882a593Smuzhiyun 	.map = i2c_host_notify_irq_map,
1355*4882a593Smuzhiyun };
1356*4882a593Smuzhiyun 
i2c_setup_host_notify_irq_domain(struct i2c_adapter * adap)1357*4882a593Smuzhiyun static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun 	struct irq_domain *domain;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1362*4882a593Smuzhiyun 		return 0;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	domain = irq_domain_create_linear(adap->dev.parent->fwnode,
1365*4882a593Smuzhiyun 					  I2C_ADDR_7BITS_COUNT,
1366*4882a593Smuzhiyun 					  &i2c_host_notify_irq_ops, adap);
1367*4882a593Smuzhiyun 	if (!domain)
1368*4882a593Smuzhiyun 		return -ENOMEM;
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	adap->host_notify_domain = domain;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	return 0;
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun /**
1376*4882a593Smuzhiyun  * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1377*4882a593Smuzhiyun  * I2C client.
1378*4882a593Smuzhiyun  * @adap: the adapter
1379*4882a593Smuzhiyun  * @addr: the I2C address of the notifying device
1380*4882a593Smuzhiyun  * Context: can't sleep
1381*4882a593Smuzhiyun  *
1382*4882a593Smuzhiyun  * Helper function to be called from an I2C bus driver's interrupt
1383*4882a593Smuzhiyun  * handler. It will schedule the Host Notify IRQ.
1384*4882a593Smuzhiyun  */
i2c_handle_smbus_host_notify(struct i2c_adapter * adap,unsigned short addr)1385*4882a593Smuzhiyun int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1386*4882a593Smuzhiyun {
1387*4882a593Smuzhiyun 	int irq;
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	if (!adap)
1390*4882a593Smuzhiyun 		return -EINVAL;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	irq = irq_find_mapping(adap->host_notify_domain, addr);
1393*4882a593Smuzhiyun 	if (irq <= 0)
1394*4882a593Smuzhiyun 		return -ENXIO;
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	generic_handle_irq(irq);
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	return 0;
1399*4882a593Smuzhiyun }
1400*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1401*4882a593Smuzhiyun 
i2c_register_adapter(struct i2c_adapter * adap)1402*4882a593Smuzhiyun static int i2c_register_adapter(struct i2c_adapter *adap)
1403*4882a593Smuzhiyun {
1404*4882a593Smuzhiyun 	int res = -EINVAL;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	/* Can't register until after driver model init */
1407*4882a593Smuzhiyun 	if (WARN_ON(!is_registered)) {
1408*4882a593Smuzhiyun 		res = -EAGAIN;
1409*4882a593Smuzhiyun 		goto out_list;
1410*4882a593Smuzhiyun 	}
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	/* Sanity checks */
1413*4882a593Smuzhiyun 	if (WARN(!adap->name[0], "i2c adapter has no name"))
1414*4882a593Smuzhiyun 		goto out_list;
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	if (!adap->algo) {
1417*4882a593Smuzhiyun 		pr_err("adapter '%s': no algo supplied!\n", adap->name);
1418*4882a593Smuzhiyun 		goto out_list;
1419*4882a593Smuzhiyun 	}
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	if (!adap->lock_ops)
1422*4882a593Smuzhiyun 		adap->lock_ops = &i2c_adapter_lock_ops;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	adap->locked_flags = 0;
1425*4882a593Smuzhiyun 	rt_mutex_init(&adap->bus_lock);
1426*4882a593Smuzhiyun 	rt_mutex_init(&adap->mux_lock);
1427*4882a593Smuzhiyun 	mutex_init(&adap->userspace_clients_lock);
1428*4882a593Smuzhiyun 	INIT_LIST_HEAD(&adap->userspace_clients);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	/* Set default timeout to 1 second if not already set */
1431*4882a593Smuzhiyun 	if (adap->timeout == 0)
1432*4882a593Smuzhiyun 		adap->timeout = HZ;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* register soft irqs for Host Notify */
1435*4882a593Smuzhiyun 	res = i2c_setup_host_notify_irq_domain(adap);
1436*4882a593Smuzhiyun 	if (res) {
1437*4882a593Smuzhiyun 		pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1438*4882a593Smuzhiyun 		       adap->name, res);
1439*4882a593Smuzhiyun 		goto out_list;
1440*4882a593Smuzhiyun 	}
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1443*4882a593Smuzhiyun 	adap->dev.bus = &i2c_bus_type;
1444*4882a593Smuzhiyun 	adap->dev.type = &i2c_adapter_type;
1445*4882a593Smuzhiyun 	res = device_register(&adap->dev);
1446*4882a593Smuzhiyun 	if (res) {
1447*4882a593Smuzhiyun 		pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
1448*4882a593Smuzhiyun 		goto out_list;
1449*4882a593Smuzhiyun 	}
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	res = of_i2c_setup_smbus_alert(adap);
1452*4882a593Smuzhiyun 	if (res)
1453*4882a593Smuzhiyun 		goto out_reg;
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	pm_runtime_no_callbacks(&adap->dev);
1456*4882a593Smuzhiyun 	pm_suspend_ignore_children(&adap->dev, true);
1457*4882a593Smuzhiyun 	pm_runtime_enable(&adap->dev);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	res = i2c_init_recovery(adap);
1460*4882a593Smuzhiyun 	if (res == -EPROBE_DEFER)
1461*4882a593Smuzhiyun 		goto out_reg;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1466*4882a593Smuzhiyun 	res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1467*4882a593Smuzhiyun 				       adap->dev.parent);
1468*4882a593Smuzhiyun 	if (res)
1469*4882a593Smuzhiyun 		dev_warn(&adap->dev,
1470*4882a593Smuzhiyun 			 "Failed to create compatibility class link\n");
1471*4882a593Smuzhiyun #endif
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 	/* create pre-declared device nodes */
1474*4882a593Smuzhiyun 	of_i2c_register_devices(adap);
1475*4882a593Smuzhiyun 	i2c_acpi_install_space_handler(adap);
1476*4882a593Smuzhiyun 	i2c_acpi_register_devices(adap);
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	if (adap->nr < __i2c_first_dynamic_bus_num)
1479*4882a593Smuzhiyun 		i2c_scan_static_board_info(adap);
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	/* Notify drivers */
1482*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1483*4882a593Smuzhiyun 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1484*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	return 0;
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun out_reg:
1489*4882a593Smuzhiyun 	init_completion(&adap->dev_released);
1490*4882a593Smuzhiyun 	device_unregister(&adap->dev);
1491*4882a593Smuzhiyun 	wait_for_completion(&adap->dev_released);
1492*4882a593Smuzhiyun out_list:
1493*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1494*4882a593Smuzhiyun 	idr_remove(&i2c_adapter_idr, adap->nr);
1495*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1496*4882a593Smuzhiyun 	return res;
1497*4882a593Smuzhiyun }
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun /**
1500*4882a593Smuzhiyun  * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1501*4882a593Smuzhiyun  * @adap: the adapter to register (with adap->nr initialized)
1502*4882a593Smuzhiyun  * Context: can sleep
1503*4882a593Smuzhiyun  *
1504*4882a593Smuzhiyun  * See i2c_add_numbered_adapter() for details.
1505*4882a593Smuzhiyun  */
__i2c_add_numbered_adapter(struct i2c_adapter * adap)1506*4882a593Smuzhiyun static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1507*4882a593Smuzhiyun {
1508*4882a593Smuzhiyun 	int id;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1511*4882a593Smuzhiyun 	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
1512*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1513*4882a593Smuzhiyun 	if (WARN(id < 0, "couldn't get idr"))
1514*4882a593Smuzhiyun 		return id == -ENOSPC ? -EBUSY : id;
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	return i2c_register_adapter(adap);
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun /**
1520*4882a593Smuzhiyun  * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1521*4882a593Smuzhiyun  * @adapter: the adapter to add
1522*4882a593Smuzhiyun  * Context: can sleep
1523*4882a593Smuzhiyun  *
1524*4882a593Smuzhiyun  * This routine is used to declare an I2C adapter when its bus number
1525*4882a593Smuzhiyun  * doesn't matter or when its bus number is specified by an dt alias.
1526*4882a593Smuzhiyun  * Examples of bases when the bus number doesn't matter: I2C adapters
1527*4882a593Smuzhiyun  * dynamically added by USB links or PCI plugin cards.
1528*4882a593Smuzhiyun  *
1529*4882a593Smuzhiyun  * When this returns zero, a new bus number was allocated and stored
1530*4882a593Smuzhiyun  * in adap->nr, and the specified adapter became available for clients.
1531*4882a593Smuzhiyun  * Otherwise, a negative errno value is returned.
1532*4882a593Smuzhiyun  */
i2c_add_adapter(struct i2c_adapter * adapter)1533*4882a593Smuzhiyun int i2c_add_adapter(struct i2c_adapter *adapter)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	struct device *dev = &adapter->dev;
1536*4882a593Smuzhiyun 	int id;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	if (dev->of_node) {
1539*4882a593Smuzhiyun 		id = of_alias_get_id(dev->of_node, "i2c");
1540*4882a593Smuzhiyun 		if (id >= 0) {
1541*4882a593Smuzhiyun 			adapter->nr = id;
1542*4882a593Smuzhiyun 			return __i2c_add_numbered_adapter(adapter);
1543*4882a593Smuzhiyun 		}
1544*4882a593Smuzhiyun 	}
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1547*4882a593Smuzhiyun 	id = idr_alloc(&i2c_adapter_idr, adapter,
1548*4882a593Smuzhiyun 		       __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1549*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1550*4882a593Smuzhiyun 	if (WARN(id < 0, "couldn't get idr"))
1551*4882a593Smuzhiyun 		return id;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	adapter->nr = id;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	return i2c_register_adapter(adapter);
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_add_adapter);
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun /**
1560*4882a593Smuzhiyun  * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1561*4882a593Smuzhiyun  * @adap: the adapter to register (with adap->nr initialized)
1562*4882a593Smuzhiyun  * Context: can sleep
1563*4882a593Smuzhiyun  *
1564*4882a593Smuzhiyun  * This routine is used to declare an I2C adapter when its bus number
1565*4882a593Smuzhiyun  * matters.  For example, use it for I2C adapters from system-on-chip CPUs,
1566*4882a593Smuzhiyun  * or otherwise built in to the system's mainboard, and where i2c_board_info
1567*4882a593Smuzhiyun  * is used to properly configure I2C devices.
1568*4882a593Smuzhiyun  *
1569*4882a593Smuzhiyun  * If the requested bus number is set to -1, then this function will behave
1570*4882a593Smuzhiyun  * identically to i2c_add_adapter, and will dynamically assign a bus number.
1571*4882a593Smuzhiyun  *
1572*4882a593Smuzhiyun  * If no devices have pre-been declared for this bus, then be sure to
1573*4882a593Smuzhiyun  * register the adapter before any dynamically allocated ones.  Otherwise
1574*4882a593Smuzhiyun  * the required bus ID may not be available.
1575*4882a593Smuzhiyun  *
1576*4882a593Smuzhiyun  * When this returns zero, the specified adapter became available for
1577*4882a593Smuzhiyun  * clients using the bus number provided in adap->nr.  Also, the table
1578*4882a593Smuzhiyun  * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1579*4882a593Smuzhiyun  * and the appropriate driver model device nodes are created.  Otherwise, a
1580*4882a593Smuzhiyun  * negative errno value is returned.
1581*4882a593Smuzhiyun  */
i2c_add_numbered_adapter(struct i2c_adapter * adap)1582*4882a593Smuzhiyun int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1583*4882a593Smuzhiyun {
1584*4882a593Smuzhiyun 	if (adap->nr == -1) /* -1 means dynamically assign bus id */
1585*4882a593Smuzhiyun 		return i2c_add_adapter(adap);
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	return __i2c_add_numbered_adapter(adap);
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1590*4882a593Smuzhiyun 
i2c_do_del_adapter(struct i2c_driver * driver,struct i2c_adapter * adapter)1591*4882a593Smuzhiyun static void i2c_do_del_adapter(struct i2c_driver *driver,
1592*4882a593Smuzhiyun 			      struct i2c_adapter *adapter)
1593*4882a593Smuzhiyun {
1594*4882a593Smuzhiyun 	struct i2c_client *client, *_n;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	/* Remove the devices we created ourselves as the result of hardware
1597*4882a593Smuzhiyun 	 * probing (using a driver's detect method) */
1598*4882a593Smuzhiyun 	list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1599*4882a593Smuzhiyun 		if (client->adapter == adapter) {
1600*4882a593Smuzhiyun 			dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1601*4882a593Smuzhiyun 				client->name, client->addr);
1602*4882a593Smuzhiyun 			list_del(&client->detected);
1603*4882a593Smuzhiyun 			i2c_unregister_device(client);
1604*4882a593Smuzhiyun 		}
1605*4882a593Smuzhiyun 	}
1606*4882a593Smuzhiyun }
1607*4882a593Smuzhiyun 
__unregister_client(struct device * dev,void * dummy)1608*4882a593Smuzhiyun static int __unregister_client(struct device *dev, void *dummy)
1609*4882a593Smuzhiyun {
1610*4882a593Smuzhiyun 	struct i2c_client *client = i2c_verify_client(dev);
1611*4882a593Smuzhiyun 	if (client && strcmp(client->name, "dummy"))
1612*4882a593Smuzhiyun 		i2c_unregister_device(client);
1613*4882a593Smuzhiyun 	return 0;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun 
__unregister_dummy(struct device * dev,void * dummy)1616*4882a593Smuzhiyun static int __unregister_dummy(struct device *dev, void *dummy)
1617*4882a593Smuzhiyun {
1618*4882a593Smuzhiyun 	struct i2c_client *client = i2c_verify_client(dev);
1619*4882a593Smuzhiyun 	i2c_unregister_device(client);
1620*4882a593Smuzhiyun 	return 0;
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun 
__process_removed_adapter(struct device_driver * d,void * data)1623*4882a593Smuzhiyun static int __process_removed_adapter(struct device_driver *d, void *data)
1624*4882a593Smuzhiyun {
1625*4882a593Smuzhiyun 	i2c_do_del_adapter(to_i2c_driver(d), data);
1626*4882a593Smuzhiyun 	return 0;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun /**
1630*4882a593Smuzhiyun  * i2c_del_adapter - unregister I2C adapter
1631*4882a593Smuzhiyun  * @adap: the adapter being unregistered
1632*4882a593Smuzhiyun  * Context: can sleep
1633*4882a593Smuzhiyun  *
1634*4882a593Smuzhiyun  * This unregisters an I2C adapter which was previously registered
1635*4882a593Smuzhiyun  * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1636*4882a593Smuzhiyun  */
i2c_del_adapter(struct i2c_adapter * adap)1637*4882a593Smuzhiyun void i2c_del_adapter(struct i2c_adapter *adap)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun 	struct i2c_adapter *found;
1640*4882a593Smuzhiyun 	struct i2c_client *client, *next;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	/* First make sure that this adapter was ever added */
1643*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1644*4882a593Smuzhiyun 	found = idr_find(&i2c_adapter_idr, adap->nr);
1645*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1646*4882a593Smuzhiyun 	if (found != adap) {
1647*4882a593Smuzhiyun 		pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
1648*4882a593Smuzhiyun 		return;
1649*4882a593Smuzhiyun 	}
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	i2c_acpi_remove_space_handler(adap);
1652*4882a593Smuzhiyun 	/* Tell drivers about this removal */
1653*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1654*4882a593Smuzhiyun 	bus_for_each_drv(&i2c_bus_type, NULL, adap,
1655*4882a593Smuzhiyun 			       __process_removed_adapter);
1656*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	/* Remove devices instantiated from sysfs */
1659*4882a593Smuzhiyun 	mutex_lock_nested(&adap->userspace_clients_lock,
1660*4882a593Smuzhiyun 			  i2c_adapter_depth(adap));
1661*4882a593Smuzhiyun 	list_for_each_entry_safe(client, next, &adap->userspace_clients,
1662*4882a593Smuzhiyun 				 detected) {
1663*4882a593Smuzhiyun 		dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1664*4882a593Smuzhiyun 			client->addr);
1665*4882a593Smuzhiyun 		list_del(&client->detected);
1666*4882a593Smuzhiyun 		i2c_unregister_device(client);
1667*4882a593Smuzhiyun 	}
1668*4882a593Smuzhiyun 	mutex_unlock(&adap->userspace_clients_lock);
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	/* Detach any active clients. This can't fail, thus we do not
1671*4882a593Smuzhiyun 	 * check the returned value. This is a two-pass process, because
1672*4882a593Smuzhiyun 	 * we can't remove the dummy devices during the first pass: they
1673*4882a593Smuzhiyun 	 * could have been instantiated by real devices wishing to clean
1674*4882a593Smuzhiyun 	 * them up properly, so we give them a chance to do that first. */
1675*4882a593Smuzhiyun 	device_for_each_child(&adap->dev, NULL, __unregister_client);
1676*4882a593Smuzhiyun 	device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1679*4882a593Smuzhiyun 	class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1680*4882a593Smuzhiyun 				 adap->dev.parent);
1681*4882a593Smuzhiyun #endif
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	/* device name is gone after device_unregister */
1684*4882a593Smuzhiyun 	dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	pm_runtime_disable(&adap->dev);
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 	i2c_host_notify_irq_teardown(adap);
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	/* wait until all references to the device are gone
1691*4882a593Smuzhiyun 	 *
1692*4882a593Smuzhiyun 	 * FIXME: This is old code and should ideally be replaced by an
1693*4882a593Smuzhiyun 	 * alternative which results in decoupling the lifetime of the struct
1694*4882a593Smuzhiyun 	 * device from the i2c_adapter, like spi or netdev do. Any solution
1695*4882a593Smuzhiyun 	 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
1696*4882a593Smuzhiyun 	 */
1697*4882a593Smuzhiyun 	init_completion(&adap->dev_released);
1698*4882a593Smuzhiyun 	device_unregister(&adap->dev);
1699*4882a593Smuzhiyun 	wait_for_completion(&adap->dev_released);
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 	/* free bus id */
1702*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1703*4882a593Smuzhiyun 	idr_remove(&i2c_adapter_idr, adap->nr);
1704*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	/* Clear the device structure in case this adapter is ever going to be
1707*4882a593Smuzhiyun 	   added again */
1708*4882a593Smuzhiyun 	memset(&adap->dev, 0, sizeof(adap->dev));
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_del_adapter);
1711*4882a593Smuzhiyun 
i2c_parse_timing(struct device * dev,char * prop_name,u32 * cur_val_p,u32 def_val,bool use_def)1712*4882a593Smuzhiyun static void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p,
1713*4882a593Smuzhiyun 			    u32 def_val, bool use_def)
1714*4882a593Smuzhiyun {
1715*4882a593Smuzhiyun 	int ret;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	ret = device_property_read_u32(dev, prop_name, cur_val_p);
1718*4882a593Smuzhiyun 	if (ret && use_def)
1719*4882a593Smuzhiyun 		*cur_val_p = def_val;
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p);
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun /**
1725*4882a593Smuzhiyun  * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1726*4882a593Smuzhiyun  * @dev: The device to scan for I2C timing properties
1727*4882a593Smuzhiyun  * @t: the i2c_timings struct to be filled with values
1728*4882a593Smuzhiyun  * @use_defaults: bool to use sane defaults derived from the I2C specification
1729*4882a593Smuzhiyun  *		  when properties are not found, otherwise don't update
1730*4882a593Smuzhiyun  *
1731*4882a593Smuzhiyun  * Scan the device for the generic I2C properties describing timing parameters
1732*4882a593Smuzhiyun  * for the signal and fill the given struct with the results. If a property was
1733*4882a593Smuzhiyun  * not found and use_defaults was true, then maximum timings are assumed which
1734*4882a593Smuzhiyun  * are derived from the I2C specification. If use_defaults is not used, the
1735*4882a593Smuzhiyun  * results will be as before, so drivers can apply their own defaults before
1736*4882a593Smuzhiyun  * calling this helper. The latter is mainly intended for avoiding regressions
1737*4882a593Smuzhiyun  * of existing drivers which want to switch to this function. New drivers
1738*4882a593Smuzhiyun  * almost always should use the defaults.
1739*4882a593Smuzhiyun  */
i2c_parse_fw_timings(struct device * dev,struct i2c_timings * t,bool use_defaults)1740*4882a593Smuzhiyun void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1741*4882a593Smuzhiyun {
1742*4882a593Smuzhiyun 	bool u = use_defaults;
1743*4882a593Smuzhiyun 	u32 d;
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz,
1746*4882a593Smuzhiyun 			 I2C_MAX_STANDARD_MODE_FREQ, u);
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 :
1749*4882a593Smuzhiyun 	    t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1750*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u);
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1753*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u);
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-scl-internal-delay-ns",
1756*4882a593Smuzhiyun 			 &t->scl_int_delay_ns, 0, u);
1757*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns,
1758*4882a593Smuzhiyun 			 t->scl_fall_ns, u);
1759*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u);
1760*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-digital-filter-width-ns",
1761*4882a593Smuzhiyun 			 &t->digital_filter_width_ns, 0, u);
1762*4882a593Smuzhiyun 	i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency",
1763*4882a593Smuzhiyun 			 &t->analog_filter_cutoff_freq_hz, 0, u);
1764*4882a593Smuzhiyun }
1765*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
1768*4882a593Smuzhiyun 
i2c_for_each_dev(void * data,int (* fn)(struct device * dev,void * data))1769*4882a593Smuzhiyun int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
1770*4882a593Smuzhiyun {
1771*4882a593Smuzhiyun 	int res;
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 	mutex_lock(&core_lock);
1774*4882a593Smuzhiyun 	res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1775*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	return res;
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1780*4882a593Smuzhiyun 
__process_new_driver(struct device * dev,void * data)1781*4882a593Smuzhiyun static int __process_new_driver(struct device *dev, void *data)
1782*4882a593Smuzhiyun {
1783*4882a593Smuzhiyun 	if (dev->type != &i2c_adapter_type)
1784*4882a593Smuzhiyun 		return 0;
1785*4882a593Smuzhiyun 	return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun /*
1789*4882a593Smuzhiyun  * An i2c_driver is used with one or more i2c_client (device) nodes to access
1790*4882a593Smuzhiyun  * i2c slave chips, on a bus instance associated with some i2c_adapter.
1791*4882a593Smuzhiyun  */
1792*4882a593Smuzhiyun 
i2c_register_driver(struct module * owner,struct i2c_driver * driver)1793*4882a593Smuzhiyun int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1794*4882a593Smuzhiyun {
1795*4882a593Smuzhiyun 	int res;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 	/* Can't register until after driver model init */
1798*4882a593Smuzhiyun 	if (WARN_ON(!is_registered))
1799*4882a593Smuzhiyun 		return -EAGAIN;
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	/* add the driver to the list of i2c drivers in the driver core */
1802*4882a593Smuzhiyun 	driver->driver.owner = owner;
1803*4882a593Smuzhiyun 	driver->driver.bus = &i2c_bus_type;
1804*4882a593Smuzhiyun 	INIT_LIST_HEAD(&driver->clients);
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	/* When registration returns, the driver core
1807*4882a593Smuzhiyun 	 * will have called probe() for all matching-but-unbound devices.
1808*4882a593Smuzhiyun 	 */
1809*4882a593Smuzhiyun 	res = driver_register(&driver->driver);
1810*4882a593Smuzhiyun 	if (res)
1811*4882a593Smuzhiyun 		return res;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	pr_debug("driver [%s] registered\n", driver->driver.name);
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	/* Walk the adapters that are already present */
1816*4882a593Smuzhiyun 	i2c_for_each_dev(driver, __process_new_driver);
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun 	return 0;
1819*4882a593Smuzhiyun }
1820*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_register_driver);
1821*4882a593Smuzhiyun 
__process_removed_driver(struct device * dev,void * data)1822*4882a593Smuzhiyun static int __process_removed_driver(struct device *dev, void *data)
1823*4882a593Smuzhiyun {
1824*4882a593Smuzhiyun 	if (dev->type == &i2c_adapter_type)
1825*4882a593Smuzhiyun 		i2c_do_del_adapter(data, to_i2c_adapter(dev));
1826*4882a593Smuzhiyun 	return 0;
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun /**
1830*4882a593Smuzhiyun  * i2c_del_driver - unregister I2C driver
1831*4882a593Smuzhiyun  * @driver: the driver being unregistered
1832*4882a593Smuzhiyun  * Context: can sleep
1833*4882a593Smuzhiyun  */
i2c_del_driver(struct i2c_driver * driver)1834*4882a593Smuzhiyun void i2c_del_driver(struct i2c_driver *driver)
1835*4882a593Smuzhiyun {
1836*4882a593Smuzhiyun 	i2c_for_each_dev(driver, __process_removed_driver);
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	driver_unregister(&driver->driver);
1839*4882a593Smuzhiyun 	pr_debug("driver [%s] unregistered\n", driver->driver.name);
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_del_driver);
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun struct i2c_addr_cnt {
1846*4882a593Smuzhiyun 	int addr;
1847*4882a593Smuzhiyun 	int cnt;
1848*4882a593Smuzhiyun };
1849*4882a593Smuzhiyun 
__i2c_check_addr_ex(struct device * dev,void * addrp)1850*4882a593Smuzhiyun static int __i2c_check_addr_ex(struct device *dev, void *addrp)
1851*4882a593Smuzhiyun {
1852*4882a593Smuzhiyun 	struct i2c_client *client = i2c_verify_client(dev);
1853*4882a593Smuzhiyun 	struct i2c_addr_cnt *addrinfo = (struct i2c_addr_cnt *)addrp;
1854*4882a593Smuzhiyun 	int addr = addrinfo->addr;
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	if (client && client->addr == addr)
1857*4882a593Smuzhiyun 		addrinfo->cnt++;
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 	return 0;
1860*4882a593Smuzhiyun }
1861*4882a593Smuzhiyun 
i2c_check_addr_ex(struct i2c_adapter * adapter,int addr)1862*4882a593Smuzhiyun static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr)
1863*4882a593Smuzhiyun {
1864*4882a593Smuzhiyun 	struct i2c_addr_cnt addrinfo;
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 	addrinfo.addr = addr;
1867*4882a593Smuzhiyun 	addrinfo.cnt = 0;
1868*4882a593Smuzhiyun 	device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex);
1869*4882a593Smuzhiyun 	return addrinfo.cnt;
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun struct i2c_cmd_arg {
1873*4882a593Smuzhiyun 	unsigned	cmd;
1874*4882a593Smuzhiyun 	void		*arg;
1875*4882a593Smuzhiyun };
1876*4882a593Smuzhiyun 
i2c_cmd(struct device * dev,void * _arg)1877*4882a593Smuzhiyun static int i2c_cmd(struct device *dev, void *_arg)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun 	struct i2c_client	*client = i2c_verify_client(dev);
1880*4882a593Smuzhiyun 	struct i2c_cmd_arg	*arg = _arg;
1881*4882a593Smuzhiyun 	struct i2c_driver	*driver;
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	if (!client || !client->dev.driver)
1884*4882a593Smuzhiyun 		return 0;
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 	driver = to_i2c_driver(client->dev.driver);
1887*4882a593Smuzhiyun 	if (driver->command)
1888*4882a593Smuzhiyun 		driver->command(client, arg->cmd, arg->arg);
1889*4882a593Smuzhiyun 	return 0;
1890*4882a593Smuzhiyun }
1891*4882a593Smuzhiyun 
i2c_clients_command(struct i2c_adapter * adap,unsigned int cmd,void * arg)1892*4882a593Smuzhiyun void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1893*4882a593Smuzhiyun {
1894*4882a593Smuzhiyun 	struct i2c_cmd_arg	cmd_arg;
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	cmd_arg.cmd = cmd;
1897*4882a593Smuzhiyun 	cmd_arg.arg = arg;
1898*4882a593Smuzhiyun 	device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1899*4882a593Smuzhiyun }
1900*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_clients_command);
1901*4882a593Smuzhiyun 
i2c_init(void)1902*4882a593Smuzhiyun static int __init i2c_init(void)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun 	int retval;
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 	retval = of_alias_get_highest_id("i2c");
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	down_write(&__i2c_board_lock);
1909*4882a593Smuzhiyun 	if (retval >= __i2c_first_dynamic_bus_num)
1910*4882a593Smuzhiyun 		__i2c_first_dynamic_bus_num = retval + 1;
1911*4882a593Smuzhiyun 	up_write(&__i2c_board_lock);
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	retval = bus_register(&i2c_bus_type);
1914*4882a593Smuzhiyun 	if (retval)
1915*4882a593Smuzhiyun 		return retval;
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	is_registered = true;
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1920*4882a593Smuzhiyun 	i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1921*4882a593Smuzhiyun 	if (!i2c_adapter_compat_class) {
1922*4882a593Smuzhiyun 		retval = -ENOMEM;
1923*4882a593Smuzhiyun 		goto bus_err;
1924*4882a593Smuzhiyun 	}
1925*4882a593Smuzhiyun #endif
1926*4882a593Smuzhiyun 	retval = i2c_add_driver(&dummy_driver);
1927*4882a593Smuzhiyun 	if (retval)
1928*4882a593Smuzhiyun 		goto class_err;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1931*4882a593Smuzhiyun 		WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1932*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_ACPI))
1933*4882a593Smuzhiyun 		WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 	return 0;
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun class_err:
1938*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1939*4882a593Smuzhiyun 	class_compat_unregister(i2c_adapter_compat_class);
1940*4882a593Smuzhiyun bus_err:
1941*4882a593Smuzhiyun #endif
1942*4882a593Smuzhiyun 	is_registered = false;
1943*4882a593Smuzhiyun 	bus_unregister(&i2c_bus_type);
1944*4882a593Smuzhiyun 	return retval;
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun 
i2c_exit(void)1947*4882a593Smuzhiyun static void __exit i2c_exit(void)
1948*4882a593Smuzhiyun {
1949*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_ACPI))
1950*4882a593Smuzhiyun 		WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
1951*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1952*4882a593Smuzhiyun 		WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
1953*4882a593Smuzhiyun 	i2c_del_driver(&dummy_driver);
1954*4882a593Smuzhiyun #ifdef CONFIG_I2C_COMPAT
1955*4882a593Smuzhiyun 	class_compat_unregister(i2c_adapter_compat_class);
1956*4882a593Smuzhiyun #endif
1957*4882a593Smuzhiyun 	bus_unregister(&i2c_bus_type);
1958*4882a593Smuzhiyun 	tracepoint_synchronize_unregister();
1959*4882a593Smuzhiyun }
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun /* We must initialize early, because some subsystems register i2c drivers
1962*4882a593Smuzhiyun  * in subsys_initcall() code, but are linked (and initialized) before i2c.
1963*4882a593Smuzhiyun  */
1964*4882a593Smuzhiyun postcore_initcall(i2c_init);
1965*4882a593Smuzhiyun module_exit(i2c_exit);
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun /* ----------------------------------------------------
1968*4882a593Smuzhiyun  * the functional interface to the i2c busses.
1969*4882a593Smuzhiyun  * ----------------------------------------------------
1970*4882a593Smuzhiyun  */
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun /* Check if val is exceeding the quirk IFF quirk is non 0 */
1973*4882a593Smuzhiyun #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1974*4882a593Smuzhiyun 
i2c_quirk_error(struct i2c_adapter * adap,struct i2c_msg * msg,char * err_msg)1975*4882a593Smuzhiyun static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun 	dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1978*4882a593Smuzhiyun 			    err_msg, msg->addr, msg->len,
1979*4882a593Smuzhiyun 			    msg->flags & I2C_M_RD ? "read" : "write");
1980*4882a593Smuzhiyun 	return -EOPNOTSUPP;
1981*4882a593Smuzhiyun }
1982*4882a593Smuzhiyun 
i2c_check_for_quirks(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)1983*4882a593Smuzhiyun static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1984*4882a593Smuzhiyun {
1985*4882a593Smuzhiyun 	const struct i2c_adapter_quirks *q = adap->quirks;
1986*4882a593Smuzhiyun 	int max_num = q->max_num_msgs, i;
1987*4882a593Smuzhiyun 	bool do_len_check = true;
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	if (q->flags & I2C_AQ_COMB) {
1990*4882a593Smuzhiyun 		max_num = 2;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 		/* special checks for combined messages */
1993*4882a593Smuzhiyun 		if (num == 2) {
1994*4882a593Smuzhiyun 			if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1995*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 			if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1998*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 			if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2001*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 			if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2004*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[0], "msg too long");
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 			if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2007*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[1], "msg too long");
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 			do_len_check = false;
2010*4882a593Smuzhiyun 		}
2011*4882a593Smuzhiyun 	}
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	if (i2c_quirk_exceeded(num, max_num))
2014*4882a593Smuzhiyun 		return i2c_quirk_error(adap, &msgs[0], "too many messages");
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	for (i = 0; i < num; i++) {
2017*4882a593Smuzhiyun 		u16 len = msgs[i].len;
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 		if (msgs[i].flags & I2C_M_RD) {
2020*4882a593Smuzhiyun 			if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2021*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[i], "msg too long");
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 			if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
2024*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[i], "no zero length");
2025*4882a593Smuzhiyun 		} else {
2026*4882a593Smuzhiyun 			if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2027*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[i], "msg too long");
2028*4882a593Smuzhiyun 
2029*4882a593Smuzhiyun 			if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
2030*4882a593Smuzhiyun 				return i2c_quirk_error(adap, &msgs[i], "no zero length");
2031*4882a593Smuzhiyun 		}
2032*4882a593Smuzhiyun 	}
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun 	return 0;
2035*4882a593Smuzhiyun }
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun /**
2038*4882a593Smuzhiyun  * __i2c_transfer - unlocked flavor of i2c_transfer
2039*4882a593Smuzhiyun  * @adap: Handle to I2C bus
2040*4882a593Smuzhiyun  * @msgs: One or more messages to execute before STOP is issued to
2041*4882a593Smuzhiyun  *	terminate the operation; each message begins with a START.
2042*4882a593Smuzhiyun  * @num: Number of messages to be executed.
2043*4882a593Smuzhiyun  *
2044*4882a593Smuzhiyun  * Returns negative errno, else the number of messages executed.
2045*4882a593Smuzhiyun  *
2046*4882a593Smuzhiyun  * Adapter lock must be held when calling this function. No debug logging
2047*4882a593Smuzhiyun  * takes place. adap->algo->master_xfer existence isn't checked.
2048*4882a593Smuzhiyun  */
__i2c_transfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)2049*4882a593Smuzhiyun int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun 	unsigned long orig_jiffies;
2052*4882a593Smuzhiyun 	int ret, try;
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	if (WARN_ON(!msgs || num < 1))
2055*4882a593Smuzhiyun 		return -EINVAL;
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 	ret = __i2c_check_suspended(adap);
2058*4882a593Smuzhiyun 	if (ret)
2059*4882a593Smuzhiyun 		return ret;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2062*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	/*
2065*4882a593Smuzhiyun 	 * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
2066*4882a593Smuzhiyun 	 * enabled.  This is an efficient way of keeping the for-loop from
2067*4882a593Smuzhiyun 	 * being executed when not needed.
2068*4882a593Smuzhiyun 	 */
2069*4882a593Smuzhiyun 	if (static_branch_unlikely(&i2c_trace_msg_key)) {
2070*4882a593Smuzhiyun 		int i;
2071*4882a593Smuzhiyun 		for (i = 0; i < num; i++)
2072*4882a593Smuzhiyun 			if (msgs[i].flags & I2C_M_RD)
2073*4882a593Smuzhiyun 				trace_i2c_read(adap, &msgs[i], i);
2074*4882a593Smuzhiyun 			else
2075*4882a593Smuzhiyun 				trace_i2c_write(adap, &msgs[i], i);
2076*4882a593Smuzhiyun 	}
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	/* Retry automatically on arbitration loss */
2079*4882a593Smuzhiyun 	orig_jiffies = jiffies;
2080*4882a593Smuzhiyun 	for (ret = 0, try = 0; try <= adap->retries; try++) {
2081*4882a593Smuzhiyun 		if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
2082*4882a593Smuzhiyun 			ret = adap->algo->master_xfer_atomic(adap, msgs, num);
2083*4882a593Smuzhiyun 		else
2084*4882a593Smuzhiyun 			ret = adap->algo->master_xfer(adap, msgs, num);
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 		if (ret != -EAGAIN)
2087*4882a593Smuzhiyun 			break;
2088*4882a593Smuzhiyun 		if (time_after(jiffies, orig_jiffies + adap->timeout))
2089*4882a593Smuzhiyun 			break;
2090*4882a593Smuzhiyun 	}
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	if (static_branch_unlikely(&i2c_trace_msg_key)) {
2093*4882a593Smuzhiyun 		int i;
2094*4882a593Smuzhiyun 		for (i = 0; i < ret; i++)
2095*4882a593Smuzhiyun 			if (msgs[i].flags & I2C_M_RD)
2096*4882a593Smuzhiyun 				trace_i2c_reply(adap, &msgs[i], i);
2097*4882a593Smuzhiyun 		trace_i2c_result(adap, num, ret);
2098*4882a593Smuzhiyun 	}
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	return ret;
2101*4882a593Smuzhiyun }
2102*4882a593Smuzhiyun EXPORT_SYMBOL(__i2c_transfer);
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun /**
2105*4882a593Smuzhiyun  * i2c_transfer - execute a single or combined I2C message
2106*4882a593Smuzhiyun  * @adap: Handle to I2C bus
2107*4882a593Smuzhiyun  * @msgs: One or more messages to execute before STOP is issued to
2108*4882a593Smuzhiyun  *	terminate the operation; each message begins with a START.
2109*4882a593Smuzhiyun  * @num: Number of messages to be executed.
2110*4882a593Smuzhiyun  *
2111*4882a593Smuzhiyun  * Returns negative errno, else the number of messages executed.
2112*4882a593Smuzhiyun  *
2113*4882a593Smuzhiyun  * Note that there is no requirement that each message be sent to
2114*4882a593Smuzhiyun  * the same slave address, although that is the most common model.
2115*4882a593Smuzhiyun  */
i2c_transfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)2116*4882a593Smuzhiyun int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun 	int ret;
2119*4882a593Smuzhiyun 
2120*4882a593Smuzhiyun 	if (!adap->algo->master_xfer) {
2121*4882a593Smuzhiyun 		dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2122*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2123*4882a593Smuzhiyun 	}
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	/* REVISIT the fault reporting model here is weak:
2126*4882a593Smuzhiyun 	 *
2127*4882a593Smuzhiyun 	 *  - When we get an error after receiving N bytes from a slave,
2128*4882a593Smuzhiyun 	 *    there is no way to report "N".
2129*4882a593Smuzhiyun 	 *
2130*4882a593Smuzhiyun 	 *  - When we get a NAK after transmitting N bytes to a slave,
2131*4882a593Smuzhiyun 	 *    there is no way to report "N" ... or to let the master
2132*4882a593Smuzhiyun 	 *    continue executing the rest of this combined message, if
2133*4882a593Smuzhiyun 	 *    that's the appropriate response.
2134*4882a593Smuzhiyun 	 *
2135*4882a593Smuzhiyun 	 *  - When for example "num" is two and we successfully complete
2136*4882a593Smuzhiyun 	 *    the first message but get an error part way through the
2137*4882a593Smuzhiyun 	 *    second, it's unclear whether that should be reported as
2138*4882a593Smuzhiyun 	 *    one (discarding status on the second message) or errno
2139*4882a593Smuzhiyun 	 *    (discarding status on the first one).
2140*4882a593Smuzhiyun 	 */
2141*4882a593Smuzhiyun 	ret = __i2c_lock_bus_helper(adap);
2142*4882a593Smuzhiyun 	if (ret)
2143*4882a593Smuzhiyun 		return ret;
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	ret = __i2c_transfer(adap, msgs, num);
2146*4882a593Smuzhiyun 	i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	return ret;
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_transfer);
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun /**
2153*4882a593Smuzhiyun  * i2c_transfer_buffer_flags - issue a single I2C message transferring data
2154*4882a593Smuzhiyun  *			       to/from a buffer
2155*4882a593Smuzhiyun  * @client: Handle to slave device
2156*4882a593Smuzhiyun  * @buf: Where the data is stored
2157*4882a593Smuzhiyun  * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
2158*4882a593Smuzhiyun  * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
2159*4882a593Smuzhiyun  *
2160*4882a593Smuzhiyun  * Returns negative errno, or else the number of bytes transferred.
2161*4882a593Smuzhiyun  */
i2c_transfer_buffer_flags(const struct i2c_client * client,char * buf,int count,u16 flags)2162*4882a593Smuzhiyun int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
2163*4882a593Smuzhiyun 			      int count, u16 flags)
2164*4882a593Smuzhiyun {
2165*4882a593Smuzhiyun 	int ret;
2166*4882a593Smuzhiyun 	struct i2c_msg msg = {
2167*4882a593Smuzhiyun 		.addr = client->addr,
2168*4882a593Smuzhiyun 		.flags = flags | (client->flags & I2C_M_TEN),
2169*4882a593Smuzhiyun 		.len = count,
2170*4882a593Smuzhiyun 		.buf = buf,
2171*4882a593Smuzhiyun 	};
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	ret = i2c_transfer(client->adapter, &msg, 1);
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun 	/*
2176*4882a593Smuzhiyun 	 * If everything went ok (i.e. 1 msg transferred), return #bytes
2177*4882a593Smuzhiyun 	 * transferred, else error code.
2178*4882a593Smuzhiyun 	 */
2179*4882a593Smuzhiyun 	return (ret == 1) ? count : ret;
2180*4882a593Smuzhiyun }
2181*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_transfer_buffer_flags);
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun /**
2184*4882a593Smuzhiyun  * i2c_get_device_id - get manufacturer, part id and die revision of a device
2185*4882a593Smuzhiyun  * @client: The device to query
2186*4882a593Smuzhiyun  * @id: The queried information
2187*4882a593Smuzhiyun  *
2188*4882a593Smuzhiyun  * Returns negative errno on error, zero on success.
2189*4882a593Smuzhiyun  */
i2c_get_device_id(const struct i2c_client * client,struct i2c_device_identity * id)2190*4882a593Smuzhiyun int i2c_get_device_id(const struct i2c_client *client,
2191*4882a593Smuzhiyun 		      struct i2c_device_identity *id)
2192*4882a593Smuzhiyun {
2193*4882a593Smuzhiyun 	struct i2c_adapter *adap = client->adapter;
2194*4882a593Smuzhiyun 	union i2c_smbus_data raw_id;
2195*4882a593Smuzhiyun 	int ret;
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
2198*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun 	raw_id.block[0] = 3;
2201*4882a593Smuzhiyun 	ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
2202*4882a593Smuzhiyun 			     I2C_SMBUS_READ, client->addr << 1,
2203*4882a593Smuzhiyun 			     I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
2204*4882a593Smuzhiyun 	if (ret)
2205*4882a593Smuzhiyun 		return ret;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
2208*4882a593Smuzhiyun 	id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
2209*4882a593Smuzhiyun 	id->die_revision = raw_id.block[3] & 0x7;
2210*4882a593Smuzhiyun 	return 0;
2211*4882a593Smuzhiyun }
2212*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_get_device_id);
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun /* ----------------------------------------------------
2215*4882a593Smuzhiyun  * the i2c address scanning function
2216*4882a593Smuzhiyun  * Will not work for 10-bit addresses!
2217*4882a593Smuzhiyun  * ----------------------------------------------------
2218*4882a593Smuzhiyun  */
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun /*
2221*4882a593Smuzhiyun  * Legacy default probe function, mostly relevant for SMBus. The default
2222*4882a593Smuzhiyun  * probe method is a quick write, but it is known to corrupt the 24RF08
2223*4882a593Smuzhiyun  * EEPROMs due to a state machine bug, and could also irreversibly
2224*4882a593Smuzhiyun  * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2225*4882a593Smuzhiyun  * we use a short byte read instead. Also, some bus drivers don't implement
2226*4882a593Smuzhiyun  * quick write, so we fallback to a byte read in that case too.
2227*4882a593Smuzhiyun  * On x86, there is another special case for FSC hardware monitoring chips,
2228*4882a593Smuzhiyun  * which want regular byte reads (address 0x73.) Fortunately, these are the
2229*4882a593Smuzhiyun  * only known chips using this I2C address on PC hardware.
2230*4882a593Smuzhiyun  * Returns 1 if probe succeeded, 0 if not.
2231*4882a593Smuzhiyun  */
i2c_default_probe(struct i2c_adapter * adap,unsigned short addr)2232*4882a593Smuzhiyun static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2233*4882a593Smuzhiyun {
2234*4882a593Smuzhiyun 	int err;
2235*4882a593Smuzhiyun 	union i2c_smbus_data dummy;
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun #ifdef CONFIG_X86
2238*4882a593Smuzhiyun 	if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2239*4882a593Smuzhiyun 	 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2240*4882a593Smuzhiyun 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2241*4882a593Smuzhiyun 				     I2C_SMBUS_BYTE_DATA, &dummy);
2242*4882a593Smuzhiyun 	else
2243*4882a593Smuzhiyun #endif
2244*4882a593Smuzhiyun 	if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2245*4882a593Smuzhiyun 	 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
2246*4882a593Smuzhiyun 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2247*4882a593Smuzhiyun 				     I2C_SMBUS_QUICK, NULL);
2248*4882a593Smuzhiyun 	else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2249*4882a593Smuzhiyun 		err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2250*4882a593Smuzhiyun 				     I2C_SMBUS_BYTE, &dummy);
2251*4882a593Smuzhiyun 	else {
2252*4882a593Smuzhiyun 		dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2253*4882a593Smuzhiyun 			 addr);
2254*4882a593Smuzhiyun 		err = -EOPNOTSUPP;
2255*4882a593Smuzhiyun 	}
2256*4882a593Smuzhiyun 
2257*4882a593Smuzhiyun 	return err >= 0;
2258*4882a593Smuzhiyun }
2259*4882a593Smuzhiyun 
i2c_detect_address(struct i2c_client * temp_client,struct i2c_driver * driver)2260*4882a593Smuzhiyun static int i2c_detect_address(struct i2c_client *temp_client,
2261*4882a593Smuzhiyun 			      struct i2c_driver *driver)
2262*4882a593Smuzhiyun {
2263*4882a593Smuzhiyun 	struct i2c_board_info info;
2264*4882a593Smuzhiyun 	struct i2c_adapter *adapter = temp_client->adapter;
2265*4882a593Smuzhiyun 	int addr = temp_client->addr;
2266*4882a593Smuzhiyun 	int err;
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun 	/* Make sure the address is valid */
2269*4882a593Smuzhiyun 	err = i2c_check_7bit_addr_validity_strict(addr);
2270*4882a593Smuzhiyun 	if (err) {
2271*4882a593Smuzhiyun 		dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2272*4882a593Smuzhiyun 			 addr);
2273*4882a593Smuzhiyun 		return err;
2274*4882a593Smuzhiyun 	}
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	/* Skip if already in use (7 bit, no need to encode flags) */
2277*4882a593Smuzhiyun 	if (i2c_check_addr_busy(adapter, addr))
2278*4882a593Smuzhiyun 		return 0;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	/* Make sure there is something at this address */
2281*4882a593Smuzhiyun 	if (!i2c_default_probe(adapter, addr))
2282*4882a593Smuzhiyun 		return 0;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 	/* Finally call the custom detection function */
2285*4882a593Smuzhiyun 	memset(&info, 0, sizeof(struct i2c_board_info));
2286*4882a593Smuzhiyun 	info.addr = addr;
2287*4882a593Smuzhiyun 	err = driver->detect(temp_client, &info);
2288*4882a593Smuzhiyun 	if (err) {
2289*4882a593Smuzhiyun 		/* -ENODEV is returned if the detection fails. We catch it
2290*4882a593Smuzhiyun 		   here as this isn't an error. */
2291*4882a593Smuzhiyun 		return err == -ENODEV ? 0 : err;
2292*4882a593Smuzhiyun 	}
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 	/* Consistency check */
2295*4882a593Smuzhiyun 	if (info.type[0] == '\0') {
2296*4882a593Smuzhiyun 		dev_err(&adapter->dev,
2297*4882a593Smuzhiyun 			"%s detection function provided no name for 0x%x\n",
2298*4882a593Smuzhiyun 			driver->driver.name, addr);
2299*4882a593Smuzhiyun 	} else {
2300*4882a593Smuzhiyun 		struct i2c_client *client;
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun 		/* Detection succeeded, instantiate the device */
2303*4882a593Smuzhiyun 		if (adapter->class & I2C_CLASS_DEPRECATED)
2304*4882a593Smuzhiyun 			dev_warn(&adapter->dev,
2305*4882a593Smuzhiyun 				"This adapter will soon drop class based instantiation of devices. "
2306*4882a593Smuzhiyun 				"Please make sure client 0x%02x gets instantiated by other means. "
2307*4882a593Smuzhiyun 				"Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
2308*4882a593Smuzhiyun 				info.addr);
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun 		dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2311*4882a593Smuzhiyun 			info.type, info.addr);
2312*4882a593Smuzhiyun 		client = i2c_new_client_device(adapter, &info);
2313*4882a593Smuzhiyun 		if (!IS_ERR(client))
2314*4882a593Smuzhiyun 			list_add_tail(&client->detected, &driver->clients);
2315*4882a593Smuzhiyun 		else
2316*4882a593Smuzhiyun 			dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2317*4882a593Smuzhiyun 				info.type, info.addr);
2318*4882a593Smuzhiyun 	}
2319*4882a593Smuzhiyun 	return 0;
2320*4882a593Smuzhiyun }
2321*4882a593Smuzhiyun 
i2c_detect(struct i2c_adapter * adapter,struct i2c_driver * driver)2322*4882a593Smuzhiyun static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2323*4882a593Smuzhiyun {
2324*4882a593Smuzhiyun 	const unsigned short *address_list;
2325*4882a593Smuzhiyun 	struct i2c_client *temp_client;
2326*4882a593Smuzhiyun 	int i, err = 0;
2327*4882a593Smuzhiyun 
2328*4882a593Smuzhiyun 	address_list = driver->address_list;
2329*4882a593Smuzhiyun 	if (!driver->detect || !address_list)
2330*4882a593Smuzhiyun 		return 0;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 	/* Warn that the adapter lost class based instantiation */
2333*4882a593Smuzhiyun 	if (adapter->class == I2C_CLASS_DEPRECATED) {
2334*4882a593Smuzhiyun 		dev_dbg(&adapter->dev,
2335*4882a593Smuzhiyun 			"This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2336*4882a593Smuzhiyun 			"If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
2337*4882a593Smuzhiyun 			driver->driver.name);
2338*4882a593Smuzhiyun 		return 0;
2339*4882a593Smuzhiyun 	}
2340*4882a593Smuzhiyun 
2341*4882a593Smuzhiyun 	/* Stop here if the classes do not match */
2342*4882a593Smuzhiyun 	if (!(adapter->class & driver->class))
2343*4882a593Smuzhiyun 		return 0;
2344*4882a593Smuzhiyun 
2345*4882a593Smuzhiyun 	/* Set up a temporary client to help detect callback */
2346*4882a593Smuzhiyun 	temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2347*4882a593Smuzhiyun 	if (!temp_client)
2348*4882a593Smuzhiyun 		return -ENOMEM;
2349*4882a593Smuzhiyun 	temp_client->adapter = adapter;
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
2352*4882a593Smuzhiyun 		dev_dbg(&adapter->dev,
2353*4882a593Smuzhiyun 			"found normal entry for adapter %d, addr 0x%02x\n",
2354*4882a593Smuzhiyun 			i2c_adapter_id(adapter), address_list[i]);
2355*4882a593Smuzhiyun 		temp_client->addr = address_list[i];
2356*4882a593Smuzhiyun 		err = i2c_detect_address(temp_client, driver);
2357*4882a593Smuzhiyun 		if (unlikely(err))
2358*4882a593Smuzhiyun 			break;
2359*4882a593Smuzhiyun 	}
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 	kfree(temp_client);
2362*4882a593Smuzhiyun 	return err;
2363*4882a593Smuzhiyun }
2364*4882a593Smuzhiyun 
i2c_probe_func_quick_read(struct i2c_adapter * adap,unsigned short addr)2365*4882a593Smuzhiyun int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2366*4882a593Smuzhiyun {
2367*4882a593Smuzhiyun 	return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2368*4882a593Smuzhiyun 			      I2C_SMBUS_QUICK, NULL) >= 0;
2369*4882a593Smuzhiyun }
2370*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2371*4882a593Smuzhiyun 
2372*4882a593Smuzhiyun struct i2c_client *
i2c_new_scanned_device(struct i2c_adapter * adap,struct i2c_board_info * info,unsigned short const * addr_list,int (* probe)(struct i2c_adapter * adap,unsigned short addr))2373*4882a593Smuzhiyun i2c_new_scanned_device(struct i2c_adapter *adap,
2374*4882a593Smuzhiyun 		       struct i2c_board_info *info,
2375*4882a593Smuzhiyun 		       unsigned short const *addr_list,
2376*4882a593Smuzhiyun 		       int (*probe)(struct i2c_adapter *adap, unsigned short addr))
2377*4882a593Smuzhiyun {
2378*4882a593Smuzhiyun 	int i;
2379*4882a593Smuzhiyun 
2380*4882a593Smuzhiyun 	if (!probe)
2381*4882a593Smuzhiyun 		probe = i2c_default_probe;
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 	for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2384*4882a593Smuzhiyun 		/* Check address validity */
2385*4882a593Smuzhiyun 		if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
2386*4882a593Smuzhiyun 			dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2387*4882a593Smuzhiyun 				 addr_list[i]);
2388*4882a593Smuzhiyun 			continue;
2389*4882a593Smuzhiyun 		}
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 		/* Check address availability (7 bit, no need to encode flags) */
2392*4882a593Smuzhiyun 		if (i2c_check_addr_busy(adap, addr_list[i])) {
2393*4882a593Smuzhiyun 			dev_dbg(&adap->dev,
2394*4882a593Smuzhiyun 				"Address 0x%02x already in use, not probing\n",
2395*4882a593Smuzhiyun 				addr_list[i]);
2396*4882a593Smuzhiyun 			continue;
2397*4882a593Smuzhiyun 		}
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun 		/* Test address responsiveness */
2400*4882a593Smuzhiyun 		if (probe(adap, addr_list[i]))
2401*4882a593Smuzhiyun 			break;
2402*4882a593Smuzhiyun 	}
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	if (addr_list[i] == I2C_CLIENT_END) {
2405*4882a593Smuzhiyun 		dev_dbg(&adap->dev, "Probing failed, no device found\n");
2406*4882a593Smuzhiyun 		return ERR_PTR(-ENODEV);
2407*4882a593Smuzhiyun 	}
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun 	info->addr = addr_list[i];
2410*4882a593Smuzhiyun 	return i2c_new_client_device(adap, info);
2411*4882a593Smuzhiyun }
2412*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
2413*4882a593Smuzhiyun 
i2c_get_adapter(int nr)2414*4882a593Smuzhiyun struct i2c_adapter *i2c_get_adapter(int nr)
2415*4882a593Smuzhiyun {
2416*4882a593Smuzhiyun 	struct i2c_adapter *adapter;
2417*4882a593Smuzhiyun 
2418*4882a593Smuzhiyun 	mutex_lock(&core_lock);
2419*4882a593Smuzhiyun 	adapter = idr_find(&i2c_adapter_idr, nr);
2420*4882a593Smuzhiyun 	if (!adapter)
2421*4882a593Smuzhiyun 		goto exit;
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	if (try_module_get(adapter->owner))
2424*4882a593Smuzhiyun 		get_device(&adapter->dev);
2425*4882a593Smuzhiyun 	else
2426*4882a593Smuzhiyun 		adapter = NULL;
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun  exit:
2429*4882a593Smuzhiyun 	mutex_unlock(&core_lock);
2430*4882a593Smuzhiyun 	return adapter;
2431*4882a593Smuzhiyun }
2432*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_get_adapter);
2433*4882a593Smuzhiyun 
i2c_put_adapter(struct i2c_adapter * adap)2434*4882a593Smuzhiyun void i2c_put_adapter(struct i2c_adapter *adap)
2435*4882a593Smuzhiyun {
2436*4882a593Smuzhiyun 	if (!adap)
2437*4882a593Smuzhiyun 		return;
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun 	module_put(adap->owner);
2440*4882a593Smuzhiyun 	/* Should be last, otherwise we risk use-after-free with 'adap' */
2441*4882a593Smuzhiyun 	put_device(&adap->dev);
2442*4882a593Smuzhiyun }
2443*4882a593Smuzhiyun EXPORT_SYMBOL(i2c_put_adapter);
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun /**
2446*4882a593Smuzhiyun  * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2447*4882a593Smuzhiyun  * @msg: the message to be checked
2448*4882a593Smuzhiyun  * @threshold: the minimum number of bytes for which using DMA makes sense.
2449*4882a593Smuzhiyun  *	       Should at least be 1.
2450*4882a593Smuzhiyun  *
2451*4882a593Smuzhiyun  * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2452*4882a593Smuzhiyun  *	   Or a valid pointer to be used with DMA. After use, release it by
2453*4882a593Smuzhiyun  *	   calling i2c_put_dma_safe_msg_buf().
2454*4882a593Smuzhiyun  *
2455*4882a593Smuzhiyun  * This function must only be called from process context!
2456*4882a593Smuzhiyun  */
i2c_get_dma_safe_msg_buf(struct i2c_msg * msg,unsigned int threshold)2457*4882a593Smuzhiyun u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2458*4882a593Smuzhiyun {
2459*4882a593Smuzhiyun 	/* also skip 0-length msgs for bogus thresholds of 0 */
2460*4882a593Smuzhiyun 	if (!threshold)
2461*4882a593Smuzhiyun 		pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
2462*4882a593Smuzhiyun 			 msg->addr);
2463*4882a593Smuzhiyun 	if (msg->len < threshold || msg->len == 0)
2464*4882a593Smuzhiyun 		return NULL;
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun 	if (msg->flags & I2C_M_DMA_SAFE)
2467*4882a593Smuzhiyun 		return msg->buf;
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun 	pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2470*4882a593Smuzhiyun 		 msg->addr, msg->len);
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun 	if (msg->flags & I2C_M_RD)
2473*4882a593Smuzhiyun 		return kzalloc(msg->len, GFP_KERNEL);
2474*4882a593Smuzhiyun 	else
2475*4882a593Smuzhiyun 		return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun /**
2480*4882a593Smuzhiyun  * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2481*4882a593Smuzhiyun  * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2482*4882a593Smuzhiyun  * @msg: the message which the buffer corresponds to
2483*4882a593Smuzhiyun  * @xferred: bool saying if the message was transferred
2484*4882a593Smuzhiyun  */
i2c_put_dma_safe_msg_buf(u8 * buf,struct i2c_msg * msg,bool xferred)2485*4882a593Smuzhiyun void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
2486*4882a593Smuzhiyun {
2487*4882a593Smuzhiyun 	if (!buf || buf == msg->buf)
2488*4882a593Smuzhiyun 		return;
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	if (xferred && msg->flags & I2C_M_RD)
2491*4882a593Smuzhiyun 		memcpy(msg->buf, buf, msg->len);
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun 	kfree(buf);
2494*4882a593Smuzhiyun }
2495*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
2496*4882a593Smuzhiyun 
2497*4882a593Smuzhiyun MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2498*4882a593Smuzhiyun MODULE_DESCRIPTION("I2C-Bus main module");
2499*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2500