Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * TI DAVINCI I2C adapter driver.
4 *
5 * Copyright (C) 2006 Texas Instruments.
6 * Copyright (C) 2007 MontaVista Software Inc.
7 *
8 * Updated by Vinod & Sudhakar Feb 2005
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * ----------------------------------------------------------------------------
13 */
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/clk.h>
19#include <linux/errno.h>
20#include <linux/sched.h>
21#include <linux/err.h>
22#include <linux/interrupt.h>
23#include <linux/platform_device.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/cpufreq.h>
27#include <linux/gpio/consumer.h>
28#include <linux/of.h>
29#include <linux/platform_data/i2c-davinci.h>
30#include <linux/pm_runtime.h>
31
32/* ----- global defines ----------------------------------------------- */
33
34#define DAVINCI_I2C_TIMEOUT (1*HZ)
35#define DAVINCI_I2C_MAX_TRIES 2
36#define DAVINCI_I2C_OWN_ADDRESS 0x08
37#define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_SCD | \
38 DAVINCI_I2C_IMR_ARDY | \
39 DAVINCI_I2C_IMR_NACK | \
40 DAVINCI_I2C_IMR_AL)
41
42#define DAVINCI_I2C_OAR_REG 0x00
43#define DAVINCI_I2C_IMR_REG 0x04
44#define DAVINCI_I2C_STR_REG 0x08
45#define DAVINCI_I2C_CLKL_REG 0x0c
46#define DAVINCI_I2C_CLKH_REG 0x10
47#define DAVINCI_I2C_CNT_REG 0x14
48#define DAVINCI_I2C_DRR_REG 0x18
49#define DAVINCI_I2C_SAR_REG 0x1c
50#define DAVINCI_I2C_DXR_REG 0x20
51#define DAVINCI_I2C_MDR_REG 0x24
52#define DAVINCI_I2C_IVR_REG 0x28
53#define DAVINCI_I2C_EMDR_REG 0x2c
54#define DAVINCI_I2C_PSC_REG 0x30
55#define DAVINCI_I2C_FUNC_REG 0x48
56#define DAVINCI_I2C_DIR_REG 0x4c
57#define DAVINCI_I2C_DIN_REG 0x50
58#define DAVINCI_I2C_DOUT_REG 0x54
59#define DAVINCI_I2C_DSET_REG 0x58
60#define DAVINCI_I2C_DCLR_REG 0x5c
61
62#define DAVINCI_I2C_IVR_AAS 0x07
63#define DAVINCI_I2C_IVR_SCD 0x06
64#define DAVINCI_I2C_IVR_XRDY 0x05
65#define DAVINCI_I2C_IVR_RDR 0x04
66#define DAVINCI_I2C_IVR_ARDY 0x03
67#define DAVINCI_I2C_IVR_NACK 0x02
68#define DAVINCI_I2C_IVR_AL 0x01
69
70#define DAVINCI_I2C_STR_BB BIT(12)
71#define DAVINCI_I2C_STR_RSFULL BIT(11)
72#define DAVINCI_I2C_STR_SCD BIT(5)
73#define DAVINCI_I2C_STR_ARDY BIT(2)
74#define DAVINCI_I2C_STR_NACK BIT(1)
75#define DAVINCI_I2C_STR_AL BIT(0)
76
77#define DAVINCI_I2C_MDR_NACK BIT(15)
78#define DAVINCI_I2C_MDR_STT BIT(13)
79#define DAVINCI_I2C_MDR_STP BIT(11)
80#define DAVINCI_I2C_MDR_MST BIT(10)
81#define DAVINCI_I2C_MDR_TRX BIT(9)
82#define DAVINCI_I2C_MDR_XA BIT(8)
83#define DAVINCI_I2C_MDR_RM BIT(7)
84#define DAVINCI_I2C_MDR_IRS BIT(5)
85
86#define DAVINCI_I2C_IMR_AAS BIT(6)
87#define DAVINCI_I2C_IMR_SCD BIT(5)
88#define DAVINCI_I2C_IMR_XRDY BIT(4)
89#define DAVINCI_I2C_IMR_RRDY BIT(3)
90#define DAVINCI_I2C_IMR_ARDY BIT(2)
91#define DAVINCI_I2C_IMR_NACK BIT(1)
92#define DAVINCI_I2C_IMR_AL BIT(0)
93
94/* set SDA and SCL as GPIO */
95#define DAVINCI_I2C_FUNC_PFUNC0 BIT(0)
96
97/* set SCL as output when used as GPIO*/
98#define DAVINCI_I2C_DIR_PDIR0 BIT(0)
99/* set SDA as output when used as GPIO*/
100#define DAVINCI_I2C_DIR_PDIR1 BIT(1)
101
102/* read SCL GPIO level */
103#define DAVINCI_I2C_DIN_PDIN0 BIT(0)
104/* read SDA GPIO level */
105#define DAVINCI_I2C_DIN_PDIN1 BIT(1)
106
107/*set the SCL GPIO high */
108#define DAVINCI_I2C_DSET_PDSET0 BIT(0)
109/*set the SDA GPIO high */
110#define DAVINCI_I2C_DSET_PDSET1 BIT(1)
111
112/* set the SCL GPIO low */
113#define DAVINCI_I2C_DCLR_PDCLR0 BIT(0)
114/* set the SDA GPIO low */
115#define DAVINCI_I2C_DCLR_PDCLR1 BIT(1)
116
117/* timeout for pm runtime autosuspend */
118#define DAVINCI_I2C_PM_TIMEOUT 1000 /* ms */
119
120struct davinci_i2c_dev {
121 struct device *dev;
122 void __iomem *base;
123 struct completion cmd_complete;
124 struct clk *clk;
125 int cmd_err;
126 u8 *buf;
127 size_t buf_len;
128 int irq;
129 int stop;
130 u8 terminate;
131 struct i2c_adapter adapter;
132#ifdef CONFIG_CPU_FREQ
133 struct notifier_block freq_transition;
134#endif
135 struct davinci_i2c_platform_data *pdata;
136};
137
138/* default platform data to use if not supplied in the platform_device */
139static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
140 .bus_freq = 100,
141 .bus_delay = 0,
142};
143
144static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
145 int reg, u16 val)
146{
147 writew_relaxed(val, i2c_dev->base + reg);
148}
149
150static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
151{
152 return readw_relaxed(i2c_dev->base + reg);
153}
154
155static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
156 int val)
157{
158 u16 w;
159
160 w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
161 if (!val) /* put I2C into reset */
162 w &= ~DAVINCI_I2C_MDR_IRS;
163 else /* take I2C out of reset */
164 w |= DAVINCI_I2C_MDR_IRS;
165
166 davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
167}
168
169static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
170{
171 struct davinci_i2c_platform_data *pdata = dev->pdata;
172 u16 psc;
173 u32 clk;
174 u32 d;
175 u32 clkh;
176 u32 clkl;
177 u32 input_clock = clk_get_rate(dev->clk);
178 struct device_node *of_node = dev->dev->of_node;
179
180 /* NOTE: I2C Clock divider programming info
181 * As per I2C specs the following formulas provide prescaler
182 * and low/high divider values
183 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
184 * module clk
185 *
186 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
187 *
188 * Thus,
189 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
190 *
191 * where if PSC == 0, d = 7,
192 * if PSC == 1, d = 6
193 * if PSC > 1 , d = 5
194 *
195 * Note:
196 * d is always 6 on Keystone I2C controller
197 */
198
199 /*
200 * Both Davinci and current Keystone User Guides recommend a value
201 * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
202 * always produce enough margin between SDA and SCL transitions.
203 * Measurements show that the higher the module clock is, the
204 * bigger is the margin, providing more reliable communication.
205 * So we better target for 12MHz.
206 */
207 psc = (input_clock / 12000000) - 1;
208 if ((input_clock / (psc + 1)) > 12000000)
209 psc++; /* better to run under spec than over */
210 d = (psc >= 2) ? 5 : 7 - psc;
211
212 if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c"))
213 d = 6;
214
215 clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000));
216 /* Avoid driving the bus too fast because of rounding errors above */
217 if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000)
218 clk++;
219 /*
220 * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
221 * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
222 * to LOW ratio as 1 to 2 is more safe.
223 */
224 if (pdata->bus_freq > 100)
225 clkl = (clk << 1) / 3;
226 else
227 clkl = (clk >> 1);
228 /*
229 * It's not always possible to have 1 to 2 ratio when d=7, so fall back
230 * to minimal possible clkh in this case.
231 *
232 * Note:
233 * CLKH is not allowed to be 0, in this case I2C clock is not generated
234 * at all
235 */
236 if (clk > clkl + d) {
237 clkh = clk - clkl - d;
238 clkl -= d;
239 } else {
240 clkh = 1;
241 clkl = clk - (d << 1);
242 }
243
244 davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
245 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
246 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
247
248 dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
249}
250
251/*
252 * This function configures I2C and brings I2C out of reset.
253 * This function is called during I2C init function. This function
254 * also gets called if I2C encounters any errors.
255 */
256static int i2c_davinci_init(struct davinci_i2c_dev *dev)
257{
258 struct davinci_i2c_platform_data *pdata = dev->pdata;
259
260 /* put I2C into reset */
261 davinci_i2c_reset_ctrl(dev, 0);
262
263 /* compute clock dividers */
264 i2c_davinci_calc_clk_dividers(dev);
265
266 /* Respond at reserved "SMBus Host" target address" (and zero);
267 * we seem to have no option to not respond...
268 */
269 davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS);
270
271 dev_dbg(dev->dev, "PSC = %d\n",
272 davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
273 dev_dbg(dev->dev, "CLKL = %d\n",
274 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
275 dev_dbg(dev->dev, "CLKH = %d\n",
276 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
277 dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
278 pdata->bus_freq, pdata->bus_delay);
279
280
281 /* Take the I2C module out of reset: */
282 davinci_i2c_reset_ctrl(dev, 1);
283
284 /* Enable interrupts */
285 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
286
287 return 0;
288}
289
290/*
291 * This routine does i2c bus recovery by using i2c_generic_scl_recovery
292 * which is provided by I2C Bus recovery infrastructure.
293 */
294static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
295{
296 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
297
298 /* Disable interrupts */
299 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);
300
301 /* put I2C into reset */
302 davinci_i2c_reset_ctrl(dev, 0);
303}
304
305static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
306{
307 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
308
309 i2c_davinci_init(dev);
310}
311
312static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
313 .recover_bus = i2c_generic_scl_recovery,
314 .prepare_recovery = davinci_i2c_prepare_recovery,
315 .unprepare_recovery = davinci_i2c_unprepare_recovery,
316};
317
318static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
319{
320 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
321
322 if (val)
323 davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
324 DAVINCI_I2C_DSET_PDSET0);
325 else
326 davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
327 DAVINCI_I2C_DCLR_PDCLR0);
328}
329
330static int davinci_i2c_get_scl(struct i2c_adapter *adap)
331{
332 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
333 int val;
334
335 /* read the state of SCL */
336 val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
337 return val & DAVINCI_I2C_DIN_PDIN0;
338}
339
340static int davinci_i2c_get_sda(struct i2c_adapter *adap)
341{
342 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
343 int val;
344
345 /* read the state of SDA */
346 val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
347 return val & DAVINCI_I2C_DIN_PDIN1;
348}
349
350static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
351{
352 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
353
354 davinci_i2c_prepare_recovery(adap);
355
356 /* SCL output, SDA input */
357 davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);
358
359 /* change to GPIO mode */
360 davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
361 DAVINCI_I2C_FUNC_PFUNC0);
362}
363
364static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
365{
366 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
367
368 /* change back to I2C mode */
369 davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);
370
371 davinci_i2c_unprepare_recovery(adap);
372}
373
374static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
375 .recover_bus = i2c_generic_scl_recovery,
376 .set_scl = davinci_i2c_set_scl,
377 .get_scl = davinci_i2c_get_scl,
378 .get_sda = davinci_i2c_get_sda,
379 .prepare_recovery = davinci_i2c_scl_prepare_recovery,
380 .unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
381};
382
383/*
384 * Waiting for bus not busy
385 */
386static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
387{
388 unsigned long timeout = jiffies + dev->adapter.timeout;
389
390 do {
391 if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
392 return 0;
393 schedule_timeout_uninterruptible(1);
394 } while (time_before_eq(jiffies, timeout));
395
396 dev_warn(dev->dev, "timeout waiting for bus ready\n");
397 i2c_recover_bus(&dev->adapter);
398
399 /*
400 * if bus is still "busy" here, it's most probably a HW problem like
401 * short-circuit
402 */
403 if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
404 return -EIO;
405
406 return 0;
407}
408
409/*
410 * Low level read/write transaction. This function is called from
411 * i2c_davinci_xfer.
412 */
413static int
414i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
415{
416 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
417 struct davinci_i2c_platform_data *pdata = dev->pdata;
418 u32 flag;
419 u16 w;
420 unsigned long time_left;
421
422 if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) {
423 dev_warn(dev->dev, "transfer to own address aborted\n");
424 return -EADDRNOTAVAIL;
425 }
426
427 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
428 if (pdata->bus_delay)
429 udelay(pdata->bus_delay);
430
431 /* set the target address */
432 davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
433
434 dev->buf = msg->buf;
435 dev->buf_len = msg->len;
436 dev->stop = stop;
437
438 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
439
440 reinit_completion(&dev->cmd_complete);
441 dev->cmd_err = 0;
442
443 /* Take I2C out of reset and configure it as controller */
444 flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
445
446 if (msg->flags & I2C_M_TEN)
447 flag |= DAVINCI_I2C_MDR_XA;
448 if (!(msg->flags & I2C_M_RD))
449 flag |= DAVINCI_I2C_MDR_TRX;
450 if (msg->len == 0)
451 flag |= DAVINCI_I2C_MDR_RM;
452
453 /* Enable receive or transmit interrupts */
454 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
455 if (msg->flags & I2C_M_RD)
456 w |= DAVINCI_I2C_IMR_RRDY;
457 else
458 w |= DAVINCI_I2C_IMR_XRDY;
459 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
460
461 dev->terminate = 0;
462
463 /*
464 * Write mode register first as needed for correct behaviour
465 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
466 * occurring before we have loaded DXR
467 */
468 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
469
470 /*
471 * First byte should be set here, not after interrupt,
472 * because transmit-data-ready interrupt can come before
473 * NACK-interrupt during sending of previous message and
474 * ICDXR may have wrong data
475 * It also saves us one interrupt, slightly faster
476 */
477 if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
478 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
479 dev->buf_len--;
480 }
481
482 /* Set STT to begin transmit now DXR is loaded */
483 flag |= DAVINCI_I2C_MDR_STT;
484 if (stop && msg->len != 0)
485 flag |= DAVINCI_I2C_MDR_STP;
486 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
487
488 time_left = wait_for_completion_timeout(&dev->cmd_complete,
489 dev->adapter.timeout);
490 if (!time_left) {
491 i2c_recover_bus(adap);
492 dev->buf_len = 0;
493 return -ETIMEDOUT;
494 }
495 if (dev->buf_len) {
496 /* This should be 0 if all bytes were transferred
497 * or dev->cmd_err denotes an error.
498 */
499 dev_err(dev->dev, "abnormal termination buf_len=%zu\n",
500 dev->buf_len);
501 dev->terminate = 1;
502 wmb();
503 dev->buf_len = 0;
504 return -EREMOTEIO;
505 }
506
507 /* no error */
508 if (likely(!dev->cmd_err))
509 return msg->len;
510
511 /* We have an error */
512 if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
513 i2c_davinci_init(dev);
514 return -EIO;
515 }
516
517 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
518 if (msg->flags & I2C_M_IGNORE_NAK)
519 return msg->len;
520 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
521 w |= DAVINCI_I2C_MDR_STP;
522 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
523 return -EREMOTEIO;
524 }
525 return -EIO;
526}
527
528/*
529 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
530 */
531static int
532i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
533{
534 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
535 int i;
536 int ret;
537
538 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
539
540 ret = pm_runtime_resume_and_get(dev->dev);
541 if (ret < 0) {
542 dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret);
543 return ret;
544 }
545
546 ret = i2c_davinci_wait_bus_not_busy(dev);
547 if (ret < 0) {
548 dev_warn(dev->dev, "timeout waiting for bus ready\n");
549 goto out;
550 }
551
552 for (i = 0; i < num; i++) {
553 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
554 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
555 ret);
556 if (ret < 0)
557 goto out;
558 }
559
560 ret = num;
561
562out:
563 pm_runtime_mark_last_busy(dev->dev);
564 pm_runtime_put_autosuspend(dev->dev);
565
566 return ret;
567}
568
569static u32 i2c_davinci_func(struct i2c_adapter *adap)
570{
571 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
572}
573
574static void terminate_read(struct davinci_i2c_dev *dev)
575{
576 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
577 w |= DAVINCI_I2C_MDR_NACK;
578 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
579
580 /* Throw away data */
581 davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
582 if (!dev->terminate)
583 dev_err(dev->dev, "RDR IRQ while no data requested\n");
584}
585static void terminate_write(struct davinci_i2c_dev *dev)
586{
587 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
588 w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
589 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
590
591 if (!dev->terminate)
592 dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
593}
594
595/*
596 * Interrupt service routine. This gets called whenever an I2C interrupt
597 * occurs.
598 */
599static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
600{
601 struct davinci_i2c_dev *dev = dev_id;
602 u32 stat;
603 int count = 0;
604 u16 w;
605
606 if (pm_runtime_suspended(dev->dev))
607 return IRQ_NONE;
608
609 while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
610 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
611 if (count++ == 100) {
612 dev_warn(dev->dev, "Too much work in one IRQ\n");
613 break;
614 }
615
616 switch (stat) {
617 case DAVINCI_I2C_IVR_AL:
618 /* Arbitration lost, must retry */
619 dev->cmd_err |= DAVINCI_I2C_STR_AL;
620 dev->buf_len = 0;
621 complete(&dev->cmd_complete);
622 break;
623
624 case DAVINCI_I2C_IVR_NACK:
625 dev->cmd_err |= DAVINCI_I2C_STR_NACK;
626 dev->buf_len = 0;
627 complete(&dev->cmd_complete);
628 break;
629
630 case DAVINCI_I2C_IVR_ARDY:
631 davinci_i2c_write_reg(dev,
632 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
633 if (((dev->buf_len == 0) && (dev->stop != 0)) ||
634 (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
635 w = davinci_i2c_read_reg(dev,
636 DAVINCI_I2C_MDR_REG);
637 w |= DAVINCI_I2C_MDR_STP;
638 davinci_i2c_write_reg(dev,
639 DAVINCI_I2C_MDR_REG, w);
640 }
641 complete(&dev->cmd_complete);
642 break;
643
644 case DAVINCI_I2C_IVR_RDR:
645 if (dev->buf_len) {
646 *dev->buf++ =
647 davinci_i2c_read_reg(dev,
648 DAVINCI_I2C_DRR_REG);
649 dev->buf_len--;
650 if (dev->buf_len)
651 continue;
652
653 davinci_i2c_write_reg(dev,
654 DAVINCI_I2C_STR_REG,
655 DAVINCI_I2C_IMR_RRDY);
656 } else {
657 /* signal can terminate transfer */
658 terminate_read(dev);
659 }
660 break;
661
662 case DAVINCI_I2C_IVR_XRDY:
663 if (dev->buf_len) {
664 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
665 *dev->buf++);
666 dev->buf_len--;
667 if (dev->buf_len)
668 continue;
669
670 w = davinci_i2c_read_reg(dev,
671 DAVINCI_I2C_IMR_REG);
672 w &= ~DAVINCI_I2C_IMR_XRDY;
673 davinci_i2c_write_reg(dev,
674 DAVINCI_I2C_IMR_REG,
675 w);
676 } else {
677 /* signal can terminate transfer */
678 terminate_write(dev);
679 }
680 break;
681
682 case DAVINCI_I2C_IVR_SCD:
683 davinci_i2c_write_reg(dev,
684 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
685 complete(&dev->cmd_complete);
686 break;
687
688 case DAVINCI_I2C_IVR_AAS:
689 dev_dbg(dev->dev, "Address as target interrupt\n");
690 break;
691
692 default:
693 dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
694 break;
695 }
696 }
697
698 return count ? IRQ_HANDLED : IRQ_NONE;
699}
700
701#ifdef CONFIG_CPU_FREQ
702static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
703 unsigned long val, void *data)
704{
705 struct davinci_i2c_dev *dev;
706
707 dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
708
709 i2c_lock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
710 if (val == CPUFREQ_PRECHANGE) {
711 davinci_i2c_reset_ctrl(dev, 0);
712 } else if (val == CPUFREQ_POSTCHANGE) {
713 i2c_davinci_calc_clk_dividers(dev);
714 davinci_i2c_reset_ctrl(dev, 1);
715 }
716 i2c_unlock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
717
718 return 0;
719}
720
721static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
722{
723 dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
724
725 return cpufreq_register_notifier(&dev->freq_transition,
726 CPUFREQ_TRANSITION_NOTIFIER);
727}
728
729static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
730{
731 cpufreq_unregister_notifier(&dev->freq_transition,
732 CPUFREQ_TRANSITION_NOTIFIER);
733}
734#else
735static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
736{
737 return 0;
738}
739
740static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
741{
742}
743#endif
744
745static const struct i2c_algorithm i2c_davinci_algo = {
746 .xfer = i2c_davinci_xfer,
747 .functionality = i2c_davinci_func,
748};
749
750static const struct of_device_id davinci_i2c_of_match[] = {
751 {.compatible = "ti,davinci-i2c", },
752 {.compatible = "ti,keystone-i2c", },
753 {},
754};
755MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
756
757static int davinci_i2c_probe(struct platform_device *pdev)
758{
759 struct davinci_i2c_dev *dev;
760 struct i2c_adapter *adap;
761 struct i2c_bus_recovery_info *rinfo;
762 int r, irq;
763
764 irq = platform_get_irq(pdev, 0);
765 if (irq < 0)
766 return irq;
767
768 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
769 if (!dev)
770 return -ENOMEM;
771
772 init_completion(&dev->cmd_complete);
773
774 dev->dev = &pdev->dev;
775 dev->irq = irq;
776 dev->pdata = dev_get_platdata(&pdev->dev);
777 platform_set_drvdata(pdev, dev);
778
779 if (!dev->pdata && pdev->dev.of_node) {
780 u32 prop;
781
782 dev->pdata = devm_kzalloc(&pdev->dev,
783 sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
784 if (!dev->pdata)
785 return -ENOMEM;
786
787 memcpy(dev->pdata, &davinci_i2c_platform_data_default,
788 sizeof(struct davinci_i2c_platform_data));
789 if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
790 &prop))
791 dev->pdata->bus_freq = prop / 1000;
792
793 dev->pdata->has_pfunc =
794 of_property_read_bool(pdev->dev.of_node,
795 "ti,has-pfunc");
796 } else if (!dev->pdata) {
797 dev->pdata = &davinci_i2c_platform_data_default;
798 }
799
800 dev->clk = devm_clk_get(&pdev->dev, NULL);
801 if (IS_ERR(dev->clk))
802 return PTR_ERR(dev->clk);
803
804 dev->base = devm_platform_ioremap_resource(pdev, 0);
805 if (IS_ERR(dev->base)) {
806 return PTR_ERR(dev->base);
807 }
808
809 pm_runtime_set_autosuspend_delay(dev->dev,
810 DAVINCI_I2C_PM_TIMEOUT);
811 pm_runtime_use_autosuspend(dev->dev);
812
813 pm_runtime_enable(dev->dev);
814
815 r = pm_runtime_resume_and_get(dev->dev);
816 if (r < 0) {
817 dev_err(dev->dev, "failed to runtime_get device: %d\n", r);
818 goto err_pm;
819 }
820
821 i2c_davinci_init(dev);
822
823 r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
824 pdev->name, dev);
825 if (r) {
826 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
827 goto err_unuse_clocks;
828 }
829
830 r = i2c_davinci_cpufreq_register(dev);
831 if (r) {
832 dev_err(&pdev->dev, "failed to register cpufreq\n");
833 goto err_unuse_clocks;
834 }
835
836 adap = &dev->adapter;
837 i2c_set_adapdata(adap, dev);
838 adap->owner = THIS_MODULE;
839 adap->class = I2C_CLASS_DEPRECATED;
840 strscpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
841 adap->algo = &i2c_davinci_algo;
842 adap->dev.parent = &pdev->dev;
843 adap->timeout = DAVINCI_I2C_TIMEOUT;
844 adap->dev.of_node = pdev->dev.of_node;
845
846 if (dev->pdata->has_pfunc)
847 adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
848 else if (dev->pdata->gpio_recovery) {
849 rinfo = &davinci_i2c_gpio_recovery_info;
850 adap->bus_recovery_info = rinfo;
851 rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl",
852 GPIOD_OUT_HIGH_OPEN_DRAIN);
853 if (IS_ERR(rinfo->scl_gpiod)) {
854 r = PTR_ERR(rinfo->scl_gpiod);
855 goto err_unuse_clocks;
856 }
857 rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
858 if (IS_ERR(rinfo->sda_gpiod)) {
859 r = PTR_ERR(rinfo->sda_gpiod);
860 goto err_unuse_clocks;
861 }
862 }
863
864 adap->nr = pdev->id;
865 r = i2c_add_numbered_adapter(adap);
866 if (r)
867 goto err_unuse_clocks;
868
869 pm_runtime_mark_last_busy(dev->dev);
870 pm_runtime_put_autosuspend(dev->dev);
871
872 return 0;
873
874err_unuse_clocks:
875 pm_runtime_dont_use_autosuspend(dev->dev);
876 pm_runtime_put_sync(dev->dev);
877err_pm:
878 pm_runtime_disable(dev->dev);
879
880 return r;
881}
882
883static void davinci_i2c_remove(struct platform_device *pdev)
884{
885 struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
886 int ret;
887
888 i2c_davinci_cpufreq_deregister(dev);
889
890 i2c_del_adapter(&dev->adapter);
891
892 ret = pm_runtime_get_sync(&pdev->dev);
893 if (ret < 0)
894 dev_err(&pdev->dev, "Failed to resume device\n");
895 else
896 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
897
898 pm_runtime_dont_use_autosuspend(dev->dev);
899 pm_runtime_put_sync(dev->dev);
900 pm_runtime_disable(dev->dev);
901}
902
903static int davinci_i2c_suspend(struct device *dev)
904{
905 struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
906
907 /* put I2C into reset */
908 davinci_i2c_reset_ctrl(i2c_dev, 0);
909
910 return 0;
911}
912
913static int davinci_i2c_resume(struct device *dev)
914{
915 struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
916
917 /* take I2C out of reset */
918 davinci_i2c_reset_ctrl(i2c_dev, 1);
919
920 return 0;
921}
922
923static const struct dev_pm_ops davinci_i2c_pm = {
924 .suspend = davinci_i2c_suspend,
925 .resume = davinci_i2c_resume,
926 NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
927 pm_runtime_force_resume)
928};
929
930static const struct platform_device_id davinci_i2c_driver_ids[] = {
931 { .name = "i2c_davinci", },
932 { /* sentinel */ }
933};
934MODULE_DEVICE_TABLE(platform, davinci_i2c_driver_ids);
935
936static struct platform_driver davinci_i2c_driver = {
937 .probe = davinci_i2c_probe,
938 .remove = davinci_i2c_remove,
939 .id_table = davinci_i2c_driver_ids,
940 .driver = {
941 .name = "i2c_davinci",
942 .pm = pm_sleep_ptr(&davinci_i2c_pm),
943 .of_match_table = davinci_i2c_of_match,
944 },
945};
946
947/* I2C may be needed to bring up other drivers */
948static int __init davinci_i2c_init_driver(void)
949{
950 return platform_driver_register(&davinci_i2c_driver);
951}
952subsys_initcall(davinci_i2c_init_driver);
953
954static void __exit davinci_i2c_exit_driver(void)
955{
956 platform_driver_unregister(&davinci_i2c_driver);
957}
958module_exit(davinci_i2c_exit_driver);
959
960MODULE_AUTHOR("Texas Instruments India");
961MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
962MODULE_LICENSE("GPL");
1/*
2 * TI DAVINCI I2C adapter driver.
3 *
4 * Copyright (C) 2006 Texas Instruments.
5 * Copyright (C) 2007 MontaVista Software Inc.
6 *
7 * Updated by Vinod & Sudhakar Feb 2005
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * ----------------------------------------------------------------------------
25 *
26 */
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/delay.h>
30#include <linux/i2c.h>
31#include <linux/clk.h>
32#include <linux/errno.h>
33#include <linux/sched.h>
34#include <linux/err.h>
35#include <linux/interrupt.h>
36#include <linux/platform_device.h>
37#include <linux/io.h>
38#include <linux/slab.h>
39#include <linux/cpufreq.h>
40#include <linux/gpio.h>
41#include <linux/of_device.h>
42#include <linux/platform_data/i2c-davinci.h>
43
44/* ----- global defines ----------------------------------------------- */
45
46#define DAVINCI_I2C_TIMEOUT (1*HZ)
47#define DAVINCI_I2C_MAX_TRIES 2
48#define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
49 DAVINCI_I2C_IMR_SCD | \
50 DAVINCI_I2C_IMR_ARDY | \
51 DAVINCI_I2C_IMR_NACK | \
52 DAVINCI_I2C_IMR_AL)
53
54#define DAVINCI_I2C_OAR_REG 0x00
55#define DAVINCI_I2C_IMR_REG 0x04
56#define DAVINCI_I2C_STR_REG 0x08
57#define DAVINCI_I2C_CLKL_REG 0x0c
58#define DAVINCI_I2C_CLKH_REG 0x10
59#define DAVINCI_I2C_CNT_REG 0x14
60#define DAVINCI_I2C_DRR_REG 0x18
61#define DAVINCI_I2C_SAR_REG 0x1c
62#define DAVINCI_I2C_DXR_REG 0x20
63#define DAVINCI_I2C_MDR_REG 0x24
64#define DAVINCI_I2C_IVR_REG 0x28
65#define DAVINCI_I2C_EMDR_REG 0x2c
66#define DAVINCI_I2C_PSC_REG 0x30
67
68#define DAVINCI_I2C_IVR_AAS 0x07
69#define DAVINCI_I2C_IVR_SCD 0x06
70#define DAVINCI_I2C_IVR_XRDY 0x05
71#define DAVINCI_I2C_IVR_RDR 0x04
72#define DAVINCI_I2C_IVR_ARDY 0x03
73#define DAVINCI_I2C_IVR_NACK 0x02
74#define DAVINCI_I2C_IVR_AL 0x01
75
76#define DAVINCI_I2C_STR_BB BIT(12)
77#define DAVINCI_I2C_STR_RSFULL BIT(11)
78#define DAVINCI_I2C_STR_SCD BIT(5)
79#define DAVINCI_I2C_STR_ARDY BIT(2)
80#define DAVINCI_I2C_STR_NACK BIT(1)
81#define DAVINCI_I2C_STR_AL BIT(0)
82
83#define DAVINCI_I2C_MDR_NACK BIT(15)
84#define DAVINCI_I2C_MDR_STT BIT(13)
85#define DAVINCI_I2C_MDR_STP BIT(11)
86#define DAVINCI_I2C_MDR_MST BIT(10)
87#define DAVINCI_I2C_MDR_TRX BIT(9)
88#define DAVINCI_I2C_MDR_XA BIT(8)
89#define DAVINCI_I2C_MDR_RM BIT(7)
90#define DAVINCI_I2C_MDR_IRS BIT(5)
91
92#define DAVINCI_I2C_IMR_AAS BIT(6)
93#define DAVINCI_I2C_IMR_SCD BIT(5)
94#define DAVINCI_I2C_IMR_XRDY BIT(4)
95#define DAVINCI_I2C_IMR_RRDY BIT(3)
96#define DAVINCI_I2C_IMR_ARDY BIT(2)
97#define DAVINCI_I2C_IMR_NACK BIT(1)
98#define DAVINCI_I2C_IMR_AL BIT(0)
99
100struct davinci_i2c_dev {
101 struct device *dev;
102 void __iomem *base;
103 struct completion cmd_complete;
104 struct clk *clk;
105 int cmd_err;
106 u8 *buf;
107 size_t buf_len;
108 int irq;
109 int stop;
110 u8 terminate;
111 struct i2c_adapter adapter;
112#ifdef CONFIG_CPU_FREQ
113 struct completion xfr_complete;
114 struct notifier_block freq_transition;
115#endif
116 struct davinci_i2c_platform_data *pdata;
117};
118
119/* default platform data to use if not supplied in the platform_device */
120static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
121 .bus_freq = 100,
122 .bus_delay = 0,
123};
124
125static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
126 int reg, u16 val)
127{
128 writew_relaxed(val, i2c_dev->base + reg);
129}
130
131static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
132{
133 return readw_relaxed(i2c_dev->base + reg);
134}
135
136/* Generate a pulse on the i2c clock pin. */
137static void davinci_i2c_clock_pulse(unsigned int scl_pin)
138{
139 u16 i;
140
141 if (scl_pin) {
142 /* Send high and low on the SCL line */
143 for (i = 0; i < 9; i++) {
144 gpio_set_value(scl_pin, 0);
145 udelay(20);
146 gpio_set_value(scl_pin, 1);
147 udelay(20);
148 }
149 }
150}
151
152/* This routine does i2c bus recovery as specified in the
153 * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
154 */
155static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev)
156{
157 u32 flag = 0;
158 struct davinci_i2c_platform_data *pdata = dev->pdata;
159
160 dev_err(dev->dev, "initiating i2c bus recovery\n");
161 /* Send NACK to the slave */
162 flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
163 flag |= DAVINCI_I2C_MDR_NACK;
164 /* write the data into mode register */
165 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
166 davinci_i2c_clock_pulse(pdata->scl_pin);
167 /* Send STOP */
168 flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
169 flag |= DAVINCI_I2C_MDR_STP;
170 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
171}
172
173static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
174 int val)
175{
176 u16 w;
177
178 w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
179 if (!val) /* put I2C into reset */
180 w &= ~DAVINCI_I2C_MDR_IRS;
181 else /* take I2C out of reset */
182 w |= DAVINCI_I2C_MDR_IRS;
183
184 davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
185}
186
187static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
188{
189 struct davinci_i2c_platform_data *pdata = dev->pdata;
190 u16 psc;
191 u32 clk;
192 u32 d;
193 u32 clkh;
194 u32 clkl;
195 u32 input_clock = clk_get_rate(dev->clk);
196
197 /* NOTE: I2C Clock divider programming info
198 * As per I2C specs the following formulas provide prescaler
199 * and low/high divider values
200 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
201 * module clk
202 *
203 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
204 *
205 * Thus,
206 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
207 *
208 * where if PSC == 0, d = 7,
209 * if PSC == 1, d = 6
210 * if PSC > 1 , d = 5
211 */
212
213 /* get minimum of 7 MHz clock, but max of 12 MHz */
214 psc = (input_clock / 7000000) - 1;
215 if ((input_clock / (psc + 1)) > 12000000)
216 psc++; /* better to run under spec than over */
217 d = (psc >= 2) ? 5 : 7 - psc;
218
219 clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
220 clkh = clk >> 1;
221 clkl = clk - clkh;
222
223 davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
224 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
225 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
226
227 dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
228}
229
230/*
231 * This function configures I2C and brings I2C out of reset.
232 * This function is called during I2C init function. This function
233 * also gets called if I2C encounters any errors.
234 */
235static int i2c_davinci_init(struct davinci_i2c_dev *dev)
236{
237 struct davinci_i2c_platform_data *pdata = dev->pdata;
238
239 /* put I2C into reset */
240 davinci_i2c_reset_ctrl(dev, 0);
241
242 /* compute clock dividers */
243 i2c_davinci_calc_clk_dividers(dev);
244
245 /* Respond at reserved "SMBus Host" slave address" (and zero);
246 * we seem to have no option to not respond...
247 */
248 davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
249
250 dev_dbg(dev->dev, "PSC = %d\n",
251 davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
252 dev_dbg(dev->dev, "CLKL = %d\n",
253 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
254 dev_dbg(dev->dev, "CLKH = %d\n",
255 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
256 dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
257 pdata->bus_freq, pdata->bus_delay);
258
259
260 /* Take the I2C module out of reset: */
261 davinci_i2c_reset_ctrl(dev, 1);
262
263 /* Enable interrupts */
264 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
265
266 return 0;
267}
268
269/*
270 * Waiting for bus not busy
271 */
272static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
273 char allow_sleep)
274{
275 unsigned long timeout;
276 static u16 to_cnt;
277
278 timeout = jiffies + dev->adapter.timeout;
279 while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
280 & DAVINCI_I2C_STR_BB) {
281 if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
282 if (time_after(jiffies, timeout)) {
283 dev_warn(dev->dev,
284 "timeout waiting for bus ready\n");
285 to_cnt++;
286 return -ETIMEDOUT;
287 } else {
288 to_cnt = 0;
289 davinci_i2c_recover_bus(dev);
290 i2c_davinci_init(dev);
291 }
292 }
293 if (allow_sleep)
294 schedule_timeout(1);
295 }
296
297 return 0;
298}
299
300/*
301 * Low level master read/write transaction. This function is called
302 * from i2c_davinci_xfer.
303 */
304static int
305i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
306{
307 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
308 struct davinci_i2c_platform_data *pdata = dev->pdata;
309 u32 flag;
310 u16 w;
311 int r;
312
313 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
314 if (pdata->bus_delay)
315 udelay(pdata->bus_delay);
316
317 /* set the slave address */
318 davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
319
320 dev->buf = msg->buf;
321 dev->buf_len = msg->len;
322 dev->stop = stop;
323
324 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
325
326 reinit_completion(&dev->cmd_complete);
327 dev->cmd_err = 0;
328
329 /* Take I2C out of reset and configure it as master */
330 flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
331
332 /* if the slave address is ten bit address, enable XA bit */
333 if (msg->flags & I2C_M_TEN)
334 flag |= DAVINCI_I2C_MDR_XA;
335 if (!(msg->flags & I2C_M_RD))
336 flag |= DAVINCI_I2C_MDR_TRX;
337 if (msg->len == 0)
338 flag |= DAVINCI_I2C_MDR_RM;
339
340 /* Enable receive or transmit interrupts */
341 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
342 if (msg->flags & I2C_M_RD)
343 w |= DAVINCI_I2C_IMR_RRDY;
344 else
345 w |= DAVINCI_I2C_IMR_XRDY;
346 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
347
348 dev->terminate = 0;
349
350 /*
351 * Write mode register first as needed for correct behaviour
352 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
353 * occurring before we have loaded DXR
354 */
355 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
356
357 /*
358 * First byte should be set here, not after interrupt,
359 * because transmit-data-ready interrupt can come before
360 * NACK-interrupt during sending of previous message and
361 * ICDXR may have wrong data
362 * It also saves us one interrupt, slightly faster
363 */
364 if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
365 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
366 dev->buf_len--;
367 }
368
369 /* Set STT to begin transmit now DXR is loaded */
370 flag |= DAVINCI_I2C_MDR_STT;
371 if (stop && msg->len != 0)
372 flag |= DAVINCI_I2C_MDR_STP;
373 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
374
375 r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
376 dev->adapter.timeout);
377 if (r == 0) {
378 dev_err(dev->dev, "controller timed out\n");
379 davinci_i2c_recover_bus(dev);
380 i2c_davinci_init(dev);
381 dev->buf_len = 0;
382 return -ETIMEDOUT;
383 }
384 if (dev->buf_len) {
385 /* This should be 0 if all bytes were transferred
386 * or dev->cmd_err denotes an error.
387 * A signal may have aborted the transfer.
388 */
389 if (r >= 0) {
390 dev_err(dev->dev, "abnormal termination buf_len=%i\n",
391 dev->buf_len);
392 r = -EREMOTEIO;
393 }
394 dev->terminate = 1;
395 wmb();
396 dev->buf_len = 0;
397 }
398 if (r < 0)
399 return r;
400
401 /* no error */
402 if (likely(!dev->cmd_err))
403 return msg->len;
404
405 /* We have an error */
406 if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
407 i2c_davinci_init(dev);
408 return -EIO;
409 }
410
411 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
412 if (msg->flags & I2C_M_IGNORE_NAK)
413 return msg->len;
414 if (stop) {
415 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
416 w |= DAVINCI_I2C_MDR_STP;
417 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
418 }
419 return -EREMOTEIO;
420 }
421 return -EIO;
422}
423
424/*
425 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
426 */
427static int
428i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
429{
430 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
431 int i;
432 int ret;
433
434 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
435
436 ret = i2c_davinci_wait_bus_not_busy(dev, 1);
437 if (ret < 0) {
438 dev_warn(dev->dev, "timeout waiting for bus ready\n");
439 return ret;
440 }
441
442 for (i = 0; i < num; i++) {
443 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
444 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
445 ret);
446 if (ret < 0)
447 return ret;
448 }
449
450#ifdef CONFIG_CPU_FREQ
451 complete(&dev->xfr_complete);
452#endif
453
454 return num;
455}
456
457static u32 i2c_davinci_func(struct i2c_adapter *adap)
458{
459 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
460}
461
462static void terminate_read(struct davinci_i2c_dev *dev)
463{
464 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
465 w |= DAVINCI_I2C_MDR_NACK;
466 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
467
468 /* Throw away data */
469 davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
470 if (!dev->terminate)
471 dev_err(dev->dev, "RDR IRQ while no data requested\n");
472}
473static void terminate_write(struct davinci_i2c_dev *dev)
474{
475 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
476 w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
477 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
478
479 if (!dev->terminate)
480 dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
481}
482
483/*
484 * Interrupt service routine. This gets called whenever an I2C interrupt
485 * occurs.
486 */
487static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
488{
489 struct davinci_i2c_dev *dev = dev_id;
490 u32 stat;
491 int count = 0;
492 u16 w;
493
494 while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
495 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
496 if (count++ == 100) {
497 dev_warn(dev->dev, "Too much work in one IRQ\n");
498 break;
499 }
500
501 switch (stat) {
502 case DAVINCI_I2C_IVR_AL:
503 /* Arbitration lost, must retry */
504 dev->cmd_err |= DAVINCI_I2C_STR_AL;
505 dev->buf_len = 0;
506 complete(&dev->cmd_complete);
507 break;
508
509 case DAVINCI_I2C_IVR_NACK:
510 dev->cmd_err |= DAVINCI_I2C_STR_NACK;
511 dev->buf_len = 0;
512 complete(&dev->cmd_complete);
513 break;
514
515 case DAVINCI_I2C_IVR_ARDY:
516 davinci_i2c_write_reg(dev,
517 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
518 if (((dev->buf_len == 0) && (dev->stop != 0)) ||
519 (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
520 w = davinci_i2c_read_reg(dev,
521 DAVINCI_I2C_MDR_REG);
522 w |= DAVINCI_I2C_MDR_STP;
523 davinci_i2c_write_reg(dev,
524 DAVINCI_I2C_MDR_REG, w);
525 }
526 complete(&dev->cmd_complete);
527 break;
528
529 case DAVINCI_I2C_IVR_RDR:
530 if (dev->buf_len) {
531 *dev->buf++ =
532 davinci_i2c_read_reg(dev,
533 DAVINCI_I2C_DRR_REG);
534 dev->buf_len--;
535 if (dev->buf_len)
536 continue;
537
538 davinci_i2c_write_reg(dev,
539 DAVINCI_I2C_STR_REG,
540 DAVINCI_I2C_IMR_RRDY);
541 } else {
542 /* signal can terminate transfer */
543 terminate_read(dev);
544 }
545 break;
546
547 case DAVINCI_I2C_IVR_XRDY:
548 if (dev->buf_len) {
549 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
550 *dev->buf++);
551 dev->buf_len--;
552 if (dev->buf_len)
553 continue;
554
555 w = davinci_i2c_read_reg(dev,
556 DAVINCI_I2C_IMR_REG);
557 w &= ~DAVINCI_I2C_IMR_XRDY;
558 davinci_i2c_write_reg(dev,
559 DAVINCI_I2C_IMR_REG,
560 w);
561 } else {
562 /* signal can terminate transfer */
563 terminate_write(dev);
564 }
565 break;
566
567 case DAVINCI_I2C_IVR_SCD:
568 davinci_i2c_write_reg(dev,
569 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
570 complete(&dev->cmd_complete);
571 break;
572
573 case DAVINCI_I2C_IVR_AAS:
574 dev_dbg(dev->dev, "Address as slave interrupt\n");
575 break;
576
577 default:
578 dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
579 break;
580 }
581 }
582
583 return count ? IRQ_HANDLED : IRQ_NONE;
584}
585
586#ifdef CONFIG_CPU_FREQ
587static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
588 unsigned long val, void *data)
589{
590 struct davinci_i2c_dev *dev;
591
592 dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
593 if (val == CPUFREQ_PRECHANGE) {
594 wait_for_completion(&dev->xfr_complete);
595 davinci_i2c_reset_ctrl(dev, 0);
596 } else if (val == CPUFREQ_POSTCHANGE) {
597 i2c_davinci_calc_clk_dividers(dev);
598 davinci_i2c_reset_ctrl(dev, 1);
599 }
600
601 return 0;
602}
603
604static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
605{
606 dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
607
608 return cpufreq_register_notifier(&dev->freq_transition,
609 CPUFREQ_TRANSITION_NOTIFIER);
610}
611
612static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
613{
614 cpufreq_unregister_notifier(&dev->freq_transition,
615 CPUFREQ_TRANSITION_NOTIFIER);
616}
617#else
618static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
619{
620 return 0;
621}
622
623static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
624{
625}
626#endif
627
628static struct i2c_algorithm i2c_davinci_algo = {
629 .master_xfer = i2c_davinci_xfer,
630 .functionality = i2c_davinci_func,
631};
632
633static const struct of_device_id davinci_i2c_of_match[] = {
634 {.compatible = "ti,davinci-i2c", },
635 {},
636};
637MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
638
639static int davinci_i2c_probe(struct platform_device *pdev)
640{
641 struct davinci_i2c_dev *dev;
642 struct i2c_adapter *adap;
643 struct resource *mem, *irq;
644 int r;
645
646 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
647 if (!irq) {
648 dev_err(&pdev->dev, "no irq resource?\n");
649 return -ENODEV;
650 }
651
652 dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
653 GFP_KERNEL);
654 if (!dev) {
655 dev_err(&pdev->dev, "Memory allocation failed\n");
656 return -ENOMEM;
657 }
658
659 init_completion(&dev->cmd_complete);
660#ifdef CONFIG_CPU_FREQ
661 init_completion(&dev->xfr_complete);
662#endif
663 dev->dev = &pdev->dev;
664 dev->irq = irq->start;
665 dev->pdata = dev_get_platdata(&pdev->dev);
666 platform_set_drvdata(pdev, dev);
667
668 if (!dev->pdata && pdev->dev.of_node) {
669 u32 prop;
670
671 dev->pdata = devm_kzalloc(&pdev->dev,
672 sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
673 if (!dev->pdata)
674 return -ENOMEM;
675
676 memcpy(dev->pdata, &davinci_i2c_platform_data_default,
677 sizeof(struct davinci_i2c_platform_data));
678 if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
679 &prop))
680 dev->pdata->bus_freq = prop / 1000;
681 } else if (!dev->pdata) {
682 dev->pdata = &davinci_i2c_platform_data_default;
683 }
684
685 dev->clk = devm_clk_get(&pdev->dev, NULL);
686 if (IS_ERR(dev->clk))
687 return -ENODEV;
688 clk_prepare_enable(dev->clk);
689
690 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
691 dev->base = devm_ioremap_resource(&pdev->dev, mem);
692 if (IS_ERR(dev->base)) {
693 r = PTR_ERR(dev->base);
694 goto err_unuse_clocks;
695 }
696
697 i2c_davinci_init(dev);
698
699 r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
700 pdev->name, dev);
701 if (r) {
702 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
703 goto err_unuse_clocks;
704 }
705
706 r = i2c_davinci_cpufreq_register(dev);
707 if (r) {
708 dev_err(&pdev->dev, "failed to register cpufreq\n");
709 goto err_unuse_clocks;
710 }
711
712 adap = &dev->adapter;
713 i2c_set_adapdata(adap, dev);
714 adap->owner = THIS_MODULE;
715 adap->class = I2C_CLASS_HWMON | I2C_CLASS_DEPRECATED;
716 strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
717 adap->algo = &i2c_davinci_algo;
718 adap->dev.parent = &pdev->dev;
719 adap->timeout = DAVINCI_I2C_TIMEOUT;
720 adap->dev.of_node = pdev->dev.of_node;
721
722 adap->nr = pdev->id;
723 r = i2c_add_numbered_adapter(adap);
724 if (r) {
725 dev_err(&pdev->dev, "failure adding adapter\n");
726 goto err_unuse_clocks;
727 }
728
729 return 0;
730
731err_unuse_clocks:
732 clk_disable_unprepare(dev->clk);
733 dev->clk = NULL;
734 return r;
735}
736
737static int davinci_i2c_remove(struct platform_device *pdev)
738{
739 struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
740
741 i2c_davinci_cpufreq_deregister(dev);
742
743 i2c_del_adapter(&dev->adapter);
744
745 clk_disable_unprepare(dev->clk);
746 dev->clk = NULL;
747
748 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
749
750 return 0;
751}
752
753#ifdef CONFIG_PM
754static int davinci_i2c_suspend(struct device *dev)
755{
756 struct platform_device *pdev = to_platform_device(dev);
757 struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
758
759 /* put I2C into reset */
760 davinci_i2c_reset_ctrl(i2c_dev, 0);
761 clk_disable_unprepare(i2c_dev->clk);
762
763 return 0;
764}
765
766static int davinci_i2c_resume(struct device *dev)
767{
768 struct platform_device *pdev = to_platform_device(dev);
769 struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
770
771 clk_prepare_enable(i2c_dev->clk);
772 /* take I2C out of reset */
773 davinci_i2c_reset_ctrl(i2c_dev, 1);
774
775 return 0;
776}
777
778static const struct dev_pm_ops davinci_i2c_pm = {
779 .suspend = davinci_i2c_suspend,
780 .resume = davinci_i2c_resume,
781};
782
783#define davinci_i2c_pm_ops (&davinci_i2c_pm)
784#else
785#define davinci_i2c_pm_ops NULL
786#endif
787
788/* work with hotplug and coldplug */
789MODULE_ALIAS("platform:i2c_davinci");
790
791static struct platform_driver davinci_i2c_driver = {
792 .probe = davinci_i2c_probe,
793 .remove = davinci_i2c_remove,
794 .driver = {
795 .name = "i2c_davinci",
796 .owner = THIS_MODULE,
797 .pm = davinci_i2c_pm_ops,
798 .of_match_table = davinci_i2c_of_match,
799 },
800};
801
802/* I2C may be needed to bring up other drivers */
803static int __init davinci_i2c_init_driver(void)
804{
805 return platform_driver_register(&davinci_i2c_driver);
806}
807subsys_initcall(davinci_i2c_init_driver);
808
809static void __exit davinci_i2c_exit_driver(void)
810{
811 platform_driver_unregister(&davinci_i2c_driver);
812}
813module_exit(davinci_i2c_exit_driver);
814
815MODULE_AUTHOR("Texas Instruments India");
816MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
817MODULE_LICENSE("GPL");