Lines Matching full:i2c
10 #include <linux/i2c.h>
53 #define DEV(i2c) ((i2c)->adap.dev.parent) argument
69 static void zx2967_i2c_writel(struct zx2967_i2c *i2c, in zx2967_i2c_writel() argument
72 writel_relaxed(val, i2c->reg_base + reg); in zx2967_i2c_writel()
75 static u32 zx2967_i2c_readl(struct zx2967_i2c *i2c, unsigned long reg) in zx2967_i2c_readl() argument
77 return readl_relaxed(i2c->reg_base + reg); in zx2967_i2c_readl()
80 static void zx2967_i2c_writesb(struct zx2967_i2c *i2c, in zx2967_i2c_writesb() argument
83 writesb(i2c->reg_base + reg, data, len); in zx2967_i2c_writesb()
86 static void zx2967_i2c_readsb(struct zx2967_i2c *i2c, in zx2967_i2c_readsb() argument
89 readsb(i2c->reg_base + reg, data, len); in zx2967_i2c_readsb()
92 static void zx2967_i2c_start_ctrl(struct zx2967_i2c *i2c) in zx2967_i2c_start_ctrl() argument
97 status = zx2967_i2c_readl(i2c, REG_STAT); in zx2967_i2c_start_ctrl()
99 zx2967_i2c_writel(i2c, status, REG_STAT); in zx2967_i2c_start_ctrl()
101 ctl = zx2967_i2c_readl(i2c, REG_CMD); in zx2967_i2c_start_ctrl()
102 if (i2c->msg_rd) in zx2967_i2c_start_ctrl()
108 zx2967_i2c_writel(i2c, ctl, REG_CMD); in zx2967_i2c_start_ctrl()
111 static void zx2967_i2c_flush_fifos(struct zx2967_i2c *i2c) in zx2967_i2c_flush_fifos() argument
116 if (i2c->msg_rd) { in zx2967_i2c_flush_fifos()
124 val |= zx2967_i2c_readl(i2c, offset); in zx2967_i2c_flush_fifos()
125 zx2967_i2c_writel(i2c, val, offset); in zx2967_i2c_flush_fifos()
128 static int zx2967_i2c_empty_rx_fifo(struct zx2967_i2c *i2c, u32 size) in zx2967_i2c_empty_rx_fifo() argument
134 dev_err(DEV(i2c), "fifo size %d over the max value %d\n", in zx2967_i2c_empty_rx_fifo()
139 zx2967_i2c_readsb(i2c, val, REG_DATA, size); in zx2967_i2c_empty_rx_fifo()
141 *i2c->cur_trans++ = val[i]; in zx2967_i2c_empty_rx_fifo()
142 i2c->residue--; in zx2967_i2c_empty_rx_fifo()
150 static int zx2967_i2c_fill_tx_fifo(struct zx2967_i2c *i2c) in zx2967_i2c_fill_tx_fifo() argument
152 size_t residue = i2c->residue; in zx2967_i2c_fill_tx_fifo()
153 u8 *buf = i2c->cur_trans; in zx2967_i2c_fill_tx_fifo()
156 dev_err(DEV(i2c), "residue is %d\n", (int)residue); in zx2967_i2c_fill_tx_fifo()
161 zx2967_i2c_writesb(i2c, buf, REG_DATA, residue); in zx2967_i2c_fill_tx_fifo()
164 i2c->residue = 0; in zx2967_i2c_fill_tx_fifo()
165 i2c->cur_trans = NULL; in zx2967_i2c_fill_tx_fifo()
167 zx2967_i2c_writesb(i2c, buf, REG_DATA, I2C_FIFO_MAX); in zx2967_i2c_fill_tx_fifo()
168 i2c->residue -= I2C_FIFO_MAX; in zx2967_i2c_fill_tx_fifo()
169 i2c->cur_trans += I2C_FIFO_MAX; in zx2967_i2c_fill_tx_fifo()
177 static int zx2967_i2c_reset_hardware(struct zx2967_i2c *i2c) in zx2967_i2c_reset_hardware() argument
183 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_i2c_reset_hardware()
185 clk_div = clk_get_rate(i2c->clk) / i2c->clk_freq - 1; in zx2967_i2c_reset_hardware()
186 zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_FS); in zx2967_i2c_reset_hardware()
187 zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_HS); in zx2967_i2c_reset_hardware()
189 zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_WRCONF); in zx2967_i2c_reset_hardware()
190 zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_RDCONF); in zx2967_i2c_reset_hardware()
191 zx2967_i2c_writel(i2c, 1, REG_RDCONF); in zx2967_i2c_reset_hardware()
193 zx2967_i2c_flush_fifos(i2c); in zx2967_i2c_reset_hardware()
198 static void zx2967_i2c_isr_clr(struct zx2967_i2c *i2c) in zx2967_i2c_isr_clr() argument
202 status = zx2967_i2c_readl(i2c, REG_STAT); in zx2967_i2c_isr_clr()
204 zx2967_i2c_writel(i2c, status, REG_STAT); in zx2967_i2c_isr_clr()
210 struct zx2967_i2c *i2c = (struct zx2967_i2c *)dev_id; in zx2967_i2c_isr() local
212 status = zx2967_i2c_readl(i2c, REG_STAT) & I2C_INT_MASK; in zx2967_i2c_isr()
213 zx2967_i2c_isr_clr(i2c); in zx2967_i2c_isr()
216 i2c->error = -ENXIO; in zx2967_i2c_isr()
218 i2c->error = -EIO; in zx2967_i2c_isr()
220 i2c->error = 0; in zx2967_i2c_isr()
224 complete(&i2c->complete); in zx2967_i2c_isr()
229 static void zx2967_set_addr(struct zx2967_i2c *i2c, u16 addr) in zx2967_set_addr() argument
234 zx2967_i2c_writel(i2c, val, REG_DEVADDR_L); in zx2967_set_addr()
237 zx2967_i2c_writel(i2c, val, REG_DEVADDR_H); in zx2967_set_addr()
239 val = zx2967_i2c_readl(i2c, REG_CMD) | I2C_ADDR_MODE_TEN; in zx2967_set_addr()
241 val = zx2967_i2c_readl(i2c, REG_CMD) & ~I2C_ADDR_MODE_TEN; in zx2967_set_addr()
242 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_set_addr()
245 static int zx2967_i2c_xfer_bytes(struct zx2967_i2c *i2c, u32 bytes) in zx2967_i2c_xfer_bytes() argument
248 int rd = i2c->msg_rd; in zx2967_i2c_xfer_bytes()
251 reinit_completion(&i2c->complete); in zx2967_i2c_xfer_bytes()
254 zx2967_i2c_writel(i2c, bytes - 1, REG_RDCONF); in zx2967_i2c_xfer_bytes()
256 ret = zx2967_i2c_fill_tx_fifo(i2c); in zx2967_i2c_xfer_bytes()
261 zx2967_i2c_start_ctrl(i2c); in zx2967_i2c_xfer_bytes()
263 time_left = wait_for_completion_timeout(&i2c->complete, in zx2967_i2c_xfer_bytes()
268 if (i2c->error) in zx2967_i2c_xfer_bytes()
269 return i2c->error; in zx2967_i2c_xfer_bytes()
271 return rd ? zx2967_i2c_empty_rx_fifo(i2c, bytes) : 0; in zx2967_i2c_xfer_bytes()
274 static int zx2967_i2c_xfer_msg(struct zx2967_i2c *i2c, in zx2967_i2c_xfer_msg() argument
280 zx2967_i2c_flush_fifos(i2c); in zx2967_i2c_xfer_msg()
282 i2c->cur_trans = msg->buf; in zx2967_i2c_xfer_msg()
283 i2c->residue = msg->len; in zx2967_i2c_xfer_msg()
284 i2c->access_cnt = msg->len / I2C_FIFO_MAX; in zx2967_i2c_xfer_msg()
285 i2c->msg_rd = msg->flags & I2C_M_RD; in zx2967_i2c_xfer_msg()
287 for (i = 0; i < i2c->access_cnt; i++) { in zx2967_i2c_xfer_msg()
288 ret = zx2967_i2c_xfer_bytes(i2c, I2C_FIFO_MAX); in zx2967_i2c_xfer_msg()
293 if (i2c->residue > 0) { in zx2967_i2c_xfer_msg()
294 ret = zx2967_i2c_xfer_bytes(i2c, i2c->residue); in zx2967_i2c_xfer_msg()
299 i2c->residue = 0; in zx2967_i2c_xfer_msg()
300 i2c->access_cnt = 0; in zx2967_i2c_xfer_msg()
308 struct zx2967_i2c *i2c = i2c_get_adapdata(adap); in zx2967_i2c_xfer() local
312 zx2967_set_addr(i2c, msgs->addr); in zx2967_i2c_xfer()
315 ret = zx2967_i2c_xfer_msg(i2c, &msgs[i]); in zx2967_i2c_xfer()
324 zx2967_smbus_xfer_prepare(struct zx2967_i2c *i2c, u16 addr, in zx2967_smbus_xfer_prepare() argument
330 val = zx2967_i2c_readl(i2c, REG_RDCONF); in zx2967_smbus_xfer_prepare()
332 zx2967_i2c_writel(i2c, val, REG_RDCONF); in zx2967_smbus_xfer_prepare()
333 zx2967_set_addr(i2c, addr); in zx2967_smbus_xfer_prepare()
334 val = zx2967_i2c_readl(i2c, REG_CMD); in zx2967_smbus_xfer_prepare()
336 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_smbus_xfer_prepare()
340 zx2967_i2c_writel(i2c, command, REG_DATA); in zx2967_smbus_xfer_prepare()
343 zx2967_i2c_writel(i2c, command, REG_DATA); in zx2967_smbus_xfer_prepare()
345 zx2967_i2c_writel(i2c, data->byte, REG_DATA); in zx2967_smbus_xfer_prepare()
348 zx2967_i2c_writel(i2c, command, REG_DATA); in zx2967_smbus_xfer_prepare()
350 zx2967_i2c_writel(i2c, (data->word >> 8), REG_DATA); in zx2967_smbus_xfer_prepare()
351 zx2967_i2c_writel(i2c, (data->word & 0xff), in zx2967_smbus_xfer_prepare()
358 static int zx2967_smbus_xfer_read(struct zx2967_i2c *i2c, int size, in zx2967_smbus_xfer_read() argument
365 reinit_completion(&i2c->complete); in zx2967_smbus_xfer_read()
367 val = zx2967_i2c_readl(i2c, REG_CMD); in zx2967_smbus_xfer_read()
369 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_smbus_xfer_read()
371 val = zx2967_i2c_readl(i2c, REG_CMD); in zx2967_smbus_xfer_read()
373 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_smbus_xfer_read()
375 time_left = wait_for_completion_timeout(&i2c->complete, in zx2967_smbus_xfer_read()
380 if (i2c->error) in zx2967_smbus_xfer_read()
381 return i2c->error; in zx2967_smbus_xfer_read()
386 val = zx2967_i2c_readl(i2c, REG_DATA); in zx2967_smbus_xfer_read()
391 buf[0] = zx2967_i2c_readl(i2c, REG_DATA); in zx2967_smbus_xfer_read()
392 buf[1] = zx2967_i2c_readl(i2c, REG_DATA); in zx2967_smbus_xfer_read()
402 static int zx2967_smbus_xfer_write(struct zx2967_i2c *i2c) in zx2967_smbus_xfer_write() argument
407 reinit_completion(&i2c->complete); in zx2967_smbus_xfer_write()
408 val = zx2967_i2c_readl(i2c, REG_CMD); in zx2967_smbus_xfer_write()
410 zx2967_i2c_writel(i2c, val, REG_CMD); in zx2967_smbus_xfer_write()
412 time_left = wait_for_completion_timeout(&i2c->complete, in zx2967_smbus_xfer_write()
417 if (i2c->error) in zx2967_smbus_xfer_write()
418 return i2c->error; in zx2967_smbus_xfer_write()
427 struct zx2967_i2c *i2c = i2c_get_adapdata(adap); in zx2967_smbus_xfer() local
437 zx2967_smbus_xfer_prepare(i2c, addr, read_write, in zx2967_smbus_xfer()
445 return zx2967_smbus_xfer_read(i2c, size, data); in zx2967_smbus_xfer()
447 return zx2967_smbus_xfer_write(i2c); in zx2967_smbus_xfer()
464 struct zx2967_i2c *i2c = dev_get_drvdata(dev); in zx2967_i2c_suspend() local
466 i2c_mark_adapter_suspended(&i2c->adap); in zx2967_i2c_suspend()
467 clk_disable_unprepare(i2c->clk); in zx2967_i2c_suspend()
474 struct zx2967_i2c *i2c = dev_get_drvdata(dev); in zx2967_i2c_resume() local
476 clk_prepare_enable(i2c->clk); in zx2967_i2c_resume()
477 i2c_mark_adapter_resumed(&i2c->adap); in zx2967_i2c_resume()
496 { .compatible = "zte,zx296718-i2c", },
503 struct zx2967_i2c *i2c; in zx2967_i2c_probe() local
508 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in zx2967_i2c_probe()
509 if (!i2c) in zx2967_i2c_probe()
529 &i2c->clk_freq); in zx2967_i2c_probe()
539 i2c->irq = ret; in zx2967_i2c_probe()
540 i2c->reg_base = reg_base; in zx2967_i2c_probe()
541 i2c->clk = clk; in zx2967_i2c_probe()
543 init_completion(&i2c->complete); in zx2967_i2c_probe()
544 platform_set_drvdata(pdev, i2c); in zx2967_i2c_probe()
546 ret = zx2967_i2c_reset_hardware(i2c); in zx2967_i2c_probe()
548 dev_err(&pdev->dev, "failed to initialize i2c controller\n"); in zx2967_i2c_probe()
552 ret = devm_request_irq(&pdev->dev, i2c->irq, in zx2967_i2c_probe()
553 zx2967_i2c_isr, 0, dev_name(&pdev->dev), i2c); in zx2967_i2c_probe()
555 dev_err(&pdev->dev, "failed to request irq %i\n", i2c->irq); in zx2967_i2c_probe()
559 i2c_set_adapdata(&i2c->adap, i2c); in zx2967_i2c_probe()
560 strlcpy(i2c->adap.name, "zx2967 i2c adapter", in zx2967_i2c_probe()
561 sizeof(i2c->adap.name)); in zx2967_i2c_probe()
562 i2c->adap.algo = &zx2967_i2c_algo; in zx2967_i2c_probe()
563 i2c->adap.quirks = &zx2967_i2c_quirks; in zx2967_i2c_probe()
564 i2c->adap.nr = pdev->id; in zx2967_i2c_probe()
565 i2c->adap.dev.parent = &pdev->dev; in zx2967_i2c_probe()
566 i2c->adap.dev.of_node = pdev->dev.of_node; in zx2967_i2c_probe()
568 ret = i2c_add_numbered_adapter(&i2c->adap); in zx2967_i2c_probe()
575 clk_disable_unprepare(i2c->clk); in zx2967_i2c_probe()
581 struct zx2967_i2c *i2c = platform_get_drvdata(pdev); in zx2967_i2c_remove() local
583 i2c_del_adapter(&i2c->adap); in zx2967_i2c_remove()
584 clk_disable_unprepare(i2c->clk); in zx2967_i2c_remove()
601 MODULE_DESCRIPTION("ZTE ZX2967 I2C Bus Controller driver");