i2c-core-base.c 66.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
4
5
6
7
8
9
/*
 * Linux I2C core
 *
 * Copyright (C) 1995-99 Simon G. Vogl
 *   With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
 *   Mux support by Rodolfo Giometti <giometti@enneenne.com> and
 *   Michael Lawnick <michael.lawnick.ext@nsn.com>
 *
10
 * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org>
11
 */
Linus Torvalds's avatar
Linus Torvalds committed
12

Wolfram Sang's avatar
Wolfram Sang committed
13
14
#define pr_fmt(fmt) "i2c-core: " fmt

15
#include <dt-bindings/i2c/i2c.h>
Wolfram Sang's avatar
Wolfram Sang committed
16
17
18
#include <linux/acpi.h>
#include <linux/clk/clk-conf.h>
#include <linux/completion.h>
19
#include <linux/delay.h>
Wolfram Sang's avatar
Wolfram Sang committed
20
#include <linux/err.h>
Linus Torvalds's avatar
Linus Torvalds committed
21
#include <linux/errno.h>
22
#include <linux/gpio/consumer.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
#include <linux/i2c.h>
24
#include <linux/i2c-smbus.h>
Linus Torvalds's avatar
Linus Torvalds committed
25
#include <linux/idr.h>
Wolfram Sang's avatar
Wolfram Sang committed
26
27
28
29
30
#include <linux/init.h>
#include <linux/irqflags.h>
#include <linux/jump_label.h>
#include <linux/kernel.h>
#include <linux/module.h>
31
#include <linux/mutex.h>
32
#include <linux/of_device.h>
Wolfram Sang's avatar
Wolfram Sang committed
33
#include <linux/of.h>
34
#include <linux/of_irq.h>
35
#include <linux/pinctrl/consumer.h>
36
#include <linux/pm_domain.h>
Wolfram Sang's avatar
Wolfram Sang committed
37
#include <linux/pm_runtime.h>
38
#include <linux/pm_wakeirq.h>
39
#include <linux/property.h>
Wolfram Sang's avatar
Wolfram Sang committed
40
41
#include <linux/rwsem.h>
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
42

43
44
#include "i2c-core.h"

45
46
#define CREATE_TRACE_POINTS
#include <trace/events/i2c.h>
Linus Torvalds's avatar
Linus Torvalds committed
47

48
49
50
#define I2C_ADDR_OFFSET_TEN_BIT	0xa000
#define I2C_ADDR_OFFSET_SLAVE	0x1000

51
52
53
#define I2C_ADDR_7BITS_MAX	0x77
#define I2C_ADDR_7BITS_COUNT	(I2C_ADDR_7BITS_MAX + 1)

54
55
#define I2C_ADDR_DEVICE_ID	0x7c

56
57
/*
 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
58
 * deletion of detected devices are serialized
59
 */
Jean Delvare's avatar
Jean Delvare committed
60
static DEFINE_MUTEX(core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
61
62
static DEFINE_IDR(i2c_adapter_idr);

63
static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
64

65
static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
66
static bool is_registered;
67

68
int i2c_transfer_trace_reg(void)
69
{
70
	static_branch_inc(&i2c_trace_msg_key);
71
	return 0;
72
73
74
75
}

void i2c_transfer_trace_unreg(void)
{
76
	static_branch_dec(&i2c_trace_msg_key);
77
78
}

79
const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
80
81
						const struct i2c_client *client)
{
82
83
84
	if (!(id && client))
		return NULL;

85
86
87
88
89
90
91
	while (id->name[0]) {
		if (strcmp(client->name, id->name) == 0)
			return id;
		id++;
	}
	return NULL;
}
92
EXPORT_SYMBOL_GPL(i2c_match_id);
93

Linus Torvalds's avatar
Linus Torvalds committed
94
95
static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
96
97
98
	struct i2c_client	*client = i2c_verify_client(dev);
	struct i2c_driver	*driver;

David Brownell's avatar
David Brownell committed
99

100
	/* Attempt an OF style match */
