blob: edb643bf7a6dfce39f64b92ea786de2caab9964f [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13/*
14 * QUP driver for Qualcomm MSM platforms
15 *
16 */
17
18/* #define DEBUG */
19
20#include <linux/clk.h>
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/i2c.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/delay.h>
27#include <linux/io.h>
28#include <linux/mutex.h>
29#include <linux/timer.h>
30#include <linux/slab.h>
31#include <mach/board.h>
32#include <linux/slab.h>
33#include <linux/pm_runtime.h>
34#include <linux/gpio.h>
35
36MODULE_LICENSE("GPL v2");
37MODULE_VERSION("0.2");
38MODULE_ALIAS("platform:i2c_qup");
39
40/* QUP Registers */
41enum {
42 QUP_CONFIG = 0x0,
43 QUP_STATE = 0x4,
44 QUP_IO_MODE = 0x8,
45 QUP_SW_RESET = 0xC,
46 QUP_OPERATIONAL = 0x18,
47 QUP_ERROR_FLAGS = 0x1C,
48 QUP_ERROR_FLAGS_EN = 0x20,
49 QUP_MX_READ_CNT = 0x208,
50 QUP_MX_INPUT_CNT = 0x200,
51 QUP_MX_WR_CNT = 0x100,
52 QUP_OUT_DEBUG = 0x108,
53 QUP_OUT_FIFO_CNT = 0x10C,
54 QUP_OUT_FIFO_BASE = 0x110,
55 QUP_IN_READ_CUR = 0x20C,
56 QUP_IN_DEBUG = 0x210,
57 QUP_IN_FIFO_CNT = 0x214,
58 QUP_IN_FIFO_BASE = 0x218,
59 QUP_I2C_CLK_CTL = 0x400,
60 QUP_I2C_STATUS = 0x404,
61};
62
63/* QUP States and reset values */
64enum {
65 QUP_RESET_STATE = 0,
66 QUP_RUN_STATE = 1U,
67 QUP_STATE_MASK = 3U,
68 QUP_PAUSE_STATE = 3U,
69 QUP_STATE_VALID = 1U << 2,
70 QUP_I2C_MAST_GEN = 1U << 4,
71 QUP_OPERATIONAL_RESET = 0xFF0,
72 QUP_I2C_STATUS_RESET = 0xFFFFFC,
73};
74
75/* QUP OPERATIONAL FLAGS */
76enum {
77 QUP_OUT_SVC_FLAG = 1U << 8,
78 QUP_IN_SVC_FLAG = 1U << 9,
79 QUP_MX_INPUT_DONE = 1U << 11,
80};
81
82/* I2C mini core related values */
83enum {
84 I2C_MINI_CORE = 2U << 8,
85 I2C_N_VAL = 0xF,
86
87};
88
89/* Packing Unpacking words in FIFOs , and IO modes*/
90enum {
91 QUP_WR_BLK_MODE = 1U << 10,
92 QUP_RD_BLK_MODE = 1U << 12,
93 QUP_UNPACK_EN = 1U << 14,
94 QUP_PACK_EN = 1U << 15,
95};
96
97/* QUP tags */
98enum {
99 QUP_OUT_NOP = 0,
100 QUP_OUT_START = 1U << 8,
101 QUP_OUT_DATA = 2U << 8,
102 QUP_OUT_STOP = 3U << 8,
103 QUP_OUT_REC = 4U << 8,
104 QUP_IN_DATA = 5U << 8,
105 QUP_IN_STOP = 6U << 8,
106 QUP_IN_NACK = 7U << 8,
107};
108
109/* Status, Error flags */
110enum {
111 I2C_STATUS_WR_BUFFER_FULL = 1U << 0,
112 I2C_STATUS_BUS_ACTIVE = 1U << 8,
113 I2C_STATUS_ERROR_MASK = 0x38000FC,
114 QUP_I2C_NACK_FLAG = 1U << 3,
115 QUP_IN_NOT_EMPTY = 1U << 5,
116 QUP_STATUS_ERROR_FLAGS = 0x7C,
117};
118
119/* Master status clock states */
120enum {
121 I2C_CLK_RESET_BUSIDLE_STATE = 0,
122 I2C_CLK_FORCED_LOW_STATE = 5,
123};
124
125#define QUP_MAX_CLK_STATE_RETRIES 300
126
127static char const * const i2c_rsrcs[] = {"i2c_clk", "i2c_sda"};
128
129struct qup_i2c_dev {
130 struct device *dev;
131 void __iomem *base; /* virtual */
132 void __iomem *gsbi; /* virtual */
133 int in_irq;
134 int out_irq;
135 int err_irq;
136 int num_irqs;
137 struct clk *clk;
138 struct clk *pclk;
139 struct i2c_adapter adapter;
140
141 struct i2c_msg *msg;
142 int pos;
143 int cnt;
144 int err;
145 int mode;
146 int clk_ctl;
147 int one_bit_t;
148 int out_fifo_sz;
149 int in_fifo_sz;
150 int out_blk_sz;
151 int in_blk_sz;
152 int wr_sz;
153 struct msm_i2c_platform_data *pdata;
154 int suspended;
155 int clk_state;
156 struct timer_list pwr_timer;
157 struct mutex mlock;
158 void *complete;
159 int i2c_gpios[ARRAY_SIZE(i2c_rsrcs)];
160};
161
162#ifdef DEBUG
163static void
164qup_print_status(struct qup_i2c_dev *dev)
165{
166 uint32_t val;
167 val = readl_relaxed(dev->base+QUP_CONFIG);
168 dev_dbg(dev->dev, "Qup config is :0x%x\n", val);
169 val = readl_relaxed(dev->base+QUP_STATE);
170 dev_dbg(dev->dev, "Qup state is :0x%x\n", val);
171 val = readl_relaxed(dev->base+QUP_IO_MODE);
172 dev_dbg(dev->dev, "Qup mode is :0x%x\n", val);
173}
174#else
175static inline void qup_print_status(struct qup_i2c_dev *dev)
176{
177}
178#endif
179
180static irqreturn_t
181qup_i2c_interrupt(int irq, void *devid)
182{
183 struct qup_i2c_dev *dev = devid;
184 uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS);
185 uint32_t status1 = readl_relaxed(dev->base + QUP_ERROR_FLAGS);
186 uint32_t op_flgs = readl_relaxed(dev->base + QUP_OPERATIONAL);
187 int err = 0;
188
189 if (!dev->msg || !dev->complete) {
190 /* Clear Error interrupt if it's a level triggered interrupt*/
191 if (dev->num_irqs == 1) {
192 writel_relaxed(QUP_RESET_STATE, dev->base+QUP_STATE);
193 /* Ensure that state is written before ISR exits */
194 mb();
195 }
196 return IRQ_HANDLED;
197 }
198
199 if (status & I2C_STATUS_ERROR_MASK) {
200 dev_err(dev->dev, "QUP: I2C status flags :0x%x, irq:%d\n",
201 status, irq);
202 err = status;
203 /* Clear Error interrupt if it's a level triggered interrupt*/
204 if (dev->num_irqs == 1) {
205 writel_relaxed(QUP_RESET_STATE, dev->base+QUP_STATE);
206 /* Ensure that state is written before ISR exits */
207 mb();
208 }
209 goto intr_done;
210 }
211
212 if (status1 & 0x7F) {
213 dev_err(dev->dev, "QUP: QUP status flags :0x%x\n", status1);
214 err = -status1;
215 /* Clear Error interrupt if it's a level triggered interrupt*/
216 if (dev->num_irqs == 1) {
217 writel_relaxed((status1 & QUP_STATUS_ERROR_FLAGS),
218 dev->base + QUP_ERROR_FLAGS);
219 /* Ensure that error flags are cleared before ISR
220 * exits
221 */
222 mb();
223 }
224 goto intr_done;
225 }
226
227 if ((dev->num_irqs == 3) && (dev->msg->flags == I2C_M_RD)
228 && (irq == dev->out_irq))
229 return IRQ_HANDLED;
230 if (op_flgs & QUP_OUT_SVC_FLAG) {
231 writel_relaxed(QUP_OUT_SVC_FLAG, dev->base + QUP_OPERATIONAL);
232 /* Ensure that service flag is acknowledged before ISR exits */
233 mb();
234 }
235 if (dev->msg->flags == I2C_M_RD) {
236 if ((op_flgs & QUP_MX_INPUT_DONE) ||
237 (op_flgs & QUP_IN_SVC_FLAG)) {
238 writel_relaxed(QUP_IN_SVC_FLAG, dev->base
239 + QUP_OPERATIONAL);
240 /* Ensure that service flag is acknowledged before ISR
241 * exits
242 */
243 mb();
244 } else
245 return IRQ_HANDLED;
246 }
247
248intr_done:
249 dev_dbg(dev->dev, "QUP intr= %d, i2c status=0x%x, qup status = 0x%x\n",
250 irq, status, status1);
251 qup_print_status(dev);
252 dev->err = err;
253 complete(dev->complete);
254 return IRQ_HANDLED;
255}
256
257static void
258qup_i2c_pwr_mgmt(struct qup_i2c_dev *dev, unsigned int state)
259{
260 dev->clk_state = state;
261 if (state != 0) {
262 clk_enable(dev->clk);
263 if (dev->pclk)
264 clk_enable(dev->pclk);
265 } else {
266 clk_disable(dev->clk);
267 if (dev->pclk)
268 clk_disable(dev->pclk);
269 }
270}
271
272static void
273qup_i2c_pwr_timer(unsigned long data)
274{
275 struct qup_i2c_dev *dev = (struct qup_i2c_dev *) data;
276 dev_dbg(dev->dev, "QUP_Power: Inactivity based power management\n");
277 if (dev->clk_state == 1)
278 qup_i2c_pwr_mgmt(dev, 0);
279}
280
281static int
282qup_i2c_poll_writeready(struct qup_i2c_dev *dev, int rem)
283{
284 uint32_t retries = 0;
285
286 while (retries != 2000) {
287 uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS);
288
289 if (!(status & I2C_STATUS_WR_BUFFER_FULL)) {
290 if (((dev->msg->flags & I2C_M_RD) || (rem == 0)) &&
291 !(status & I2C_STATUS_BUS_ACTIVE))
292 return 0;
293 else if ((dev->msg->flags == 0) && (rem > 0))
294 return 0;
295 else /* 1-bit delay before we check for bus busy */
296 udelay(dev->one_bit_t);
297 }
298 if (retries++ == 1000)
299 udelay(100);
300 }
301 qup_print_status(dev);
302 return -ETIMEDOUT;
303}
304
305static int qup_i2c_poll_clock_ready(struct qup_i2c_dev *dev)
306{
307 uint32_t retries = 0;
308
309 /*
310 * Wait for the clock state to transition to either IDLE or FORCED
311 * LOW. This will usually happen within one cycle of the i2c clock.
312 */
313
314 while (retries++ < QUP_MAX_CLK_STATE_RETRIES) {
315 uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS);
316 uint32_t clk_state = (status >> 13) & 0x7;
317
318 if (clk_state == I2C_CLK_RESET_BUSIDLE_STATE ||
319 clk_state == I2C_CLK_FORCED_LOW_STATE)
320 return 0;
321 /* 1-bit delay before we check again */
322 udelay(dev->one_bit_t);
323 }
324
325 dev_err(dev->dev, "Error waiting for clk ready\n");
326 return -ETIMEDOUT;
327}
328
329static int
330qup_i2c_poll_state(struct qup_i2c_dev *dev, uint32_t state)
331{
332 uint32_t retries = 0;
333
334 dev_dbg(dev->dev, "Polling Status for state:0x%x\n", state);
335
336 while (retries != 2000) {
337 uint32_t status = readl_relaxed(dev->base + QUP_STATE);
338
339 if ((status & (QUP_STATE_VALID | state)) ==
340 (QUP_STATE_VALID | state))
341 return 0;
342 else if (retries++ == 1000)
343 udelay(100);
344 }
345 return -ETIMEDOUT;
346}
347
348static inline int qup_i2c_request_gpios(struct qup_i2c_dev *dev)
349{
350 int i;
351 int result = 0;
352
353 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
354 if (dev->i2c_gpios[i] >= 0) {
355 result = gpio_request(dev->i2c_gpios[i], i2c_rsrcs[i]);
356 if (result) {
357 dev_err(dev->dev,
358 "gpio_request for pin %d failed\
359 with error %d\n", dev->i2c_gpios[i],
360 result);
361 goto error;
362 }
363 }
364 }
365 return 0;
366
367error:
368 for (; --i >= 0;) {
369 if (dev->i2c_gpios[i] >= 0)
370 gpio_free(dev->i2c_gpios[i]);
371 }
372 return result;
373}
374
375static inline void qup_i2c_free_gpios(struct qup_i2c_dev *dev)
376{
377 int i;
378
379 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
380 if (dev->i2c_gpios[i] >= 0)
381 gpio_free(dev->i2c_gpios[i]);
382 }
383}
384
385#ifdef DEBUG
386static void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val,
387 uint32_t addr, int rdwr)
388{
389 if (rdwr)
390 dev_dbg(dev->dev, "RD:Wrote 0x%x to out_ff:0x%x\n", val, addr);
391 else
392 dev_dbg(dev->dev, "WR:Wrote 0x%x to out_ff:0x%x\n", val, addr);
393}
394#else
395static inline void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val,
396 uint32_t addr, int rdwr)
397{
398}
399#endif
400
401static void
402qup_issue_read(struct qup_i2c_dev *dev, struct i2c_msg *msg, int *idx,
403 uint32_t carry_over)
404{
405 uint16_t addr = (msg->addr << 1) | 1;
406 /* QUP limit 256 bytes per read. By HW design, 0 in the 8-bit field
407 * is treated as 256 byte read.
408 */
409 uint16_t rd_len = ((dev->cnt == 256) ? 0 : dev->cnt);
410
411 if (*idx % 4) {
412 writel_relaxed(carry_over | ((QUP_OUT_START | addr) << 16),
413 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx-2)); */
414
415 qup_verify_fifo(dev, carry_over |
416 ((QUP_OUT_START | addr) << 16), (uint32_t)dev->base
417 + QUP_OUT_FIFO_BASE + (*idx - 2), 1);
418 writel_relaxed((QUP_OUT_REC | rd_len),
419 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx+2)); */
420
421 qup_verify_fifo(dev, (QUP_OUT_REC | rd_len),
422 (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx + 2), 1);
423 } else {
424 writel_relaxed(((QUP_OUT_REC | rd_len) << 16)
425 | QUP_OUT_START | addr,
426 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx)); */
427
428 qup_verify_fifo(dev, QUP_OUT_REC << 16 | rd_len << 16 |
429 QUP_OUT_START | addr,
430 (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx), 1);
431 }
432 *idx += 4;
433}
434
435static void
436qup_issue_write(struct qup_i2c_dev *dev, struct i2c_msg *msg, int rem,
437 int *idx, uint32_t *carry_over)
438{
439 int entries = dev->cnt;
440 int empty_sl = dev->wr_sz - ((*idx) >> 1);
441 int i = 0;
442 uint32_t val = 0;
443 uint32_t last_entry = 0;
444 uint16_t addr = msg->addr << 1;
445
446 if (dev->pos == 0) {
447 if (*idx % 4) {
448 writel_relaxed(*carry_over | ((QUP_OUT_START |
449 addr) << 16),
450 dev->base + QUP_OUT_FIFO_BASE);
451
452 qup_verify_fifo(dev, *carry_over | QUP_OUT_DATA << 16 |
453 addr << 16, (uint32_t)dev->base +
454 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
455 } else
456 val = QUP_OUT_START | addr;
457 *idx += 2;
458 i++;
459 entries++;
460 } else {
461 /* Avoid setp time issue by adding 1 NOP when number of bytes
462 * are more than FIFO/BLOCK size. setup time issue can't appear
463 * otherwise since next byte to be written will always be ready
464 */
465 val = (QUP_OUT_NOP | 1);
466 *idx += 2;
467 i++;
468 entries++;
469 }
470 if (entries > empty_sl)
471 entries = empty_sl;
472
473 for (; i < (entries - 1); i++) {
474 if (*idx % 4) {
475 writel_relaxed(val | ((QUP_OUT_DATA |
476 msg->buf[dev->pos]) << 16),
477 dev->base + QUP_OUT_FIFO_BASE);
478
479 qup_verify_fifo(dev, val | QUP_OUT_DATA << 16 |
480 msg->buf[dev->pos] << 16, (uint32_t)dev->base +
481 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
482 } else
483 val = QUP_OUT_DATA | msg->buf[dev->pos];
484 (*idx) += 2;
485 dev->pos++;
486 }
487 if (dev->pos < (msg->len - 1))
488 last_entry = QUP_OUT_DATA;
489 else if (rem > 1) /* not last array entry */
490 last_entry = QUP_OUT_DATA;
491 else
492 last_entry = QUP_OUT_STOP;
493 if ((*idx % 4) == 0) {
494 /*
495 * If read-start and read-command end up in different fifos, it
496 * may result in extra-byte being read due to extra-read cycle.
497 * Avoid that by inserting NOP as the last entry of fifo only
498 * if write command(s) leave 1 space in fifo.
499 */
500 if (rem > 1) {
501 struct i2c_msg *next = msg + 1;
502 if (next->addr == msg->addr && (next->flags | I2C_M_RD)
503 && *idx == ((dev->wr_sz*2) - 4)) {
504 writel_relaxed(((last_entry |
505 msg->buf[dev->pos]) |
506 ((1 | QUP_OUT_NOP) << 16)), dev->base +
507 QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
508
509 qup_verify_fifo(dev,
510 ((last_entry | msg->buf[dev->pos]) |
511 ((1 | QUP_OUT_NOP) << 16)),
512 (uint32_t)dev->base +
513 QUP_OUT_FIFO_BASE + (*idx), 0);
514 *idx += 2;
515 } else if (next->flags == 0 && dev->pos == msg->len - 1
516 && *idx < (dev->wr_sz*2)) {
517 /* Last byte of an intermittent write */
518 writel_relaxed((last_entry |
519 msg->buf[dev->pos]),
520 dev->base + QUP_OUT_FIFO_BASE);
521
522 qup_verify_fifo(dev,
523 last_entry | msg->buf[dev->pos],
524 (uint32_t)dev->base +
525 QUP_OUT_FIFO_BASE + (*idx), 0);
526 *idx += 2;
527 } else
528 *carry_over = (last_entry | msg->buf[dev->pos]);
529 } else {
530 writel_relaxed((last_entry | msg->buf[dev->pos]),
531 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
532
533 qup_verify_fifo(dev, last_entry | msg->buf[dev->pos],
534 (uint32_t)dev->base + QUP_OUT_FIFO_BASE +
535 (*idx), 0);
536 }
537 } else {
538 writel_relaxed(val | ((last_entry | msg->buf[dev->pos]) << 16),
539 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
540
541 qup_verify_fifo(dev, val | (last_entry << 16) |
542 (msg->buf[dev->pos] << 16), (uint32_t)dev->base +
543 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
544 }
545
546 *idx += 2;
547 dev->pos++;
548 dev->cnt = msg->len - dev->pos;
549}
550
551static int
552qup_update_state(struct qup_i2c_dev *dev, uint32_t state)
553{
554 if (qup_i2c_poll_state(dev, 0) != 0)
555 return -EIO;
556 writel_relaxed(state, dev->base + QUP_STATE);
557 if (qup_i2c_poll_state(dev, state) != 0)
558 return -EIO;
559 return 0;
560}
561
562static void
563qup_set_read_mode(struct qup_i2c_dev *dev, int rd_len)
564{
565 uint32_t wr_mode = (dev->wr_sz < dev->out_fifo_sz) ?
566 QUP_WR_BLK_MODE : 0;
567 if (rd_len > 256) {
568 dev_dbg(dev->dev, "HW limit: Breaking reads in chunk of 256\n");
569 rd_len = 256;
570 }
571 if (rd_len <= dev->in_fifo_sz) {
572 writel_relaxed(wr_mode | QUP_PACK_EN | QUP_UNPACK_EN,
573 dev->base + QUP_IO_MODE);
574 writel_relaxed(rd_len, dev->base + QUP_MX_READ_CNT);
575 } else {
576 writel_relaxed(wr_mode | QUP_RD_BLK_MODE |
577 QUP_PACK_EN | QUP_UNPACK_EN, dev->base + QUP_IO_MODE);
578 writel_relaxed(rd_len, dev->base + QUP_MX_INPUT_CNT);
579 }
580}
581
582static int
583qup_set_wr_mode(struct qup_i2c_dev *dev, int rem)
584{
585 int total_len = 0;
586 int ret = 0;
587 if (dev->msg->len >= (dev->out_fifo_sz - 1)) {
588 total_len = dev->msg->len + 1 +
589 (dev->msg->len/(dev->out_blk_sz-1));
590 writel_relaxed(QUP_WR_BLK_MODE | QUP_PACK_EN | QUP_UNPACK_EN,
591 dev->base + QUP_IO_MODE);
592 dev->wr_sz = dev->out_blk_sz;
593 } else
594 writel_relaxed(QUP_PACK_EN | QUP_UNPACK_EN,
595 dev->base + QUP_IO_MODE);
596
597 if (rem > 1) {
598 struct i2c_msg *next = dev->msg + 1;
599 if (next->addr == dev->msg->addr &&
600 next->flags == I2C_M_RD) {
601 qup_set_read_mode(dev, next->len);
602 /* make sure read start & read command are in 1 blk */
603 if ((total_len % dev->out_blk_sz) ==
604 (dev->out_blk_sz - 1))
605 total_len += 3;
606 else
607 total_len += 2;
608 }
609 }
610 /* WRITE COUNT register valid/used only in block mode */
611 if (dev->wr_sz == dev->out_blk_sz)
612 writel_relaxed(total_len, dev->base + QUP_MX_WR_CNT);
613 return ret;
614}
615
616static int
617qup_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
618{
619 DECLARE_COMPLETION_ONSTACK(complete);
620 struct qup_i2c_dev *dev = i2c_get_adapdata(adap);
621 int ret;
622 int rem = num;
623 long timeout;
624 int err;
625
626 del_timer_sync(&dev->pwr_timer);
627 mutex_lock(&dev->mlock);
628
629 if (dev->suspended) {
630 mutex_unlock(&dev->mlock);
631 return -EIO;
632 }
633
634 if (dev->clk_state == 0) {
635 if (dev->clk_ctl == 0) {
636 if (dev->pdata->src_clk_rate > 0)
637 clk_set_rate(dev->clk,
638 dev->pdata->src_clk_rate);
639 else
640 dev->pdata->src_clk_rate = 19200000;
641 }
642 qup_i2c_pwr_mgmt(dev, 1);
643 }
644 /* Initialize QUP registers during first transfer */
645 if (dev->clk_ctl == 0) {
646 int fs_div;
647 int hs_div;
648 uint32_t fifo_reg;
649
650 if (dev->gsbi) {
651 writel_relaxed(0x2 << 4, dev->gsbi);
652 /* GSBI memory is not in the same 1K region as other
653 * QUP registers. mb() here ensures that the GSBI
654 * register is updated in correct order and that the
655 * write has gone through before programming QUP core
656 * registers
657 */
658 mb();
659 }
660
661 fs_div = ((dev->pdata->src_clk_rate
662 / dev->pdata->clk_freq) / 2) - 3;
663 hs_div = 3;
664 dev->clk_ctl = ((hs_div & 0x7) << 8) | (fs_div & 0xff);
665 fifo_reg = readl_relaxed(dev->base + QUP_IO_MODE);
666 if (fifo_reg & 0x3)
667 dev->out_blk_sz = (fifo_reg & 0x3) * 16;
668 else
669 dev->out_blk_sz = 16;
670 if (fifo_reg & 0x60)
671 dev->in_blk_sz = ((fifo_reg & 0x60) >> 5) * 16;
672 else
673 dev->in_blk_sz = 16;
674 /*
675 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
676 * associated with each byte written/received
677 */
678 dev->out_blk_sz /= 2;
679 dev->in_blk_sz /= 2;
680 dev->out_fifo_sz = dev->out_blk_sz *
681 (2 << ((fifo_reg & 0x1C) >> 2));
682 dev->in_fifo_sz = dev->in_blk_sz *
683 (2 << ((fifo_reg & 0x380) >> 7));
684 dev_dbg(dev->dev, "QUP IN:bl:%d, ff:%d, OUT:bl:%d, ff:%d\n",
685 dev->in_blk_sz, dev->in_fifo_sz,
686 dev->out_blk_sz, dev->out_fifo_sz);
687 }
688
689 writel_relaxed(1, dev->base + QUP_SW_RESET);
690 ret = qup_i2c_poll_state(dev, QUP_RESET_STATE);
691 if (ret) {
692 dev_err(dev->dev, "QUP Busy:Trying to recover\n");
693 goto out_err;
694 }
695
696 if (dev->num_irqs == 3) {
697 enable_irq(dev->in_irq);
698 enable_irq(dev->out_irq);
699 }
700 enable_irq(dev->err_irq);
701
702 /* Initialize QUP registers */
703 writel_relaxed(0, dev->base + QUP_CONFIG);
704 writel_relaxed(QUP_OPERATIONAL_RESET, dev->base + QUP_OPERATIONAL);
705 writel_relaxed(QUP_STATUS_ERROR_FLAGS, dev->base + QUP_ERROR_FLAGS_EN);
706
707 writel_relaxed(I2C_MINI_CORE | I2C_N_VAL, dev->base + QUP_CONFIG);
708
709 /* Initialize I2C mini core registers */
710 writel_relaxed(0, dev->base + QUP_I2C_CLK_CTL);
711 writel_relaxed(QUP_I2C_STATUS_RESET, dev->base + QUP_I2C_STATUS);
712
713 while (rem) {
714 bool filled = false;
715
716 dev->cnt = msgs->len - dev->pos;
717 dev->msg = msgs;
718
719 dev->wr_sz = dev->out_fifo_sz;
720 dev->err = 0;
721 dev->complete = &complete;
722
723 if (qup_i2c_poll_state(dev, QUP_I2C_MAST_GEN) != 0) {
724 ret = -EIO;
725 goto out_err;
726 }
727
728 qup_print_status(dev);
729 /* HW limits Read upto 256 bytes in 1 read without stop */
730 if (dev->msg->flags & I2C_M_RD) {
731 qup_set_read_mode(dev, dev->cnt);
732 if (dev->cnt > 256)
733 dev->cnt = 256;
734 } else {
735 ret = qup_set_wr_mode(dev, rem);
736 if (ret != 0)
737 goto out_err;
738 /* Don't fill block till we get interrupt */
739 if (dev->wr_sz == dev->out_blk_sz)
740 filled = true;
741 }
742
743 err = qup_update_state(dev, QUP_RUN_STATE);
744 if (err < 0) {
745 ret = err;
746 goto out_err;
747 }
748
749 qup_print_status(dev);
750 writel_relaxed(dev->clk_ctl, dev->base + QUP_I2C_CLK_CTL);
751 /* CLK_CTL register is not in the same 1K region as other QUP
752 * registers. Ensure that clock control is written before
753 * programming other QUP registers
754 */
755 mb();
756
757 do {
758 int idx = 0;
759 uint32_t carry_over = 0;
760
761 /* Transition to PAUSE state only possible from RUN */
762 err = qup_update_state(dev, QUP_PAUSE_STATE);
763 if (err < 0) {
764 ret = err;
765 goto out_err;
766 }
767
768 qup_print_status(dev);
769 /* This operation is Write, check the next operation
770 * and decide mode
771 */
772 while (filled == false) {
773 if ((msgs->flags & I2C_M_RD))
774 qup_issue_read(dev, msgs, &idx,
775 carry_over);
776 else if (!(msgs->flags & I2C_M_RD))
777 qup_issue_write(dev, msgs, rem, &idx,
778 &carry_over);
779 if (idx >= (dev->wr_sz << 1))
780 filled = true;
781 /* Start new message */
782 if (filled == false) {
783 if (msgs->flags & I2C_M_RD)
784 filled = true;
785 else if (rem > 1) {
786 /* Only combine operations with
787 * same address
788 */
789 struct i2c_msg *next = msgs + 1;
790 if (next->addr != msgs->addr)
791 filled = true;
792 else {
793 rem--;
794 msgs++;
795 dev->msg = msgs;
796 dev->pos = 0;
797 dev->cnt = msgs->len;
798 if (msgs->len > 256)
799 dev->cnt = 256;
800 }
801 } else
802 filled = true;
803 }
804 }
805 err = qup_update_state(dev, QUP_RUN_STATE);
806 if (err < 0) {
807 ret = err;
808 goto out_err;
809 }
810 dev_dbg(dev->dev, "idx:%d, rem:%d, num:%d, mode:%d\n",
811 idx, rem, num, dev->mode);
812
813 qup_print_status(dev);
814 timeout = wait_for_completion_timeout(&complete, HZ);
815 if (!timeout) {
816 uint32_t istatus = readl_relaxed(dev->base +
817 QUP_I2C_STATUS);
818 uint32_t qstatus = readl_relaxed(dev->base +
819 QUP_ERROR_FLAGS);
820 uint32_t op_flgs = readl_relaxed(dev->base +
821 QUP_OPERATIONAL);
822
823 dev_err(dev->dev, "Transaction timed out\n");
824 dev_err(dev->dev, "I2C Status: %x\n", istatus);
825 dev_err(dev->dev, "QUP Status: %x\n", qstatus);
826 dev_err(dev->dev, "OP Flags: %x\n", op_flgs);
827 writel_relaxed(1, dev->base + QUP_SW_RESET);
828 /* Make sure that the write has gone through
829 * before returning from the function
830 */
831 mb();
832 ret = -ETIMEDOUT;
833 goto out_err;
834 }
835 if (dev->err) {
836 if (dev->err > 0 &&
837 dev->err & QUP_I2C_NACK_FLAG)
838 dev_err(dev->dev,
839 "I2C slave addr:0x%x not connected\n",
840 dev->msg->addr);
841 else if (dev->err < 0) {
842 dev_err(dev->dev,
843 "QUP data xfer error %d\n", dev->err);
844 ret = dev->err;
845 goto out_err;
846 }
847 ret = -dev->err;
848 goto out_err;
849 }
850 if (dev->msg->flags & I2C_M_RD) {
851 int i;
852 uint32_t dval = 0;
853 for (i = 0; dev->pos < dev->msg->len; i++,
854 dev->pos++) {
855 uint32_t rd_status =
856 readl_relaxed(dev->base
857 + QUP_OPERATIONAL);
858 if (i % 2 == 0) {
859 if ((rd_status &
860 QUP_IN_NOT_EMPTY) == 0)
861 break;
862 dval = readl_relaxed(dev->base +
863 QUP_IN_FIFO_BASE);
864 dev->msg->buf[dev->pos] =
865 dval & 0xFF;
866 } else
867 dev->msg->buf[dev->pos] =
868 ((dval & 0xFF0000) >>
869 16);
870 }
871 dev->cnt -= i;
872 } else
873 filled = false; /* refill output FIFO */
874 dev_dbg(dev->dev, "pos:%d, len:%d, cnt:%d\n",
875 dev->pos, msgs->len, dev->cnt);
876 } while (dev->cnt > 0);
877 if (dev->cnt == 0) {
878 if (msgs->len == dev->pos) {
879 rem--;
880 msgs++;
881 dev->pos = 0;
882 }
883 if (rem) {
884 err = qup_i2c_poll_clock_ready(dev);
885 if (err < 0) {
886 ret = err;
887 goto out_err;
888 }
889 err = qup_update_state(dev, QUP_RESET_STATE);
890 if (err < 0) {
891 ret = err;
892 goto out_err;
893 }
894 }
895 }
896 /* Wait for I2C bus to be idle */
897 ret = qup_i2c_poll_writeready(dev, rem);
898 if (ret) {
899 dev_err(dev->dev,
900 "Error waiting for write ready\n");
901 goto out_err;
902 }
903 }
904
905 ret = num;
906 out_err:
907 disable_irq(dev->err_irq);
908 if (dev->num_irqs == 3) {
909 disable_irq(dev->in_irq);
910 disable_irq(dev->out_irq);
911 }
912 dev->complete = NULL;
913 dev->msg = NULL;
914 dev->pos = 0;
915 dev->err = 0;
916 dev->cnt = 0;
917 dev->pwr_timer.expires = jiffies + 3*HZ;
918 add_timer(&dev->pwr_timer);
919 mutex_unlock(&dev->mlock);
920 return ret;
921}
922
923static u32
924qup_i2c_func(struct i2c_adapter *adap)
925{
926 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
927}
928
929static const struct i2c_algorithm qup_i2c_algo = {
930 .master_xfer = qup_i2c_xfer,
931 .functionality = qup_i2c_func,
932};
933
934static int __devinit
935qup_i2c_probe(struct platform_device *pdev)
936{
937 struct qup_i2c_dev *dev;
938 struct resource *qup_mem, *gsbi_mem, *qup_io, *gsbi_io, *res;
939 struct resource *in_irq, *out_irq, *err_irq;
940 struct clk *clk, *pclk;
941 int ret = 0;
942 int i;
943 struct msm_i2c_platform_data *pdata;
944 const char *qup_apps_clk_name = "qup_clk";
945
946 gsbi_mem = NULL;
947 dev_dbg(&pdev->dev, "qup_i2c_probe\n");
948
949 pdata = pdev->dev.platform_data;
950 if (!pdata) {
951 dev_err(&pdev->dev, "platform data not initialized\n");
952 return -ENOSYS;
953 }
954 qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
955 "qup_phys_addr");
956 if (!qup_mem) {
957 dev_err(&pdev->dev, "no qup mem resource?\n");
958 return -ENODEV;
959 }
960
961 /*
962 * We only have 1 interrupt for new hardware targets and in_irq,
963 * out_irq will be NULL for those platforms
964 */
965 in_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
966 "qup_in_intr");
967
968 out_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
969 "qup_out_intr");
970
971 err_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
972 "qup_err_intr");
973 if (!err_irq) {
974 dev_err(&pdev->dev, "no error irq resource?\n");
975 return -ENODEV;
976 }
977
978 qup_io = request_mem_region(qup_mem->start, resource_size(qup_mem),
979 pdev->name);
980 if (!qup_io) {
981 dev_err(&pdev->dev, "QUP region already claimed\n");
982 return -EBUSY;
983 }
984 if (!pdata->use_gsbi_shared_mode) {
985 gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
986 "gsbi_qup_i2c_addr");
987 if (!gsbi_mem) {
988 dev_err(&pdev->dev, "no gsbi mem resource?\n");
989 return -ENODEV;
990 }
991 gsbi_io = request_mem_region(gsbi_mem->start,
992 resource_size(gsbi_mem),
993 pdev->name);
994 if (!gsbi_io) {
995 dev_err(&pdev->dev, "GSBI region already claimed\n");
996 return -EBUSY;
997 }
998 }
999
1000 if (pdata->clk != NULL)
1001 qup_apps_clk_name = pdata->clk;
1002
1003 clk = clk_get(&pdev->dev, qup_apps_clk_name);
1004 if (IS_ERR(clk)) {
1005 dev_err(&pdev->dev, "Could not get clock\n");
1006 ret = PTR_ERR(clk);
1007 goto err_clk_get_failed;
1008 }
1009
1010 if (pdata->pclk != NULL) {
1011 pclk = clk_get(&pdev->dev, pdata->pclk);
1012 if (IS_ERR(pclk)) {
1013 dev_err(&pdev->dev, "Could not get pclock\n");
1014 ret = PTR_ERR(pclk);
1015 clk_put(clk);
1016 goto err_clk_get_failed;
1017 }
1018 } else
1019 pclk = NULL;
1020
1021 if (!(pdata->msm_i2c_config_gpio)) {
1022 dev_err(&pdev->dev, "config_gpio function not initialized\n");
1023 ret = -ENOSYS;
1024 goto err_config_failed;
1025 }
1026
1027 /* We support frequencies upto FAST Mode(400KHz) */
1028 if (pdata->clk_freq <= 0 ||
1029 pdata->clk_freq > 400000) {
1030 dev_err(&pdev->dev, "clock frequency not supported\n");
1031 ret = -EIO;
1032 goto err_config_failed;
1033 }
1034
1035 dev = kzalloc(sizeof(struct qup_i2c_dev), GFP_KERNEL);
1036 if (!dev) {
1037 ret = -ENOMEM;
1038 goto err_alloc_dev_failed;
1039 }
1040
1041 dev->dev = &pdev->dev;
1042 if (in_irq)
1043 dev->in_irq = in_irq->start;
1044 if (out_irq)
1045 dev->out_irq = out_irq->start;
1046 dev->err_irq = err_irq->start;
1047 if (in_irq && out_irq)
1048 dev->num_irqs = 3;
1049 else
1050 dev->num_irqs = 1;
1051 dev->clk = clk;
1052 dev->pclk = pclk;
1053 dev->base = ioremap(qup_mem->start, resource_size(qup_mem));
1054 if (!dev->base) {
1055 ret = -ENOMEM;
1056 goto err_ioremap_failed;
1057 }
1058
1059 /* Configure GSBI block to use I2C functionality */
1060 if (gsbi_mem) {
1061 dev->gsbi = ioremap(gsbi_mem->start, resource_size(gsbi_mem));
1062 if (!dev->gsbi) {
1063 ret = -ENOMEM;
1064 goto err_gsbi_failed;
1065 }
1066 }
1067
1068 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
1069 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
1070 i2c_rsrcs[i]);
1071 dev->i2c_gpios[i] = res ? res->start : -1;
1072 }
1073
1074 ret = qup_i2c_request_gpios(dev);
1075 if (ret)
1076 goto err_request_gpio_failed;
1077
1078 platform_set_drvdata(pdev, dev);
1079
1080 dev->one_bit_t = USEC_PER_SEC/pdata->clk_freq;
1081 dev->pdata = pdata;
1082 dev->clk_ctl = 0;
1083 dev->pos = 0;
1084
1085 /*
1086 * We use num_irqs to also indicate if we got 3 interrupts or just 1.
1087 * If we have just 1, we use err_irq as the general purpose irq
1088 * and handle the changes in ISR accordingly
1089 * Per Hardware guidelines, if we have 3 interrupts, they are always
1090 * edge triggering, and if we have 1, it's always level-triggering
1091 */
1092 if (dev->num_irqs == 3) {
1093 ret = request_irq(dev->in_irq, qup_i2c_interrupt,
1094 IRQF_TRIGGER_RISING, "qup_in_intr", dev);
1095 if (ret) {
1096 dev_err(&pdev->dev, "request_in_irq failed\n");
1097 goto err_request_irq_failed;
1098 }
1099 /*
1100 * We assume out_irq exists if in_irq does since platform
1101 * configuration either has 3 interrupts assigned to QUP or 1
1102 */
1103 ret = request_irq(dev->out_irq, qup_i2c_interrupt,
1104 IRQF_TRIGGER_RISING, "qup_out_intr", dev);
1105 if (ret) {
1106 dev_err(&pdev->dev, "request_out_irq failed\n");
1107 free_irq(dev->in_irq, dev);
1108 goto err_request_irq_failed;
1109 }
1110 ret = request_irq(dev->err_irq, qup_i2c_interrupt,
1111 IRQF_TRIGGER_RISING, "qup_err_intr", dev);
1112 if (ret) {
1113 dev_err(&pdev->dev, "request_err_irq failed\n");
1114 free_irq(dev->out_irq, dev);
1115 free_irq(dev->in_irq, dev);
1116 goto err_request_irq_failed;
1117 }
1118 } else {
1119 ret = request_irq(dev->err_irq, qup_i2c_interrupt,
1120 IRQF_TRIGGER_HIGH, "qup_err_intr", dev);
1121 if (ret) {
1122 dev_err(&pdev->dev, "request_err_irq failed\n");
1123 goto err_request_irq_failed;
1124 }
1125 }
1126 disable_irq(dev->err_irq);
1127 if (dev->num_irqs == 3) {
1128 disable_irq(dev->in_irq);
1129 disable_irq(dev->out_irq);
1130 }
1131 i2c_set_adapdata(&dev->adapter, dev);
1132 dev->adapter.algo = &qup_i2c_algo;
1133 strlcpy(dev->adapter.name,
1134 "QUP I2C adapter",
1135 sizeof(dev->adapter.name));
1136 dev->adapter.nr = pdev->id;
1137 pdata->msm_i2c_config_gpio(dev->adapter.nr, 1);
1138
1139 dev->suspended = 0;
1140 mutex_init(&dev->mlock);
1141 dev->clk_state = 0;
1142 setup_timer(&dev->pwr_timer, qup_i2c_pwr_timer, (unsigned long) dev);
1143
1144 pm_runtime_set_active(&pdev->dev);
1145 pm_runtime_enable(&pdev->dev);
1146
1147 ret = i2c_add_numbered_adapter(&dev->adapter);
1148 if (ret) {
1149 dev_err(&pdev->dev, "i2c_add_adapter failed\n");
1150 if (dev->num_irqs == 3) {
1151 free_irq(dev->out_irq, dev);
1152 free_irq(dev->in_irq, dev);
1153 }
1154 free_irq(dev->err_irq, dev);
1155 } else
1156 return 0;
1157
1158
1159err_request_irq_failed:
1160 qup_i2c_free_gpios(dev);
1161 if (dev->gsbi)
1162 iounmap(dev->gsbi);
1163err_request_gpio_failed:
1164err_gsbi_failed:
1165 iounmap(dev->base);
1166err_ioremap_failed:
1167 kfree(dev);
1168err_alloc_dev_failed:
1169err_config_failed:
1170 clk_put(clk);
1171 if (pclk)
1172 clk_put(pclk);
1173err_clk_get_failed:
1174 if (gsbi_mem)
1175 release_mem_region(gsbi_mem->start, resource_size(gsbi_mem));
1176 release_mem_region(qup_mem->start, resource_size(qup_mem));
1177 return ret;
1178}
1179
1180static int __devexit
1181qup_i2c_remove(struct platform_device *pdev)
1182{
1183 struct qup_i2c_dev *dev = platform_get_drvdata(pdev);
1184 struct resource *qup_mem, *gsbi_mem;
1185
1186 /* Grab mutex to ensure ongoing transaction is over */
1187 mutex_lock(&dev->mlock);
1188 dev->suspended = 1;
1189 mutex_unlock(&dev->mlock);
1190 mutex_destroy(&dev->mlock);
1191 del_timer_sync(&dev->pwr_timer);
1192 if (dev->clk_state != 0)
1193 qup_i2c_pwr_mgmt(dev, 0);
1194 platform_set_drvdata(pdev, NULL);
1195 if (dev->num_irqs == 3) {
1196 free_irq(dev->out_irq, dev);
1197 free_irq(dev->in_irq, dev);
1198 }
1199 free_irq(dev->err_irq, dev);
1200 i2c_del_adapter(&dev->adapter);
1201 clk_put(dev->clk);
1202 if (dev->pclk)
1203 clk_put(dev->pclk);
1204 qup_i2c_free_gpios(dev);
1205 if (dev->gsbi)
1206 iounmap(dev->gsbi);
1207 iounmap(dev->base);
1208
1209 pm_runtime_disable(&pdev->dev);
1210
1211 kfree(dev);
1212 if (!(dev->pdata->use_gsbi_shared_mode)) {
1213 gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1214 "gsbi_qup_i2c_addr");
1215 release_mem_region(gsbi_mem->start, resource_size(gsbi_mem));
1216 }
1217 qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1218 "qup_phys_addr");
1219 release_mem_region(qup_mem->start, resource_size(qup_mem));
1220 return 0;
1221}
1222
1223#ifdef CONFIG_PM
1224static int qup_i2c_suspend(struct device *device)
1225{
1226 struct platform_device *pdev = to_platform_device(device);
1227 struct qup_i2c_dev *dev = platform_get_drvdata(pdev);
1228
1229 /* Grab mutex to ensure ongoing transaction is over */
1230 mutex_lock(&dev->mlock);
1231 dev->suspended = 1;
1232 mutex_unlock(&dev->mlock);
1233 del_timer_sync(&dev->pwr_timer);
1234 if (dev->clk_state != 0)
1235 qup_i2c_pwr_mgmt(dev, 0);
1236 qup_i2c_free_gpios(dev);
1237 return 0;
1238}
1239
1240static int qup_i2c_resume(struct device *device)
1241{
1242 struct platform_device *pdev = to_platform_device(device);
1243 struct qup_i2c_dev *dev = platform_get_drvdata(pdev);
1244 BUG_ON(qup_i2c_request_gpios(dev) != 0);
1245 dev->suspended = 0;
1246 return 0;
1247}
1248#endif /* CONFIG_PM */
1249
1250#ifdef CONFIG_PM_RUNTIME
1251static int i2c_qup_runtime_idle(struct device *dev)
1252{
1253 dev_dbg(dev, "pm_runtime: idle...\n");
1254 return 0;
1255}
1256
1257static int i2c_qup_runtime_suspend(struct device *dev)
1258{
1259 dev_dbg(dev, "pm_runtime: suspending...\n");
1260 return 0;
1261}
1262
1263static int i2c_qup_runtime_resume(struct device *dev)
1264{
1265 dev_dbg(dev, "pm_runtime: resuming...\n");
1266 return 0;
1267}
1268#endif
1269
1270static const struct dev_pm_ops i2c_qup_dev_pm_ops = {
1271 SET_SYSTEM_SLEEP_PM_OPS(
1272 qup_i2c_suspend,
1273 qup_i2c_resume
1274 )
1275 SET_RUNTIME_PM_OPS(
1276 i2c_qup_runtime_suspend,
1277 i2c_qup_runtime_resume,
1278 i2c_qup_runtime_idle
1279 )
1280};
1281
1282static struct platform_driver qup_i2c_driver = {
1283 .probe = qup_i2c_probe,
1284 .remove = __devexit_p(qup_i2c_remove),
1285 .driver = {
1286 .name = "qup_i2c",
1287 .owner = THIS_MODULE,
1288 .pm = &i2c_qup_dev_pm_ops,
1289 },
1290};
1291
1292/* QUP may be needed to bring up other drivers */
1293static int __init
1294qup_i2c_init_driver(void)
1295{
1296 return platform_driver_register(&qup_i2c_driver);
1297}
1298arch_initcall(qup_i2c_init_driver);
1299
1300static void __exit qup_i2c_exit_driver(void)
1301{
1302 platform_driver_unregister(&qup_i2c_driver);
1303}
1304module_exit(qup_i2c_exit_driver);
1305