Lines Matching full:i2c

3  * drivers/i2c/busses/i2c-mt7621.c
9 * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
15 #include <linux/i2c.h>
56 /* timeout waiting for I2C devices to respond */
69 static int mtk_i2c_wait_idle(struct mtk_i2c *i2c) in mtk_i2c_wait_idle() argument
74 ret = readl_relaxed_poll_timeout(i2c->base + REG_SM0CTL1_REG, in mtk_i2c_wait_idle()
78 dev_dbg(i2c->dev, "idle err(%d)\n", ret); in mtk_i2c_wait_idle()
83 static void mtk_i2c_reset(struct mtk_i2c *i2c) in mtk_i2c_reset() argument
87 ret = device_reset(i2c->adap.dev.parent); in mtk_i2c_reset()
89 dev_err(i2c->dev, "I2C reset failed!\n"); in mtk_i2c_reset()
95 iowrite32(((i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK) | SM0CTL0_EN | in mtk_i2c_reset()
96 SM0CTL0_SCL_STRETCH, i2c->base + REG_SM0CTL0_REG); in mtk_i2c_reset()
97 iowrite32(0, i2c->base + REG_SM0CFG2_REG); in mtk_i2c_reset()
100 static void mtk_i2c_dump_reg(struct mtk_i2c *i2c) in mtk_i2c_dump_reg() argument
102 dev_dbg(i2c->dev, in mtk_i2c_dump_reg()
104 ioread32(i2c->base + REG_SM0CFG2_REG), in mtk_i2c_dump_reg()
105 ioread32(i2c->base + REG_SM0CTL0_REG), in mtk_i2c_dump_reg()
106 ioread32(i2c->base + REG_SM0CTL1_REG), in mtk_i2c_dump_reg()
107 ioread32(i2c->base + REG_SM0D0_REG), in mtk_i2c_dump_reg()
108 ioread32(i2c->base + REG_SM0D1_REG)); in mtk_i2c_dump_reg()
111 static int mtk_i2c_check_ack(struct mtk_i2c *i2c, u32 expected) in mtk_i2c_check_ack() argument
113 u32 ack = readl_relaxed(i2c->base + REG_SM0CTL1_REG); in mtk_i2c_check_ack()
119 static int mtk_i2c_master_start(struct mtk_i2c *i2c) in mtk_i2c_master_start() argument
121 iowrite32(SM0CTL1_START | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG); in mtk_i2c_master_start()
122 return mtk_i2c_wait_idle(i2c); in mtk_i2c_master_start()
125 static int mtk_i2c_master_stop(struct mtk_i2c *i2c) in mtk_i2c_master_stop() argument
127 iowrite32(SM0CTL1_STOP | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG); in mtk_i2c_master_stop()
128 return mtk_i2c_wait_idle(i2c); in mtk_i2c_master_stop()
131 static int mtk_i2c_master_cmd(struct mtk_i2c *i2c, u32 cmd, int page_len) in mtk_i2c_master_cmd() argument
134 i2c->base + REG_SM0CTL1_REG); in mtk_i2c_master_cmd()
135 return mtk_i2c_wait_idle(i2c); in mtk_i2c_master_cmd()
141 struct mtk_i2c *i2c; in mtk_i2c_master_xfer() local
148 i2c = i2c_get_adapdata(adap); in mtk_i2c_master_xfer()
154 ret = mtk_i2c_wait_idle(i2c); in mtk_i2c_master_xfer()
159 ret = mtk_i2c_master_start(i2c); in mtk_i2c_master_xfer()
170 iowrite32(addr, i2c->base + REG_SM0D0_REG); in mtk_i2c_master_xfer()
171 ret = mtk_i2c_master_cmd(i2c, SM0CTL1_WRITE, 2); in mtk_i2c_master_xfer()
177 iowrite32(addr, i2c->base + REG_SM0D0_REG); in mtk_i2c_master_xfer()
178 ret = mtk_i2c_master_cmd(i2c, SM0CTL1_WRITE, 1); in mtk_i2c_master_xfer()
185 ret = mtk_i2c_check_ack(i2c, BIT(0)); in mtk_i2c_master_xfer()
199 iowrite32(data[0], i2c->base + REG_SM0D0_REG); in mtk_i2c_master_xfer()
200 iowrite32(data[1], i2c->base + REG_SM0D1_REG); in mtk_i2c_master_xfer()
204 ret = mtk_i2c_master_cmd(i2c, cmd, page_len); in mtk_i2c_master_xfer()
209 data[0] = ioread32(i2c->base + REG_SM0D0_REG); in mtk_i2c_master_xfer()
210 data[1] = ioread32(i2c->base + REG_SM0D1_REG); in mtk_i2c_master_xfer()
214 ret = mtk_i2c_check_ack(i2c, in mtk_i2c_master_xfer()
224 ret = mtk_i2c_master_stop(i2c); in mtk_i2c_master_xfer()
232 ret = mtk_i2c_master_stop(i2c); in mtk_i2c_master_xfer()
238 mtk_i2c_dump_reg(i2c); in mtk_i2c_master_xfer()
239 mtk_i2c_reset(i2c); in mtk_i2c_master_xfer()
254 { .compatible = "mediatek,mt7621-i2c" },
260 static void mtk_i2c_init(struct mtk_i2c *i2c) in mtk_i2c_init() argument
262 i2c->clk_div = clk_get_rate(i2c->clk) / i2c->bus_freq - 1; in mtk_i2c_init()
263 if (i2c->clk_div < 99) in mtk_i2c_init()
264 i2c->clk_div = 99; in mtk_i2c_init()
265 if (i2c->clk_div > SM0CTL0_CLK_DIV_MAX) in mtk_i2c_init()
266 i2c->clk_div = SM0CTL0_CLK_DIV_MAX; in mtk_i2c_init()
268 mtk_i2c_reset(i2c); in mtk_i2c_init()
274 struct mtk_i2c *i2c; in mtk_i2c_probe() local
280 i2c = devm_kzalloc(&pdev->dev, sizeof(struct mtk_i2c), GFP_KERNEL); in mtk_i2c_probe()
281 if (!i2c) in mtk_i2c_probe()
284 i2c->base = devm_ioremap_resource(&pdev->dev, res); in mtk_i2c_probe()
285 if (IS_ERR(i2c->base)) in mtk_i2c_probe()
286 return PTR_ERR(i2c->base); in mtk_i2c_probe()
288 i2c->clk = devm_clk_get(&pdev->dev, NULL); in mtk_i2c_probe()
289 if (IS_ERR(i2c->clk)) { in mtk_i2c_probe()
291 return PTR_ERR(i2c->clk); in mtk_i2c_probe()
293 ret = clk_prepare_enable(i2c->clk); in mtk_i2c_probe()
299 i2c->dev = &pdev->dev; in mtk_i2c_probe()
302 &i2c->bus_freq)) in mtk_i2c_probe()
303 i2c->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; in mtk_i2c_probe()
305 if (i2c->bus_freq == 0) { in mtk_i2c_probe()
306 dev_warn(i2c->dev, "clock-frequency 0 not supported\n"); in mtk_i2c_probe()
310 adap = &i2c->adap; in mtk_i2c_probe()
315 i2c_set_adapdata(adap, i2c); in mtk_i2c_probe()
319 platform_set_drvdata(pdev, i2c); in mtk_i2c_probe()
321 mtk_i2c_init(i2c); in mtk_i2c_probe()
327 dev_info(&pdev->dev, "clock %u kHz\n", i2c->bus_freq / 1000); in mtk_i2c_probe()
334 struct mtk_i2c *i2c = platform_get_drvdata(pdev); in mtk_i2c_remove() local
336 clk_disable_unprepare(i2c->clk); in mtk_i2c_remove()
337 i2c_del_adapter(&i2c->adap); in mtk_i2c_remove()
346 .name = "i2c-mt7621",
354 MODULE_DESCRIPTION("MT7621 I2C host driver");
356 MODULE_ALIAS("platform:MT7621-I2C");