101
	if (i2c_of_match_device(drv->of_match_table, client))
102
103
		return 1;

104
105
106
107
	/* Then ACPI style match */
	if (acpi_driver_match_device(dev, drv))
		return 1;

108
	driver = to_i2c_driver(drv);
109
110
111
112

	/* Finally an I2C match */
	if (i2c_match_id(driver->id_table, client))
		return 1;
113

114
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
115
116
}

117
static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell's avatar
David Brownell committed
118
{
119
	struct i2c_client *client = to_i2c_client(dev);
120
121
	int rc;

122
123
124
125
	rc = of_device_uevent_modalias(dev, env);
	if (rc != -ENODEV)
		return rc;

126
127
128
	rc = acpi_device_uevent_modalias(dev, env);
	if (rc != -ENODEV)
		return rc;
David Brownell's avatar
David Brownell committed
129

130
	return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell's avatar
David Brownell committed
131
132
}

133
134
135
/* i2c bus recovery routines */
static int get_scl_gpio_value(struct i2c_adapter *adap)
{
136
	return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
137
138
139
140
}

static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
{
141
	gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
142
143
144
145
}

static int get_sda_gpio_value(struct i2c_adapter *adap)
{
146
	return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
147
148
}

149
static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
150
{
151
	gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
152
153
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
static int i2c_generic_bus_free(struct i2c_adapter *adap)
{
	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
	int ret = -EOPNOTSUPP;

	if (bri->get_bus_free)
		ret = bri->get_bus_free(adap);
	else if (bri->get_sda)
		ret = bri->get_sda(adap);

	if (ret < 0)
		return ret;

	return ret ? 0 : -EBUSY;
}

170
171
172
173
174
175
176
177
/*
 * We are generating clock pulses. ndelay() determines durating of clk pulses.
 * We will generate clock with rate 100 KHz and so duration of both clock levels
 * is: delay in ns = (10^6 / 100) / 2
 */
#define RECOVERY_NDELAY		5000
#define RECOVERY_CLK_CNT	9

178
int i2c_generic_scl_recovery(struct i2c_adapter *adap)
179
180
{
	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
181
	int i = 0, scl = 1, ret = 0;
182
183

	if (bri->prepare_recovery)
184
		bri->prepare_recovery(adap);
185
186
	if (bri->pinctrl)
		pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
187

188
189
190
191
	/*
	 * If we can set SDA, we will always create a STOP to ensure additional
	 * pulses will do no harm. This is achieved by letting SDA follow SCL
	 * half a cycle later. Check the 'incomplete_write_byte' fault injector
192
193
	 * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
	 * here for simplicity.
194
	 */
195
	bri->set_scl(adap, scl);
196
	ndelay(RECOVERY_NDELAY);
197
	if (bri->set_sda)
198
199
		bri->set_sda(adap, scl);
	ndelay(RECOVERY_NDELAY / 2);
200

201
202
203
204
	/*
	 * By this time SCL is high, as we need to give 9 falling-rising edges
	 */
	while (i++ < RECOVERY_CLK_CNT * 2) {
205
		if (scl) {
206
207
208
209
210
211
212
213
214
			/* SCL shouldn't be low here */
			if (!bri->get_scl(adap)) {
				dev_err(&adap->dev,
					"SCL is stuck low, exit recovery\n");
				ret = -EBUSY;
				break;
			}
		}

215
216
		scl = !scl;
		bri->set_scl(adap, scl);
217
		/* Creating STOP again, see above */
218
219
220
221
222
223
224
		if (scl)  {
			/* Honour minimum tsu:sto */
			ndelay(RECOVERY_NDELAY);
		} else {
			/* Honour minimum tf and thd:dat */
			ndelay(RECOVERY_NDELAY / 2);
		}
225
		if (bri->set_sda)
226
			bri->set_sda(adap, scl);
227
		ndelay(RECOVERY_NDELAY / 2);
228

229
		if (scl) {
230
			ret = i2c_generic_bus_free(adap);
231
232
233
			if (ret == 0)
				break;
		}
234
235
	}

236
237
238
239
	/* If we can't check bus status, assume recovery worked */
	if (ret == -EOPNOTSUPP)
		ret = 0;

240
	if (bri->unprepare_recovery)
241
		bri->unprepare_recovery(adap);
242
243
	if (bri->pinctrl)
		pinctrl_select_state(bri->pinctrl, bri->pins_default);
244
245
246

	return ret;
}
247
EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
248
249
250
251
252
253
254
255
256

int i2c_recover_bus(struct i2c_adapter *adap)
{
	if (!adap->bus_recovery_info)
		return -EOPNOTSUPP;

	dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
	return adap->bus_recovery_info->recover_bus(adap);
}
257
EXPORT_SYMBOL_GPL(i2c_recover_bus);
258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
static void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap)
{
	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
	struct device *dev = &adap->dev;
	struct pinctrl *p = bri->pinctrl;

	/*
	 * we can't change states without pinctrl, so remove the states if
	 * populated
	 */
	if (!p) {
		bri->pins_default = NULL;
		bri->pins_gpio = NULL;
		return;
	}

	if (!bri->pins_default) {
		bri->pins_default = pinctrl_lookup_state(p,
							 PINCTRL_STATE_DEFAULT);
		if (IS_ERR(bri->pins_default)) {
			dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n");
			bri->pins_default = NULL;
		}
	}
	if (!bri->pins_gpio) {
		bri->pins_gpio = pinctrl_lookup_state(p, "gpio");
		if (IS_ERR(bri->pins_gpio))
			bri->pins_gpio = pinctrl_lookup_state(p, "recovery");

		if (IS_ERR(bri->pins_gpio)) {
			dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n");
			bri->pins_gpio = NULL;
		}
	}

	/* for pinctrl state changes, we need all the information */
	if (bri->pins_default && bri->pins_gpio) {
		dev_info(dev, "using pinctrl states for GPIO recovery");
	} else {
		bri->pinctrl = NULL;
		bri->pins_default = NULL;
		bri->pins_gpio = NULL;
	}
}

