i2c-davinci.c 25.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * TI DAVINCI I2C adapter driver.
 *
 * Copyright (C) 2006 Texas Instruments.
 * Copyright (C) 2007 MontaVista Software Inc.
 *
 * Updated by Vinod & Sudhakar Feb 2005
 *
 * ----------------------------------------------------------------------------
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * ----------------------------------------------------------------------------
 *
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/clk.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/io.h>
34
#include <linux/slab.h>
35
#include <linux/cpufreq.h>
36
#include <linux/gpio/consumer.h>
Heiko Schocher's avatar
Heiko Schocher committed
37
#include <linux/of_device.h>
38
#include <linux/platform_data/i2c-davinci.h>
39
#include <linux/pm_runtime.h>
40
41
42
43

/* ----- global defines ----------------------------------------------- */

#define DAVINCI_I2C_TIMEOUT	(1*HZ)
44
#define DAVINCI_I2C_MAX_TRIES	2
45
46
#define DAVINCI_I2C_OWN_ADDRESS	0x08
#define I2C_DAVINCI_INTR_ALL    (DAVINCI_I2C_IMR_SCD | \
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
				 DAVINCI_I2C_IMR_ARDY | \
				 DAVINCI_I2C_IMR_NACK | \
				 DAVINCI_I2C_IMR_AL)

#define DAVINCI_I2C_OAR_REG	0x00
#define DAVINCI_I2C_IMR_REG	0x04
#define DAVINCI_I2C_STR_REG	0x08
#define DAVINCI_I2C_CLKL_REG	0x0c
#define DAVINCI_I2C_CLKH_REG	0x10
#define DAVINCI_I2C_CNT_REG	0x14
#define DAVINCI_I2C_DRR_REG	0x18
#define DAVINCI_I2C_SAR_REG	0x1c
#define DAVINCI_I2C_DXR_REG	0x20
#define DAVINCI_I2C_MDR_REG	0x24
#define DAVINCI_I2C_IVR_REG	0x28
#define DAVINCI_I2C_EMDR_REG	0x2c
#define DAVINCI_I2C_PSC_REG	0x30
64
65
66
67
68
69
#define DAVINCI_I2C_FUNC_REG	0x48
#define DAVINCI_I2C_DIR_REG	0x4c
#define DAVINCI_I2C_DIN_REG	0x50
#define DAVINCI_I2C_DOUT_REG	0x54
#define DAVINCI_I2C_DSET_REG	0x58
#define DAVINCI_I2C_DCLR_REG	0x5c
70
71
72
73
74
75
76
77
78

#define DAVINCI_I2C_IVR_AAS	0x07
#define DAVINCI_I2C_IVR_SCD	0x06
#define DAVINCI_I2C_IVR_XRDY	0x05
#define DAVINCI_I2C_IVR_RDR	0x04
#define DAVINCI_I2C_IVR_ARDY	0x03
#define DAVINCI_I2C_IVR_NACK	0x02
#define DAVINCI_I2C_IVR_AL	0x01

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define DAVINCI_I2C_STR_BB	BIT(12)
#define DAVINCI_I2C_STR_RSFULL	BIT(11)
#define DAVINCI_I2C_STR_SCD	BIT(5)
#define DAVINCI_I2C_STR_ARDY	BIT(2)
#define DAVINCI_I2C_STR_NACK	BIT(1)
#define DAVINCI_I2C_STR_AL	BIT(0)

#define DAVINCI_I2C_MDR_NACK	BIT(15)
#define DAVINCI_I2C_MDR_STT	BIT(13)
#define DAVINCI_I2C_MDR_STP	BIT(11)
#define DAVINCI_I2C_MDR_MST	BIT(10)
#define DAVINCI_I2C_MDR_TRX	BIT(9)
#define DAVINCI_I2C_MDR_XA	BIT(8)
#define DAVINCI_I2C_MDR_RM	BIT(7)
#define DAVINCI_I2C_MDR_IRS	BIT(5)

#define DAVINCI_I2C_IMR_AAS	BIT(6)
#define DAVINCI_I2C_IMR_SCD	BIT(5)
#define DAVINCI_I2C_IMR_XRDY	BIT(4)
#define DAVINCI_I2C_IMR_RRDY	BIT(3)
#define DAVINCI_I2C_IMR_ARDY	BIT(2)
#define DAVINCI_I2C_IMR_NACK	BIT(1)
#define DAVINCI_I2C_IMR_AL	BIT(0)
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* set SDA and SCL as GPIO */
#define DAVINCI_I2C_FUNC_PFUNC0	BIT(0)

