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> 18ce38815dSXudong Chen #include <linux/kernel.h> 19ce38815dSXudong Chen #include <linux/mm.h> 20ce38815dSXudong Chen #include <linux/module.h> 21ce38815dSXudong Chen #include <linux/of_address.h> 226e29577fSRyder Lee #include <linux/of_device.h> 23ce38815dSXudong Chen #include <linux/of_irq.h> 24ce38815dSXudong Chen #include <linux/platform_device.h> 25ce38815dSXudong Chen #include <linux/scatterlist.h> 26ce38815dSXudong Chen #include <linux/sched.h> 27ce38815dSXudong Chen #include <linux/slab.h> 28ce38815dSXudong Chen 29b2ed11e2SEddie Huang #define I2C_RS_TRANSFER (1 << 4) 30cad6dc5dSQii Wang #define I2C_ARB_LOST (1 << 3) 31ce38815dSXudong Chen #define I2C_HS_NACKERR (1 << 2) 32ce38815dSXudong Chen #define I2C_ACKERR (1 << 1) 33ce38815dSXudong Chen #define I2C_TRANSAC_COMP (1 << 0) 34ce38815dSXudong Chen #define I2C_TRANSAC_START (1 << 0) 35b2ed11e2SEddie Huang #define I2C_RS_MUL_CNFG (1 << 15) 36b2ed11e2SEddie Huang #define I2C_RS_MUL_TRIG (1 << 14) 37ce38815dSXudong Chen #define I2C_DCM_DISABLE 0x0000 38ce38815dSXudong Chen #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003 39ce38815dSXudong Chen #define I2C_IO_CONFIG_PUSH_PULL 0x0000 40ce38815dSXudong Chen #define I2C_SOFT_RST 0x0001 4105f6f727SQii Wang #define I2C_HANDSHAKE_RST 0x0020 42ce38815dSXudong Chen #define I2C_FIFO_ADDR_CLR 0x0001 43ce38815dSXudong Chen #define I2C_DELAY_LEN 0x0002 44ce38815dSXudong Chen #define I2C_TIME_CLR_VALUE 0x0000 45ce38815dSXudong Chen #define I2C_TIME_DEFAULT_VALUE 0x0003 46ce38815dSXudong Chen #define I2C_WRRD_TRANAC_VALUE 0x0002 47ce38815dSXudong Chen #define I2C_RD_TRANAC_VALUE 0x0001 48be5ce0e9SQii Wang #define I2C_SCL_MIS_COMP_VALUE 0x0000 4905f6f727SQii Wang #define I2C_CHN_CLR_FLAG 0x0000 50ce38815dSXudong Chen 51ce38815dSXudong Chen #define I2C_DMA_CON_TX 0x0000 52ce38815dSXudong Chen #define I2C_DMA_CON_RX 0x0001 538426fe70SQii Wang #define I2C_DMA_ASYNC_MODE 0x0004 548426fe70SQii Wang #define I2C_DMA_SKIP_CONFIG 0x0010 558426fe70SQii Wang #define I2C_DMA_DIR_CHANGE 0x0200 56ce38815dSXudong Chen #define I2C_DMA_START_EN 0x0001 57ce38815dSXudong Chen #define I2C_DMA_INT_FLAG_NONE 0x0000 58ce38815dSXudong Chen #define I2C_DMA_CLR_FLAG 0x0000 5905f6f727SQii Wang #define I2C_DMA_WARM_RST 0x0001 60ea89ef1fSEddie Huang #define I2C_DMA_HARD_RST 0x0002 6105f6f727SQii Wang #define I2C_DMA_HANDSHAKE_RST 0x0004 62ce38815dSXudong Chen 63ce38815dSXudong Chen #define MAX_SAMPLE_CNT_DIV 8 64ce38815dSXudong Chen #define MAX_STEP_CNT_DIV 64 65be5ce0e9SQii Wang #define MAX_CLOCK_DIV 256 66ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV 8 67be5ce0e9SQii Wang #define I2C_STANDARD_MODE_BUFFER (1000 / 2) 68be5ce0e9SQii Wang #define I2C_FAST_MODE_BUFFER (300 / 2) 69be5ce0e9SQii Wang #define I2C_FAST_MODE_PLUS_BUFFER (20 / 2) 70ce38815dSXudong Chen 71ce38815dSXudong Chen #define I2C_CONTROL_RS (0x1 << 1) 72ce38815dSXudong Chen #define I2C_CONTROL_DMA_EN (0x1 << 2) 73ce38815dSXudong Chen #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3) 74ce38815dSXudong Chen #define I2C_CONTROL_DIR_CHANGE (0x1 << 4) 75ce38815dSXudong Chen #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5) 76ce38815dSXudong Chen #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6) 77a15c91baSQii Wang #define I2C_CONTROL_DMAACK_EN (0x1 << 8) 78a15c91baSQii Wang #define I2C_CONTROL_ASYNC_MODE (0x1 << 9) 79ce38815dSXudong Chen #define I2C_CONTROL_WRAPPER (0x1 << 0) 80ce38815dSXudong Chen 81ce38815dSXudong Chen #define I2C_DRV_NAME "i2c-mt65xx" 82ce38815dSXudong Chen 83ce38815dSXudong Chen enum DMA_REGS_OFFSET { 84ce38815dSXudong Chen OFFSET_INT_FLAG = 0x0, 85ce38815dSXudong Chen OFFSET_INT_EN = 0x04, 86ce38815dSXudong Chen OFFSET_EN = 0x08, 87ea89ef1fSEddie Huang OFFSET_RST = 0x0c, 88ce38815dSXudong Chen OFFSET_CON = 0x18, 89ce38815dSXudong Chen OFFSET_TX_MEM_ADDR = 0x1c, 90ce38815dSXudong Chen OFFSET_RX_MEM_ADDR = 0x20, 91ce38815dSXudong Chen OFFSET_TX_LEN = 0x24, 92ce38815dSXudong Chen OFFSET_RX_LEN = 0x28, 93f4f4fed6SLiguo Zhang OFFSET_TX_4G_MODE = 0x54, 94f4f4fed6SLiguo Zhang OFFSET_RX_4G_MODE = 0x58, 95ce38815dSXudong Chen }; 96ce38815dSXudong Chen 97ce38815dSXudong Chen enum i2c_trans_st_rs { 98ce38815dSXudong Chen I2C_TRANS_STOP = 0, 99ce38815dSXudong Chen I2C_TRANS_REPEATED_START, 100ce38815dSXudong Chen }; 101ce38815dSXudong Chen 102ce38815dSXudong Chen enum mtk_trans_op { 103ce38815dSXudong Chen I2C_MASTER_WR = 1, 104ce38815dSXudong Chen I2C_MASTER_RD, 105ce38815dSXudong Chen I2C_MASTER_WRRD, 106ce38815dSXudong Chen }; 107ce38815dSXudong Chen 108ce38815dSXudong Chen enum I2C_REGS_OFFSET { 109bc6eaf17SQii Wang OFFSET_DATA_PORT, 110bc6eaf17SQii Wang OFFSET_SLAVE_ADDR, 111bc6eaf17SQii Wang OFFSET_INTR_MASK, 112bc6eaf17SQii Wang OFFSET_INTR_STAT, 113bc6eaf17SQii Wang OFFSET_CONTROL, 114bc6eaf17SQii Wang OFFSET_TRANSFER_LEN, 115bc6eaf17SQii Wang OFFSET_TRANSAC_LEN, 116bc6eaf17SQii Wang OFFSET_DELAY_LEN, 117bc6eaf17SQii Wang OFFSET_TIMING, 118bc6eaf17SQii Wang OFFSET_START, 119bc6eaf17SQii Wang OFFSET_EXT_CONF, 120bc6eaf17SQii Wang OFFSET_FIFO_STAT, 121bc6eaf17SQii Wang OFFSET_FIFO_THRESH, 122bc6eaf17SQii Wang OFFSET_FIFO_ADDR_CLR, 123bc6eaf17SQii Wang OFFSET_IO_CONFIG, 124bc6eaf17SQii Wang OFFSET_RSV_DEBUG, 125bc6eaf17SQii Wang OFFSET_HS, 126bc6eaf17SQii Wang OFFSET_SOFTRESET, 127bc6eaf17SQii Wang OFFSET_DCM_EN, 128bc6eaf17SQii Wang OFFSET_PATH_DIR, 129bc6eaf17SQii Wang OFFSET_DEBUGSTAT, 130bc6eaf17SQii Wang OFFSET_DEBUGCTRL, 131bc6eaf17SQii Wang OFFSET_TRANSFER_LEN_AUX, 132bc6eaf17SQii Wang OFFSET_CLOCK_DIV, 13325708278SQii Wang OFFSET_LTIMING, 134be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO, 135be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO, 136be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT, 137be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING, 138be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING, 139be5ce0e9SQii Wang OFFSET_SDA_TIMING, 140bc6eaf17SQii Wang }; 141bc6eaf17SQii Wang 142bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = { 143bc6eaf17SQii Wang [OFFSET_DATA_PORT] = 0x0, 144bc6eaf17SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 145bc6eaf17SQii Wang [OFFSET_INTR_MASK] = 0x8, 146bc6eaf17SQii Wang [OFFSET_INTR_STAT] = 0xc, 147bc6eaf17SQii Wang [OFFSET_CONTROL] = 0x10, 148bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 149bc6eaf17SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 150bc6eaf17SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 151bc6eaf17SQii Wang [OFFSET_TIMING] = 0x20, 152bc6eaf17SQii Wang [OFFSET_START] = 0x24, 153bc6eaf17SQii Wang [OFFSET_EXT_CONF] = 0x28, 154bc6eaf17SQii Wang [OFFSET_FIFO_STAT] = 0x30, 155bc6eaf17SQii Wang [OFFSET_FIFO_THRESH] = 0x34, 156bc6eaf17SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 157bc6eaf17SQii Wang [OFFSET_IO_CONFIG] = 0x40, 158bc6eaf17SQii Wang [OFFSET_RSV_DEBUG] = 0x44, 159bc6eaf17SQii Wang [OFFSET_HS] = 0x48, 160bc6eaf17SQii Wang [OFFSET_SOFTRESET] = 0x50, 161bc6eaf17SQii Wang [OFFSET_DCM_EN] = 0x54, 162bc6eaf17SQii Wang [OFFSET_PATH_DIR] = 0x60, 163bc6eaf17SQii Wang [OFFSET_DEBUGSTAT] = 0x64, 164bc6eaf17SQii Wang [OFFSET_DEBUGCTRL] = 0x68, 165bc6eaf17SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x6c, 166bc6eaf17SQii Wang [OFFSET_CLOCK_DIV] = 0x70, 167be5ce0e9SQii Wang [OFFSET_SCL_HIGH_LOW_RATIO] = 0x74, 168be5ce0e9SQii Wang [OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78, 169be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x7C, 170be5ce0e9SQii Wang [OFFSET_STA_STO_AC_TIMING] = 0x80, 171be5ce0e9SQii Wang [OFFSET_HS_STA_STO_AC_TIMING] = 0x84, 172be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x88, 173ce38815dSXudong Chen }; 174ce38815dSXudong Chen 17525708278SQii Wang static const u16 mt_i2c_regs_v2[] = { 17625708278SQii Wang [OFFSET_DATA_PORT] = 0x0, 17725708278SQii Wang [OFFSET_SLAVE_ADDR] = 0x4, 17825708278SQii Wang [OFFSET_INTR_MASK] = 0x8, 17925708278SQii Wang [OFFSET_INTR_STAT] = 0xc, 18025708278SQii Wang [OFFSET_CONTROL] = 0x10, 18125708278SQii Wang [OFFSET_TRANSFER_LEN] = 0x14, 18225708278SQii Wang [OFFSET_TRANSAC_LEN] = 0x18, 18325708278SQii Wang [OFFSET_DELAY_LEN] = 0x1c, 18425708278SQii Wang [OFFSET_TIMING] = 0x20, 18525708278SQii Wang [OFFSET_START] = 0x24, 18625708278SQii Wang [OFFSET_EXT_CONF] = 0x28, 18725708278SQii Wang [OFFSET_LTIMING] = 0x2c, 18825708278SQii Wang [OFFSET_HS] = 0x30, 18925708278SQii Wang [OFFSET_IO_CONFIG] = 0x34, 19025708278SQii Wang [OFFSET_FIFO_ADDR_CLR] = 0x38, 191be5ce0e9SQii Wang [OFFSET_SDA_TIMING] = 0x3c, 19225708278SQii Wang [OFFSET_TRANSFER_LEN_AUX] = 0x44, 19325708278SQii Wang [OFFSET_CLOCK_DIV] = 0x48, 19425708278SQii Wang [OFFSET_SOFTRESET] = 0x50, 195be5ce0e9SQii Wang [OFFSET_SCL_MIS_COMP_POINT] = 0x90, 19625708278SQii Wang [OFFSET_DEBUGSTAT] = 0xe0, 19725708278SQii Wang [OFFSET_DEBUGCTRL] = 0xe8, 19825708278SQii Wang [OFFSET_FIFO_STAT] = 0xf4, 19925708278SQii Wang [OFFSET_FIFO_THRESH] = 0xf8, 20025708278SQii Wang [OFFSET_DCM_EN] = 0xf88, 20125708278SQii Wang }; 20225708278SQii Wang 203ce38815dSXudong Chen struct mtk_i2c_compatible { 204ce38815dSXudong Chen const struct i2c_adapter_quirks *quirks; 205bc6eaf17SQii Wang const u16 *regs; 206ce38815dSXudong Chen unsigned char pmic_i2c: 1; 207ce38815dSXudong Chen unsigned char dcm: 1; 208b2ed11e2SEddie Huang unsigned char auto_restart: 1; 209173b77e8SLiguo Zhang unsigned char aux_len_reg: 1; 2105a10e7d7SJun Gao unsigned char timing_adjust: 1; 211a15c91baSQii Wang unsigned char dma_sync: 1; 21225708278SQii Wang unsigned char ltiming_adjust: 1; 2138426fe70SQii Wang unsigned char apdma_sync: 1; 214908d9843SQii Wang unsigned char max_dma_support; 215ce38815dSXudong Chen }; 216ce38815dSXudong Chen 217be5ce0e9SQii Wang struct mtk_i2c_ac_timing { 218be5ce0e9SQii Wang u16 htiming; 219be5ce0e9SQii Wang u16 ltiming; 220be5ce0e9SQii Wang u16 hs; 221be5ce0e9SQii Wang u16 ext; 222be5ce0e9SQii Wang u16 inter_clk_div; 223be5ce0e9SQii Wang u16 scl_hl_ratio; 224be5ce0e9SQii Wang u16 hs_scl_hl_ratio; 225be5ce0e9SQii Wang u16 sta_stop; 226be5ce0e9SQii Wang u16 hs_sta_stop; 227be5ce0e9SQii Wang u16 sda_timing; 228be5ce0e9SQii Wang }; 229be5ce0e9SQii Wang 230ce38815dSXudong Chen struct mtk_i2c { 231ce38815dSXudong Chen struct i2c_adapter adap; /* i2c host adapter */ 232ce38815dSXudong Chen struct device *dev; 233ce38815dSXudong Chen struct completion msg_complete; 234a80f2494SQii Wang struct i2c_timings timing_info; 235ce38815dSXudong Chen 236ce38815dSXudong Chen /* set in i2c probe */ 237ce38815dSXudong Chen void __iomem *base; /* i2c base addr */ 238ce38815dSXudong Chen void __iomem *pdmabase; /* dma base address*/ 239ce38815dSXudong Chen struct clk *clk_main; /* main clock for i2c bus */ 240ce38815dSXudong Chen struct clk *clk_dma; /* DMA clock for i2c via DMA */ 241ce38815dSXudong Chen struct clk *clk_pmic; /* PMIC clock for i2c from PMIC */ 242cad6dc5dSQii Wang struct clk *clk_arb; /* Arbitrator clock for i2c */ 243ce38815dSXudong Chen bool have_pmic; /* can use i2c pins from PMIC */ 244ce38815dSXudong Chen bool use_push_pull; /* IO config push-pull mode */ 245ce38815dSXudong Chen 246ce38815dSXudong Chen u16 irq_stat; /* interrupt status */ 247f2326401SJun Gao unsigned int clk_src_div; 248ce38815dSXudong Chen unsigned int speed_hz; /* The speed in transfer */ 249ce38815dSXudong Chen enum mtk_trans_op op; 250ce38815dSXudong Chen u16 timing_reg; 251ce38815dSXudong Chen u16 high_speed_reg; 25225708278SQii Wang u16 ltiming_reg; 253173b77e8SLiguo Zhang unsigned char auto_restart; 2548378d01fSLiguo Zhang bool ignore_restart_irq; 255be5ce0e9SQii Wang struct mtk_i2c_ac_timing ac_timing; 256ce38815dSXudong Chen const struct mtk_i2c_compatible *dev_comp; 257ce38815dSXudong Chen }; 258ce38815dSXudong Chen 259be5ce0e9SQii Wang /** 260be5ce0e9SQii Wang * struct i2c_spec_values: 261b0102a89SMatthias Brugger * @min_low_ns: min LOW period of the SCL clock 262b0102a89SMatthias Brugger * @min_su_sta_ns: min set-up time for a repeated START condition 263b0102a89SMatthias Brugger * @max_hd_dat_ns: max data hold time 264b0102a89SMatthias Brugger * @min_su_dat_ns: min data set-up time 265be5ce0e9SQii Wang */ 266be5ce0e9SQii Wang struct i2c_spec_values { 267be5ce0e9SQii Wang unsigned int min_low_ns; 268be5ce0e9SQii Wang unsigned int min_su_sta_ns; 269be5ce0e9SQii Wang unsigned int max_hd_dat_ns; 270be5ce0e9SQii Wang unsigned int min_su_dat_ns; 271be5ce0e9SQii Wang }; 272be5ce0e9SQii Wang 273be5ce0e9SQii Wang static const struct i2c_spec_values standard_mode_spec = { 274be5ce0e9SQii Wang .min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 275be5ce0e9SQii Wang .min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER, 276be5ce0e9SQii Wang .max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER, 277be5ce0e9SQii Wang .min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER, 278be5ce0e9SQii Wang }; 279be5ce0e9SQii Wang 280be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_spec = { 281be5ce0e9SQii Wang .min_low_ns = 1300 + I2C_FAST_MODE_BUFFER, 282be5ce0e9SQii Wang .min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER, 283be5ce0e9SQii Wang .max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER, 284be5ce0e9SQii Wang .min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER, 285be5ce0e9SQii Wang }; 286be5ce0e9SQii Wang 287be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_plus_spec = { 288be5ce0e9SQii Wang .min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER, 289be5ce0e9SQii Wang .min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER, 290be5ce0e9SQii Wang .max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER, 291be5ce0e9SQii Wang .min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER, 292be5ce0e9SQii Wang }; 293be5ce0e9SQii Wang 294ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = { 295ce38815dSXudong Chen .flags = I2C_AQ_COMB_WRITE_THEN_READ, 296ce38815dSXudong Chen .max_num_msgs = 1, 297ce38815dSXudong Chen .max_write_len = 255, 298ce38815dSXudong Chen .max_read_len = 255, 299ce38815dSXudong Chen .max_comb_1st_msg_len = 255, 300ce38815dSXudong Chen .max_comb_2nd_msg_len = 31, 301ce38815dSXudong Chen }; 302ce38815dSXudong Chen 3031304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = { 3041304fe09SJun Gao .max_num_msgs = 255, 3051304fe09SJun Gao }; 3061304fe09SJun Gao 307abf4923eSHsin-Yi Wang static const struct i2c_adapter_quirks mt8183_i2c_quirks = { 308abf4923eSHsin-Yi Wang .flags = I2C_AQ_NO_ZERO_LEN, 309abf4923eSHsin-Yi Wang }; 310abf4923eSHsin-Yi Wang 3115a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = { 312bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3135a10e7d7SJun Gao .pmic_i2c = 0, 3145a10e7d7SJun Gao .dcm = 1, 3155a10e7d7SJun Gao .auto_restart = 1, 3165a10e7d7SJun Gao .aux_len_reg = 1, 3175a10e7d7SJun Gao .timing_adjust = 1, 318a15c91baSQii Wang .dma_sync = 0, 31925708278SQii Wang .ltiming_adjust = 0, 3208426fe70SQii Wang .apdma_sync = 0, 321908d9843SQii Wang .max_dma_support = 33, 3225a10e7d7SJun Gao }; 3235a10e7d7SJun Gao 324ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = { 325ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 326bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 327ce38815dSXudong Chen .pmic_i2c = 0, 328ce38815dSXudong Chen .dcm = 1, 329b2ed11e2SEddie Huang .auto_restart = 0, 330173b77e8SLiguo Zhang .aux_len_reg = 0, 3315a10e7d7SJun Gao .timing_adjust = 0, 332a15c91baSQii Wang .dma_sync = 0, 33325708278SQii Wang .ltiming_adjust = 0, 3348426fe70SQii Wang .apdma_sync = 0, 335908d9843SQii Wang .max_dma_support = 32, 336ce38815dSXudong Chen }; 337ce38815dSXudong Chen 338ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = { 339ce38815dSXudong Chen .quirks = &mt6577_i2c_quirks, 340bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 341ce38815dSXudong Chen .pmic_i2c = 1, 342ce38815dSXudong Chen .dcm = 0, 343b2ed11e2SEddie Huang .auto_restart = 0, 344173b77e8SLiguo Zhang .aux_len_reg = 0, 3455a10e7d7SJun Gao .timing_adjust = 0, 346a15c91baSQii Wang .dma_sync = 0, 34725708278SQii Wang .ltiming_adjust = 0, 3488426fe70SQii Wang .apdma_sync = 0, 349908d9843SQii Wang .max_dma_support = 32, 350b2ed11e2SEddie Huang }; 351b2ed11e2SEddie Huang 3521304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = { 3531304fe09SJun Gao .quirks = &mt7622_i2c_quirks, 354bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 3551304fe09SJun Gao .pmic_i2c = 0, 3561304fe09SJun Gao .dcm = 1, 3571304fe09SJun Gao .auto_restart = 1, 3581304fe09SJun Gao .aux_len_reg = 1, 3595a10e7d7SJun Gao .timing_adjust = 0, 360a15c91baSQii Wang .dma_sync = 0, 36125708278SQii Wang .ltiming_adjust = 0, 3628426fe70SQii Wang .apdma_sync = 0, 363908d9843SQii Wang .max_dma_support = 32, 3641304fe09SJun Gao }; 3651304fe09SJun Gao 366b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = { 367bc6eaf17SQii Wang .regs = mt_i2c_regs_v1, 368b2ed11e2SEddie Huang .pmic_i2c = 0, 369b2ed11e2SEddie Huang .dcm = 1, 370b2ed11e2SEddie Huang .auto_restart = 1, 371173b77e8SLiguo Zhang .aux_len_reg = 1, 3725a10e7d7SJun Gao .timing_adjust = 0, 373a15c91baSQii Wang .dma_sync = 0, 37425708278SQii Wang .ltiming_adjust = 0, 3758426fe70SQii Wang .apdma_sync = 0, 376908d9843SQii Wang .max_dma_support = 33, 37725708278SQii Wang }; 37825708278SQii Wang 37925708278SQii Wang static const struct mtk_i2c_compatible mt8183_compat = { 380abf4923eSHsin-Yi Wang .quirks = &mt8183_i2c_quirks, 38125708278SQii Wang .regs = mt_i2c_regs_v2, 38225708278SQii Wang .pmic_i2c = 0, 38325708278SQii Wang .dcm = 0, 38425708278SQii Wang .auto_restart = 1, 38525708278SQii Wang .aux_len_reg = 1, 38625708278SQii Wang .timing_adjust = 1, 38725708278SQii Wang .dma_sync = 1, 38825708278SQii Wang .ltiming_adjust = 1, 3898426fe70SQii Wang .apdma_sync = 0, 390908d9843SQii Wang .max_dma_support = 33, 391ce38815dSXudong Chen }; 392ce38815dSXudong Chen 393789e67baSQii Wang static const struct mtk_i2c_compatible mt8192_compat = { 394789e67baSQii Wang .quirks = &mt8183_i2c_quirks, 395789e67baSQii Wang .regs = mt_i2c_regs_v2, 396789e67baSQii Wang .pmic_i2c = 0, 397789e67baSQii Wang .dcm = 0, 398789e67baSQii Wang .auto_restart = 1, 399789e67baSQii Wang .aux_len_reg = 1, 400789e67baSQii Wang .timing_adjust = 1, 401789e67baSQii Wang .dma_sync = 1, 402789e67baSQii Wang .ltiming_adjust = 1, 403789e67baSQii Wang .apdma_sync = 1, 404789e67baSQii Wang .max_dma_support = 36, 405789e67baSQii Wang }; 406789e67baSQii Wang 407ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = { 4085a10e7d7SJun Gao { .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat }, 409ce38815dSXudong Chen { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat }, 410ce38815dSXudong Chen { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat }, 4111304fe09SJun Gao { .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat }, 412b2ed11e2SEddie Huang { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat }, 41325708278SQii Wang { .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat }, 414789e67baSQii Wang { .compatible = "mediatek,mt8192-i2c", .data = &mt8192_compat }, 415ce38815dSXudong Chen {} 416ce38815dSXudong Chen }; 417ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); 418ce38815dSXudong Chen 419bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg) 420bc6eaf17SQii Wang { 421bc6eaf17SQii Wang return readw(i2c->base + i2c->dev_comp->regs[reg]); 422bc6eaf17SQii Wang } 423bc6eaf17SQii Wang 424bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val, 425bc6eaf17SQii Wang enum I2C_REGS_OFFSET reg) 426bc6eaf17SQii Wang { 427bc6eaf17SQii Wang writew(val, i2c->base + i2c->dev_comp->regs[reg]); 428bc6eaf17SQii Wang } 429bc6eaf17SQii Wang 430ce38815dSXudong Chen static int mtk_i2c_clock_enable(struct mtk_i2c *i2c) 431ce38815dSXudong Chen { 432ce38815dSXudong Chen int ret; 433ce38815dSXudong Chen 434ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_dma); 435ce38815dSXudong Chen if (ret) 436ce38815dSXudong Chen return ret; 437ce38815dSXudong Chen 438ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_main); 439ce38815dSXudong Chen if (ret) 440ce38815dSXudong Chen goto err_main; 441ce38815dSXudong Chen 442ce38815dSXudong Chen if (i2c->have_pmic) { 443ce38815dSXudong Chen ret = clk_prepare_enable(i2c->clk_pmic); 444ce38815dSXudong Chen if (ret) 445ce38815dSXudong Chen goto err_pmic; 446ce38815dSXudong Chen } 447cad6dc5dSQii Wang 448cad6dc5dSQii Wang if (i2c->clk_arb) { 449cad6dc5dSQii Wang ret = clk_prepare_enable(i2c->clk_arb); 450cad6dc5dSQii Wang if (ret) 451cad6dc5dSQii Wang goto err_arb; 452cad6dc5dSQii Wang } 453cad6dc5dSQii Wang 454ce38815dSXudong Chen return 0; 455ce38815dSXudong Chen 456cad6dc5dSQii Wang err_arb: 457cad6dc5dSQii Wang if (i2c->have_pmic) 458cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_pmic); 459ce38815dSXudong Chen err_pmic: 460ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 461ce38815dSXudong Chen err_main: 462ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 463ce38815dSXudong Chen 464ce38815dSXudong Chen return ret; 465ce38815dSXudong Chen } 466ce38815dSXudong Chen 467ce38815dSXudong Chen static void mtk_i2c_clock_disable(struct mtk_i2c *i2c) 468ce38815dSXudong Chen { 469cad6dc5dSQii Wang if (i2c->clk_arb) 470cad6dc5dSQii Wang clk_disable_unprepare(i2c->clk_arb); 471cad6dc5dSQii Wang 472ce38815dSXudong Chen if (i2c->have_pmic) 473ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_pmic); 474ce38815dSXudong Chen 475ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_main); 476ce38815dSXudong Chen clk_disable_unprepare(i2c->clk_dma); 477ce38815dSXudong Chen } 478ce38815dSXudong Chen 479ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c) 480ce38815dSXudong Chen { 481ce38815dSXudong Chen u16 control_reg; 482ce38815dSXudong Chen 4833186b880SQii Wang if (i2c->dev_comp->apdma_sync) { 48405f6f727SQii Wang writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST); 48505f6f727SQii Wang udelay(10); 48605f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 48705f6f727SQii Wang udelay(10); 48805f6f727SQii Wang writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST, 48905f6f727SQii Wang i2c->pdmabase + OFFSET_RST); 49005f6f727SQii Wang mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST, 49105f6f727SQii Wang OFFSET_SOFTRESET); 49205f6f727SQii Wang udelay(10); 49305f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 49405f6f727SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 49505f6f727SQii Wang } else { 496aafced67SQii Wang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 497aafced67SQii Wang udelay(50); 498aafced67SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 499bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 50005f6f727SQii Wang } 501ce38815dSXudong Chen 502ce38815dSXudong Chen /* Set ioconfig */ 503ce38815dSXudong Chen if (i2c->use_push_pull) 504bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 505ce38815dSXudong Chen else 506bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 507ce38815dSXudong Chen 508ce38815dSXudong Chen if (i2c->dev_comp->dcm) 509bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 510ce38815dSXudong Chen 511bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 512bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 51325708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 51425708278SQii Wang mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING); 515ce38815dSXudong Chen 516be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) { 517be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ext, OFFSET_EXT_CONF); 518be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, 519be5ce0e9SQii Wang OFFSET_CLOCK_DIV); 520be5ce0e9SQii Wang mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE, 521be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT); 522be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing, 523be5ce0e9SQii Wang OFFSET_SDA_TIMING); 524be5ce0e9SQii Wang 525be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 526be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.htiming, 527be5ce0e9SQii Wang OFFSET_TIMING); 528be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS); 529be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ltiming, 530be5ce0e9SQii Wang OFFSET_LTIMING); 531be5ce0e9SQii Wang } else { 532be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio, 533be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO); 534be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio, 535be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO); 536be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop, 537be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING); 538be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop, 539be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING); 540be5ce0e9SQii Wang } 541be5ce0e9SQii Wang } 542be5ce0e9SQii Wang 543ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 544ce38815dSXudong Chen if (i2c->have_pmic) 545bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 546ce38815dSXudong Chen 547ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 548ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 549a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 550a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 551a15c91baSQii Wang 552bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 553bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 554ce38815dSXudong Chen } 555ce38815dSXudong Chen 556be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed) 557be5ce0e9SQii Wang { 558be5ce0e9SQii Wang if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 559be5ce0e9SQii Wang return &standard_mode_spec; 560be5ce0e9SQii Wang else if (speed <= I2C_MAX_FAST_MODE_FREQ) 561be5ce0e9SQii Wang return &fast_mode_spec; 562be5ce0e9SQii Wang else 563be5ce0e9SQii Wang return &fast_mode_plus_spec; 564be5ce0e9SQii Wang } 565be5ce0e9SQii Wang 566be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed) 567be5ce0e9SQii Wang { 56863ce8e3dSQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) 569be5ce0e9SQii Wang return MAX_HS_STEP_CNT_DIV; 570be5ce0e9SQii Wang else 571be5ce0e9SQii Wang return MAX_STEP_CNT_DIV; 572be5ce0e9SQii Wang } 573be5ce0e9SQii Wang 574be5ce0e9SQii Wang /* 575be5ce0e9SQii Wang * Check and Calculate i2c ac-timing 576be5ce0e9SQii Wang * 577be5ce0e9SQii Wang * Hardware design: 578be5ce0e9SQii Wang * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src 579be5ce0e9SQii Wang * xxx_cnt_div = spec->min_xxx_ns / sample_ns 580be5ce0e9SQii Wang * 581be5ce0e9SQii Wang * Sample_ns is rounded down for xxx_cnt_div would be greater 582be5ce0e9SQii Wang * than the smallest spec. 583be5ce0e9SQii Wang * The sda_timing is chosen as the middle value between 584be5ce0e9SQii Wang * the largest and smallest. 585be5ce0e9SQii Wang */ 586be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, 587be5ce0e9SQii Wang unsigned int clk_src, 588be5ce0e9SQii Wang unsigned int check_speed, 589be5ce0e9SQii Wang unsigned int step_cnt, 590be5ce0e9SQii Wang unsigned int sample_cnt) 591be5ce0e9SQii Wang { 592be5ce0e9SQii Wang const struct i2c_spec_values *spec; 593be5ce0e9SQii Wang unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt; 594be5ce0e9SQii Wang unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f; 595be5ce0e9SQii Wang unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), 596be5ce0e9SQii Wang clk_src); 597be5ce0e9SQii Wang 598be5ce0e9SQii Wang if (!i2c->dev_comp->timing_adjust) 599be5ce0e9SQii Wang return 0; 600be5ce0e9SQii Wang 601be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 602be5ce0e9SQii Wang max_sta_cnt = 0x100; 603be5ce0e9SQii Wang 604be5ce0e9SQii Wang spec = mtk_i2c_get_spec(check_speed); 605be5ce0e9SQii Wang 606be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 607be5ce0e9SQii Wang clk_ns = 1000000000 / clk_src; 608be5ce0e9SQii Wang else 609be5ce0e9SQii Wang clk_ns = sample_ns / 2; 610be5ce0e9SQii Wang 611a80f2494SQii Wang su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns + 612a80f2494SQii Wang i2c->timing_info.scl_int_delay_ns, clk_ns); 613be5ce0e9SQii Wang if (su_sta_cnt > max_sta_cnt) 614be5ce0e9SQii Wang return -1; 615be5ce0e9SQii Wang 616be5ce0e9SQii Wang low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns); 617be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(check_speed); 618be5ce0e9SQii Wang if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) { 619be5ce0e9SQii Wang if (low_cnt > step_cnt) { 620be5ce0e9SQii Wang high_cnt = 2 * step_cnt - low_cnt; 621be5ce0e9SQii Wang } else { 622be5ce0e9SQii Wang high_cnt = step_cnt; 623be5ce0e9SQii Wang low_cnt = step_cnt; 624be5ce0e9SQii Wang } 625be5ce0e9SQii Wang } else { 626be5ce0e9SQii Wang return -2; 627be5ce0e9SQii Wang } 628be5ce0e9SQii Wang 629be5ce0e9SQii Wang sda_max = spec->max_hd_dat_ns / sample_ns; 630be5ce0e9SQii Wang if (sda_max > low_cnt) 631be5ce0e9SQii Wang sda_max = 0; 632be5ce0e9SQii Wang 633be5ce0e9SQii Wang sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns); 634be5ce0e9SQii Wang if (sda_min < low_cnt) 635be5ce0e9SQii Wang sda_min = 0; 636be5ce0e9SQii Wang 637be5ce0e9SQii Wang if (sda_min > sda_max) 638be5ce0e9SQii Wang return -3; 639be5ce0e9SQii Wang 64063ce8e3dSQii Wang if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 641be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 642be5ce0e9SQii Wang i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE | 643be5ce0e9SQii Wang (sample_cnt << 12) | (high_cnt << 8); 644be5ce0e9SQii Wang i2c->ac_timing.ltiming &= ~GENMASK(15, 9); 645be5ce0e9SQii Wang i2c->ac_timing.ltiming |= (sample_cnt << 12) | 646be5ce0e9SQii Wang (low_cnt << 9); 647be5ce0e9SQii Wang i2c->ac_timing.ext &= ~GENMASK(7, 1); 648be5ce0e9SQii Wang i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0); 649be5ce0e9SQii Wang } else { 650be5ce0e9SQii Wang i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) | 651be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 652be5ce0e9SQii Wang i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) | 653be5ce0e9SQii Wang su_sta_cnt; 654be5ce0e9SQii Wang } 655be5ce0e9SQii Wang i2c->ac_timing.sda_timing &= ~GENMASK(11, 6); 656be5ce0e9SQii Wang i2c->ac_timing.sda_timing |= (1 << 12) | 657be5ce0e9SQii Wang ((sda_max + sda_min) / 2) << 6; 658be5ce0e9SQii Wang } else { 659be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 660be5ce0e9SQii Wang i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt); 661be5ce0e9SQii Wang i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt); 662be5ce0e9SQii Wang i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0); 663be5ce0e9SQii Wang } else { 664be5ce0e9SQii Wang i2c->ac_timing.scl_hl_ratio = (1 << 12) | 665be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 666be5ce0e9SQii Wang i2c->ac_timing.sta_stop = (su_sta_cnt << 8) | 667be5ce0e9SQii Wang su_sta_cnt; 668be5ce0e9SQii Wang } 669be5ce0e9SQii Wang 670be5ce0e9SQii Wang i2c->ac_timing.sda_timing = (1 << 12) | 671be5ce0e9SQii Wang (sda_max + sda_min) / 2; 672be5ce0e9SQii Wang } 673be5ce0e9SQii Wang 674be5ce0e9SQii Wang return 0; 675be5ce0e9SQii Wang } 676be5ce0e9SQii Wang 677ce38815dSXudong Chen /* 678ce38815dSXudong Chen * Calculate i2c port speed 679ce38815dSXudong Chen * 680ce38815dSXudong Chen * Hardware design: 681ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 682ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 683ce38815dSXudong Chen * 684ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 685ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 686ce38815dSXudong Chen * sample_cnt and step_cn 687ce38815dSXudong Chen */ 688f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 689f2326401SJun Gao unsigned int target_speed, 690f2326401SJun Gao unsigned int *timing_step_cnt, 691f2326401SJun Gao unsigned int *timing_sample_cnt) 692ce38815dSXudong Chen { 693ce38815dSXudong Chen unsigned int step_cnt; 694ce38815dSXudong Chen unsigned int sample_cnt; 695ce38815dSXudong Chen unsigned int max_step_cnt; 696ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 697ce38815dSXudong Chen unsigned int base_step_cnt; 698ce38815dSXudong Chen unsigned int opt_div; 699ce38815dSXudong Chen unsigned int best_mul; 700ce38815dSXudong Chen unsigned int cnt_mul; 701be5ce0e9SQii Wang int ret = -EINVAL; 702ce38815dSXudong Chen 703ff6f3affSQii Wang if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) 704ff6f3affSQii Wang target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; 705ce38815dSXudong Chen 706be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(target_speed); 707ce38815dSXudong Chen base_step_cnt = max_step_cnt; 708ce38815dSXudong Chen /* Find the best combination */ 709ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 710ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 711ce38815dSXudong Chen 712ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 713ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 714ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 715ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 716ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 717ce38815dSXudong Chen */ 718ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 719ce38815dSXudong Chen step_cnt = DIV_ROUND_UP(opt_div, sample_cnt); 720ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 721ce38815dSXudong Chen if (step_cnt > max_step_cnt) 722ce38815dSXudong Chen continue; 723ce38815dSXudong Chen 724ce38815dSXudong Chen if (cnt_mul < best_mul) { 725be5ce0e9SQii Wang ret = mtk_i2c_check_ac_timing(i2c, clk_src, 726be5ce0e9SQii Wang target_speed, step_cnt - 1, sample_cnt - 1); 727be5ce0e9SQii Wang if (ret) 728be5ce0e9SQii Wang continue; 729be5ce0e9SQii Wang 730ce38815dSXudong Chen best_mul = cnt_mul; 731ce38815dSXudong Chen base_sample_cnt = sample_cnt; 732ce38815dSXudong Chen base_step_cnt = step_cnt; 733ce38815dSXudong Chen if (best_mul == opt_div) 734ce38815dSXudong Chen break; 735ce38815dSXudong Chen } 736ce38815dSXudong Chen } 737ce38815dSXudong Chen 738be5ce0e9SQii Wang if (ret) 739be5ce0e9SQii Wang return -EINVAL; 740be5ce0e9SQii Wang 741ce38815dSXudong Chen sample_cnt = base_sample_cnt; 742ce38815dSXudong Chen step_cnt = base_step_cnt; 743ce38815dSXudong Chen 744ce38815dSXudong Chen if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) { 745ce38815dSXudong Chen /* In this case, hardware can't support such 746ce38815dSXudong Chen * low i2c_bus_freq 747ce38815dSXudong Chen */ 748ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 749ce38815dSXudong Chen return -EINVAL; 750ce38815dSXudong Chen } 751ce38815dSXudong Chen 752f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 753f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 754f2326401SJun Gao 755f2326401SJun Gao return 0; 756f2326401SJun Gao } 757f2326401SJun Gao 758f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 759f2326401SJun Gao { 760f2326401SJun Gao unsigned int clk_src; 761f2326401SJun Gao unsigned int step_cnt; 762f2326401SJun Gao unsigned int sample_cnt; 76325708278SQii Wang unsigned int l_step_cnt; 76425708278SQii Wang unsigned int l_sample_cnt; 765f2326401SJun Gao unsigned int target_speed; 766be5ce0e9SQii Wang unsigned int clk_div; 767be5ce0e9SQii Wang unsigned int max_clk_div; 768f2326401SJun Gao int ret; 769f2326401SJun Gao 770f2326401SJun Gao target_speed = i2c->speed_hz; 771be5ce0e9SQii Wang parent_clk /= i2c->clk_src_div; 772be5ce0e9SQii Wang 773be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) 774be5ce0e9SQii Wang max_clk_div = MAX_CLOCK_DIV; 775be5ce0e9SQii Wang else 776be5ce0e9SQii Wang max_clk_div = 1; 777be5ce0e9SQii Wang 778be5ce0e9SQii Wang for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { 779be5ce0e9SQii Wang clk_src = parent_clk / clk_div; 780ce38815dSXudong Chen 781b44658e7SQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 782f2326401SJun Gao /* Set master code speed register */ 783be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 784be5ce0e9SQii Wang I2C_MAX_FAST_MODE_FREQ, 785be5ce0e9SQii Wang &l_step_cnt, 786be5ce0e9SQii Wang &l_sample_cnt); 787f2326401SJun Gao if (ret < 0) 788be5ce0e9SQii Wang continue; 789f2326401SJun Gao 79025708278SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 791f2326401SJun Gao 792ce38815dSXudong Chen /* Set the high speed mode register */ 793be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 794be5ce0e9SQii Wang target_speed, &step_cnt, 795be5ce0e9SQii Wang &sample_cnt); 796f2326401SJun Gao if (ret < 0) 797be5ce0e9SQii Wang continue; 798f2326401SJun Gao 799ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 800ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 80125708278SQii Wang 80225708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 803be5ce0e9SQii Wang i2c->ltiming_reg = 804be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt | 80525708278SQii Wang (sample_cnt << 12) | (step_cnt << 9); 806ce38815dSXudong Chen } else { 807be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 808be5ce0e9SQii Wang target_speed, &l_step_cnt, 809be5ce0e9SQii Wang &l_sample_cnt); 810f2326401SJun Gao if (ret < 0) 811be5ce0e9SQii Wang continue; 812f2326401SJun Gao 813be5ce0e9SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 814f2326401SJun Gao 815ce38815dSXudong Chen /* Disable the high speed transaction */ 816ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 81725708278SQii Wang 81825708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 819be5ce0e9SQii Wang i2c->ltiming_reg = 820be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt; 821ce38815dSXudong Chen } 822ce38815dSXudong Chen 823be5ce0e9SQii Wang break; 824be5ce0e9SQii Wang } 825be5ce0e9SQii Wang 826be5ce0e9SQii Wang i2c->ac_timing.inter_clk_div = clk_div - 1; 827be5ce0e9SQii Wang 828ce38815dSXudong Chen return 0; 829ce38815dSXudong Chen } 830ce38815dSXudong Chen 831b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 832b2ed11e2SEddie Huang int num, int left_num) 833ce38815dSXudong Chen { 834ce38815dSXudong Chen u16 addr_reg; 835b2ed11e2SEddie Huang u16 start_reg; 836ce38815dSXudong Chen u16 control_reg; 837b2ed11e2SEddie Huang u16 restart_flag = 0; 8388426fe70SQii Wang u16 dma_sync = 0; 839f4f4fed6SLiguo Zhang u32 reg_4g_mode; 840fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 841fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 842ce38815dSXudong Chen dma_addr_t rpaddr = 0; 843ce38815dSXudong Chen dma_addr_t wpaddr = 0; 844ce38815dSXudong Chen int ret; 845ce38815dSXudong Chen 846ce38815dSXudong Chen i2c->irq_stat = 0; 847ce38815dSXudong Chen 848173b77e8SLiguo Zhang if (i2c->auto_restart) 849b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 850b2ed11e2SEddie Huang 851ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 852ce38815dSXudong Chen 853bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 854ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 85563ce8e3dSQii Wang if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1)) 856ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 857ce38815dSXudong Chen 858ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 859ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 860ce38815dSXudong Chen 861bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 862ce38815dSXudong Chen 8630d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 864bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 865ce38815dSXudong Chen 866ce38815dSXudong Chen /* Clear interrupt status */ 867bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 868cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 869bc6eaf17SQii Wang 870bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 871ce38815dSXudong Chen 872ce38815dSXudong Chen /* Enable interrupt */ 873bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 874cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 875ce38815dSXudong Chen 876ce38815dSXudong Chen /* Set transfer and transaction len */ 877ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 878173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 879bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 880bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 881173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 882173b77e8SLiguo Zhang } else { 883bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 884bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 885173b77e8SLiguo Zhang } 886bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 887ce38815dSXudong Chen } else { 888bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 889bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 890ce38815dSXudong Chen } 891ce38815dSXudong Chen 8928426fe70SQii Wang if (i2c->dev_comp->apdma_sync) { 8938426fe70SQii Wang dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE; 8948426fe70SQii Wang if (i2c->op == I2C_MASTER_WRRD) 8958426fe70SQii Wang dma_sync |= I2C_DMA_DIR_CHANGE; 8968426fe70SQii Wang } 8978426fe70SQii Wang 898ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 899ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 900ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 9018426fe70SQii Wang writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON); 902fc66b39fSJun Gao 903bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 904fc66b39fSJun Gao if (!dma_rd_buf) 905ce38815dSXudong Chen return -ENOMEM; 906f4f4fed6SLiguo Zhang 907fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 908fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 909fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 910fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 911fc66b39fSJun Gao 912fc66b39fSJun Gao return -ENOMEM; 913fc66b39fSJun Gao } 914fc66b39fSJun Gao 915908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 916908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 917f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 918f4f4fed6SLiguo Zhang } 919f4f4fed6SLiguo Zhang 920ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 921ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 922ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 923ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 9248426fe70SQii Wang writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON); 925fc66b39fSJun Gao 926bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 927fc66b39fSJun Gao if (!dma_wr_buf) 928ce38815dSXudong Chen return -ENOMEM; 929f4f4fed6SLiguo Zhang 930fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 931fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 932fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 933fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 934fc66b39fSJun Gao 935fc66b39fSJun Gao return -ENOMEM; 936fc66b39fSJun Gao } 937fc66b39fSJun Gao 938908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 939908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 940f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 941f4f4fed6SLiguo Zhang } 942f4f4fed6SLiguo Zhang 943ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 944ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 945ce38815dSXudong Chen } else { 946ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 9478426fe70SQii Wang writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON); 948fc66b39fSJun Gao 949bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 950fc66b39fSJun Gao if (!dma_wr_buf) 951ce38815dSXudong Chen return -ENOMEM; 952fc66b39fSJun Gao 953fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 954fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 955fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 956fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 957fc66b39fSJun Gao 958fc66b39fSJun Gao return -ENOMEM; 959fc66b39fSJun Gao } 960fc66b39fSJun Gao 961bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 962fc66b39fSJun Gao if (!dma_rd_buf) { 963fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 964fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 965fc66b39fSJun Gao 966fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 967fc66b39fSJun Gao 968fc66b39fSJun Gao return -ENOMEM; 969fc66b39fSJun Gao } 970fc66b39fSJun Gao 971fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 972ce38815dSXudong Chen (msgs + 1)->len, 973ce38815dSXudong Chen DMA_FROM_DEVICE); 974ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 975ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 976ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 977fc66b39fSJun Gao 978fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 979fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 980fc66b39fSJun Gao 981ce38815dSXudong Chen return -ENOMEM; 982ce38815dSXudong Chen } 983f4f4fed6SLiguo Zhang 984908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 985908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 986f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 987f4f4fed6SLiguo Zhang 988908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 989f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 990f4f4fed6SLiguo Zhang } 991f4f4fed6SLiguo Zhang 992ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 993ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 994ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 995ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 996ce38815dSXudong Chen } 997ce38815dSXudong Chen 998ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 999b2ed11e2SEddie Huang 1000173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1001b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 1002b2ed11e2SEddie Huang } else { 1003b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 1004b2ed11e2SEddie Huang if (left_num >= 1) 1005b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 1006b2ed11e2SEddie Huang } 1007bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 1008ce38815dSXudong Chen 1009ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 1010ce38815dSXudong Chen i2c->adap.timeout); 1011ce38815dSXudong Chen 1012ce38815dSXudong Chen /* Clear interrupt mask */ 1013bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1014cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 1015ce38815dSXudong Chen 1016ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 1017ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1018ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1019fc66b39fSJun Gao 1020fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1021ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 1022ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 1023ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 1024fc66b39fSJun Gao 1025fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 1026ce38815dSXudong Chen } else { 1027ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 1028ce38815dSXudong Chen DMA_TO_DEVICE); 1029ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 1030ce38815dSXudong Chen DMA_FROM_DEVICE); 1031fc66b39fSJun Gao 1032fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1033fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 1034ce38815dSXudong Chen } 1035ce38815dSXudong Chen 1036ce38815dSXudong Chen if (ret == 0) { 1037ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 1038ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1039ce38815dSXudong Chen return -ETIMEDOUT; 1040ce38815dSXudong Chen } 1041ce38815dSXudong Chen 1042ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 1043ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 1044ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1045ce38815dSXudong Chen return -ENXIO; 1046ce38815dSXudong Chen } 1047ce38815dSXudong Chen 1048ce38815dSXudong Chen return 0; 1049ce38815dSXudong Chen } 1050ce38815dSXudong Chen 1051ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 1052ce38815dSXudong Chen struct i2c_msg msgs[], int num) 1053ce38815dSXudong Chen { 1054ce38815dSXudong Chen int ret; 1055ce38815dSXudong Chen int left_num = num; 1056ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 1057ce38815dSXudong Chen 1058ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1059ce38815dSXudong Chen if (ret) 1060ce38815dSXudong Chen return ret; 1061ce38815dSXudong Chen 1062173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 1063173b77e8SLiguo Zhang 1064173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 1065173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 1066173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 1067173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 1068173b77e8SLiguo Zhang i2c->auto_restart = 0; 1069173b77e8SLiguo Zhang } 1070173b77e8SLiguo Zhang } 1071173b77e8SLiguo Zhang 107263ce8e3dSQii Wang if (i2c->auto_restart && num >= 2 && 107363ce8e3dSQii Wang i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 10748378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 10758378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 10768378d01fSLiguo Zhang */ 10778378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 10788378d01fSLiguo Zhang else 10798378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 10808378d01fSLiguo Zhang 1081b2ed11e2SEddie Huang while (left_num--) { 1082ce38815dSXudong Chen if (!msgs->buf) { 1083ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 1084ce38815dSXudong Chen ret = -EINVAL; 1085ce38815dSXudong Chen goto err_exit; 1086ce38815dSXudong Chen } 1087ce38815dSXudong Chen 1088ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 1089ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 1090ce38815dSXudong Chen else 1091ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 1092ce38815dSXudong Chen 1093173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1094ce38815dSXudong Chen if (num > 1) { 1095ce38815dSXudong Chen /* combined two messages into one transaction */ 1096ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 1097ce38815dSXudong Chen left_num--; 1098ce38815dSXudong Chen } 1099b2ed11e2SEddie Huang } 1100ce38815dSXudong Chen 1101ce38815dSXudong Chen /* always use DMA mode. */ 1102b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 1103ce38815dSXudong Chen if (ret < 0) 1104ce38815dSXudong Chen goto err_exit; 1105ce38815dSXudong Chen 1106b2ed11e2SEddie Huang msgs++; 1107b2ed11e2SEddie Huang } 1108ce38815dSXudong Chen /* the return value is number of executed messages */ 1109ce38815dSXudong Chen ret = num; 1110ce38815dSXudong Chen 1111ce38815dSXudong Chen err_exit: 1112ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1113ce38815dSXudong Chen return ret; 1114ce38815dSXudong Chen } 1115ce38815dSXudong Chen 1116ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 1117ce38815dSXudong Chen { 1118ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 1119b2ed11e2SEddie Huang u16 restart_flag = 0; 112028c0a843SEddie Huang u16 intr_stat; 1121b2ed11e2SEddie Huang 1122173b77e8SLiguo Zhang if (i2c->auto_restart) 1123b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1124ce38815dSXudong Chen 1125bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 1126bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 1127ce38815dSXudong Chen 112828c0a843SEddie Huang /* 112928c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 113028c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 113128c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 113228c0a843SEddie Huang */ 113328c0a843SEddie Huang i2c->irq_stat |= intr_stat; 11348378d01fSLiguo Zhang 11358378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 11368378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 11378378d01fSLiguo Zhang i2c->irq_stat = 0; 1138bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 1139bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 11408378d01fSLiguo Zhang } else { 114128c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 1142ce38815dSXudong Chen complete(&i2c->msg_complete); 11438378d01fSLiguo Zhang } 1144ce38815dSXudong Chen 1145ce38815dSXudong Chen return IRQ_HANDLED; 1146ce38815dSXudong Chen } 1147ce38815dSXudong Chen 1148ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 1149ce38815dSXudong Chen { 115062931ac2SFabien Parent if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN)) 1151abf4923eSHsin-Yi Wang return I2C_FUNC_I2C | 1152abf4923eSHsin-Yi Wang (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1153abf4923eSHsin-Yi Wang else 1154ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1155ce38815dSXudong Chen } 1156ce38815dSXudong Chen 1157ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 1158ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 1159ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 1160ce38815dSXudong Chen }; 1161ce38815dSXudong Chen 1162f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 1163ce38815dSXudong Chen { 1164ce38815dSXudong Chen int ret; 1165ce38815dSXudong Chen 1166ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 1167ce38815dSXudong Chen if (ret < 0) 116890224e64SAndy Shevchenko i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ; 1169ce38815dSXudong Chen 1170f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 1171ce38815dSXudong Chen if (ret < 0) 1172ce38815dSXudong Chen return ret; 1173ce38815dSXudong Chen 1174f2326401SJun Gao if (i2c->clk_src_div == 0) 1175ce38815dSXudong Chen return -EINVAL; 1176ce38815dSXudong Chen 1177ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 1178ce38815dSXudong Chen i2c->use_push_pull = 1179ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 1180ce38815dSXudong Chen 1181a80f2494SQii Wang i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); 1182a80f2494SQii Wang 1183ce38815dSXudong Chen return 0; 1184ce38815dSXudong Chen } 1185ce38815dSXudong Chen 1186ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 1187ce38815dSXudong Chen { 1188ce38815dSXudong Chen int ret = 0; 1189ce38815dSXudong Chen struct mtk_i2c *i2c; 1190ce38815dSXudong Chen struct clk *clk; 1191ce38815dSXudong Chen struct resource *res; 1192ce38815dSXudong Chen int irq; 1193ce38815dSXudong Chen 1194ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 1195ce38815dSXudong Chen if (!i2c) 1196ce38815dSXudong Chen return -ENOMEM; 1197ce38815dSXudong Chen 1198ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1199ce38815dSXudong Chen i2c->base = devm_ioremap_resource(&pdev->dev, res); 1200ce38815dSXudong Chen if (IS_ERR(i2c->base)) 1201ce38815dSXudong Chen return PTR_ERR(i2c->base); 1202ce38815dSXudong Chen 1203ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1204ce38815dSXudong Chen i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); 1205ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 1206ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 1207ce38815dSXudong Chen 1208ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 1209ce38815dSXudong Chen if (irq <= 0) 1210ce38815dSXudong Chen return irq; 1211ce38815dSXudong Chen 1212ce38815dSXudong Chen init_completion(&i2c->msg_complete); 1213ce38815dSXudong Chen 12146e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 1215ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 1216ce38815dSXudong Chen i2c->dev = &pdev->dev; 1217ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 1218ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 1219ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 1220ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 1221ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 1222ce38815dSXudong Chen i2c->adap.retries = 1; 1223ce38815dSXudong Chen 12245a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 12255a10e7d7SJun Gao if (ret) 12265a10e7d7SJun Gao return -EINVAL; 12275a10e7d7SJun Gao 1228ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 1229ce38815dSXudong Chen return -EINVAL; 1230ce38815dSXudong Chen 1231ce38815dSXudong Chen i2c->clk_main = devm_clk_get(&pdev->dev, "main"); 1232ce38815dSXudong Chen if (IS_ERR(i2c->clk_main)) { 1233ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 1234ce38815dSXudong Chen return PTR_ERR(i2c->clk_main); 1235ce38815dSXudong Chen } 1236ce38815dSXudong Chen 1237ce38815dSXudong Chen i2c->clk_dma = devm_clk_get(&pdev->dev, "dma"); 1238ce38815dSXudong Chen if (IS_ERR(i2c->clk_dma)) { 1239ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 1240ce38815dSXudong Chen return PTR_ERR(i2c->clk_dma); 1241ce38815dSXudong Chen } 1242ce38815dSXudong Chen 1243cad6dc5dSQii Wang i2c->clk_arb = devm_clk_get(&pdev->dev, "arb"); 1244cad6dc5dSQii Wang if (IS_ERR(i2c->clk_arb)) 1245cad6dc5dSQii Wang i2c->clk_arb = NULL; 1246cad6dc5dSQii Wang 1247ce38815dSXudong Chen clk = i2c->clk_main; 1248ce38815dSXudong Chen if (i2c->have_pmic) { 1249ce38815dSXudong Chen i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic"); 1250ce38815dSXudong Chen if (IS_ERR(i2c->clk_pmic)) { 1251ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 1252ce38815dSXudong Chen return PTR_ERR(i2c->clk_pmic); 1253ce38815dSXudong Chen } 1254ce38815dSXudong Chen clk = i2c->clk_pmic; 1255ce38815dSXudong Chen } 1256ce38815dSXudong Chen 1257ce38815dSXudong Chen strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1258ce38815dSXudong Chen 1259f2326401SJun Gao ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk)); 1260ce38815dSXudong Chen if (ret) { 1261ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 1262ce38815dSXudong Chen return -EINVAL; 1263ce38815dSXudong Chen } 1264ce38815dSXudong Chen 1265908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1266908d9843SQii Wang ret = dma_set_mask(&pdev->dev, 1267908d9843SQii Wang DMA_BIT_MASK(i2c->dev_comp->max_dma_support)); 1268f4f4fed6SLiguo Zhang if (ret) { 1269f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 1270f4f4fed6SLiguo Zhang return ret; 1271f4f4fed6SLiguo Zhang } 1272f4f4fed6SLiguo Zhang } 1273f4f4fed6SLiguo Zhang 1274ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1275ce38815dSXudong Chen if (ret) { 1276ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 1277ce38815dSXudong Chen return ret; 1278ce38815dSXudong Chen } 1279ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1280ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1281ce38815dSXudong Chen 1282ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 1283de96c394SQii Wang IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, 1284*7fb9dc81SQii Wang dev_name(&pdev->dev), i2c); 1285ce38815dSXudong Chen if (ret < 0) { 1286ce38815dSXudong Chen dev_err(&pdev->dev, 1287ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 1288ce38815dSXudong Chen return ret; 1289ce38815dSXudong Chen } 1290ce38815dSXudong Chen 1291ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 1292ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 1293ea734404SWolfram Sang if (ret) 1294ce38815dSXudong Chen return ret; 1295ce38815dSXudong Chen 1296ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 1297ce38815dSXudong Chen 1298ce38815dSXudong Chen return 0; 1299ce38815dSXudong Chen } 1300ce38815dSXudong Chen 1301ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev) 1302ce38815dSXudong Chen { 1303ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 1304ce38815dSXudong Chen 1305ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 1306ce38815dSXudong Chen 1307ce38815dSXudong Chen return 0; 1308ce38815dSXudong Chen } 1309ce38815dSXudong Chen 131009027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP 1311de96c394SQii Wang static int mtk_i2c_suspend_noirq(struct device *dev) 1312de96c394SQii Wang { 1313de96c394SQii Wang struct mtk_i2c *i2c = dev_get_drvdata(dev); 1314de96c394SQii Wang 1315de96c394SQii Wang i2c_mark_adapter_suspended(&i2c->adap); 1316de96c394SQii Wang 1317de96c394SQii Wang return 0; 1318de96c394SQii Wang } 1319de96c394SQii Wang 1320de96c394SQii Wang static int mtk_i2c_resume_noirq(struct device *dev) 132109027e08SLiguo Zhang { 1322f6762cedSJun Gao int ret; 132309027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 132409027e08SLiguo Zhang 1325f6762cedSJun Gao ret = mtk_i2c_clock_enable(i2c); 1326f6762cedSJun Gao if (ret) { 1327f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 1328f6762cedSJun Gao return ret; 1329f6762cedSJun Gao } 1330f6762cedSJun Gao 133109027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 133209027e08SLiguo Zhang 1333f6762cedSJun Gao mtk_i2c_clock_disable(i2c); 1334f6762cedSJun Gao 1335de96c394SQii Wang i2c_mark_adapter_resumed(&i2c->adap); 1336de96c394SQii Wang 133709027e08SLiguo Zhang return 0; 133809027e08SLiguo Zhang } 133909027e08SLiguo Zhang #endif 134009027e08SLiguo Zhang 134109027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 1342de96c394SQii Wang SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq, 1343de96c394SQii Wang mtk_i2c_resume_noirq) 134409027e08SLiguo Zhang }; 134509027e08SLiguo Zhang 1346ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1347ce38815dSXudong Chen .probe = mtk_i2c_probe, 1348ce38815dSXudong Chen .remove = mtk_i2c_remove, 1349ce38815dSXudong Chen .driver = { 1350ce38815dSXudong Chen .name = I2C_DRV_NAME, 135109027e08SLiguo Zhang .pm = &mtk_i2c_pm, 1352ce38815dSXudong Chen .of_match_table = of_match_ptr(mtk_i2c_of_match), 1353ce38815dSXudong Chen }, 1354ce38815dSXudong Chen }; 1355ce38815dSXudong Chen 1356ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1357ce38815dSXudong Chen 1358ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1359ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1360ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1361