static int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap)
{
	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
	struct device *dev = &adap->dev;
	struct gpio_desc *gpiod;
	int ret = 0;

	/*
	 * don't touch the recovery information if the driver is not using
	 * generic SCL recovery
	 */
	if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery)
		return 0;

	/*
	 * pins might be taken as GPIO, so we should inform pinctrl about
	 * this and move the state to GPIO
	 */
	if (bri->pinctrl)
		pinctrl_select_state(bri->pinctrl, bri->pins_gpio);

	/*
	 * if there is incomplete or no recovery information, see if generic
	 * GPIO recovery is available
	 */
	if (!bri->scl_gpiod) {
		gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
		if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
			ret  = -EPROBE_DEFER;
			goto cleanup_pinctrl_state;
		}
		if (!IS_ERR(gpiod)) {
			bri->scl_gpiod = gpiod;
			bri->recover_bus = i2c_generic_scl_recovery;
			dev_info(dev, "using generic GPIOs for recovery\n");
		}
	}

	/* SDA GPIOD line is optional, so we care about DEFER only */
	if (!bri->sda_gpiod) {
		/*
		 * We have SCL. Pull SCL low and wait a bit so that SDA glitches
		 * have no effect.
		 */
		gpiod_direction_output(bri->scl_gpiod, 0);
		udelay(10);
		gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);

		/* Wait a bit in case of a SDA glitch, and then release SCL. */
		udelay(10);
		gpiod_direction_output(bri->scl_gpiod, 1);

		if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
			ret = -EPROBE_DEFER;
			goto cleanup_pinctrl_state;
		}
		if (!IS_ERR(gpiod))
			bri->sda_gpiod = gpiod;
	}

cleanup_pinctrl_state:
	/* change the state of the pins back to their default state */
	if (bri->pinctrl)
		pinctrl_select_state(bri->pinctrl, bri->pins_default);

	return ret;
}

