11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ce38815dSXudong Chen /* 3ce38815dSXudong Chen * Copyright (c) 2014 MediaTek Inc. 4ce38815dSXudong Chen * Author: Xudong Chen <xudong.chen@mediatek.com> 5ce38815dSXudong Chen */ 6ce38815dSXudong Chen 7ce38815dSXudong Chen #include <linux/clk.h> 8ce38815dSXudong Chen #include <linux/completion.h> 9ce38815dSXudong Chen #include <linux/delay.h> 10ce38815dSXudong Chen #include <linux/device.h> 11ce38815dSXudong Chen #include <linux/dma-mapping.h> 12ce38815dSXudong Chen #include <linux/err.h> 13ce38815dSXudong Chen #include <linux/errno.h> 14ce38815dSXudong Chen #include <linux/i2c.h> 15ce38815dSXudong Chen #include <linux/init.h> 16ce38815dSXudong Chen #include <linux/interrupt.h> 17ce38815dSXudong Chen #include <linux/io.h> 18e3e4949eSKewei Xu #include <linux/iopoll.h> 19ce38815dSXudong Chen #include <linux/kernel.h> 20ce38815dSXudong Chen #include <linux/mm.h> 21ce38815dSXudong Chen #include <linux/module.h> 2259738ab2SRob Herring #include <linux/of.h> 23ce38815dSXudong Chen #include <linux/platform_device.h> 24ce38815dSXudong Chen #include <linux/scatterlist.h> 25ce38815dSXudong Chen #include <linux/sched.h> 26ce38815dSXudong Chen #include <linux/slab.h> 27ce38815dSXudong Chen 28b2ed11e2SEddie Huang #define I2C_RS_TRANSFER (1 << 4) 29cad6dc5dSQii Wang #define I2C_ARB_LOST (1 << 3) 30ce38815dSXudong Chen #define I2C_HS_NACKERR (1 << 2) 31ce38815dSXudong Chen #define I2C_ACKERR (1 << 1) 32ce38815dSXudong Chen #define I2C_TRANSAC_COMP (1 << 0) 33ce38815dSXudong Chen #define I2C_TRANSAC_START (1 << 0) 34b2ed11e2SEddie Huang #define I2C_RS_MUL_CNFG (1 << 15) 35b2ed11e2SEddie Huang #define I2C_RS_MUL_TRIG (1 << 14) 36ce38815dSXudong Chen #define I2C_DCM_DISABLE 0x0000 37ce38815dSXudong Chen #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003 38ce38815dSXudong Chen #define I2C_IO_CONFIG_PUSH_PULL 0x0000 39ce38815dSXudong Chen #define I2C_SOFT_RST 0x0001 4005f6f727SQii Wang #define I2C_HANDSHAKE_RST 0x0020 41ce38815dSXudong Chen #define I2C_FIFO_ADDR_CLR 0x0001 42ce38815dSXudong Chen #define I2C_DELAY_LEN 0x0002 433bce7703SKewei Xu #define I2C_ST_START_CON 0x8001 443bce7703SKewei Xu #define I2C_FS_START_CON 0x1800 45ce38815dSXudong Chen #define I2C_TIME_CLR_VALUE 0x0000 46ce38815dSXudong Chen #define I2C_TIME_DEFAULT_VALUE 0x0003 47ce38815dSXudong Chen #define I2C_WRRD_TRANAC_VALUE 0x0002 48ce38815dSXudong Chen #define I2C_RD_TRANAC_VALUE 0x0001 49be5ce0e9SQii Wang #define I2C_SCL_MIS_COMP_VALUE 0x0000 5005f6f727SQii Wang #define I2C_CHN_CLR_FLAG 0x0000 51e3e4949eSKewei Xu #define I2C_RELIABILITY 0x0010 52e3e4949eSKewei Xu #define I2C_DMAACK_ENABLE 0x0008 53ce38815dSXudong Chen 54ce38815dSXudong Chen #define I2C_DMA_CON_TX 0x0000 55ce38815dSXudong Chen #define I2C_DMA_CON_RX 0x0001 568426fe70SQii Wang #define I2C_DMA_ASYNC_MODE 0x0004 578426fe70SQii Wang #define I2C_DMA_SKIP_CONFIG 0x0010 588426fe70SQii Wang #define I2C_DMA_DIR_CHANGE 0x0200 59ce38815dSXudong Chen #define I2C_DMA_START_EN 0x0001 60ce38815dSXudong Chen #define I2C_DMA_INT_FLAG_NONE 0x0000 61ce38815dSXudong Chen #define I2C_DMA_CLR_FLAG 0x0000 6205f6f727SQii Wang #define I2C_DMA_WARM_RST 0x0001 63ea89ef1fSEddie Huang #define I2C_DMA_HARD_RST 0x0002 6405f6f727SQii Wang #define I2C_DMA_HANDSHAKE_RST 0x0004 65ce38815dSXudong Chen 66ce38815dSXudong Chen #define MAX_SAMPLE_CNT_DIV 8 67ce38815dSXudong Chen #define MAX_STEP_CNT_DIV 64 68b5a796c6SKewei Xu #define MAX_CLOCK_DIV_8BITS 256 69b5a796c6SKewei Xu #define MAX_CLOCK_DIV_5BITS 32 70ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV 8 71b5a796c6SKewei Xu #define I2C_STANDARD_MODE_BUFFER (1000 / 3) 72b5a796c6SKewei Xu #define I2C_FAST_MODE_BUFFER (300 / 3) 73b5a796c6SKewei Xu #define I2C_FAST_MODE_PLUS_BUFFER (20 / 3) 74ce38815dSXudong Chen 75ce38815dSXudong Chen #define I2C_CONTROL_RS (0x1 << 1) 76ce38815dSXudong Chen #define I2C_CONTROL_DMA_EN (0x1 << 2) 77ce38815dSXudong Chen #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3) 78ce38815dSXudong Chen #define I2C_CONTROL_DIR_CHANGE (0x1 << 4) 79ce38815dSXudong Chen #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5) 80ce38815dSXudong Chen #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6) 81a15c91baSQii Wang #define I2C_CONTROL_DMAACK_EN (0x1 << 8) 82a15c91baSQii Wang #define I2C_CONTROL_ASYNC_MODE (0x1 << 9) 83ce38815dSXudong Chen #define I2C_CONTROL_WRAPPER (0x1 << 0) 84ce38815dSXudong Chen 85ce38815dSXudong Chen #define I2C_DRV_NAME "i2c-mt65xx" 86ce38815dSXudong Chen 870016a32fSAngeloGioacchino Del Regno /** 880016a32fSAngeloGioacchino Del Regno * enum i2c_mt65xx_clks - Clocks enumeration for MT65XX I2C 890016a32fSAngeloGioacchino Del Regno * 900016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_MAIN: main clock for i2c bus 910016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_DMA: DMA clock for i2c via DMA 920016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_PMIC: PMIC clock for i2c from PMIC 930016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_ARB: Arbitrator clock for i2c 940016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_MAX: Number of supported clocks 950016a32fSAngeloGioacchino Del Regno */ 960016a32fSAngeloGioacchino Del Regno enum i2c_mt65xx_clks { 970016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_MAIN = 0, 980016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_DMA, 990016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_PMIC, 1000016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_ARB, 1010016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_MAX 1020016a32fSAngeloGioacchino Del Regno }; 1030016a32fSAngeloGioacchino Del Regno 1040016a32fSAngeloGioacchino Del Regno static const char * const i2c_mt65xx_clk_ids[I2C_MT65XX_CLK_MAX] = { 1050016a32fSAngeloGioacchino Del Regno "main", "dma", "pmic", "arb" 1060016a32fSAngeloGioacchino Del Regno }; 1070016a32fSAngeloGioacchino Del Regno 108ce38815dSXudong Chen enum DMA_REGS_OFFSET { 109ce38815dSXudong Chen OFFSET_INT_FLAG = 0x0, 110ce38815dSXudong Chen OFFSET_INT_EN = 0x04, 111ce38815dSXudong Chen OFFSET_EN = 0x08, 112ea89ef1fSEddie Huang OFFSET_RST = 0x0c, 113ce38815dSXudong Chen OFFSET_CON = 0x18, 114ce38815dSXudong Chen OFFSET_TX_MEM_ADDR = 0x1c, 115ce38815dSXudong Chen OFFSET_RX_MEM_ADDR = 0x20, 116ce38815dSXudong Chen OFFSET_TX_LEN = 0x24, 117ce38815dSXudong Chen OFFSET_RX_LEN = 0x28, 118f4f4fed6SLiguo Zhang OFFSET_TX_4G_MODE = 0x54, 119f4f4fed6SLiguo Zhang OFFSET_RX_4G_MODE = 0x58, 120ce38815dSXudong Chen }; 121ce38815dSXudong Chen 122ce38815dSXudong Chen enum i2c_trans_st_rs { 123ce38815dSXudong Chen I2C_TRANS_STOP = 0, 124ce38815dSXudong Chen I2C_TRANS_REPEATED_START, 125ce38815dSXudong Chen }; 126ce38815dSXudong Chen 127ce38815dSXudong Chen enum mtk_trans_op { 128ce38815dSXudong Chen I2C_MASTER_WR = 1, 129ce38815dSXudong Chen I2C_MASTER_RD, 130ce38815dSXudong Chen I2C_MASTER_WRRD, 131ce38815dSXudong Chen }; 132ce38815dSXudong Chen 133ce38815dSXudong Chen enum I2C_REGS_OFFSET { 134bc6eaf17SQii Wang OFFSET_DATA_PORT, 135bc6eaf17SQii Wang OFFSET_SLAVE_ADDR, 136bc6eaf17SQii Wang OFFSET_INTR_MASK, 137bc6eaf17SQii Wang OFFSET_INTR_STAT, 138bc6eaf17SQii Wang OFFSET_CONTROL, 139bc6eaf17SQii Wang OFFSET_TRANSFER_LEN, 140bc6eaf17SQii Wang OFFSET_TRANSAC_LEN, 141bc6eaf17SQii Wang OFFSET_DELAY_LEN, 142bc6eaf17SQii Wang OFFSET_TIMING, 143bc6eaf17SQii Wang OFFSET_START, 144bc6eaf17SQii Wang OFFSET_EXT_CONF, 145bc6eaf17SQii Wang OFFSET_FIFO_STAT, 146bc6eaf17SQii Wang OFFSET_FIFO_THRESH, 147bc6eaf17SQii Wang OFFSET_FIFO_ADDR_CLR, 148bc6eaf17SQii Wang OFFSET_IO_CONFIG, 149bc6eaf17SQii Wang OFFSET_RSV_DEBUG, 150bc6eaf17SQii Wang OFFSET_HS, 151bc6eaf17SQii Wang OFFSET_SOFTRESET, 152bc6eaf17SQii Wang OFFSET_DCM_EN, 153cc28e578SKewei Xu OFFSET_MULTI_DMA, 154bc6eaf17SQii Wang OFFSET_PATH_DIR, 155bc6eaf17SQii Wang OFFSET_DEBUGSTAT, 156bc6eaf17SQii Wang OFFSET_DEBUGCTRL, 157bc6eaf17SQii Wang OFFSET_TRANSFER_LEN_AUX, 158bc6eaf17SQii Wang OFFSET_CLOCK_DIV, 15925708278SQii Wang OFFSET_LTIMING, 160be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO, 161be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO, 162be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT, 163be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING, 164be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING, 165be5ce0e9SQii Wang OFFSET_SDA_TIMING, 166bc6eaf17SQii Wang }; 167bc6eaf17SQii Wang 168bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = { 169bc6eaf17SQii Wang [OFFSET_DATA_PORT] = 0x0, 170bc6eaf17SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 171bc6eaf17SQii Wang [OFFSET_INTR_MASK] = 0x8, 172bc6eaf17SQii Wang [OFFSET_INTR_STAT] = 0xc, 173bc6eaf17SQii Wang [OFFSET_CONTROL] = 0x10, 174bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 175bc6eaf17SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 176bc6eaf17SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 177bc6eaf17SQii Wang [OFFSET_TIMING] = 0x20, 178bc6eaf17SQii Wang [OFFSET_START] = 0x24, 179bc6eaf17SQii Wang [OFFSET_EXT_CONF] = 0x28, 180bc6eaf17SQii Wang [OFFSET_FIFO_STAT] = 0x30, 181bc6eaf17SQii Wang [OFFSET_FIFO_THRESH] = 0x34, 182bc6eaf17SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 183bc6eaf17SQii Wang [OFFSET_IO_CONFIG] = 0x40, 184bc6eaf17SQii Wang [OFFSET_RSV_DEBUG] = 0x44, 185bc6eaf17SQii Wang [OFFSET_HS] = 0x48, 186bc6eaf17SQii Wang [OFFSET_SOFTRESET] = 0x50, 187bc6eaf17SQii Wang [OFFSET_DCM_EN] = 0x54, 188bc6eaf17SQii Wang [OFFSET_PATH_DIR] = 0x60, 189bc6eaf17SQii Wang [OFFSET_DEBUGSTAT] = 0x64, 190bc6eaf17SQii Wang [OFFSET_DEBUGCTRL] = 0x68, 191bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x6c, 192bc6eaf17SQii Wang [OFFSET_CLOCK_DIV] = 0x70, 193be5ce0e9SQii Wang [OFFSET_SCL_HIGH_LOW_RATIO] = 0x74, 194be5ce0e9SQii Wang [OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78, 195be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x7C, 196be5ce0e9SQii Wang [OFFSET_STA_STO_AC_TIMING] = 0x80, 197be5ce0e9SQii Wang [OFFSET_HS_STA_STO_AC_TIMING] = 0x84, 198be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x88, 199ce38815dSXudong Chen }; 200ce38815dSXudong Chen 20125708278SQii Wang static const u16 mt_i2c_regs_v2[] = { 20225708278SQii Wang [OFFSET_DATA_PORT] = 0x0, 20325708278SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 20425708278SQii Wang [OFFSET_INTR_MASK] = 0x8, 20525708278SQii Wang [OFFSET_INTR_STAT] = 0xc, 20625708278SQii Wang [OFFSET_CONTROL] = 0x10, 20725708278SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 20825708278SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 20925708278SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 21025708278SQii Wang [OFFSET_TIMING] = 0x20, 21125708278SQii Wang [OFFSET_START] = 0x24, 21225708278SQii Wang [OFFSET_EXT_CONF] = 0x28, 21325708278SQii Wang [OFFSET_LTIMING] = 0x2c, 21425708278SQii Wang [OFFSET_HS] = 0x30, 21525708278SQii Wang [OFFSET_IO_CONFIG] = 0x34, 21625708278SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 217be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x3c, 21825708278SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x44, 21925708278SQii Wang [OFFSET_CLOCK_DIV] = 0x48, 22025708278SQii Wang [OFFSET_SOFTRESET] = 0x50, 221cc28e578SKewei Xu [OFFSET_MULTI_DMA] = 0x8c, 222be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x90, 223b8228aeaSKewei Xu [OFFSET_DEBUGSTAT] = 0xe4, 22425708278SQii Wang [OFFSET_DEBUGCTRL] = 0xe8, 22525708278SQii Wang [OFFSET_FIFO_STAT] = 0xf4, 22625708278SQii Wang [OFFSET_FIFO_THRESH] = 0xf8, 22725708278SQii Wang [OFFSET_DCM_EN] = 0xf88, 22825708278SQii Wang }; 22925708278SQii Wang 2301b48006eSKewei Xu static const u16 mt_i2c_regs_v3[] = { 2311b48006eSKewei Xu [OFFSET_DATA_PORT] = 0x0, 2321b48006eSKewei Xu [OFFSET_INTR_MASK] = 0x8, 2331b48006eSKewei Xu [OFFSET_INTR_STAT] = 0xc, 2341b48006eSKewei Xu [OFFSET_CONTROL] = 0x10, 2351b48006eSKewei Xu [OFFSET_TRANSFER_LEN] = 0x14, 2361b48006eSKewei Xu [OFFSET_TRANSAC_LEN] = 0x18, 2371b48006eSKewei Xu [OFFSET_DELAY_LEN] = 0x1c, 2381b48006eSKewei Xu [OFFSET_TIMING] = 0x20, 2391b48006eSKewei Xu [OFFSET_START] = 0x24, 2401b48006eSKewei Xu [OFFSET_EXT_CONF] = 0x28, 2411b48006eSKewei Xu [OFFSET_LTIMING] = 0x2c, 2421b48006eSKewei Xu [OFFSET_HS] = 0x30, 2431b48006eSKewei Xu [OFFSET_IO_CONFIG] = 0x34, 2441b48006eSKewei Xu [OFFSET_FIFO_ADDR_CLR] = 0x38, 2451b48006eSKewei Xu [OFFSET_SDA_TIMING] = 0x3c, 2461b48006eSKewei Xu [OFFSET_TRANSFER_LEN_AUX] = 0x44, 2471b48006eSKewei Xu [OFFSET_CLOCK_DIV] = 0x48, 2481b48006eSKewei Xu [OFFSET_SOFTRESET] = 0x50, 2491b48006eSKewei Xu [OFFSET_MULTI_DMA] = 0x8c, 2501b48006eSKewei Xu [OFFSET_SCL_MIS_COMP_POINT] = 0x90, 2511b48006eSKewei Xu [OFFSET_SLAVE_ADDR] = 0x94, 2521b48006eSKewei Xu [OFFSET_DEBUGSTAT] = 0xe4, 2531b48006eSKewei Xu [OFFSET_DEBUGCTRL] = 0xe8, 2541b48006eSKewei Xu [OFFSET_FIFO_STAT] = 0xf4, 2551b48006eSKewei Xu [OFFSET_FIFO_THRESH] = 0xf8, 2561b48006eSKewei Xu [OFFSET_DCM_EN] = 0xf88, 2571b48006eSKewei Xu }; 2581b48006eSKewei Xu 259ce38815dSXudong Chen struct mtk_i2c_compatible { 260ce38815dSXudong Chen const struct i2c_adapter_quirks *quirks; 261bc6eaf17SQii Wang const u16 *regs; 262ce38815dSXudong Chen unsigned char pmic_i2c: 1; 263ce38815dSXudong Chen unsigned char dcm: 1; 264b2ed11e2SEddie Huang unsigned char auto_restart: 1; 265173b77e8SLiguo Zhang unsigned char aux_len_reg: 1; 2665a10e7d7SJun Gao unsigned char timing_adjust: 1; 267a15c91baSQii Wang unsigned char dma_sync: 1; 26825708278SQii Wang unsigned char ltiming_adjust: 1; 2698426fe70SQii Wang unsigned char apdma_sync: 1; 270908d9843SQii Wang unsigned char max_dma_support; 271ce38815dSXudong Chen }; 272ce38815dSXudong Chen 273be5ce0e9SQii Wang struct mtk_i2c_ac_timing { 274be5ce0e9SQii Wang u16 htiming; 275be5ce0e9SQii Wang u16 ltiming; 276be5ce0e9SQii Wang u16 hs; 277be5ce0e9SQii Wang u16 ext; 278be5ce0e9SQii Wang u16 inter_clk_div; 279be5ce0e9SQii Wang u16 scl_hl_ratio; 280be5ce0e9SQii Wang u16 hs_scl_hl_ratio; 281be5ce0e9SQii Wang u16 sta_stop; 282be5ce0e9SQii Wang u16 hs_sta_stop; 283be5ce0e9SQii Wang u16 sda_timing; 284be5ce0e9SQii Wang }; 285be5ce0e9SQii Wang 286ce38815dSXudong Chen struct mtk_i2c { 287ce38815dSXudong Chen struct i2c_adapter adap; /* i2c host adapter */ 288ce38815dSXudong Chen struct device *dev; 289ce38815dSXudong Chen struct completion msg_complete; 290a80f2494SQii Wang struct i2c_timings timing_info; 291ce38815dSXudong Chen 292ce38815dSXudong Chen /* set in i2c probe */ 293ce38815dSXudong Chen void __iomem *base; /* i2c base addr */ 294ce38815dSXudong Chen void __iomem *pdmabase; /* dma base address*/ 2950016a32fSAngeloGioacchino Del Regno struct clk_bulk_data clocks[I2C_MT65XX_CLK_MAX]; /* clocks for i2c */ 296ce38815dSXudong Chen bool have_pmic; /* can use i2c pins from PMIC */ 297ce38815dSXudong Chen bool use_push_pull; /* IO config push-pull mode */ 298ce38815dSXudong Chen 299ce38815dSXudong Chen u16 irq_stat; /* interrupt status */ 300f2326401SJun Gao unsigned int clk_src_div; 301ce38815dSXudong Chen unsigned int speed_hz; /* The speed in transfer */ 302ce38815dSXudong Chen enum mtk_trans_op op; 303ce38815dSXudong Chen u16 timing_reg; 304ce38815dSXudong Chen u16 high_speed_reg; 30525708278SQii Wang u16 ltiming_reg; 306173b77e8SLiguo Zhang unsigned char auto_restart; 3078378d01fSLiguo Zhang bool ignore_restart_irq; 308be5ce0e9SQii Wang struct mtk_i2c_ac_timing ac_timing; 309ce38815dSXudong Chen const struct mtk_i2c_compatible *dev_comp; 310ce38815dSXudong Chen }; 311ce38815dSXudong Chen 312be5ce0e9SQii Wang /** 313be5ce0e9SQii Wang * struct i2c_spec_values: 314b0102a89SMatthias Brugger * @min_low_ns: min LOW period of the SCL clock 315b0102a89SMatthias Brugger * @min_su_sta_ns: min set-up time for a repeated START condition 316b0102a89SMatthias Brugger * @max_hd_dat_ns: max data hold time 317b0102a89SMatthias Brugger * @min_su_dat_ns: min data set-up time 318be5ce0e9SQii Wang */ 319be5ce0e9SQii Wang struct i2c_spec_values { 320be5ce0e9SQii Wang unsigned int min_low_ns; 321be5ce0e9SQii Wang unsigned int min_su_sta_ns; 322be5ce0e9SQii Wang unsigned int max_hd_dat_ns; 323be5ce0e9SQii Wang unsigned int min_su_dat_ns; 324be5ce0e9SQii Wang }; 325be5ce0e9SQii Wang 326be5ce0e9SQii Wang static const struct i2c_spec_values standard_mode_spec = { 327be5ce0e9SQii Wang .min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 328be5ce0e9SQii Wang .min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 329be5ce0e9SQii Wang .max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER, 330be5ce0e9SQii Wang .min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER, 331be5ce0e9SQii Wang }; 332be5ce0e9SQii Wang 333be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_spec = { 334be5ce0e9SQii Wang .min_low_ns = 1300 + I2C_FAST_MODE_BUFFER, 335be5ce0e9SQii Wang .min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER, 336be5ce0e9SQii Wang .max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER, 337be5ce0e9SQii Wang .min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER, 338be5ce0e9SQii Wang }; 339be5ce0e9SQii Wang 340be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_plus_spec = { 341be5ce0e9SQii Wang .min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER, 342be5ce0e9SQii Wang .min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER, 343be5ce0e9SQii Wang .max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER, 344be5ce0e9SQii Wang .min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER, 345be5ce0e9SQii Wang }; 346be5ce0e9SQii Wang 347ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = { 348ce38815dSXudong Chen .flags = I2C_AQ_COMB_WRITE_THEN_READ, 349ce38815dSXudong Chen .max_num_msgs = 1, 350ce38815dSXudong Chen .max_write_len = 255, 351ce38815dSXudong Chen .max_read_len = 255, 352ce38815dSXudong Chen .max_comb_1st_msg_len = 255, 353ce38815dSXudong Chen .max_comb_2nd_msg_len = 31, 354ce38815dSXudong Chen }; 355ce38815dSXudong Chen 3561304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = { 3571304fe09SJun Gao .max_num_msgs = 255, 3581304fe09SJun Gao }; 3591304fe09SJun Gao 360abf4923eSHsin-Yi Wang static const struct i2c_adapter_quirks mt8183_i2c_quirks = { 361abf4923eSHsin-Yi Wang .flags = I2C_AQ_NO_ZERO_LEN, 362abf4923eSHsin-Yi Wang }; 363abf4923eSHsin-Yi Wang 3645a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = { 365bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3665a10e7d7SJun Gao .pmic_i2c = 0, 3675a10e7d7SJun Gao .dcm = 1, 3685a10e7d7SJun Gao .auto_restart = 1, 3695a10e7d7SJun Gao .aux_len_reg = 1, 3705a10e7d7SJun Gao .timing_adjust = 1, 371a15c91baSQii Wang .dma_sync = 0, 37225708278SQii Wang .ltiming_adjust = 0, 3738426fe70SQii Wang .apdma_sync = 0, 374908d9843SQii Wang .max_dma_support = 33, 3755a10e7d7SJun Gao }; 3765a10e7d7SJun Gao 377ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = { 378ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 379bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 380ce38815dSXudong Chen .pmic_i2c = 0, 381ce38815dSXudong Chen .dcm = 1, 382b2ed11e2SEddie Huang .auto_restart = 0, 383173b77e8SLiguo Zhang .aux_len_reg = 0, 3845a10e7d7SJun Gao .timing_adjust = 0, 385a15c91baSQii Wang .dma_sync = 0, 38625708278SQii Wang .ltiming_adjust = 0, 3878426fe70SQii Wang .apdma_sync = 0, 388908d9843SQii Wang .max_dma_support = 32, 389ce38815dSXudong Chen }; 390ce38815dSXudong Chen 391ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = { 392ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 393bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 394ce38815dSXudong Chen .pmic_i2c = 1, 395ce38815dSXudong Chen .dcm = 0, 396b2ed11e2SEddie Huang .auto_restart = 0, 397173b77e8SLiguo Zhang .aux_len_reg = 0, 3985a10e7d7SJun Gao .timing_adjust = 0, 399a15c91baSQii Wang .dma_sync = 0, 40025708278SQii Wang .ltiming_adjust = 0, 4018426fe70SQii Wang .apdma_sync = 0, 402908d9843SQii Wang .max_dma_support = 32, 403b2ed11e2SEddie Huang }; 404b2ed11e2SEddie Huang 4051304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = { 4061304fe09SJun Gao .quirks = &mt7622_i2c_quirks, 407bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 4081304fe09SJun Gao .pmic_i2c = 0, 4091304fe09SJun Gao .dcm = 1, 4101304fe09SJun Gao .auto_restart = 1, 4111304fe09SJun Gao .aux_len_reg = 1, 4125a10e7d7SJun Gao .timing_adjust = 0, 413a15c91baSQii Wang .dma_sync = 0, 41425708278SQii Wang .ltiming_adjust = 0, 4158426fe70SQii Wang .apdma_sync = 0, 416908d9843SQii Wang .max_dma_support = 32, 4171304fe09SJun Gao }; 4181304fe09SJun Gao 4191bff55b5SKewei Xu static const struct mtk_i2c_compatible mt8168_compat = { 4201bff55b5SKewei Xu .regs = mt_i2c_regs_v1, 4211bff55b5SKewei Xu .pmic_i2c = 0, 4221bff55b5SKewei Xu .dcm = 1, 4231bff55b5SKewei Xu .auto_restart = 1, 4241bff55b5SKewei Xu .aux_len_reg = 1, 4251bff55b5SKewei Xu .timing_adjust = 1, 4261bff55b5SKewei Xu .dma_sync = 1, 4271bff55b5SKewei Xu .ltiming_adjust = 0, 4281bff55b5SKewei Xu .apdma_sync = 0, 4291bff55b5SKewei Xu .max_dma_support = 33, 4301bff55b5SKewei Xu }; 4311bff55b5SKewei Xu 432f82fd184SDaniel Golle static const struct mtk_i2c_compatible mt7981_compat = { 433f82fd184SDaniel Golle .regs = mt_i2c_regs_v3, 434f82fd184SDaniel Golle .pmic_i2c = 0, 435f82fd184SDaniel Golle .dcm = 0, 436f82fd184SDaniel Golle .auto_restart = 1, 437f82fd184SDaniel Golle .aux_len_reg = 1, 438f82fd184SDaniel Golle .timing_adjust = 1, 439f82fd184SDaniel Golle .dma_sync = 1, 440f82fd184SDaniel Golle .ltiming_adjust = 1, 441f82fd184SDaniel Golle .max_dma_support = 33 442f82fd184SDaniel Golle }; 443f82fd184SDaniel Golle 444e0b7afc0SFrank Wunderlich static const struct mtk_i2c_compatible mt7986_compat = { 445e0b7afc0SFrank Wunderlich .quirks = &mt7622_i2c_quirks, 446e0b7afc0SFrank Wunderlich .regs = mt_i2c_regs_v1, 447e0b7afc0SFrank Wunderlich .pmic_i2c = 0, 448e0b7afc0SFrank Wunderlich .dcm = 1, 449e0b7afc0SFrank Wunderlich .auto_restart = 1, 450e0b7afc0SFrank Wunderlich .aux_len_reg = 1, 451e0b7afc0SFrank Wunderlich .timing_adjust = 0, 452e0b7afc0SFrank Wunderlich .dma_sync = 1, 453e0b7afc0SFrank Wunderlich .ltiming_adjust = 0, 454e0b7afc0SFrank Wunderlich .max_dma_support = 32, 455e0b7afc0SFrank Wunderlich }; 456e0b7afc0SFrank Wunderlich 457b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = { 458bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 459b2ed11e2SEddie Huang .pmic_i2c = 0, 460b2ed11e2SEddie Huang .dcm = 1, 461b2ed11e2SEddie Huang .auto_restart = 1, 462173b77e8SLiguo Zhang .aux_len_reg = 1, 4635a10e7d7SJun Gao .timing_adjust = 0, 464a15c91baSQii Wang .dma_sync = 0, 46525708278SQii Wang .ltiming_adjust = 0, 4668426fe70SQii Wang .apdma_sync = 0, 467908d9843SQii Wang .max_dma_support = 33, 46825708278SQii Wang }; 46925708278SQii Wang 47025708278SQii Wang static const struct mtk_i2c_compatible mt8183_compat = { 471abf4923eSHsin-Yi Wang .quirks = &mt8183_i2c_quirks, 47225708278SQii Wang .regs = mt_i2c_regs_v2, 47325708278SQii Wang .pmic_i2c = 0, 47425708278SQii Wang .dcm = 0, 47525708278SQii Wang .auto_restart = 1, 47625708278SQii Wang .aux_len_reg = 1, 47725708278SQii Wang .timing_adjust = 1, 47825708278SQii Wang .dma_sync = 1, 47925708278SQii Wang .ltiming_adjust = 1, 4808426fe70SQii Wang .apdma_sync = 0, 481908d9843SQii Wang .max_dma_support = 33, 482ce38815dSXudong Chen }; 483ce38815dSXudong Chen 48493470531SKewei Xu static const struct mtk_i2c_compatible mt8186_compat = { 48593470531SKewei Xu .regs = mt_i2c_regs_v2, 48693470531SKewei Xu .pmic_i2c = 0, 48793470531SKewei Xu .dcm = 0, 48893470531SKewei Xu .auto_restart = 1, 48993470531SKewei Xu .aux_len_reg = 1, 49093470531SKewei Xu .timing_adjust = 1, 49193470531SKewei Xu .dma_sync = 0, 49293470531SKewei Xu .ltiming_adjust = 1, 49393470531SKewei Xu .apdma_sync = 0, 49493470531SKewei Xu .max_dma_support = 36, 49593470531SKewei Xu }; 49693470531SKewei Xu 4971b48006eSKewei Xu static const struct mtk_i2c_compatible mt8188_compat = { 4981b48006eSKewei Xu .regs = mt_i2c_regs_v3, 4991b48006eSKewei Xu .pmic_i2c = 0, 5001b48006eSKewei Xu .dcm = 0, 5011b48006eSKewei Xu .auto_restart = 1, 5021b48006eSKewei Xu .aux_len_reg = 1, 5031b48006eSKewei Xu .timing_adjust = 1, 5041b48006eSKewei Xu .dma_sync = 0, 5051b48006eSKewei Xu .ltiming_adjust = 1, 5061b48006eSKewei Xu .apdma_sync = 1, 5071b48006eSKewei Xu .max_dma_support = 36, 5081b48006eSKewei Xu }; 5091b48006eSKewei Xu 510789e67baSQii Wang static const struct mtk_i2c_compatible mt8192_compat = { 511789e67baSQii Wang .quirks = &mt8183_i2c_quirks, 512789e67baSQii Wang .regs = mt_i2c_regs_v2, 513789e67baSQii Wang .pmic_i2c = 0, 514789e67baSQii Wang .dcm = 0, 515789e67baSQii Wang .auto_restart = 1, 516789e67baSQii Wang .aux_len_reg = 1, 517789e67baSQii Wang .timing_adjust = 1, 518789e67baSQii Wang .dma_sync = 1, 519789e67baSQii Wang .ltiming_adjust = 1, 520789e67baSQii Wang .apdma_sync = 1, 521789e67baSQii Wang .max_dma_support = 36, 522789e67baSQii Wang }; 523789e67baSQii Wang 524ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = { 5255a10e7d7SJun Gao { .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat }, 526ce38815dSXudong Chen { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat }, 527ce38815dSXudong Chen { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat }, 5281304fe09SJun Gao { .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat }, 529f82fd184SDaniel Golle { .compatible = "mediatek,mt7981-i2c", .data = &mt7981_compat }, 530e0b7afc0SFrank Wunderlich { .compatible = "mediatek,mt7986-i2c", .data = &mt7986_compat }, 5311bff55b5SKewei Xu { .compatible = "mediatek,mt8168-i2c", .data = &mt8168_compat }, 532b2ed11e2SEddie Huang { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat }, 53325708278SQii Wang { .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat }, 53493470531SKewei Xu { .compatible = "mediatek,mt8186-i2c", .data = &mt8186_compat }, 5351b48006eSKewei Xu { .compatible = "mediatek,mt8188-i2c", .data = &mt8188_compat }, 536789e67baSQii Wang { .compatible = "mediatek,mt8192-i2c", .data = &mt8192_compat }, 537ce38815dSXudong Chen {} 538ce38815dSXudong Chen }; 539ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); 540ce38815dSXudong Chen 541bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg) 542bc6eaf17SQii Wang { 543bc6eaf17SQii Wang return readw(i2c->base + i2c->dev_comp->regs[reg]); 544bc6eaf17SQii Wang } 545bc6eaf17SQii Wang 546bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val, 547bc6eaf17SQii Wang enum I2C_REGS_OFFSET reg) 548bc6eaf17SQii Wang { 549bc6eaf17SQii Wang writew(val, i2c->base + i2c->dev_comp->regs[reg]); 550bc6eaf17SQii Wang } 551bc6eaf17SQii Wang 552ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c) 553ce38815dSXudong Chen { 554ce38815dSXudong Chen u16 control_reg; 555fed1bd51SQii Wang u16 intr_stat_reg; 5563bce7703SKewei Xu u16 ext_conf_val; 557fed1bd51SQii Wang 558fed1bd51SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_START); 559fed1bd51SQii Wang intr_stat_reg = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 560fed1bd51SQii Wang mtk_i2c_writew(i2c, intr_stat_reg, OFFSET_INTR_STAT); 561ce38815dSXudong Chen 5623186b880SQii Wang if (i2c->dev_comp->apdma_sync) { 56305f6f727SQii Wang writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST); 56405f6f727SQii Wang udelay(10); 56505f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 56605f6f727SQii Wang udelay(10); 56705f6f727SQii Wang writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST, 56805f6f727SQii Wang i2c->pdmabase + OFFSET_RST); 56905f6f727SQii Wang mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST, 57005f6f727SQii Wang OFFSET_SOFTRESET); 57105f6f727SQii Wang udelay(10); 57205f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 57305f6f727SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 57405f6f727SQii Wang } else { 575aafced67SQii Wang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 576aafced67SQii Wang udelay(50); 577aafced67SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 578bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 57905f6f727SQii Wang } 580ce38815dSXudong Chen 581ce38815dSXudong Chen /* Set ioconfig */ 582ce38815dSXudong Chen if (i2c->use_push_pull) 583bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 584ce38815dSXudong Chen else 585bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 586ce38815dSXudong Chen 587ce38815dSXudong Chen if (i2c->dev_comp->dcm) 588bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 589ce38815dSXudong Chen 590bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 591bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 59225708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 59325708278SQii Wang mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING); 594ce38815dSXudong Chen 5953bce7703SKewei Xu if (i2c->speed_hz <= I2C_MAX_STANDARD_MODE_FREQ) 5963bce7703SKewei Xu ext_conf_val = I2C_ST_START_CON; 5973bce7703SKewei Xu else 5983bce7703SKewei Xu ext_conf_val = I2C_FS_START_CON; 5993bce7703SKewei Xu 600be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) { 6013bce7703SKewei Xu ext_conf_val = i2c->ac_timing.ext; 602be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, 603be5ce0e9SQii Wang OFFSET_CLOCK_DIV); 604be5ce0e9SQii Wang mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE, 605be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT); 606be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing, 607be5ce0e9SQii Wang OFFSET_SDA_TIMING); 608be5ce0e9SQii Wang 609be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 610be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.htiming, 611be5ce0e9SQii Wang OFFSET_TIMING); 612be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS); 613be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ltiming, 614be5ce0e9SQii Wang OFFSET_LTIMING); 615be5ce0e9SQii Wang } else { 616be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio, 617be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO); 618be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio, 619be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO); 620be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop, 621be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING); 622be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop, 623be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING); 624be5ce0e9SQii Wang } 625be5ce0e9SQii Wang } 6263bce7703SKewei Xu mtk_i2c_writew(i2c, ext_conf_val, OFFSET_EXT_CONF); 627be5ce0e9SQii Wang 628ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 629ce38815dSXudong Chen if (i2c->have_pmic) 630bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 631ce38815dSXudong Chen 632ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 633ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 634a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 635a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 636a15c91baSQii Wang 637bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 638bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 639ce38815dSXudong Chen } 640ce38815dSXudong Chen 641be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed) 642be5ce0e9SQii Wang { 643be5ce0e9SQii Wang if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 644be5ce0e9SQii Wang return &standard_mode_spec; 645be5ce0e9SQii Wang else if (speed <= I2C_MAX_FAST_MODE_FREQ) 646be5ce0e9SQii Wang return &fast_mode_spec; 647be5ce0e9SQii Wang else 648be5ce0e9SQii Wang return &fast_mode_plus_spec; 649be5ce0e9SQii Wang } 650be5ce0e9SQii Wang 651be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed) 652be5ce0e9SQii Wang { 65363ce8e3dSQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) 654be5ce0e9SQii Wang return MAX_HS_STEP_CNT_DIV; 655be5ce0e9SQii Wang else 656be5ce0e9SQii Wang return MAX_STEP_CNT_DIV; 657be5ce0e9SQii Wang } 658be5ce0e9SQii Wang 659b5a796c6SKewei Xu static int mtk_i2c_get_clk_div_restri(struct mtk_i2c *i2c, 660b5a796c6SKewei Xu unsigned int sample_cnt) 661b5a796c6SKewei Xu { 662b5a796c6SKewei Xu int clk_div_restri = 0; 663b5a796c6SKewei Xu 664b5a796c6SKewei Xu if (i2c->dev_comp->ltiming_adjust == 0) 665b5a796c6SKewei Xu return 0; 666b5a796c6SKewei Xu 667b5a796c6SKewei Xu if (sample_cnt == 1) { 668b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 669b5a796c6SKewei Xu clk_div_restri = 0; 670b5a796c6SKewei Xu else 671b5a796c6SKewei Xu clk_div_restri = 1; 672b5a796c6SKewei Xu } else { 673b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 674b5a796c6SKewei Xu clk_div_restri = -1; 675b5a796c6SKewei Xu else if (i2c->ac_timing.inter_clk_div == 1) 676b5a796c6SKewei Xu clk_div_restri = 0; 677b5a796c6SKewei Xu else 678b5a796c6SKewei Xu clk_div_restri = 1; 679b5a796c6SKewei Xu } 680b5a796c6SKewei Xu 681b5a796c6SKewei Xu return clk_div_restri; 682b5a796c6SKewei Xu } 683b5a796c6SKewei Xu 684be5ce0e9SQii Wang /* 685be5ce0e9SQii Wang * Check and Calculate i2c ac-timing 686be5ce0e9SQii Wang * 687be5ce0e9SQii Wang * Hardware design: 688be5ce0e9SQii Wang * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src 689be5ce0e9SQii Wang * xxx_cnt_div = spec->min_xxx_ns / sample_ns 690be5ce0e9SQii Wang * 691be5ce0e9SQii Wang * Sample_ns is rounded down for xxx_cnt_div would be greater 692be5ce0e9SQii Wang * than the smallest spec. 693be5ce0e9SQii Wang * The sda_timing is chosen as the middle value between 694be5ce0e9SQii Wang * the largest and smallest. 695be5ce0e9SQii Wang */ 696be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, 697be5ce0e9SQii Wang unsigned int clk_src, 698be5ce0e9SQii Wang unsigned int check_speed, 699be5ce0e9SQii Wang unsigned int step_cnt, 700be5ce0e9SQii Wang unsigned int sample_cnt) 701be5ce0e9SQii Wang { 702be5ce0e9SQii Wang const struct i2c_spec_values *spec; 703be5ce0e9SQii Wang unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt; 704be5ce0e9SQii Wang unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f; 705be5ce0e9SQii Wang unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), 706be5ce0e9SQii Wang clk_src); 707be5ce0e9SQii Wang 708be5ce0e9SQii Wang if (!i2c->dev_comp->timing_adjust) 709be5ce0e9SQii Wang return 0; 710be5ce0e9SQii Wang 711be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 712be5ce0e9SQii Wang max_sta_cnt = 0x100; 713be5ce0e9SQii Wang 714be5ce0e9SQii Wang spec = mtk_i2c_get_spec(check_speed); 715be5ce0e9SQii Wang 716be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 717be5ce0e9SQii Wang clk_ns = 1000000000 / clk_src; 718be5ce0e9SQii Wang else 719be5ce0e9SQii Wang clk_ns = sample_ns / 2; 720be5ce0e9SQii Wang 721a80f2494SQii Wang su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns + 722a80f2494SQii Wang i2c->timing_info.scl_int_delay_ns, clk_ns); 723be5ce0e9SQii Wang if (su_sta_cnt > max_sta_cnt) 724be5ce0e9SQii Wang return -1; 725be5ce0e9SQii Wang 726be5ce0e9SQii Wang low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns); 727be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(check_speed); 728be5ce0e9SQii Wang if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) { 729be5ce0e9SQii Wang if (low_cnt > step_cnt) { 730be5ce0e9SQii Wang high_cnt = 2 * step_cnt - low_cnt; 731be5ce0e9SQii Wang } else { 732be5ce0e9SQii Wang high_cnt = step_cnt; 733be5ce0e9SQii Wang low_cnt = step_cnt; 734be5ce0e9SQii Wang } 735be5ce0e9SQii Wang } else { 736be5ce0e9SQii Wang return -2; 737be5ce0e9SQii Wang } 738be5ce0e9SQii Wang 739be5ce0e9SQii Wang sda_max = spec->max_hd_dat_ns / sample_ns; 740be5ce0e9SQii Wang if (sda_max > low_cnt) 741be5ce0e9SQii Wang sda_max = 0; 742be5ce0e9SQii Wang 743be5ce0e9SQii Wang sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns); 744be5ce0e9SQii Wang if (sda_min < low_cnt) 745be5ce0e9SQii Wang sda_min = 0; 746be5ce0e9SQii Wang 747be5ce0e9SQii Wang if (sda_min > sda_max) 748be5ce0e9SQii Wang return -3; 749be5ce0e9SQii Wang 75063ce8e3dSQii Wang if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 751be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 752be5ce0e9SQii Wang i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE | 753be5ce0e9SQii Wang (sample_cnt << 12) | (high_cnt << 8); 754be5ce0e9SQii Wang i2c->ac_timing.ltiming &= ~GENMASK(15, 9); 755be5ce0e9SQii Wang i2c->ac_timing.ltiming |= (sample_cnt << 12) | 756be5ce0e9SQii Wang (low_cnt << 9); 757be5ce0e9SQii Wang i2c->ac_timing.ext &= ~GENMASK(7, 1); 758be5ce0e9SQii Wang i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0); 759be5ce0e9SQii Wang } else { 760be5ce0e9SQii Wang i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) | 761be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 762be5ce0e9SQii Wang i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) | 763be5ce0e9SQii Wang su_sta_cnt; 764be5ce0e9SQii Wang } 765be5ce0e9SQii Wang i2c->ac_timing.sda_timing &= ~GENMASK(11, 6); 766be5ce0e9SQii Wang i2c->ac_timing.sda_timing |= (1 << 12) | 767be5ce0e9SQii Wang ((sda_max + sda_min) / 2) << 6; 768be5ce0e9SQii Wang } else { 769be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 770be5ce0e9SQii Wang i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt); 771be5ce0e9SQii Wang i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt); 772be5ce0e9SQii Wang i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0); 773be5ce0e9SQii Wang } else { 774be5ce0e9SQii Wang i2c->ac_timing.scl_hl_ratio = (1 << 12) | 775be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 776be5ce0e9SQii Wang i2c->ac_timing.sta_stop = (su_sta_cnt << 8) | 777be5ce0e9SQii Wang su_sta_cnt; 778be5ce0e9SQii Wang } 779be5ce0e9SQii Wang 780be5ce0e9SQii Wang i2c->ac_timing.sda_timing = (1 << 12) | 781be5ce0e9SQii Wang (sda_max + sda_min) / 2; 782be5ce0e9SQii Wang } 783be5ce0e9SQii Wang 784be5ce0e9SQii Wang return 0; 785be5ce0e9SQii Wang } 786be5ce0e9SQii Wang 787ce38815dSXudong Chen /* 788ce38815dSXudong Chen * Calculate i2c port speed 789ce38815dSXudong Chen * 790ce38815dSXudong Chen * Hardware design: 791ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 792ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 793ce38815dSXudong Chen * 794ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 795ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 796ce38815dSXudong Chen * sample_cnt and step_cn 797ce38815dSXudong Chen */ 798f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 799f2326401SJun Gao unsigned int target_speed, 800f2326401SJun Gao unsigned int *timing_step_cnt, 801f2326401SJun Gao unsigned int *timing_sample_cnt) 802ce38815dSXudong Chen { 803ce38815dSXudong Chen unsigned int step_cnt; 804ce38815dSXudong Chen unsigned int sample_cnt; 805ce38815dSXudong Chen unsigned int max_step_cnt; 806ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 807ce38815dSXudong Chen unsigned int base_step_cnt; 808ce38815dSXudong Chen unsigned int opt_div; 809ce38815dSXudong Chen unsigned int best_mul; 810ce38815dSXudong Chen unsigned int cnt_mul; 811be5ce0e9SQii Wang int ret = -EINVAL; 812b5a796c6SKewei Xu int clk_div_restri = 0; 813ce38815dSXudong Chen 814ff6f3affSQii Wang if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) 815ff6f3affSQii Wang target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; 816ce38815dSXudong Chen 817be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(target_speed); 818ce38815dSXudong Chen base_step_cnt = max_step_cnt; 819ce38815dSXudong Chen /* Find the best combination */ 820ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 821ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 822ce38815dSXudong Chen 823ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 824ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 825ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 826ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 827ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 828ce38815dSXudong Chen */ 829ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 830b5a796c6SKewei Xu clk_div_restri = mtk_i2c_get_clk_div_restri(i2c, sample_cnt); 831b5a796c6SKewei Xu step_cnt = DIV_ROUND_UP(opt_div + clk_div_restri, sample_cnt); 832ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 833ce38815dSXudong Chen if (step_cnt > max_step_cnt) 834ce38815dSXudong Chen continue; 835ce38815dSXudong Chen 836ce38815dSXudong Chen if (cnt_mul < best_mul) { 837be5ce0e9SQii Wang ret = mtk_i2c_check_ac_timing(i2c, clk_src, 838be5ce0e9SQii Wang target_speed, step_cnt - 1, sample_cnt - 1); 839be5ce0e9SQii Wang if (ret) 840be5ce0e9SQii Wang continue; 841be5ce0e9SQii Wang 842ce38815dSXudong Chen best_mul = cnt_mul; 843ce38815dSXudong Chen base_sample_cnt = sample_cnt; 844ce38815dSXudong Chen base_step_cnt = step_cnt; 845b5a796c6SKewei Xu if (best_mul == (opt_div + clk_div_restri)) 846ce38815dSXudong Chen break; 847ce38815dSXudong Chen } 848ce38815dSXudong Chen } 849ce38815dSXudong Chen 850be5ce0e9SQii Wang if (ret) 851be5ce0e9SQii Wang return -EINVAL; 852be5ce0e9SQii Wang 853ce38815dSXudong Chen sample_cnt = base_sample_cnt; 854ce38815dSXudong Chen step_cnt = base_step_cnt; 855ce38815dSXudong Chen 856b5a796c6SKewei Xu if ((clk_src / (2 * (sample_cnt * step_cnt - clk_div_restri))) > 857b5a796c6SKewei Xu target_speed) { 858ce38815dSXudong Chen /* In this case, hardware can't support such 859ce38815dSXudong Chen * low i2c_bus_freq 860ce38815dSXudong Chen */ 861ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 862ce38815dSXudong Chen return -EINVAL; 863ce38815dSXudong Chen } 864ce38815dSXudong Chen 865f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 866f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 867f2326401SJun Gao 868f2326401SJun Gao return 0; 869f2326401SJun Gao } 870f2326401SJun Gao 871f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 872f2326401SJun Gao { 873f2326401SJun Gao unsigned int clk_src; 874f2326401SJun Gao unsigned int step_cnt; 875f2326401SJun Gao unsigned int sample_cnt; 87625708278SQii Wang unsigned int l_step_cnt; 87725708278SQii Wang unsigned int l_sample_cnt; 878f2326401SJun Gao unsigned int target_speed; 879be5ce0e9SQii Wang unsigned int clk_div; 880be5ce0e9SQii Wang unsigned int max_clk_div; 881f2326401SJun Gao int ret; 882f2326401SJun Gao 883f2326401SJun Gao target_speed = i2c->speed_hz; 884be5ce0e9SQii Wang parent_clk /= i2c->clk_src_div; 885be5ce0e9SQii Wang 886b5a796c6SKewei Xu if (i2c->dev_comp->timing_adjust && i2c->dev_comp->ltiming_adjust) 887b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_5BITS; 888b5a796c6SKewei Xu else if (i2c->dev_comp->timing_adjust) 889b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_8BITS; 890be5ce0e9SQii Wang else 891be5ce0e9SQii Wang max_clk_div = 1; 892be5ce0e9SQii Wang 893be5ce0e9SQii Wang for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { 894be5ce0e9SQii Wang clk_src = parent_clk / clk_div; 895b5a796c6SKewei Xu i2c->ac_timing.inter_clk_div = clk_div - 1; 896ce38815dSXudong Chen 897b44658e7SQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 898f2326401SJun Gao /* Set master code speed register */ 899be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 900be5ce0e9SQii Wang I2C_MAX_FAST_MODE_FREQ, 901be5ce0e9SQii Wang &l_step_cnt, 902be5ce0e9SQii Wang &l_sample_cnt); 903f2326401SJun Gao if (ret < 0) 904be5ce0e9SQii Wang continue; 905f2326401SJun Gao 90625708278SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 907f2326401SJun Gao 908ce38815dSXudong Chen /* Set the high speed mode register */ 909be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 910be5ce0e9SQii Wang target_speed, &step_cnt, 911be5ce0e9SQii Wang &sample_cnt); 912f2326401SJun Gao if (ret < 0) 913be5ce0e9SQii Wang continue; 914f2326401SJun Gao 915ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 916ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 91725708278SQii Wang 91825708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 919be5ce0e9SQii Wang i2c->ltiming_reg = 920be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt | 92125708278SQii Wang (sample_cnt << 12) | (step_cnt << 9); 922ce38815dSXudong Chen } else { 923be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 924be5ce0e9SQii Wang target_speed, &l_step_cnt, 925be5ce0e9SQii Wang &l_sample_cnt); 926f2326401SJun Gao if (ret < 0) 927be5ce0e9SQii Wang continue; 928f2326401SJun Gao 929be5ce0e9SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 930f2326401SJun Gao 931ce38815dSXudong Chen /* Disable the high speed transaction */ 932ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 93325708278SQii Wang 93425708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 935be5ce0e9SQii Wang i2c->ltiming_reg = 936be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt; 937ce38815dSXudong Chen } 938ce38815dSXudong Chen 939be5ce0e9SQii Wang break; 940be5ce0e9SQii Wang } 941be5ce0e9SQii Wang 942be5ce0e9SQii Wang 943ce38815dSXudong Chen return 0; 944ce38815dSXudong Chen } 945ce38815dSXudong Chen 946cc28e578SKewei Xu static void i2c_dump_register(struct mtk_i2c *i2c) 947cc28e578SKewei Xu { 948cc28e578SKewei Xu dev_dbg(i2c->dev, "SLAVE_ADDR: 0x%x, INTR_MASK: 0x%x\n", 949cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_SLAVE_ADDR), 950cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_MASK)); 951cc28e578SKewei Xu dev_dbg(i2c->dev, "INTR_STAT: 0x%x, CONTROL: 0x%x\n", 952cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_STAT), 953cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CONTROL)); 954cc28e578SKewei Xu dev_dbg(i2c->dev, "TRANSFER_LEN: 0x%x, TRANSAC_LEN: 0x%x\n", 955cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN), 956cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSAC_LEN)); 957cc28e578SKewei Xu dev_dbg(i2c->dev, "DELAY_LEN: 0x%x, HTIMING: 0x%x\n", 958cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DELAY_LEN), 959cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TIMING)); 960cc28e578SKewei Xu dev_dbg(i2c->dev, "START: 0x%x, EXT_CONF: 0x%x\n", 961cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_START), 962cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_EXT_CONF)); 963cc28e578SKewei Xu dev_dbg(i2c->dev, "HS: 0x%x, IO_CONFIG: 0x%x\n", 964cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_HS), 965cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_IO_CONFIG)); 966cc28e578SKewei Xu dev_dbg(i2c->dev, "DCM_EN: 0x%x, TRANSFER_LEN_AUX: 0x%x\n", 967cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DCM_EN), 968cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN_AUX)); 969cc28e578SKewei Xu dev_dbg(i2c->dev, "CLOCK_DIV: 0x%x, FIFO_STAT: 0x%x\n", 970cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CLOCK_DIV), 971cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_FIFO_STAT)); 972cc28e578SKewei Xu dev_dbg(i2c->dev, "DEBUGCTRL : 0x%x, DEBUGSTAT: 0x%x\n", 973cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGCTRL), 974cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGSTAT)); 975cc28e578SKewei Xu if (i2c->dev_comp->regs == mt_i2c_regs_v2) { 976cc28e578SKewei Xu dev_dbg(i2c->dev, "LTIMING: 0x%x, MULTI_DMA: 0x%x\n", 977cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_LTIMING), 978cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_MULTI_DMA)); 979cc28e578SKewei Xu } 980cc28e578SKewei Xu dev_dbg(i2c->dev, "\nDMA_INT_FLAG: 0x%x, DMA_INT_EN: 0x%x\n", 981cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_FLAG), 982cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_EN)); 983cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_EN: 0x%x, DMA_CON: 0x%x\n", 984cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_EN), 985cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_CON)); 986cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_MEM_ADDR: 0x%x, DMA_RX_MEM_ADDR: 0x%x\n", 987cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_MEM_ADDR), 988cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_MEM_ADDR)); 989cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_LEN: 0x%x, DMA_RX_LEN: 0x%x\n", 990cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_LEN), 991cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_LEN)); 992cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_4G_MODE: 0x%x, DMA_RX_4G_MODE: 0x%x", 993cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_4G_MODE), 994cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_4G_MODE)); 995cc28e578SKewei Xu } 996cc28e578SKewei Xu 997b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 998b2ed11e2SEddie Huang int num, int left_num) 999ce38815dSXudong Chen { 1000ce38815dSXudong Chen u16 addr_reg; 1001b2ed11e2SEddie Huang u16 start_reg; 1002ce38815dSXudong Chen u16 control_reg; 1003b2ed11e2SEddie Huang u16 restart_flag = 0; 10048426fe70SQii Wang u16 dma_sync = 0; 1005f4f4fed6SLiguo Zhang u32 reg_4g_mode; 1006e3e4949eSKewei Xu u32 reg_dma_reset; 1007fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 1008fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 1009ce38815dSXudong Chen dma_addr_t rpaddr = 0; 1010ce38815dSXudong Chen dma_addr_t wpaddr = 0; 1011ce38815dSXudong Chen int ret; 1012ce38815dSXudong Chen 1013ce38815dSXudong Chen i2c->irq_stat = 0; 1014ce38815dSXudong Chen 1015173b77e8SLiguo Zhang if (i2c->auto_restart) 1016b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1017b2ed11e2SEddie Huang 1018ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 1019ce38815dSXudong Chen 1020e3e4949eSKewei Xu if (i2c->dev_comp->apdma_sync && 1021e3e4949eSKewei Xu i2c->op != I2C_MASTER_WRRD && num > 1) { 1022e3e4949eSKewei Xu mtk_i2c_writew(i2c, 0x00, OFFSET_DEBUGCTRL); 1023e3e4949eSKewei Xu writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_WARM_RST, 1024e3e4949eSKewei Xu i2c->pdmabase + OFFSET_RST); 1025e3e4949eSKewei Xu 1026e3e4949eSKewei Xu ret = readw_poll_timeout(i2c->pdmabase + OFFSET_RST, 1027e3e4949eSKewei Xu reg_dma_reset, 1028e3e4949eSKewei Xu !(reg_dma_reset & I2C_DMA_WARM_RST), 1029e3e4949eSKewei Xu 0, 100); 1030e3e4949eSKewei Xu if (ret) { 1031e3e4949eSKewei Xu dev_err(i2c->dev, "DMA warm reset timeout\n"); 1032e3e4949eSKewei Xu return -ETIMEDOUT; 1033e3e4949eSKewei Xu } 1034e3e4949eSKewei Xu 1035e3e4949eSKewei Xu writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 1036e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST, OFFSET_SOFTRESET); 1037e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 1038e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_RELIABILITY | I2C_DMAACK_ENABLE, 1039e3e4949eSKewei Xu OFFSET_DEBUGCTRL); 1040e3e4949eSKewei Xu } 1041e3e4949eSKewei Xu 1042bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 1043ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 104463ce8e3dSQii Wang if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1)) 1045ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 1046ce38815dSXudong Chen 1047ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 1048ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 1049ce38815dSXudong Chen 1050bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 1051ce38815dSXudong Chen 10520d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 1053bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 1054ce38815dSXudong Chen 1055ce38815dSXudong Chen /* Clear interrupt status */ 1056bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1057cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 1058bc6eaf17SQii Wang 1059bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 1060ce38815dSXudong Chen 1061ce38815dSXudong Chen /* Enable interrupt */ 1062bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1063cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 1064ce38815dSXudong Chen 1065ce38815dSXudong Chen /* Set transfer and transaction len */ 1066ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 1067173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 1068bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1069bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 1070173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 1071173b77e8SLiguo Zhang } else { 1072bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 1073bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 1074173b77e8SLiguo Zhang } 1075bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 1076ce38815dSXudong Chen } else { 1077bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1078bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 1079ce38815dSXudong Chen } 1080ce38815dSXudong Chen 10818426fe70SQii Wang if (i2c->dev_comp->apdma_sync) { 10828426fe70SQii Wang dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE; 10838426fe70SQii Wang if (i2c->op == I2C_MASTER_WRRD) 10848426fe70SQii Wang dma_sync |= I2C_DMA_DIR_CHANGE; 10858426fe70SQii Wang } 10868426fe70SQii Wang 1087ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 1088ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 1089ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 10908426fe70SQii Wang writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON); 1091fc66b39fSJun Gao 1092bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1093fc66b39fSJun Gao if (!dma_rd_buf) 1094ce38815dSXudong Chen return -ENOMEM; 1095f4f4fed6SLiguo Zhang 1096fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1097fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 1098fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 1099fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 1100fc66b39fSJun Gao 1101fc66b39fSJun Gao return -ENOMEM; 1102fc66b39fSJun Gao } 1103fc66b39fSJun Gao 1104908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1105908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1106f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1107f4f4fed6SLiguo Zhang } 1108f4f4fed6SLiguo Zhang 1109ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1110ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 1111ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 1112ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 11138426fe70SQii Wang writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON); 1114fc66b39fSJun Gao 1115bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1116fc66b39fSJun Gao if (!dma_wr_buf) 1117ce38815dSXudong Chen return -ENOMEM; 1118f4f4fed6SLiguo Zhang 1119fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1120fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1121fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1122fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1123fc66b39fSJun Gao 1124fc66b39fSJun Gao return -ENOMEM; 1125fc66b39fSJun Gao } 1126fc66b39fSJun Gao 1127908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1128908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1129f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1130f4f4fed6SLiguo Zhang } 1131f4f4fed6SLiguo Zhang 1132ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1133ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1134ce38815dSXudong Chen } else { 1135ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 11368426fe70SQii Wang writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON); 1137fc66b39fSJun Gao 1138bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1139fc66b39fSJun Gao if (!dma_wr_buf) 1140ce38815dSXudong Chen return -ENOMEM; 1141fc66b39fSJun Gao 1142fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1143fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1144fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1145fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1146fc66b39fSJun Gao 1147fc66b39fSJun Gao return -ENOMEM; 1148fc66b39fSJun Gao } 1149fc66b39fSJun Gao 1150bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 1151fc66b39fSJun Gao if (!dma_rd_buf) { 1152fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 1153fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1154fc66b39fSJun Gao 1155fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1156fc66b39fSJun Gao 1157fc66b39fSJun Gao return -ENOMEM; 1158fc66b39fSJun Gao } 1159fc66b39fSJun Gao 1160fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1161ce38815dSXudong Chen (msgs + 1)->len, 1162ce38815dSXudong Chen DMA_FROM_DEVICE); 1163ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 1164ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1165ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1166fc66b39fSJun Gao 1167fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1168fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 1169fc66b39fSJun Gao 1170ce38815dSXudong Chen return -ENOMEM; 1171ce38815dSXudong Chen } 1172f4f4fed6SLiguo Zhang 1173908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1174908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1175f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1176f4f4fed6SLiguo Zhang 1177908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1178f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1179f4f4fed6SLiguo Zhang } 1180f4f4fed6SLiguo Zhang 1181ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1182ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1183ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1184ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 1185ce38815dSXudong Chen } 1186ce38815dSXudong Chen 1187ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 1188b2ed11e2SEddie Huang 1189173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1190b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 1191b2ed11e2SEddie Huang } else { 1192b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 1193b2ed11e2SEddie Huang if (left_num >= 1) 1194b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 1195b2ed11e2SEddie Huang } 1196bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 1197ce38815dSXudong Chen 1198ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 1199ce38815dSXudong Chen i2c->adap.timeout); 1200ce38815dSXudong Chen 1201ce38815dSXudong Chen /* Clear interrupt mask */ 1202bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1203cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 1204ce38815dSXudong Chen 1205ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 1206ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1207ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1208fc66b39fSJun Gao 1209fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1210ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 1211ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 1212ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 1213fc66b39fSJun Gao 1214fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 1215ce38815dSXudong Chen } else { 1216ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 1217ce38815dSXudong Chen DMA_TO_DEVICE); 1218ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 1219ce38815dSXudong Chen DMA_FROM_DEVICE); 1220fc66b39fSJun Gao 1221fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1222fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 1223ce38815dSXudong Chen } 1224ce38815dSXudong Chen 1225ce38815dSXudong Chen if (ret == 0) { 1226ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 1227cc28e578SKewei Xu i2c_dump_register(i2c); 1228ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1229ce38815dSXudong Chen return -ETIMEDOUT; 1230ce38815dSXudong Chen } 1231ce38815dSXudong Chen 1232ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 1233ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 1234ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1235ce38815dSXudong Chen return -ENXIO; 1236ce38815dSXudong Chen } 1237ce38815dSXudong Chen 1238ce38815dSXudong Chen return 0; 1239ce38815dSXudong Chen } 1240ce38815dSXudong Chen 1241ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 1242ce38815dSXudong Chen struct i2c_msg msgs[], int num) 1243ce38815dSXudong Chen { 1244ce38815dSXudong Chen int ret; 1245ce38815dSXudong Chen int left_num = num; 1246ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 1247ce38815dSXudong Chen 12488b4fc246SAngeloGioacchino Del Regno ret = clk_bulk_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1249ce38815dSXudong Chen if (ret) 1250ce38815dSXudong Chen return ret; 1251ce38815dSXudong Chen 1252173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 1253173b77e8SLiguo Zhang 1254173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 1255173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 1256173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 1257173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 1258173b77e8SLiguo Zhang i2c->auto_restart = 0; 1259173b77e8SLiguo Zhang } 1260173b77e8SLiguo Zhang } 1261173b77e8SLiguo Zhang 126263ce8e3dSQii Wang if (i2c->auto_restart && num >= 2 && 126363ce8e3dSQii Wang i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 12648378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 12658378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 12668378d01fSLiguo Zhang */ 12678378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 12688378d01fSLiguo Zhang else 12698378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 12708378d01fSLiguo Zhang 1271b2ed11e2SEddie Huang while (left_num--) { 1272ce38815dSXudong Chen if (!msgs->buf) { 1273ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 1274ce38815dSXudong Chen ret = -EINVAL; 1275ce38815dSXudong Chen goto err_exit; 1276ce38815dSXudong Chen } 1277ce38815dSXudong Chen 1278ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 1279ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 1280ce38815dSXudong Chen else 1281ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 1282ce38815dSXudong Chen 1283173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1284ce38815dSXudong Chen if (num > 1) { 1285ce38815dSXudong Chen /* combined two messages into one transaction */ 1286ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 1287ce38815dSXudong Chen left_num--; 1288ce38815dSXudong Chen } 1289b2ed11e2SEddie Huang } 1290ce38815dSXudong Chen 1291ce38815dSXudong Chen /* always use DMA mode. */ 1292b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 1293ce38815dSXudong Chen if (ret < 0) 1294ce38815dSXudong Chen goto err_exit; 1295ce38815dSXudong Chen 1296b2ed11e2SEddie Huang msgs++; 1297b2ed11e2SEddie Huang } 1298ce38815dSXudong Chen /* the return value is number of executed messages */ 1299ce38815dSXudong Chen ret = num; 1300ce38815dSXudong Chen 1301ce38815dSXudong Chen err_exit: 13028b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1303ce38815dSXudong Chen return ret; 1304ce38815dSXudong Chen } 1305ce38815dSXudong Chen 1306ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 1307ce38815dSXudong Chen { 1308ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 1309b2ed11e2SEddie Huang u16 restart_flag = 0; 131028c0a843SEddie Huang u16 intr_stat; 1311b2ed11e2SEddie Huang 1312173b77e8SLiguo Zhang if (i2c->auto_restart) 1313b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1314ce38815dSXudong Chen 1315bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 1316bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 1317ce38815dSXudong Chen 131828c0a843SEddie Huang /* 131928c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 132028c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 132128c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 132228c0a843SEddie Huang */ 132328c0a843SEddie Huang i2c->irq_stat |= intr_stat; 13248378d01fSLiguo Zhang 13258378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 13268378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 13278378d01fSLiguo Zhang i2c->irq_stat = 0; 1328bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 1329bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 13308378d01fSLiguo Zhang } else { 133128c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 1332ce38815dSXudong Chen complete(&i2c->msg_complete); 13338378d01fSLiguo Zhang } 1334ce38815dSXudong Chen 1335ce38815dSXudong Chen return IRQ_HANDLED; 1336ce38815dSXudong Chen } 1337ce38815dSXudong Chen 1338ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 1339ce38815dSXudong Chen { 134062931ac2SFabien Parent if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN)) 1341abf4923eSHsin-Yi Wang return I2C_FUNC_I2C | 1342abf4923eSHsin-Yi Wang (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1343abf4923eSHsin-Yi Wang else 1344ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1345ce38815dSXudong Chen } 1346ce38815dSXudong Chen 1347ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 1348ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 1349ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 1350ce38815dSXudong Chen }; 1351ce38815dSXudong Chen 1352f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 1353ce38815dSXudong Chen { 1354ce38815dSXudong Chen int ret; 1355ce38815dSXudong Chen 1356ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 1357ce38815dSXudong Chen if (ret < 0) 135890224e64SAndy Shevchenko i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ; 1359ce38815dSXudong Chen 1360f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 1361ce38815dSXudong Chen if (ret < 0) 1362ce38815dSXudong Chen return ret; 1363ce38815dSXudong Chen 1364f2326401SJun Gao if (i2c->clk_src_div == 0) 1365ce38815dSXudong Chen return -EINVAL; 1366ce38815dSXudong Chen 1367ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 1368ce38815dSXudong Chen i2c->use_push_pull = 1369ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 1370ce38815dSXudong Chen 1371a80f2494SQii Wang i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); 1372a80f2494SQii Wang 1373ce38815dSXudong Chen return 0; 1374ce38815dSXudong Chen } 1375ce38815dSXudong Chen 1376ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 1377ce38815dSXudong Chen { 1378ce38815dSXudong Chen int ret = 0; 1379ce38815dSXudong Chen struct mtk_i2c *i2c; 13800016a32fSAngeloGioacchino Del Regno int i, irq, speed_clk; 1381ce38815dSXudong Chen 1382ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 1383ce38815dSXudong Chen if (!i2c) 1384ce38815dSXudong Chen return -ENOMEM; 1385ce38815dSXudong Chen 138683a7f470Sye xingchen i2c->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); 1387ce38815dSXudong Chen if (IS_ERR(i2c->base)) 1388ce38815dSXudong Chen return PTR_ERR(i2c->base); 1389ce38815dSXudong Chen 139083a7f470Sye xingchen i2c->pdmabase = devm_platform_get_and_ioremap_resource(pdev, 1, NULL); 1391ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 1392ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 1393ce38815dSXudong Chen 1394ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 139558fb7c64SSergey Shtylyov if (irq < 0) 1396ce38815dSXudong Chen return irq; 1397ce38815dSXudong Chen 1398ce38815dSXudong Chen init_completion(&i2c->msg_complete); 1399ce38815dSXudong Chen 14006e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 1401ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 1402ce38815dSXudong Chen i2c->dev = &pdev->dev; 1403ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 1404ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 1405ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 1406ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 1407ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 1408ce38815dSXudong Chen i2c->adap.retries = 1; 14099029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = devm_regulator_get_optional(&pdev->dev, "vbus"); 14109029b9b2SHsin-Yi Wang if (IS_ERR(i2c->adap.bus_regulator)) { 14119029b9b2SHsin-Yi Wang if (PTR_ERR(i2c->adap.bus_regulator) == -ENODEV) 14129029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = NULL; 14139029b9b2SHsin-Yi Wang else 14149029b9b2SHsin-Yi Wang return PTR_ERR(i2c->adap.bus_regulator); 14159029b9b2SHsin-Yi Wang } 1416ce38815dSXudong Chen 14175a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 14185a10e7d7SJun Gao if (ret) 14195a10e7d7SJun Gao return -EINVAL; 14205a10e7d7SJun Gao 1421ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 1422ce38815dSXudong Chen return -EINVAL; 1423ce38815dSXudong Chen 14240016a32fSAngeloGioacchino Del Regno /* Fill in clk-bulk IDs */ 14250016a32fSAngeloGioacchino Del Regno for (i = 0; i < I2C_MT65XX_CLK_MAX; i++) 14260016a32fSAngeloGioacchino Del Regno i2c->clocks[i].id = i2c_mt65xx_clk_ids[i]; 14270016a32fSAngeloGioacchino Del Regno 14280016a32fSAngeloGioacchino Del Regno /* Get clocks one by one, some may be optional */ 14290016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_MAIN].clk = devm_clk_get(&pdev->dev, "main"); 14300016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_MAIN].clk)) { 1431ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 14320016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_MAIN].clk); 1433ce38815dSXudong Chen } 1434ce38815dSXudong Chen 14350016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_DMA].clk = devm_clk_get(&pdev->dev, "dma"); 14360016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_DMA].clk)) { 1437ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 14380016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_DMA].clk); 1439ce38815dSXudong Chen } 1440ce38815dSXudong Chen 14410016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_ARB].clk = devm_clk_get_optional(&pdev->dev, "arb"); 14420016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_ARB].clk)) 14430016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_ARB].clk); 1444cad6dc5dSQii Wang 1445*bcfaaa97SDaniel Golle i2c->clocks[I2C_MT65XX_CLK_PMIC].clk = devm_clk_get_optional(&pdev->dev, "pmic"); 14460016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_PMIC].clk)) { 1447ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 14480016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_PMIC].clk); 1449ce38815dSXudong Chen } 1450*bcfaaa97SDaniel Golle 1451*bcfaaa97SDaniel Golle if (i2c->have_pmic) { 1452*bcfaaa97SDaniel Golle if (!i2c->clocks[I2C_MT65XX_CLK_PMIC].clk) { 1453*bcfaaa97SDaniel Golle dev_err(&pdev->dev, "cannot get pmic clock\n"); 1454*bcfaaa97SDaniel Golle return -ENODEV; 1455*bcfaaa97SDaniel Golle } 14560016a32fSAngeloGioacchino Del Regno speed_clk = I2C_MT65XX_CLK_PMIC; 14570016a32fSAngeloGioacchino Del Regno } else { 14580016a32fSAngeloGioacchino Del Regno speed_clk = I2C_MT65XX_CLK_MAIN; 1459ce38815dSXudong Chen } 1460ce38815dSXudong Chen 1461ea1558ceSWolfram Sang strscpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1462ce38815dSXudong Chen 14630016a32fSAngeloGioacchino Del Regno ret = mtk_i2c_set_speed(i2c, clk_get_rate(i2c->clocks[speed_clk].clk)); 1464ce38815dSXudong Chen if (ret) { 1465ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 1466ce38815dSXudong Chen return -EINVAL; 1467ce38815dSXudong Chen } 1468ce38815dSXudong Chen 1469908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1470908d9843SQii Wang ret = dma_set_mask(&pdev->dev, 1471908d9843SQii Wang DMA_BIT_MASK(i2c->dev_comp->max_dma_support)); 1472f4f4fed6SLiguo Zhang if (ret) { 1473f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 1474f4f4fed6SLiguo Zhang return ret; 1475f4f4fed6SLiguo Zhang } 1476f4f4fed6SLiguo Zhang } 1477f4f4fed6SLiguo Zhang 14780016a32fSAngeloGioacchino Del Regno ret = clk_bulk_prepare_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1479ce38815dSXudong Chen if (ret) { 1480ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 1481ce38815dSXudong Chen return ret; 1482ce38815dSXudong Chen } 1483ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 14848b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1485ce38815dSXudong Chen 1486ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 1487de96c394SQii Wang IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, 14887fb9dc81SQii Wang dev_name(&pdev->dev), i2c); 1489ce38815dSXudong Chen if (ret < 0) { 1490ce38815dSXudong Chen dev_err(&pdev->dev, 1491ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 1492de87b603SChristophe JAILLET goto err_bulk_unprepare; 1493ce38815dSXudong Chen } 1494ce38815dSXudong Chen 1495ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 1496ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 1497ea734404SWolfram Sang if (ret) 1498de87b603SChristophe JAILLET goto err_bulk_unprepare; 1499ce38815dSXudong Chen 1500ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 1501ce38815dSXudong Chen 1502ce38815dSXudong Chen return 0; 1503de87b603SChristophe JAILLET 1504de87b603SChristophe JAILLET err_bulk_unprepare: 1505de87b603SChristophe JAILLET clk_bulk_unprepare(I2C_MT65XX_CLK_MAX, i2c->clocks); 1506de87b603SChristophe JAILLET 1507de87b603SChristophe JAILLET return ret; 1508ce38815dSXudong Chen } 1509ce38815dSXudong Chen 1510e190a0c3SUwe Kleine-König static void mtk_i2c_remove(struct platform_device *pdev) 1511ce38815dSXudong Chen { 1512ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 1513ce38815dSXudong Chen 1514ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 1515ce38815dSXudong Chen 15168b4fc246SAngeloGioacchino Del Regno clk_bulk_unprepare(I2C_MT65XX_CLK_MAX, i2c->clocks); 1517ce38815dSXudong Chen } 1518ce38815dSXudong Chen 1519de96c394SQii Wang static int mtk_i2c_suspend_noirq(struct device *dev) 1520de96c394SQii Wang { 1521de96c394SQii Wang struct mtk_i2c *i2c = dev_get_drvdata(dev); 1522de96c394SQii Wang 1523de96c394SQii Wang i2c_mark_adapter_suspended(&i2c->adap); 15248b4fc246SAngeloGioacchino Del Regno clk_bulk_unprepare(I2C_MT65XX_CLK_MAX, i2c->clocks); 1525de96c394SQii Wang 1526de96c394SQii Wang return 0; 1527de96c394SQii Wang } 1528de96c394SQii Wang 1529de96c394SQii Wang static int mtk_i2c_resume_noirq(struct device *dev) 153009027e08SLiguo Zhang { 1531f6762cedSJun Gao int ret; 153209027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 153309027e08SLiguo Zhang 15340016a32fSAngeloGioacchino Del Regno ret = clk_bulk_prepare_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1535f6762cedSJun Gao if (ret) { 1536f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 1537f6762cedSJun Gao return ret; 1538f6762cedSJun Gao } 1539f6762cedSJun Gao 154009027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 154109027e08SLiguo Zhang 15428b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1543f6762cedSJun Gao 1544de96c394SQii Wang i2c_mark_adapter_resumed(&i2c->adap); 1545de96c394SQii Wang 154609027e08SLiguo Zhang return 0; 154709027e08SLiguo Zhang } 154809027e08SLiguo Zhang 154909027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 1550ba733668SPaul Cercueil NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq, 1551de96c394SQii Wang mtk_i2c_resume_noirq) 155209027e08SLiguo Zhang }; 155309027e08SLiguo Zhang 1554ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1555ce38815dSXudong Chen .probe = mtk_i2c_probe, 1556e190a0c3SUwe Kleine-König .remove_new = mtk_i2c_remove, 1557ce38815dSXudong Chen .driver = { 1558ce38815dSXudong Chen .name = I2C_DRV_NAME, 1559ba733668SPaul Cercueil .pm = pm_sleep_ptr(&mtk_i2c_pm), 1560ee4de636SKrzysztof Kozlowski .of_match_table = mtk_i2c_of_match, 1561ce38815dSXudong Chen }, 1562ce38815dSXudong Chen }; 1563ce38815dSXudong Chen 1564ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1565ce38815dSXudong Chen 1566ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1567ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1568ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1569