blob: 8f2a94248b247d65278a8e827d07139763673a91 [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
Sagar Dharia57ac1ac2011-08-06 15:12:44 -0600257static int
258qup_i2c_poll_state(struct qup_i2c_dev *dev, uint32_t req_state, bool only_valid)
259{
260 uint32_t retries = 0;
261
262 dev_dbg(dev->dev, "Polling for state:0x%x, or valid-only:%d\n",
263 req_state, only_valid);
264
265 while (retries != 2000) {
266 uint32_t status = readl_relaxed(dev->base + QUP_STATE);
267
268 /*
269 * If only valid bit needs to be checked, requested state is
270 * 'don't care'
271 */
272 if (status & QUP_STATE_VALID) {
273 if (only_valid)
274 return 0;
275 else if ((req_state & QUP_I2C_MAST_GEN) &&
276 (status & QUP_I2C_MAST_GEN))
277 return 0;
278 else if ((status & QUP_STATE_MASK) == req_state)
279 return 0;
280 }
281 if (retries++ == 1000)
282 udelay(100);
283 }
284 return -ETIMEDOUT;
285}
286
287static int
288qup_update_state(struct qup_i2c_dev *dev, uint32_t state)
289{
290 if (qup_i2c_poll_state(dev, 0, true) != 0)
291 return -EIO;
292 writel_relaxed(state, dev->base + QUP_STATE);
293 if (qup_i2c_poll_state(dev, state, false) != 0)
294 return -EIO;
295 return 0;
296}
297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298static void
299qup_i2c_pwr_mgmt(struct qup_i2c_dev *dev, unsigned int state)
300{
301 dev->clk_state = state;
302 if (state != 0) {
303 clk_enable(dev->clk);
Matt Wagantallac294852011-08-17 15:44:58 -0700304 clk_enable(dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 } else {
Sagar Dharia57ac1ac2011-08-06 15:12:44 -0600306 qup_update_state(dev, QUP_RESET_STATE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 clk_disable(dev->clk);
Matt Wagantallac294852011-08-17 15:44:58 -0700308 clk_disable(dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 }
310}
311
312static void
313qup_i2c_pwr_timer(unsigned long data)
314{
315 struct qup_i2c_dev *dev = (struct qup_i2c_dev *) data;
316 dev_dbg(dev->dev, "QUP_Power: Inactivity based power management\n");
317 if (dev->clk_state == 1)
318 qup_i2c_pwr_mgmt(dev, 0);
319}
320
321static int
322qup_i2c_poll_writeready(struct qup_i2c_dev *dev, int rem)
323{
324 uint32_t retries = 0;
325
326 while (retries != 2000) {
327 uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS);
328
329 if (!(status & I2C_STATUS_WR_BUFFER_FULL)) {
330 if (((dev->msg->flags & I2C_M_RD) || (rem == 0)) &&
331 !(status & I2C_STATUS_BUS_ACTIVE))
332 return 0;
333 else if ((dev->msg->flags == 0) && (rem > 0))
334 return 0;
335 else /* 1-bit delay before we check for bus busy */
336 udelay(dev->one_bit_t);
337 }
338 if (retries++ == 1000)
339 udelay(100);
340 }
341 qup_print_status(dev);
342 return -ETIMEDOUT;
343}
344
345static int qup_i2c_poll_clock_ready(struct qup_i2c_dev *dev)
346{
347 uint32_t retries = 0;
348
349 /*
350 * Wait for the clock state to transition to either IDLE or FORCED
351 * LOW. This will usually happen within one cycle of the i2c clock.
352 */
353
354 while (retries++ < QUP_MAX_CLK_STATE_RETRIES) {
355 uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS);
356 uint32_t clk_state = (status >> 13) & 0x7;
357
358 if (clk_state == I2C_CLK_RESET_BUSIDLE_STATE ||
359 clk_state == I2C_CLK_FORCED_LOW_STATE)
360 return 0;
361 /* 1-bit delay before we check again */
362 udelay(dev->one_bit_t);
363 }
364
365 dev_err(dev->dev, "Error waiting for clk ready\n");
366 return -ETIMEDOUT;
367}
368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369static inline int qup_i2c_request_gpios(struct qup_i2c_dev *dev)
370{
371 int i;
372 int result = 0;
373
374 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
375 if (dev->i2c_gpios[i] >= 0) {
376 result = gpio_request(dev->i2c_gpios[i], i2c_rsrcs[i]);
377 if (result) {
378 dev_err(dev->dev,
379 "gpio_request for pin %d failed\
380 with error %d\n", dev->i2c_gpios[i],
381 result);
382 goto error;
383 }
384 }
385 }
386 return 0;
387
388error:
389 for (; --i >= 0;) {
390 if (dev->i2c_gpios[i] >= 0)
391 gpio_free(dev->i2c_gpios[i]);
392 }
393 return result;
394}
395
396static inline void qup_i2c_free_gpios(struct qup_i2c_dev *dev)
397{
398 int i;
399
400 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
401 if (dev->i2c_gpios[i] >= 0)
402 gpio_free(dev->i2c_gpios[i]);
403 }
404}
405
406#ifdef DEBUG
407static void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val,
408 uint32_t addr, int rdwr)
409{
410 if (rdwr)
411 dev_dbg(dev->dev, "RD:Wrote 0x%x to out_ff:0x%x\n", val, addr);
412 else
413 dev_dbg(dev->dev, "WR:Wrote 0x%x to out_ff:0x%x\n", val, addr);
414}
415#else
416static inline void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val,
417 uint32_t addr, int rdwr)
418{
419}
420#endif
421
422static void
423qup_issue_read(struct qup_i2c_dev *dev, struct i2c_msg *msg, int *idx,
424 uint32_t carry_over)
425{
426 uint16_t addr = (msg->addr << 1) | 1;
427 /* QUP limit 256 bytes per read. By HW design, 0 in the 8-bit field
428 * is treated as 256 byte read.
429 */
430 uint16_t rd_len = ((dev->cnt == 256) ? 0 : dev->cnt);
431
432 if (*idx % 4) {
433 writel_relaxed(carry_over | ((QUP_OUT_START | addr) << 16),
434 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx-2)); */
435
436 qup_verify_fifo(dev, carry_over |
437 ((QUP_OUT_START | addr) << 16), (uint32_t)dev->base
438 + QUP_OUT_FIFO_BASE + (*idx - 2), 1);
439 writel_relaxed((QUP_OUT_REC | rd_len),
440 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx+2)); */
441
442 qup_verify_fifo(dev, (QUP_OUT_REC | rd_len),
443 (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx + 2), 1);
444 } else {
445 writel_relaxed(((QUP_OUT_REC | rd_len) << 16)
446 | QUP_OUT_START | addr,
447 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx)); */
448
449 qup_verify_fifo(dev, QUP_OUT_REC << 16 | rd_len << 16 |
450 QUP_OUT_START | addr,
451 (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx), 1);
452 }
453 *idx += 4;
454}
455
456static void
457qup_issue_write(struct qup_i2c_dev *dev, struct i2c_msg *msg, int rem,
458 int *idx, uint32_t *carry_over)
459{
460 int entries = dev->cnt;
461 int empty_sl = dev->wr_sz - ((*idx) >> 1);
462 int i = 0;
463 uint32_t val = 0;
464 uint32_t last_entry = 0;
465 uint16_t addr = msg->addr << 1;
466
467 if (dev->pos == 0) {
468 if (*idx % 4) {
469 writel_relaxed(*carry_over | ((QUP_OUT_START |
470 addr) << 16),
471 dev->base + QUP_OUT_FIFO_BASE);
472
473 qup_verify_fifo(dev, *carry_over | QUP_OUT_DATA << 16 |
474 addr << 16, (uint32_t)dev->base +
475 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
476 } else
477 val = QUP_OUT_START | addr;
478 *idx += 2;
479 i++;
480 entries++;
481 } else {
482 /* Avoid setp time issue by adding 1 NOP when number of bytes
483 * are more than FIFO/BLOCK size. setup time issue can't appear
484 * otherwise since next byte to be written will always be ready
485 */
486 val = (QUP_OUT_NOP | 1);
487 *idx += 2;
488 i++;
489 entries++;
490 }
491 if (entries > empty_sl)
492 entries = empty_sl;
493
494 for (; i < (entries - 1); i++) {
495 if (*idx % 4) {
496 writel_relaxed(val | ((QUP_OUT_DATA |
497 msg->buf[dev->pos]) << 16),
498 dev->base + QUP_OUT_FIFO_BASE);
499
500 qup_verify_fifo(dev, val | QUP_OUT_DATA << 16 |
501 msg->buf[dev->pos] << 16, (uint32_t)dev->base +
502 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
503 } else
504 val = QUP_OUT_DATA | msg->buf[dev->pos];
505 (*idx) += 2;
506 dev->pos++;
507 }
508 if (dev->pos < (msg->len - 1))
509 last_entry = QUP_OUT_DATA;
510 else if (rem > 1) /* not last array entry */
511 last_entry = QUP_OUT_DATA;
512 else
513 last_entry = QUP_OUT_STOP;
514 if ((*idx % 4) == 0) {
515 /*
516 * If read-start and read-command end up in different fifos, it
517 * may result in extra-byte being read due to extra-read cycle.
518 * Avoid that by inserting NOP as the last entry of fifo only
519 * if write command(s) leave 1 space in fifo.
520 */
521 if (rem > 1) {
522 struct i2c_msg *next = msg + 1;
523 if (next->addr == msg->addr && (next->flags | I2C_M_RD)
524 && *idx == ((dev->wr_sz*2) - 4)) {
525 writel_relaxed(((last_entry |
526 msg->buf[dev->pos]) |
527 ((1 | QUP_OUT_NOP) << 16)), dev->base +
528 QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
529
530 qup_verify_fifo(dev,
531 ((last_entry | msg->buf[dev->pos]) |
532 ((1 | QUP_OUT_NOP) << 16)),
533 (uint32_t)dev->base +
534 QUP_OUT_FIFO_BASE + (*idx), 0);
535 *idx += 2;
536 } else if (next->flags == 0 && dev->pos == msg->len - 1
537 && *idx < (dev->wr_sz*2)) {
538 /* Last byte of an intermittent write */
539 writel_relaxed((last_entry |
540 msg->buf[dev->pos]),
541 dev->base + QUP_OUT_FIFO_BASE);
542
543 qup_verify_fifo(dev,
544 last_entry | msg->buf[dev->pos],
545 (uint32_t)dev->base +
546 QUP_OUT_FIFO_BASE + (*idx), 0);
547 *idx += 2;
548 } else
549 *carry_over = (last_entry | msg->buf[dev->pos]);
550 } else {
551 writel_relaxed((last_entry | msg->buf[dev->pos]),
552 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
553
554 qup_verify_fifo(dev, last_entry | msg->buf[dev->pos],
555 (uint32_t)dev->base + QUP_OUT_FIFO_BASE +
556 (*idx), 0);
557 }
558 } else {
559 writel_relaxed(val | ((last_entry | msg->buf[dev->pos]) << 16),
560 dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */
561
562 qup_verify_fifo(dev, val | (last_entry << 16) |
563 (msg->buf[dev->pos] << 16), (uint32_t)dev->base +
564 QUP_OUT_FIFO_BASE + (*idx) - 2, 0);
565 }
566
567 *idx += 2;
568 dev->pos++;
569 dev->cnt = msg->len - dev->pos;
570}
571
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572static void
573qup_set_read_mode(struct qup_i2c_dev *dev, int rd_len)
574{
575 uint32_t wr_mode = (dev->wr_sz < dev->out_fifo_sz) ?
576 QUP_WR_BLK_MODE : 0;
577 if (rd_len > 256) {
578 dev_dbg(dev->dev, "HW limit: Breaking reads in chunk of 256\n");
579 rd_len = 256;
580 }
581 if (rd_len <= dev->in_fifo_sz) {
582 writel_relaxed(wr_mode | QUP_PACK_EN | QUP_UNPACK_EN,
583 dev->base + QUP_IO_MODE);
584 writel_relaxed(rd_len, dev->base + QUP_MX_READ_CNT);
585 } else {
586 writel_relaxed(wr_mode | QUP_RD_BLK_MODE |
587 QUP_PACK_EN | QUP_UNPACK_EN, dev->base + QUP_IO_MODE);
588 writel_relaxed(rd_len, dev->base + QUP_MX_INPUT_CNT);
589 }
590}
591
592static int
593qup_set_wr_mode(struct qup_i2c_dev *dev, int rem)
594{
595 int total_len = 0;
596 int ret = 0;
597 if (dev->msg->len >= (dev->out_fifo_sz - 1)) {
598 total_len = dev->msg->len + 1 +
599 (dev->msg->len/(dev->out_blk_sz-1));
600 writel_relaxed(QUP_WR_BLK_MODE | QUP_PACK_EN | QUP_UNPACK_EN,
601 dev->base + QUP_IO_MODE);
602 dev->wr_sz = dev->out_blk_sz;
603 } else
604 writel_relaxed(QUP_PACK_EN | QUP_UNPACK_EN,
605 dev->base + QUP_IO_MODE);
606
607 if (rem > 1) {
608 struct i2c_msg *next = dev->msg + 1;
609 if (next->addr == dev->msg->addr &&
610 next->flags == I2C_M_RD) {
611 qup_set_read_mode(dev, next->len);
612 /* make sure read start & read command are in 1 blk */
613 if ((total_len % dev->out_blk_sz) ==
614 (dev->out_blk_sz - 1))
615 total_len += 3;
616 else
617 total_len += 2;
618 }
619 }
620 /* WRITE COUNT register valid/used only in block mode */
621 if (dev->wr_sz == dev->out_blk_sz)
622 writel_relaxed(total_len, dev->base + QUP_MX_WR_CNT);
623 return ret;
624}
625
626static int
627qup_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
628{
629 DECLARE_COMPLETION_ONSTACK(complete);
630 struct qup_i2c_dev *dev = i2c_get_adapdata(adap);
631 int ret;
632 int rem = num;
633 long timeout;
634 int err;
635
636 del_timer_sync(&dev->pwr_timer);
637 mutex_lock(&dev->mlock);
638
639 if (dev->suspended) {
640 mutex_unlock(&dev->mlock);
641 return -EIO;
642 }
643
644 if (dev->clk_state == 0) {
645 if (dev->clk_ctl == 0) {
646 if (dev->pdata->src_clk_rate > 0)
647 clk_set_rate(dev->clk,
648 dev->pdata->src_clk_rate);
649 else
650 dev->pdata->src_clk_rate = 19200000;
651 }
652 qup_i2c_pwr_mgmt(dev, 1);
653 }
654 /* Initialize QUP registers during first transfer */
655 if (dev->clk_ctl == 0) {
656 int fs_div;
657 int hs_div;
658 uint32_t fifo_reg;
659
660 if (dev->gsbi) {
661 writel_relaxed(0x2 << 4, dev->gsbi);
662 /* GSBI memory is not in the same 1K region as other
663 * QUP registers. mb() here ensures that the GSBI
664 * register is updated in correct order and that the
665 * write has gone through before programming QUP core
666 * registers
667 */
668 mb();
669 }
670
671 fs_div = ((dev->pdata->src_clk_rate
672 / dev->pdata->clk_freq) / 2) - 3;
673 hs_div = 3;
674 dev->clk_ctl = ((hs_div & 0x7) << 8) | (fs_div & 0xff);
675 fifo_reg = readl_relaxed(dev->base + QUP_IO_MODE);
676 if (fifo_reg & 0x3)
677 dev->out_blk_sz = (fifo_reg & 0x3) * 16;
678 else
679 dev->out_blk_sz = 16;
680 if (fifo_reg & 0x60)
681 dev->in_blk_sz = ((fifo_reg & 0x60) >> 5) * 16;
682 else
683 dev->in_blk_sz = 16;
684 /*
685 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
686 * associated with each byte written/received
687 */
688 dev->out_blk_sz /= 2;
689 dev->in_blk_sz /= 2;
690 dev->out_fifo_sz = dev->out_blk_sz *
691 (2 << ((fifo_reg & 0x1C) >> 2));
692 dev->in_fifo_sz = dev->in_blk_sz *
693 (2 << ((fifo_reg & 0x380) >> 7));
694 dev_dbg(dev->dev, "QUP IN:bl:%d, ff:%d, OUT:bl:%d, ff:%d\n",
695 dev->in_blk_sz, dev->in_fifo_sz,
696 dev->out_blk_sz, dev->out_fifo_sz);
697 }
698
699 writel_relaxed(1, dev->base + QUP_SW_RESET);
Sagar Dharia518e2302011-08-05 11:03:03 -0600700 ret = qup_i2c_poll_state(dev, QUP_RESET_STATE, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 if (ret) {
702 dev_err(dev->dev, "QUP Busy:Trying to recover\n");
703 goto out_err;
704 }
705
706 if (dev->num_irqs == 3) {
707 enable_irq(dev->in_irq);
708 enable_irq(dev->out_irq);
709 }
710 enable_irq(dev->err_irq);
711
712 /* Initialize QUP registers */
713 writel_relaxed(0, dev->base + QUP_CONFIG);
714 writel_relaxed(QUP_OPERATIONAL_RESET, dev->base + QUP_OPERATIONAL);
715 writel_relaxed(QUP_STATUS_ERROR_FLAGS, dev->base + QUP_ERROR_FLAGS_EN);
716
717 writel_relaxed(I2C_MINI_CORE | I2C_N_VAL, dev->base + QUP_CONFIG);
718
719 /* Initialize I2C mini core registers */
720 writel_relaxed(0, dev->base + QUP_I2C_CLK_CTL);
721 writel_relaxed(QUP_I2C_STATUS_RESET, dev->base + QUP_I2C_STATUS);
722
723 while (rem) {
724 bool filled = false;
725
726 dev->cnt = msgs->len - dev->pos;
727 dev->msg = msgs;
728
729 dev->wr_sz = dev->out_fifo_sz;
730 dev->err = 0;
731 dev->complete = &complete;
732
Sagar Dharia518e2302011-08-05 11:03:03 -0600733 if (qup_i2c_poll_state(dev, QUP_I2C_MAST_GEN, false) != 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 ret = -EIO;
735 goto out_err;
736 }
737
738 qup_print_status(dev);
739 /* HW limits Read upto 256 bytes in 1 read without stop */
740 if (dev->msg->flags & I2C_M_RD) {
741 qup_set_read_mode(dev, dev->cnt);
742 if (dev->cnt > 256)
743 dev->cnt = 256;
744 } else {
745 ret = qup_set_wr_mode(dev, rem);
746 if (ret != 0)
747 goto out_err;
748 /* Don't fill block till we get interrupt */
749 if (dev->wr_sz == dev->out_blk_sz)
750 filled = true;
751 }
752
753 err = qup_update_state(dev, QUP_RUN_STATE);
754 if (err < 0) {
755 ret = err;
756 goto out_err;
757 }
758
759 qup_print_status(dev);
760 writel_relaxed(dev->clk_ctl, dev->base + QUP_I2C_CLK_CTL);
761 /* CLK_CTL register is not in the same 1K region as other QUP
762 * registers. Ensure that clock control is written before
763 * programming other QUP registers
764 */
765 mb();
766
767 do {
768 int idx = 0;
769 uint32_t carry_over = 0;
770
771 /* Transition to PAUSE state only possible from RUN */
772 err = qup_update_state(dev, QUP_PAUSE_STATE);
773 if (err < 0) {
774 ret = err;
775 goto out_err;
776 }
777
778 qup_print_status(dev);
779 /* This operation is Write, check the next operation
780 * and decide mode
781 */
782 while (filled == false) {
783 if ((msgs->flags & I2C_M_RD))
784 qup_issue_read(dev, msgs, &idx,
785 carry_over);
786 else if (!(msgs->flags & I2C_M_RD))
787 qup_issue_write(dev, msgs, rem, &idx,
788 &carry_over);
789 if (idx >= (dev->wr_sz << 1))
790 filled = true;
791 /* Start new message */
792 if (filled == false) {
793 if (msgs->flags & I2C_M_RD)
794 filled = true;
795 else if (rem > 1) {
796 /* Only combine operations with
797 * same address
798 */
799 struct i2c_msg *next = msgs + 1;
800 if (next->addr != msgs->addr)
801 filled = true;
802 else {
803 rem--;
804 msgs++;
805 dev->msg = msgs;
806 dev->pos = 0;
807 dev->cnt = msgs->len;
808 if (msgs->len > 256)
809 dev->cnt = 256;
810 }
811 } else
812 filled = true;
813 }
814 }
815 err = qup_update_state(dev, QUP_RUN_STATE);
816 if (err < 0) {
817 ret = err;
818 goto out_err;
819 }
820 dev_dbg(dev->dev, "idx:%d, rem:%d, num:%d, mode:%d\n",
821 idx, rem, num, dev->mode);
822
823 qup_print_status(dev);
824 timeout = wait_for_completion_timeout(&complete, HZ);
825 if (!timeout) {
826 uint32_t istatus = readl_relaxed(dev->base +
827 QUP_I2C_STATUS);
828 uint32_t qstatus = readl_relaxed(dev->base +
829 QUP_ERROR_FLAGS);
830 uint32_t op_flgs = readl_relaxed(dev->base +
831 QUP_OPERATIONAL);
832
833 dev_err(dev->dev, "Transaction timed out\n");
834 dev_err(dev->dev, "I2C Status: %x\n", istatus);
835 dev_err(dev->dev, "QUP Status: %x\n", qstatus);
836 dev_err(dev->dev, "OP Flags: %x\n", op_flgs);
837 writel_relaxed(1, dev->base + QUP_SW_RESET);
838 /* Make sure that the write has gone through
839 * before returning from the function
840 */
841 mb();
842 ret = -ETIMEDOUT;
843 goto out_err;
844 }
845 if (dev->err) {
846 if (dev->err > 0 &&
847 dev->err & QUP_I2C_NACK_FLAG)
848 dev_err(dev->dev,
849 "I2C slave addr:0x%x not connected\n",
850 dev->msg->addr);
851 else if (dev->err < 0) {
852 dev_err(dev->dev,
853 "QUP data xfer error %d\n", dev->err);
854 ret = dev->err;
855 goto out_err;
856 }
857 ret = -dev->err;
858 goto out_err;
859 }
860 if (dev->msg->flags & I2C_M_RD) {
861 int i;
862 uint32_t dval = 0;
863 for (i = 0; dev->pos < dev->msg->len; i++,
864 dev->pos++) {
865 uint32_t rd_status =
866 readl_relaxed(dev->base
867 + QUP_OPERATIONAL);
868 if (i % 2 == 0) {
869 if ((rd_status &
870 QUP_IN_NOT_EMPTY) == 0)
871 break;
872 dval = readl_relaxed(dev->base +
873 QUP_IN_FIFO_BASE);
874 dev->msg->buf[dev->pos] =
875 dval & 0xFF;
876 } else
877 dev->msg->buf[dev->pos] =
878 ((dval & 0xFF0000) >>
879 16);
880 }
881 dev->cnt -= i;
882 } else
883 filled = false; /* refill output FIFO */
884 dev_dbg(dev->dev, "pos:%d, len:%d, cnt:%d\n",
885 dev->pos, msgs->len, dev->cnt);
886 } while (dev->cnt > 0);
887 if (dev->cnt == 0) {
888 if (msgs->len == dev->pos) {
889 rem--;
890 msgs++;
891 dev->pos = 0;
892 }
893 if (rem) {
894 err = qup_i2c_poll_clock_ready(dev);
895 if (err < 0) {
896 ret = err;
897 goto out_err;
898 }
899 err = qup_update_state(dev, QUP_RESET_STATE);
900 if (err < 0) {
901 ret = err;
902 goto out_err;
903 }
904 }
905 }
906 /* Wait for I2C bus to be idle */
907 ret = qup_i2c_poll_writeready(dev, rem);
908 if (ret) {
909 dev_err(dev->dev,
910 "Error waiting for write ready\n");
911 goto out_err;
912 }
913 }
914
915 ret = num;
916 out_err:
917 disable_irq(dev->err_irq);
918 if (dev->num_irqs == 3) {
919 disable_irq(dev->in_irq);
920 disable_irq(dev->out_irq);
921 }
922 dev->complete = NULL;
923 dev->msg = NULL;
924 dev->pos = 0;
925 dev->err = 0;
926 dev->cnt = 0;
927 dev->pwr_timer.expires = jiffies + 3*HZ;
928 add_timer(&dev->pwr_timer);
929 mutex_unlock(&dev->mlock);
930 return ret;
931}
932
933static u32
934qup_i2c_func(struct i2c_adapter *adap)
935{
936 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
937}
938
939static const struct i2c_algorithm qup_i2c_algo = {
940 .master_xfer = qup_i2c_xfer,
941 .functionality = qup_i2c_func,
942};
943
944static int __devinit
945qup_i2c_probe(struct platform_device *pdev)
946{
947 struct qup_i2c_dev *dev;
948 struct resource *qup_mem, *gsbi_mem, *qup_io, *gsbi_io, *res;
949 struct resource *in_irq, *out_irq, *err_irq;
950 struct clk *clk, *pclk;
951 int ret = 0;
952 int i;
953 struct msm_i2c_platform_data *pdata;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954
955 gsbi_mem = NULL;
956 dev_dbg(&pdev->dev, "qup_i2c_probe\n");
957
958 pdata = pdev->dev.platform_data;
959 if (!pdata) {
960 dev_err(&pdev->dev, "platform data not initialized\n");
961 return -ENOSYS;
962 }
963 qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
964 "qup_phys_addr");
965 if (!qup_mem) {
966 dev_err(&pdev->dev, "no qup mem resource?\n");
967 return -ENODEV;
968 }
969
970 /*
971 * We only have 1 interrupt for new hardware targets and in_irq,
972 * out_irq will be NULL for those platforms
973 */
974 in_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
975 "qup_in_intr");
976
977 out_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
978 "qup_out_intr");
979
980 err_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
981 "qup_err_intr");
982 if (!err_irq) {
983 dev_err(&pdev->dev, "no error irq resource?\n");
984 return -ENODEV;
985 }
986
987 qup_io = request_mem_region(qup_mem->start, resource_size(qup_mem),
988 pdev->name);
989 if (!qup_io) {
990 dev_err(&pdev->dev, "QUP region already claimed\n");
991 return -EBUSY;
992 }
993 if (!pdata->use_gsbi_shared_mode) {
994 gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
995 "gsbi_qup_i2c_addr");
996 if (!gsbi_mem) {
997 dev_err(&pdev->dev, "no gsbi mem resource?\n");
Harini Jayaramanee31ae92011-09-20 18:32:34 -0600998 ret = -ENODEV;
999 goto err_res_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 }
1001 gsbi_io = request_mem_region(gsbi_mem->start,
1002 resource_size(gsbi_mem),
1003 pdev->name);
1004 if (!gsbi_io) {
1005 dev_err(&pdev->dev, "GSBI region already claimed\n");
Harini Jayaramanee31ae92011-09-20 18:32:34 -06001006 ret = -EBUSY;
1007 goto err_res_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 }
1009 }
1010
Matt Wagantallac294852011-08-17 15:44:58 -07001011 clk = clk_get(&pdev->dev, "core_clk");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 if (IS_ERR(clk)) {
Matt Wagantallac294852011-08-17 15:44:58 -07001013 dev_err(&pdev->dev, "Could not get core_clk\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 ret = PTR_ERR(clk);
1015 goto err_clk_get_failed;
1016 }
1017
Matt Wagantallac294852011-08-17 15:44:58 -07001018 pclk = clk_get(&pdev->dev, "iface_clk");
1019 if (IS_ERR(pclk)) {
1020 dev_err(&pdev->dev, "Could not get iface_clk\n");
1021 ret = PTR_ERR(pclk);
1022 clk_put(clk);
1023 goto err_clk_get_failed;
1024 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 /* We support frequencies upto FAST Mode(400KHz) */
1027 if (pdata->clk_freq <= 0 ||
1028 pdata->clk_freq > 400000) {
1029 dev_err(&pdev->dev, "clock frequency not supported\n");
1030 ret = -EIO;
1031 goto err_config_failed;
1032 }
1033
1034 dev = kzalloc(sizeof(struct qup_i2c_dev), GFP_KERNEL);
1035 if (!dev) {
1036 ret = -ENOMEM;
1037 goto err_alloc_dev_failed;
1038 }
1039
1040 dev->dev = &pdev->dev;
1041 if (in_irq)
1042 dev->in_irq = in_irq->start;
1043 if (out_irq)
1044 dev->out_irq = out_irq->start;
1045 dev->err_irq = err_irq->start;
1046 if (in_irq && out_irq)
1047 dev->num_irqs = 3;
1048 else
1049 dev->num_irqs = 1;
1050 dev->clk = clk;
1051 dev->pclk = pclk;
1052 dev->base = ioremap(qup_mem->start, resource_size(qup_mem));
1053 if (!dev->base) {
1054 ret = -ENOMEM;
1055 goto err_ioremap_failed;
1056 }
1057
1058 /* Configure GSBI block to use I2C functionality */
1059 if (gsbi_mem) {
1060 dev->gsbi = ioremap(gsbi_mem->start, resource_size(gsbi_mem));
1061 if (!dev->gsbi) {
1062 ret = -ENOMEM;
1063 goto err_gsbi_failed;
1064 }
1065 }
1066
1067 for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) {
1068 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
1069 i2c_rsrcs[i]);
1070 dev->i2c_gpios[i] = res ? res->start : -1;
1071 }
1072
1073 ret = qup_i2c_request_gpios(dev);
1074 if (ret)
1075 goto err_request_gpio_failed;
1076
1077 platform_set_drvdata(pdev, dev);
1078
1079 dev->one_bit_t = USEC_PER_SEC/pdata->clk_freq;
1080 dev->pdata = pdata;
1081 dev->clk_ctl = 0;
1082 dev->pos = 0;
1083
1084 /*
1085 * We use num_irqs to also indicate if we got 3 interrupts or just 1.
1086 * If we have just 1, we use err_irq as the general purpose irq
1087 * and handle the changes in ISR accordingly
1088 * Per Hardware guidelines, if we have 3 interrupts, they are always
1089 * edge triggering, and if we have 1, it's always level-triggering
1090 */
1091 if (dev->num_irqs == 3) {
1092 ret = request_irq(dev->in_irq, qup_i2c_interrupt,
1093 IRQF_TRIGGER_RISING, "qup_in_intr", dev);
1094 if (ret) {
1095 dev_err(&pdev->dev, "request_in_irq failed\n");
1096 goto err_request_irq_failed;
1097 }
1098 /*
1099 * We assume out_irq exists if in_irq does since platform
1100 * configuration either has 3 interrupts assigned to QUP or 1
1101 */
1102 ret = request_irq(dev->out_irq, qup_i2c_interrupt,
1103 IRQF_TRIGGER_RISING, "qup_out_intr", dev);
1104 if (ret) {
1105 dev_err(&pdev->dev, "request_out_irq failed\n");
1106 free_irq(dev->in_irq, dev);
1107 goto err_request_irq_failed;
1108 }
1109 ret = request_irq(dev->err_irq, qup_i2c_interrupt,
1110 IRQF_TRIGGER_RISING, "qup_err_intr", dev);
1111 if (ret) {
1112 dev_err(&pdev->dev, "request_err_irq failed\n");
1113 free_irq(dev->out_irq, dev);
1114 free_irq(dev->in_irq, dev);
1115 goto err_request_irq_failed;
1116 }
1117 } else {
1118 ret = request_irq(dev->err_irq, qup_i2c_interrupt,
1119 IRQF_TRIGGER_HIGH, "qup_err_intr", dev);
1120 if (ret) {
1121 dev_err(&pdev->dev, "request_err_irq failed\n");
1122 goto err_request_irq_failed;
1123 }
1124 }
1125 disable_irq(dev->err_irq);
1126 if (dev->num_irqs == 3) {
1127 disable_irq(dev->in_irq);
1128 disable_irq(dev->out_irq);
1129 }
1130 i2c_set_adapdata(&dev->adapter, dev);
1131 dev->adapter.algo = &qup_i2c_algo;
1132 strlcpy(dev->adapter.name,
1133 "QUP I2C adapter",
1134 sizeof(dev->adapter.name));
1135 dev->adapter.nr = pdev->id;
Harini Jayaramance67cf82011-08-05 09:26:06 -06001136 if (pdata->msm_i2c_config_gpio)
1137 pdata->msm_i2c_config_gpio(dev->adapter.nr, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138
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);
Matt Wagantallac294852011-08-17 15:44:58 -07001171 clk_put(pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172err_clk_get_failed:
1173 if (gsbi_mem)
1174 release_mem_region(gsbi_mem->start, resource_size(gsbi_mem));
Harini Jayaramanee31ae92011-09-20 18:32:34 -06001175err_res_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 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);
Matt Wagantallac294852011-08-17 15:44:58 -07001202 clk_put(dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203 qup_i2c_free_gpios(dev);
1204 if (dev->gsbi)
1205 iounmap(dev->gsbi);
1206 iounmap(dev->base);
1207
1208 pm_runtime_disable(&pdev->dev);
1209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 if (!(dev->pdata->use_gsbi_shared_mode)) {
1211 gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1212 "gsbi_qup_i2c_addr");
1213 release_mem_region(gsbi_mem->start, resource_size(gsbi_mem));
1214 }
1215 qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1216 "qup_phys_addr");
1217 release_mem_region(qup_mem->start, resource_size(qup_mem));
Harini Jayaramanee31ae92011-09-20 18:32:34 -06001218 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 return 0;
1220}
1221
1222#ifdef CONFIG_PM
1223static int qup_i2c_suspend(struct device *device)
1224{
1225 struct platform_device *pdev = to_platform_device(device);
1226 struct qup_i2c_dev *dev = platform_get_drvdata(pdev);
1227
1228 /* Grab mutex to ensure ongoing transaction is over */
1229 mutex_lock(&dev->mlock);
1230 dev->suspended = 1;
1231 mutex_unlock(&dev->mlock);
1232 del_timer_sync(&dev->pwr_timer);
1233 if (dev->clk_state != 0)
1234 qup_i2c_pwr_mgmt(dev, 0);
1235 qup_i2c_free_gpios(dev);
1236 return 0;
1237}
1238
1239static int qup_i2c_resume(struct device *device)
1240{
1241 struct platform_device *pdev = to_platform_device(device);
1242 struct qup_i2c_dev *dev = platform_get_drvdata(pdev);
1243 BUG_ON(qup_i2c_request_gpios(dev) != 0);
1244 dev->suspended = 0;
1245 return 0;
1246}
1247#endif /* CONFIG_PM */
1248
1249#ifdef CONFIG_PM_RUNTIME
1250static int i2c_qup_runtime_idle(struct device *dev)
1251{
1252 dev_dbg(dev, "pm_runtime: idle...\n");
1253 return 0;
1254}
1255
1256static int i2c_qup_runtime_suspend(struct device *dev)
1257{
1258 dev_dbg(dev, "pm_runtime: suspending...\n");
1259 return 0;
1260}
1261
1262static int i2c_qup_runtime_resume(struct device *dev)
1263{
1264 dev_dbg(dev, "pm_runtime: resuming...\n");
1265 return 0;
1266}
1267#endif
1268
1269static const struct dev_pm_ops i2c_qup_dev_pm_ops = {
1270 SET_SYSTEM_SLEEP_PM_OPS(
1271 qup_i2c_suspend,
1272 qup_i2c_resume
1273 )
1274 SET_RUNTIME_PM_OPS(
1275 i2c_qup_runtime_suspend,
1276 i2c_qup_runtime_resume,
1277 i2c_qup_runtime_idle
1278 )
1279};
1280
1281static struct platform_driver qup_i2c_driver = {
1282 .probe = qup_i2c_probe,
1283 .remove = __devexit_p(qup_i2c_remove),
1284 .driver = {
1285 .name = "qup_i2c",
1286 .owner = THIS_MODULE,
1287 .pm = &i2c_qup_dev_pm_ops,
1288 },
1289};
1290
1291/* QUP may be needed to bring up other drivers */
1292static int __init
1293qup_i2c_init_driver(void)
1294{
1295 return platform_driver_register(&qup_i2c_driver);
1296}
1297arch_initcall(qup_i2c_init_driver);
1298
1299static void __exit qup_i2c_exit_driver(void)
1300{
1301 platform_driver_unregister(&qup_i2c_driver);
1302}
1303module_exit(qup_i2c_exit_driver);
1304