static int i2c_gpio_init_recovery(struct i2c_adapter *adap)
{
	i2c_gpio_init_pinctrl_recovery(adap);
	return i2c_gpio_init_generic_recovery(adap);
}

378
static int i2c_init_recovery(struct i2c_adapter *adap)
379
380
381
382
383
{
	struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
	char *err_str;

	if (!bri)
384
		return 0;
385

386
387
	if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER)
		return -EPROBE_DEFER;
388

389
390
391
392
393
	if (!bri->recover_bus) {
		err_str = "no recover_bus() found";
		goto err;
	}

394
	if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
395
396
		bri->get_scl = get_scl_gpio_value;
		bri->set_scl = set_scl_gpio_value;
397
		if (bri->sda_gpiod) {
398
			bri->get_sda = get_sda_gpio_value;
399
400
401
402
			/* FIXME: add proper flag instead of '0' once available */
			if (gpiod_get_direction(bri->sda_gpiod) == 0)
				bri->set_sda = set_sda_gpio_value;
		}
403
	} else if (bri->recover_bus == i2c_generic_scl_recovery) {
404
405
406
407
408
		/* Generic SCL recovery */
		if (!bri->set_scl || !bri->get_scl) {
			err_str = "no {get|set}_scl() found";
			goto err;
		}
409
410
411
412
		if (!bri->set_sda && !bri->get_sda) {
			err_str = "either get_sda() or set_sda() needed";
			goto err;
		}
413
414
	}

415
	return 0;
416
417
418
 err:
	dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
	adap->bus_recovery_info = NULL;
419
420

	return -EINVAL;
421
422
}

423
424
425
426
427
428
429
430
431
432
433
static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
{
	struct i2c_adapter *adap = client->adapter;
	unsigned int irq;

	if (!adap->host_notify_domain)
		return -ENXIO;

	if (client->flags & I2C_CLIENT_TEN)
		return -EINVAL;

434
	irq = irq_create_mapping(adap->host_notify_domain, client->addr);
435
436
437
438

	return irq > 0 ? irq : -ENXIO;
}

439
static int i2c_device_probe(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
440
{
441
442
	struct i2c_client	*client = i2c_verify_client(dev);
	struct i2c_driver	*driver;
443
	int status;
David Brownell's avatar
David Brownell committed
444

445
446
447
	if (!client)
		return 0;

448
449
	client->irq = client->init_irq;

450
	if (!client->irq) {
451
452
		int irq = -ENOENT;

453
454
		if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
			dev_dbg(dev, "Using Host Notify IRQ\n");
455
456
			/* Keep adapter active when Host Notify is required */
			pm_runtime_get_sync(&client->adapter->dev);
457
458
			irq = i2c_smbus_host_notify_to_irq(client);
		} else if (dev->of_node) {
459
460
461
462
			irq = of_irq_get_byname(dev->of_node, "irq");
			if (irq == -EINVAL || irq == -ENODATA)
				irq = of_irq_get(dev->of_node, 0);
		} else if (ACPI_COMPANION(dev)) {
463
			irq = i2c_acpi_get_irq(client);
464
		}
465
466
467
468
		if (irq == -EPROBE_DEFER) {
			status = irq;
			goto put_sync_adapter;
		}
469

470
471
		if (irq < 0)
			irq = 0;
472
473
474
475

		client->irq = irq;
	}

476
477
	driver = to_i2c_driver(dev->driver);

478
	/*
479
480
	 * An I2C ID table is not mandatory, if and only if, a suitable OF
	 * or ACPI ID table is supplied for the probing device.
481
482
	 */
	if (!driver->id_table &&
483
	    !acpi_driver_match_device(dev, dev->driver) &&
484
485
486
487
	    !i2c_of_match_device(dev->driver->of_match_table, client)) {
		status = -ENODEV;
		goto put_sync_adapter;
	}
488

489
	if (client->flags & I2C_CLIENT_WAKE) {
490
		int wakeirq;
491

492
		wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
493
494
495
496
		if (wakeirq == -EPROBE_DEFER) {
			status = wakeirq;
			goto put_sync_adapter;
		}
497
498
499
500
501
502

		device_init_wakeup(&client->dev, true);

		if (wakeirq > 0 && wakeirq != client->irq)
			status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
		else if (client->irq > 0)
503
			status = dev_pm_set_wake_irq(dev, client->irq);
504
505
506
507
		else
			status = 0;

		if (status)
508
			dev_warn(&client->dev, "failed to set up wakeup irq\n");
509
510
	}

David Brownell's avatar
David Brownell committed
511
	dev_dbg(dev, "probe\n");
512

513
514
	status = of_clk_set_defaults(dev->of_node, false);
	if (status < 0)
515
		goto err_clear_wakeup_irq;
516

517
	status = dev_pm_domain_attach(&client->dev, true);
518
	if (status)
519
520
		goto err_clear_wakeup_irq;

521
522
523
524
525
526
527
528
529
530
531
532
	/*
	 * When there are no more users of probe(),
	 * rename probe_new to probe.
	 */
	if (driver->probe_new)
		status = driver->probe_new(client);
	else if (driver->probe)
		status = driver->probe(client,
				       i2c_match_id(driver->id_table, client));
	else
		status = -EINVAL;

533
534
	if (status)
		goto err_detach_pm_domain;
535

536
537
538
539
540
541
542
	return 0;

err_detach_pm_domain:
	dev_pm_domain_detach(&client->dev, true);
err_clear_wakeup_irq:
	dev_pm_clear_wake_irq(&client->dev);
	device_init_wakeup(&client->dev, false);
543
544
545
546
put_sync_adapter:
	if (client->flags & I2C_CLIENT_HOST_NOTIFY)
		pm_runtime_put_sync(&client->adapter->dev);

547
	return status;
548
}
Linus Torvalds's avatar
Linus Torvalds committed
549