/* set SCL as output when used as GPIO*/
#define DAVINCI_I2C_DIR_PDIR0	BIT(0)
/* set SDA as output when used as GPIO*/
#define DAVINCI_I2C_DIR_PDIR1	BIT(1)

/* read SCL GPIO level */
#define DAVINCI_I2C_DIN_PDIN0 BIT(0)
/* read SDA GPIO level */
#define DAVINCI_I2C_DIN_PDIN1 BIT(1)

/*set the SCL GPIO high */
#define DAVINCI_I2C_DSET_PDSET0	BIT(0)
/*set the SDA GPIO high */
#define DAVINCI_I2C_DSET_PDSET1	BIT(1)

/* set the SCL GPIO low */
#define DAVINCI_I2C_DCLR_PDCLR0	BIT(0)
/* set the SDA GPIO low */
#define DAVINCI_I2C_DCLR_PDCLR1	BIT(1)

126
127
128
/* timeout for pm runtime autosuspend */
#define DAVINCI_I2C_PM_TIMEOUT	1000	/* ms */

129
130
131
132
133
134
135
136
137
struct davinci_i2c_dev {
	struct device           *dev;
	void __iomem		*base;
	struct completion	cmd_complete;
	struct clk              *clk;
	int			cmd_err;
	u8			*buf;
	size_t			buf_len;
	int			irq;
138
	int			stop;
139
	u8			terminate;
140
	struct i2c_adapter	adapter;
141
142
143
#ifdef CONFIG_CPU_FREQ
	struct notifier_block	freq_transition;
#endif
Heiko Schocher's avatar
Heiko Schocher committed
144
	struct davinci_i2c_platform_data *pdata;
145
146
147
148
149
150
151
152
153
154
155
};

/* default platform data to use if not supplied in the platform_device */
static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
	.bus_freq	= 100,
	.bus_delay	= 0,
};

static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
					 int reg, u16 val)
{
156
	writew_relaxed(val, i2c_dev->base + reg);
157
158
159
160
}

static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
{
161
	return readw_relaxed(i2c_dev->base + reg);
162
163
}

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
								int val)
{
	u16 w;

	w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
	if (!val)	/* put I2C into reset */
		w &= ~DAVINCI_I2C_MDR_IRS;
	else		/* take I2C out of reset */
		w |= DAVINCI_I2C_MDR_IRS;

	davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
}

static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
179
{
Heiko Schocher's avatar
Heiko Schocher committed
180
	struct davinci_i2c_platform_data *pdata = dev->pdata;
181
182
	u16 psc;
	u32 clk;
183
	u32 d;
184
185
186
	u32 clkh;
	u32 clkl;
	u32 input_clock = clk_get_rate(dev->clk);
187
	struct device_node *of_node = dev->dev->of_node;
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

	/* NOTE: I2C Clock divider programming info
	 * As per I2C specs the following formulas provide prescaler
	 * and low/high divider values
	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
	 *                       module clk
	 *
	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
	 *
	 * Thus,
	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
	 *
	 * where if PSC == 0, d = 7,
	 *       if PSC == 1, d = 6
	 *       if PSC > 1 , d = 5
203
204
205
	 *
	 * Note:
	 * d is always 6 on Keystone I2C controller
206
207
	 */

208
209
210
211
212
213
214
215
216
	/*
	 * Both Davinci and current Keystone User Guides recommend a value
	 * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
	 * always produce enough margin between SDA and SCL transitions.
	 * Measurements show that the higher the module clock is, the
	 * bigger is the margin, providing more reliable communication.
	 * So we better target for 12MHz.
	 */
	psc = (input_clock / 12000000) - 1;
217
218
219
	if ((input_clock / (psc + 1)) > 12000000)
		psc++;	/* better to run under spec than over */
	d = (psc >= 2) ? 5 : 7 - psc;
220

221
222
223
	if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c"))
		d = 6;

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000));
	/* Avoid driving the bus too fast because of rounding errors above */
	if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000)
		clk++;
	/*
	 * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
	 * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
	 * to LOW ratio as 1 to 2 is more safe.
	 */
	if (pdata->bus_freq > 100)
		clkl = (clk << 1) / 3;
	else
		clkl = (clk >> 1);
	/*
	 * It's not always possible to have 1 to 2 ratio when d=7, so fall back
	 * to minimal possible clkh in this case.
	 */
	if (clk >= clkl + d) {
		clkh = clk - clkl - d;
		clkl -= d;
	} else {
		clkh = 0;
		clkl = clk - (d << 1);
	}
