i2c: exynos5: simplify clock frequency handling
[platform/kernel/linux-exynos.git] / drivers / i2c / busses / i2c-exynos5.c
1 /**
2  * i2c-exynos5.c - Samsung Exynos5 I2C Controller Driver
3  *
4  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13
14 #include <linux/i2c.h>
15 #include <linux/time.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/slab.h>
23 #include <linux/io.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/spinlock.h>
27
28 /*
29  * HSI2C controller from Samsung supports 2 modes of operation
30  * 1. Auto mode: Where in master automatically controls the whole transaction
31  * 2. Manual mode: Software controls the transaction by issuing commands
32  *    START, READ, WRITE, STOP, RESTART in I2C_MANUAL_CMD register.
33  *
34  * Operation mode can be selected by setting AUTO_MODE bit in I2C_CONF register
35  *
36  * Special bits are available for both modes of operation to set commands
37  * and for checking transfer status
38  */
39
40 /* Register Map */
41 #define HSI2C_CTL               0x00
42 #define HSI2C_FIFO_CTL          0x04
43 #define HSI2C_TRAILIG_CTL       0x08
44 #define HSI2C_CLK_CTL           0x0C
45 #define HSI2C_CLK_SLOT          0x10
46 #define HSI2C_INT_ENABLE        0x20
47 #define HSI2C_INT_STATUS        0x24
48 #define HSI2C_ERR_STATUS        0x2C
49 #define HSI2C_FIFO_STATUS       0x30
50 #define HSI2C_TX_DATA           0x34
51 #define HSI2C_RX_DATA           0x38
52 #define HSI2C_CONF              0x40
53 #define HSI2C_AUTO_CONF         0x44
54 #define HSI2C_TIMEOUT           0x48
55 #define HSI2C_MANUAL_CMD        0x4C
56 #define HSI2C_TRANS_STATUS      0x50
57 #define HSI2C_TIMING_HS1        0x54
58 #define HSI2C_TIMING_HS2        0x58
59 #define HSI2C_TIMING_HS3        0x5C
60 #define HSI2C_TIMING_FS1        0x60
61 #define HSI2C_TIMING_FS2        0x64
62 #define HSI2C_TIMING_FS3        0x68
63 #define HSI2C_TIMING_SLA        0x6C
64 #define HSI2C_ADDR              0x70
65
66 /* I2C_CTL Register bits */
67 #define HSI2C_FUNC_MODE_I2C                     (1u << 0)
68 #define HSI2C_MASTER                            (1u << 3)
69 #define HSI2C_RXCHON                            (1u << 6)
70 #define HSI2C_TXCHON                            (1u << 7)
71 #define HSI2C_SW_RST                            (1u << 31)
72
73 /* I2C_FIFO_CTL Register bits */
74 #define HSI2C_RXFIFO_EN                         (1u << 0)
75 #define HSI2C_TXFIFO_EN                         (1u << 1)
76 #define HSI2C_RXFIFO_TRIGGER_LEVEL(x)           ((x) << 4)
77 #define HSI2C_TXFIFO_TRIGGER_LEVEL(x)           ((x) << 16)
78
79 /* I2C_TRAILING_CTL Register bits */
80 #define HSI2C_TRAILING_COUNT                    (0xf)
81
82 /* I2C_INT_EN Register bits */
83 #define HSI2C_INT_TX_ALMOSTEMPTY_EN             (1u << 0)
84 #define HSI2C_INT_RX_ALMOSTFULL_EN              (1u << 1)
85 #define HSI2C_INT_TRAILING_EN                   (1u << 6)
86
87 /* I2C_INT_STAT Register bits */
88 #define HSI2C_INT_TX_ALMOSTEMPTY                (1u << 0)
89 #define HSI2C_INT_RX_ALMOSTFULL                 (1u << 1)
90 #define HSI2C_INT_TX_UNDERRUN                   (1u << 2)
91 #define HSI2C_INT_TX_OVERRUN                    (1u << 3)
92 #define HSI2C_INT_RX_UNDERRUN                   (1u << 4)
93 #define HSI2C_INT_RX_OVERRUN                    (1u << 5)
94 #define HSI2C_INT_TRAILING                      (1u << 6)
95 #define HSI2C_INT_I2C                           (1u << 9)
96
97 #define HSI2C_INT_TRANS_DONE                    (1u << 7)
98 #define HSI2C_INT_TRANS_ABORT                   (1u << 8)
99 #define HSI2C_INT_NO_DEV_ACK                    (1u << 9)
100 #define HSI2C_INT_NO_DEV                        (1u << 10)
101 #define HSI2C_INT_TIMEOUT                       (1u << 11)
102 #define HSI2C_INT_I2C_TRANS                     (HSI2C_INT_TRANS_DONE | \
103                                                 HSI2C_INT_TRANS_ABORT | \
104                                                 HSI2C_INT_NO_DEV_ACK |  \
105                                                 HSI2C_INT_NO_DEV |      \
106                                                 HSI2C_INT_TIMEOUT)
107
108 /* I2C_FIFO_STAT Register bits */
109 #define HSI2C_RX_FIFO_EMPTY                     (1u << 24)
110 #define HSI2C_RX_FIFO_FULL                      (1u << 23)
111 #define HSI2C_RX_FIFO_LVL(x)                    ((x >> 16) & 0x7f)
112 #define HSI2C_TX_FIFO_EMPTY                     (1u << 8)
113 #define HSI2C_TX_FIFO_FULL                      (1u << 7)
114 #define HSI2C_TX_FIFO_LVL(x)                    ((x >> 0) & 0x7f)
115
116 /* I2C_CONF Register bits */
117 #define HSI2C_AUTO_MODE                         (1u << 31)
118 #define HSI2C_10BIT_ADDR_MODE                   (1u << 30)
119 #define HSI2C_HS_MODE                           (1u << 29)
120
121 /* I2C_AUTO_CONF Register bits */
122 #define HSI2C_READ_WRITE                        (1u << 16)
123 #define HSI2C_STOP_AFTER_TRANS                  (1u << 17)
124 #define HSI2C_MASTER_RUN                        (1u << 31)
125
126 /* I2C_TIMEOUT Register bits */
127 #define HSI2C_TIMEOUT_EN                        (1u << 31)
128 #define HSI2C_TIMEOUT_MASK                      0xff
129
130 /* I2C_TRANS_STATUS register bits */
131 #define HSI2C_MASTER_BUSY                       (1u << 17)
132 #define HSI2C_SLAVE_BUSY                        (1u << 16)
133
134 /* I2C_TRANS_STATUS register bits for Exynos5 variant */
135 #define HSI2C_TIMEOUT_AUTO                      (1u << 4)
136 #define HSI2C_NO_DEV                            (1u << 3)
137 #define HSI2C_NO_DEV_ACK                        (1u << 2)
138 #define HSI2C_TRANS_ABORT                       (1u << 1)
139 #define HSI2C_TRANS_DONE                        (1u << 0)
140
141 /* I2C_TRANS_STATUS register bits for Exynos7 variant */
142 #define HSI2C_MASTER_ST_MASK                    0xf
143 #define HSI2C_MASTER_ST_IDLE                    0x0
144 #define HSI2C_MASTER_ST_START                   0x1
145 #define HSI2C_MASTER_ST_RESTART                 0x2
146 #define HSI2C_MASTER_ST_STOP                    0x3
147 #define HSI2C_MASTER_ST_MASTER_ID               0x4
148 #define HSI2C_MASTER_ST_ADDR0                   0x5
149 #define HSI2C_MASTER_ST_ADDR1                   0x6
150 #define HSI2C_MASTER_ST_ADDR2                   0x7
151 #define HSI2C_MASTER_ST_ADDR_SR                 0x8
152 #define HSI2C_MASTER_ST_READ                    0x9
153 #define HSI2C_MASTER_ST_WRITE                   0xa
154 #define HSI2C_MASTER_ST_NO_ACK                  0xb
155 #define HSI2C_MASTER_ST_LOSE                    0xc
156 #define HSI2C_MASTER_ST_WAIT                    0xd
157 #define HSI2C_MASTER_ST_WAIT_CMD                0xe
158
159 /* I2C_ADDR register bits */
160 #define HSI2C_SLV_ADDR_SLV(x)                   ((x & 0x3ff) << 0)
161 #define HSI2C_SLV_ADDR_MAS(x)                   ((x & 0x3ff) << 10)
162 #define HSI2C_MASTER_ID(x)                      ((x & 0xff) << 24)
163 #define MASTER_ID(x)                            ((x & 0x7) + 0x08)
164
165 /*
166  * Controller operating frequency, timing values for operation
167  * are calculated against this frequency
168  */
169 #define HSI2C_HS_TX_CLOCK       1000000
170 #define HSI2C_FS_TX_CLOCK       100000
171
172 #define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(1000))
173
174 #define HSI2C_EXYNOS7   BIT(0)
175
176 struct exynos5_i2c {
177         struct i2c_adapter      adap;
178         unsigned int            suspended:1;
179
180         struct i2c_msg          *msg;
181         struct completion       msg_complete;
182         unsigned int            msg_ptr;
183
184         unsigned int            irq;
185
186         void __iomem            *regs;
187         struct clk              *clk;
188         struct device           *dev;
189         int                     state;
190
191         spinlock_t              lock;           /* IRQ synchronization */
192
193         /*
194          * Since the TRANS_DONE bit is cleared on read, and we may read it
195          * either during an IRQ or after a transaction, keep track of its
196          * state here.
197          */
198         int                     trans_done;
199
200         /* Controller operating frequency */
201         unsigned int            op_clock;
202
203         /* Version of HS-I2C Hardware */
204         struct exynos_hsi2c_variant     *variant;
205 };
206
207 /**
208  * struct exynos_hsi2c_variant - platform specific HSI2C driver data
209  * @fifo_depth: the fifo depth supported by the HSI2C module
210  *
211  * Specifies platform specific configuration of HSI2C module.
212  * Note: A structure for driver specific platform data is used for future
213  * expansion of its usage.
214  */
215 struct exynos_hsi2c_variant {
216         unsigned int    fifo_depth;
217         unsigned int    hw;
218 };
219
220 static const struct exynos_hsi2c_variant exynos5250_hsi2c_data = {
221         .fifo_depth     = 64,
222 };
223
224 static const struct exynos_hsi2c_variant exynos5260_hsi2c_data = {
225         .fifo_depth     = 16,
226 };
227
228 static const struct exynos_hsi2c_variant exynos7_hsi2c_data = {
229         .fifo_depth     = 16,
230         .hw             = HSI2C_EXYNOS7,
231 };
232
233 static const struct of_device_id exynos5_i2c_match[] = {
234         {
235                 .compatible = "samsung,exynos5-hsi2c",
236                 .data = &exynos5250_hsi2c_data
237         }, {
238                 .compatible = "samsung,exynos5250-hsi2c",
239                 .data = &exynos5250_hsi2c_data
240         }, {
241                 .compatible = "samsung,exynos5260-hsi2c",
242                 .data = &exynos5260_hsi2c_data
243         }, {
244                 .compatible = "samsung,exynos7-hsi2c",
245                 .data = &exynos7_hsi2c_data
246         }, {},
247 };
248 MODULE_DEVICE_TABLE(of, exynos5_i2c_match);
249
250 static inline struct exynos_hsi2c_variant *exynos5_i2c_get_variant
251                                         (struct platform_device *pdev)
252 {
253         const struct of_device_id *match;
254
255         match = of_match_node(exynos5_i2c_match, pdev->dev.of_node);
256         return (struct exynos_hsi2c_variant *)match->data;
257 }
258
259 static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
260 {
261         writel(readl(i2c->regs + HSI2C_INT_STATUS),
262                                 i2c->regs + HSI2C_INT_STATUS);
263 }
264
265 /*
266  * exynos5_i2c_set_timing: updates the registers with appropriate
267  * timing values calculated
268  *
269  * Returns 0 on success, -EINVAL if the cycle length cannot
270  * be calculated.
271  */
272 static int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, bool hs_timings)
273 {
274         u32 i2c_timing_s1;
275         u32 i2c_timing_s2;
276         u32 i2c_timing_s3;
277         u32 i2c_timing_sla;
278         unsigned int t_start_su, t_start_hd;
279         unsigned int t_stop_su;
280         unsigned int t_data_su, t_data_hd;
281         unsigned int t_scl_l, t_scl_h;
282         unsigned int t_sr_release;
283         unsigned int t_ftl_cycle;
284         unsigned int clkin = clk_get_rate(i2c->clk);
285         unsigned int op_clk = hs_timings ? i2c->op_clock :
286                 (i2c->op_clock >= HSI2C_HS_TX_CLOCK) ? HSI2C_FS_TX_CLOCK :
287                 i2c->op_clock;
288         int div, clk_cycle, temp;
289
290         /*
291          * In case of HSI2C controller in Exynos5 series
292          * FPCLK / FI2C =
293          * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
294          *
295          * In case of HSI2C controllers in Exynos7 series
296          * FPCLK / FI2C =
297          * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + FLT_CYCLE
298          *
299          * clk_cycle := TSCLK_L + TSCLK_H
300          * temp := (CLK_DIV + 1) * (clk_cycle + 2)
301          *
302          * Constraints: 4 <= temp, 0 <= CLK_DIV < 256, 2 <= clk_cycle <= 510
303          *
304          */
305         t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
306         temp = clkin / op_clk - 8 - t_ftl_cycle;
307         if (i2c->variant->hw != HSI2C_EXYNOS7)
308                 temp -= t_ftl_cycle;
309         div = temp / 512;
310         clk_cycle = temp / (div + 1) - 2;
311         if (temp < 4 || div >= 256 || clk_cycle < 2) {
312                 dev_warn(i2c->dev, "Failed to calculate divisor");
313                 return -EINVAL;
314         }
315
316         t_scl_l = clk_cycle / 2;
317         t_scl_h = clk_cycle / 2;
318         t_start_su = t_scl_l;
319         t_start_hd = t_scl_l;
320         t_stop_su = t_scl_l;
321         t_data_su = t_scl_l / 2;
322         t_data_hd = t_scl_l / 2;
323         t_sr_release = clk_cycle;
324
325         i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
326         i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
327         i2c_timing_s3 = div << 16 | t_sr_release << 0;
328         i2c_timing_sla = t_data_hd << 0;
329
330         dev_dbg(i2c->dev, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
331                 t_start_su, t_start_hd, t_stop_su);
332         dev_dbg(i2c->dev, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
333                 t_data_su, t_scl_l, t_scl_h);
334         dev_dbg(i2c->dev, "nClkDiv: %X, tSR_RELEASE: %X\n",
335                 div, t_sr_release);
336         dev_dbg(i2c->dev, "tDATA_HD: %X\n", t_data_hd);
337
338         if (hs_timings) {
339                 writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_HS1);
340                 writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_HS2);
341                 writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_HS3);
342         } else {
343                 writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_FS1);
344                 writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_FS2);
345                 writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_FS3);
346         }
347         writel(i2c_timing_sla, i2c->regs + HSI2C_TIMING_SLA);
348
349         return 0;
350 }
351
352 static int exynos5_hsi2c_clock_setup(struct exynos5_i2c *i2c)
353 {
354         /*
355          * Configure the Fast speed timing values
356          * Even the High Speed mode initially starts with Fast mode
357          */
358         if (exynos5_i2c_set_timing(i2c, false)) {
359                 dev_err(i2c->dev, "HSI2C FS Clock set up failed\n");
360                 return -EINVAL;
361         }
362
363         /* configure the High speed timing values */
364         if (i2c->op_clock >= HSI2C_HS_TX_CLOCK) {
365                 if (exynos5_i2c_set_timing(i2c, true)) {
366                         dev_err(i2c->dev, "HSI2C HS Clock set up failed\n");
367                         return -EINVAL;
368                 }
369         }
370
371         return 0;
372 }
373
374 /*
375  * exynos5_i2c_init: configures the controller for I2C functionality
376  * Programs I2C controller for Master mode operation
377  */
378 static void exynos5_i2c_init(struct exynos5_i2c *i2c)
379 {
380         u32 i2c_conf = readl(i2c->regs + HSI2C_CONF);
381         u32 i2c_timeout = readl(i2c->regs + HSI2C_TIMEOUT);
382
383         /* Clear to disable Timeout */
384         i2c_timeout &= ~HSI2C_TIMEOUT_EN;
385         writel(i2c_timeout, i2c->regs + HSI2C_TIMEOUT);
386
387         writel((HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
388                                         i2c->regs + HSI2C_CTL);
389         writel(HSI2C_TRAILING_COUNT, i2c->regs + HSI2C_TRAILIG_CTL);
390
391         if (i2c->op_clock >= HSI2C_HS_TX_CLOCK) {
392                 writel(HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr)),
393                                         i2c->regs + HSI2C_ADDR);
394                 i2c_conf |= HSI2C_HS_MODE;
395         }
396
397         writel(i2c_conf | HSI2C_AUTO_MODE, i2c->regs + HSI2C_CONF);
398 }
399
400 static void exynos5_i2c_reset(struct exynos5_i2c *i2c)
401 {
402         u32 i2c_ctl;
403
404         /* Set and clear the bit for reset */
405         i2c_ctl = readl(i2c->regs + HSI2C_CTL);
406         i2c_ctl |= HSI2C_SW_RST;
407         writel(i2c_ctl, i2c->regs + HSI2C_CTL);
408
409         i2c_ctl = readl(i2c->regs + HSI2C_CTL);
410         i2c_ctl &= ~HSI2C_SW_RST;
411         writel(i2c_ctl, i2c->regs + HSI2C_CTL);
412
413         /* We don't expect calculations to fail during the run */
414         exynos5_hsi2c_clock_setup(i2c);
415         /* Initialize the configure registers */
416         exynos5_i2c_init(i2c);
417 }
418
419 /*
420  * exynos5_i2c_irq: top level IRQ servicing routine
421  *
422  * INT_STATUS registers gives the interrupt details. Further,
423  * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
424  * state of the bus.
425  */
426 static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
427 {
428         struct exynos5_i2c *i2c = dev_id;
429         u32 fifo_level, int_status, fifo_status, trans_status;
430         unsigned char byte;
431         int len = 0;
432
433         i2c->state = -EINVAL;
434
435         spin_lock(&i2c->lock);
436
437         int_status = readl(i2c->regs + HSI2C_INT_STATUS);
438         writel(int_status, i2c->regs + HSI2C_INT_STATUS);
439
440         /* handle interrupt related to the transfer status */
441         if (i2c->variant->hw == HSI2C_EXYNOS7) {
442                 if (int_status & HSI2C_INT_TRANS_DONE) {
443                         i2c->trans_done = 1;
444                         i2c->state = 0;
445                 } else if (int_status & HSI2C_INT_TRANS_ABORT) {
446                         dev_dbg(i2c->dev, "Deal with arbitration lose\n");
447                         i2c->state = -EAGAIN;
448                         goto stop;
449                 } else if (int_status & HSI2C_INT_NO_DEV_ACK) {
450                         dev_dbg(i2c->dev, "No ACK from device\n");
451                         i2c->state = -ENXIO;
452                         goto stop;
453                 } else if (int_status & HSI2C_INT_NO_DEV) {
454                         dev_dbg(i2c->dev, "No device\n");
455                         i2c->state = -ENXIO;
456                         goto stop;
457                 } else if (int_status & HSI2C_INT_TIMEOUT) {
458                         dev_dbg(i2c->dev, "Accessing device timed out\n");
459                         i2c->state = -ETIMEDOUT;
460                         goto stop;
461                 }
462
463                 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
464                 if ((trans_status & HSI2C_MASTER_ST_MASK) == HSI2C_MASTER_ST_LOSE) {
465                         i2c->state = -EAGAIN;
466                         goto stop;
467                 }
468         } else if (int_status & HSI2C_INT_I2C) {
469                 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
470                 if (trans_status & HSI2C_NO_DEV_ACK) {
471                         dev_dbg(i2c->dev, "No ACK from device\n");
472                         i2c->state = -ENXIO;
473                         goto stop;
474                 } else if (trans_status & HSI2C_NO_DEV) {
475                         dev_dbg(i2c->dev, "No device\n");
476                         i2c->state = -ENXIO;
477                         goto stop;
478                 } else if (trans_status & HSI2C_TRANS_ABORT) {
479                         dev_dbg(i2c->dev, "Deal with arbitration lose\n");
480                         i2c->state = -EAGAIN;
481                         goto stop;
482                 } else if (trans_status & HSI2C_TIMEOUT_AUTO) {
483                         dev_dbg(i2c->dev, "Accessing device timed out\n");
484                         i2c->state = -ETIMEDOUT;
485                         goto stop;
486                 } else if (trans_status & HSI2C_TRANS_DONE) {
487                         i2c->trans_done = 1;
488                         i2c->state = 0;
489                 }
490         }
491
492         if ((i2c->msg->flags & I2C_M_RD) && (int_status &
493                         (HSI2C_INT_TRAILING | HSI2C_INT_RX_ALMOSTFULL))) {
494                 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
495                 fifo_level = HSI2C_RX_FIFO_LVL(fifo_status);
496                 len = min(fifo_level, i2c->msg->len - i2c->msg_ptr);
497
498                 while (len > 0) {
499                         byte = (unsigned char)
500                                 readl(i2c->regs + HSI2C_RX_DATA);
501                         i2c->msg->buf[i2c->msg_ptr++] = byte;
502                         len--;
503                 }
504                 i2c->state = 0;
505         } else if (int_status & HSI2C_INT_TX_ALMOSTEMPTY) {
506                 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
507                 fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
508
509                 len = i2c->variant->fifo_depth - fifo_level;
510                 if (len > (i2c->msg->len - i2c->msg_ptr)) {
511                         u32 int_en = readl(i2c->regs + HSI2C_INT_ENABLE);
512
513                         int_en &= ~HSI2C_INT_TX_ALMOSTEMPTY_EN;
514                         writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
515                         len = i2c->msg->len - i2c->msg_ptr;
516                 }
517
518                 while (len > 0) {
519                         byte = i2c->msg->buf[i2c->msg_ptr++];
520                         writel(byte, i2c->regs + HSI2C_TX_DATA);
521                         len--;
522                 }
523                 i2c->state = 0;
524         }
525
526  stop:
527         if ((i2c->trans_done && (i2c->msg->len == i2c->msg_ptr)) ||
528             (i2c->state < 0)) {
529                 writel(0, i2c->regs + HSI2C_INT_ENABLE);
530                 exynos5_i2c_clr_pend_irq(i2c);
531                 complete(&i2c->msg_complete);
532         }
533
534         spin_unlock(&i2c->lock);
535
536         return IRQ_HANDLED;
537 }
538
539 /*
540  * exynos5_i2c_wait_bus_idle
541  *
542  * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
543  * cleared.
544  *
545  * Returns -EBUSY if the bus cannot be bought to idle
546  */
547 static int exynos5_i2c_wait_bus_idle(struct exynos5_i2c *i2c)
548 {
549         unsigned long stop_time;
550         u32 trans_status;
551
552         /* wait for 100 milli seconds for the bus to be idle */
553         stop_time = jiffies + msecs_to_jiffies(100) + 1;
554         do {
555                 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
556                 if (!(trans_status & HSI2C_MASTER_BUSY))
557                         return 0;
558
559                 usleep_range(50, 200);
560         } while (time_before(jiffies, stop_time));
561
562         return -EBUSY;
563 }
564
565 /*
566  * exynos5_i2c_message_start: Configures the bus and starts the xfer
567  * i2c: struct exynos5_i2c pointer for the current bus
568  * stop: Enables stop after transfer if set. Set for last transfer of
569  *       in the list of messages.
570  *
571  * Configures the bus for read/write function
572  * Sets chip address to talk to, message length to be sent.
573  * Enables appropriate interrupts and sends start xfer command.
574  */
575 static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
576 {
577         u32 i2c_ctl;
578         u32 int_en = 0;
579         u32 i2c_auto_conf = 0;
580         u32 fifo_ctl;
581         unsigned long flags;
582         unsigned short trig_lvl;
583
584         if (i2c->variant->hw == HSI2C_EXYNOS7)
585                 int_en |= HSI2C_INT_I2C_TRANS;
586         else
587                 int_en |= HSI2C_INT_I2C;
588
589         i2c_ctl = readl(i2c->regs + HSI2C_CTL);
590         i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
591         fifo_ctl = HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN;
592
593         if (i2c->msg->flags & I2C_M_RD) {
594                 i2c_ctl |= HSI2C_RXCHON;
595
596                 i2c_auto_conf |= HSI2C_READ_WRITE;
597
598                 trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
599                         (i2c->variant->fifo_depth * 3 / 4) : i2c->msg->len;
600                 fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(trig_lvl);
601
602                 int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
603                         HSI2C_INT_TRAILING_EN);
604         } else {
605                 i2c_ctl |= HSI2C_TXCHON;
606
607                 trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
608                         (i2c->variant->fifo_depth * 1 / 4) : i2c->msg->len;
609                 fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(trig_lvl);
610
611                 int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
612         }
613
614         writel(HSI2C_SLV_ADDR_MAS(i2c->msg->addr), i2c->regs + HSI2C_ADDR);
615
616         writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL);
617         writel(i2c_ctl, i2c->regs + HSI2C_CTL);
618
619         /*
620          * Enable interrupts before starting the transfer so that we don't
621          * miss any INT_I2C interrupts.
622          */
623         spin_lock_irqsave(&i2c->lock, flags);
624         writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
625
626         if (stop == 1)
627                 i2c_auto_conf |= HSI2C_STOP_AFTER_TRANS;
628         i2c_auto_conf |= i2c->msg->len;
629         i2c_auto_conf |= HSI2C_MASTER_RUN;
630         writel(i2c_auto_conf, i2c->regs + HSI2C_AUTO_CONF);
631         spin_unlock_irqrestore(&i2c->lock, flags);
632 }
633
634 static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
635                               struct i2c_msg *msgs, int stop)
636 {
637         unsigned long timeout;
638         int ret;
639
640         i2c->msg = msgs;
641         i2c->msg_ptr = 0;
642         i2c->trans_done = 0;
643
644         reinit_completion(&i2c->msg_complete);
645
646         exynos5_i2c_message_start(i2c, stop);
647
648         timeout = wait_for_completion_timeout(&i2c->msg_complete,
649                                               EXYNOS5_I2C_TIMEOUT);
650         if (timeout == 0)
651                 ret = -ETIMEDOUT;
652         else
653                 ret = i2c->state;
654
655         /*
656          * If this is the last message to be transfered (stop == 1)
657          * Then check if the bus can be brought back to idle.
658          */
659         if (ret == 0 && stop)
660                 ret = exynos5_i2c_wait_bus_idle(i2c);
661
662         if (ret < 0) {
663                 exynos5_i2c_reset(i2c);
664                 if (ret == -ETIMEDOUT)
665                         dev_warn(i2c->dev, "%s timeout\n",
666                                  (msgs->flags & I2C_M_RD) ? "rx" : "tx");
667         }
668
669         /* Return the state as in interrupt routine */
670         return ret;
671 }
672
673 static int exynos5_i2c_xfer(struct i2c_adapter *adap,
674                         struct i2c_msg *msgs, int num)
675 {
676         struct exynos5_i2c *i2c = adap->algo_data;
677         int i = 0, ret = 0, stop = 0;
678
679         if (i2c->suspended) {
680                 dev_err(i2c->dev, "HS-I2C is not initialized.\n");
681                 return -EIO;
682         }
683
684         ret = clk_enable(i2c->clk);
685         if (ret)
686                 return ret;
687
688         for (i = 0; i < num; i++, msgs++) {
689                 stop = (i == num - 1);
690
691                 ret = exynos5_i2c_xfer_msg(i2c, msgs, stop);
692
693                 if (ret < 0)
694                         goto out;
695         }
696
697         if (i == num) {
698                 ret = num;
699         } else {
700                 /* Only one message, cannot access the device */
701                 if (i == 1)
702                         ret = -EREMOTEIO;
703                 else
704                         ret = i;
705
706                 dev_warn(i2c->dev, "xfer message failed\n");
707         }
708
709  out:
710         clk_disable(i2c->clk);
711         return ret;
712 }
713
714 static u32 exynos5_i2c_func(struct i2c_adapter *adap)
715 {
716         return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
717 }
718
719 static const struct i2c_algorithm exynos5_i2c_algorithm = {
720         .master_xfer            = exynos5_i2c_xfer,
721         .functionality          = exynos5_i2c_func,
722 };
723
724 static int exynos5_i2c_probe(struct platform_device *pdev)
725 {
726         struct device_node *np = pdev->dev.of_node;
727         struct exynos5_i2c *i2c;
728         struct resource *mem;
729         int ret;
730
731         i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
732         if (!i2c)
733                 return -ENOMEM;
734
735         if (of_property_read_u32(np, "clock-frequency", &i2c->op_clock))
736                 i2c->op_clock = HSI2C_FS_TX_CLOCK;
737
738         strlcpy(i2c->adap.name, "exynos5-i2c", sizeof(i2c->adap.name));
739         i2c->adap.owner   = THIS_MODULE;
740         i2c->adap.algo    = &exynos5_i2c_algorithm;
741         i2c->adap.retries = 3;
742
743         i2c->dev = &pdev->dev;
744         i2c->clk = devm_clk_get(&pdev->dev, "hsi2c");
745         if (IS_ERR(i2c->clk)) {
746                 dev_err(&pdev->dev, "cannot get clock\n");
747                 return -ENOENT;
748         }
749
750         ret = clk_prepare_enable(i2c->clk);
751         if (ret)
752                 return ret;
753
754         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
755         i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
756         if (IS_ERR(i2c->regs)) {
757                 ret = PTR_ERR(i2c->regs);
758                 goto err_clk;
759         }
760
761         i2c->adap.dev.of_node = np;
762         i2c->adap.algo_data = i2c;
763         i2c->adap.dev.parent = &pdev->dev;
764
765         /* Clear pending interrupts from u-boot or misc causes */
766         exynos5_i2c_clr_pend_irq(i2c);
767
768         spin_lock_init(&i2c->lock);
769         init_completion(&i2c->msg_complete);
770
771         i2c->irq = ret = platform_get_irq(pdev, 0);
772         if (ret <= 0) {
773                 dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
774                 ret = -EINVAL;
775                 goto err_clk;
776         }
777
778         ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
779                                 IRQF_NO_SUSPEND | IRQF_ONESHOT,
780                                 dev_name(&pdev->dev), i2c);
781
782         if (ret != 0) {
783                 dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
784                 goto err_clk;
785         }
786
787         /* Need to check the variant before setting up. */
788         i2c->variant = exynos5_i2c_get_variant(pdev);
789
790         ret = exynos5_hsi2c_clock_setup(i2c);
791         if (ret)
792                 goto err_clk;
793
794         exynos5_i2c_reset(i2c);
795
796         ret = i2c_add_adapter(&i2c->adap);
797         if (ret < 0)
798                 goto err_clk;
799
800         platform_set_drvdata(pdev, i2c);
801
802         clk_disable(i2c->clk);
803
804         return 0;
805
806  err_clk:
807         clk_disable_unprepare(i2c->clk);
808         return ret;
809 }
810
811 static int exynos5_i2c_remove(struct platform_device *pdev)
812 {
813         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
814
815         i2c_del_adapter(&i2c->adap);
816
817         clk_unprepare(i2c->clk);
818
819         return 0;
820 }
821
822 #ifdef CONFIG_PM_SLEEP
823 static int exynos5_i2c_suspend_noirq(struct device *dev)
824 {
825         struct platform_device *pdev = to_platform_device(dev);
826         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
827
828         i2c->suspended = 1;
829
830         clk_unprepare(i2c->clk);
831
832         return 0;
833 }
834
835 static int exynos5_i2c_resume_noirq(struct device *dev)
836 {
837         struct platform_device *pdev = to_platform_device(dev);
838         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
839         int ret = 0;
840
841         ret = clk_prepare_enable(i2c->clk);
842         if (ret)
843                 return ret;
844
845         ret = exynos5_hsi2c_clock_setup(i2c);
846         if (ret) {
847                 clk_disable_unprepare(i2c->clk);
848                 return ret;
849         }
850
851         exynos5_i2c_init(i2c);
852         clk_disable(i2c->clk);
853         i2c->suspended = 0;
854
855         return 0;
856 }
857 #endif
858
859 static const struct dev_pm_ops exynos5_i2c_dev_pm_ops = {
860         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos5_i2c_suspend_noirq,
861                                       exynos5_i2c_resume_noirq)
862 };
863
864 static struct platform_driver exynos5_i2c_driver = {
865         .probe          = exynos5_i2c_probe,
866         .remove         = exynos5_i2c_remove,
867         .driver         = {
868                 .name   = "exynos5-hsi2c",
869                 .pm     = &exynos5_i2c_dev_pm_ops,
870                 .of_match_table = exynos5_i2c_match,
871         },
872 };
873
874 module_platform_driver(exynos5_i2c_driver);
875
876 MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
877 MODULE_AUTHOR("Naveen Krishna Chatradhi, <ch.naveen@samsung.com>");
878 MODULE_AUTHOR("Taekgyun Ko, <taeggyun.ko@samsung.com>");
879 MODULE_LICENSE("GPL v2");