550
551
static int i2c_device_remove(struct device *dev)
{
552
	struct i2c_client	*client = i2c_verify_client(dev);
David Brownell's avatar
David Brownell committed
553
	struct i2c_driver	*driver;
554
	int status = 0;
David Brownell's avatar
David Brownell committed
555

556
	if (!client || !dev->driver)
David Brownell's avatar
David Brownell committed
557
558
559
560
561
562
563
		return 0;

	driver = to_i2c_driver(dev->driver);
	if (driver->remove) {
		dev_dbg(dev, "remove\n");
		status = driver->remove(client);
	}
564

565
	dev_pm_domain_detach(&client->dev, true);
566
567
568
569

	dev_pm_clear_wake_irq(&client->dev);
	device_init_wakeup(&client->dev, false);

570
	client->irq = 0;
571
572
	if (client->flags & I2C_CLIENT_HOST_NOTIFY)
		pm_runtime_put(&client->adapter->dev);
573

David Brownell's avatar
David Brownell committed
574
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
575
576
}

577
static void i2c_device_shutdown(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
578
{
579
	struct i2c_client *client = i2c_verify_client(dev);
580
581
	struct i2c_driver *driver;

582
	if (!client || !dev->driver)
583
584
585
		return;
	driver = to_i2c_driver(dev->driver);
	if (driver->shutdown)
586
		driver->shutdown(client);
Linus Torvalds's avatar
Linus Torvalds committed
587
588
}

589
590
591
592
593
static void i2c_client_dev_release(struct device *dev)
{
	kfree(to_i2c_client(dev));
}

Zhenwen Xu's avatar
Zhenwen Xu committed
594
static ssize_t
595
name_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell's avatar
David Brownell committed
596
{
597
598
	return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
		       to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell's avatar
David Brownell committed
599
}
600
static DEVICE_ATTR_RO(name);
David Brownell's avatar
David Brownell committed
601

Zhenwen Xu's avatar
Zhenwen Xu committed
602
static ssize_t
603
modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell's avatar
David Brownell committed
604
605
{
	struct i2c_client *client = to_i2c_client(dev);
606
607
	int len;

608
609
610
611
	len = of_device_modalias(dev, buf, PAGE_SIZE);
	if (len != -ENODEV)
		return len;

612
613
614
615
	len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
	if (len != -ENODEV)
		return len;

616
	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell's avatar
David Brownell committed
617
}
618
static DEVICE_ATTR_RO(modalias);
619
620
621

static struct attribute *i2c_dev_attrs[] = {
	&dev_attr_name.attr,
David Brownell's avatar
David Brownell committed
622
	/* modalias helps coldplug:  modprobe $(cat .../modalias) */
623
624
625
	&dev_attr_modalias.attr,
	NULL
};
626
ATTRIBUTE_GROUPS(i2c_dev);
627

628
struct bus_type i2c_bus_type = {
629
630
631
632
633
	.name		= "i2c",
	.match		= i2c_device_match,
	.probe		= i2c_device_probe,
	.remove		= i2c_device_remove,
	.shutdown	= i2c_device_shutdown,
634
};
635
EXPORT_SYMBOL_GPL(i2c_bus_type);
636

637
struct device_type i2c_client_type = {
638
	.groups		= i2c_dev_groups,
639
640
641
	.uevent		= i2c_device_uevent,
	.release	= i2c_client_dev_release,
};
642
EXPORT_SYMBOL_GPL(i2c_client_type);
643

644
645
646
647
648
649
650
651
652
653
654
655

/**
 * i2c_verify_client - return parameter as i2c_client, or NULL
 * @dev: device, probably from some driver model iterator
 *
 * When traversing the driver model tree, perhaps using driver model
 * iterators like @device_for_each_child(), you can't assume very much
 * about the nodes you find.  Use this function to avoid oopses caused
 * by wrongly treating some non-I2C device as an i2c_client.
 */
struct i2c_client *i2c_verify_client(struct device *dev)
{
656
	return (dev->type == &i2c_client_type)
657
658
659
660
661
662
			? to_i2c_client(dev)
			: NULL;
}
EXPORT_SYMBOL(i2c_verify_client);


663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* Return a unique address which takes the flags of the client into account */
static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
{
	unsigned short addr = client->addr;

	/* For some client flags, add an arbitrary offset to avoid collisions */
	if (client->flags & I2C_CLIENT_TEN)
		addr |= I2C_ADDR_OFFSET_TEN_BIT;

	if (client->flags & I2C_CLIENT_SLAVE)
		addr |= I2C_ADDR_OFFSET_SLAVE;

	return addr;
}

678
/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi's avatar
Lucas De Marchi committed
679
 * are purposely not enforced, except for the general call address. */
680
static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
681
{
682
	if (flags & I2C_CLIENT_TEN) {
683
		/* 10-bit address, all values are valid */
684
		if (addr > 0x3ff)
685
686
687
			return -EINVAL;
	} else {
		/* 7-bit address, reject the general call address */
688
		if (addr == 0x00 || addr > 0x7f)
689
690
691
692
693
			return -EINVAL;
	}
	return 0;
}

694
695
696
697
/* And this is a strict address validity check, used when probing. If a
 * device uses a reserved address, then it shouldn't be probed. 7-bit
 * addressing is assumed, 10-bit address devices are rare and should be
 * explicitly enumerated. */
698
int i2c_check_7bit_addr_validity_strict(unsigned short addr)
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
{
	/*
	 * Reserved addresses per I2C specification:
	 *  0x00       General call address / START byte
	 *  0x01       CBUS address
	 *  0x02       Reserved for different bus format
	 *  0x03       Reserved for future purposes
	 *  0x04-0x07  Hs-mode master code
	 *  0x78-0x7b  10-bit slave addressing
	 *  0x7c-0x7f  Reserved for future purposes
	 */
	if (addr < 0x08 || addr > 0x77)
		return -EINVAL;
	return 0;
}

715
716
717
718
719
static int __i2c_check_addr_busy(struct device *dev, void *addrp)
{
	struct i2c_client	*client = i2c_verify_client(dev);
	int			addr = *(int *)addrp;

720
	if (client && i2c_encode_flags_to_addr(client) == addr)
721
722
723
724
		return -EBUSY;
	return 0;
}

725
726
727
/* walk up mux tree */
static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
{
728
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
729
730
731
732
733
	int result;

	result = device_for_each_child(&adapter->dev, &addr,
					__i2c_check_addr_busy);

734
735
	if (!result && parent)
		result = i2c_check_mux_parents(parent, addr);
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753

	return result;
}

/* recurse down mux tree */
static int i2c_check_mux_children(struct device *dev, void *addrp)
{
	int result;

	if (dev->type == &i2c_adapter_type)
		result = device_for_each_child(dev, addrp,
						i2c_check_mux_children);
	else
		result = __i2c_check_addr_busy(dev, addrp);

	return result;
}

754
755
static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
{
756
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
757
758
	int result = 0;

759
760
	if (parent)
		result = i2c_check_mux_parents(parent, addr);
761
762
763
764
765
766

	if (!result)
		result = device_for_each_child(&adapter->dev, &addr,
						i2c_check_mux_children);

	return result;
767
768
}

769
/**
770
 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
771
 * @adapter: Target I2C bus segment
772
773
 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
 *	locks only this branch in the adapter tree
774
 */
775
776
static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
				 unsigned int flags)
