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 70*b5a796c6SKewei Xu #define MAX_CLOCK_DIV_8BITS 256 71*b5a796c6SKewei Xu #define MAX_CLOCK_DIV_5BITS 32 72ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV 8 73*b5a796c6SKewei Xu #define I2C_STANDARD_MODE_BUFFER (1000 / 3) 74*b5a796c6SKewei Xu #define I2C_FAST_MODE_BUFFER (300 / 3) 75*b5a796c6SKewei 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 89ce38815dSXudong Chen enum DMA_REGS_OFFSET { 90ce38815dSXudong Chen OFFSET_INT_FLAG = 0x0, 91ce38815dSXudong Chen OFFSET_INT_EN = 0x04, 92ce38815dSXudong Chen OFFSET_EN = 0x08, 93ea89ef1fSEddie Huang OFFSET_RST = 0x0c, 94ce38815dSXudong Chen OFFSET_CON = 0x18, 95ce38815dSXudong Chen OFFSET_TX_MEM_ADDR = 0x1c, 96ce38815dSXudong Chen OFFSET_RX_MEM_ADDR = 0x20, 97ce38815dSXudong Chen OFFSET_TX_LEN = 0x24, 98ce38815dSXudong Chen OFFSET_RX_LEN = 0x28, 99f4f4fed6SLiguo Zhang OFFSET_TX_4G_MODE = 0x54, 100f4f4fed6SLiguo Zhang OFFSET_RX_4G_MODE = 0x58, 101ce38815dSXudong Chen }; 102ce38815dSXudong Chen 103ce38815dSXudong Chen enum i2c_trans_st_rs { 104ce38815dSXudong Chen I2C_TRANS_STOP = 0, 105ce38815dSXudong Chen I2C_TRANS_REPEATED_START, 106ce38815dSXudong Chen }; 107ce38815dSXudong Chen 108ce38815dSXudong Chen enum mtk_trans_op { 109ce38815dSXudong Chen I2C_MASTER_WR = 1, 110ce38815dSXudong Chen I2C_MASTER_RD, 111ce38815dSXudong Chen I2C_MASTER_WRRD, 112ce38815dSXudong Chen }; 113ce38815dSXudong Chen 114ce38815dSXudong Chen enum I2C_REGS_OFFSET { 115bc6eaf17SQii Wang OFFSET_DATA_PORT, 116bc6eaf17SQii Wang OFFSET_SLAVE_ADDR, 117bc6eaf17SQii Wang OFFSET_INTR_MASK, 118bc6eaf17SQii Wang OFFSET_INTR_STAT, 119bc6eaf17SQii Wang OFFSET_CONTROL, 120bc6eaf17SQii Wang OFFSET_TRANSFER_LEN, 121bc6eaf17SQii Wang OFFSET_TRANSAC_LEN, 122bc6eaf17SQii Wang OFFSET_DELAY_LEN, 123bc6eaf17SQii Wang OFFSET_TIMING, 124bc6eaf17SQii Wang OFFSET_START, 125bc6eaf17SQii Wang OFFSET_EXT_CONF, 126bc6eaf17SQii Wang OFFSET_FIFO_STAT, 127bc6eaf17SQii Wang OFFSET_FIFO_THRESH, 128bc6eaf17SQii Wang OFFSET_FIFO_ADDR_CLR, 129bc6eaf17SQii Wang OFFSET_IO_CONFIG, 130bc6eaf17SQii Wang OFFSET_RSV_DEBUG, 131bc6eaf17SQii Wang OFFSET_HS, 132bc6eaf17SQii Wang OFFSET_SOFTRESET, 133bc6eaf17SQii Wang OFFSET_DCM_EN, 134cc28e578SKewei Xu OFFSET_MULTI_DMA, 135bc6eaf17SQii Wang OFFSET_PATH_DIR, 136bc6eaf17SQii Wang OFFSET_DEBUGSTAT, 137bc6eaf17SQii Wang OFFSET_DEBUGCTRL, 138bc6eaf17SQii Wang OFFSET_TRANSFER_LEN_AUX, 139bc6eaf17SQii Wang OFFSET_CLOCK_DIV, 14025708278SQii Wang OFFSET_LTIMING, 141be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO, 142be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO, 143be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT, 144be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING, 145be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING, 146be5ce0e9SQii Wang OFFSET_SDA_TIMING, 147bc6eaf17SQii Wang }; 148bc6eaf17SQii Wang 149bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = { 150bc6eaf17SQii Wang [OFFSET_DATA_PORT] = 0x0, 151bc6eaf17SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 152bc6eaf17SQii Wang [OFFSET_INTR_MASK] = 0x8, 153bc6eaf17SQii Wang [OFFSET_INTR_STAT] = 0xc, 154bc6eaf17SQii Wang [OFFSET_CONTROL] = 0x10, 155bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 156bc6eaf17SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 157bc6eaf17SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 158bc6eaf17SQii Wang [OFFSET_TIMING] = 0x20, 159bc6eaf17SQii Wang [OFFSET_START] = 0x24, 160bc6eaf17SQii Wang [OFFSET_EXT_CONF] = 0x28, 161bc6eaf17SQii Wang [OFFSET_FIFO_STAT] = 0x30, 162bc6eaf17SQii Wang [OFFSET_FIFO_THRESH] = 0x34, 163bc6eaf17SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 164bc6eaf17SQii Wang [OFFSET_IO_CONFIG] = 0x40, 165bc6eaf17SQii Wang [OFFSET_RSV_DEBUG] = 0x44, 166bc6eaf17SQii Wang [OFFSET_HS] = 0x48, 167bc6eaf17SQii Wang [OFFSET_SOFTRESET] = 0x50, 168bc6eaf17SQii Wang [OFFSET_DCM_EN] = 0x54, 169bc6eaf17SQii Wang [OFFSET_PATH_DIR] = 0x60, 170bc6eaf17SQii Wang [OFFSET_DEBUGSTAT] = 0x64, 171bc6eaf17SQii Wang [OFFSET_DEBUGCTRL] = 0x68, 172bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x6c, 173bc6eaf17SQii Wang [OFFSET_CLOCK_DIV] = 0x70, 174be5ce0e9SQii Wang [OFFSET_SCL_HIGH_LOW_RATIO] = 0x74, 175be5ce0e9SQii Wang [OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78, 176be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x7C, 177be5ce0e9SQii Wang [OFFSET_STA_STO_AC_TIMING] = 0x80, 178be5ce0e9SQii Wang [OFFSET_HS_STA_STO_AC_TIMING] = 0x84, 179be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x88, 180ce38815dSXudong Chen }; 181ce38815dSXudong Chen 18225708278SQii Wang static const u16 mt_i2c_regs_v2[] = { 18325708278SQii Wang [OFFSET_DATA_PORT] = 0x0, 18425708278SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 18525708278SQii Wang [OFFSET_INTR_MASK] = 0x8, 18625708278SQii Wang [OFFSET_INTR_STAT] = 0xc, 18725708278SQii Wang [OFFSET_CONTROL] = 0x10, 18825708278SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 18925708278SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 19025708278SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 19125708278SQii Wang [OFFSET_TIMING] = 0x20, 19225708278SQii Wang [OFFSET_START] = 0x24, 19325708278SQii Wang [OFFSET_EXT_CONF] = 0x28, 19425708278SQii Wang [OFFSET_LTIMING] = 0x2c, 19525708278SQii Wang [OFFSET_HS] = 0x30, 19625708278SQii Wang [OFFSET_IO_CONFIG] = 0x34, 19725708278SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 198be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x3c, 19925708278SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x44, 20025708278SQii Wang [OFFSET_CLOCK_DIV] = 0x48, 20125708278SQii Wang [OFFSET_SOFTRESET] = 0x50, 202cc28e578SKewei Xu [OFFSET_MULTI_DMA] = 0x8c, 203be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x90, 204b8228aeaSKewei Xu [OFFSET_DEBUGSTAT] = 0xe4, 20525708278SQii Wang [OFFSET_DEBUGCTRL] = 0xe8, 20625708278SQii Wang [OFFSET_FIFO_STAT] = 0xf4, 20725708278SQii Wang [OFFSET_FIFO_THRESH] = 0xf8, 20825708278SQii Wang [OFFSET_DCM_EN] = 0xf88, 20925708278SQii Wang }; 21025708278SQii Wang 211ce38815dSXudong Chen struct mtk_i2c_compatible { 212ce38815dSXudong Chen const struct i2c_adapter_quirks *quirks; 213bc6eaf17SQii Wang const u16 *regs; 214ce38815dSXudong Chen unsigned char pmic_i2c: 1; 215ce38815dSXudong Chen unsigned char dcm: 1; 216b2ed11e2SEddie Huang unsigned char auto_restart: 1; 217173b77e8SLiguo Zhang unsigned char aux_len_reg: 1; 2185a10e7d7SJun Gao unsigned char timing_adjust: 1; 219a15c91baSQii Wang unsigned char dma_sync: 1; 22025708278SQii Wang unsigned char ltiming_adjust: 1; 2218426fe70SQii Wang unsigned char apdma_sync: 1; 222908d9843SQii Wang unsigned char max_dma_support; 223ce38815dSXudong Chen }; 224ce38815dSXudong Chen 225be5ce0e9SQii Wang struct mtk_i2c_ac_timing { 226be5ce0e9SQii Wang u16 htiming; 227be5ce0e9SQii Wang u16 ltiming; 228be5ce0e9SQii Wang u16 hs; 229be5ce0e9SQii Wang u16 ext; 230be5ce0e9SQii Wang u16 inter_clk_div; 231be5ce0e9SQii Wang u16 scl_hl_ratio; 232be5ce0e9SQii Wang u16 hs_scl_hl_ratio; 233be5ce0e9SQii Wang u16 sta_stop; 234be5ce0e9SQii Wang u16 hs_sta_stop; 235be5ce0e9SQii Wang u16 sda_timing; 236be5ce0e9SQii Wang }; 237be5ce0e9SQii Wang 238ce38815dSXudong Chen struct mtk_i2c { 239ce38815dSXudong Chen struct i2c_adapter adap; /* i2c host adapter */ 240ce38815dSXudong Chen struct device *dev; 241ce38815dSXudong Chen struct completion msg_complete; 242a80f2494SQii Wang struct i2c_timings timing_info; 243ce38815dSXudong Chen 244ce38815dSXudong Chen /* set in i2c probe */ 245ce38815dSXudong Chen void __iomem *base; /* i2c base addr */ 246ce38815dSXudong Chen void __iomem *pdmabase; /* dma base address*/ 247ce38815dSXudong Chen struct clk *clk_main; /* main clock for i2c bus */ 248ce38815dSXudong Chen struct clk *clk_dma; /* DMA clock for i2c via DMA */ 249ce38815dSXudong Chen struct clk *clk_pmic; /* PMIC clock for i2c from PMIC */ 250cad6dc5dSQii Wang struct clk *clk_arb; /* Arbitrator clock for i2c */ 251ce38815dSXudong Chen bool have_pmic; /* can use i2c pins from PMIC */ 252ce38815dSXudong Chen bool use_push_pull; /* IO config push-pull mode */ 253ce38815dSXudong Chen 254ce38815dSXudong Chen u16 irq_stat; /* interrupt status */ 255f2326401SJun Gao unsigned int clk_src_div; 256ce38815dSXudong Chen unsigned int speed_hz; /* The speed in transfer */ 257ce38815dSXudong Chen enum mtk_trans_op op; 258ce38815dSXudong Chen u16 timing_reg; 259ce38815dSXudong Chen u16 high_speed_reg; 26025708278SQii Wang u16 ltiming_reg; 261173b77e8SLiguo Zhang unsigned char auto_restart; 2628378d01fSLiguo Zhang bool ignore_restart_irq; 263be5ce0e9SQii Wang struct mtk_i2c_ac_timing ac_timing; 264ce38815dSXudong Chen const struct mtk_i2c_compatible *dev_comp; 265ce38815dSXudong Chen }; 266ce38815dSXudong Chen 267be5ce0e9SQii Wang /** 268be5ce0e9SQii Wang * struct i2c_spec_values: 269b0102a89SMatthias Brugger * @min_low_ns: min LOW period of the SCL clock 270b0102a89SMatthias Brugger * @min_su_sta_ns: min set-up time for a repeated START condition 271b0102a89SMatthias Brugger * @max_hd_dat_ns: max data hold time 272b0102a89SMatthias Brugger * @min_su_dat_ns: min data set-up time 273be5ce0e9SQii Wang */ 274be5ce0e9SQii Wang struct i2c_spec_values { 275be5ce0e9SQii Wang unsigned int min_low_ns; 276be5ce0e9SQii Wang unsigned int min_su_sta_ns; 277be5ce0e9SQii Wang unsigned int max_hd_dat_ns; 278be5ce0e9SQii Wang unsigned int min_su_dat_ns; 279be5ce0e9SQii Wang }; 280be5ce0e9SQii Wang 281be5ce0e9SQii Wang static const struct i2c_spec_values standard_mode_spec = { 282be5ce0e9SQii Wang .min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 283be5ce0e9SQii Wang .min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 284be5ce0e9SQii Wang .max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER, 285be5ce0e9SQii Wang .min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER, 286be5ce0e9SQii Wang }; 287be5ce0e9SQii Wang 288be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_spec = { 289be5ce0e9SQii Wang .min_low_ns = 1300 + I2C_FAST_MODE_BUFFER, 290be5ce0e9SQii Wang .min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER, 291be5ce0e9SQii Wang .max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER, 292be5ce0e9SQii Wang .min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER, 293be5ce0e9SQii Wang }; 294be5ce0e9SQii Wang 295be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_plus_spec = { 296be5ce0e9SQii Wang .min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER, 297be5ce0e9SQii Wang .min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER, 298be5ce0e9SQii Wang .max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER, 299be5ce0e9SQii Wang .min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER, 300be5ce0e9SQii Wang }; 301be5ce0e9SQii Wang 302ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = { 303ce38815dSXudong Chen .flags = I2C_AQ_COMB_WRITE_THEN_READ, 304ce38815dSXudong Chen .max_num_msgs = 1, 305ce38815dSXudong Chen .max_write_len = 255, 306ce38815dSXudong Chen .max_read_len = 255, 307ce38815dSXudong Chen .max_comb_1st_msg_len = 255, 308ce38815dSXudong Chen .max_comb_2nd_msg_len = 31, 309ce38815dSXudong Chen }; 310ce38815dSXudong Chen 3111304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = { 3121304fe09SJun Gao .max_num_msgs = 255, 3131304fe09SJun Gao }; 3141304fe09SJun Gao 315abf4923eSHsin-Yi Wang static const struct i2c_adapter_quirks mt8183_i2c_quirks = { 316abf4923eSHsin-Yi Wang .flags = I2C_AQ_NO_ZERO_LEN, 317abf4923eSHsin-Yi Wang }; 318abf4923eSHsin-Yi Wang 3195a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = { 320bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3215a10e7d7SJun Gao .pmic_i2c = 0, 3225a10e7d7SJun Gao .dcm = 1, 3235a10e7d7SJun Gao .auto_restart = 1, 3245a10e7d7SJun Gao .aux_len_reg = 1, 3255a10e7d7SJun Gao .timing_adjust = 1, 326a15c91baSQii Wang .dma_sync = 0, 32725708278SQii Wang .ltiming_adjust = 0, 3288426fe70SQii Wang .apdma_sync = 0, 329908d9843SQii Wang .max_dma_support = 33, 3305a10e7d7SJun Gao }; 3315a10e7d7SJun Gao 332ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = { 333ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 334bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 335ce38815dSXudong Chen .pmic_i2c = 0, 336ce38815dSXudong Chen .dcm = 1, 337b2ed11e2SEddie Huang .auto_restart = 0, 338173b77e8SLiguo Zhang .aux_len_reg = 0, 3395a10e7d7SJun Gao .timing_adjust = 0, 340a15c91baSQii Wang .dma_sync = 0, 34125708278SQii Wang .ltiming_adjust = 0, 3428426fe70SQii Wang .apdma_sync = 0, 343908d9843SQii Wang .max_dma_support = 32, 344ce38815dSXudong Chen }; 345ce38815dSXudong Chen 346ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = { 347ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 348bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 349ce38815dSXudong Chen .pmic_i2c = 1, 350ce38815dSXudong Chen .dcm = 0, 351b2ed11e2SEddie Huang .auto_restart = 0, 352173b77e8SLiguo Zhang .aux_len_reg = 0, 3535a10e7d7SJun Gao .timing_adjust = 0, 354a15c91baSQii Wang .dma_sync = 0, 35525708278SQii Wang .ltiming_adjust = 0, 3568426fe70SQii Wang .apdma_sync = 0, 357908d9843SQii Wang .max_dma_support = 32, 358b2ed11e2SEddie Huang }; 359b2ed11e2SEddie Huang 3601304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = { 3611304fe09SJun Gao .quirks = &mt7622_i2c_quirks, 362bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3631304fe09SJun Gao .pmic_i2c = 0, 3641304fe09SJun Gao .dcm = 1, 3651304fe09SJun Gao .auto_restart = 1, 3661304fe09SJun Gao .aux_len_reg = 1, 3675a10e7d7SJun Gao .timing_adjust = 0, 368a15c91baSQii Wang .dma_sync = 0, 36925708278SQii Wang .ltiming_adjust = 0, 3708426fe70SQii Wang .apdma_sync = 0, 371908d9843SQii Wang .max_dma_support = 32, 3721304fe09SJun Gao }; 3731304fe09SJun Gao 374b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = { 375bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 376b2ed11e2SEddie Huang .pmic_i2c = 0, 377b2ed11e2SEddie Huang .dcm = 1, 378b2ed11e2SEddie Huang .auto_restart = 1, 379173b77e8SLiguo Zhang .aux_len_reg = 1, 3805a10e7d7SJun Gao .timing_adjust = 0, 381a15c91baSQii Wang .dma_sync = 0, 38225708278SQii Wang .ltiming_adjust = 0, 3838426fe70SQii Wang .apdma_sync = 0, 384908d9843SQii Wang .max_dma_support = 33, 38525708278SQii Wang }; 38625708278SQii Wang 38725708278SQii Wang static const struct mtk_i2c_compatible mt8183_compat = { 388abf4923eSHsin-Yi Wang .quirks = &mt8183_i2c_quirks, 38925708278SQii Wang .regs = mt_i2c_regs_v2, 39025708278SQii Wang .pmic_i2c = 0, 39125708278SQii Wang .dcm = 0, 39225708278SQii Wang .auto_restart = 1, 39325708278SQii Wang .aux_len_reg = 1, 39425708278SQii Wang .timing_adjust = 1, 39525708278SQii Wang .dma_sync = 1, 39625708278SQii Wang .ltiming_adjust = 1, 3978426fe70SQii Wang .apdma_sync = 0, 398908d9843SQii Wang .max_dma_support = 33, 399ce38815dSXudong Chen }; 400ce38815dSXudong Chen 40193470531SKewei Xu static const struct mtk_i2c_compatible mt8186_compat = { 40293470531SKewei Xu .regs = mt_i2c_regs_v2, 40393470531SKewei Xu .pmic_i2c = 0, 40493470531SKewei Xu .dcm = 0, 40593470531SKewei Xu .auto_restart = 1, 40693470531SKewei Xu .aux_len_reg = 1, 40793470531SKewei Xu .timing_adjust = 1, 40893470531SKewei Xu .dma_sync = 0, 40993470531SKewei Xu .ltiming_adjust = 1, 41093470531SKewei Xu .apdma_sync = 0, 41193470531SKewei Xu .max_dma_support = 36, 41293470531SKewei Xu }; 41393470531SKewei Xu 414789e67baSQii Wang static const struct mtk_i2c_compatible mt8192_compat = { 415789e67baSQii Wang .quirks = &mt8183_i2c_quirks, 416789e67baSQii Wang .regs = mt_i2c_regs_v2, 417789e67baSQii Wang .pmic_i2c = 0, 418789e67baSQii Wang .dcm = 0, 419789e67baSQii Wang .auto_restart = 1, 420789e67baSQii Wang .aux_len_reg = 1, 421789e67baSQii Wang .timing_adjust = 1, 422789e67baSQii Wang .dma_sync = 1, 423789e67baSQii Wang .ltiming_adjust = 1, 424789e67baSQii Wang .apdma_sync = 1, 425789e67baSQii Wang .max_dma_support = 36, 426789e67baSQii Wang }; 427789e67baSQii Wang 428ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = { 4295a10e7d7SJun Gao { .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat }, 430ce38815dSXudong Chen { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat }, 431ce38815dSXudong Chen { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat }, 4321304fe09SJun Gao { .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat }, 433b2ed11e2SEddie Huang { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat }, 43425708278SQii Wang { .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat }, 43593470531SKewei Xu { .compatible = "mediatek,mt8186-i2c", .data = &mt8186_compat }, 436789e67baSQii Wang { .compatible = "mediatek,mt8192-i2c", .data = &mt8192_compat }, 437ce38815dSXudong Chen {} 438ce38815dSXudong Chen }; 439ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); 440ce38815dSXudong Chen 441bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg) 442bc6eaf17SQii Wang { 443bc6eaf17SQii Wang return readw(i2c->base + i2c->dev_comp->regs[reg]); 444bc6eaf17SQii Wang } 445bc6eaf17SQii Wang 446bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val, 447bc6eaf17SQii Wang enum I2C_REGS_OFFSET reg) 448bc6eaf17SQii Wang { 449bc6eaf17SQii Wang writew(val, i2c->base + i2c->dev_comp->regs[reg]); 450bc6eaf17SQii Wang } 451bc6eaf17SQii Wang 452ce38815dSXudong Chen static int mtk_i2c_clock_enable(struct mtk_i2c *i2c) 453ce38815dSXudong Chen { 454ce38815dSXudong Chen int ret; 455ce38815dSXudong Chen 456ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_dma); 457ce38815dSXudong Chen if (ret) 458ce38815dSXudong Chen return ret; 459ce38815dSXudong Chen 460ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_main); 461ce38815dSXudong Chen if (ret) 462ce38815dSXudong Chen goto err_main; 463ce38815dSXudong Chen 464ce38815dSXudong Chen if (i2c->have_pmic) { 465ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_pmic); 466ce38815dSXudong Chen if (ret) 467ce38815dSXudong Chen goto err_pmic; 468ce38815dSXudong Chen } 469cad6dc5dSQii Wang 470cad6dc5dSQii Wang if (i2c->clk_arb) { 471cad6dc5dSQii Wang ret = clk_prepare_enable(i2c->clk_arb); 472cad6dc5dSQii Wang if (ret) 473cad6dc5dSQii Wang goto err_arb; 474cad6dc5dSQii Wang } 475cad6dc5dSQii Wang 476ce38815dSXudong Chen return 0; 477ce38815dSXudong Chen 478cad6dc5dSQii Wang err_arb: 479cad6dc5dSQii Wang if (i2c->have_pmic) 480cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_pmic); 481ce38815dSXudong Chen err_pmic: 482ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 483ce38815dSXudong Chen err_main: 484ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 485ce38815dSXudong Chen 486ce38815dSXudong Chen return ret; 487ce38815dSXudong Chen } 488ce38815dSXudong Chen 489ce38815dSXudong Chen static void mtk_i2c_clock_disable(struct mtk_i2c *i2c) 490ce38815dSXudong Chen { 491cad6dc5dSQii Wang if (i2c->clk_arb) 492cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_arb); 493cad6dc5dSQii Wang 494ce38815dSXudong Chen if (i2c->have_pmic) 495ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_pmic); 496ce38815dSXudong Chen 497ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 498ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 499ce38815dSXudong Chen } 500ce38815dSXudong Chen 501ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c) 502ce38815dSXudong Chen { 503ce38815dSXudong Chen u16 control_reg; 504fed1bd51SQii Wang u16 intr_stat_reg; 5053bce7703SKewei Xu u16 ext_conf_val; 506fed1bd51SQii Wang 507fed1bd51SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_START); 508fed1bd51SQii Wang intr_stat_reg = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 509fed1bd51SQii Wang mtk_i2c_writew(i2c, intr_stat_reg, OFFSET_INTR_STAT); 510ce38815dSXudong Chen 5113186b880SQii Wang if (i2c->dev_comp->apdma_sync) { 51205f6f727SQii Wang writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST); 51305f6f727SQii Wang udelay(10); 51405f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 51505f6f727SQii Wang udelay(10); 51605f6f727SQii Wang writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST, 51705f6f727SQii Wang i2c->pdmabase + OFFSET_RST); 51805f6f727SQii Wang mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST, 51905f6f727SQii Wang OFFSET_SOFTRESET); 52005f6f727SQii Wang udelay(10); 52105f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 52205f6f727SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 52305f6f727SQii Wang } else { 524aafced67SQii Wang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 525aafced67SQii Wang udelay(50); 526aafced67SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 527bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 52805f6f727SQii Wang } 529ce38815dSXudong Chen 530ce38815dSXudong Chen /* Set ioconfig */ 531ce38815dSXudong Chen if (i2c->use_push_pull) 532bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 533ce38815dSXudong Chen else 534bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 535ce38815dSXudong Chen 536ce38815dSXudong Chen if (i2c->dev_comp->dcm) 537bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 538ce38815dSXudong Chen 539bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 540bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 54125708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 54225708278SQii Wang mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING); 543ce38815dSXudong Chen 5443bce7703SKewei Xu if (i2c->speed_hz <= I2C_MAX_STANDARD_MODE_FREQ) 5453bce7703SKewei Xu ext_conf_val = I2C_ST_START_CON; 5463bce7703SKewei Xu else 5473bce7703SKewei Xu ext_conf_val = I2C_FS_START_CON; 5483bce7703SKewei Xu 549be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) { 5503bce7703SKewei Xu ext_conf_val = i2c->ac_timing.ext; 551be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, 552be5ce0e9SQii Wang OFFSET_CLOCK_DIV); 553be5ce0e9SQii Wang mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE, 554be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT); 555be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing, 556be5ce0e9SQii Wang OFFSET_SDA_TIMING); 557be5ce0e9SQii Wang 558be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 559be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.htiming, 560be5ce0e9SQii Wang OFFSET_TIMING); 561be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS); 562be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ltiming, 563be5ce0e9SQii Wang OFFSET_LTIMING); 564be5ce0e9SQii Wang } else { 565be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio, 566be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO); 567be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio, 568be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO); 569be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop, 570be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING); 571be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop, 572be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING); 573be5ce0e9SQii Wang } 574be5ce0e9SQii Wang } 5753bce7703SKewei Xu mtk_i2c_writew(i2c, ext_conf_val, OFFSET_EXT_CONF); 576be5ce0e9SQii Wang 577ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 578ce38815dSXudong Chen if (i2c->have_pmic) 579bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 580ce38815dSXudong Chen 581ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 582ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 583a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 584a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 585a15c91baSQii Wang 586bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 587bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 588ce38815dSXudong Chen } 589ce38815dSXudong Chen 590be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed) 591be5ce0e9SQii Wang { 592be5ce0e9SQii Wang if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 593be5ce0e9SQii Wang return &standard_mode_spec; 594be5ce0e9SQii Wang else if (speed <= I2C_MAX_FAST_MODE_FREQ) 595be5ce0e9SQii Wang return &fast_mode_spec; 596be5ce0e9SQii Wang else 597be5ce0e9SQii Wang return &fast_mode_plus_spec; 598be5ce0e9SQii Wang } 599be5ce0e9SQii Wang 600be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed) 601be5ce0e9SQii Wang { 60263ce8e3dSQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) 603be5ce0e9SQii Wang return MAX_HS_STEP_CNT_DIV; 604be5ce0e9SQii Wang else 605be5ce0e9SQii Wang return MAX_STEP_CNT_DIV; 606be5ce0e9SQii Wang } 607be5ce0e9SQii Wang 608*b5a796c6SKewei Xu static int mtk_i2c_get_clk_div_restri(struct mtk_i2c *i2c, 609*b5a796c6SKewei Xu unsigned int sample_cnt) 610*b5a796c6SKewei Xu { 611*b5a796c6SKewei Xu int clk_div_restri = 0; 612*b5a796c6SKewei Xu 613*b5a796c6SKewei Xu if (i2c->dev_comp->ltiming_adjust == 0) 614*b5a796c6SKewei Xu return 0; 615*b5a796c6SKewei Xu 616*b5a796c6SKewei Xu if (sample_cnt == 1) { 617*b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 618*b5a796c6SKewei Xu clk_div_restri = 0; 619*b5a796c6SKewei Xu else 620*b5a796c6SKewei Xu clk_div_restri = 1; 621*b5a796c6SKewei Xu } else { 622*b5a796c6SKewei Xu if (i2c->ac_timing.inter_clk_div == 0) 623*b5a796c6SKewei Xu clk_div_restri = -1; 624*b5a796c6SKewei Xu else if (i2c->ac_timing.inter_clk_div == 1) 625*b5a796c6SKewei Xu clk_div_restri = 0; 626*b5a796c6SKewei Xu else 627*b5a796c6SKewei Xu clk_div_restri = 1; 628*b5a796c6SKewei Xu } 629*b5a796c6SKewei Xu 630*b5a796c6SKewei Xu return clk_div_restri; 631*b5a796c6SKewei Xu } 632*b5a796c6SKewei Xu 633be5ce0e9SQii Wang /* 634be5ce0e9SQii Wang * Check and Calculate i2c ac-timing 635be5ce0e9SQii Wang * 636be5ce0e9SQii Wang * Hardware design: 637be5ce0e9SQii Wang * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src 638be5ce0e9SQii Wang * xxx_cnt_div = spec->min_xxx_ns / sample_ns 639be5ce0e9SQii Wang * 640be5ce0e9SQii Wang * Sample_ns is rounded down for xxx_cnt_div would be greater 641be5ce0e9SQii Wang * than the smallest spec. 642be5ce0e9SQii Wang * The sda_timing is chosen as the middle value between 643be5ce0e9SQii Wang * the largest and smallest. 644be5ce0e9SQii Wang */ 645be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, 646be5ce0e9SQii Wang unsigned int clk_src, 647be5ce0e9SQii Wang unsigned int check_speed, 648be5ce0e9SQii Wang unsigned int step_cnt, 649be5ce0e9SQii Wang unsigned int sample_cnt) 650be5ce0e9SQii Wang { 651be5ce0e9SQii Wang const struct i2c_spec_values *spec; 652be5ce0e9SQii Wang unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt; 653be5ce0e9SQii Wang unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f; 654be5ce0e9SQii Wang unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), 655be5ce0e9SQii Wang clk_src); 656be5ce0e9SQii Wang 657be5ce0e9SQii Wang if (!i2c->dev_comp->timing_adjust) 658be5ce0e9SQii Wang return 0; 659be5ce0e9SQii Wang 660be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 661be5ce0e9SQii Wang max_sta_cnt = 0x100; 662be5ce0e9SQii Wang 663be5ce0e9SQii Wang spec = mtk_i2c_get_spec(check_speed); 664be5ce0e9SQii Wang 665be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 666be5ce0e9SQii Wang clk_ns = 1000000000 / clk_src; 667be5ce0e9SQii Wang else 668be5ce0e9SQii Wang clk_ns = sample_ns / 2; 669be5ce0e9SQii Wang 670a80f2494SQii Wang su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns + 671a80f2494SQii Wang i2c->timing_info.scl_int_delay_ns, clk_ns); 672be5ce0e9SQii Wang if (su_sta_cnt > max_sta_cnt) 673be5ce0e9SQii Wang return -1; 674be5ce0e9SQii Wang 675be5ce0e9SQii Wang low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns); 676be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(check_speed); 677be5ce0e9SQii Wang if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) { 678be5ce0e9SQii Wang if (low_cnt > step_cnt) { 679be5ce0e9SQii Wang high_cnt = 2 * step_cnt - low_cnt; 680be5ce0e9SQii Wang } else { 681be5ce0e9SQii Wang high_cnt = step_cnt; 682be5ce0e9SQii Wang low_cnt = step_cnt; 683be5ce0e9SQii Wang } 684be5ce0e9SQii Wang } else { 685be5ce0e9SQii Wang return -2; 686be5ce0e9SQii Wang } 687be5ce0e9SQii Wang 688be5ce0e9SQii Wang sda_max = spec->max_hd_dat_ns / sample_ns; 689be5ce0e9SQii Wang if (sda_max > low_cnt) 690be5ce0e9SQii Wang sda_max = 0; 691be5ce0e9SQii Wang 692be5ce0e9SQii Wang sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns); 693be5ce0e9SQii Wang if (sda_min < low_cnt) 694be5ce0e9SQii Wang sda_min = 0; 695be5ce0e9SQii Wang 696be5ce0e9SQii Wang if (sda_min > sda_max) 697be5ce0e9SQii Wang return -3; 698be5ce0e9SQii Wang 69963ce8e3dSQii Wang if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 700be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 701be5ce0e9SQii Wang i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE | 702be5ce0e9SQii Wang (sample_cnt << 12) | (high_cnt << 8); 703be5ce0e9SQii Wang i2c->ac_timing.ltiming &= ~GENMASK(15, 9); 704be5ce0e9SQii Wang i2c->ac_timing.ltiming |= (sample_cnt << 12) | 705be5ce0e9SQii Wang (low_cnt << 9); 706be5ce0e9SQii Wang i2c->ac_timing.ext &= ~GENMASK(7, 1); 707be5ce0e9SQii Wang i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0); 708be5ce0e9SQii Wang } else { 709be5ce0e9SQii Wang i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) | 710be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 711be5ce0e9SQii Wang i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) | 712be5ce0e9SQii Wang su_sta_cnt; 713be5ce0e9SQii Wang } 714be5ce0e9SQii Wang i2c->ac_timing.sda_timing &= ~GENMASK(11, 6); 715be5ce0e9SQii Wang i2c->ac_timing.sda_timing |= (1 << 12) | 716be5ce0e9SQii Wang ((sda_max + sda_min) / 2) << 6; 717be5ce0e9SQii Wang } else { 718be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 719be5ce0e9SQii Wang i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt); 720be5ce0e9SQii Wang i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt); 721be5ce0e9SQii Wang i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0); 722be5ce0e9SQii Wang } else { 723be5ce0e9SQii Wang i2c->ac_timing.scl_hl_ratio = (1 << 12) | 724be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 725be5ce0e9SQii Wang i2c->ac_timing.sta_stop = (su_sta_cnt << 8) | 726be5ce0e9SQii Wang su_sta_cnt; 727be5ce0e9SQii Wang } 728be5ce0e9SQii Wang 729be5ce0e9SQii Wang i2c->ac_timing.sda_timing = (1 << 12) | 730be5ce0e9SQii Wang (sda_max + sda_min) / 2; 731be5ce0e9SQii Wang } 732be5ce0e9SQii Wang 733be5ce0e9SQii Wang return 0; 734be5ce0e9SQii Wang } 735be5ce0e9SQii Wang 736ce38815dSXudong Chen /* 737ce38815dSXudong Chen * Calculate i2c port speed 738ce38815dSXudong Chen * 739ce38815dSXudong Chen * Hardware design: 740ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 741ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 742ce38815dSXudong Chen * 743ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 744ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 745ce38815dSXudong Chen * sample_cnt and step_cn 746ce38815dSXudong Chen */ 747f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 748f2326401SJun Gao unsigned int target_speed, 749f2326401SJun Gao unsigned int *timing_step_cnt, 750f2326401SJun Gao unsigned int *timing_sample_cnt) 751ce38815dSXudong Chen { 752ce38815dSXudong Chen unsigned int step_cnt; 753ce38815dSXudong Chen unsigned int sample_cnt; 754ce38815dSXudong Chen unsigned int max_step_cnt; 755ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 756ce38815dSXudong Chen unsigned int base_step_cnt; 757ce38815dSXudong Chen unsigned int opt_div; 758ce38815dSXudong Chen unsigned int best_mul; 759ce38815dSXudong Chen unsigned int cnt_mul; 760be5ce0e9SQii Wang int ret = -EINVAL; 761*b5a796c6SKewei Xu int clk_div_restri = 0; 762ce38815dSXudong Chen 763ff6f3affSQii Wang if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) 764ff6f3affSQii Wang target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; 765ce38815dSXudong Chen 766be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(target_speed); 767ce38815dSXudong Chen base_step_cnt = max_step_cnt; 768ce38815dSXudong Chen /* Find the best combination */ 769ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 770ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 771ce38815dSXudong Chen 772ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 773ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 774ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 775ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 776ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 777ce38815dSXudong Chen */ 778ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 779*b5a796c6SKewei Xu clk_div_restri = mtk_i2c_get_clk_div_restri(i2c, sample_cnt); 780*b5a796c6SKewei Xu step_cnt = DIV_ROUND_UP(opt_div + clk_div_restri, sample_cnt); 781ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 782ce38815dSXudong Chen if (step_cnt > max_step_cnt) 783ce38815dSXudong Chen continue; 784ce38815dSXudong Chen 785ce38815dSXudong Chen if (cnt_mul < best_mul) { 786be5ce0e9SQii Wang ret = mtk_i2c_check_ac_timing(i2c, clk_src, 787be5ce0e9SQii Wang target_speed, step_cnt - 1, sample_cnt - 1); 788be5ce0e9SQii Wang if (ret) 789be5ce0e9SQii Wang continue; 790be5ce0e9SQii Wang 791ce38815dSXudong Chen best_mul = cnt_mul; 792ce38815dSXudong Chen base_sample_cnt = sample_cnt; 793ce38815dSXudong Chen base_step_cnt = step_cnt; 794*b5a796c6SKewei Xu if (best_mul == (opt_div + clk_div_restri)) 795ce38815dSXudong Chen break; 796ce38815dSXudong Chen } 797ce38815dSXudong Chen } 798ce38815dSXudong Chen 799be5ce0e9SQii Wang if (ret) 800be5ce0e9SQii Wang return -EINVAL; 801be5ce0e9SQii Wang 802ce38815dSXudong Chen sample_cnt = base_sample_cnt; 803ce38815dSXudong Chen step_cnt = base_step_cnt; 804ce38815dSXudong Chen 805*b5a796c6SKewei Xu if ((clk_src / (2 * (sample_cnt * step_cnt - clk_div_restri))) > 806*b5a796c6SKewei Xu target_speed) { 807ce38815dSXudong Chen /* In this case, hardware can't support such 808ce38815dSXudong Chen * low i2c_bus_freq 809ce38815dSXudong Chen */ 810ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 811ce38815dSXudong Chen return -EINVAL; 812ce38815dSXudong Chen } 813ce38815dSXudong Chen 814f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 815f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 816f2326401SJun Gao 817f2326401SJun Gao return 0; 818f2326401SJun Gao } 819f2326401SJun Gao 820f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 821f2326401SJun Gao { 822f2326401SJun Gao unsigned int clk_src; 823f2326401SJun Gao unsigned int step_cnt; 824f2326401SJun Gao unsigned int sample_cnt; 82525708278SQii Wang unsigned int l_step_cnt; 82625708278SQii Wang unsigned int l_sample_cnt; 827f2326401SJun Gao unsigned int target_speed; 828be5ce0e9SQii Wang unsigned int clk_div; 829be5ce0e9SQii Wang unsigned int max_clk_div; 830f2326401SJun Gao int ret; 831f2326401SJun Gao 832f2326401SJun Gao target_speed = i2c->speed_hz; 833be5ce0e9SQii Wang parent_clk /= i2c->clk_src_div; 834be5ce0e9SQii Wang 835*b5a796c6SKewei Xu if (i2c->dev_comp->timing_adjust && i2c->dev_comp->ltiming_adjust) 836*b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_5BITS; 837*b5a796c6SKewei Xu else if (i2c->dev_comp->timing_adjust) 838*b5a796c6SKewei Xu max_clk_div = MAX_CLOCK_DIV_8BITS; 839be5ce0e9SQii Wang else 840be5ce0e9SQii Wang max_clk_div = 1; 841be5ce0e9SQii Wang 842be5ce0e9SQii Wang for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { 843be5ce0e9SQii Wang clk_src = parent_clk / clk_div; 844*b5a796c6SKewei Xu i2c->ac_timing.inter_clk_div = clk_div - 1; 845ce38815dSXudong Chen 846b44658e7SQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 847f2326401SJun Gao /* Set master code speed register */ 848be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 849be5ce0e9SQii Wang I2C_MAX_FAST_MODE_FREQ, 850be5ce0e9SQii Wang &l_step_cnt, 851be5ce0e9SQii Wang &l_sample_cnt); 852f2326401SJun Gao if (ret < 0) 853be5ce0e9SQii Wang continue; 854f2326401SJun Gao 85525708278SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 856f2326401SJun Gao 857ce38815dSXudong Chen /* Set the high speed mode register */ 858be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 859be5ce0e9SQii Wang target_speed, &step_cnt, 860be5ce0e9SQii Wang &sample_cnt); 861f2326401SJun Gao if (ret < 0) 862be5ce0e9SQii Wang continue; 863f2326401SJun Gao 864ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 865ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 86625708278SQii Wang 86725708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 868be5ce0e9SQii Wang i2c->ltiming_reg = 869be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt | 87025708278SQii Wang (sample_cnt << 12) | (step_cnt << 9); 871ce38815dSXudong Chen } else { 872be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 873be5ce0e9SQii Wang target_speed, &l_step_cnt, 874be5ce0e9SQii Wang &l_sample_cnt); 875f2326401SJun Gao if (ret < 0) 876be5ce0e9SQii Wang continue; 877f2326401SJun Gao 878be5ce0e9SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 879f2326401SJun Gao 880ce38815dSXudong Chen /* Disable the high speed transaction */ 881ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 88225708278SQii Wang 88325708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 884be5ce0e9SQii Wang i2c->ltiming_reg = 885be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt; 886ce38815dSXudong Chen } 887ce38815dSXudong Chen 888be5ce0e9SQii Wang break; 889be5ce0e9SQii Wang } 890be5ce0e9SQii Wang 891be5ce0e9SQii Wang 892ce38815dSXudong Chen return 0; 893ce38815dSXudong Chen } 894ce38815dSXudong Chen 895cc28e578SKewei Xu static void i2c_dump_register(struct mtk_i2c *i2c) 896cc28e578SKewei Xu { 897cc28e578SKewei Xu dev_dbg(i2c->dev, "SLAVE_ADDR: 0x%x, INTR_MASK: 0x%x\n", 898cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_SLAVE_ADDR), 899cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_MASK)); 900cc28e578SKewei Xu dev_dbg(i2c->dev, "INTR_STAT: 0x%x, CONTROL: 0x%x\n", 901cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_INTR_STAT), 902cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CONTROL)); 903cc28e578SKewei Xu dev_dbg(i2c->dev, "TRANSFER_LEN: 0x%x, TRANSAC_LEN: 0x%x\n", 904cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN), 905cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSAC_LEN)); 906cc28e578SKewei Xu dev_dbg(i2c->dev, "DELAY_LEN: 0x%x, HTIMING: 0x%x\n", 907cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DELAY_LEN), 908cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TIMING)); 909cc28e578SKewei Xu dev_dbg(i2c->dev, "START: 0x%x, EXT_CONF: 0x%x\n", 910cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_START), 911cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_EXT_CONF)); 912cc28e578SKewei Xu dev_dbg(i2c->dev, "HS: 0x%x, IO_CONFIG: 0x%x\n", 913cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_HS), 914cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_IO_CONFIG)); 915cc28e578SKewei Xu dev_dbg(i2c->dev, "DCM_EN: 0x%x, TRANSFER_LEN_AUX: 0x%x\n", 916cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DCM_EN), 917cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_TRANSFER_LEN_AUX)); 918cc28e578SKewei Xu dev_dbg(i2c->dev, "CLOCK_DIV: 0x%x, FIFO_STAT: 0x%x\n", 919cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_CLOCK_DIV), 920cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_FIFO_STAT)); 921cc28e578SKewei Xu dev_dbg(i2c->dev, "DEBUGCTRL : 0x%x, DEBUGSTAT: 0x%x\n", 922cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGCTRL), 923cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_DEBUGSTAT)); 924cc28e578SKewei Xu if (i2c->dev_comp->regs == mt_i2c_regs_v2) { 925cc28e578SKewei Xu dev_dbg(i2c->dev, "LTIMING: 0x%x, MULTI_DMA: 0x%x\n", 926cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_LTIMING), 927cc28e578SKewei Xu mtk_i2c_readw(i2c, OFFSET_MULTI_DMA)); 928cc28e578SKewei Xu } 929cc28e578SKewei Xu dev_dbg(i2c->dev, "\nDMA_INT_FLAG: 0x%x, DMA_INT_EN: 0x%x\n", 930cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_FLAG), 931cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_INT_EN)); 932cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_EN: 0x%x, DMA_CON: 0x%x\n", 933cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_EN), 934cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_CON)); 935cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_MEM_ADDR: 0x%x, DMA_RX_MEM_ADDR: 0x%x\n", 936cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_MEM_ADDR), 937cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_MEM_ADDR)); 938cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_LEN: 0x%x, DMA_RX_LEN: 0x%x\n", 939cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_LEN), 940cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_LEN)); 941cc28e578SKewei Xu dev_dbg(i2c->dev, "DMA_TX_4G_MODE: 0x%x, DMA_RX_4G_MODE: 0x%x", 942cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_TX_4G_MODE), 943cc28e578SKewei Xu readl(i2c->pdmabase + OFFSET_RX_4G_MODE)); 944cc28e578SKewei Xu } 945cc28e578SKewei Xu 946b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 947b2ed11e2SEddie Huang int num, int left_num) 948ce38815dSXudong Chen { 949ce38815dSXudong Chen u16 addr_reg; 950b2ed11e2SEddie Huang u16 start_reg; 951ce38815dSXudong Chen u16 control_reg; 952b2ed11e2SEddie Huang u16 restart_flag = 0; 9538426fe70SQii Wang u16 dma_sync = 0; 954f4f4fed6SLiguo Zhang u32 reg_4g_mode; 955e3e4949eSKewei Xu u32 reg_dma_reset; 956fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 957fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 958ce38815dSXudong Chen dma_addr_t rpaddr = 0; 959ce38815dSXudong Chen dma_addr_t wpaddr = 0; 960ce38815dSXudong Chen int ret; 961ce38815dSXudong Chen 962ce38815dSXudong Chen i2c->irq_stat = 0; 963ce38815dSXudong Chen 964173b77e8SLiguo Zhang if (i2c->auto_restart) 965b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 966b2ed11e2SEddie Huang 967ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 968ce38815dSXudong Chen 969e3e4949eSKewei Xu if (i2c->dev_comp->apdma_sync && 970e3e4949eSKewei Xu i2c->op != I2C_MASTER_WRRD && num > 1) { 971e3e4949eSKewei Xu mtk_i2c_writew(i2c, 0x00, OFFSET_DEBUGCTRL); 972e3e4949eSKewei Xu writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_WARM_RST, 973e3e4949eSKewei Xu i2c->pdmabase + OFFSET_RST); 974e3e4949eSKewei Xu 975e3e4949eSKewei Xu ret = readw_poll_timeout(i2c->pdmabase + OFFSET_RST, 976e3e4949eSKewei Xu reg_dma_reset, 977e3e4949eSKewei Xu !(reg_dma_reset & I2C_DMA_WARM_RST), 978e3e4949eSKewei Xu 0, 100); 979e3e4949eSKewei Xu if (ret) { 980e3e4949eSKewei Xu dev_err(i2c->dev, "DMA warm reset timeout\n"); 981e3e4949eSKewei Xu return -ETIMEDOUT; 982e3e4949eSKewei Xu } 983e3e4949eSKewei Xu 984e3e4949eSKewei Xu writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 985e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST, OFFSET_SOFTRESET); 986e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 987e3e4949eSKewei Xu mtk_i2c_writew(i2c, I2C_RELIABILITY | I2C_DMAACK_ENABLE, 988e3e4949eSKewei Xu OFFSET_DEBUGCTRL); 989e3e4949eSKewei Xu } 990e3e4949eSKewei Xu 991bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 992ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 99363ce8e3dSQii Wang if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1)) 994ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 995ce38815dSXudong Chen 996ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 997ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 998ce38815dSXudong Chen 999bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 1000ce38815dSXudong Chen 10010d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 1002bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 1003ce38815dSXudong Chen 1004ce38815dSXudong Chen /* Clear interrupt status */ 1005bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1006cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 1007bc6eaf17SQii Wang 1008bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 1009ce38815dSXudong Chen 1010ce38815dSXudong Chen /* Enable interrupt */ 1011bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1012cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 1013ce38815dSXudong Chen 1014ce38815dSXudong Chen /* Set transfer and transaction len */ 1015ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 1016173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 1017bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1018bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 1019173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 1020173b77e8SLiguo Zhang } else { 1021bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 1022bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 1023173b77e8SLiguo Zhang } 1024bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 1025ce38815dSXudong Chen } else { 1026bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 1027bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 1028ce38815dSXudong Chen } 1029ce38815dSXudong Chen 10308426fe70SQii Wang if (i2c->dev_comp->apdma_sync) { 10318426fe70SQii Wang dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE; 10328426fe70SQii Wang if (i2c->op == I2C_MASTER_WRRD) 10338426fe70SQii Wang dma_sync |= I2C_DMA_DIR_CHANGE; 10348426fe70SQii Wang } 10358426fe70SQii Wang 1036ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 1037ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 1038ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 10398426fe70SQii Wang writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON); 1040fc66b39fSJun Gao 1041bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1042fc66b39fSJun Gao if (!dma_rd_buf) 1043ce38815dSXudong Chen return -ENOMEM; 1044f4f4fed6SLiguo Zhang 1045fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1046fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 1047fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 1048fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 1049fc66b39fSJun Gao 1050fc66b39fSJun Gao return -ENOMEM; 1051fc66b39fSJun Gao } 1052fc66b39fSJun Gao 1053908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1054908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1055f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1056f4f4fed6SLiguo Zhang } 1057f4f4fed6SLiguo Zhang 1058ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1059ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 1060ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 1061ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 10628426fe70SQii Wang writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON); 1063fc66b39fSJun Gao 1064bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1065fc66b39fSJun Gao if (!dma_wr_buf) 1066ce38815dSXudong Chen return -ENOMEM; 1067f4f4fed6SLiguo Zhang 1068fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1069fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1070fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1071fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1072fc66b39fSJun Gao 1073fc66b39fSJun Gao return -ENOMEM; 1074fc66b39fSJun Gao } 1075fc66b39fSJun Gao 1076908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1077908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1078f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1079f4f4fed6SLiguo Zhang } 1080f4f4fed6SLiguo Zhang 1081ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1082ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1083ce38815dSXudong Chen } else { 1084ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 10858426fe70SQii Wang writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON); 1086fc66b39fSJun Gao 1087bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 1088fc66b39fSJun Gao if (!dma_wr_buf) 1089ce38815dSXudong Chen return -ENOMEM; 1090fc66b39fSJun Gao 1091fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 1092fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1093fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 1094fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1095fc66b39fSJun Gao 1096fc66b39fSJun Gao return -ENOMEM; 1097fc66b39fSJun Gao } 1098fc66b39fSJun Gao 1099bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 1100fc66b39fSJun Gao if (!dma_rd_buf) { 1101fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 1102fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 1103fc66b39fSJun Gao 1104fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1105fc66b39fSJun Gao 1106fc66b39fSJun Gao return -ENOMEM; 1107fc66b39fSJun Gao } 1108fc66b39fSJun Gao 1109fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 1110ce38815dSXudong Chen (msgs + 1)->len, 1111ce38815dSXudong Chen DMA_FROM_DEVICE); 1112ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 1113ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1114ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1115fc66b39fSJun Gao 1116fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 1117fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 1118fc66b39fSJun Gao 1119ce38815dSXudong Chen return -ENOMEM; 1120ce38815dSXudong Chen } 1121f4f4fed6SLiguo Zhang 1122908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1123908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 1124f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 1125f4f4fed6SLiguo Zhang 1126908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 1127f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 1128f4f4fed6SLiguo Zhang } 1129f4f4fed6SLiguo Zhang 1130ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 1131ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 1132ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1133ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 1134ce38815dSXudong Chen } 1135ce38815dSXudong Chen 1136ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 1137b2ed11e2SEddie Huang 1138173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1139b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 1140b2ed11e2SEddie Huang } else { 1141b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 1142b2ed11e2SEddie Huang if (left_num >= 1) 1143b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 1144b2ed11e2SEddie Huang } 1145bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 1146ce38815dSXudong Chen 1147ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 1148ce38815dSXudong Chen i2c->adap.timeout); 1149ce38815dSXudong Chen 1150ce38815dSXudong Chen /* Clear interrupt mask */ 1151bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1152cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 1153ce38815dSXudong Chen 1154ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 1155ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1156ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1157fc66b39fSJun Gao 1158fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1159ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 1160ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 1161ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 1162fc66b39fSJun Gao 1163fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 1164ce38815dSXudong Chen } else { 1165ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 1166ce38815dSXudong Chen DMA_TO_DEVICE); 1167ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 1168ce38815dSXudong Chen DMA_FROM_DEVICE); 1169fc66b39fSJun Gao 1170fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1171fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 1172ce38815dSXudong Chen } 1173ce38815dSXudong Chen 1174ce38815dSXudong Chen if (ret == 0) { 1175ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 1176cc28e578SKewei Xu i2c_dump_register(i2c); 1177ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1178ce38815dSXudong Chen return -ETIMEDOUT; 1179ce38815dSXudong Chen } 1180ce38815dSXudong Chen 1181ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 1182ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 1183ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1184ce38815dSXudong Chen return -ENXIO; 1185ce38815dSXudong Chen } 1186ce38815dSXudong Chen 1187ce38815dSXudong Chen return 0; 1188ce38815dSXudong Chen } 1189ce38815dSXudong Chen 1190ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 1191ce38815dSXudong Chen struct i2c_msg msgs[], int num) 1192ce38815dSXudong Chen { 1193ce38815dSXudong Chen int ret; 1194ce38815dSXudong Chen int left_num = num; 1195ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 1196ce38815dSXudong Chen 1197ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1198ce38815dSXudong Chen if (ret) 1199ce38815dSXudong Chen return ret; 1200ce38815dSXudong Chen 1201173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 1202173b77e8SLiguo Zhang 1203173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 1204173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 1205173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 1206173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 1207173b77e8SLiguo Zhang i2c->auto_restart = 0; 1208173b77e8SLiguo Zhang } 1209173b77e8SLiguo Zhang } 1210173b77e8SLiguo Zhang 121163ce8e3dSQii Wang if (i2c->auto_restart && num >= 2 && 121263ce8e3dSQii Wang i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 12138378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 12148378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 12158378d01fSLiguo Zhang */ 12168378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 12178378d01fSLiguo Zhang else 12188378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 12198378d01fSLiguo Zhang 1220b2ed11e2SEddie Huang while (left_num--) { 1221ce38815dSXudong Chen if (!msgs->buf) { 1222ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 1223ce38815dSXudong Chen ret = -EINVAL; 1224ce38815dSXudong Chen goto err_exit; 1225ce38815dSXudong Chen } 1226ce38815dSXudong Chen 1227ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 1228ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 1229ce38815dSXudong Chen else 1230ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 1231ce38815dSXudong Chen 1232173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1233ce38815dSXudong Chen if (num > 1) { 1234ce38815dSXudong Chen /* combined two messages into one transaction */ 1235ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 1236ce38815dSXudong Chen left_num--; 1237ce38815dSXudong Chen } 1238b2ed11e2SEddie Huang } 1239ce38815dSXudong Chen 1240ce38815dSXudong Chen /* always use DMA mode. */ 1241b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 1242ce38815dSXudong Chen if (ret < 0) 1243ce38815dSXudong Chen goto err_exit; 1244ce38815dSXudong Chen 1245b2ed11e2SEddie Huang msgs++; 1246b2ed11e2SEddie Huang } 1247ce38815dSXudong Chen /* the return value is number of executed messages */ 1248ce38815dSXudong Chen ret = num; 1249ce38815dSXudong Chen 1250ce38815dSXudong Chen err_exit: 1251ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1252ce38815dSXudong Chen return ret; 1253ce38815dSXudong Chen } 1254ce38815dSXudong Chen 1255ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 1256ce38815dSXudong Chen { 1257ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 1258b2ed11e2SEddie Huang u16 restart_flag = 0; 125928c0a843SEddie Huang u16 intr_stat; 1260b2ed11e2SEddie Huang 1261173b77e8SLiguo Zhang if (i2c->auto_restart) 1262b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1263ce38815dSXudong Chen 1264bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 1265bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 1266ce38815dSXudong Chen 126728c0a843SEddie Huang /* 126828c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 126928c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 127028c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 127128c0a843SEddie Huang */ 127228c0a843SEddie Huang i2c->irq_stat |= intr_stat; 12738378d01fSLiguo Zhang 12748378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 12758378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 12768378d01fSLiguo Zhang i2c->irq_stat = 0; 1277bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 1278bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 12798378d01fSLiguo Zhang } else { 128028c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 1281ce38815dSXudong Chen complete(&i2c->msg_complete); 12828378d01fSLiguo Zhang } 1283ce38815dSXudong Chen 1284ce38815dSXudong Chen return IRQ_HANDLED; 1285ce38815dSXudong Chen } 1286ce38815dSXudong Chen 1287ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 1288ce38815dSXudong Chen { 128962931ac2SFabien Parent if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN)) 1290abf4923eSHsin-Yi Wang return I2C_FUNC_I2C | 1291abf4923eSHsin-Yi Wang (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1292abf4923eSHsin-Yi Wang else 1293ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1294ce38815dSXudong Chen } 1295ce38815dSXudong Chen 1296ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 1297ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 1298ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 1299ce38815dSXudong Chen }; 1300ce38815dSXudong Chen 1301f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 1302ce38815dSXudong Chen { 1303ce38815dSXudong Chen int ret; 1304ce38815dSXudong Chen 1305ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 1306ce38815dSXudong Chen if (ret < 0) 130790224e64SAndy Shevchenko i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ; 1308ce38815dSXudong Chen 1309f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 1310ce38815dSXudong Chen if (ret < 0) 1311ce38815dSXudong Chen return ret; 1312ce38815dSXudong Chen 1313f2326401SJun Gao if (i2c->clk_src_div == 0) 1314ce38815dSXudong Chen return -EINVAL; 1315ce38815dSXudong Chen 1316ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 1317ce38815dSXudong Chen i2c->use_push_pull = 1318ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 1319ce38815dSXudong Chen 1320a80f2494SQii Wang i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); 1321a80f2494SQii Wang 1322ce38815dSXudong Chen return 0; 1323ce38815dSXudong Chen } 1324ce38815dSXudong Chen 1325ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 1326ce38815dSXudong Chen { 1327ce38815dSXudong Chen int ret = 0; 1328ce38815dSXudong Chen struct mtk_i2c *i2c; 1329ce38815dSXudong Chen struct clk *clk; 1330ce38815dSXudong Chen struct resource *res; 1331ce38815dSXudong Chen int irq; 1332ce38815dSXudong Chen 1333ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 1334ce38815dSXudong Chen if (!i2c) 1335ce38815dSXudong Chen return -ENOMEM; 1336ce38815dSXudong Chen 1337ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1338ce38815dSXudong Chen i2c->base = devm_ioremap_resource(&pdev->dev, res); 1339ce38815dSXudong Chen if (IS_ERR(i2c->base)) 1340ce38815dSXudong Chen return PTR_ERR(i2c->base); 1341ce38815dSXudong Chen 1342ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1343ce38815dSXudong Chen i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); 1344ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 1345ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 1346ce38815dSXudong Chen 1347ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 134858fb7c64SSergey Shtylyov if (irq < 0) 1349ce38815dSXudong Chen return irq; 1350ce38815dSXudong Chen 1351ce38815dSXudong Chen init_completion(&i2c->msg_complete); 1352ce38815dSXudong Chen 13536e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 1354ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 1355ce38815dSXudong Chen i2c->dev = &pdev->dev; 1356ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 1357ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 1358ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 1359ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 1360ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 1361ce38815dSXudong Chen i2c->adap.retries = 1; 13629029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = devm_regulator_get_optional(&pdev->dev, "vbus"); 13639029b9b2SHsin-Yi Wang if (IS_ERR(i2c->adap.bus_regulator)) { 13649029b9b2SHsin-Yi Wang if (PTR_ERR(i2c->adap.bus_regulator) == -ENODEV) 13659029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = NULL; 13669029b9b2SHsin-Yi Wang else 13679029b9b2SHsin-Yi Wang return PTR_ERR(i2c->adap.bus_regulator); 13689029b9b2SHsin-Yi Wang } 1369ce38815dSXudong Chen 13705a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 13715a10e7d7SJun Gao if (ret) 13725a10e7d7SJun Gao return -EINVAL; 13735a10e7d7SJun Gao 1374ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 1375ce38815dSXudong Chen return -EINVAL; 1376ce38815dSXudong Chen 1377ce38815dSXudong Chen i2c->clk_main = devm_clk_get(&pdev->dev, "main"); 1378ce38815dSXudong Chen if (IS_ERR(i2c->clk_main)) { 1379ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 1380ce38815dSXudong Chen return PTR_ERR(i2c->clk_main); 1381ce38815dSXudong Chen } 1382ce38815dSXudong Chen 1383ce38815dSXudong Chen i2c->clk_dma = devm_clk_get(&pdev->dev, "dma"); 1384ce38815dSXudong Chen if (IS_ERR(i2c->clk_dma)) { 1385ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 1386ce38815dSXudong Chen return PTR_ERR(i2c->clk_dma); 1387ce38815dSXudong Chen } 1388ce38815dSXudong Chen 1389cad6dc5dSQii Wang i2c->clk_arb = devm_clk_get(&pdev->dev, "arb"); 1390cad6dc5dSQii Wang if (IS_ERR(i2c->clk_arb)) 1391cad6dc5dSQii Wang i2c->clk_arb = NULL; 1392cad6dc5dSQii Wang 1393ce38815dSXudong Chen clk = i2c->clk_main; 1394ce38815dSXudong Chen if (i2c->have_pmic) { 1395ce38815dSXudong Chen i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic"); 1396ce38815dSXudong Chen if (IS_ERR(i2c->clk_pmic)) { 1397ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 1398ce38815dSXudong Chen return PTR_ERR(i2c->clk_pmic); 1399ce38815dSXudong Chen } 1400ce38815dSXudong Chen clk = i2c->clk_pmic; 1401ce38815dSXudong Chen } 1402ce38815dSXudong Chen 1403ce38815dSXudong Chen strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1404ce38815dSXudong Chen 1405f2326401SJun Gao ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk)); 1406ce38815dSXudong Chen if (ret) { 1407ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 1408ce38815dSXudong Chen return -EINVAL; 1409ce38815dSXudong Chen } 1410ce38815dSXudong Chen 1411908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1412908d9843SQii Wang ret = dma_set_mask(&pdev->dev, 1413908d9843SQii Wang DMA_BIT_MASK(i2c->dev_comp->max_dma_support)); 1414f4f4fed6SLiguo Zhang if (ret) { 1415f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 1416f4f4fed6SLiguo Zhang return ret; 1417f4f4fed6SLiguo Zhang } 1418f4f4fed6SLiguo Zhang } 1419f4f4fed6SLiguo Zhang 1420ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1421ce38815dSXudong Chen if (ret) { 1422ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 1423ce38815dSXudong Chen return ret; 1424ce38815dSXudong Chen } 1425ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1426ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1427ce38815dSXudong Chen 1428ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 1429de96c394SQii Wang IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, 14307fb9dc81SQii Wang dev_name(&pdev->dev), i2c); 1431ce38815dSXudong Chen if (ret < 0) { 1432ce38815dSXudong Chen dev_err(&pdev->dev, 1433ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 1434ce38815dSXudong Chen return ret; 1435ce38815dSXudong Chen } 1436ce38815dSXudong Chen 1437ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 1438ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 1439ea734404SWolfram Sang if (ret) 1440ce38815dSXudong Chen return ret; 1441ce38815dSXudong Chen 1442ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 1443ce38815dSXudong Chen 1444ce38815dSXudong Chen return 0; 1445ce38815dSXudong Chen } 1446ce38815dSXudong Chen 1447ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev) 1448ce38815dSXudong Chen { 1449ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 1450ce38815dSXudong Chen 1451ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 1452ce38815dSXudong Chen 1453ce38815dSXudong Chen return 0; 1454ce38815dSXudong Chen } 1455ce38815dSXudong Chen 145609027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP 1457de96c394SQii Wang static int mtk_i2c_suspend_noirq(struct device *dev) 1458de96c394SQii Wang { 1459de96c394SQii Wang struct mtk_i2c *i2c = dev_get_drvdata(dev); 1460de96c394SQii Wang 1461de96c394SQii Wang i2c_mark_adapter_suspended(&i2c->adap); 1462de96c394SQii Wang 1463de96c394SQii Wang return 0; 1464de96c394SQii Wang } 1465de96c394SQii Wang 1466de96c394SQii Wang static int mtk_i2c_resume_noirq(struct device *dev) 146709027e08SLiguo Zhang { 1468f6762cedSJun Gao int ret; 146909027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 147009027e08SLiguo Zhang 1471f6762cedSJun Gao ret = mtk_i2c_clock_enable(i2c); 1472f6762cedSJun Gao if (ret) { 1473f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 1474f6762cedSJun Gao return ret; 1475f6762cedSJun Gao } 1476f6762cedSJun Gao 147709027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 147809027e08SLiguo Zhang 1479f6762cedSJun Gao mtk_i2c_clock_disable(i2c); 1480f6762cedSJun Gao 1481de96c394SQii Wang i2c_mark_adapter_resumed(&i2c->adap); 1482de96c394SQii Wang 148309027e08SLiguo Zhang return 0; 148409027e08SLiguo Zhang } 148509027e08SLiguo Zhang #endif 148609027e08SLiguo Zhang 148709027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 1488de96c394SQii Wang SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq, 1489de96c394SQii Wang mtk_i2c_resume_noirq) 149009027e08SLiguo Zhang }; 149109027e08SLiguo Zhang 1492ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1493ce38815dSXudong Chen .probe = mtk_i2c_probe, 1494ce38815dSXudong Chen .remove = mtk_i2c_remove, 1495ce38815dSXudong Chen .driver = { 1496ce38815dSXudong Chen .name = I2C_DRV_NAME, 149709027e08SLiguo Zhang .pm = &mtk_i2c_pm, 1498ce38815dSXudong Chen .of_match_table = of_match_ptr(mtk_i2c_of_match), 1499ce38815dSXudong Chen }, 1500ce38815dSXudong Chen }; 1501ce38815dSXudong Chen 1502ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1503ce38815dSXudong Chen 1504ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1505ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1506ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1507