Merge tag 'media/v4.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-microblaze.git] / drivers / i2c / busses / i2c-uniphier-f.c
1 /*
2  * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/i2c.h>
17 #include <linux/iopoll.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22
23 #define UNIPHIER_FI2C_CR        0x00    /* control register */
24 #define     UNIPHIER_FI2C_CR_MST        BIT(3)  /* master mode */
25 #define     UNIPHIER_FI2C_CR_STA        BIT(2)  /* start condition */
26 #define     UNIPHIER_FI2C_CR_STO        BIT(1)  /* stop condition */
27 #define     UNIPHIER_FI2C_CR_NACK       BIT(0)  /* do not return ACK */
28 #define UNIPHIER_FI2C_DTTX      0x04    /* TX FIFO */
29 #define     UNIPHIER_FI2C_DTTX_CMD      BIT(8)  /* send command (slave addr) */
30 #define     UNIPHIER_FI2C_DTTX_RD       BIT(0)  /* read transaction */
31 #define UNIPHIER_FI2C_DTRX      0x04    /* RX FIFO */
32 #define UNIPHIER_FI2C_SLAD      0x0c    /* slave address */
33 #define UNIPHIER_FI2C_CYC       0x10    /* clock cycle control */
34 #define UNIPHIER_FI2C_LCTL      0x14    /* clock low period control */
35 #define UNIPHIER_FI2C_SSUT      0x18    /* restart/stop setup time control */
36 #define UNIPHIER_FI2C_DSUT      0x1c    /* data setup time control */
37 #define UNIPHIER_FI2C_INT       0x20    /* interrupt status */
38 #define UNIPHIER_FI2C_IE        0x24    /* interrupt enable */
39 #define UNIPHIER_FI2C_IC        0x28    /* interrupt clear */
40 #define     UNIPHIER_FI2C_INT_TE        BIT(9)  /* TX FIFO empty */
41 #define     UNIPHIER_FI2C_INT_RF        BIT(8)  /* RX FIFO full */
42 #define     UNIPHIER_FI2C_INT_TC        BIT(7)  /* send complete (STOP) */
43 #define     UNIPHIER_FI2C_INT_RC        BIT(6)  /* receive complete (STOP) */
44 #define     UNIPHIER_FI2C_INT_TB        BIT(5)  /* sent specified bytes */
45 #define     UNIPHIER_FI2C_INT_RB        BIT(4)  /* received specified bytes */
46 #define     UNIPHIER_FI2C_INT_NA        BIT(2)  /* no ACK */
47 #define     UNIPHIER_FI2C_INT_AL        BIT(1)  /* arbitration lost */
48 #define UNIPHIER_FI2C_SR        0x2c    /* status register */
49 #define     UNIPHIER_FI2C_SR_DB         BIT(12) /* device busy */
50 #define     UNIPHIER_FI2C_SR_STS        BIT(11) /* stop condition detected */
51 #define     UNIPHIER_FI2C_SR_BB         BIT(8)  /* bus busy */
52 #define     UNIPHIER_FI2C_SR_RFF        BIT(3)  /* RX FIFO full */
53 #define     UNIPHIER_FI2C_SR_RNE        BIT(2)  /* RX FIFO not empty */
54 #define     UNIPHIER_FI2C_SR_TNF        BIT(1)  /* TX FIFO not full */
55 #define     UNIPHIER_FI2C_SR_TFE        BIT(0)  /* TX FIFO empty */
56 #define UNIPHIER_FI2C_RST       0x34    /* reset control */
57 #define     UNIPHIER_FI2C_RST_TBRST     BIT(2)  /* clear TX FIFO */
58 #define     UNIPHIER_FI2C_RST_RBRST     BIT(1)  /* clear RX FIFO */
59 #define     UNIPHIER_FI2C_RST_RST       BIT(0)  /* forcible bus reset */
60 #define UNIPHIER_FI2C_BM        0x38    /* bus monitor */
61 #define     UNIPHIER_FI2C_BM_SDAO       BIT(3)  /* output for SDA line */
62 #define     UNIPHIER_FI2C_BM_SDAS       BIT(2)  /* readback of SDA line */
63 #define     UNIPHIER_FI2C_BM_SCLO       BIT(1)  /* output for SCL line */
64 #define     UNIPHIER_FI2C_BM_SCLS       BIT(0)  /* readback of SCL line */
65 #define UNIPHIER_FI2C_NOISE     0x3c    /* noise filter control */
66 #define UNIPHIER_FI2C_TBC       0x40    /* TX byte count setting */
67 #define UNIPHIER_FI2C_RBC       0x44    /* RX byte count setting */
68 #define UNIPHIER_FI2C_TBCM      0x48    /* TX byte count monitor */
69 #define UNIPHIER_FI2C_RBCM      0x4c    /* RX byte count monitor */
70 #define UNIPHIER_FI2C_BRST      0x50    /* bus reset */
71 #define     UNIPHIER_FI2C_BRST_FOEN     BIT(1)  /* normal operation */
72 #define     UNIPHIER_FI2C_BRST_RSCL     BIT(0)  /* release SCL */
73
74 #define UNIPHIER_FI2C_INT_FAULTS        \
75                                 (UNIPHIER_FI2C_INT_NA | UNIPHIER_FI2C_INT_AL)
76 #define UNIPHIER_FI2C_INT_STOP          \
77                                 (UNIPHIER_FI2C_INT_TC | UNIPHIER_FI2C_INT_RC)
78
79 #define UNIPHIER_FI2C_RD                BIT(0)
80 #define UNIPHIER_FI2C_STOP              BIT(1)
81 #define UNIPHIER_FI2C_MANUAL_NACK       BIT(2)
82 #define UNIPHIER_FI2C_BYTE_WISE         BIT(3)
83 #define UNIPHIER_FI2C_DEFER_STOP_COMP   BIT(4)
84
85 #define UNIPHIER_FI2C_DEFAULT_SPEED     100000
86 #define UNIPHIER_FI2C_MAX_SPEED         400000
87 #define UNIPHIER_FI2C_FIFO_SIZE         8
88
89 struct uniphier_fi2c_priv {
90         struct completion comp;
91         struct i2c_adapter adap;
92         void __iomem *membase;
93         struct clk *clk;
94         unsigned int len;
95         u8 *buf;
96         u32 enabled_irqs;
97         int error;
98         unsigned int flags;
99         unsigned int busy_cnt;
100         unsigned int clk_cycle;
101 };
102
103 static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
104                                       bool first)
105 {
106         int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
107
108         /*
109          * TX-FIFO stores slave address in it for the first access.
110          * Decrement the counter.
111          */
112         if (first)
113                 fifo_space--;
114
115         while (priv->len) {
116                 if (fifo_space-- <= 0)
117                         break;
118
119                 dev_dbg(&priv->adap.dev, "write data: %02x\n", *priv->buf);
120                 writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
121                 priv->len--;
122         }
123 }
124
125 static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
126 {
127         int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
128                                                 1 : UNIPHIER_FI2C_FIFO_SIZE;
129
130         while (priv->len) {
131                 if (fifo_left-- <= 0)
132                         break;
133
134                 *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
135                 dev_dbg(&priv->adap.dev, "read data: %02x\n", priv->buf[-1]);
136                 priv->len--;
137         }
138 }
139
140 static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
141 {
142         writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
143 }
144
145 static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv)
146 {
147         writel(-1, priv->membase + UNIPHIER_FI2C_IC);
148 }
149
150 static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
151 {
152         dev_dbg(&priv->adap.dev, "stop condition\n");
153
154         priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
155         uniphier_fi2c_set_irqs(priv);
156         writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
157                priv->membase + UNIPHIER_FI2C_CR);
158 }
159
160 static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
161 {
162         struct uniphier_fi2c_priv *priv = dev_id;
163         u32 irq_status;
164
165         irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
166
167         dev_dbg(&priv->adap.dev,
168                 "interrupt: enabled_irqs=%04x, irq_status=%04x\n",
169                 priv->enabled_irqs, irq_status);
170
171         if (irq_status & UNIPHIER_FI2C_INT_STOP)
172                 goto complete;
173
174         if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
175                 dev_dbg(&priv->adap.dev, "arbitration lost\n");
176                 priv->error = -EAGAIN;
177                 goto complete;
178         }
179
180         if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
181                 dev_dbg(&priv->adap.dev, "could not get ACK\n");
182                 priv->error = -ENXIO;
183                 if (priv->flags & UNIPHIER_FI2C_RD) {
184                         /*
185                          * work around a hardware bug:
186                          * The receive-completed interrupt is never set even if
187                          * STOP condition is detected after the address phase
188                          * of read transaction fails to get ACK.
189                          * To avoid time-out error, we issue STOP here,
190                          * but do not wait for its completion.
191                          * It should be checked after exiting this handler.
192                          */
193                         uniphier_fi2c_stop(priv);
194                         priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
195                         goto complete;
196                 }
197                 goto stop;
198         }
199
200         if (irq_status & UNIPHIER_FI2C_INT_TE) {
201                 if (!priv->len)
202                         goto data_done;
203
204                 uniphier_fi2c_fill_txfifo(priv, false);
205                 goto handled;
206         }
207
208         if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
209                 uniphier_fi2c_drain_rxfifo(priv);
210                 if (!priv->len)
211                         goto data_done;
212
213                 if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
214                         if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
215                             !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
216                                 dev_dbg(&priv->adap.dev,
217                                         "enable read byte count IRQ\n");
218                                 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
219                                 uniphier_fi2c_set_irqs(priv);
220                                 priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
221                         }
222                         if (priv->len <= 1) {
223                                 dev_dbg(&priv->adap.dev, "set NACK\n");
224                                 writel(UNIPHIER_FI2C_CR_MST |
225                                        UNIPHIER_FI2C_CR_NACK,
226                                        priv->membase + UNIPHIER_FI2C_CR);
227                         }
228                 }
229
230                 goto handled;
231         }
232
233         return IRQ_NONE;
234
235 data_done:
236         if (priv->flags & UNIPHIER_FI2C_STOP) {
237 stop:
238                 uniphier_fi2c_stop(priv);
239         } else {
240 complete:
241                 priv->enabled_irqs = 0;
242                 uniphier_fi2c_set_irqs(priv);
243                 complete(&priv->comp);
244         }
245
246 handled:
247         uniphier_fi2c_clear_irqs(priv);
248
249         return IRQ_HANDLED;
250 }
251
252 static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr)
253 {
254         priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
255         /* do not use TX byte counter */
256         writel(0, priv->membase + UNIPHIER_FI2C_TBC);
257         /* set slave address */
258         writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
259                priv->membase + UNIPHIER_FI2C_DTTX);
260         /* first chunk of data */
261         uniphier_fi2c_fill_txfifo(priv, true);
262 }
263
264 static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
265 {
266         priv->flags |= UNIPHIER_FI2C_RD;
267
268         if (likely(priv->len < 256)) {
269                 /*
270                  * If possible, use RX byte counter.
271                  * It can automatically handle NACK for the last byte.
272                  */
273                 writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
274                 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
275                                       UNIPHIER_FI2C_INT_RB;
276         } else {
277                 /*
278                  * The byte counter can not count over 256.  In this case,
279                  * do not use it at all.  Drain data when FIFO gets full,
280                  * but treat the last portion as a special case.
281                  */
282                 writel(0, priv->membase + UNIPHIER_FI2C_RBC);
283                 priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
284                 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
285         }
286
287         /* set slave address with RD bit */
288         writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
289                priv->membase + UNIPHIER_FI2C_DTTX);
290 }
291
292 static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
293 {
294         writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
295 }
296
297 static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
298 {
299         writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
300                priv->membase + UNIPHIER_FI2C_BRST);
301 }
302
303 static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
304 {
305         uniphier_fi2c_reset(priv);
306         i2c_recover_bus(&priv->adap);
307 }
308
309 static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
310                                          struct i2c_msg *msg, bool stop)
311 {
312         struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
313         bool is_read = msg->flags & I2C_M_RD;
314         unsigned long time_left;
315
316         dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
317                 is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
318
319         priv->len = msg->len;
320         priv->buf = msg->buf;
321         priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
322         priv->error = 0;
323         priv->flags = 0;
324
325         if (stop)
326                 priv->flags |= UNIPHIER_FI2C_STOP;
327
328         reinit_completion(&priv->comp);
329         uniphier_fi2c_clear_irqs(priv);
330         writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
331                priv->membase + UNIPHIER_FI2C_RST);      /* reset TX/RX FIFO */
332
333         if (is_read)
334                 uniphier_fi2c_rx_init(priv, msg->addr);
335         else
336                 uniphier_fi2c_tx_init(priv, msg->addr);
337
338         uniphier_fi2c_set_irqs(priv);
339
340         dev_dbg(&adap->dev, "start condition\n");
341         writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
342                priv->membase + UNIPHIER_FI2C_CR);
343
344         time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
345         if (!time_left) {
346                 dev_err(&adap->dev, "transaction timeout.\n");
347                 uniphier_fi2c_recover(priv);
348                 return -ETIMEDOUT;
349         }
350         dev_dbg(&adap->dev, "complete\n");
351
352         if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
353                 u32 status;
354                 int ret;
355
356                 ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
357                                          status,
358                                          (status & UNIPHIER_FI2C_SR_STS) &&
359                                          !(status & UNIPHIER_FI2C_SR_BB),
360                                          1, 20);
361                 if (ret) {
362                         dev_err(&adap->dev,
363                                 "stop condition was not completed.\n");
364                         uniphier_fi2c_recover(priv);
365                         return ret;
366                 }
367         }
368
369         return priv->error;
370 }
371
372 static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
373 {
374         struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
375
376         if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
377                 if (priv->busy_cnt++ > 3) {
378                         /*
379                          * If bus busy continues too long, it is probably
380                          * in a wrong state.  Try bus recovery.
381                          */
382                         uniphier_fi2c_recover(priv);
383                         priv->busy_cnt = 0;
384                 }
385
386                 return -EAGAIN;
387         }
388
389         priv->busy_cnt = 0;
390         return 0;
391 }
392
393 static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
394                                      struct i2c_msg *msgs, int num)
395 {
396         struct i2c_msg *msg, *emsg = msgs + num;
397         int ret;
398
399         ret = uniphier_fi2c_check_bus_busy(adap);
400         if (ret)
401                 return ret;
402
403         for (msg = msgs; msg < emsg; msg++) {
404                 /* Emit STOP if it is the last message or I2C_M_STOP is set. */
405                 bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
406
407                 ret = uniphier_fi2c_master_xfer_one(adap, msg, stop);
408                 if (ret)
409                         return ret;
410         }
411
412         return num;
413 }
414
415 static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
416 {
417         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
418 }
419
420 static const struct i2c_algorithm uniphier_fi2c_algo = {
421         .master_xfer = uniphier_fi2c_master_xfer,
422         .functionality = uniphier_fi2c_functionality,
423 };
424
425 static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
426 {
427         struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
428
429         return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
430                                                         UNIPHIER_FI2C_BM_SCLS);
431 }
432
433 static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
434 {
435         struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
436
437         writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
438                priv->membase + UNIPHIER_FI2C_BRST);
439 }
440
441 static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
442 {
443         struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
444
445         return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
446                                                         UNIPHIER_FI2C_BM_SDAS);
447 }
448
449 static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
450 {
451         uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
452 }
453
454 static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
455         .recover_bus = i2c_generic_scl_recovery,
456         .get_scl = uniphier_fi2c_get_scl,
457         .set_scl = uniphier_fi2c_set_scl,
458         .get_sda = uniphier_fi2c_get_sda,
459         .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
460 };
461
462 static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv)
463 {
464         unsigned int cyc = priv->clk_cycle;
465         u32 tmp;
466
467         tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
468         tmp |= UNIPHIER_FI2C_CR_MST;
469         writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
470
471         uniphier_fi2c_reset(priv);
472
473         writel(cyc, priv->membase + UNIPHIER_FI2C_CYC);
474         writel(cyc / 2, priv->membase + UNIPHIER_FI2C_LCTL);
475         writel(cyc / 2, priv->membase + UNIPHIER_FI2C_SSUT);
476         writel(cyc / 16, priv->membase + UNIPHIER_FI2C_DSUT);
477
478         uniphier_fi2c_prepare_operation(priv);
479 }
480
481 static int uniphier_fi2c_probe(struct platform_device *pdev)
482 {
483         struct device *dev = &pdev->dev;
484         struct uniphier_fi2c_priv *priv;
485         struct resource *regs;
486         u32 bus_speed;
487         unsigned long clk_rate;
488         int irq, ret;
489
490         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
491         if (!priv)
492                 return -ENOMEM;
493
494         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
495         priv->membase = devm_ioremap_resource(dev, regs);
496         if (IS_ERR(priv->membase))
497                 return PTR_ERR(priv->membase);
498
499         irq = platform_get_irq(pdev, 0);
500         if (irq < 0) {
501                 dev_err(dev, "failed to get IRQ number\n");
502                 return irq;
503         }
504
505         if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
506                 bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
507
508         if (!bus_speed || bus_speed > UNIPHIER_FI2C_MAX_SPEED) {
509                 dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
510                 return -EINVAL;
511         }
512
513         priv->clk = devm_clk_get(dev, NULL);
514         if (IS_ERR(priv->clk)) {
515                 dev_err(dev, "failed to get clock\n");
516                 return PTR_ERR(priv->clk);
517         }
518
519         ret = clk_prepare_enable(priv->clk);
520         if (ret)
521                 return ret;
522
523         clk_rate = clk_get_rate(priv->clk);
524         if (!clk_rate) {
525                 dev_err(dev, "input clock rate should not be zero\n");
526                 ret = -EINVAL;
527                 goto disable_clk;
528         }
529
530         priv->clk_cycle = clk_rate / bus_speed;
531         init_completion(&priv->comp);
532         priv->adap.owner = THIS_MODULE;
533         priv->adap.algo = &uniphier_fi2c_algo;
534         priv->adap.dev.parent = dev;
535         priv->adap.dev.of_node = dev->of_node;
536         strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
537         priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
538         i2c_set_adapdata(&priv->adap, priv);
539         platform_set_drvdata(pdev, priv);
540
541         uniphier_fi2c_hw_init(priv);
542
543         ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
544                                pdev->name, priv);
545         if (ret) {
546                 dev_err(dev, "failed to request irq %d\n", irq);
547                 goto disable_clk;
548         }
549
550         ret = i2c_add_adapter(&priv->adap);
551 disable_clk:
552         if (ret)
553                 clk_disable_unprepare(priv->clk);
554
555         return ret;
556 }
557
558 static int uniphier_fi2c_remove(struct platform_device *pdev)
559 {
560         struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
561
562         i2c_del_adapter(&priv->adap);
563         clk_disable_unprepare(priv->clk);
564
565         return 0;
566 }
567
568 static int __maybe_unused uniphier_fi2c_suspend(struct device *dev)
569 {
570         struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
571
572         clk_disable_unprepare(priv->clk);
573
574         return 0;
575 }
576
577 static int __maybe_unused uniphier_fi2c_resume(struct device *dev)
578 {
579         struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
580         int ret;
581
582         ret = clk_prepare_enable(priv->clk);
583         if (ret)
584                 return ret;
585
586         uniphier_fi2c_hw_init(priv);
587
588         return 0;
589 }
590
591 static const struct dev_pm_ops uniphier_fi2c_pm_ops = {
592         SET_SYSTEM_SLEEP_PM_OPS(uniphier_fi2c_suspend, uniphier_fi2c_resume)
593 };
594
595 static const struct of_device_id uniphier_fi2c_match[] = {
596         { .compatible = "socionext,uniphier-fi2c" },
597         { /* sentinel */ }
598 };
599 MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
600
601 static struct platform_driver uniphier_fi2c_drv = {
602         .probe  = uniphier_fi2c_probe,
603         .remove = uniphier_fi2c_remove,
604         .driver = {
605                 .name  = "uniphier-fi2c",
606                 .of_match_table = uniphier_fi2c_match,
607                 .pm = &uniphier_fi2c_pm_ops,
608         },
609 };
610 module_platform_driver(uniphier_fi2c_drv);
611
612 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
613 MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
614 MODULE_LICENSE("GPL");