777
{
778
	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
779
780
781
}

/**
782
 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
783
 * @adapter: Target I2C bus segment
784
785
 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
 *	trylocks only this branch in the adapter tree
786
 */
787
788
static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
				   unsigned int flags)
789
{
790
	return rt_mutex_trylock(&adapter->bus_lock);
791
792
793
}

/**
794
 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
795
 * @adapter: Target I2C bus segment
796
797
 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
 *	unlocks only this branch in the adapter tree
798
 */
799
800
static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
				   unsigned int flags)
801
{
802
	rt_mutex_unlock(&adapter->bus_lock);
803
804
}

805
static void i2c_dev_set_name(struct i2c_adapter *adap,
806
807
			     struct i2c_client *client,
			     struct i2c_board_info const *info)
808
809
810
{
	struct acpi_device *adev = ACPI_COMPANION(&client->dev);

811
812
813
814
815
	if (info && info->dev_name) {
		dev_set_name(&client->dev, "i2c-%s", info->dev_name);
		return;
	}

816
817
818
819
820
821
	if (adev) {
		dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
		return;
	}

	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
822
		     i2c_encode_flags_to_addr(client));
823
824
}

825
826
int i2c_dev_irq_from_resources(const struct resource *resources,
			       unsigned int num_resources)
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
{
	struct irq_data *irqd;
	int i;

	for (i = 0; i < num_resources; i++) {
		const struct resource *r = &resources[i];

		if (resource_type(r) != IORESOURCE_IRQ)
			continue;

		if (r->flags & IORESOURCE_BITS) {
			irqd = irq_get_irq_data(r->start);
			if (!irqd)
				break;

			irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
		}

		return r->start;
	}

	return 0;
}