248
249
250
251
252

	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);

253
254
255
256
257
258
259
260
261
262
	dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
}

/*
 * This function configures I2C and brings I2C out of reset.
 * This function is called during I2C init function. This function
 * also gets called if I2C encounters any errors.
 */
static int i2c_davinci_init(struct davinci_i2c_dev *dev)
{
Heiko Schocher's avatar
Heiko Schocher committed
263
	struct davinci_i2c_platform_data *pdata = dev->pdata;
264
265
266
267
268
269
270

	/* put I2C into reset */
	davinci_i2c_reset_ctrl(dev, 0);

	/* compute clock dividers */
	i2c_davinci_calc_clk_dividers(dev);

271
272
273
	/* Respond at reserved "SMBus Host" slave address" (and zero);
	 * we seem to have no option to not respond...
	 */
274
	davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS);
275

276
277
278
279
280
281
	dev_dbg(dev->dev, "PSC  = %d\n",
		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
	dev_dbg(dev->dev, "CLKL = %d\n",
		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
	dev_dbg(dev->dev, "CLKH = %d\n",
		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
282
283
	dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
		pdata->bus_freq, pdata->bus_delay);
284

Heiko Schocher's avatar
Heiko Schocher committed
285

286
	/* Take the I2C module out of reset: */
287
	davinci_i2c_reset_ctrl(dev, 1);
288
289
290
291
292
293
294

	/* Enable interrupts */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);

	return 0;
}

295
/*
296
 * This routine does i2c bus recovery by using i2c_generic_scl_recovery
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
 * which is provided by I2C Bus recovery infrastructure.
 */
static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);

	/* Disable interrupts */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);

	/* put I2C into reset */
	davinci_i2c_reset_ctrl(dev, 0);
}

static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);

	i2c_davinci_init(dev);
}

static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
318
	.recover_bus = i2c_generic_scl_recovery,
319
320
321
322
	.prepare_recovery = davinci_i2c_prepare_recovery,
	.unprepare_recovery = davinci_i2c_unprepare_recovery,
};

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
378
379
380
381
382
383
384
385
386
387
static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);

	if (val)
		davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
				      DAVINCI_I2C_DSET_PDSET0);
	else
		davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
				      DAVINCI_I2C_DCLR_PDCLR0);
}

static int davinci_i2c_get_scl(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
	int val;

	/* read the state of SCL */
	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
	return val & DAVINCI_I2C_DIN_PDIN0;
}

static int davinci_i2c_get_sda(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
	int val;

	/* read the state of SDA */
	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
	return val & DAVINCI_I2C_DIN_PDIN1;
}

static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);

	davinci_i2c_prepare_recovery(adap);

	/* SCL output, SDA input */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);

	/* change to GPIO mode */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
			      DAVINCI_I2C_FUNC_PFUNC0);
}

static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);

	/* change back to I2C mode */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);

	davinci_i2c_unprepare_recovery(adap);
}

static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
	.recover_bus = i2c_generic_scl_recovery,
	.set_scl = davinci_i2c_set_scl,
	.get_scl = davinci_i2c_get_scl,
	.get_sda = davinci_i2c_get_sda,
	.prepare_recovery = davinci_i2c_scl_prepare_recovery,
	.unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
};

388
389
390
/*
 * Waiting for bus not busy
 */
391
static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
392
{
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
	unsigned long timeout = jiffies + dev->adapter.timeout;

	do {
		if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
			return 0;
		schedule_timeout_uninterruptible(1);
	} while (time_before_eq(jiffies, timeout));

	dev_warn(dev->dev, "timeout waiting for bus ready\n");
	i2c_recover_bus(&dev->adapter);

	/*
	 * if bus is still "busy" here, it's most probably a HW problem like
	 * short-circuit
	 */
	if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
		return -EIO;
410
411
412
413
414
415
416
417
418
419
420
421

	return 0;
}

/*
 * Low level master read/write transaction. This function is called
 * from i2c_davinci_xfer.
 */
static int
i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
Heiko Schocher's avatar
Heiko Schocher committed
422
	struct davinci_i2c_platform_data *pdata = dev->pdata;
