1ce38815dSXudong Chen /* 2ce38815dSXudong Chen * Copyright (c) 2014 MediaTek Inc. 3ce38815dSXudong Chen * Author: Xudong Chen <xudong.chen@mediatek.com> 4ce38815dSXudong Chen * 5ce38815dSXudong Chen * This program is free software; you can redistribute it and/or modify 6ce38815dSXudong Chen * it under the terms of the GNU General Public License version 2 as 7ce38815dSXudong Chen * published by the Free Software Foundation. 8ce38815dSXudong Chen * 9ce38815dSXudong Chen * This program is distributed in the hope that it will be useful, 10ce38815dSXudong Chen * but WITHOUT ANY WARRANTY; without even the implied warranty of 11ce38815dSXudong Chen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12ce38815dSXudong Chen * GNU General Public License for more details. 13ce38815dSXudong Chen */ 14ce38815dSXudong Chen 15ce38815dSXudong Chen #include <linux/clk.h> 16ce38815dSXudong Chen #include <linux/completion.h> 17ce38815dSXudong Chen #include <linux/delay.h> 18ce38815dSXudong Chen #include <linux/device.h> 19ce38815dSXudong Chen #include <linux/dma-mapping.h> 20ce38815dSXudong Chen #include <linux/err.h> 21ce38815dSXudong Chen #include <linux/errno.h> 22ce38815dSXudong Chen #include <linux/i2c.h> 23ce38815dSXudong Chen #include <linux/init.h> 24ce38815dSXudong Chen #include <linux/interrupt.h> 25ce38815dSXudong Chen #include <linux/io.h> 26ce38815dSXudong Chen #include <linux/kernel.h> 27ce38815dSXudong Chen #include <linux/mm.h> 28ce38815dSXudong Chen #include <linux/module.h> 29ce38815dSXudong Chen #include <linux/of_address.h> 306e29577fSRyder Lee #include <linux/of_device.h> 31ce38815dSXudong Chen #include <linux/of_irq.h> 32ce38815dSXudong Chen #include <linux/platform_device.h> 33ce38815dSXudong Chen #include <linux/scatterlist.h> 34ce38815dSXudong Chen #include <linux/sched.h> 35ce38815dSXudong Chen #include <linux/slab.h> 36ce38815dSXudong Chen 37b2ed11e2SEddie Huang #define I2C_RS_TRANSFER (1 << 4) 38cad6dc5dSQii Wang #define I2C_ARB_LOST (1 << 3) 39ce38815dSXudong Chen #define I2C_HS_NACKERR (1 << 2) 40ce38815dSXudong Chen #define I2C_ACKERR (1 << 1) 41ce38815dSXudong Chen #define I2C_TRANSAC_COMP (1 << 0) 42ce38815dSXudong Chen #define I2C_TRANSAC_START (1 << 0) 43b2ed11e2SEddie Huang #define I2C_RS_MUL_CNFG (1 << 15) 44b2ed11e2SEddie Huang #define I2C_RS_MUL_TRIG (1 << 14) 45ce38815dSXudong Chen #define I2C_DCM_DISABLE 0x0000 46ce38815dSXudong Chen #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003 47ce38815dSXudong Chen #define I2C_IO_CONFIG_PUSH_PULL 0x0000 48ce38815dSXudong Chen #define I2C_SOFT_RST 0x0001 49ce38815dSXudong Chen #define I2C_FIFO_ADDR_CLR 0x0001 50ce38815dSXudong Chen #define I2C_DELAY_LEN 0x0002 51ce38815dSXudong Chen #define I2C_ST_START_CON 0x8001 52ce38815dSXudong Chen #define I2C_FS_START_CON 0x1800 53ce38815dSXudong Chen #define I2C_TIME_CLR_VALUE 0x0000 54ce38815dSXudong Chen #define I2C_TIME_DEFAULT_VALUE 0x0003 55ce38815dSXudong Chen #define I2C_WRRD_TRANAC_VALUE 0x0002 56ce38815dSXudong Chen #define I2C_RD_TRANAC_VALUE 0x0001 57ce38815dSXudong Chen 58ce38815dSXudong Chen #define I2C_DMA_CON_TX 0x0000 59ce38815dSXudong Chen #define I2C_DMA_CON_RX 0x0001 60ce38815dSXudong Chen #define I2C_DMA_START_EN 0x0001 61ce38815dSXudong Chen #define I2C_DMA_INT_FLAG_NONE 0x0000 62ce38815dSXudong Chen #define I2C_DMA_CLR_FLAG 0x0000 63ea89ef1fSEddie Huang #define I2C_DMA_HARD_RST 0x0002 64f4f4fed6SLiguo Zhang #define I2C_DMA_4G_MODE 0x0001 65ce38815dSXudong Chen 665a10e7d7SJun Gao #define I2C_DEFAULT_CLK_DIV 5 67ce38815dSXudong Chen #define I2C_DEFAULT_SPEED 100000 /* hz */ 68ce38815dSXudong Chen #define MAX_FS_MODE_SPEED 400000 69ce38815dSXudong Chen #define MAX_HS_MODE_SPEED 3400000 70ce38815dSXudong Chen #define MAX_SAMPLE_CNT_DIV 8 71ce38815dSXudong Chen #define MAX_STEP_CNT_DIV 64 72ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV 8 73ce38815dSXudong Chen 74ce38815dSXudong Chen #define I2C_CONTROL_RS (0x1 << 1) 75ce38815dSXudong Chen #define I2C_CONTROL_DMA_EN (0x1 << 2) 76ce38815dSXudong Chen #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3) 77ce38815dSXudong Chen #define I2C_CONTROL_DIR_CHANGE (0x1 << 4) 78ce38815dSXudong Chen #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5) 79ce38815dSXudong Chen #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6) 80*a15c91baSQii Wang #define I2C_CONTROL_DMAACK_EN (0x1 << 8) 81*a15c91baSQii Wang #define I2C_CONTROL_ASYNC_MODE (0x1 << 9) 82ce38815dSXudong Chen #define I2C_CONTROL_WRAPPER (0x1 << 0) 83ce38815dSXudong Chen 84ce38815dSXudong Chen #define I2C_DRV_NAME "i2c-mt65xx" 85ce38815dSXudong Chen 86ce38815dSXudong Chen enum DMA_REGS_OFFSET { 87ce38815dSXudong Chen OFFSET_INT_FLAG = 0x0, 88ce38815dSXudong Chen OFFSET_INT_EN = 0x04, 89ce38815dSXudong Chen OFFSET_EN = 0x08, 90ea89ef1fSEddie Huang OFFSET_RST = 0x0c, 91ce38815dSXudong Chen OFFSET_CON = 0x18, 92ce38815dSXudong Chen OFFSET_TX_MEM_ADDR = 0x1c, 93ce38815dSXudong Chen OFFSET_RX_MEM_ADDR = 0x20, 94ce38815dSXudong Chen OFFSET_TX_LEN = 0x24, 95ce38815dSXudong Chen OFFSET_RX_LEN = 0x28, 96f4f4fed6SLiguo Zhang OFFSET_TX_4G_MODE = 0x54, 97f4f4fed6SLiguo Zhang OFFSET_RX_4G_MODE = 0x58, 98ce38815dSXudong Chen }; 99ce38815dSXudong Chen 100ce38815dSXudong Chen enum i2c_trans_st_rs { 101ce38815dSXudong Chen I2C_TRANS_STOP = 0, 102ce38815dSXudong Chen I2C_TRANS_REPEATED_START, 103ce38815dSXudong Chen }; 104ce38815dSXudong Chen 105ce38815dSXudong Chen enum mtk_trans_op { 106ce38815dSXudong Chen I2C_MASTER_WR = 1, 107ce38815dSXudong Chen I2C_MASTER_RD, 108ce38815dSXudong Chen I2C_MASTER_WRRD, 109ce38815dSXudong Chen }; 110ce38815dSXudong Chen 111ce38815dSXudong Chen enum I2C_REGS_OFFSET { 112bc6eaf17SQii Wang OFFSET_DATA_PORT, 113bc6eaf17SQii Wang OFFSET_SLAVE_ADDR, 114bc6eaf17SQii Wang OFFSET_INTR_MASK, 115bc6eaf17SQii Wang OFFSET_INTR_STAT, 116bc6eaf17SQii Wang OFFSET_CONTROL, 117bc6eaf17SQii Wang OFFSET_TRANSFER_LEN, 118bc6eaf17SQii Wang OFFSET_TRANSAC_LEN, 119bc6eaf17SQii Wang OFFSET_DELAY_LEN, 120bc6eaf17SQii Wang OFFSET_TIMING, 121bc6eaf17SQii Wang OFFSET_START, 122bc6eaf17SQii Wang OFFSET_EXT_CONF, 123bc6eaf17SQii Wang OFFSET_FIFO_STAT, 124bc6eaf17SQii Wang OFFSET_FIFO_THRESH, 125bc6eaf17SQii Wang OFFSET_FIFO_ADDR_CLR, 126bc6eaf17SQii Wang OFFSET_IO_CONFIG, 127bc6eaf17SQii Wang OFFSET_RSV_DEBUG, 128bc6eaf17SQii Wang OFFSET_HS, 129bc6eaf17SQii Wang OFFSET_SOFTRESET, 130bc6eaf17SQii Wang OFFSET_DCM_EN, 131bc6eaf17SQii Wang OFFSET_PATH_DIR, 132bc6eaf17SQii Wang OFFSET_DEBUGSTAT, 133bc6eaf17SQii Wang OFFSET_DEBUGCTRL, 134bc6eaf17SQii Wang OFFSET_TRANSFER_LEN_AUX, 135bc6eaf17SQii Wang OFFSET_CLOCK_DIV, 136bc6eaf17SQii Wang }; 137bc6eaf17SQii Wang 138bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = { 139bc6eaf17SQii Wang [OFFSET_DATA_PORT] = 0x0, 140bc6eaf17SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 141bc6eaf17SQii Wang [OFFSET_INTR_MASK] = 0x8, 142bc6eaf17SQii Wang [OFFSET_INTR_STAT] = 0xc, 143bc6eaf17SQii Wang [OFFSET_CONTROL] = 0x10, 144bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 145bc6eaf17SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 146bc6eaf17SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 147bc6eaf17SQii Wang [OFFSET_TIMING] = 0x20, 148bc6eaf17SQii Wang [OFFSET_START] = 0x24, 149bc6eaf17SQii Wang [OFFSET_EXT_CONF] = 0x28, 150bc6eaf17SQii Wang [OFFSET_FIFO_STAT] = 0x30, 151bc6eaf17SQii Wang [OFFSET_FIFO_THRESH] = 0x34, 152bc6eaf17SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 153bc6eaf17SQii Wang [OFFSET_IO_CONFIG] = 0x40, 154bc6eaf17SQii Wang [OFFSET_RSV_DEBUG] = 0x44, 155bc6eaf17SQii Wang [OFFSET_HS] = 0x48, 156bc6eaf17SQii Wang [OFFSET_SOFTRESET] = 0x50, 157bc6eaf17SQii Wang [OFFSET_DCM_EN] = 0x54, 158bc6eaf17SQii Wang [OFFSET_PATH_DIR] = 0x60, 159bc6eaf17SQii Wang [OFFSET_DEBUGSTAT] = 0x64, 160bc6eaf17SQii Wang [OFFSET_DEBUGCTRL] = 0x68, 161bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x6c, 162bc6eaf17SQii Wang [OFFSET_CLOCK_DIV] = 0x70, 163ce38815dSXudong Chen }; 164ce38815dSXudong Chen 165ce38815dSXudong Chen struct mtk_i2c_compatible { 166ce38815dSXudong Chen const struct i2c_adapter_quirks *quirks; 167bc6eaf17SQii Wang const u16 *regs; 168ce38815dSXudong Chen unsigned char pmic_i2c: 1; 169ce38815dSXudong Chen unsigned char dcm: 1; 170b2ed11e2SEddie Huang unsigned char auto_restart: 1; 171173b77e8SLiguo Zhang unsigned char aux_len_reg: 1; 172f4f4fed6SLiguo Zhang unsigned char support_33bits: 1; 1735a10e7d7SJun Gao unsigned char timing_adjust: 1; 174*a15c91baSQii Wang unsigned char dma_sync: 1; 175ce38815dSXudong Chen }; 176ce38815dSXudong Chen 177ce38815dSXudong Chen struct mtk_i2c { 178ce38815dSXudong Chen struct i2c_adapter adap; /* i2c host adapter */ 179ce38815dSXudong Chen struct device *dev; 180ce38815dSXudong Chen struct completion msg_complete; 181ce38815dSXudong Chen 182ce38815dSXudong Chen /* set in i2c probe */ 183ce38815dSXudong Chen void __iomem *base; /* i2c base addr */ 184ce38815dSXudong Chen void __iomem *pdmabase; /* dma base address*/ 185ce38815dSXudong Chen struct clk *clk_main; /* main clock for i2c bus */ 186ce38815dSXudong Chen struct clk *clk_dma; /* DMA clock for i2c via DMA */ 187ce38815dSXudong Chen struct clk *clk_pmic; /* PMIC clock for i2c from PMIC */ 188cad6dc5dSQii Wang struct clk *clk_arb; /* Arbitrator clock for i2c */ 189ce38815dSXudong Chen bool have_pmic; /* can use i2c pins from PMIC */ 190ce38815dSXudong Chen bool use_push_pull; /* IO config push-pull mode */ 191ce38815dSXudong Chen 192ce38815dSXudong Chen u16 irq_stat; /* interrupt status */ 193f2326401SJun Gao unsigned int clk_src_div; 194ce38815dSXudong Chen unsigned int speed_hz; /* The speed in transfer */ 195ce38815dSXudong Chen enum mtk_trans_op op; 196ce38815dSXudong Chen u16 timing_reg; 197ce38815dSXudong Chen u16 high_speed_reg; 198173b77e8SLiguo Zhang unsigned char auto_restart; 1998378d01fSLiguo Zhang bool ignore_restart_irq; 200ce38815dSXudong Chen const struct mtk_i2c_compatible *dev_comp; 201ce38815dSXudong Chen }; 202ce38815dSXudong Chen 203ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = { 204ce38815dSXudong Chen .flags = I2C_AQ_COMB_WRITE_THEN_READ, 205ce38815dSXudong Chen .max_num_msgs = 1, 206ce38815dSXudong Chen .max_write_len = 255, 207ce38815dSXudong Chen .max_read_len = 255, 208ce38815dSXudong Chen .max_comb_1st_msg_len = 255, 209ce38815dSXudong Chen .max_comb_2nd_msg_len = 31, 210ce38815dSXudong Chen }; 211ce38815dSXudong Chen 2121304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = { 2131304fe09SJun Gao .max_num_msgs = 255, 2141304fe09SJun Gao }; 2151304fe09SJun Gao 2165a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = { 217bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 2185a10e7d7SJun Gao .pmic_i2c = 0, 2195a10e7d7SJun Gao .dcm = 1, 2205a10e7d7SJun Gao .auto_restart = 1, 2215a10e7d7SJun Gao .aux_len_reg = 1, 2225a10e7d7SJun Gao .support_33bits = 1, 2235a10e7d7SJun Gao .timing_adjust = 1, 224*a15c91baSQii Wang .dma_sync = 0, 2255a10e7d7SJun Gao }; 2265a10e7d7SJun Gao 227ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = { 228ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 229bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 230ce38815dSXudong Chen .pmic_i2c = 0, 231ce38815dSXudong Chen .dcm = 1, 232b2ed11e2SEddie Huang .auto_restart = 0, 233173b77e8SLiguo Zhang .aux_len_reg = 0, 234f4f4fed6SLiguo Zhang .support_33bits = 0, 2355a10e7d7SJun Gao .timing_adjust = 0, 236*a15c91baSQii Wang .dma_sync = 0, 237ce38815dSXudong Chen }; 238ce38815dSXudong Chen 239ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = { 240ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 241bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 242ce38815dSXudong Chen .pmic_i2c = 1, 243ce38815dSXudong Chen .dcm = 0, 244b2ed11e2SEddie Huang .auto_restart = 0, 245173b77e8SLiguo Zhang .aux_len_reg = 0, 246f4f4fed6SLiguo Zhang .support_33bits = 0, 2475a10e7d7SJun Gao .timing_adjust = 0, 248*a15c91baSQii Wang .dma_sync = 0, 249b2ed11e2SEddie Huang }; 250b2ed11e2SEddie Huang 2511304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = { 2521304fe09SJun Gao .quirks = &mt7622_i2c_quirks, 253bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 2541304fe09SJun Gao .pmic_i2c = 0, 2551304fe09SJun Gao .dcm = 1, 2561304fe09SJun Gao .auto_restart = 1, 2571304fe09SJun Gao .aux_len_reg = 1, 2581304fe09SJun Gao .support_33bits = 0, 2595a10e7d7SJun Gao .timing_adjust = 0, 260*a15c91baSQii Wang .dma_sync = 0, 2611304fe09SJun Gao }; 2621304fe09SJun Gao 263b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = { 264bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 265b2ed11e2SEddie Huang .pmic_i2c = 0, 266b2ed11e2SEddie Huang .dcm = 1, 267b2ed11e2SEddie Huang .auto_restart = 1, 268173b77e8SLiguo Zhang .aux_len_reg = 1, 269f4f4fed6SLiguo Zhang .support_33bits = 1, 2705a10e7d7SJun Gao .timing_adjust = 0, 271*a15c91baSQii Wang .dma_sync = 0, 272ce38815dSXudong Chen }; 273ce38815dSXudong Chen 274ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = { 2755a10e7d7SJun Gao { .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat }, 276ce38815dSXudong Chen { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat }, 277ce38815dSXudong Chen { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat }, 2781304fe09SJun Gao { .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat }, 279b2ed11e2SEddie Huang { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat }, 280ce38815dSXudong Chen {} 281ce38815dSXudong Chen }; 282ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); 283ce38815dSXudong Chen 284bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg) 285bc6eaf17SQii Wang { 286bc6eaf17SQii Wang return readw(i2c->base + i2c->dev_comp->regs[reg]); 287bc6eaf17SQii Wang } 288bc6eaf17SQii Wang 289bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val, 290bc6eaf17SQii Wang enum I2C_REGS_OFFSET reg) 291bc6eaf17SQii Wang { 292bc6eaf17SQii Wang writew(val, i2c->base + i2c->dev_comp->regs[reg]); 293bc6eaf17SQii Wang } 294bc6eaf17SQii Wang 295ce38815dSXudong Chen static int mtk_i2c_clock_enable(struct mtk_i2c *i2c) 296ce38815dSXudong Chen { 297ce38815dSXudong Chen int ret; 298ce38815dSXudong Chen 299ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_dma); 300ce38815dSXudong Chen if (ret) 301ce38815dSXudong Chen return ret; 302ce38815dSXudong Chen 303ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_main); 304ce38815dSXudong Chen if (ret) 305ce38815dSXudong Chen goto err_main; 306ce38815dSXudong Chen 307ce38815dSXudong Chen if (i2c->have_pmic) { 308ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_pmic); 309ce38815dSXudong Chen if (ret) 310ce38815dSXudong Chen goto err_pmic; 311ce38815dSXudong Chen } 312cad6dc5dSQii Wang 313cad6dc5dSQii Wang if (i2c->clk_arb) { 314cad6dc5dSQii Wang ret = clk_prepare_enable(i2c->clk_arb); 315cad6dc5dSQii Wang if (ret) 316cad6dc5dSQii Wang goto err_arb; 317cad6dc5dSQii Wang } 318cad6dc5dSQii Wang 319ce38815dSXudong Chen return 0; 320ce38815dSXudong Chen 321cad6dc5dSQii Wang err_arb: 322cad6dc5dSQii Wang if (i2c->have_pmic) 323cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_pmic); 324ce38815dSXudong Chen err_pmic: 325ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 326ce38815dSXudong Chen err_main: 327ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 328ce38815dSXudong Chen 329ce38815dSXudong Chen return ret; 330ce38815dSXudong Chen } 331ce38815dSXudong Chen 332ce38815dSXudong Chen static void mtk_i2c_clock_disable(struct mtk_i2c *i2c) 333ce38815dSXudong Chen { 334cad6dc5dSQii Wang if (i2c->clk_arb) 335cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_arb); 336cad6dc5dSQii Wang 337ce38815dSXudong Chen if (i2c->have_pmic) 338ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_pmic); 339ce38815dSXudong Chen 340ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 341ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 342ce38815dSXudong Chen } 343ce38815dSXudong Chen 344ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c) 345ce38815dSXudong Chen { 346ce38815dSXudong Chen u16 control_reg; 347ce38815dSXudong Chen 348bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 349ce38815dSXudong Chen 350ce38815dSXudong Chen /* Set ioconfig */ 351ce38815dSXudong Chen if (i2c->use_push_pull) 352bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 353ce38815dSXudong Chen else 354bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 355ce38815dSXudong Chen 356ce38815dSXudong Chen if (i2c->dev_comp->dcm) 357bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 358ce38815dSXudong Chen 3595a10e7d7SJun Gao if (i2c->dev_comp->timing_adjust) 360bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DEFAULT_CLK_DIV - 1, OFFSET_CLOCK_DIV); 3615a10e7d7SJun Gao 362bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 363bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 364ce38815dSXudong Chen 365ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 366ce38815dSXudong Chen if (i2c->have_pmic) 367bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 368ce38815dSXudong Chen 369ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 370ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 371*a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 372*a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 373*a15c91baSQii Wang 374bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 375bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 376ea89ef1fSEddie Huang 377ea89ef1fSEddie Huang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 378ea89ef1fSEddie Huang udelay(50); 379ea89ef1fSEddie Huang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 380ce38815dSXudong Chen } 381ce38815dSXudong Chen 382ce38815dSXudong Chen /* 383ce38815dSXudong Chen * Calculate i2c port speed 384ce38815dSXudong Chen * 385ce38815dSXudong Chen * Hardware design: 386ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 387ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 388ce38815dSXudong Chen * 389ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 390ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 391ce38815dSXudong Chen * sample_cnt and step_cn 392ce38815dSXudong Chen */ 393f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 394f2326401SJun Gao unsigned int target_speed, 395f2326401SJun Gao unsigned int *timing_step_cnt, 396f2326401SJun Gao unsigned int *timing_sample_cnt) 397ce38815dSXudong Chen { 398ce38815dSXudong Chen unsigned int step_cnt; 399ce38815dSXudong Chen unsigned int sample_cnt; 400ce38815dSXudong Chen unsigned int max_step_cnt; 401ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 402ce38815dSXudong Chen unsigned int base_step_cnt; 403ce38815dSXudong Chen unsigned int opt_div; 404ce38815dSXudong Chen unsigned int best_mul; 405ce38815dSXudong Chen unsigned int cnt_mul; 406ce38815dSXudong Chen 407ce38815dSXudong Chen if (target_speed > MAX_HS_MODE_SPEED) 408ce38815dSXudong Chen target_speed = MAX_HS_MODE_SPEED; 409ce38815dSXudong Chen 410ce38815dSXudong Chen if (target_speed > MAX_FS_MODE_SPEED) 411ce38815dSXudong Chen max_step_cnt = MAX_HS_STEP_CNT_DIV; 412ce38815dSXudong Chen else 413ce38815dSXudong Chen max_step_cnt = MAX_STEP_CNT_DIV; 414ce38815dSXudong Chen 415ce38815dSXudong Chen base_step_cnt = max_step_cnt; 416ce38815dSXudong Chen /* Find the best combination */ 417ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 418ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 419ce38815dSXudong Chen 420ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 421ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 422ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 423ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 424ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 425ce38815dSXudong Chen */ 426ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 427ce38815dSXudong Chen step_cnt = DIV_ROUND_UP(opt_div, sample_cnt); 428ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 429ce38815dSXudong Chen if (step_cnt > max_step_cnt) 430ce38815dSXudong Chen continue; 431ce38815dSXudong Chen 432ce38815dSXudong Chen if (cnt_mul < best_mul) { 433ce38815dSXudong Chen best_mul = cnt_mul; 434ce38815dSXudong Chen base_sample_cnt = sample_cnt; 435ce38815dSXudong Chen base_step_cnt = step_cnt; 436ce38815dSXudong Chen if (best_mul == opt_div) 437ce38815dSXudong Chen break; 438ce38815dSXudong Chen } 439ce38815dSXudong Chen } 440ce38815dSXudong Chen 441ce38815dSXudong Chen sample_cnt = base_sample_cnt; 442ce38815dSXudong Chen step_cnt = base_step_cnt; 443ce38815dSXudong Chen 444ce38815dSXudong Chen if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) { 445ce38815dSXudong Chen /* In this case, hardware can't support such 446ce38815dSXudong Chen * low i2c_bus_freq 447ce38815dSXudong Chen */ 448ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 449ce38815dSXudong Chen return -EINVAL; 450ce38815dSXudong Chen } 451ce38815dSXudong Chen 452f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 453f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 454f2326401SJun Gao 455f2326401SJun Gao return 0; 456f2326401SJun Gao } 457f2326401SJun Gao 458f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 459f2326401SJun Gao { 460f2326401SJun Gao unsigned int clk_src; 461f2326401SJun Gao unsigned int step_cnt; 462f2326401SJun Gao unsigned int sample_cnt; 463f2326401SJun Gao unsigned int target_speed; 464f2326401SJun Gao int ret; 465f2326401SJun Gao 466f2326401SJun Gao clk_src = parent_clk / i2c->clk_src_div; 467f2326401SJun Gao target_speed = i2c->speed_hz; 468ce38815dSXudong Chen 469ce38815dSXudong Chen if (target_speed > MAX_FS_MODE_SPEED) { 470f2326401SJun Gao /* Set master code speed register */ 471f2326401SJun Gao ret = mtk_i2c_calculate_speed(i2c, clk_src, MAX_FS_MODE_SPEED, 472f2326401SJun Gao &step_cnt, &sample_cnt); 473f2326401SJun Gao if (ret < 0) 474f2326401SJun Gao return ret; 475f2326401SJun Gao 476f2326401SJun Gao i2c->timing_reg = (sample_cnt << 8) | step_cnt; 477f2326401SJun Gao 478ce38815dSXudong Chen /* Set the high speed mode register */ 479f2326401SJun Gao ret = mtk_i2c_calculate_speed(i2c, clk_src, target_speed, 480f2326401SJun Gao &step_cnt, &sample_cnt); 481f2326401SJun Gao if (ret < 0) 482f2326401SJun Gao return ret; 483f2326401SJun Gao 484ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 485ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 486ce38815dSXudong Chen } else { 487f2326401SJun Gao ret = mtk_i2c_calculate_speed(i2c, clk_src, target_speed, 488f2326401SJun Gao &step_cnt, &sample_cnt); 489f2326401SJun Gao if (ret < 0) 490f2326401SJun Gao return ret; 491f2326401SJun Gao 492f2326401SJun Gao i2c->timing_reg = (sample_cnt << 8) | step_cnt; 493f2326401SJun Gao 494ce38815dSXudong Chen /* Disable the high speed transaction */ 495ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 496ce38815dSXudong Chen } 497ce38815dSXudong Chen 498ce38815dSXudong Chen return 0; 499ce38815dSXudong Chen } 500ce38815dSXudong Chen 501f4f4fed6SLiguo Zhang static inline u32 mtk_i2c_set_4g_mode(dma_addr_t addr) 502f4f4fed6SLiguo Zhang { 503f4f4fed6SLiguo Zhang return (addr & BIT_ULL(32)) ? I2C_DMA_4G_MODE : I2C_DMA_CLR_FLAG; 504f4f4fed6SLiguo Zhang } 505f4f4fed6SLiguo Zhang 506b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 507b2ed11e2SEddie Huang int num, int left_num) 508ce38815dSXudong Chen { 509ce38815dSXudong Chen u16 addr_reg; 510b2ed11e2SEddie Huang u16 start_reg; 511ce38815dSXudong Chen u16 control_reg; 512b2ed11e2SEddie Huang u16 restart_flag = 0; 513f4f4fed6SLiguo Zhang u32 reg_4g_mode; 514fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 515fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 516ce38815dSXudong Chen dma_addr_t rpaddr = 0; 517ce38815dSXudong Chen dma_addr_t wpaddr = 0; 518ce38815dSXudong Chen int ret; 519ce38815dSXudong Chen 520ce38815dSXudong Chen i2c->irq_stat = 0; 521ce38815dSXudong Chen 522173b77e8SLiguo Zhang if (i2c->auto_restart) 523b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 524b2ed11e2SEddie Huang 525ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 526ce38815dSXudong Chen 527bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 528ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 52993caa0daSqii wang if ((i2c->speed_hz > MAX_FS_MODE_SPEED) || (left_num >= 1)) 530ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 531ce38815dSXudong Chen 532ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 533ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 534ce38815dSXudong Chen 535bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 536ce38815dSXudong Chen 537ce38815dSXudong Chen /* set start condition */ 53893caa0daSqii wang if (i2c->speed_hz <= I2C_DEFAULT_SPEED) 539bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_ST_START_CON, OFFSET_EXT_CONF); 540ce38815dSXudong Chen else 541bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FS_START_CON, OFFSET_EXT_CONF); 542ce38815dSXudong Chen 5430d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 544bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 545ce38815dSXudong Chen 546ce38815dSXudong Chen /* Clear interrupt status */ 547bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 548cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 549bc6eaf17SQii Wang 550bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 551ce38815dSXudong Chen 552ce38815dSXudong Chen /* Enable interrupt */ 553bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 554cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 555ce38815dSXudong Chen 556ce38815dSXudong Chen /* Set transfer and transaction len */ 557ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 558173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 559bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 560bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 561173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 562173b77e8SLiguo Zhang } else { 563bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 564bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 565173b77e8SLiguo Zhang } 566bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 567ce38815dSXudong Chen } else { 568bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 569bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 570ce38815dSXudong Chen } 571ce38815dSXudong Chen 572ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 573ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 574ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 575ce38815dSXudong Chen writel(I2C_DMA_CON_RX, i2c->pdmabase + OFFSET_CON); 576fc66b39fSJun Gao 577bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 578fc66b39fSJun Gao if (!dma_rd_buf) 579ce38815dSXudong Chen return -ENOMEM; 580f4f4fed6SLiguo Zhang 581fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 582fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 583fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 584fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 585fc66b39fSJun Gao 586fc66b39fSJun Gao return -ENOMEM; 587fc66b39fSJun Gao } 588fc66b39fSJun Gao 589f4f4fed6SLiguo Zhang if (i2c->dev_comp->support_33bits) { 590f4f4fed6SLiguo Zhang reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr); 591f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 592f4f4fed6SLiguo Zhang } 593f4f4fed6SLiguo Zhang 594ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 595ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 596ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 597ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 598ce38815dSXudong Chen writel(I2C_DMA_CON_TX, i2c->pdmabase + OFFSET_CON); 599fc66b39fSJun Gao 600bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 601fc66b39fSJun Gao if (!dma_wr_buf) 602ce38815dSXudong Chen return -ENOMEM; 603f4f4fed6SLiguo Zhang 604fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 605fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 606fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 607fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 608fc66b39fSJun Gao 609fc66b39fSJun Gao return -ENOMEM; 610fc66b39fSJun Gao } 611fc66b39fSJun Gao 612f4f4fed6SLiguo Zhang if (i2c->dev_comp->support_33bits) { 613f4f4fed6SLiguo Zhang reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr); 614f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 615f4f4fed6SLiguo Zhang } 616f4f4fed6SLiguo Zhang 617ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 618ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 619ce38815dSXudong Chen } else { 620ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 621ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_CON); 622fc66b39fSJun Gao 623bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 624fc66b39fSJun Gao if (!dma_wr_buf) 625ce38815dSXudong Chen return -ENOMEM; 626fc66b39fSJun Gao 627fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 628fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 629fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 630fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 631fc66b39fSJun Gao 632fc66b39fSJun Gao return -ENOMEM; 633fc66b39fSJun Gao } 634fc66b39fSJun Gao 635bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 636fc66b39fSJun Gao if (!dma_rd_buf) { 637fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 638fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 639fc66b39fSJun Gao 640fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 641fc66b39fSJun Gao 642fc66b39fSJun Gao return -ENOMEM; 643fc66b39fSJun Gao } 644fc66b39fSJun Gao 645fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 646ce38815dSXudong Chen (msgs + 1)->len, 647ce38815dSXudong Chen DMA_FROM_DEVICE); 648ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 649ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 650ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 651fc66b39fSJun Gao 652fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 653fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 654fc66b39fSJun Gao 655ce38815dSXudong Chen return -ENOMEM; 656ce38815dSXudong Chen } 657f4f4fed6SLiguo Zhang 658f4f4fed6SLiguo Zhang if (i2c->dev_comp->support_33bits) { 659f4f4fed6SLiguo Zhang reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr); 660f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 661f4f4fed6SLiguo Zhang 662f4f4fed6SLiguo Zhang reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr); 663f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 664f4f4fed6SLiguo Zhang } 665f4f4fed6SLiguo Zhang 666ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 667ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 668ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 669ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 670ce38815dSXudong Chen } 671ce38815dSXudong Chen 672ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 673b2ed11e2SEddie Huang 674173b77e8SLiguo Zhang if (!i2c->auto_restart) { 675b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 676b2ed11e2SEddie Huang } else { 677b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 678b2ed11e2SEddie Huang if (left_num >= 1) 679b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 680b2ed11e2SEddie Huang } 681bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 682ce38815dSXudong Chen 683ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 684ce38815dSXudong Chen i2c->adap.timeout); 685ce38815dSXudong Chen 686ce38815dSXudong Chen /* Clear interrupt mask */ 687bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 688cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 689ce38815dSXudong Chen 690ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 691ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 692ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 693fc66b39fSJun Gao 694fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 695ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 696ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 697ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 698fc66b39fSJun Gao 699fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 700ce38815dSXudong Chen } else { 701ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 702ce38815dSXudong Chen DMA_TO_DEVICE); 703ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 704ce38815dSXudong Chen DMA_FROM_DEVICE); 705fc66b39fSJun Gao 706fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 707fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 708ce38815dSXudong Chen } 709ce38815dSXudong Chen 710ce38815dSXudong Chen if (ret == 0) { 711ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 712ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 713ce38815dSXudong Chen return -ETIMEDOUT; 714ce38815dSXudong Chen } 715ce38815dSXudong Chen 716ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 717ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 718ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 719ce38815dSXudong Chen return -ENXIO; 720ce38815dSXudong Chen } 721ce38815dSXudong Chen 722ce38815dSXudong Chen return 0; 723ce38815dSXudong Chen } 724ce38815dSXudong Chen 725ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 726ce38815dSXudong Chen struct i2c_msg msgs[], int num) 727ce38815dSXudong Chen { 728ce38815dSXudong Chen int ret; 729ce38815dSXudong Chen int left_num = num; 730ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 731ce38815dSXudong Chen 732ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 733ce38815dSXudong Chen if (ret) 734ce38815dSXudong Chen return ret; 735ce38815dSXudong Chen 736173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 737173b77e8SLiguo Zhang 738173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 739173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 740173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 741173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 742173b77e8SLiguo Zhang i2c->auto_restart = 0; 743173b77e8SLiguo Zhang } 744173b77e8SLiguo Zhang } 745173b77e8SLiguo Zhang 7468378d01fSLiguo Zhang if (i2c->auto_restart && num >= 2 && i2c->speed_hz > MAX_FS_MODE_SPEED) 7478378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 7488378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 7498378d01fSLiguo Zhang */ 7508378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 7518378d01fSLiguo Zhang else 7528378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 7538378d01fSLiguo Zhang 754b2ed11e2SEddie Huang while (left_num--) { 755ce38815dSXudong Chen if (!msgs->buf) { 756ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 757ce38815dSXudong Chen ret = -EINVAL; 758ce38815dSXudong Chen goto err_exit; 759ce38815dSXudong Chen } 760ce38815dSXudong Chen 761ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 762ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 763ce38815dSXudong Chen else 764ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 765ce38815dSXudong Chen 766173b77e8SLiguo Zhang if (!i2c->auto_restart) { 767ce38815dSXudong Chen if (num > 1) { 768ce38815dSXudong Chen /* combined two messages into one transaction */ 769ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 770ce38815dSXudong Chen left_num--; 771ce38815dSXudong Chen } 772b2ed11e2SEddie Huang } 773ce38815dSXudong Chen 774ce38815dSXudong Chen /* always use DMA mode. */ 775b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 776ce38815dSXudong Chen if (ret < 0) 777ce38815dSXudong Chen goto err_exit; 778ce38815dSXudong Chen 779b2ed11e2SEddie Huang msgs++; 780b2ed11e2SEddie Huang } 781ce38815dSXudong Chen /* the return value is number of executed messages */ 782ce38815dSXudong Chen ret = num; 783ce38815dSXudong Chen 784ce38815dSXudong Chen err_exit: 785ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 786ce38815dSXudong Chen return ret; 787ce38815dSXudong Chen } 788ce38815dSXudong Chen 789ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 790ce38815dSXudong Chen { 791ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 792b2ed11e2SEddie Huang u16 restart_flag = 0; 79328c0a843SEddie Huang u16 intr_stat; 794b2ed11e2SEddie Huang 795173b77e8SLiguo Zhang if (i2c->auto_restart) 796b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 797ce38815dSXudong Chen 798bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 799bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 800ce38815dSXudong Chen 80128c0a843SEddie Huang /* 80228c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 80328c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 80428c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 80528c0a843SEddie Huang */ 80628c0a843SEddie Huang i2c->irq_stat |= intr_stat; 8078378d01fSLiguo Zhang 8088378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 8098378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 8108378d01fSLiguo Zhang i2c->irq_stat = 0; 811bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 812bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 8138378d01fSLiguo Zhang } else { 81428c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 815ce38815dSXudong Chen complete(&i2c->msg_complete); 8168378d01fSLiguo Zhang } 817ce38815dSXudong Chen 818ce38815dSXudong Chen return IRQ_HANDLED; 819ce38815dSXudong Chen } 820ce38815dSXudong Chen 821ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 822ce38815dSXudong Chen { 823ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 824ce38815dSXudong Chen } 825ce38815dSXudong Chen 826ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 827ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 828ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 829ce38815dSXudong Chen }; 830ce38815dSXudong Chen 831f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 832ce38815dSXudong Chen { 833ce38815dSXudong Chen int ret; 834ce38815dSXudong Chen 835ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 836ce38815dSXudong Chen if (ret < 0) 837ce38815dSXudong Chen i2c->speed_hz = I2C_DEFAULT_SPEED; 838ce38815dSXudong Chen 839f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 840ce38815dSXudong Chen if (ret < 0) 841ce38815dSXudong Chen return ret; 842ce38815dSXudong Chen 843f2326401SJun Gao if (i2c->clk_src_div == 0) 844ce38815dSXudong Chen return -EINVAL; 845ce38815dSXudong Chen 846ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 847ce38815dSXudong Chen i2c->use_push_pull = 848ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 849ce38815dSXudong Chen 850ce38815dSXudong Chen return 0; 851ce38815dSXudong Chen } 852ce38815dSXudong Chen 853ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 854ce38815dSXudong Chen { 855ce38815dSXudong Chen int ret = 0; 856ce38815dSXudong Chen struct mtk_i2c *i2c; 857ce38815dSXudong Chen struct clk *clk; 858ce38815dSXudong Chen struct resource *res; 859ce38815dSXudong Chen int irq; 860ce38815dSXudong Chen 861ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 862ce38815dSXudong Chen if (!i2c) 863ce38815dSXudong Chen return -ENOMEM; 864ce38815dSXudong Chen 865ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 866ce38815dSXudong Chen i2c->base = devm_ioremap_resource(&pdev->dev, res); 867ce38815dSXudong Chen if (IS_ERR(i2c->base)) 868ce38815dSXudong Chen return PTR_ERR(i2c->base); 869ce38815dSXudong Chen 870ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 871ce38815dSXudong Chen i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); 872ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 873ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 874ce38815dSXudong Chen 875ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 876ce38815dSXudong Chen if (irq <= 0) 877ce38815dSXudong Chen return irq; 878ce38815dSXudong Chen 879ce38815dSXudong Chen init_completion(&i2c->msg_complete); 880ce38815dSXudong Chen 8816e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 882ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 883ce38815dSXudong Chen i2c->dev = &pdev->dev; 884ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 885ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 886ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 887ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 888ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 889ce38815dSXudong Chen i2c->adap.retries = 1; 890ce38815dSXudong Chen 8915a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 8925a10e7d7SJun Gao if (ret) 8935a10e7d7SJun Gao return -EINVAL; 8945a10e7d7SJun Gao 8955a10e7d7SJun Gao if (i2c->dev_comp->timing_adjust) 8965a10e7d7SJun Gao i2c->clk_src_div *= I2C_DEFAULT_CLK_DIV; 8975a10e7d7SJun Gao 898ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 899ce38815dSXudong Chen return -EINVAL; 900ce38815dSXudong Chen 901ce38815dSXudong Chen i2c->clk_main = devm_clk_get(&pdev->dev, "main"); 902ce38815dSXudong Chen if (IS_ERR(i2c->clk_main)) { 903ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 904ce38815dSXudong Chen return PTR_ERR(i2c->clk_main); 905ce38815dSXudong Chen } 906ce38815dSXudong Chen 907ce38815dSXudong Chen i2c->clk_dma = devm_clk_get(&pdev->dev, "dma"); 908ce38815dSXudong Chen if (IS_ERR(i2c->clk_dma)) { 909ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 910ce38815dSXudong Chen return PTR_ERR(i2c->clk_dma); 911ce38815dSXudong Chen } 912ce38815dSXudong Chen 913cad6dc5dSQii Wang i2c->clk_arb = devm_clk_get(&pdev->dev, "arb"); 914cad6dc5dSQii Wang if (IS_ERR(i2c->clk_arb)) 915cad6dc5dSQii Wang i2c->clk_arb = NULL; 916cad6dc5dSQii Wang 917ce38815dSXudong Chen clk = i2c->clk_main; 918ce38815dSXudong Chen if (i2c->have_pmic) { 919ce38815dSXudong Chen i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic"); 920ce38815dSXudong Chen if (IS_ERR(i2c->clk_pmic)) { 921ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 922ce38815dSXudong Chen return PTR_ERR(i2c->clk_pmic); 923ce38815dSXudong Chen } 924ce38815dSXudong Chen clk = i2c->clk_pmic; 925ce38815dSXudong Chen } 926ce38815dSXudong Chen 927ce38815dSXudong Chen strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 928ce38815dSXudong Chen 929f2326401SJun Gao ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk)); 930ce38815dSXudong Chen if (ret) { 931ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 932ce38815dSXudong Chen return -EINVAL; 933ce38815dSXudong Chen } 934ce38815dSXudong Chen 935f4f4fed6SLiguo Zhang if (i2c->dev_comp->support_33bits) { 936f4f4fed6SLiguo Zhang ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(33)); 937f4f4fed6SLiguo Zhang if (ret) { 938f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 939f4f4fed6SLiguo Zhang return ret; 940f4f4fed6SLiguo Zhang } 941f4f4fed6SLiguo Zhang } 942f4f4fed6SLiguo Zhang 943ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 944ce38815dSXudong Chen if (ret) { 945ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 946ce38815dSXudong Chen return ret; 947ce38815dSXudong Chen } 948ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 949ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 950ce38815dSXudong Chen 951ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 952ce38815dSXudong Chen IRQF_TRIGGER_NONE, I2C_DRV_NAME, i2c); 953ce38815dSXudong Chen if (ret < 0) { 954ce38815dSXudong Chen dev_err(&pdev->dev, 955ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 956ce38815dSXudong Chen return ret; 957ce38815dSXudong Chen } 958ce38815dSXudong Chen 959ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 960ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 961ea734404SWolfram Sang if (ret) 962ce38815dSXudong Chen return ret; 963ce38815dSXudong Chen 964ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 965ce38815dSXudong Chen 966ce38815dSXudong Chen return 0; 967ce38815dSXudong Chen } 968ce38815dSXudong Chen 969ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev) 970ce38815dSXudong Chen { 971ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 972ce38815dSXudong Chen 973ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 974ce38815dSXudong Chen 975ce38815dSXudong Chen return 0; 976ce38815dSXudong Chen } 977ce38815dSXudong Chen 97809027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP 97909027e08SLiguo Zhang static int mtk_i2c_resume(struct device *dev) 98009027e08SLiguo Zhang { 981f6762cedSJun Gao int ret; 98209027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 98309027e08SLiguo Zhang 984f6762cedSJun Gao ret = mtk_i2c_clock_enable(i2c); 985f6762cedSJun Gao if (ret) { 986f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 987f6762cedSJun Gao return ret; 988f6762cedSJun Gao } 989f6762cedSJun Gao 99009027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 99109027e08SLiguo Zhang 992f6762cedSJun Gao mtk_i2c_clock_disable(i2c); 993f6762cedSJun Gao 99409027e08SLiguo Zhang return 0; 99509027e08SLiguo Zhang } 99609027e08SLiguo Zhang #endif 99709027e08SLiguo Zhang 99809027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 99909027e08SLiguo Zhang SET_SYSTEM_SLEEP_PM_OPS(NULL, mtk_i2c_resume) 100009027e08SLiguo Zhang }; 100109027e08SLiguo Zhang 1002ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1003ce38815dSXudong Chen .probe = mtk_i2c_probe, 1004ce38815dSXudong Chen .remove = mtk_i2c_remove, 1005ce38815dSXudong Chen .driver = { 1006ce38815dSXudong Chen .name = I2C_DRV_NAME, 100709027e08SLiguo Zhang .pm = &mtk_i2c_pm, 1008ce38815dSXudong Chen .of_match_table = of_match_ptr(mtk_i2c_of_match), 1009ce38815dSXudong Chen }, 1010ce38815dSXudong Chen }; 1011ce38815dSXudong Chen 1012ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1013ce38815dSXudong Chen 1014ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1015ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1016ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1017