851
/**
852
 * i2c_new_client_device - instantiate an i2c device
853
854
 * @adap: the adapter managing the device
 * @info: describes one I2C device; bus_num is ignored
David Brownell's avatar
David Brownell committed
855
 * Context: can sleep
856
 *
857
858
859
860
861
862
 * Create an i2c device. Binding is handled through driver model
 * probe()/remove() methods.  A driver may be bound to this device when we
 * return from this function, or any later moment (e.g. maybe hotplugging will
 * load the driver module).  This call is not appropriate for use by mainboard
 * initialization logic, which usually runs during an arch_initcall() long
 * before any i2c_adapter could exist.
863
864
 *
 * This returns the new i2c client, which may be saved for later use with
865
 * i2c_unregister_device(); or an ERR_PTR to describe the error.
866
 */
867
struct i2c_client *
868
i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
869
870
871
872
873
874
{
	struct i2c_client	*client;
	int			status;

	client = kzalloc(sizeof *client, GFP_KERNEL);
	if (!client)
875
		return ERR_PTR(-ENOMEM);
876
877
878
879

	client->adapter = adap;

	client->dev.platform_data = info->platform_data;
880
	client->flags = info->flags;
881
	client->addr = info->addr;
882

883
884
885
	client->init_irq = info->irq;
	if (!client->init_irq)
		client->init_irq = i2c_dev_irq_from_resources(info->resources,
886
							 info->num_resources);
887
888
889

	strlcpy(client->name, info->type, sizeof(client->name));

890
	status = i2c_check_addr_validity(client->addr, client->flags);
891
892
893
894
895
896
	if (status) {
		dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
			client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
		goto out_err_silent;
	}

897
	/* Check for address business */
898
	status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
899
900
901
902
903
	if (status)
		goto out_err;

	client->dev.parent = &client->adapter->dev;
	client->dev.bus = &i2c_bus_type;
904
	client->dev.type = &i2c_client_type;
905
	client->dev.of_node = of_node_get(info->of_node);
906
	client->dev.fwnode = info->fwnode;
907

908
	i2c_dev_set_name(adap, client, info);
909
910
911
912
913
914
915

	if (info->properties) {
		status = device_add_properties(&client->dev, info->properties);
		if (status) {
			dev_err(&adap->dev,
				"Failed to add properties to client %s: %d\n",
				client->name, status);
916
			goto out_err_put_of_node;
917
918
919
		}
	}

920
921
	status = device_register(&client->dev);
	if (status)
922
		goto out_free_props;
923
924
925
926

	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
		client->name, dev_name(&client->dev));