423
424
	u32 flag;
	u16 w;
425
	unsigned long time_left;
426

427
428
429
430
431
	if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) {
		dev_warn(dev->dev, "transfer to own address aborted\n");
		return -EADDRNOTAVAIL;
	}

432
433
434
435
436
437
438
439
440
	/* Introduce a delay, required for some boards (e.g Davinci EVM) */
	if (pdata->bus_delay)
		udelay(pdata->bus_delay);

	/* set the slave address */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);

	dev->buf = msg->buf;
	dev->buf_len = msg->len;
441
	dev->stop = stop;
442
443
444

	davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);

445
	reinit_completion(&dev->cmd_complete);
446
447
	dev->cmd_err = 0;

448
449
	/* Take I2C out of reset and configure it as master */
	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
450
451
452
453
454
455

	/* if the slave address is ten bit address, enable XA bit */
	if (msg->flags & I2C_M_TEN)
		flag |= DAVINCI_I2C_MDR_XA;
	if (!(msg->flags & I2C_M_RD))
		flag |= DAVINCI_I2C_MDR_TRX;
456
	if (msg->len == 0)
457
		flag |= DAVINCI_I2C_MDR_RM;
458
459
460
461

	/* Enable receive or transmit interrupts */
	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
	if (msg->flags & I2C_M_RD)
462
		w |= DAVINCI_I2C_IMR_RRDY;
463
	else
464
		w |= DAVINCI_I2C_IMR_XRDY;
465
466
	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);

467
	dev->terminate = 0;
468

469
470
471
	/*
	 * Write mode register first as needed for correct behaviour
	 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
Lucas De Marchi's avatar
Lucas De Marchi committed
472
	 * occurring before we have loaded DXR
473
474
475
	 */
	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);

476
477
478
479
480
	/*
	 * First byte should be set here, not after interrupt,
	 * because transmit-data-ready interrupt can come before
	 * NACK-interrupt during sending of previous message and
	 * ICDXR may have wrong data
481
	 * It also saves us one interrupt, slightly faster
482
483
484
485
486
487
	 */
	if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
		davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
		dev->buf_len--;
	}

488
489
490
491
	/* Set STT to begin transmit now DXR is loaded */
	flag |= DAVINCI_I2C_MDR_STT;
	if (stop && msg->len != 0)
		flag |= DAVINCI_I2C_MDR_STP;
492
493
	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);

494
495
496
	time_left = wait_for_completion_timeout(&dev->cmd_complete,
						dev->adapter.timeout);
	if (!time_left) {
497
		dev_err(dev->dev, "controller timed out\n");
498
		i2c_recover_bus(adap);
499
		dev->buf_len = 0;
500
501
		return -ETIMEDOUT;
	}
502
503
504
505
	if (dev->buf_len) {
		/* This should be 0 if all bytes were transferred
		 * or dev->cmd_err denotes an error.
		 */
506
		dev_err(dev->dev, "abnormal termination buf_len=%zu\n",
507
			dev->buf_len);
508
509
510
		dev->terminate = 1;
		wmb();
		dev->buf_len = 0;
511
		return -EREMOTEIO;
512
	}
513
514
515
516
517
518
519
520
521
522
523
524
525
526

	/* no error */
	if (likely(!dev->cmd_err))
		return msg->len;

	/* We have an error */
	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
		i2c_davinci_init(dev);
		return -EIO;
	}

	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return msg->len;
527
528
529
		w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
		w |= DAVINCI_I2C_MDR_STP;
		davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
		return -EREMOTEIO;
	}
	return -EIO;
}

/*
 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
 */
static int
i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{
	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
	int i;
	int ret;

545
	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
546

547
548
549
550
551
552
553
	ret = pm_runtime_get_sync(dev->dev);
	if (ret < 0) {
		dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret);
		pm_runtime_put_noidle(dev->dev);
		return ret;
	}

554
	ret = i2c_davinci_wait_bus_not_busy(dev);
555
556
	if (ret < 0) {
		dev_warn(dev->dev, "timeout waiting for bus ready\n");
557
		goto out;
558
559
560
561
	}

	for (i = 0; i < num; i++) {
		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
562
563
		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
			ret);
564
		if (ret < 0)
565
			goto out;
566
	}
567

568
	ret = num;
569

570
571
572
573
574
out:
	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);

	return ret;
575
576
577
578
}

