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> 22ce38815dSXudong Chen #include <linux/of_address.h> 236e29577fSRyder Lee #include <linux/of_device.h> 24ce38815dSXudong Chen #include <linux/of_irq.h> 25ce38815dSXudong Chen #include <linux/platform_device.h> 26ce38815dSXudong Chen #include <linux/scatterlist.h> 27ce38815dSXudong Chen #include <linux/sched.h> 28ce38815dSXudong Chen #include <linux/slab.h> 29ce38815dSXudong Chen 30b2ed11e2SEddie Huang #define I2C_RS_TRANSFER (1 << 4) 31cad6dc5dSQii Wang #define I2C_ARB_LOST (1 << 3) 32ce38815dSXudong Chen #define I2C_HS_NACKERR (1 << 2) 33ce38815dSXudong Chen #define I2C_ACKERR (1 << 1) 34ce38815dSXudong Chen #define I2C_TRANSAC_COMP (1 << 0) 35ce38815dSXudong Chen #define I2C_TRANSAC_START (1 << 0) 36b2ed11e2SEddie Huang #define I2C_RS_MUL_CNFG (1 << 15) 37b2ed11e2SEddie Huang #define I2C_RS_MUL_TRIG (1 << 14) 38ce38815dSXudong Chen #define I2C_DCM_DISABLE 0x0000 39ce38815dSXudong Chen #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003 40ce38815dSXudong Chen #define I2C_IO_CONFIG_PUSH_PULL 0x0000 41ce38815dSXudong Chen #define I2C_SOFT_RST 0x0001 4205f6f727SQii Wang #define I2C_HANDSHAKE_RST 0x0020 43ce38815dSXudong Chen #define I2C_FIFO_ADDR_CLR 0x0001 44ce38815dSXudong Chen #define I2C_DELAY_LEN 0x0002 453bce7703SKewei Xu #define I2C_ST_START_CON 0x8001 463bce7703SKewei Xu #define I2C_FS_START_CON 0x1800 47ce38815dSXudong Chen #define I2C_TIME_CLR_VALUE 0x0000 48ce38815dSXudong Chen #define I2C_TIME_DEFAULT_VALUE 0x0003 49ce38815dSXudong Chen #define I2C_WRRD_TRANAC_VALUE 0x0002 50ce38815dSXudong Chen #define I2C_RD_TRANAC_VALUE 0x0001 51be5ce0e9SQii Wang #define I2C_SCL_MIS_COMP_VALUE 0x0000 5205f6f727SQii Wang #define I2C_CHN_CLR_FLAG 0x0000 53e3e4949eSKewei Xu #define I2C_RELIABILITY 0x0010 54e3e4949eSKewei Xu #define I2C_DMAACK_ENABLE 0x0008 55ce38815dSXudong Chen 56ce38815dSXudong Chen #define I2C_DMA_CON_TX 0x0000 57ce38815dSXudong Chen #define I2C_DMA_CON_RX 0x0001 588426fe70SQii Wang #define I2C_DMA_ASYNC_MODE 0x0004 598426fe70SQii Wang #define I2C_DMA_SKIP_CONFIG 0x0010 608426fe70SQii Wang #define I2C_DMA_DIR_CHANGE 0x0200 61ce38815dSXudong Chen #define I2C_DMA_START_EN 0x0001 62ce38815dSXudong Chen #define I2C_DMA_INT_FLAG_NONE 0x0000 63ce38815dSXudong Chen #define I2C_DMA_CLR_FLAG 0x0000 6405f6f727SQii Wang #define I2C_DMA_WARM_RST 0x0001 65ea89ef1fSEddie Huang #define I2C_DMA_HARD_RST 0x0002 6605f6f727SQii Wang #define I2C_DMA_HANDSHAKE_RST 0x0004 67ce38815dSXudong Chen 68ce38815dSXudong Chen #define MAX_SAMPLE_CNT_DIV 8 69ce38815dSXudong Chen #define MAX_STEP_CNT_DIV 64 70b5a796c6SKewei Xu #define MAX_CLOCK_DIV_8BITS 256 71b5a796c6SKewei Xu #define MAX_CLOCK_DIV_5BITS 32 72ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV 8 73b5a796c6SKewei Xu #define I2C_STANDARD_MODE_BUFFER (1000 / 3) 74b5a796c6SKewei Xu #define I2C_FAST_MODE_BUFFER (300 / 3) 75b5a796c6SKewei Xu #define I2C_FAST_MODE_PLUS_BUFFER (20 / 3) 76ce38815dSXudong Chen 77ce38815dSXudong Chen #define I2C_CONTROL_RS (0x1 << 1) 78ce38815dSXudong Chen #define I2C_CONTROL_DMA_EN (0x1 << 2) 79ce38815dSXudong Chen #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3) 80ce38815dSXudong Chen #define I2C_CONTROL_DIR_CHANGE (0x1 << 4) 81ce38815dSXudong Chen #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5) 82ce38815dSXudong Chen #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6) 83a15c91baSQii Wang #define I2C_CONTROL_DMAACK_EN (0x1 << 8) 84a15c91baSQii Wang #define I2C_CONTROL_ASYNC_MODE (0x1 << 9) 85ce38815dSXudong Chen #define I2C_CONTROL_WRAPPER (0x1 << 0) 86ce38815dSXudong Chen 87ce38815dSXudong Chen #define I2C_DRV_NAME "i2c-mt65xx" 88ce38815dSXudong Chen 890016a32fSAngeloGioacchino Del Regno /** 900016a32fSAngeloGioacchino Del Regno * enum i2c_mt65xx_clks - Clocks enumeration for MT65XX I2C 910016a32fSAngeloGioacchino Del Regno * 920016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_MAIN: main clock for i2c bus 930016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_DMA: DMA clock for i2c via DMA 940016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_PMIC: PMIC clock for i2c from PMIC 950016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_ARB: Arbitrator clock for i2c 960016a32fSAngeloGioacchino Del Regno * @I2C_MT65XX_CLK_MAX: Number of supported clocks 970016a32fSAngeloGioacchino Del Regno */ 980016a32fSAngeloGioacchino Del Regno enum i2c_mt65xx_clks { 990016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_MAIN = 0, 1000016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_DMA, 1010016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_PMIC, 1020016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_ARB, 1030016a32fSAngeloGioacchino Del Regno I2C_MT65XX_CLK_MAX 1040016a32fSAngeloGioacchino Del Regno }; 1050016a32fSAngeloGioacchino Del Regno 1060016a32fSAngeloGioacchino Del Regno static const char * const i2c_mt65xx_clk_ids[I2C_MT65XX_CLK_MAX] = { 1070016a32fSAngeloGioacchino Del Regno "main", "dma", "pmic", "arb" 1080016a32fSAngeloGioacchino Del Regno }; 1090016a32fSAngeloGioacchino Del Regno 110ce38815dSXudong Chen enum DMA_REGS_OFFSET { 111ce38815dSXudong Chen OFFSET_INT_FLAG = 0x0, 112ce38815dSXudong Chen OFFSET_INT_EN = 0x04, 113ce38815dSXudong Chen OFFSET_EN = 0x08, 114ea89ef1fSEddie Huang OFFSET_RST = 0x0c, 115ce38815dSXudong Chen OFFSET_CON = 0x18, 116ce38815dSXudong Chen OFFSET_TX_MEM_ADDR = 0x1c, 117ce38815dSXudong Chen OFFSET_RX_MEM_ADDR = 0x20, 118ce38815dSXudong Chen OFFSET_TX_LEN = 0x24, 119ce38815dSXudong Chen OFFSET_RX_LEN = 0x28, 120f4f4fed6SLiguo Zhang OFFSET_TX_4G_MODE = 0x54, 121f4f4fed6SLiguo Zhang OFFSET_RX_4G_MODE = 0x58, 122ce38815dSXudong Chen }; 123ce38815dSXudong Chen 124ce38815dSXudong Chen enum i2c_trans_st_rs { 125ce38815dSXudong Chen I2C_TRANS_STOP = 0, 126ce38815dSXudong Chen I2C_TRANS_REPEATED_START, 127ce38815dSXudong Chen }; 128ce38815dSXudong Chen 129ce38815dSXudong Chen enum mtk_trans_op { 130ce38815dSXudong Chen I2C_MASTER_WR = 1, 131ce38815dSXudong Chen I2C_MASTER_RD, 132ce38815dSXudong Chen I2C_MASTER_WRRD, 133ce38815dSXudong Chen }; 134ce38815dSXudong Chen 135ce38815dSXudong Chen enum I2C_REGS_OFFSET { 136bc6eaf17SQii Wang OFFSET_DATA_PORT, 137bc6eaf17SQii Wang OFFSET_SLAVE_ADDR, 138bc6eaf17SQii Wang OFFSET_INTR_MASK, 139bc6eaf17SQii Wang OFFSET_INTR_STAT, 140bc6eaf17SQii Wang OFFSET_CONTROL, 141bc6eaf17SQii Wang OFFSET_TRANSFER_LEN, 142bc6eaf17SQii Wang OFFSET_TRANSAC_LEN, 143bc6eaf17SQii Wang OFFSET_DELAY_LEN, 144bc6eaf17SQii Wang OFFSET_TIMING, 145bc6eaf17SQii Wang OFFSET_START, 146bc6eaf17SQii Wang OFFSET_EXT_CONF, 147bc6eaf17SQii Wang OFFSET_FIFO_STAT, 148bc6eaf17SQii Wang OFFSET_FIFO_THRESH, 149bc6eaf17SQii Wang OFFSET_FIFO_ADDR_CLR, 150bc6eaf17SQii Wang OFFSET_IO_CONFIG, 151bc6eaf17SQii Wang OFFSET_RSV_DEBUG, 152bc6eaf17SQii Wang OFFSET_HS, 153bc6eaf17SQii Wang OFFSET_SOFTRESET, 154bc6eaf17SQii Wang OFFSET_DCM_EN, 155cc28e578SKewei Xu OFFSET_MULTI_DMA, 156bc6eaf17SQii Wang OFFSET_PATH_DIR, 157bc6eaf17SQii Wang OFFSET_DEBUGSTAT, 158bc6eaf17SQii Wang OFFSET_DEBUGCTRL, 159bc6eaf17SQii Wang OFFSET_TRANSFER_LEN_AUX, 160bc6eaf17SQii Wang OFFSET_CLOCK_DIV, 16125708278SQii Wang OFFSET_LTIMING, 162be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO, 163be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO, 164be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT, 165be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING, 166be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING, 167be5ce0e9SQii Wang OFFSET_SDA_TIMING, 168bc6eaf17SQii Wang }; 169bc6eaf17SQii Wang 170bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = { 171bc6eaf17SQii Wang [OFFSET_DATA_PORT] = 0x0, 172bc6eaf17SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 173bc6eaf17SQii Wang [OFFSET_INTR_MASK] = 0x8, 174bc6eaf17SQii Wang [OFFSET_INTR_STAT] = 0xc, 175bc6eaf17SQii Wang [OFFSET_CONTROL] = 0x10, 176bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 177bc6eaf17SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 178bc6eaf17SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 179bc6eaf17SQii Wang [OFFSET_TIMING] = 0x20, 180bc6eaf17SQii Wang [OFFSET_START] = 0x24, 181bc6eaf17SQii Wang [OFFSET_EXT_CONF] = 0x28, 182bc6eaf17SQii Wang [OFFSET_FIFO_STAT] = 0x30, 183bc6eaf17SQii Wang [OFFSET_FIFO_THRESH] = 0x34, 184bc6eaf17SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 185bc6eaf17SQii Wang [OFFSET_IO_CONFIG] = 0x40, 186bc6eaf17SQii Wang [OFFSET_RSV_DEBUG] = 0x44, 187bc6eaf17SQii Wang [OFFSET_HS] = 0x48, 188bc6eaf17SQii Wang [OFFSET_SOFTRESET] = 0x50, 189bc6eaf17SQii Wang [OFFSET_DCM_EN] = 0x54, 190bc6eaf17SQii Wang [OFFSET_PATH_DIR] = 0x60, 191bc6eaf17SQii Wang [OFFSET_DEBUGSTAT] = 0x64, 192bc6eaf17SQii Wang [OFFSET_DEBUGCTRL] = 0x68, 193bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x6c, 194bc6eaf17SQii Wang [OFFSET_CLOCK_DIV] = 0x70, 195be5ce0e9SQii Wang [OFFSET_SCL_HIGH_LOW_RATIO] = 0x74, 196be5ce0e9SQii Wang [OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78, 197be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x7C, 198be5ce0e9SQii Wang [OFFSET_STA_STO_AC_TIMING] = 0x80, 199be5ce0e9SQii Wang [OFFSET_HS_STA_STO_AC_TIMING] = 0x84, 200be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x88, 201ce38815dSXudong Chen }; 202ce38815dSXudong Chen 20325708278SQii Wang static const u16 mt_i2c_regs_v2[] = { 20425708278SQii Wang [OFFSET_DATA_PORT] = 0x0, 20525708278SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 20625708278SQii Wang [OFFSET_INTR_MASK] = 0x8, 20725708278SQii Wang [OFFSET_INTR_STAT] = 0xc, 20825708278SQii Wang [OFFSET_CONTROL] = 0x10, 20925708278SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 21025708278SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 21125708278SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 21225708278SQii Wang [OFFSET_TIMING] = 0x20, 21325708278SQii Wang [OFFSET_START] = 0x24, 21425708278SQii Wang [OFFSET_EXT_CONF] = 0x28, 21525708278SQii Wang [OFFSET_LTIMING] = 0x2c, 21625708278SQii Wang [OFFSET_HS] = 0x30, 21725708278SQii Wang [OFFSET_IO_CONFIG] = 0x34, 21825708278SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 219be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x3c, 22025708278SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x44, 22125708278SQii Wang [OFFSET_CLOCK_DIV] = 0x48, 22225708278SQii Wang [OFFSET_SOFTRESET] = 0x50, 223cc28e578SKewei Xu [OFFSET_MULTI_DMA] = 0x8c, 224be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x90, 225b8228aeaSKewei Xu [OFFSET_DEBUGSTAT] = 0xe4, 22625708278SQii Wang [OFFSET_DEBUGCTRL] = 0xe8, 22725708278SQii Wang [OFFSET_FIFO_STAT] = 0xf4, 22825708278SQii Wang [OFFSET_FIFO_THRESH] = 0xf8, 22925708278SQii Wang [OFFSET_DCM_EN] = 0xf88, 23025708278SQii Wang }; 23125708278SQii Wang 232ce38815dSXudong Chen struct mtk_i2c_compatible { 233ce38815dSXudong Chen const struct i2c_adapter_quirks *quirks; 234bc6eaf17SQii Wang const u16 *regs; 235ce38815dSXudong Chen unsigned char pmic_i2c: 1; 236ce38815dSXudong Chen unsigned char dcm: 1; 237b2ed11e2SEddie Huang unsigned char auto_restart: 1; 238173b77e8SLiguo Zhang unsigned char aux_len_reg: 1; 2395a10e7d7SJun Gao unsigned char timing_adjust: 1; 240a15c91baSQii Wang unsigned char dma_sync: 1; 24125708278SQii Wang unsigned char ltiming_adjust: 1; 2428426fe70SQii Wang unsigned char apdma_sync: 1; 243908d9843SQii Wang unsigned char max_dma_support; 244ce38815dSXudong Chen }; 245ce38815dSXudong Chen 246be5ce0e9SQii Wang struct mtk_i2c_ac_timing { 247be5ce0e9SQii Wang u16 htiming; 248be5ce0e9SQii Wang u16 ltiming; 249be5ce0e9SQii Wang u16 hs; 250be5ce0e9SQii Wang u16 ext; 251be5ce0e9SQii Wang u16 inter_clk_div; 252be5ce0e9SQii Wang u16 scl_hl_ratio; 253be5ce0e9SQii Wang u16 hs_scl_hl_ratio; 254be5ce0e9SQii Wang u16 sta_stop; 255be5ce0e9SQii Wang u16 hs_sta_stop; 256be5ce0e9SQii Wang u16 sda_timing; 257be5ce0e9SQii Wang }; 258be5ce0e9SQii Wang 259ce38815dSXudong Chen struct mtk_i2c { 260ce38815dSXudong Chen struct i2c_adapter adap; /* i2c host adapter */ 261ce38815dSXudong Chen struct device *dev; 262ce38815dSXudong Chen struct completion msg_complete; 263a80f2494SQii Wang struct i2c_timings timing_info; 264ce38815dSXudong Chen 265ce38815dSXudong Chen /* set in i2c probe */ 266ce38815dSXudong Chen void __iomem *base; /* i2c base addr */ 267ce38815dSXudong Chen void __iomem *pdmabase; /* dma base address*/ 2680016a32fSAngeloGioacchino Del Regno struct clk_bulk_data clocks[I2C_MT65XX_CLK_MAX]; /* clocks for i2c */ 269ce38815dSXudong Chen bool have_pmic; /* can use i2c pins from PMIC */ 270ce38815dSXudong Chen bool use_push_pull; /* IO config push-pull mode */ 271ce38815dSXudong Chen 272ce38815dSXudong Chen u16 irq_stat; /* interrupt status */ 273f2326401SJun Gao unsigned int clk_src_div; 274ce38815dSXudong Chen unsigned int speed_hz; /* The speed in transfer */ 275ce38815dSXudong Chen enum mtk_trans_op op; 276ce38815dSXudong Chen u16 timing_reg; 277ce38815dSXudong Chen u16 high_speed_reg; 27825708278SQii Wang u16 ltiming_reg; 279173b77e8SLiguo Zhang unsigned char auto_restart; 2808378d01fSLiguo Zhang bool ignore_restart_irq; 281be5ce0e9SQii Wang struct mtk_i2c_ac_timing ac_timing; 282ce38815dSXudong Chen const struct mtk_i2c_compatible *dev_comp; 283ce38815dSXudong Chen }; 284ce38815dSXudong Chen 285be5ce0e9SQii Wang /** 286be5ce0e9SQii Wang * struct i2c_spec_values: 287b0102a89SMatthias Brugger * @min_low_ns: min LOW period of the SCL clock 288b0102a89SMatthias Brugger * @min_su_sta_ns: min set-up time for a repeated START condition 289b0102a89SMatthias Brugger * @max_hd_dat_ns: max data hold time 290b0102a89SMatthias Brugger * @min_su_dat_ns: min data set-up time 291be5ce0e9SQii Wang */ 292be5ce0e9SQii Wang struct i2c_spec_values { 293be5ce0e9SQii Wang unsigned int min_low_ns; 294be5ce0e9SQii Wang unsigned int min_su_sta_ns; 295be5ce0e9SQii Wang unsigned int max_hd_dat_ns; 296be5ce0e9SQii Wang unsigned int min_su_dat_ns; 297be5ce0e9SQii Wang }; 298be5ce0e9SQii Wang 299be5ce0e9SQii Wang static const struct i2c_spec_values standard_mode_spec = { 300be5ce0e9SQii Wang .min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 301be5ce0e9SQii Wang .min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 302be5ce0e9SQii Wang .max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER, 303be5ce0e9SQii Wang .min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER, 304be5ce0e9SQii Wang }; 305be5ce0e9SQii Wang 306be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_spec = { 307be5ce0e9SQii Wang .min_low_ns = 1300 + I2C_FAST_MODE_BUFFER, 308be5ce0e9SQii Wang .min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER, 309be5ce0e9SQii Wang .max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER, 310be5ce0e9SQii Wang .min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER, 311be5ce0e9SQii Wang }; 312be5ce0e9SQii Wang 313be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_plus_spec = { 314be5ce0e9SQii Wang .min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER, 315be5ce0e9SQii Wang .min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER, 316be5ce0e9SQii Wang .max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER, 317be5ce0e9SQii Wang .min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER, 318be5ce0e9SQii Wang }; 319be5ce0e9SQii Wang 320ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = { 321ce38815dSXudong Chen .flags = I2C_AQ_COMB_WRITE_THEN_READ, 322ce38815dSXudong Chen .max_num_msgs = 1, 323ce38815dSXudong Chen .max_write_len = 255, 324ce38815dSXudong Chen .max_read_len = 255, 325ce38815dSXudong Chen .max_comb_1st_msg_len = 255, 326ce38815dSXudong Chen .max_comb_2nd_msg_len = 31, 327ce38815dSXudong Chen }; 328ce38815dSXudong Chen 3291304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = { 3301304fe09SJun Gao .max_num_msgs = 255, 3311304fe09SJun Gao }; 3321304fe09SJun Gao 333abf4923eSHsin-Yi Wang static const struct i2c_adapter_quirks mt8183_i2c_quirks = { 334abf4923eSHsin-Yi Wang .flags = I2C_AQ_NO_ZERO_LEN, 335abf4923eSHsin-Yi Wang }; 336abf4923eSHsin-Yi Wang 3375a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = { 338bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3395a10e7d7SJun Gao .pmic_i2c = 0, 3405a10e7d7SJun Gao .dcm = 1, 3415a10e7d7SJun Gao .auto_restart = 1, 3425a10e7d7SJun Gao .aux_len_reg = 1, 3435a10e7d7SJun Gao .timing_adjust = 1, 344a15c91baSQii Wang .dma_sync = 0, 34525708278SQii Wang .ltiming_adjust = 0, 3468426fe70SQii Wang .apdma_sync = 0, 347908d9843SQii Wang .max_dma_support = 33, 3485a10e7d7SJun Gao }; 3495a10e7d7SJun Gao 350ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = { 351ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 352bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 353ce38815dSXudong Chen .pmic_i2c = 0, 354ce38815dSXudong Chen .dcm = 1, 355b2ed11e2SEddie Huang .auto_restart = 0, 356173b77e8SLiguo Zhang .aux_len_reg = 0, 3575a10e7d7SJun Gao .timing_adjust = 0, 358a15c91baSQii Wang .dma_sync = 0, 35925708278SQii Wang .ltiming_adjust = 0, 3608426fe70SQii Wang .apdma_sync = 0, 361908d9843SQii Wang .max_dma_support = 32, 362ce38815dSXudong Chen }; 363ce38815dSXudong Chen 364ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = { 365ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 366bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 367ce38815dSXudong Chen .pmic_i2c = 1, 368ce38815dSXudong Chen .dcm = 0, 369b2ed11e2SEddie Huang .auto_restart = 0, 370173b77e8SLiguo Zhang .aux_len_reg = 0, 3715a10e7d7SJun Gao .timing_adjust = 0, 372a15c91baSQii Wang .dma_sync = 0, 37325708278SQii Wang .ltiming_adjust = 0, 3748426fe70SQii Wang .apdma_sync = 0, 375908d9843SQii Wang .max_dma_support = 32, 376b2ed11e2SEddie Huang }; 377b2ed11e2SEddie Huang 3781304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = { 3791304fe09SJun Gao .quirks = &mt7622_i2c_quirks, 380bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3811304fe09SJun Gao .pmic_i2c = 0, 3821304fe09SJun Gao .dcm = 1, 3831304fe09SJun Gao .auto_restart = 1, 3841304fe09SJun Gao .aux_len_reg = 1, 3855a10e7d7SJun Gao .timing_adjust = 0, 386a15c91baSQii Wang .dma_sync = 0, 38725708278SQii Wang .ltiming_adjust = 0, 3888426fe70SQii Wang .apdma_sync = 0, 389908d9843SQii Wang .max_dma_support = 32, 3901304fe09SJun Gao }; 3911304fe09SJun Gao 3921bff55b5SKewei Xu static const struct mtk_i2c_compatible mt8168_compat = { 3931bff55b5SKewei Xu .regs = mt_i2c_regs_v1, 3941bff55b5SKewei Xu .pmic_i2c = 0, 3951bff55b5SKewei Xu .dcm = 1, 3961bff55b5SKewei Xu .auto_restart = 1, 3971bff55b5SKewei Xu .aux_len_reg = 1, 3981bff55b5SKewei Xu .timing_adjust = 1, 3991bff55b5SKewei Xu .dma_sync = 1, 4001bff55b5SKewei Xu .ltiming_adjust = 0, 4011bff55b5SKewei Xu .apdma_sync = 0, 4021bff55b5SKewei Xu .max_dma_support = 33, 4031bff55b5SKewei Xu }; 4041bff55b5SKewei Xu 405b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = { 406bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 407b2ed11e2SEddie Huang .pmic_i2c = 0, 408b2ed11e2SEddie Huang .dcm = 1, 409b2ed11e2SEddie Huang .auto_restart = 1, 410173b77e8SLiguo Zhang .aux_len_reg = 1, 4115a10e7d7SJun Gao .timing_adjust = 0, 412a15c91baSQii Wang .dma_sync = 0, 41325708278SQii Wang .ltiming_adjust = 0, 4148426fe70SQii Wang .apdma_sync = 0, 415908d9843SQii Wang .max_dma_support = 33, 41625708278SQii Wang }; 41725708278SQii Wang 41825708278SQii Wang static const struct mtk_i2c_compatible mt8183_compat = { 419abf4923eSHsin-Yi Wang .quirks = &mt8183_i2c_quirks, 42025708278SQii Wang .regs = mt_i2c_regs_v2, 42125708278SQii Wang .pmic_i2c = 0, 42225708278SQii Wang .dcm = 0, 42325708278SQii Wang .auto_restart = 1, 42425708278SQii Wang .aux_len_reg = 1, 42525708278SQii Wang .timing_adjust = 1, 42625708278SQii Wang .dma_sync = 1, 42725708278SQii Wang .ltiming_adjust = 1, 4288426fe70SQii Wang .apdma_sync = 0, 429908d9843SQii Wang .max_dma_support = 33, 430ce38815dSXudong Chen }; 431ce38815dSXudong Chen 43293470531SKewei Xu static const struct mtk_i2c_compatible mt8186_compat = { 43393470531SKewei Xu .regs = mt_i2c_regs_v2, 43493470531SKewei Xu .pmic_i2c = 0, 43593470531SKewei Xu .dcm = 0, 43693470531SKewei Xu .auto_restart = 1, 43793470531SKewei Xu .aux_len_reg = 1, 43893470531SKewei Xu .timing_adjust = 1, 43993470531SKewei Xu .dma_sync = 0, 44093470531SKewei Xu .ltiming_adjust = 1, 44193470531SKewei Xu .apdma_sync = 0, 44293470531SKewei Xu .max_dma_support = 36, 44393470531SKewei Xu }; 44493470531SKewei Xu 445789e67baSQii Wang static const struct mtk_i2c_compatible mt8192_compat = { 446789e67baSQii Wang .quirks = &mt8183_i2c_quirks, 447789e67baSQii Wang .regs = mt_i2c_regs_v2, 448789e67baSQii Wang .pmic_i2c = 0, 449789e67baSQii Wang .dcm = 0, 450789e67baSQii Wang .auto_restart = 1, 451789e67baSQii Wang .aux_len_reg = 1, 452789e67baSQii Wang .timing_adjust = 1, 453789e67baSQii Wang .dma_sync = 1, 454789e67baSQii Wang .ltiming_adjust = 1, 455789e67baSQii Wang .apdma_sync = 1, 456789e67baSQii Wang .max_dma_support = 36, 457789e67baSQii Wang }; 458789e67baSQii Wang 459ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = { 4605a10e7d7SJun Gao { .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat }, 461ce38815dSXudong Chen { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat }, 462ce38815dSXudong Chen { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat }, 4631304fe09SJun Gao { .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat }, 4641bff55b5SKewei Xu { .compatible = "mediatek,mt8168-i2c", .data = &mt8168_compat }, 465b2ed11e2SEddie Huang { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat }, 46625708278SQii Wang { .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat }, 46793470531SKewei Xu { .compatible = "mediatek,mt8186-i2c", .data = &mt8186_compat }, 468789e67baSQii Wang { .compatible = "mediatek,mt8192-i2c", .data = &mt8192_compat }, 469ce38815dSXudong Chen {} 470ce38815dSXudong Chen }; 471ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); 472ce38815dSXudong Chen 473bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg) 474bc6eaf17SQii Wang { 475bc6eaf17SQii Wang return readw(i2c->base + i2c->dev_comp->regs[reg]); 476bc6eaf17SQii Wang } 477bc6eaf17SQii Wang 478bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val, 479bc6eaf17SQii Wang enum I2C_REGS_OFFSET reg) 480bc6eaf17SQii Wang { 481bc6eaf17SQii Wang writew(val, i2c->base + i2c->dev_comp->regs[reg]); 482bc6eaf17SQii Wang } 483bc6eaf17SQii Wang 484ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c) 485ce38815dSXudong Chen { 486ce38815dSXudong Chen u16 control_reg; 487fed1bd51SQii Wang u16 intr_stat_reg; 4883bce7703SKewei Xu u16 ext_conf_val; 489fed1bd51SQii Wang 490fed1bd51SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_START); 491fed1bd51SQii Wang intr_stat_reg = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 492fed1bd51SQii Wang mtk_i2c_writew(i2c, intr_stat_reg, OFFSET_INTR_STAT); 493ce38815dSXudong Chen 4943186b880SQii Wang if (i2c->dev_comp->apdma_sync) { 49505f6f727SQii Wang writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST); 49605f6f727SQii Wang udelay(10); 49705f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 49805f6f727SQii Wang udelay(10); 49905f6f727SQii Wang writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST, 50005f6f727SQii Wang i2c->pdmabase + OFFSET_RST); 50105f6f727SQii Wang mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST, 50205f6f727SQii Wang OFFSET_SOFTRESET); 50305f6f727SQii Wang udelay(10); 50405f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 50505f6f727SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 50605f6f727SQii Wang } else { 507aafced67SQii Wang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 508aafced67SQii Wang udelay(50); 509aafced67SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 510bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 51105f6f727SQii Wang } 512ce38815dSXudong Chen 513ce38815dSXudong Chen /* Set ioconfig */ 514ce38815dSXudong Chen if (i2c->use_push_pull) 515bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 516ce38815dSXudong Chen else 517bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 518ce38815dSXudong Chen 519ce38815dSXudong Chen if (i2c->dev_comp->dcm) 520bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 521ce38815dSXudong Chen 522bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 523bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 52425708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 52525708278SQii Wang mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING); 526ce38815dSXudong Chen 5273bce7703SKewei Xu if (i2c->speed_hz <= I2C_MAX_STANDARD_MODE_FREQ) 5283bce7703SKewei Xu ext_conf_val = I2C_ST_START_CON; 5293bce7703SKewei Xu else 5303bce7703SKewei Xu ext_conf_val = I2C_FS_START_CON; 5313bce7703SKewei Xu 532be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) { 5333bce7703SKewei Xu ext_conf_val = i2c->ac_timing.ext; 534be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, 535be5ce0e9SQii Wang OFFSET_CLOCK_DIV); 536be5ce0e9SQii Wang mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE, 537be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT); 538be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing, 539be5ce0e9SQii Wang OFFSET_SDA_TIMING); 540be5ce0e9SQii Wang 541be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 542be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.htiming, 543be5ce0e9SQii Wang OFFSET_TIMING); 544be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS); 545be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ltiming, 546be5ce0e9SQii Wang OFFSET_LTIMING); 547be5ce0e9SQii Wang } else { 548be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio, 549be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO); 550be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio, 551be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO); 552be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop, 553be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING); 554be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop, 555be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING); 556be5ce0e9SQii Wang } 557be5ce0e9SQii Wang } 5583bce7703SKewei Xu mtk_i2c_writew(i2c, ext_conf_val, OFFSET_EXT_CONF); 559be5ce0e9SQii Wang 560ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 561ce38815dSXudong Chen if (i2c->have_pmic) 562bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 563ce38815dSXudong Chen 564ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 565ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 566a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 567a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 568a15c91baSQii Wang 569bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 570bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 571ce38815dSXudong Chen } 572ce38815dSXudong Chen 573be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed) 574be5ce0e9SQii Wang { 575be5ce0e9SQii Wang if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 576be5ce0e9SQii Wang return &standard_mode_spec; 577be5ce0e9SQii Wang else if (speed <= I2C_MAX_FAST_MODE_FREQ) 578be5ce0e9SQii Wang return &fast_mode_spec; 579be5ce0e9SQii Wang else 580be5ce0e9SQii Wang return &fast_mode_plus_spec; 581be5ce0e9SQii Wang } 582be5ce0e9SQii Wang 583be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed) 584be5ce0e9SQii Wang { 58563ce8e3dSQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) 586be5ce0e9SQii Wang return MAX_HS_STEP_CNT_DIV; 587be5ce0e9SQii Wang else 588be5ce0e9SQii Wang return MAX_STEP_CNT_DIV; 589be5ce0e9SQii Wang } 590be5ce0e9SQii Wang 591b5a796c6SKewei Xu static int mtk_i2c_get_clk_div_restri(struct mtk_i2c *i2c, 592b5a796c6SKewei Xu unsigned int sample_cnt) 593b5a796c6SKewei Xu { 594b5a796c6SKewei Xu int clk_div_restri = 0; 595b5a796c6SKewei Xu 596b5a796c6SKewei Xu if (i2c->dev_comp->ltiming_adjust == 0) 597b5a796c6SKewei Xu return 0; 598b5a796c6SKewei Xu 599b5a796c6SKewei Xu if (sample_cnt == 1) { 600b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 601b5a796c6SKewei Xu clk_div_restri = 0; 602b5a796c6SKewei Xu else 603b5a796c6SKewei Xu clk_div_restri = 1; 604b5a796c6SKewei Xu } else { 605b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 606b5a796c6SKewei Xu clk_div_restri = -1; 607b5a796c6SKewei Xu else if (i2c->ac_timing.inter_clk_div == 1) 608b5a796c6SKewei Xu clk_div_restri = 0; 609b5a796c6SKewei Xu else 610b5a796c6SKewei Xu clk_div_restri = 1; 611b5a796c6SKewei Xu } 612b5a796c6SKewei Xu 613b5a796c6SKewei Xu return clk_div_restri; 614b5a796c6SKewei Xu } 615b5a796c6SKewei Xu 616be5ce0e9SQii Wang /* 617be5ce0e9SQii Wang * Check and Calculate i2c ac-timing 618be5ce0e9SQii Wang * 619be5ce0e9SQii Wang * Hardware design: 620be5ce0e9SQii Wang * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src 621be5ce0e9SQii Wang * xxx_cnt_div = spec->min_xxx_ns / sample_ns 622be5ce0e9SQii Wang * 623be5ce0e9SQii Wang * Sample_ns is rounded down for xxx_cnt_div would be greater 624be5ce0e9SQii Wang * than the smallest spec. 625be5ce0e9SQii Wang * The sda_timing is chosen as the middle value between 626be5ce0e9SQii Wang * the largest and smallest. 627be5ce0e9SQii Wang */ 628be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, 629be5ce0e9SQii Wang unsigned int clk_src, 630be5ce0e9SQii Wang unsigned int check_speed, 631be5ce0e9SQii Wang unsigned int step_cnt, 632be5ce0e9SQii Wang unsigned int sample_cnt) 633be5ce0e9SQii Wang { 634be5ce0e9SQii Wang const struct i2c_spec_values *spec; 635be5ce0e9SQii Wang unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt; 636be5ce0e9SQii Wang unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f; 637be5ce0e9SQii Wang unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), 638be5ce0e9SQii Wang clk_src); 639be5ce0e9SQii Wang 640be5ce0e9SQii Wang if (!i2c->dev_comp->timing_adjust) 641be5ce0e9SQii Wang return 0; 642be5ce0e9SQii Wang 643be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 644be5ce0e9SQii Wang max_sta_cnt = 0x100; 645be5ce0e9SQii Wang 646be5ce0e9SQii Wang spec = mtk_i2c_get_spec(check_speed); 647be5ce0e9SQii Wang 648be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 649be5ce0e9SQii Wang clk_ns = 1000000000 / clk_src; 650be5ce0e9SQii Wang else 651be5ce0e9SQii Wang clk_ns = sample_ns / 2; 652be5ce0e9SQii Wang 653a80f2494SQii Wang su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns + 654a80f2494SQii Wang i2c->timing_info.scl_int_delay_ns, clk_ns); 655be5ce0e9SQii Wang if (su_sta_cnt > max_sta_cnt) 656be5ce0e9SQii Wang return -1; 657be5ce0e9SQii Wang 658be5ce0e9SQii Wang low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns); 659be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(check_speed); 660be5ce0e9SQii Wang if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) { 661be5ce0e9SQii Wang if (low_cnt > step_cnt) { 662be5ce0e9SQii Wang high_cnt = 2 * step_cnt - low_cnt; 663be5ce0e9SQii Wang } else { 664be5ce0e9SQii Wang high_cnt = step_cnt; 665be5ce0e9SQii Wang low_cnt = step_cnt; 666be5ce0e9SQii Wang } 667be5ce0e9SQii Wang } else { 668be5ce0e9SQii Wang return -2; 669be5ce0e9SQii Wang } 670be5ce0e9SQii Wang 671be5ce0e9SQii Wang sda_max = spec->max_hd_dat_ns / sample_ns; 672be5ce0e9SQii Wang if (sda_max > low_cnt) 673be5ce0e9SQii Wang sda_max = 0; 674be5ce0e9SQii Wang 675be5ce0e9SQii Wang sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns); 676be5ce0e9SQii Wang if (sda_min < low_cnt) 677be5ce0e9SQii Wang sda_min = 0; 678be5ce0e9SQii Wang 679be5ce0e9SQii Wang if (sda_min > sda_max) 680be5ce0e9SQii Wang return -3; 681be5ce0e9SQii Wang 68263ce8e3dSQii Wang if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 683be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 684be5ce0e9SQii Wang i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE | 685be5ce0e9SQii Wang (sample_cnt << 12) | (high_cnt << 8); 686be5ce0e9SQii Wang i2c->ac_timing.ltiming &= ~GENMASK(15, 9); 687be5ce0e9SQii Wang i2c->ac_timing.ltiming |= (sample_cnt << 12) | 688be5ce0e9SQii Wang (low_cnt << 9); 689be5ce0e9SQii Wang i2c->ac_timing.ext &= ~GENMASK(7, 1); 690be5ce0e9SQii Wang i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0); 691be5ce0e9SQii Wang } else { 692be5ce0e9SQii Wang i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) | 693be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 694be5ce0e9SQii Wang i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) | 695be5ce0e9SQii Wang su_sta_cnt; 696be5ce0e9SQii Wang } 697be5ce0e9SQii Wang i2c->ac_timing.sda_timing &= ~GENMASK(11, 6); 698be5ce0e9SQii Wang i2c->ac_timing.sda_timing |= (1 << 12) | 699be5ce0e9SQii Wang ((sda_max + sda_min) / 2) << 6; 700be5ce0e9SQii Wang } else { 701be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 702be5ce0e9SQii Wang i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt); 703be5ce0e9SQii Wang i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt); 704be5ce0e9SQii Wang i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0); 705be5ce0e9SQii Wang } else { 706be5ce0e9SQii Wang i2c->ac_timing.scl_hl_ratio = (1 << 12) | 707be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 708be5ce0e9SQii Wang i2c->ac_timing.sta_stop = (su_sta_cnt << 8) | 709be5ce0e9SQii Wang su_sta_cnt; 710be5ce0e9SQii Wang } 711be5ce0e9SQii Wang 712be5ce0e9SQii Wang i2c->ac_timing.sda_timing = (1 << 12) | 713be5ce0e9SQii Wang (sda_max + sda_min) / 2; 714be5ce0e9SQii Wang } 715be5ce0e9SQii Wang 716be5ce0e9SQii Wang return 0; 717be5ce0e9SQii Wang } 718be5ce0e9SQii Wang 719ce38815dSXudong Chen /* 720ce38815dSXudong Chen * Calculate i2c port speed 721ce38815dSXudong Chen * 722ce38815dSXudong Chen * Hardware design: 723ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 724ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 725ce38815dSXudong Chen * 726ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 727ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 728ce38815dSXudong Chen * sample_cnt and step_cn 729ce38815dSXudong Chen */ 730f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 731f2326401SJun Gao unsigned int target_speed, 732f2326401SJun Gao unsigned int *timing_step_cnt, 733f2326401SJun Gao unsigned int *timing_sample_cnt) 734ce38815dSXudong Chen { 735ce38815dSXudong Chen unsigned int step_cnt; 736ce38815dSXudong Chen unsigned int sample_cnt; 737ce38815dSXudong Chen unsigned int max_step_cnt; 738ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 739ce38815dSXudong Chen unsigned int base_step_cnt; 740ce38815dSXudong Chen unsigned int opt_div; 741ce38815dSXudong Chen unsigned int best_mul; 742ce38815dSXudong Chen unsigned int cnt_mul; 743be5ce0e9SQii Wang int ret = -EINVAL; 744b5a796c6SKewei Xu int clk_div_restri = 0; 745ce38815dSXudong Chen 746ff6f3affSQii Wang if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) 747ff6f3affSQii Wang target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; 748ce38815dSXudong Chen 749be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(target_speed); 750ce38815dSXudong Chen base_step_cnt = max_step_cnt; 751ce38815dSXudong Chen /* Find the best combination */ 752ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 753ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 754ce38815dSXudong Chen 755ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 756ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 757ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 758ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 759ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 760ce38815dSXudong Chen */ 761ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 762b5a796c6SKewei Xu clk_div_restri = mtk_i2c_get_clk_div_restri(i2c, sample_cnt); 763b5a796c6SKewei Xu step_cnt = DIV_ROUND_UP(opt_div + clk_div_restri, sample_cnt); 764ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 765ce38815dSXudong Chen if (step_cnt > max_step_cnt) 766ce38815dSXudong Chen continue; 767ce38815dSXudong Chen 768ce38815dSXudong Chen if (cnt_mul < best_mul) { 769be5ce0e9SQii Wang ret = mtk_i2c_check_ac_timing(i2c, clk_src, 770be5ce0e9SQii Wang target_speed, step_cnt - 1, sample_cnt - 1); 771be5ce0e9SQii Wang if (ret) 772be5ce0e9SQii Wang continue; 773be5ce0e9SQii Wang 774ce38815dSXudong Chen best_mul = cnt_mul; 775ce38815dSXudong Chen base_sample_cnt = sample_cnt; 776ce38815dSXudong Chen base_step_cnt = step_cnt; 777b5a796c6SKewei Xu if (best_mul == (opt_div + clk_div_restri)) 778ce38815dSXudong Chen break; 779ce38815dSXudong Chen } 780ce38815dSXudong Chen } 781ce38815dSXudong Chen 782be5ce0e9SQii Wang if (ret) 783be5ce0e9SQii Wang return -EINVAL; 784be5ce0e9SQii Wang 785ce38815dSXudong Chen sample_cnt = base_sample_cnt; 786ce38815dSXudong Chen step_cnt = base_step_cnt; 787ce38815dSXudong Chen 788b5a796c6SKewei Xu if ((clk_src / (2 * (sample_cnt * step_cnt - clk_div_restri))) > 789b5a796c6SKewei Xu target_speed) { 790ce38815dSXudong Chen /* In this case, hardware can't support such 791ce38815dSXudong Chen * low i2c_bus_freq 792ce38815dSXudong Chen */ 793ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 794ce38815dSXudong Chen return -EINVAL; 795ce38815dSXudong Chen } 796ce38815dSXudong Chen 797f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 798f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 799f2326401SJun Gao 800f2326401SJun Gao return 0; 801f2326401SJun Gao } 802f2326401SJun Gao 803f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 804f2326401SJun Gao { 805f2326401SJun Gao unsigned int clk_src; 806f2326401SJun Gao unsigned int step_cnt; 807f2326401SJun Gao unsigned int sample_cnt; 80825708278SQii Wang unsigned int l_step_cnt; 80925708278SQii Wang unsigned int l_sample_cnt; 810f2326401SJun Gao unsigned int target_speed; 811be5ce0e9SQii Wang unsigned int clk_div; 812be5ce0e9SQii Wang unsigned int max_clk_div; 813f2326401SJun Gao int ret; 814f2326401SJun Gao 815f2326401SJun Gao target_speed = i2c->speed_hz; 816be5ce0e9SQii Wang parent_clk /= i2c->clk_src_div; 817be5ce0e9SQii Wang 818b5a796c6SKewei Xu if (i2c->dev_comp->timing_adjust && i2c->dev_comp->ltiming_adjust) 819b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_5BITS; 820b5a796c6SKewei Xu else if (i2c->dev_comp->timing_adjust) 821b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_8BITS; 822be5ce0e9SQii Wang else 823be5ce0e9SQii Wang max_clk_div = 1; 824be5ce0e9SQii Wang 825be5ce0e9SQii Wang for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { 826be5ce0e9SQii Wang clk_src = parent_clk / clk_div; 827b5a796c6SKewei Xu i2c->ac_timing.inter_clk_div = clk_div - 1; 828ce38815dSXudong Chen 829b44658e7SQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 830f2326401SJun Gao /* Set master code speed register */ 831be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 832be5ce0e9SQii Wang I2C_MAX_FAST_MODE_FREQ, 833be5ce0e9SQii Wang &l_step_cnt, 834be5ce0e9SQii Wang &l_sample_cnt); 835f2326401SJun Gao if (ret < 0) 836be5ce0e9SQii Wang continue; 837f2326401SJun Gao 83825708278SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 839f2326401SJun Gao 840ce38815dSXudong Chen /* Set the high speed mode register */ 841be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 842be5ce0e9SQii Wang target_speed, &step_cnt, 843be5ce0e9SQii Wang &sample_cnt); 844f2326401SJun Gao if (ret < 0) 845be5ce0e9SQii Wang continue; 846f2326401SJun Gao 847ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 848ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 84925708278SQii Wang 85025708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 851be5ce0e9SQii Wang i2c->ltiming_reg = 852be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt | 85325708278SQii Wang (sample_cnt << 12) | (step_cnt << 9); 854ce38815dSXudong Chen } else { 855be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 856be5ce0e9SQii Wang target_speed, &l_step_cnt, 857be5ce0e9SQii Wang &l_sample_cnt); 858f2326401SJun Gao if (ret < 0) 859be5ce0e9SQii Wang continue; 860f2326401SJun Gao 861be5ce0e9SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 862f2326401SJun Gao 863ce38815dSXudong Chen /* Disable the high speed transaction */ 864ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 86525708278SQii Wang 86625708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 867be5ce0e9SQii Wang i2c->ltiming_reg = 868be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt; 869ce38815dSXudong Chen } 870ce38815dSXudong Chen 871be5ce0e9SQii Wang break; 872be5ce0e9SQii Wang } 873be5ce0e9SQii Wang 874be5ce0e9SQii Wang 875ce38815dSXudong Chen return 0; 876ce38815dSXudong Chen } 877ce38815dSXudong Chen 878cc28e578SKewei Xu static void i2c_dump_register(struct mtk_i2c *i2c) 879cc28e578SKewei Xu { 880cc28e578SKewei Xu dev_dbg(i2c->dev, "SLAVE_ADDR: 0x%x, INTR_MASK: 0x%x\n", 881cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_SLAVE_ADDR), 882cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_MASK)); 883cc28e578SKewei Xu dev_dbg(i2c->dev, "INTR_STAT: 0x%x, CONTROL: 0x%x\n", 884cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_STAT), 885cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CONTROL)); 886cc28e578SKewei Xu dev_dbg(i2c->dev, "TRANSFER_LEN: 0x%x, TRANSAC_LEN: 0x%x\n", 887cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN), 888cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSAC_LEN)); 889cc28e578SKewei Xu dev_dbg(i2c->dev, "DELAY_LEN: 0x%x, HTIMING: 0x%x\n", 890cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DELAY_LEN), 891cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TIMING)); 892cc28e578SKewei Xu dev_dbg(i2c->dev, "START: 0x%x, EXT_CONF: 0x%x\n", 893cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_START), 894cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_EXT_CONF)); 895cc28e578SKewei Xu dev_dbg(i2c->dev, "HS: 0x%x, IO_CONFIG: 0x%x\n", 896cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_HS), 897cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_IO_CONFIG)); 898cc28e578SKewei Xu dev_dbg(i2c->dev, "DCM_EN: 0x%x, TRANSFER_LEN_AUX: 0x%x\n", 899cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DCM_EN), 900cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN_AUX)); 901cc28e578SKewei Xu dev_dbg(i2c->dev, "CLOCK_DIV: 0x%x, FIFO_STAT: 0x%x\n", 902cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CLOCK_DIV), 903cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_FIFO_STAT)); 904cc28e578SKewei Xu dev_dbg(i2c->dev, "DEBUGCTRL : 0x%x, DEBUGSTAT: 0x%x\n", 905cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGCTRL), 906cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGSTAT)); 907cc28e578SKewei Xu if (i2c->dev_comp->regs == mt_i2c_regs_v2) { 908cc28e578SKewei Xu dev_dbg(i2c->dev, "LTIMING: 0x%x, MULTI_DMA: 0x%x\n", 909cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_LTIMING), 910cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_MULTI_DMA)); 911cc28e578SKewei Xu } 912cc28e578SKewei Xu dev_dbg(i2c->dev, "\nDMA_INT_FLAG: 0x%x, DMA_INT_EN: 0x%x\n", 913cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_FLAG), 914cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_EN)); 915cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_EN: 0x%x, DMA_CON: 0x%x\n", 916cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_EN), 917cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_CON)); 918cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_MEM_ADDR: 0x%x, DMA_RX_MEM_ADDR: 0x%x\n", 919cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_MEM_ADDR), 920cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_MEM_ADDR)); 921cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_LEN: 0x%x, DMA_RX_LEN: 0x%x\n", 922cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_LEN), 923cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_LEN)); 924cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_4G_MODE: 0x%x, DMA_RX_4G_MODE: 0x%x", 925cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_4G_MODE), 926cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_4G_MODE)); 927cc28e578SKewei Xu } 928cc28e578SKewei Xu 929b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 930b2ed11e2SEddie Huang int num, int left_num) 931ce38815dSXudong Chen { 932ce38815dSXudong Chen u16 addr_reg; 933b2ed11e2SEddie Huang u16 start_reg; 934ce38815dSXudong Chen u16 control_reg; 935b2ed11e2SEddie Huang u16 restart_flag = 0; 9368426fe70SQii Wang u16 dma_sync = 0; 937f4f4fed6SLiguo Zhang u32 reg_4g_mode; 938e3e4949eSKewei Xu u32 reg_dma_reset; 939fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 940fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 941ce38815dSXudong Chen dma_addr_t rpaddr = 0; 942ce38815dSXudong Chen dma_addr_t wpaddr = 0; 943ce38815dSXudong Chen int ret; 944ce38815dSXudong Chen 945ce38815dSXudong Chen i2c->irq_stat = 0; 946ce38815dSXudong Chen 947173b77e8SLiguo Zhang if (i2c->auto_restart) 948b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 949b2ed11e2SEddie Huang 950ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 951ce38815dSXudong Chen 952e3e4949eSKewei Xu if (i2c->dev_comp->apdma_sync && 953e3e4949eSKewei Xu i2c->op != I2C_MASTER_WRRD && num > 1) { 954e3e4949eSKewei Xu mtk_i2c_writew(i2c, 0x00, OFFSET_DEBUGCTRL); 955e3e4949eSKewei Xu writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_WARM_RST, 956e3e4949eSKewei Xu i2c->pdmabase + OFFSET_RST); 957e3e4949eSKewei Xu 958e3e4949eSKewei Xu ret = readw_poll_timeout(i2c->pdmabase + OFFSET_RST, 959e3e4949eSKewei Xu reg_dma_reset, 960e3e4949eSKewei Xu !(reg_dma_reset & I2C_DMA_WARM_RST), 961e3e4949eSKewei Xu 0, 100); 962e3e4949eSKewei Xu if (ret) { 963e3e4949eSKewei Xu dev_err(i2c->dev, "DMA warm reset timeout\n"); 964e3e4949eSKewei Xu return -ETIMEDOUT; 965e3e4949eSKewei Xu } 966e3e4949eSKewei Xu 967e3e4949eSKewei Xu writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 968e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST, OFFSET_SOFTRESET); 969e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 970e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_RELIABILITY | I2C_DMAACK_ENABLE, 971e3e4949eSKewei Xu OFFSET_DEBUGCTRL); 972e3e4949eSKewei Xu } 973e3e4949eSKewei Xu 974bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 975ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 97663ce8e3dSQii Wang if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1)) 977ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 978ce38815dSXudong Chen 979ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 980ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 981ce38815dSXudong Chen 982bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 983ce38815dSXudong Chen 9840d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 985bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 986ce38815dSXudong Chen 987ce38815dSXudong Chen /* Clear interrupt status */ 988bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 989cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 990bc6eaf17SQii Wang 991bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 992ce38815dSXudong Chen 993ce38815dSXudong Chen /* Enable interrupt */ 994bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 995cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 996ce38815dSXudong Chen 997ce38815dSXudong Chen /* Set transfer and transaction len */ 998ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 999173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 1000bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1001bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 1002173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 1003173b77e8SLiguo Zhang } else { 1004bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 1005bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 1006173b77e8SLiguo Zhang } 1007bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 1008ce38815dSXudong Chen } else { 1009bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1010bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 1011ce38815dSXudong Chen } 1012ce38815dSXudong Chen 10138426fe70SQii Wang if (i2c->dev_comp->apdma_sync) { 10148426fe70SQii Wang dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE; 10158426fe70SQii Wang if (i2c->op == I2C_MASTER_WRRD) 10168426fe70SQii Wang dma_sync |= I2C_DMA_DIR_CHANGE; 10178426fe70SQii Wang } 10188426fe70SQii Wang 1019ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 1020ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 1021ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 10228426fe70SQii Wang writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON); 1023fc66b39fSJun Gao 1024bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1025fc66b39fSJun Gao if (!dma_rd_buf) 1026ce38815dSXudong Chen return -ENOMEM; 1027f4f4fed6SLiguo Zhang 1028fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1029fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 1030fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 1031fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 1032fc66b39fSJun Gao 1033fc66b39fSJun Gao return -ENOMEM; 1034fc66b39fSJun Gao } 1035fc66b39fSJun Gao 1036908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1037908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1038f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1039f4f4fed6SLiguo Zhang } 1040f4f4fed6SLiguo Zhang 1041ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1042ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 1043ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 1044ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 10458426fe70SQii Wang writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON); 1046fc66b39fSJun Gao 1047bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1048fc66b39fSJun Gao if (!dma_wr_buf) 1049ce38815dSXudong Chen return -ENOMEM; 1050f4f4fed6SLiguo Zhang 1051fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1052fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1053fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1054fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1055fc66b39fSJun Gao 1056fc66b39fSJun Gao return -ENOMEM; 1057fc66b39fSJun Gao } 1058fc66b39fSJun Gao 1059908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1060908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1061f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1062f4f4fed6SLiguo Zhang } 1063f4f4fed6SLiguo Zhang 1064ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1065ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1066ce38815dSXudong Chen } else { 1067ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 10688426fe70SQii Wang writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON); 1069fc66b39fSJun Gao 1070bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1071fc66b39fSJun Gao if (!dma_wr_buf) 1072ce38815dSXudong Chen return -ENOMEM; 1073fc66b39fSJun Gao 1074fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1075fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1076fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1077fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1078fc66b39fSJun Gao 1079fc66b39fSJun Gao return -ENOMEM; 1080fc66b39fSJun Gao } 1081fc66b39fSJun Gao 1082bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 1083fc66b39fSJun Gao if (!dma_rd_buf) { 1084fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 1085fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1086fc66b39fSJun Gao 1087fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1088fc66b39fSJun Gao 1089fc66b39fSJun Gao return -ENOMEM; 1090fc66b39fSJun Gao } 1091fc66b39fSJun Gao 1092fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1093ce38815dSXudong Chen (msgs + 1)->len, 1094ce38815dSXudong Chen DMA_FROM_DEVICE); 1095ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 1096ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1097ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1098fc66b39fSJun Gao 1099fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1100fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 1101fc66b39fSJun Gao 1102ce38815dSXudong Chen return -ENOMEM; 1103ce38815dSXudong Chen } 1104f4f4fed6SLiguo Zhang 1105908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1106908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1107f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1108f4f4fed6SLiguo Zhang 1109908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1110f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1111f4f4fed6SLiguo Zhang } 1112f4f4fed6SLiguo Zhang 1113ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1114ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1115ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1116ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 1117ce38815dSXudong Chen } 1118ce38815dSXudong Chen 1119ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 1120b2ed11e2SEddie Huang 1121173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1122b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 1123b2ed11e2SEddie Huang } else { 1124b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 1125b2ed11e2SEddie Huang if (left_num >= 1) 1126b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 1127b2ed11e2SEddie Huang } 1128bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 1129ce38815dSXudong Chen 1130ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 1131ce38815dSXudong Chen i2c->adap.timeout); 1132ce38815dSXudong Chen 1133ce38815dSXudong Chen /* Clear interrupt mask */ 1134bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1135cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 1136ce38815dSXudong Chen 1137ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 1138ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1139ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1140fc66b39fSJun Gao 1141fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1142ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 1143ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 1144ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 1145fc66b39fSJun Gao 1146fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 1147ce38815dSXudong Chen } else { 1148ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 1149ce38815dSXudong Chen DMA_TO_DEVICE); 1150ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 1151ce38815dSXudong Chen DMA_FROM_DEVICE); 1152fc66b39fSJun Gao 1153fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1154fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 1155ce38815dSXudong Chen } 1156ce38815dSXudong Chen 1157ce38815dSXudong Chen if (ret == 0) { 1158ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 1159cc28e578SKewei Xu i2c_dump_register(i2c); 1160ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1161ce38815dSXudong Chen return -ETIMEDOUT; 1162ce38815dSXudong Chen } 1163ce38815dSXudong Chen 1164ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 1165ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 1166ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1167ce38815dSXudong Chen return -ENXIO; 1168ce38815dSXudong Chen } 1169ce38815dSXudong Chen 1170ce38815dSXudong Chen return 0; 1171ce38815dSXudong Chen } 1172ce38815dSXudong Chen 1173ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 1174ce38815dSXudong Chen struct i2c_msg msgs[], int num) 1175ce38815dSXudong Chen { 1176ce38815dSXudong Chen int ret; 1177ce38815dSXudong Chen int left_num = num; 1178ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 1179ce38815dSXudong Chen 1180*8b4fc246SAngeloGioacchino Del Regno ret = clk_bulk_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1181ce38815dSXudong Chen if (ret) 1182ce38815dSXudong Chen return ret; 1183ce38815dSXudong Chen 1184173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 1185173b77e8SLiguo Zhang 1186173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 1187173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 1188173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 1189173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 1190173b77e8SLiguo Zhang i2c->auto_restart = 0; 1191173b77e8SLiguo Zhang } 1192173b77e8SLiguo Zhang } 1193173b77e8SLiguo Zhang 119463ce8e3dSQii Wang if (i2c->auto_restart && num >= 2 && 119563ce8e3dSQii Wang i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 11968378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 11978378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 11988378d01fSLiguo Zhang */ 11998378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 12008378d01fSLiguo Zhang else 12018378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 12028378d01fSLiguo Zhang 1203b2ed11e2SEddie Huang while (left_num--) { 1204ce38815dSXudong Chen if (!msgs->buf) { 1205ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 1206ce38815dSXudong Chen ret = -EINVAL; 1207ce38815dSXudong Chen goto err_exit; 1208ce38815dSXudong Chen } 1209ce38815dSXudong Chen 1210ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 1211ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 1212ce38815dSXudong Chen else 1213ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 1214ce38815dSXudong Chen 1215173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1216ce38815dSXudong Chen if (num > 1) { 1217ce38815dSXudong Chen /* combined two messages into one transaction */ 1218ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 1219ce38815dSXudong Chen left_num--; 1220ce38815dSXudong Chen } 1221b2ed11e2SEddie Huang } 1222ce38815dSXudong Chen 1223ce38815dSXudong Chen /* always use DMA mode. */ 1224b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 1225ce38815dSXudong Chen if (ret < 0) 1226ce38815dSXudong Chen goto err_exit; 1227ce38815dSXudong Chen 1228b2ed11e2SEddie Huang msgs++; 1229b2ed11e2SEddie Huang } 1230ce38815dSXudong Chen /* the return value is number of executed messages */ 1231ce38815dSXudong Chen ret = num; 1232ce38815dSXudong Chen 1233ce38815dSXudong Chen err_exit: 1234*8b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1235ce38815dSXudong Chen return ret; 1236ce38815dSXudong Chen } 1237ce38815dSXudong Chen 1238ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 1239ce38815dSXudong Chen { 1240ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 1241b2ed11e2SEddie Huang u16 restart_flag = 0; 124228c0a843SEddie Huang u16 intr_stat; 1243b2ed11e2SEddie Huang 1244173b77e8SLiguo Zhang if (i2c->auto_restart) 1245b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1246ce38815dSXudong Chen 1247bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 1248bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 1249ce38815dSXudong Chen 125028c0a843SEddie Huang /* 125128c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 125228c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 125328c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 125428c0a843SEddie Huang */ 125528c0a843SEddie Huang i2c->irq_stat |= intr_stat; 12568378d01fSLiguo Zhang 12578378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 12588378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 12598378d01fSLiguo Zhang i2c->irq_stat = 0; 1260bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 1261bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 12628378d01fSLiguo Zhang } else { 126328c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 1264ce38815dSXudong Chen complete(&i2c->msg_complete); 12658378d01fSLiguo Zhang } 1266ce38815dSXudong Chen 1267ce38815dSXudong Chen return IRQ_HANDLED; 1268ce38815dSXudong Chen } 1269ce38815dSXudong Chen 1270ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 1271ce38815dSXudong Chen { 127262931ac2SFabien Parent if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN)) 1273abf4923eSHsin-Yi Wang return I2C_FUNC_I2C | 1274abf4923eSHsin-Yi Wang (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1275abf4923eSHsin-Yi Wang else 1276ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1277ce38815dSXudong Chen } 1278ce38815dSXudong Chen 1279ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 1280ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 1281ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 1282ce38815dSXudong Chen }; 1283ce38815dSXudong Chen 1284f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 1285ce38815dSXudong Chen { 1286ce38815dSXudong Chen int ret; 1287ce38815dSXudong Chen 1288ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 1289ce38815dSXudong Chen if (ret < 0) 129090224e64SAndy Shevchenko i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ; 1291ce38815dSXudong Chen 1292f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 1293ce38815dSXudong Chen if (ret < 0) 1294ce38815dSXudong Chen return ret; 1295ce38815dSXudong Chen 1296f2326401SJun Gao if (i2c->clk_src_div == 0) 1297ce38815dSXudong Chen return -EINVAL; 1298ce38815dSXudong Chen 1299ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 1300ce38815dSXudong Chen i2c->use_push_pull = 1301ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 1302ce38815dSXudong Chen 1303a80f2494SQii Wang i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); 1304a80f2494SQii Wang 1305ce38815dSXudong Chen return 0; 1306ce38815dSXudong Chen } 1307ce38815dSXudong Chen 1308ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 1309ce38815dSXudong Chen { 1310ce38815dSXudong Chen int ret = 0; 1311ce38815dSXudong Chen struct mtk_i2c *i2c; 1312ce38815dSXudong Chen struct resource *res; 13130016a32fSAngeloGioacchino Del Regno int i, irq, speed_clk; 1314ce38815dSXudong Chen 1315ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 1316ce38815dSXudong Chen if (!i2c) 1317ce38815dSXudong Chen return -ENOMEM; 1318ce38815dSXudong Chen 1319ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1320ce38815dSXudong Chen i2c->base = devm_ioremap_resource(&pdev->dev, res); 1321ce38815dSXudong Chen if (IS_ERR(i2c->base)) 1322ce38815dSXudong Chen return PTR_ERR(i2c->base); 1323ce38815dSXudong Chen 1324ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1325ce38815dSXudong Chen i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); 1326ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 1327ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 1328ce38815dSXudong Chen 1329ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 133058fb7c64SSergey Shtylyov if (irq < 0) 1331ce38815dSXudong Chen return irq; 1332ce38815dSXudong Chen 1333ce38815dSXudong Chen init_completion(&i2c->msg_complete); 1334ce38815dSXudong Chen 13356e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 1336ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 1337ce38815dSXudong Chen i2c->dev = &pdev->dev; 1338ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 1339ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 1340ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 1341ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 1342ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 1343ce38815dSXudong Chen i2c->adap.retries = 1; 13449029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = devm_regulator_get_optional(&pdev->dev, "vbus"); 13459029b9b2SHsin-Yi Wang if (IS_ERR(i2c->adap.bus_regulator)) { 13469029b9b2SHsin-Yi Wang if (PTR_ERR(i2c->adap.bus_regulator) == -ENODEV) 13479029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = NULL; 13489029b9b2SHsin-Yi Wang else 13499029b9b2SHsin-Yi Wang return PTR_ERR(i2c->adap.bus_regulator); 13509029b9b2SHsin-Yi Wang } 1351ce38815dSXudong Chen 13525a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 13535a10e7d7SJun Gao if (ret) 13545a10e7d7SJun Gao return -EINVAL; 13555a10e7d7SJun Gao 1356ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 1357ce38815dSXudong Chen return -EINVAL; 1358ce38815dSXudong Chen 13590016a32fSAngeloGioacchino Del Regno /* Fill in clk-bulk IDs */ 13600016a32fSAngeloGioacchino Del Regno for (i = 0; i < I2C_MT65XX_CLK_MAX; i++) 13610016a32fSAngeloGioacchino Del Regno i2c->clocks[i].id = i2c_mt65xx_clk_ids[i]; 13620016a32fSAngeloGioacchino Del Regno 13630016a32fSAngeloGioacchino Del Regno /* Get clocks one by one, some may be optional */ 13640016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_MAIN].clk = devm_clk_get(&pdev->dev, "main"); 13650016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_MAIN].clk)) { 1366ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 13670016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_MAIN].clk); 1368ce38815dSXudong Chen } 1369ce38815dSXudong Chen 13700016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_DMA].clk = devm_clk_get(&pdev->dev, "dma"); 13710016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_DMA].clk)) { 1372ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 13730016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_DMA].clk); 1374ce38815dSXudong Chen } 1375ce38815dSXudong Chen 13760016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_ARB].clk = devm_clk_get_optional(&pdev->dev, "arb"); 13770016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_ARB].clk)) 13780016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_ARB].clk); 1379cad6dc5dSQii Wang 1380ce38815dSXudong Chen if (i2c->have_pmic) { 13810016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_PMIC].clk = devm_clk_get(&pdev->dev, "pmic"); 13820016a32fSAngeloGioacchino Del Regno if (IS_ERR(i2c->clocks[I2C_MT65XX_CLK_PMIC].clk)) { 1383ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 13840016a32fSAngeloGioacchino Del Regno return PTR_ERR(i2c->clocks[I2C_MT65XX_CLK_PMIC].clk); 1385ce38815dSXudong Chen } 13860016a32fSAngeloGioacchino Del Regno speed_clk = I2C_MT65XX_CLK_PMIC; 13870016a32fSAngeloGioacchino Del Regno } else { 13880016a32fSAngeloGioacchino Del Regno i2c->clocks[I2C_MT65XX_CLK_PMIC].clk = NULL; 13890016a32fSAngeloGioacchino Del Regno speed_clk = I2C_MT65XX_CLK_MAIN; 1390ce38815dSXudong Chen } 1391ce38815dSXudong Chen 1392ce38815dSXudong Chen strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1393ce38815dSXudong Chen 13940016a32fSAngeloGioacchino Del Regno ret = mtk_i2c_set_speed(i2c, clk_get_rate(i2c->clocks[speed_clk].clk)); 1395ce38815dSXudong Chen if (ret) { 1396ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 1397ce38815dSXudong Chen return -EINVAL; 1398ce38815dSXudong Chen } 1399ce38815dSXudong Chen 1400908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1401908d9843SQii Wang ret = dma_set_mask(&pdev->dev, 1402908d9843SQii Wang DMA_BIT_MASK(i2c->dev_comp->max_dma_support)); 1403f4f4fed6SLiguo Zhang if (ret) { 1404f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 1405f4f4fed6SLiguo Zhang return ret; 1406f4f4fed6SLiguo Zhang } 1407f4f4fed6SLiguo Zhang } 1408f4f4fed6SLiguo Zhang 14090016a32fSAngeloGioacchino Del Regno ret = clk_bulk_prepare_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1410ce38815dSXudong Chen if (ret) { 1411ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 1412ce38815dSXudong Chen return ret; 1413ce38815dSXudong Chen } 1414ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1415*8b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1416ce38815dSXudong Chen 1417ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 1418de96c394SQii Wang IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, 14197fb9dc81SQii Wang dev_name(&pdev->dev), i2c); 1420ce38815dSXudong Chen if (ret < 0) { 1421ce38815dSXudong Chen dev_err(&pdev->dev, 1422ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 1423ce38815dSXudong Chen return ret; 1424ce38815dSXudong Chen } 1425ce38815dSXudong Chen 1426ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 1427ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 1428ea734404SWolfram Sang if (ret) 1429ce38815dSXudong Chen return ret; 1430ce38815dSXudong Chen 1431ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 1432ce38815dSXudong Chen 1433ce38815dSXudong Chen return 0; 1434ce38815dSXudong Chen } 1435ce38815dSXudong Chen 1436ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev) 1437ce38815dSXudong Chen { 1438ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 1439ce38815dSXudong Chen 1440ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 1441ce38815dSXudong Chen 1442*8b4fc246SAngeloGioacchino Del Regno clk_bulk_unprepare(I2C_MT65XX_CLK_MAX, i2c->clocks); 1443*8b4fc246SAngeloGioacchino Del Regno 1444ce38815dSXudong Chen return 0; 1445ce38815dSXudong Chen } 1446ce38815dSXudong Chen 144709027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP 1448de96c394SQii Wang static int mtk_i2c_suspend_noirq(struct device *dev) 1449de96c394SQii Wang { 1450de96c394SQii Wang struct mtk_i2c *i2c = dev_get_drvdata(dev); 1451de96c394SQii Wang 1452de96c394SQii Wang i2c_mark_adapter_suspended(&i2c->adap); 1453*8b4fc246SAngeloGioacchino Del Regno clk_bulk_unprepare(I2C_MT65XX_CLK_MAX, i2c->clocks); 1454de96c394SQii Wang 1455de96c394SQii Wang return 0; 1456de96c394SQii Wang } 1457de96c394SQii Wang 1458de96c394SQii Wang static int mtk_i2c_resume_noirq(struct device *dev) 145909027e08SLiguo Zhang { 1460f6762cedSJun Gao int ret; 146109027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 146209027e08SLiguo Zhang 14630016a32fSAngeloGioacchino Del Regno ret = clk_bulk_prepare_enable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1464f6762cedSJun Gao if (ret) { 1465f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 1466f6762cedSJun Gao return ret; 1467f6762cedSJun Gao } 1468f6762cedSJun Gao 146909027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 147009027e08SLiguo Zhang 1471*8b4fc246SAngeloGioacchino Del Regno clk_bulk_disable(I2C_MT65XX_CLK_MAX, i2c->clocks); 1472f6762cedSJun Gao 1473de96c394SQii Wang i2c_mark_adapter_resumed(&i2c->adap); 1474de96c394SQii Wang 147509027e08SLiguo Zhang return 0; 147609027e08SLiguo Zhang } 147709027e08SLiguo Zhang #endif 147809027e08SLiguo Zhang 147909027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 1480de96c394SQii Wang SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq, 1481de96c394SQii Wang mtk_i2c_resume_noirq) 148209027e08SLiguo Zhang }; 148309027e08SLiguo Zhang 1484ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1485ce38815dSXudong Chen .probe = mtk_i2c_probe, 1486ce38815dSXudong Chen .remove = mtk_i2c_remove, 1487ce38815dSXudong Chen .driver = { 1488ce38815dSXudong Chen .name = I2C_DRV_NAME, 148909027e08SLiguo Zhang .pm = &mtk_i2c_pm, 1490ce38815dSXudong Chen .of_match_table = of_match_ptr(mtk_i2c_of_match), 1491ce38815dSXudong Chen }, 1492ce38815dSXudong Chen }; 1493ce38815dSXudong Chen 1494ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1495ce38815dSXudong Chen 1496ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1497ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1498ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1499