927
	return client;
928

929
930
931
out_free_props:
	if (info->properties)
		device_remove_properties(&client->dev);
932
933
out_err_put_of_node:
	of_node_put(info->of_node);
934
out_err:
935
936
937
	dev_err(&adap->dev,
		"Failed to register i2c client %s at 0x%02x (%d)\n",
		client->name, client->addr, status);
938
out_err_silent:
939
	kfree(client);
940
941
942
943
	return ERR_PTR(status);
}
EXPORT_SYMBOL_GPL(i2c_new_client_device);

944
/**
945
946
 * i2c_unregister_device - reverse effect of i2c_new_*_device()
 * @client: value returned from i2c_new_*_device()
David Brownell's avatar
David Brownell committed
947
 * Context: can sleep
948
949
 */
void i2c_unregister_device(struct i2c_client *client)
David Brownell's avatar
David Brownell committed
950
{
951
	if (IS_ERR_OR_NULL(client))
952
		return;
953
954

	if (client->dev.of_node) {
955
		of_node_clear_flag(client->dev.of_node, OF_POPULATED);
956
957
958
		of_node_put(client->dev.of_node);
	}

959
960
	if (ACPI_COMPANION(&client->dev))
		acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownell's avatar
David Brownell committed
961
962
	device_unregister(&client->dev);
}
963
EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownell's avatar
David Brownell committed
964
965


966
967
968
969
970
static const struct i2c_device_id dummy_id[] = {
	{ "dummy", 0 },
	{ },
};

971
972
973
974
975
976
977
static int dummy_probe(struct i2c_client *client,
		       const struct i2c_device_id *id)
{
	return 0;
}

static int dummy_remove(struct i2c_client *client)
978
979
980
981
982
983
{
	return 0;
}

static struct i2c_driver dummy_driver = {
	.driver.name	= "dummy",
984
985
	.probe		= dummy_probe,
	.remove		= dummy_remove,
986
	.id_table	= dummy_id,
987
988
989
};

/**
990
 * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
991
992
993
994
995
996
997
998
999
1000
 * @adapter: the adapter managing the device
 * @address: seven bit address to be used
 * Context: can sleep
 *
 * This returns an I2C client bound to the "dummy" driver, intended for use
 * with devices that consume multiple addresses.  Examples of such chips
 * include various EEPROMS (like 24c04 and 24c08 models).
 *
 * These dummy devices have two main uses.  First, most I2C and SMBus calls
 * except i2c_transfer() need a client handle; the dummy will be that handle.