23 #include <linux/kernel.h>
24 #include <linux/module.h>
29 #include <linux/i2c.h>
35 #include <linux/slab.h>
136 #define IIC_FLAG_HAS_ICIC67 (1 << 0)
138 #define NORMAL_SPEED 100000
149 #define ICCR_ICE 0x80
150 #define ICCR_RACK 0x40
151 #define ICCR_TRS 0x10
152 #define ICCR_BBSY 0x04
153 #define ICCR_SCP 0x01
155 #define ICSR_SCLM 0x80
156 #define ICSR_SDAM 0x40
158 #define ICSR_BUSY 0x10
160 #define ICSR_TACK 0x04
161 #define ICSR_WAIT 0x02
162 #define ICSR_DTE 0x01
164 #define ICIC_ICCLB8 0x80
165 #define ICIC_ICCHB8 0x40
166 #define ICIC_ALE 0x08
167 #define ICIC_TACKE 0x04
168 #define ICIC_WAITE 0x02
169 #define ICIC_DTEE 0x01
185 unsigned char set,
unsigned char clr)
187 iic_wr(pd, offs, (iic_rd(pd, offs) |
set) & ~clr);
198 pm_runtime_get_sync(pd->
dev);
211 tmp = num * 10 / denom;
219 if ((num/denom) > 0xff)
228 tmp = num * 10 / denom;
236 if ((num/denom) > 0xff)
264 pm_runtime_put_sync(pd->
dev);
270 unsigned char ret = 0;
273 dev_dbg(pd->
dev,
"op %d, data in 0x%02x\n", op, data);
279 iic_wr(pd,
ICCR, 0x94);
283 iic_wr(pd,
ICDR, data);
286 iic_wr(pd,
ICDR, data);
289 iic_wr(pd,
ICDR, data);
290 iic_wr(pd,
ICCR, 0x90);
293 iic_wr(pd,
ICCR, 0x81);
296 ret = iic_rd(pd,
ICDR);
301 iic_wr(pd,
ICCR, 0xc0);
306 ret = iic_rd(pd,
ICDR);
307 iic_wr(pd,
ICCR, 0xc0);
311 spin_unlock_irqrestore(&pd->
lock, flags);
313 dev_dbg(pd->
dev,
"op %d, data out 0x%02x\n", op, ret);
327 if (pd->
pos == (pd->
msg->len - 1))
338 *buf = (pd->
msg->addr & 0x7f) << 1;
342 *buf = pd->
msg->buf[pd->
pos];
350 if (pd->
pos == pd->
msg->len)
353 sh_mobile_i2c_get_data(pd, &data);
355 if (sh_mobile_i2c_is_last_byte(pd))
357 else if (sh_mobile_i2c_is_first_byte(pd))
360 i2c_op(pd,
OP_TX, data);
373 sh_mobile_i2c_get_data(pd, &data);
375 if (sh_mobile_i2c_is_first_byte(pd))
378 i2c_op(pd,
OP_TX, data);
387 real_pos = pd->
pos - 2;
389 if (pd->
pos == pd->
msg->len) {
396 data = i2c_op(pd,
OP_RX, 0);
403 return pd->
pos == (pd->
msg->len + 2);
413 sr = iic_rd(pd,
ICSR);
416 dev_dbg(pd->
dev,
"i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->
sr,
425 wakeup = sh_mobile_i2c_isr_rx(pd);
427 wakeup = sh_mobile_i2c_isr_tx(pd);
430 iic_wr(pd,
ICSR, sr & ~ICSR_WAIT);
443 dev_err(pd->
dev,
"Unsupported zero length i2c read\n");
479 for (i = 0; i < num; i++) {
482 err = start_ch(pd, msg);
493 dev_err(pd->
dev,
"Transfer request timed out\n");
497 val = iic_rd(pd,
ICSR);
499 dev_dbg(pd->
dev,
"val 0x%02x pd->sr 0x%02x\n", val, pd->
sr);
535 .functionality = sh_mobile_i2c_func,
536 .master_xfer = sh_mobile_i2c_xfer,
546 for (n = res->
start; hook && n <= res->
end; n++) {
548 dev_name(&dev->
dev), dev)) {
549 for (n--; n >= res->
start; n--)
559 return k > 0 ? 0 : -
ENOENT;
568 for (n = res->
start; n <= res->
end; n++)
588 dev_err(&dev->
dev,
"cannot allocate private data\n");
593 if (IS_ERR(pd->
clk)) {
595 ret = PTR_ERR(pd->
clk);
599 ret = sh_mobile_i2c_hook_irqs(dev, 1);
606 platform_set_drvdata(dev, pd);
610 dev_err(&dev->
dev,
"cannot find IO resource\n");
615 size = resource_size(res);
645 pm_suspend_ignore_children(&dev->
dev,
true);
650 i2c_set_adapdata(adap, pd);
653 adap->
algo = &sh_mobile_i2c_algorithm;
654 adap->
dev.parent = &dev->
dev;
657 adap->
dev.of_node = dev->
dev.of_node;
666 dev_err(&dev->
dev,
"cannot add numbered adapter\n");
670 dev_info(&dev->
dev,
"I2C adapter %d with bus speed %lu Hz\n",
679 sh_mobile_i2c_hook_irqs(dev, 0);
693 sh_mobile_i2c_hook_irqs(dev, 0);
695 pm_runtime_disable(&dev->
dev);
700 static int sh_mobile_i2c_runtime_nop(
struct device *dev)
712 static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
713 .runtime_suspend = sh_mobile_i2c_runtime_nop,
714 .runtime_resume = sh_mobile_i2c_runtime_nop,
718 { .compatible =
"renesas,rmobile-iic", },
725 .name =
"i2c-sh_mobile",
727 .pm = &sh_mobile_i2c_dev_pm_ops,
728 .of_match_table = sh_mobile_i2c_dt_ids,
730 .probe = sh_mobile_i2c_probe,
731 .remove = sh_mobile_i2c_remove,
734 static int __init sh_mobile_i2c_adap_init(
void)
739 static void __exit sh_mobile_i2c_adap_exit(
void)