0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/err.h>
0013 #include <linux/i2c.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/io.h>
0016 #include <linux/module.h>
0017 #include <linux/of_address.h>
0018 #include <linux/of_irq.h>
0019 #include <linux/of.h>
0020 #include <linux/pinctrl/consumer.h>
0021 #include <linux/platform_device.h>
0022
0023
0024 #define SSC_BRG 0x000
0025 #define SSC_TBUF 0x004
0026 #define SSC_RBUF 0x008
0027 #define SSC_CTL 0x00C
0028 #define SSC_IEN 0x010
0029 #define SSC_STA 0x014
0030 #define SSC_I2C 0x018
0031 #define SSC_SLAD 0x01C
0032 #define SSC_REP_START_HOLD 0x020
0033 #define SSC_START_HOLD 0x024
0034 #define SSC_REP_START_SETUP 0x028
0035 #define SSC_DATA_SETUP 0x02C
0036 #define SSC_STOP_SETUP 0x030
0037 #define SSC_BUS_FREE 0x034
0038 #define SSC_TX_FSTAT 0x038
0039 #define SSC_RX_FSTAT 0x03C
0040 #define SSC_PRE_SCALER_BRG 0x040
0041 #define SSC_CLR 0x080
0042 #define SSC_NOISE_SUPP_WIDTH 0x100
0043 #define SSC_PRSCALER 0x104
0044 #define SSC_NOISE_SUPP_WIDTH_DATAOUT 0x108
0045 #define SSC_PRSCALER_DATAOUT 0x10c
0046
0047
0048 #define SSC_CTL_DATA_WIDTH_9 0x8
0049 #define SSC_CTL_DATA_WIDTH_MSK 0xf
0050 #define SSC_CTL_BM 0xf
0051 #define SSC_CTL_HB BIT(4)
0052 #define SSC_CTL_PH BIT(5)
0053 #define SSC_CTL_PO BIT(6)
0054 #define SSC_CTL_SR BIT(7)
0055 #define SSC_CTL_MS BIT(8)
0056 #define SSC_CTL_EN BIT(9)
0057 #define SSC_CTL_LPB BIT(10)
0058 #define SSC_CTL_EN_TX_FIFO BIT(11)
0059 #define SSC_CTL_EN_RX_FIFO BIT(12)
0060 #define SSC_CTL_EN_CLST_RX BIT(13)
0061
0062
0063 #define SSC_IEN_RIEN BIT(0)
0064 #define SSC_IEN_TIEN BIT(1)
0065 #define SSC_IEN_TEEN BIT(2)
0066 #define SSC_IEN_REEN BIT(3)
0067 #define SSC_IEN_PEEN BIT(4)
0068 #define SSC_IEN_AASEN BIT(6)
0069 #define SSC_IEN_STOPEN BIT(7)
0070 #define SSC_IEN_ARBLEN BIT(8)
0071 #define SSC_IEN_NACKEN BIT(10)
0072 #define SSC_IEN_REPSTRTEN BIT(11)
0073 #define SSC_IEN_TX_FIFO_HALF BIT(12)
0074 #define SSC_IEN_RX_FIFO_HALF_FULL BIT(14)
0075
0076
0077 #define SSC_STA_RIR BIT(0)
0078 #define SSC_STA_TIR BIT(1)
0079 #define SSC_STA_TE BIT(2)
0080 #define SSC_STA_RE BIT(3)
0081 #define SSC_STA_PE BIT(4)
0082 #define SSC_STA_CLST BIT(5)
0083 #define SSC_STA_AAS BIT(6)
0084 #define SSC_STA_STOP BIT(7)
0085 #define SSC_STA_ARBL BIT(8)
0086 #define SSC_STA_BUSY BIT(9)
0087 #define SSC_STA_NACK BIT(10)
0088 #define SSC_STA_REPSTRT BIT(11)
0089 #define SSC_STA_TX_FIFO_HALF BIT(12)
0090 #define SSC_STA_TX_FIFO_FULL BIT(13)
0091 #define SSC_STA_RX_FIFO_HALF BIT(14)
0092
0093
0094 #define SSC_I2C_I2CM BIT(0)
0095 #define SSC_I2C_STRTG BIT(1)
0096 #define SSC_I2C_STOPG BIT(2)
0097 #define SSC_I2C_ACKG BIT(3)
0098 #define SSC_I2C_AD10 BIT(4)
0099 #define SSC_I2C_TXENB BIT(5)
0100 #define SSC_I2C_REPSTRTG BIT(11)
0101 #define SSC_I2C_SLAVE_DISABLE BIT(12)
0102
0103
0104 #define SSC_TX_FSTAT_STATUS 0x07
0105
0106
0107 #define SSC_RX_FSTAT_STATUS 0x07
0108
0109
0110 #define SSC_CLR_SSCAAS BIT(6)
0111 #define SSC_CLR_SSCSTOP BIT(7)
0112 #define SSC_CLR_SSCARBL BIT(8)
0113 #define SSC_CLR_NACK BIT(10)
0114 #define SSC_CLR_REPSTRT BIT(11)
0115
0116
0117 #define SSC_PRSC_VALUE 0x0f
0118
0119
0120 #define SSC_TXFIFO_SIZE 0x8
0121 #define SSC_RXFIFO_SIZE 0x8
0122
0123 enum st_i2c_mode {
0124 I2C_MODE_STANDARD,
0125 I2C_MODE_FAST,
0126 I2C_MODE_END,
0127 };
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 struct st_i2c_timings {
0141 u32 rate;
0142 u32 rep_start_hold;
0143 u32 rep_start_setup;
0144 u32 start_hold;
0145 u32 data_setup_time;
0146 u32 stop_setup_time;
0147 u32 bus_free_time;
0148 u32 sda_pulse_min_limit;
0149 };
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 struct st_i2c_client {
0161 u8 addr;
0162 u32 count;
0163 u32 xfered;
0164 u8 *buf;
0165 int result;
0166 bool stop;
0167 };
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 struct st_i2c_dev {
0184 struct i2c_adapter adap;
0185 struct device *dev;
0186 void __iomem *base;
0187 struct completion complete;
0188 int irq;
0189 struct clk *clk;
0190 int mode;
0191 u32 scl_min_width_us;
0192 u32 sda_min_width_us;
0193 struct st_i2c_client client;
0194 bool busy;
0195 };
0196
0197 static inline void st_i2c_set_bits(void __iomem *reg, u32 mask)
0198 {
0199 writel_relaxed(readl_relaxed(reg) | mask, reg);
0200 }
0201
0202 static inline void st_i2c_clr_bits(void __iomem *reg, u32 mask)
0203 {
0204 writel_relaxed(readl_relaxed(reg) & ~mask, reg);
0205 }
0206
0207
0208
0209
0210
0211
0212
0213
0214 static struct st_i2c_timings i2c_timings[] = {
0215 [I2C_MODE_STANDARD] = {
0216 .rate = I2C_MAX_STANDARD_MODE_FREQ,
0217 .rep_start_hold = 4400,
0218 .rep_start_setup = 5170,
0219 .start_hold = 4400,
0220 .data_setup_time = 275,
0221 .stop_setup_time = 4400,
0222 .bus_free_time = 5170,
0223 },
0224 [I2C_MODE_FAST] = {
0225 .rate = I2C_MAX_FAST_MODE_FREQ,
0226 .rep_start_hold = 660,
0227 .rep_start_setup = 660,
0228 .start_hold = 660,
0229 .data_setup_time = 110,
0230 .stop_setup_time = 660,
0231 .bus_free_time = 1430,
0232 },
0233 };
0234
0235 static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)
0236 {
0237 int count, i;
0238
0239
0240
0241
0242
0243
0244 if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR)
0245 count = SSC_RXFIFO_SIZE;
0246 else
0247 count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) &
0248 SSC_RX_FSTAT_STATUS;
0249
0250 for (i = 0; i < count; i++)
0251 readl_relaxed(i2c_dev->base + SSC_RBUF);
0252 }
0253
0254 static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev)
0255 {
0256
0257
0258
0259
0260 st_i2c_flush_rx_fifo(i2c_dev);
0261
0262 st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
0263 st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
0264 }
0265
0266
0267
0268
0269
0270 static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
0271 {
0272 unsigned long rate;
0273 u32 val, ns_per_clk;
0274 struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode];
0275
0276 st_i2c_soft_reset(i2c_dev);
0277
0278 val = SSC_CLR_REPSTRT | SSC_CLR_NACK | SSC_CLR_SSCARBL |
0279 SSC_CLR_SSCAAS | SSC_CLR_SSCSTOP;
0280 writel_relaxed(val, i2c_dev->base + SSC_CLR);
0281
0282
0283 val = SSC_CTL_PO | SSC_CTL_PH | SSC_CTL_HB | SSC_CTL_DATA_WIDTH_9;
0284 writel_relaxed(val, i2c_dev->base + SSC_CTL);
0285
0286 rate = clk_get_rate(i2c_dev->clk);
0287 ns_per_clk = 1000000000 / rate;
0288
0289
0290 val = rate / (2 * t->rate);
0291 writel_relaxed(val, i2c_dev->base + SSC_BRG);
0292
0293
0294 writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG);
0295
0296
0297 writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C);
0298
0299
0300 val = t->rep_start_hold / ns_per_clk;
0301 writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD);
0302
0303
0304 val = t->rep_start_setup / ns_per_clk;
0305 writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP);
0306
0307
0308 val = t->start_hold / ns_per_clk;
0309 writel_relaxed(val, i2c_dev->base + SSC_START_HOLD);
0310
0311
0312 val = t->data_setup_time / ns_per_clk;
0313 writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP);
0314
0315
0316 val = t->stop_setup_time / ns_per_clk;
0317 writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP);
0318
0319
0320 val = t->bus_free_time / ns_per_clk;
0321 writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE);
0322
0323
0324 val = rate / 10000000;
0325 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER);
0326 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT);
0327
0328
0329 val = i2c_dev->scl_min_width_us * rate / 100000000;
0330 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH);
0331
0332
0333 val = i2c_dev->sda_min_width_us * rate / 100000000;
0334 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT);
0335 }
0336
0337 static int st_i2c_recover_bus(struct i2c_adapter *i2c_adap)
0338 {
0339 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
0340 u32 ctl;
0341
0342 dev_dbg(i2c_dev->dev, "Trying to recover bus\n");
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 writel_relaxed(0, i2c_dev->base + SSC_IEN);
0353
0354 st_i2c_hw_config(i2c_dev);
0355
0356 ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
0357 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
0358
0359 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
0360 usleep_range(8000, 10000);
0361
0362 writel_relaxed(0, i2c_dev->base + SSC_TBUF);
0363 usleep_range(2000, 4000);
0364 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
0365
0366 return 0;
0367 }
0368
0369 static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
0370 {
0371 u32 sta;
0372 int i, ret;
0373
0374 for (i = 0; i < 10; i++) {
0375 sta = readl_relaxed(i2c_dev->base + SSC_STA);
0376 if (!(sta & SSC_STA_BUSY))
0377 return 0;
0378
0379 usleep_range(2000, 4000);
0380 }
0381
0382 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
0383
0384 ret = i2c_recover_bus(&i2c_dev->adap);
0385 if (ret) {
0386 dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret);
0387 return ret;
0388 }
0389
0390 return -EBUSY;
0391 }
0392
0393
0394
0395
0396
0397
0398 static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte)
0399 {
0400 u16 tbuf = byte << 1;
0401
0402 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF);
0403 }
0404
0405
0406
0407
0408
0409
0410
0411
0412 static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
0413 {
0414 struct st_i2c_client *c = &i2c_dev->client;
0415 u32 tx_fstat, sta;
0416 int i;
0417
0418 sta = readl_relaxed(i2c_dev->base + SSC_STA);
0419 if (sta & SSC_STA_TX_FIFO_FULL)
0420 return;
0421
0422 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
0423 tx_fstat &= SSC_TX_FSTAT_STATUS;
0424
0425 if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
0426 i = c->count;
0427 else
0428 i = SSC_TXFIFO_SIZE - tx_fstat;
0429
0430 for (; i > 0; i--, c->count--, c->buf++)
0431 st_i2c_write_tx_fifo(i2c_dev, *c->buf);
0432 }
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
0443 {
0444 struct st_i2c_client *c = &i2c_dev->client;
0445 u32 tx_fstat, sta;
0446 int i;
0447
0448 sta = readl_relaxed(i2c_dev->base + SSC_STA);
0449 if (sta & SSC_STA_TX_FIFO_FULL)
0450 return;
0451
0452 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
0453 tx_fstat &= SSC_TX_FSTAT_STATUS;
0454
0455 if (max < (SSC_TXFIFO_SIZE - tx_fstat))
0456 i = max;
0457 else
0458 i = SSC_TXFIFO_SIZE - tx_fstat;
0459
0460 for (; i > 0; i--, c->xfered++)
0461 st_i2c_write_tx_fifo(i2c_dev, 0xff);
0462 }
0463
0464 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev)
0465 {
0466 struct st_i2c_client *c = &i2c_dev->client;
0467 u32 i, sta;
0468 u16 rbuf;
0469
0470 sta = readl_relaxed(i2c_dev->base + SSC_STA);
0471 if (sta & SSC_STA_RIR) {
0472 i = SSC_RXFIFO_SIZE;
0473 } else {
0474 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT);
0475 i &= SSC_RX_FSTAT_STATUS;
0476 }
0477
0478 for (; (i > 0) && (c->count > 0); i--, c->count--) {
0479 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1;
0480 *c->buf++ = (u8)rbuf & 0xff;
0481 }
0482
0483 if (i) {
0484 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i);
0485 st_i2c_flush_rx_fifo(i2c_dev);
0486 }
0487 }
0488
0489
0490
0491
0492
0493 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev)
0494 {
0495 struct st_i2c_client *c = &i2c_dev->client;
0496
0497 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN);
0498 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
0499
0500 if (c->stop) {
0501 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN);
0502 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
0503 } else {
0504 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN);
0505 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG);
0506 }
0507 }
0508
0509
0510
0511
0512
0513 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev)
0514 {
0515 struct st_i2c_client *c = &i2c_dev->client;
0516
0517 st_i2c_flush_rx_fifo(i2c_dev);
0518
0519 if (!c->count)
0520
0521 st_i2c_terminate_xfer(i2c_dev);
0522 else
0523 st_i2c_wr_fill_tx_fifo(i2c_dev);
0524 }
0525
0526
0527
0528
0529
0530 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev)
0531 {
0532 struct st_i2c_client *c = &i2c_dev->client;
0533 u32 ien;
0534
0535
0536 if (!c->xfered) {
0537 readl_relaxed(i2c_dev->base + SSC_RBUF);
0538 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB);
0539 } else {
0540 st_i2c_read_rx_fifo(i2c_dev);
0541 }
0542
0543 if (!c->count) {
0544
0545 st_i2c_terminate_xfer(i2c_dev);
0546 } else if (c->count == 1) {
0547
0548 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG);
0549
0550
0551 ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN;
0552 writel_relaxed(ien, i2c_dev->base + SSC_IEN);
0553
0554 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count);
0555 } else {
0556 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1);
0557 }
0558 }
0559
0560
0561
0562
0563
0564
0565 static irqreturn_t st_i2c_isr_thread(int irq, void *data)
0566 {
0567 struct st_i2c_dev *i2c_dev = data;
0568 struct st_i2c_client *c = &i2c_dev->client;
0569 u32 sta, ien;
0570 int it;
0571
0572 ien = readl_relaxed(i2c_dev->base + SSC_IEN);
0573 sta = readl_relaxed(i2c_dev->base + SSC_STA);
0574
0575
0576 it = __fls(sta & ien);
0577 if (it < 0) {
0578 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n",
0579 sta, ien);
0580 return IRQ_NONE;
0581 }
0582
0583 switch (1 << it) {
0584 case SSC_STA_TE:
0585 if (c->addr & I2C_M_RD)
0586 st_i2c_handle_read(i2c_dev);
0587 else
0588 st_i2c_handle_write(i2c_dev);
0589 break;
0590
0591 case SSC_STA_STOP:
0592 case SSC_STA_REPSTRT:
0593 writel_relaxed(0, i2c_dev->base + SSC_IEN);
0594 complete(&i2c_dev->complete);
0595 break;
0596
0597 case SSC_STA_NACK:
0598 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR);
0599
0600
0601 if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) {
0602 st_i2c_handle_read(i2c_dev);
0603 break;
0604 }
0605
0606 it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
0607 writel_relaxed(it, i2c_dev->base + SSC_IEN);
0608
0609 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
0610 c->result = -EIO;
0611 break;
0612
0613 case SSC_STA_ARBL:
0614 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR);
0615
0616 it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
0617 writel_relaxed(it, i2c_dev->base + SSC_IEN);
0618
0619 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
0620 c->result = -EAGAIN;
0621 break;
0622
0623 default:
0624 dev_err(i2c_dev->dev,
0625 "it %d unhandled (sta=0x%04x)\n", it, sta);
0626 }
0627
0628
0629
0630
0631
0632
0633 readl(i2c_dev->base + SSC_IEN);
0634
0635 return IRQ_HANDLED;
0636 }
0637
0638
0639
0640
0641
0642
0643
0644
0645 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg,
0646 bool is_first, bool is_last)
0647 {
0648 struct st_i2c_client *c = &i2c_dev->client;
0649 u32 ctl, i2c, it;
0650 unsigned long timeout;
0651 int ret;
0652
0653 c->addr = i2c_8bit_addr_from_msg(msg);
0654 c->buf = msg->buf;
0655 c->count = msg->len;
0656 c->xfered = 0;
0657 c->result = 0;
0658 c->stop = is_last;
0659
0660 reinit_completion(&i2c_dev->complete);
0661
0662 ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
0663 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
0664
0665 i2c = SSC_I2C_TXENB;
0666 if (c->addr & I2C_M_RD)
0667 i2c |= SSC_I2C_ACKG;
0668 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c);
0669
0670
0671 st_i2c_write_tx_fifo(i2c_dev, c->addr);
0672
0673
0674 if (!(c->addr & I2C_M_RD))
0675 st_i2c_wr_fill_tx_fifo(i2c_dev);
0676
0677 it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN;
0678 writel_relaxed(it, i2c_dev->base + SSC_IEN);
0679
0680 if (is_first) {
0681 ret = st_i2c_wait_free_bus(i2c_dev);
0682 if (ret)
0683 return ret;
0684
0685 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
0686 }
0687
0688 timeout = wait_for_completion_timeout(&i2c_dev->complete,
0689 i2c_dev->adap.timeout);
0690 ret = c->result;
0691
0692 if (!timeout) {
0693 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n",
0694 c->addr);
0695 ret = -ETIMEDOUT;
0696 }
0697
0698 i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG;
0699 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c);
0700
0701 writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT,
0702 i2c_dev->base + SSC_CLR);
0703
0704 return ret;
0705 }
0706
0707
0708
0709
0710
0711
0712
0713 static int st_i2c_xfer(struct i2c_adapter *i2c_adap,
0714 struct i2c_msg msgs[], int num)
0715 {
0716 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
0717 int ret, i;
0718
0719 i2c_dev->busy = true;
0720
0721 ret = clk_prepare_enable(i2c_dev->clk);
0722 if (ret) {
0723 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
0724 return ret;
0725 }
0726
0727 pinctrl_pm_select_default_state(i2c_dev->dev);
0728
0729 st_i2c_hw_config(i2c_dev);
0730
0731 for (i = 0; (i < num) && !ret; i++)
0732 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1);
0733
0734 pinctrl_pm_select_idle_state(i2c_dev->dev);
0735
0736 clk_disable_unprepare(i2c_dev->clk);
0737
0738 i2c_dev->busy = false;
0739
0740 return (ret < 0) ? ret : i;
0741 }
0742
0743 #ifdef CONFIG_PM_SLEEP
0744 static int st_i2c_suspend(struct device *dev)
0745 {
0746 struct st_i2c_dev *i2c_dev = dev_get_drvdata(dev);
0747
0748 if (i2c_dev->busy)
0749 return -EBUSY;
0750
0751 pinctrl_pm_select_sleep_state(dev);
0752
0753 return 0;
0754 }
0755
0756 static int st_i2c_resume(struct device *dev)
0757 {
0758 pinctrl_pm_select_default_state(dev);
0759
0760 pinctrl_pm_select_idle_state(dev);
0761
0762 return 0;
0763 }
0764
0765 static SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume);
0766 #define ST_I2C_PM (&st_i2c_pm)
0767 #else
0768 #define ST_I2C_PM NULL
0769 #endif
0770
0771 static u32 st_i2c_func(struct i2c_adapter *adap)
0772 {
0773 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0774 }
0775
0776 static const struct i2c_algorithm st_i2c_algo = {
0777 .master_xfer = st_i2c_xfer,
0778 .functionality = st_i2c_func,
0779 };
0780
0781 static struct i2c_bus_recovery_info st_i2c_recovery_info = {
0782 .recover_bus = st_i2c_recover_bus,
0783 };
0784
0785 static int st_i2c_of_get_deglitch(struct device_node *np,
0786 struct st_i2c_dev *i2c_dev)
0787 {
0788 int ret;
0789
0790 ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us",
0791 &i2c_dev->scl_min_width_us);
0792 if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
0793 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n");
0794 return ret;
0795 }
0796
0797 ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us",
0798 &i2c_dev->sda_min_width_us);
0799 if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
0800 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n");
0801 return ret;
0802 }
0803
0804 return 0;
0805 }
0806
0807 static int st_i2c_probe(struct platform_device *pdev)
0808 {
0809 struct device_node *np = pdev->dev.of_node;
0810 struct st_i2c_dev *i2c_dev;
0811 struct resource *res;
0812 u32 clk_rate;
0813 struct i2c_adapter *adap;
0814 int ret;
0815
0816 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
0817 if (!i2c_dev)
0818 return -ENOMEM;
0819
0820 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0821 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
0822 if (IS_ERR(i2c_dev->base))
0823 return PTR_ERR(i2c_dev->base);
0824
0825 i2c_dev->irq = irq_of_parse_and_map(np, 0);
0826 if (!i2c_dev->irq) {
0827 dev_err(&pdev->dev, "IRQ missing or invalid\n");
0828 return -EINVAL;
0829 }
0830
0831 i2c_dev->clk = of_clk_get_by_name(np, "ssc");
0832 if (IS_ERR(i2c_dev->clk)) {
0833 dev_err(&pdev->dev, "Unable to request clock\n");
0834 return PTR_ERR(i2c_dev->clk);
0835 }
0836
0837 i2c_dev->mode = I2C_MODE_STANDARD;
0838 ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
0839 if (!ret && (clk_rate == I2C_MAX_FAST_MODE_FREQ))
0840 i2c_dev->mode = I2C_MODE_FAST;
0841
0842 i2c_dev->dev = &pdev->dev;
0843
0844 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq,
0845 NULL, st_i2c_isr_thread,
0846 IRQF_ONESHOT, pdev->name, i2c_dev);
0847 if (ret) {
0848 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
0849 return ret;
0850 }
0851
0852 pinctrl_pm_select_default_state(i2c_dev->dev);
0853
0854 pinctrl_pm_select_idle_state(i2c_dev->dev);
0855
0856 ret = st_i2c_of_get_deglitch(np, i2c_dev);
0857 if (ret)
0858 return ret;
0859
0860 adap = &i2c_dev->adap;
0861 i2c_set_adapdata(adap, i2c_dev);
0862 snprintf(adap->name, sizeof(adap->name), "ST I2C(%pa)", &res->start);
0863 adap->owner = THIS_MODULE;
0864 adap->timeout = 2 * HZ;
0865 adap->retries = 0;
0866 adap->algo = &st_i2c_algo;
0867 adap->bus_recovery_info = &st_i2c_recovery_info;
0868 adap->dev.parent = &pdev->dev;
0869 adap->dev.of_node = pdev->dev.of_node;
0870
0871 init_completion(&i2c_dev->complete);
0872
0873 ret = i2c_add_adapter(adap);
0874 if (ret)
0875 return ret;
0876
0877 platform_set_drvdata(pdev, i2c_dev);
0878
0879 dev_info(i2c_dev->dev, "%s initialized\n", adap->name);
0880
0881 return 0;
0882 }
0883
0884 static int st_i2c_remove(struct platform_device *pdev)
0885 {
0886 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
0887
0888 i2c_del_adapter(&i2c_dev->adap);
0889
0890 return 0;
0891 }
0892
0893 static const struct of_device_id st_i2c_match[] = {
0894 { .compatible = "st,comms-ssc-i2c", },
0895 { .compatible = "st,comms-ssc4-i2c", },
0896 {},
0897 };
0898 MODULE_DEVICE_TABLE(of, st_i2c_match);
0899
0900 static struct platform_driver st_i2c_driver = {
0901 .driver = {
0902 .name = "st-i2c",
0903 .of_match_table = st_i2c_match,
0904 .pm = ST_I2C_PM,
0905 },
0906 .probe = st_i2c_probe,
0907 .remove = st_i2c_remove,
0908 };
0909
0910 module_platform_driver(st_i2c_driver);
0911
0912 MODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>");
0913 MODULE_DESCRIPTION("STMicroelectronics I2C driver");
0914 MODULE_LICENSE("GPL v2");