static u32 i2c_davinci_func(struct i2c_adapter *adap)
{
579
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
580
581
}

582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
static void terminate_read(struct davinci_i2c_dev *dev)
{
	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
	w |= DAVINCI_I2C_MDR_NACK;
	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);

	/* Throw away data */
	davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
	if (!dev->terminate)
		dev_err(dev->dev, "RDR IRQ while no data requested\n");
}
static void terminate_write(struct davinci_i2c_dev *dev)
{
	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
	w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);

	if (!dev->terminate)
600
		dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
601
602
}

603
604
605
606
607
608
609
610
611
612
613
/*
 * Interrupt service routine. This gets called whenever an I2C interrupt
 * occurs.
 */
static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
{
	struct davinci_i2c_dev *dev = dev_id;
	u32 stat;
	int count = 0;
	u16 w;

614
615
616
	if (pm_runtime_suspended(dev->dev))
		return IRQ_NONE;

617
	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
618
		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
619
620
621
622
623
624
625
		if (count++ == 100) {
			dev_warn(dev->dev, "Too much work in one IRQ\n");
			break;
		}

		switch (stat) {
		case DAVINCI_I2C_IVR_AL:
626
			/* Arbitration lost, must retry */
627
			dev->cmd_err |= DAVINCI_I2C_STR_AL;
628
			dev->buf_len = 0;
629
630
631
632
633
			complete(&dev->cmd_complete);
			break;

		case DAVINCI_I2C_IVR_NACK:
			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
634
			dev->buf_len = 0;
635
636
637
638
			complete(&dev->cmd_complete);
			break;

		case DAVINCI_I2C_IVR_ARDY:
Troy Kisky's avatar
Troy Kisky committed
639
640
			davinci_i2c_write_reg(dev,
				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
641
642
643
644
645
646
647
648
			if (((dev->buf_len == 0) && (dev->stop != 0)) ||
			    (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
				w = davinci_i2c_read_reg(dev,
							 DAVINCI_I2C_MDR_REG);
				w |= DAVINCI_I2C_MDR_STP;
				davinci_i2c_write_reg(dev,
						      DAVINCI_I2C_MDR_REG, w);
			}
649
650
651
652
653
654
655
656
657
658
659
660
661
			complete(&dev->cmd_complete);
			break;

		case DAVINCI_I2C_IVR_RDR:
			if (dev->buf_len) {
				*dev->buf++ =
				    davinci_i2c_read_reg(dev,
							 DAVINCI_I2C_DRR_REG);
				dev->buf_len--;
				if (dev->buf_len)
					continue;

				davinci_i2c_write_reg(dev,
Troy Kisky's avatar
Troy Kisky committed
662
663
					DAVINCI_I2C_STR_REG,
					DAVINCI_I2C_IMR_RRDY);
664
665
666
667
			} else {
				/* signal can terminate transfer */
				terminate_read(dev);
			}
668
669
670
671
672
673
674
675
676
677
678
679
			break;

		case DAVINCI_I2C_IVR_XRDY:
			if (dev->buf_len) {
				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
						      *dev->buf++);
				dev->buf_len--;
				if (dev->buf_len)
					continue;

				w = davinci_i2c_read_reg(dev,
							 DAVINCI_I2C_IMR_REG);
680
				w &= ~DAVINCI_I2C_IMR_XRDY;
681
682
683
				davinci_i2c_write_reg(dev,
						      DAVINCI_I2C_IMR_REG,
						      w);
684
685
686
687
			} else {
				/* signal can terminate transfer */
				terminate_write(dev);
			}
688
689
690
			break;

		case DAVINCI_I2C_IVR_SCD:
Troy Kisky's avatar
Troy Kisky committed
691
692
			davinci_i2c_write_reg(dev,
				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
693
694
695
696
			complete(&dev->cmd_complete);
			break;

		case DAVINCI_I2C_IVR_AAS:
697
698
699
700
701
702
703
704
			dev_dbg(dev->dev, "Address as slave interrupt\n");
			break;

		default:
			dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
			break;
		}
	}
705
706
707
708

	return count ? IRQ_HANDLED : IRQ_NONE;
}

709
710
711
712
713
714
715
#ifdef CONFIG_CPU_FREQ
static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
				     unsigned long val, void *data)
{
	struct davinci_i2c_dev *dev;

	dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
716
717

	i2c_lock_adapter(&dev->adapter);
718
719
720
721
722
723
	if (val == CPUFREQ_PRECHANGE) {
		davinci_i2c_reset_ctrl(dev, 0);
	} else if (val == CPUFREQ_POSTCHANGE) {
		i2c_davinci_calc_clk_dividers(dev);
		davinci_i2c_reset_ctrl(dev, 1);
	}
724
	i2c_unlock_adapter(&dev->adapter);
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752

	return 0;
}

