18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * drivers/i2c/busses/i2c-mt7621.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2016 Michael Lee <igvtee@gmail.com> 78c2ecf20Sopenharmony_ci * Copyright (C) 2018 Jan Breuer <jan.breuer@jaybee.cz> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus. 108c2ecf20Sopenharmony_ci * (C) 2014 Sittisak <sittisaks@hotmail.com> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/clk.h> 148c2ecf20Sopenharmony_ci#include <linux/delay.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/io.h> 178c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 208c2ecf20Sopenharmony_ci#include <linux/reset.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define REG_SM0CFG2_REG 0x28 238c2ecf20Sopenharmony_ci#define REG_SM0CTL0_REG 0x40 248c2ecf20Sopenharmony_ci#define REG_SM0CTL1_REG 0x44 258c2ecf20Sopenharmony_ci#define REG_SM0D0_REG 0x50 268c2ecf20Sopenharmony_ci#define REG_SM0D1_REG 0x54 278c2ecf20Sopenharmony_ci#define REG_PINTEN_REG 0x5c 288c2ecf20Sopenharmony_ci#define REG_PINTST_REG 0x60 298c2ecf20Sopenharmony_ci#define REG_PINTCL_REG 0x64 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/* REG_SM0CFG2_REG */ 328c2ecf20Sopenharmony_ci#define SM0CFG2_IS_AUTOMODE BIT(0) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* REG_SM0CTL0_REG */ 358c2ecf20Sopenharmony_ci#define SM0CTL0_ODRAIN BIT(31) 368c2ecf20Sopenharmony_ci#define SM0CTL0_CLK_DIV_MASK (0x7ff << 16) 378c2ecf20Sopenharmony_ci#define SM0CTL0_CLK_DIV_MAX 0x7ff 388c2ecf20Sopenharmony_ci#define SM0CTL0_CS_STATUS BIT(4) 398c2ecf20Sopenharmony_ci#define SM0CTL0_SCL_STATE BIT(3) 408c2ecf20Sopenharmony_ci#define SM0CTL0_SDA_STATE BIT(2) 418c2ecf20Sopenharmony_ci#define SM0CTL0_EN BIT(1) 428c2ecf20Sopenharmony_ci#define SM0CTL0_SCL_STRETCH BIT(0) 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* REG_SM0CTL1_REG */ 458c2ecf20Sopenharmony_ci#define SM0CTL1_ACK_MASK (0xff << 16) 468c2ecf20Sopenharmony_ci#define SM0CTL1_PGLEN_MASK (0x7 << 8) 478c2ecf20Sopenharmony_ci#define SM0CTL1_PGLEN(x) ((((x) - 1) << 8) & SM0CTL1_PGLEN_MASK) 488c2ecf20Sopenharmony_ci#define SM0CTL1_READ (5 << 4) 498c2ecf20Sopenharmony_ci#define SM0CTL1_READ_LAST (4 << 4) 508c2ecf20Sopenharmony_ci#define SM0CTL1_STOP (3 << 4) 518c2ecf20Sopenharmony_ci#define SM0CTL1_WRITE (2 << 4) 528c2ecf20Sopenharmony_ci#define SM0CTL1_START (1 << 4) 538c2ecf20Sopenharmony_ci#define SM0CTL1_MODE_MASK (0x7 << 4) 548c2ecf20Sopenharmony_ci#define SM0CTL1_TRI BIT(0) 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* timeout waiting for I2C devices to respond */ 578c2ecf20Sopenharmony_ci#define TIMEOUT_MS 1000 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistruct mtk_i2c { 608c2ecf20Sopenharmony_ci void __iomem *base; 618c2ecf20Sopenharmony_ci struct device *dev; 628c2ecf20Sopenharmony_ci struct i2c_adapter adap; 638c2ecf20Sopenharmony_ci u32 bus_freq; 648c2ecf20Sopenharmony_ci u32 clk_div; 658c2ecf20Sopenharmony_ci u32 flags; 668c2ecf20Sopenharmony_ci struct clk *clk; 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic int mtk_i2c_wait_idle(struct mtk_i2c *i2c) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci int ret; 728c2ecf20Sopenharmony_ci u32 val; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci ret = readl_relaxed_poll_timeout(i2c->base + REG_SM0CTL1_REG, 758c2ecf20Sopenharmony_ci val, !(val & SM0CTL1_TRI), 768c2ecf20Sopenharmony_ci 10, TIMEOUT_MS * 1000); 778c2ecf20Sopenharmony_ci if (ret) 788c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "idle err(%d)\n", ret); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return ret; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic void mtk_i2c_reset(struct mtk_i2c *i2c) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci int ret; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci ret = device_reset(i2c->adap.dev.parent); 888c2ecf20Sopenharmony_ci if (ret) 898c2ecf20Sopenharmony_ci dev_err(i2c->dev, "I2C reset failed!\n"); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* 928c2ecf20Sopenharmony_ci * Don't set SM0CTL0_ODRAIN as its bit meaning is inverted. To 938c2ecf20Sopenharmony_ci * configure open-drain mode, this bit needs to be cleared. 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_ci iowrite32(((i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK) | SM0CTL0_EN | 968c2ecf20Sopenharmony_ci SM0CTL0_SCL_STRETCH, i2c->base + REG_SM0CTL0_REG); 978c2ecf20Sopenharmony_ci iowrite32(0, i2c->base + REG_SM0CFG2_REG); 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic void mtk_i2c_dump_reg(struct mtk_i2c *i2c) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, 1038c2ecf20Sopenharmony_ci "SM0CFG2 %08x, SM0CTL0 %08x, SM0CTL1 %08x, SM0D0 %08x, SM0D1 %08x\n", 1048c2ecf20Sopenharmony_ci ioread32(i2c->base + REG_SM0CFG2_REG), 1058c2ecf20Sopenharmony_ci ioread32(i2c->base + REG_SM0CTL0_REG), 1068c2ecf20Sopenharmony_ci ioread32(i2c->base + REG_SM0CTL1_REG), 1078c2ecf20Sopenharmony_ci ioread32(i2c->base + REG_SM0D0_REG), 1088c2ecf20Sopenharmony_ci ioread32(i2c->base + REG_SM0D1_REG)); 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic int mtk_i2c_check_ack(struct mtk_i2c *i2c, u32 expected) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci u32 ack = readl_relaxed(i2c->base + REG_SM0CTL1_REG); 1148c2ecf20Sopenharmony_ci u32 ack_expected = (expected << 16) & SM0CTL1_ACK_MASK; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci return ((ack & ack_expected) == ack_expected) ? 0 : -ENXIO; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic int mtk_i2c_master_start(struct mtk_i2c *i2c) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci iowrite32(SM0CTL1_START | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG); 1228c2ecf20Sopenharmony_ci return mtk_i2c_wait_idle(i2c); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic int mtk_i2c_master_stop(struct mtk_i2c *i2c) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci iowrite32(SM0CTL1_STOP | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG); 1288c2ecf20Sopenharmony_ci return mtk_i2c_wait_idle(i2c); 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic int mtk_i2c_master_cmd(struct mtk_i2c *i2c, u32 cmd, int page_len) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci iowrite32(cmd | SM0CTL1_TRI | SM0CTL1_PGLEN(page_len), 1348c2ecf20Sopenharmony_ci i2c->base + REG_SM0CTL1_REG); 1358c2ecf20Sopenharmony_ci return mtk_i2c_wait_idle(i2c); 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic int mtk_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, 1398c2ecf20Sopenharmony_ci int num) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct mtk_i2c *i2c; 1428c2ecf20Sopenharmony_ci struct i2c_msg *pmsg; 1438c2ecf20Sopenharmony_ci u16 addr; 1448c2ecf20Sopenharmony_ci int i, j, ret, len, page_len; 1458c2ecf20Sopenharmony_ci u32 cmd; 1468c2ecf20Sopenharmony_ci u32 data[2]; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci i2c = i2c_get_adapdata(adap); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) { 1518c2ecf20Sopenharmony_ci pmsg = &msgs[i]; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* wait hardware idle */ 1548c2ecf20Sopenharmony_ci ret = mtk_i2c_wait_idle(i2c); 1558c2ecf20Sopenharmony_ci if (ret) 1568c2ecf20Sopenharmony_ci goto err_timeout; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* start sequence */ 1598c2ecf20Sopenharmony_ci ret = mtk_i2c_master_start(i2c); 1608c2ecf20Sopenharmony_ci if (ret) 1618c2ecf20Sopenharmony_ci goto err_timeout; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* write address */ 1648c2ecf20Sopenharmony_ci if (pmsg->flags & I2C_M_TEN) { 1658c2ecf20Sopenharmony_ci /* 10 bits address */ 1668c2ecf20Sopenharmony_ci addr = 0xf0 | ((pmsg->addr >> 7) & 0x06); 1678c2ecf20Sopenharmony_ci addr |= (pmsg->addr & 0xff) << 8; 1688c2ecf20Sopenharmony_ci if (pmsg->flags & I2C_M_RD) 1698c2ecf20Sopenharmony_ci addr |= 1; 1708c2ecf20Sopenharmony_ci iowrite32(addr, i2c->base + REG_SM0D0_REG); 1718c2ecf20Sopenharmony_ci ret = mtk_i2c_master_cmd(i2c, SM0CTL1_WRITE, 2); 1728c2ecf20Sopenharmony_ci if (ret) 1738c2ecf20Sopenharmony_ci goto err_timeout; 1748c2ecf20Sopenharmony_ci } else { 1758c2ecf20Sopenharmony_ci /* 7 bits address */ 1768c2ecf20Sopenharmony_ci addr = i2c_8bit_addr_from_msg(pmsg); 1778c2ecf20Sopenharmony_ci iowrite32(addr, i2c->base + REG_SM0D0_REG); 1788c2ecf20Sopenharmony_ci ret = mtk_i2c_master_cmd(i2c, SM0CTL1_WRITE, 1); 1798c2ecf20Sopenharmony_ci if (ret) 1808c2ecf20Sopenharmony_ci goto err_timeout; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* check address ACK */ 1848c2ecf20Sopenharmony_ci if (!(pmsg->flags & I2C_M_IGNORE_NAK)) { 1858c2ecf20Sopenharmony_ci ret = mtk_i2c_check_ack(i2c, BIT(0)); 1868c2ecf20Sopenharmony_ci if (ret) 1878c2ecf20Sopenharmony_ci goto err_ack; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* transfer data */ 1918c2ecf20Sopenharmony_ci for (len = pmsg->len, j = 0; len > 0; len -= 8, j += 8) { 1928c2ecf20Sopenharmony_ci page_len = (len >= 8) ? 8 : len; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci if (pmsg->flags & I2C_M_RD) { 1958c2ecf20Sopenharmony_ci cmd = (len > 8) ? 1968c2ecf20Sopenharmony_ci SM0CTL1_READ : SM0CTL1_READ_LAST; 1978c2ecf20Sopenharmony_ci } else { 1988c2ecf20Sopenharmony_ci memcpy(data, &pmsg->buf[j], page_len); 1998c2ecf20Sopenharmony_ci iowrite32(data[0], i2c->base + REG_SM0D0_REG); 2008c2ecf20Sopenharmony_ci iowrite32(data[1], i2c->base + REG_SM0D1_REG); 2018c2ecf20Sopenharmony_ci cmd = SM0CTL1_WRITE; 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci ret = mtk_i2c_master_cmd(i2c, cmd, page_len); 2058c2ecf20Sopenharmony_ci if (ret) 2068c2ecf20Sopenharmony_ci goto err_timeout; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (pmsg->flags & I2C_M_RD) { 2098c2ecf20Sopenharmony_ci data[0] = ioread32(i2c->base + REG_SM0D0_REG); 2108c2ecf20Sopenharmony_ci data[1] = ioread32(i2c->base + REG_SM0D1_REG); 2118c2ecf20Sopenharmony_ci memcpy(&pmsg->buf[j], data, page_len); 2128c2ecf20Sopenharmony_ci } else { 2138c2ecf20Sopenharmony_ci if (!(pmsg->flags & I2C_M_IGNORE_NAK)) { 2148c2ecf20Sopenharmony_ci ret = mtk_i2c_check_ack(i2c, 2158c2ecf20Sopenharmony_ci (1 << page_len) 2168c2ecf20Sopenharmony_ci - 1); 2178c2ecf20Sopenharmony_ci if (ret) 2188c2ecf20Sopenharmony_ci goto err_ack; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci ret = mtk_i2c_master_stop(i2c); 2258c2ecf20Sopenharmony_ci if (ret) 2268c2ecf20Sopenharmony_ci goto err_timeout; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci /* the return value is number of executed messages */ 2298c2ecf20Sopenharmony_ci return i; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cierr_ack: 2328c2ecf20Sopenharmony_ci ret = mtk_i2c_master_stop(i2c); 2338c2ecf20Sopenharmony_ci if (ret) 2348c2ecf20Sopenharmony_ci goto err_timeout; 2358c2ecf20Sopenharmony_ci return -ENXIO; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cierr_timeout: 2388c2ecf20Sopenharmony_ci mtk_i2c_dump_reg(i2c); 2398c2ecf20Sopenharmony_ci mtk_i2c_reset(i2c); 2408c2ecf20Sopenharmony_ci return ret; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic u32 mtk_i2c_func(struct i2c_adapter *a) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic const struct i2c_algorithm mtk_i2c_algo = { 2498c2ecf20Sopenharmony_ci .master_xfer = mtk_i2c_master_xfer, 2508c2ecf20Sopenharmony_ci .functionality = mtk_i2c_func, 2518c2ecf20Sopenharmony_ci}; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic const struct of_device_id i2c_mtk_dt_ids[] = { 2548c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt7621-i2c" }, 2558c2ecf20Sopenharmony_ci { /* sentinel */ } 2568c2ecf20Sopenharmony_ci}; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, i2c_mtk_dt_ids); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_cistatic void mtk_i2c_init(struct mtk_i2c *i2c) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci i2c->clk_div = clk_get_rate(i2c->clk) / i2c->bus_freq - 1; 2638c2ecf20Sopenharmony_ci if (i2c->clk_div < 99) 2648c2ecf20Sopenharmony_ci i2c->clk_div = 99; 2658c2ecf20Sopenharmony_ci if (i2c->clk_div > SM0CTL0_CLK_DIV_MAX) 2668c2ecf20Sopenharmony_ci i2c->clk_div = SM0CTL0_CLK_DIV_MAX; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci mtk_i2c_reset(i2c); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic int mtk_i2c_probe(struct platform_device *pdev) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci struct resource *res; 2748c2ecf20Sopenharmony_ci struct mtk_i2c *i2c; 2758c2ecf20Sopenharmony_ci struct i2c_adapter *adap; 2768c2ecf20Sopenharmony_ci int ret; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci i2c = devm_kzalloc(&pdev->dev, sizeof(struct mtk_i2c), GFP_KERNEL); 2818c2ecf20Sopenharmony_ci if (!i2c) 2828c2ecf20Sopenharmony_ci return -ENOMEM; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci i2c->base = devm_ioremap_resource(&pdev->dev, res); 2858c2ecf20Sopenharmony_ci if (IS_ERR(i2c->base)) 2868c2ecf20Sopenharmony_ci return PTR_ERR(i2c->base); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci i2c->clk = devm_clk_get(&pdev->dev, NULL); 2898c2ecf20Sopenharmony_ci if (IS_ERR(i2c->clk)) { 2908c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "no clock defined\n"); 2918c2ecf20Sopenharmony_ci return PTR_ERR(i2c->clk); 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci ret = clk_prepare_enable(i2c->clk); 2948c2ecf20Sopenharmony_ci if (ret) { 2958c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Unable to enable clock\n"); 2968c2ecf20Sopenharmony_ci return ret; 2978c2ecf20Sopenharmony_ci } 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci i2c->dev = &pdev->dev; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci if (of_property_read_u32(pdev->dev.of_node, "clock-frequency", 3028c2ecf20Sopenharmony_ci &i2c->bus_freq)) 3038c2ecf20Sopenharmony_ci i2c->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci if (i2c->bus_freq == 0) { 3068c2ecf20Sopenharmony_ci dev_warn(i2c->dev, "clock-frequency 0 not supported\n"); 3078c2ecf20Sopenharmony_ci ret = -EINVAL; 3088c2ecf20Sopenharmony_ci goto err_disable_clk; 3098c2ecf20Sopenharmony_ci } 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci adap = &i2c->adap; 3128c2ecf20Sopenharmony_ci adap->owner = THIS_MODULE; 3138c2ecf20Sopenharmony_ci adap->algo = &mtk_i2c_algo; 3148c2ecf20Sopenharmony_ci adap->retries = 3; 3158c2ecf20Sopenharmony_ci adap->dev.parent = &pdev->dev; 3168c2ecf20Sopenharmony_ci i2c_set_adapdata(adap, i2c); 3178c2ecf20Sopenharmony_ci adap->dev.of_node = pdev->dev.of_node; 3188c2ecf20Sopenharmony_ci strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, i2c); 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci mtk_i2c_init(i2c); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci ret = i2c_add_adapter(adap); 3258c2ecf20Sopenharmony_ci if (ret < 0) 3268c2ecf20Sopenharmony_ci goto err_disable_clk; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "clock %u kHz\n", i2c->bus_freq / 1000); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return 0; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_cierr_disable_clk: 3338c2ecf20Sopenharmony_ci clk_disable_unprepare(i2c->clk); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return ret; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic int mtk_i2c_remove(struct platform_device *pdev) 3398c2ecf20Sopenharmony_ci{ 3408c2ecf20Sopenharmony_ci struct mtk_i2c *i2c = platform_get_drvdata(pdev); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci clk_disable_unprepare(i2c->clk); 3438c2ecf20Sopenharmony_ci i2c_del_adapter(&i2c->adap); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci return 0; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic struct platform_driver mtk_i2c_driver = { 3498c2ecf20Sopenharmony_ci .probe = mtk_i2c_probe, 3508c2ecf20Sopenharmony_ci .remove = mtk_i2c_remove, 3518c2ecf20Sopenharmony_ci .driver = { 3528c2ecf20Sopenharmony_ci .name = "i2c-mt7621", 3538c2ecf20Sopenharmony_ci .of_match_table = i2c_mtk_dt_ids, 3548c2ecf20Sopenharmony_ci }, 3558c2ecf20Sopenharmony_ci}; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_cimodule_platform_driver(mtk_i2c_driver); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Steven Liu"); 3608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MT7621 I2C host driver"); 3618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 3628c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:MT7621-I2C"); 363