static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
{
	dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;

	return cpufreq_register_notifier(&dev->freq_transition,
					 CPUFREQ_TRANSITION_NOTIFIER);
}

static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
{
	cpufreq_unregister_notifier(&dev->freq_transition,
				    CPUFREQ_TRANSITION_NOTIFIER);
}
#else
static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
{
	return 0;
}

static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
{
}
#endif

753
static const struct i2c_algorithm i2c_davinci_algo = {
754
755
756
757
	.master_xfer	= i2c_davinci_xfer,
	.functionality	= i2c_davinci_func,
};

Heiko Schocher's avatar
Heiko Schocher committed
758
759
static const struct of_device_id davinci_i2c_of_match[] = {
	{.compatible = "ti,davinci-i2c", },
760
	{.compatible = "ti,keystone-i2c", },
Heiko Schocher's avatar
Heiko Schocher committed
761
762
763
764
	{},
};
MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);

765
766
767
768
static int davinci_i2c_probe(struct platform_device *pdev)
{
	struct davinci_i2c_dev *dev;
	struct i2c_adapter *adap;
769
	struct resource *mem;
770
	struct i2c_bus_recovery_info *rinfo;
771
772
773
774
775
776
777
778
779
780
	int r, irq;

	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		if (!irq)
			irq = -ENXIO;
		if (irq != -EPROBE_DEFER)
			dev_err(&pdev->dev,
				"can't get irq resource ret=%d\n", irq);
		return irq;
781
782
	}

783
784
	dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
			GFP_KERNEL);
785
	if (!dev) {
786
787
		dev_err(&pdev->dev, "Memory allocation failed\n");
		return -ENOMEM;
788
789
	}

790
	init_completion(&dev->cmd_complete);
791

792
	dev->dev = &pdev->dev;
793
	dev->irq = irq;
794
	dev->pdata = dev_get_platdata(&pdev->dev);
795
796
	platform_set_drvdata(pdev, dev);

Heiko Schocher's avatar
Heiko Schocher committed
797
798
799
800
801
	if (!dev->pdata && pdev->dev.of_node) {
		u32 prop;

		dev->pdata = devm_kzalloc(&pdev->dev,
			sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
802
803
804
		if (!dev->pdata)
			return -ENOMEM;

Heiko Schocher's avatar
Heiko Schocher committed
805
806
807
808
809
		memcpy(dev->pdata, &davinci_i2c_platform_data_default,
			sizeof(struct davinci_i2c_platform_data));
		if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
			&prop))
			dev->pdata->bus_freq = prop / 1000;
810
811
812
813

		dev->pdata->has_pfunc =
			of_property_read_bool(pdev->dev.of_node,
					      "ti,has-pfunc");
Heiko Schocher's avatar
Heiko Schocher committed
814
815
816
817
	} else if (!dev->pdata) {
		dev->pdata = &davinci_i2c_platform_data_default;
	}

818
	dev->clk = devm_clk_get(&pdev->dev, NULL);
819
	if (IS_ERR(dev->clk))
820
		return PTR_ERR(dev->clk);
821

822
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
823
824
	dev->base = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(dev->base)) {
825
826
827
828
829
830
831
832
833
834
835
836
837
838
		return PTR_ERR(dev->base);
	}

	pm_runtime_set_autosuspend_delay(dev->dev,
					 DAVINCI_I2C_PM_TIMEOUT);
	pm_runtime_use_autosuspend(dev->dev);

	pm_runtime_enable(dev->dev);

	r = pm_runtime_get_sync(dev->dev);
	if (r < 0) {
		dev_err(dev->dev, "failed to runtime_get device: %d\n", r);
		pm_runtime_put_noidle(dev->dev);
		return r;
839
840
	}

841
842
	i2c_davinci_init(dev);

843
844
	r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
			pdev->name, dev);
845
846
847
848
849
	if (r) {
		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
		goto err_unuse_clocks;
	}

850
851
852
	r = i2c_davinci_cpufreq_register(dev);
	if (r) {
		dev_err(&pdev->dev, "failed to register cpufreq\n");
853
		goto err_unuse_clocks;
854
855
	}

856
857
858
	adap = &dev->adapter;
	i2c_set_adapdata(adap, dev);
	adap->owner = THIS_MODULE;
859
	adap->class = I2C_CLASS_DEPRECATED;
860
861
862
	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
	adap->algo = &i2c_davinci_algo;
	adap->dev.parent = &pdev->dev;
863
	adap->timeout = DAVINCI_I2C_TIMEOUT;
Heiko Schocher's avatar
Heiko Schocher committed
864
	adap->dev.of_node = pdev->dev.of_node;
865

866
867
	if (dev->pdata->has_pfunc)
		adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
868
	else if (dev->pdata->gpio_recovery) {
869
870
		rinfo =  &davinci_i2c_gpio_recovery_info;
		adap->bus_recovery_info = rinfo;
871
872
873
874
		rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl",
						  GPIOD_OUT_HIGH_OPEN_DRAIN);
		if (IS_ERR(rinfo->scl_gpiod)) {
			r = PTR_ERR(rinfo->scl_gpiod);
875
			goto err_unuse_clocks;
876
877
878
879
		}
		rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
		if (IS_ERR(rinfo->sda_gpiod)) {
			r = PTR_ERR(rinfo->sda_gpiod);
880
			goto err_unuse_clocks;
881
		}
882
883
	}

884
885
	adap->nr = pdev->id;
	r = i2c_add_numbered_adapter(adap);
886
	if (r)
887
		goto err_unuse_clocks;
888

889
890
891
	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);

892
893
894
	return 0;

err_unuse_clocks:
895
896
897
898
	pm_runtime_dont_use_autosuspend(dev->dev);
	pm_runtime_put_sync(dev->dev);
	pm_runtime_disable(dev->dev);

899
900
901
902
903
904
	return r;
}

static int davinci_i2c_remove(struct platform_device *pdev)
{
	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
905
	int ret;
906

907
908
	i2c_davinci_cpufreq_deregister(dev);

909
910
	i2c_del_adapter(&dev->adapter);

911
912
913
914
915
	ret = pm_runtime_get_sync(&pdev->dev);
	if (ret < 0) {
		pm_runtime_put_noidle(&pdev->dev);
		return ret;
	}
916
917
918

	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);

919
920
921
922
	pm_runtime_dont_use_autosuspend(dev->dev);
	pm_runtime_put_sync(dev->dev);
	pm_runtime_disable(dev->dev);

923
924
925
	return 0;
}

926
927
928
#ifdef CONFIG_PM
static int davinci_i2c_suspend(struct device *dev)
{
929
	struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
930
931
932
933
934
935
936
937
938

	/* put I2C into reset */
	davinci_i2c_reset_ctrl(i2c_dev, 0);

	return 0;
}

static int davinci_i2c_resume(struct device *dev)
{
939
	struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
940
941
942
943
944
945
946
947
948
949

	/* take I2C out of reset */
	davinci_i2c_reset_ctrl(i2c_dev, 1);

	return 0;
}

static const struct dev_pm_ops davinci_i2c_pm = {
	.suspend        = davinci_i2c_suspend,
	.resume         = davinci_i2c_resume,
950
951
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				      pm_runtime_force_resume)
952
953
954
955
956
957
958
};

#define davinci_i2c_pm_ops (&davinci_i2c_pm)
#else
#define davinci_i2c_pm_ops NULL
#endif

959
960
961
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:i2c_davinci");

962
963
964
965
966
static struct platform_driver davinci_i2c_driver = {
	.probe		= davinci_i2c_probe,
	.remove		= davinci_i2c_remove,
	.driver		= {
		.name	= "i2c_davinci",
967
		.pm	= davinci_i2c_pm_ops,
968
		.of_match_table = davinci_i2c_of_match,
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
	},
};

/* I2C may be needed to bring up other drivers */
static int __init davinci_i2c_init_driver(void)
{
	return platform_driver_register(&davinci_i2c_driver);
}
subsys_initcall(davinci_i2c_init_driver);

static void __exit davinci_i2c_exit_driver(void)
{
	platform_driver_unregister(&davinci_i2c_driver);
}
module_exit(davinci_i2c_exit_driver);

MODULE_AUTHOR("Texas Instruments India");
MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
MODULE_LICENSE("GPL");