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; 482fed1bd51SQii Wang u16 intr_stat_reg; 483fed1bd51SQii Wang 484fed1bd51SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_START); 485fed1bd51SQii Wang intr_stat_reg = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 486fed1bd51SQii Wang mtk_i2c_writew(i2c, intr_stat_reg, OFFSET_INTR_STAT); 487ce38815dSXudong Chen 4883186b880SQii Wang if (i2c->dev_comp->apdma_sync) { 48905f6f727SQii Wang writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST); 49005f6f727SQii Wang udelay(10); 49105f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 49205f6f727SQii Wang udelay(10); 49305f6f727SQii Wang writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST, 49405f6f727SQii Wang i2c->pdmabase + OFFSET_RST); 49505f6f727SQii Wang mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST, 49605f6f727SQii Wang OFFSET_SOFTRESET); 49705f6f727SQii Wang udelay(10); 49805f6f727SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 49905f6f727SQii Wang mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET); 50005f6f727SQii Wang } else { 501aafced67SQii Wang writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST); 502aafced67SQii Wang udelay(50); 503aafced67SQii Wang writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST); 504bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET); 50505f6f727SQii Wang } 506ce38815dSXudong Chen 507ce38815dSXudong Chen /* Set ioconfig */ 508ce38815dSXudong Chen if (i2c->use_push_pull) 509bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG); 510ce38815dSXudong Chen else 511bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG); 512ce38815dSXudong Chen 513ce38815dSXudong Chen if (i2c->dev_comp->dcm) 514bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN); 515ce38815dSXudong Chen 516bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING); 517bc6eaf17SQii Wang mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS); 51825708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 51925708278SQii Wang mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING); 520ce38815dSXudong Chen 521be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) { 522be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ext, OFFSET_EXT_CONF); 523be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, 524be5ce0e9SQii Wang OFFSET_CLOCK_DIV); 525be5ce0e9SQii Wang mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE, 526be5ce0e9SQii Wang OFFSET_SCL_MIS_COMP_POINT); 527be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing, 528be5ce0e9SQii Wang OFFSET_SDA_TIMING); 529be5ce0e9SQii Wang 530be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 531be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.htiming, 532be5ce0e9SQii Wang OFFSET_TIMING); 533be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS); 534be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.ltiming, 535be5ce0e9SQii Wang OFFSET_LTIMING); 536be5ce0e9SQii Wang } else { 537be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio, 538be5ce0e9SQii Wang OFFSET_SCL_HIGH_LOW_RATIO); 539be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio, 540be5ce0e9SQii Wang OFFSET_HS_SCL_HIGH_LOW_RATIO); 541be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop, 542be5ce0e9SQii Wang OFFSET_STA_STO_AC_TIMING); 543be5ce0e9SQii Wang mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop, 544be5ce0e9SQii Wang OFFSET_HS_STA_STO_AC_TIMING); 545be5ce0e9SQii Wang } 546be5ce0e9SQii Wang } 547be5ce0e9SQii Wang 548ce38815dSXudong Chen /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ 549ce38815dSXudong Chen if (i2c->have_pmic) 550bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR); 551ce38815dSXudong Chen 552ce38815dSXudong Chen control_reg = I2C_CONTROL_ACKERR_DET_EN | 553ce38815dSXudong Chen I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN; 554a15c91baSQii Wang if (i2c->dev_comp->dma_sync) 555a15c91baSQii Wang control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE; 556a15c91baSQii Wang 557bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 558bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN); 559ce38815dSXudong Chen } 560ce38815dSXudong Chen 561be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed) 562be5ce0e9SQii Wang { 563be5ce0e9SQii Wang if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 564be5ce0e9SQii Wang return &standard_mode_spec; 565be5ce0e9SQii Wang else if (speed <= I2C_MAX_FAST_MODE_FREQ) 566be5ce0e9SQii Wang return &fast_mode_spec; 567be5ce0e9SQii Wang else 568be5ce0e9SQii Wang return &fast_mode_plus_spec; 569be5ce0e9SQii Wang } 570be5ce0e9SQii Wang 571be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed) 572be5ce0e9SQii Wang { 57363ce8e3dSQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) 574be5ce0e9SQii Wang return MAX_HS_STEP_CNT_DIV; 575be5ce0e9SQii Wang else 576be5ce0e9SQii Wang return MAX_STEP_CNT_DIV; 577be5ce0e9SQii Wang } 578be5ce0e9SQii Wang 579be5ce0e9SQii Wang /* 580be5ce0e9SQii Wang * Check and Calculate i2c ac-timing 581be5ce0e9SQii Wang * 582be5ce0e9SQii Wang * Hardware design: 583be5ce0e9SQii Wang * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src 584be5ce0e9SQii Wang * xxx_cnt_div = spec->min_xxx_ns / sample_ns 585be5ce0e9SQii Wang * 586be5ce0e9SQii Wang * Sample_ns is rounded down for xxx_cnt_div would be greater 587be5ce0e9SQii Wang * than the smallest spec. 588be5ce0e9SQii Wang * The sda_timing is chosen as the middle value between 589be5ce0e9SQii Wang * the largest and smallest. 590be5ce0e9SQii Wang */ 591be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, 592be5ce0e9SQii Wang unsigned int clk_src, 593be5ce0e9SQii Wang unsigned int check_speed, 594be5ce0e9SQii Wang unsigned int step_cnt, 595be5ce0e9SQii Wang unsigned int sample_cnt) 596be5ce0e9SQii Wang { 597be5ce0e9SQii Wang const struct i2c_spec_values *spec; 598be5ce0e9SQii Wang unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt; 599be5ce0e9SQii Wang unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f; 600be5ce0e9SQii Wang unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), 601be5ce0e9SQii Wang clk_src); 602be5ce0e9SQii Wang 603be5ce0e9SQii Wang if (!i2c->dev_comp->timing_adjust) 604be5ce0e9SQii Wang return 0; 605be5ce0e9SQii Wang 606be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 607be5ce0e9SQii Wang max_sta_cnt = 0x100; 608be5ce0e9SQii Wang 609be5ce0e9SQii Wang spec = mtk_i2c_get_spec(check_speed); 610be5ce0e9SQii Wang 611be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) 612be5ce0e9SQii Wang clk_ns = 1000000000 / clk_src; 613be5ce0e9SQii Wang else 614be5ce0e9SQii Wang clk_ns = sample_ns / 2; 615be5ce0e9SQii Wang 616a80f2494SQii Wang su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns + 617a80f2494SQii Wang i2c->timing_info.scl_int_delay_ns, clk_ns); 618be5ce0e9SQii Wang if (su_sta_cnt > max_sta_cnt) 619be5ce0e9SQii Wang return -1; 620be5ce0e9SQii Wang 621be5ce0e9SQii Wang low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns); 622be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(check_speed); 623be5ce0e9SQii Wang if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) { 624be5ce0e9SQii Wang if (low_cnt > step_cnt) { 625be5ce0e9SQii Wang high_cnt = 2 * step_cnt - low_cnt; 626be5ce0e9SQii Wang } else { 627be5ce0e9SQii Wang high_cnt = step_cnt; 628be5ce0e9SQii Wang low_cnt = step_cnt; 629be5ce0e9SQii Wang } 630be5ce0e9SQii Wang } else { 631be5ce0e9SQii Wang return -2; 632be5ce0e9SQii Wang } 633be5ce0e9SQii Wang 634be5ce0e9SQii Wang sda_max = spec->max_hd_dat_ns / sample_ns; 635be5ce0e9SQii Wang if (sda_max > low_cnt) 636be5ce0e9SQii Wang sda_max = 0; 637be5ce0e9SQii Wang 638be5ce0e9SQii Wang sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns); 639be5ce0e9SQii Wang if (sda_min < low_cnt) 640be5ce0e9SQii Wang sda_min = 0; 641be5ce0e9SQii Wang 642be5ce0e9SQii Wang if (sda_min > sda_max) 643be5ce0e9SQii Wang return -3; 644be5ce0e9SQii Wang 64563ce8e3dSQii Wang if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 646be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 647be5ce0e9SQii Wang i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE | 648be5ce0e9SQii Wang (sample_cnt << 12) | (high_cnt << 8); 649be5ce0e9SQii Wang i2c->ac_timing.ltiming &= ~GENMASK(15, 9); 650be5ce0e9SQii Wang i2c->ac_timing.ltiming |= (sample_cnt << 12) | 651be5ce0e9SQii Wang (low_cnt << 9); 652be5ce0e9SQii Wang i2c->ac_timing.ext &= ~GENMASK(7, 1); 653be5ce0e9SQii Wang i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0); 654be5ce0e9SQii Wang } else { 655be5ce0e9SQii Wang i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) | 656be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 657be5ce0e9SQii Wang i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) | 658be5ce0e9SQii Wang su_sta_cnt; 659be5ce0e9SQii Wang } 660be5ce0e9SQii Wang i2c->ac_timing.sda_timing &= ~GENMASK(11, 6); 661be5ce0e9SQii Wang i2c->ac_timing.sda_timing |= (1 << 12) | 662be5ce0e9SQii Wang ((sda_max + sda_min) / 2) << 6; 663be5ce0e9SQii Wang } else { 664be5ce0e9SQii Wang if (i2c->dev_comp->ltiming_adjust) { 665be5ce0e9SQii Wang i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt); 666be5ce0e9SQii Wang i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt); 667be5ce0e9SQii Wang i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0); 668be5ce0e9SQii Wang } else { 669be5ce0e9SQii Wang i2c->ac_timing.scl_hl_ratio = (1 << 12) | 670be5ce0e9SQii Wang (high_cnt << 6) | low_cnt; 671be5ce0e9SQii Wang i2c->ac_timing.sta_stop = (su_sta_cnt << 8) | 672be5ce0e9SQii Wang su_sta_cnt; 673be5ce0e9SQii Wang } 674be5ce0e9SQii Wang 675be5ce0e9SQii Wang i2c->ac_timing.sda_timing = (1 << 12) | 676be5ce0e9SQii Wang (sda_max + sda_min) / 2; 677be5ce0e9SQii Wang } 678be5ce0e9SQii Wang 679be5ce0e9SQii Wang return 0; 680be5ce0e9SQii Wang } 681be5ce0e9SQii Wang 682ce38815dSXudong Chen /* 683ce38815dSXudong Chen * Calculate i2c port speed 684ce38815dSXudong Chen * 685ce38815dSXudong Chen * Hardware design: 686ce38815dSXudong Chen * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt) 687ce38815dSXudong Chen * clock_div: fixed in hardware, but may be various in different SoCs 688ce38815dSXudong Chen * 689ce38815dSXudong Chen * The calculation want to pick the highest bus frequency that is still 690ce38815dSXudong Chen * less than or equal to i2c->speed_hz. The calculation try to get 691ce38815dSXudong Chen * sample_cnt and step_cn 692ce38815dSXudong Chen */ 693f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, 694f2326401SJun Gao unsigned int target_speed, 695f2326401SJun Gao unsigned int *timing_step_cnt, 696f2326401SJun Gao unsigned int *timing_sample_cnt) 697ce38815dSXudong Chen { 698ce38815dSXudong Chen unsigned int step_cnt; 699ce38815dSXudong Chen unsigned int sample_cnt; 700ce38815dSXudong Chen unsigned int max_step_cnt; 701ce38815dSXudong Chen unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV; 702ce38815dSXudong Chen unsigned int base_step_cnt; 703ce38815dSXudong Chen unsigned int opt_div; 704ce38815dSXudong Chen unsigned int best_mul; 705ce38815dSXudong Chen unsigned int cnt_mul; 706be5ce0e9SQii Wang int ret = -EINVAL; 707ce38815dSXudong Chen 708ff6f3affSQii Wang if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) 709ff6f3affSQii Wang target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; 710ce38815dSXudong Chen 711be5ce0e9SQii Wang max_step_cnt = mtk_i2c_max_step_cnt(target_speed); 712ce38815dSXudong Chen base_step_cnt = max_step_cnt; 713ce38815dSXudong Chen /* Find the best combination */ 714ce38815dSXudong Chen opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); 715ce38815dSXudong Chen best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; 716ce38815dSXudong Chen 717ce38815dSXudong Chen /* Search for the best pair (sample_cnt, step_cnt) with 718ce38815dSXudong Chen * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV 719ce38815dSXudong Chen * 0 < step_cnt < max_step_cnt 720ce38815dSXudong Chen * sample_cnt * step_cnt >= opt_div 721ce38815dSXudong Chen * optimizing for sample_cnt * step_cnt being minimal 722ce38815dSXudong Chen */ 723ce38815dSXudong Chen for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { 724ce38815dSXudong Chen step_cnt = DIV_ROUND_UP(opt_div, sample_cnt); 725ce38815dSXudong Chen cnt_mul = step_cnt * sample_cnt; 726ce38815dSXudong Chen if (step_cnt > max_step_cnt) 727ce38815dSXudong Chen continue; 728ce38815dSXudong Chen 729ce38815dSXudong Chen if (cnt_mul < best_mul) { 730be5ce0e9SQii Wang ret = mtk_i2c_check_ac_timing(i2c, clk_src, 731be5ce0e9SQii Wang target_speed, step_cnt - 1, sample_cnt - 1); 732be5ce0e9SQii Wang if (ret) 733be5ce0e9SQii Wang continue; 734be5ce0e9SQii Wang 735ce38815dSXudong Chen best_mul = cnt_mul; 736ce38815dSXudong Chen base_sample_cnt = sample_cnt; 737ce38815dSXudong Chen base_step_cnt = step_cnt; 738ce38815dSXudong Chen if (best_mul == opt_div) 739ce38815dSXudong Chen break; 740ce38815dSXudong Chen } 741ce38815dSXudong Chen } 742ce38815dSXudong Chen 743be5ce0e9SQii Wang if (ret) 744be5ce0e9SQii Wang return -EINVAL; 745be5ce0e9SQii Wang 746ce38815dSXudong Chen sample_cnt = base_sample_cnt; 747ce38815dSXudong Chen step_cnt = base_step_cnt; 748ce38815dSXudong Chen 749ce38815dSXudong Chen if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) { 750ce38815dSXudong Chen /* In this case, hardware can't support such 751ce38815dSXudong Chen * low i2c_bus_freq 752ce38815dSXudong Chen */ 753ce38815dSXudong Chen dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed); 754ce38815dSXudong Chen return -EINVAL; 755ce38815dSXudong Chen } 756ce38815dSXudong Chen 757f2326401SJun Gao *timing_step_cnt = step_cnt - 1; 758f2326401SJun Gao *timing_sample_cnt = sample_cnt - 1; 759f2326401SJun Gao 760f2326401SJun Gao return 0; 761f2326401SJun Gao } 762f2326401SJun Gao 763f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 764f2326401SJun Gao { 765f2326401SJun Gao unsigned int clk_src; 766f2326401SJun Gao unsigned int step_cnt; 767f2326401SJun Gao unsigned int sample_cnt; 76825708278SQii Wang unsigned int l_step_cnt; 76925708278SQii Wang unsigned int l_sample_cnt; 770f2326401SJun Gao unsigned int target_speed; 771be5ce0e9SQii Wang unsigned int clk_div; 772be5ce0e9SQii Wang unsigned int max_clk_div; 773f2326401SJun Gao int ret; 774f2326401SJun Gao 775f2326401SJun Gao target_speed = i2c->speed_hz; 776be5ce0e9SQii Wang parent_clk /= i2c->clk_src_div; 777be5ce0e9SQii Wang 778be5ce0e9SQii Wang if (i2c->dev_comp->timing_adjust) 779be5ce0e9SQii Wang max_clk_div = MAX_CLOCK_DIV; 780be5ce0e9SQii Wang else 781be5ce0e9SQii Wang max_clk_div = 1; 782be5ce0e9SQii Wang 783be5ce0e9SQii Wang for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { 784be5ce0e9SQii Wang clk_src = parent_clk / clk_div; 785ce38815dSXudong Chen 786b44658e7SQii Wang if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { 787f2326401SJun Gao /* Set master code speed register */ 788be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 789be5ce0e9SQii Wang I2C_MAX_FAST_MODE_FREQ, 790be5ce0e9SQii Wang &l_step_cnt, 791be5ce0e9SQii Wang &l_sample_cnt); 792f2326401SJun Gao if (ret < 0) 793be5ce0e9SQii Wang continue; 794f2326401SJun Gao 79525708278SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 796f2326401SJun Gao 797ce38815dSXudong Chen /* Set the high speed mode register */ 798be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 799be5ce0e9SQii Wang target_speed, &step_cnt, 800be5ce0e9SQii Wang &sample_cnt); 801f2326401SJun Gao if (ret < 0) 802be5ce0e9SQii Wang continue; 803f2326401SJun Gao 804ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | 805ce38815dSXudong Chen (sample_cnt << 12) | (step_cnt << 8); 80625708278SQii Wang 80725708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 808be5ce0e9SQii Wang i2c->ltiming_reg = 809be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt | 81025708278SQii Wang (sample_cnt << 12) | (step_cnt << 9); 811ce38815dSXudong Chen } else { 812be5ce0e9SQii Wang ret = mtk_i2c_calculate_speed(i2c, clk_src, 813be5ce0e9SQii Wang target_speed, &l_step_cnt, 814be5ce0e9SQii Wang &l_sample_cnt); 815f2326401SJun Gao if (ret < 0) 816be5ce0e9SQii Wang continue; 817f2326401SJun Gao 818be5ce0e9SQii Wang i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; 819f2326401SJun Gao 820ce38815dSXudong Chen /* Disable the high speed transaction */ 821ce38815dSXudong Chen i2c->high_speed_reg = I2C_TIME_CLR_VALUE; 82225708278SQii Wang 82325708278SQii Wang if (i2c->dev_comp->ltiming_adjust) 824be5ce0e9SQii Wang i2c->ltiming_reg = 825be5ce0e9SQii Wang (l_sample_cnt << 6) | l_step_cnt; 826ce38815dSXudong Chen } 827ce38815dSXudong Chen 828be5ce0e9SQii Wang break; 829be5ce0e9SQii Wang } 830be5ce0e9SQii Wang 831be5ce0e9SQii Wang i2c->ac_timing.inter_clk_div = clk_div - 1; 832be5ce0e9SQii Wang 833ce38815dSXudong Chen return 0; 834ce38815dSXudong Chen } 835ce38815dSXudong Chen 836b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 837b2ed11e2SEddie Huang int num, int left_num) 838ce38815dSXudong Chen { 839ce38815dSXudong Chen u16 addr_reg; 840b2ed11e2SEddie Huang u16 start_reg; 841ce38815dSXudong Chen u16 control_reg; 842b2ed11e2SEddie Huang u16 restart_flag = 0; 8438426fe70SQii Wang u16 dma_sync = 0; 844f4f4fed6SLiguo Zhang u32 reg_4g_mode; 845fc66b39fSJun Gao u8 *dma_rd_buf = NULL; 846fc66b39fSJun Gao u8 *dma_wr_buf = NULL; 847ce38815dSXudong Chen dma_addr_t rpaddr = 0; 848ce38815dSXudong Chen dma_addr_t wpaddr = 0; 849ce38815dSXudong Chen int ret; 850ce38815dSXudong Chen 851ce38815dSXudong Chen i2c->irq_stat = 0; 852ce38815dSXudong Chen 853173b77e8SLiguo Zhang if (i2c->auto_restart) 854b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 855b2ed11e2SEddie Huang 856ce38815dSXudong Chen reinit_completion(&i2c->msg_complete); 857ce38815dSXudong Chen 858bc6eaf17SQii Wang control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) & 859ce38815dSXudong Chen ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS); 86063ce8e3dSQii Wang if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1)) 861ce38815dSXudong Chen control_reg |= I2C_CONTROL_RS; 862ce38815dSXudong Chen 863ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) 864ce38815dSXudong Chen control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; 865ce38815dSXudong Chen 866bc6eaf17SQii Wang mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL); 867ce38815dSXudong Chen 8680d47ce21SWolfram Sang addr_reg = i2c_8bit_addr_from_msg(msgs); 869bc6eaf17SQii Wang mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR); 870ce38815dSXudong Chen 871ce38815dSXudong Chen /* Clear interrupt status */ 872bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 873cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT); 874bc6eaf17SQii Wang 875bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR); 876ce38815dSXudong Chen 877ce38815dSXudong Chen /* Enable interrupt */ 878bc6eaf17SQii Wang mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 879cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK); 880ce38815dSXudong Chen 881ce38815dSXudong Chen /* Set transfer and transaction len */ 882ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WRRD) { 883173b77e8SLiguo Zhang if (i2c->dev_comp->aux_len_reg) { 884bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 885bc6eaf17SQii Wang mtk_i2c_writew(i2c, (msgs + 1)->len, 886173b77e8SLiguo Zhang OFFSET_TRANSFER_LEN_AUX); 887173b77e8SLiguo Zhang } else { 888bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8, 889bc6eaf17SQii Wang OFFSET_TRANSFER_LEN); 890173b77e8SLiguo Zhang } 891bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN); 892ce38815dSXudong Chen } else { 893bc6eaf17SQii Wang mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN); 894bc6eaf17SQii Wang mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN); 895ce38815dSXudong Chen } 896ce38815dSXudong Chen 8978426fe70SQii Wang if (i2c->dev_comp->apdma_sync) { 8988426fe70SQii Wang dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE; 8998426fe70SQii Wang if (i2c->op == I2C_MASTER_WRRD) 9008426fe70SQii Wang dma_sync |= I2C_DMA_DIR_CHANGE; 9018426fe70SQii Wang } 9028426fe70SQii Wang 903ce38815dSXudong Chen /* Prepare buffer data to start transfer */ 904ce38815dSXudong Chen if (i2c->op == I2C_MASTER_RD) { 905ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 9068426fe70SQii Wang writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON); 907fc66b39fSJun Gao 908bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 909fc66b39fSJun Gao if (!dma_rd_buf) 910ce38815dSXudong Chen return -ENOMEM; 911f4f4fed6SLiguo Zhang 912fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 913fc66b39fSJun Gao msgs->len, DMA_FROM_DEVICE); 914fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, rpaddr)) { 915fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false); 916fc66b39fSJun Gao 917fc66b39fSJun Gao return -ENOMEM; 918fc66b39fSJun Gao } 919fc66b39fSJun Gao 920908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 921908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 922f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 923f4f4fed6SLiguo Zhang } 924f4f4fed6SLiguo Zhang 925ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 926ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 927ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_WR) { 928ce38815dSXudong Chen writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); 9298426fe70SQii Wang writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON); 930fc66b39fSJun Gao 931bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 932fc66b39fSJun Gao if (!dma_wr_buf) 933ce38815dSXudong Chen return -ENOMEM; 934f4f4fed6SLiguo Zhang 935fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 936fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 937fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 938fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 939fc66b39fSJun Gao 940fc66b39fSJun Gao return -ENOMEM; 941fc66b39fSJun Gao } 942fc66b39fSJun Gao 943908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 944908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 945f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 946f4f4fed6SLiguo Zhang } 947f4f4fed6SLiguo Zhang 948ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 949ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 950ce38815dSXudong Chen } else { 951ce38815dSXudong Chen writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); 9528426fe70SQii Wang writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON); 953fc66b39fSJun Gao 954bc1a7f75SHsin-Yi Wang dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1); 955fc66b39fSJun Gao if (!dma_wr_buf) 956ce38815dSXudong Chen return -ENOMEM; 957fc66b39fSJun Gao 958fc66b39fSJun Gao wpaddr = dma_map_single(i2c->dev, dma_wr_buf, 959fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 960fc66b39fSJun Gao if (dma_mapping_error(i2c->dev, wpaddr)) { 961fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 962fc66b39fSJun Gao 963fc66b39fSJun Gao return -ENOMEM; 964fc66b39fSJun Gao } 965fc66b39fSJun Gao 966bc1a7f75SHsin-Yi Wang dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1); 967fc66b39fSJun Gao if (!dma_rd_buf) { 968fc66b39fSJun Gao dma_unmap_single(i2c->dev, wpaddr, 969fc66b39fSJun Gao msgs->len, DMA_TO_DEVICE); 970fc66b39fSJun Gao 971fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 972fc66b39fSJun Gao 973fc66b39fSJun Gao return -ENOMEM; 974fc66b39fSJun Gao } 975fc66b39fSJun Gao 976fc66b39fSJun Gao rpaddr = dma_map_single(i2c->dev, dma_rd_buf, 977ce38815dSXudong Chen (msgs + 1)->len, 978ce38815dSXudong Chen DMA_FROM_DEVICE); 979ce38815dSXudong Chen if (dma_mapping_error(i2c->dev, rpaddr)) { 980ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 981ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 982fc66b39fSJun Gao 983fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false); 984fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false); 985fc66b39fSJun Gao 986ce38815dSXudong Chen return -ENOMEM; 987ce38815dSXudong Chen } 988f4f4fed6SLiguo Zhang 989908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 990908d9843SQii Wang reg_4g_mode = upper_32_bits(wpaddr); 991f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE); 992f4f4fed6SLiguo Zhang 993908d9843SQii Wang reg_4g_mode = upper_32_bits(rpaddr); 994f4f4fed6SLiguo Zhang writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE); 995f4f4fed6SLiguo Zhang } 996f4f4fed6SLiguo Zhang 997ce38815dSXudong Chen writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 998ce38815dSXudong Chen writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 999ce38815dSXudong Chen writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 1000ce38815dSXudong Chen writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN); 1001ce38815dSXudong Chen } 1002ce38815dSXudong Chen 1003ce38815dSXudong Chen writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN); 1004b2ed11e2SEddie Huang 1005173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1006b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START; 1007b2ed11e2SEddie Huang } else { 1008b2ed11e2SEddie Huang start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG; 1009b2ed11e2SEddie Huang if (left_num >= 1) 1010b2ed11e2SEddie Huang start_reg |= I2C_RS_MUL_CNFG; 1011b2ed11e2SEddie Huang } 1012bc6eaf17SQii Wang mtk_i2c_writew(i2c, start_reg, OFFSET_START); 1013ce38815dSXudong Chen 1014ce38815dSXudong Chen ret = wait_for_completion_timeout(&i2c->msg_complete, 1015ce38815dSXudong Chen i2c->adap.timeout); 1016ce38815dSXudong Chen 1017ce38815dSXudong Chen /* Clear interrupt mask */ 1018bc6eaf17SQii Wang mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR | 1019cad6dc5dSQii Wang I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK); 1020ce38815dSXudong Chen 1021ce38815dSXudong Chen if (i2c->op == I2C_MASTER_WR) { 1022ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, 1023ce38815dSXudong Chen msgs->len, DMA_TO_DEVICE); 1024fc66b39fSJun Gao 1025fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1026ce38815dSXudong Chen } else if (i2c->op == I2C_MASTER_RD) { 1027ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, 1028ce38815dSXudong Chen msgs->len, DMA_FROM_DEVICE); 1029fc66b39fSJun Gao 1030fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true); 1031ce38815dSXudong Chen } else { 1032ce38815dSXudong Chen dma_unmap_single(i2c->dev, wpaddr, msgs->len, 1033ce38815dSXudong Chen DMA_TO_DEVICE); 1034ce38815dSXudong Chen dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, 1035ce38815dSXudong Chen DMA_FROM_DEVICE); 1036fc66b39fSJun Gao 1037fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true); 1038fc66b39fSJun Gao i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true); 1039ce38815dSXudong Chen } 1040ce38815dSXudong Chen 1041ce38815dSXudong Chen if (ret == 0) { 1042ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr); 1043ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1044ce38815dSXudong Chen return -ETIMEDOUT; 1045ce38815dSXudong Chen } 1046ce38815dSXudong Chen 1047ce38815dSXudong Chen if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { 1048ce38815dSXudong Chen dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr); 1049ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1050ce38815dSXudong Chen return -ENXIO; 1051ce38815dSXudong Chen } 1052ce38815dSXudong Chen 1053ce38815dSXudong Chen return 0; 1054ce38815dSXudong Chen } 1055ce38815dSXudong Chen 1056ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap, 1057ce38815dSXudong Chen struct i2c_msg msgs[], int num) 1058ce38815dSXudong Chen { 1059ce38815dSXudong Chen int ret; 1060ce38815dSXudong Chen int left_num = num; 1061ce38815dSXudong Chen struct mtk_i2c *i2c = i2c_get_adapdata(adap); 1062ce38815dSXudong Chen 1063ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1064ce38815dSXudong Chen if (ret) 1065ce38815dSXudong Chen return ret; 1066ce38815dSXudong Chen 1067173b77e8SLiguo Zhang i2c->auto_restart = i2c->dev_comp->auto_restart; 1068173b77e8SLiguo Zhang 1069173b77e8SLiguo Zhang /* checking if we can skip restart and optimize using WRRD mode */ 1070173b77e8SLiguo Zhang if (i2c->auto_restart && num == 2) { 1071173b77e8SLiguo Zhang if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) && 1072173b77e8SLiguo Zhang msgs[0].addr == msgs[1].addr) { 1073173b77e8SLiguo Zhang i2c->auto_restart = 0; 1074173b77e8SLiguo Zhang } 1075173b77e8SLiguo Zhang } 1076173b77e8SLiguo Zhang 107763ce8e3dSQii Wang if (i2c->auto_restart && num >= 2 && 107863ce8e3dSQii Wang i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 10798378d01fSLiguo Zhang /* ignore the first restart irq after the master code, 10808378d01fSLiguo Zhang * otherwise the first transfer will be discarded. 10818378d01fSLiguo Zhang */ 10828378d01fSLiguo Zhang i2c->ignore_restart_irq = true; 10838378d01fSLiguo Zhang else 10848378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 10858378d01fSLiguo Zhang 1086b2ed11e2SEddie Huang while (left_num--) { 1087ce38815dSXudong Chen if (!msgs->buf) { 1088ce38815dSXudong Chen dev_dbg(i2c->dev, "data buffer is NULL.\n"); 1089ce38815dSXudong Chen ret = -EINVAL; 1090ce38815dSXudong Chen goto err_exit; 1091ce38815dSXudong Chen } 1092ce38815dSXudong Chen 1093ce38815dSXudong Chen if (msgs->flags & I2C_M_RD) 1094ce38815dSXudong Chen i2c->op = I2C_MASTER_RD; 1095ce38815dSXudong Chen else 1096ce38815dSXudong Chen i2c->op = I2C_MASTER_WR; 1097ce38815dSXudong Chen 1098173b77e8SLiguo Zhang if (!i2c->auto_restart) { 1099ce38815dSXudong Chen if (num > 1) { 1100ce38815dSXudong Chen /* combined two messages into one transaction */ 1101ce38815dSXudong Chen i2c->op = I2C_MASTER_WRRD; 1102ce38815dSXudong Chen left_num--; 1103ce38815dSXudong Chen } 1104b2ed11e2SEddie Huang } 1105ce38815dSXudong Chen 1106ce38815dSXudong Chen /* always use DMA mode. */ 1107b2ed11e2SEddie Huang ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num); 1108ce38815dSXudong Chen if (ret < 0) 1109ce38815dSXudong Chen goto err_exit; 1110ce38815dSXudong Chen 1111b2ed11e2SEddie Huang msgs++; 1112b2ed11e2SEddie Huang } 1113ce38815dSXudong Chen /* the return value is number of executed messages */ 1114ce38815dSXudong Chen ret = num; 1115ce38815dSXudong Chen 1116ce38815dSXudong Chen err_exit: 1117ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1118ce38815dSXudong Chen return ret; 1119ce38815dSXudong Chen } 1120ce38815dSXudong Chen 1121ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id) 1122ce38815dSXudong Chen { 1123ce38815dSXudong Chen struct mtk_i2c *i2c = dev_id; 1124b2ed11e2SEddie Huang u16 restart_flag = 0; 112528c0a843SEddie Huang u16 intr_stat; 1126b2ed11e2SEddie Huang 1127173b77e8SLiguo Zhang if (i2c->auto_restart) 1128b2ed11e2SEddie Huang restart_flag = I2C_RS_TRANSFER; 1129ce38815dSXudong Chen 1130bc6eaf17SQii Wang intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT); 1131bc6eaf17SQii Wang mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT); 1132ce38815dSXudong Chen 113328c0a843SEddie Huang /* 113428c0a843SEddie Huang * when occurs ack error, i2c controller generate two interrupts 113528c0a843SEddie Huang * first is the ack error interrupt, then the complete interrupt 113628c0a843SEddie Huang * i2c->irq_stat need keep the two interrupt value. 113728c0a843SEddie Huang */ 113828c0a843SEddie Huang i2c->irq_stat |= intr_stat; 11398378d01fSLiguo Zhang 11408378d01fSLiguo Zhang if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) { 11418378d01fSLiguo Zhang i2c->ignore_restart_irq = false; 11428378d01fSLiguo Zhang i2c->irq_stat = 0; 1143bc6eaf17SQii Wang mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG | 1144bc6eaf17SQii Wang I2C_TRANSAC_START, OFFSET_START); 11458378d01fSLiguo Zhang } else { 114628c0a843SEddie Huang if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag)) 1147ce38815dSXudong Chen complete(&i2c->msg_complete); 11488378d01fSLiguo Zhang } 1149ce38815dSXudong Chen 1150ce38815dSXudong Chen return IRQ_HANDLED; 1151ce38815dSXudong Chen } 1152ce38815dSXudong Chen 1153ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap) 1154ce38815dSXudong Chen { 115562931ac2SFabien Parent if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN)) 1156abf4923eSHsin-Yi Wang return I2C_FUNC_I2C | 1157abf4923eSHsin-Yi Wang (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1158abf4923eSHsin-Yi Wang else 1159ce38815dSXudong Chen return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1160ce38815dSXudong Chen } 1161ce38815dSXudong Chen 1162ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = { 1163ce38815dSXudong Chen .master_xfer = mtk_i2c_transfer, 1164ce38815dSXudong Chen .functionality = mtk_i2c_functionality, 1165ce38815dSXudong Chen }; 1166ce38815dSXudong Chen 1167f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) 1168ce38815dSXudong Chen { 1169ce38815dSXudong Chen int ret; 1170ce38815dSXudong Chen 1171ce38815dSXudong Chen ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); 1172ce38815dSXudong Chen if (ret < 0) 117390224e64SAndy Shevchenko i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ; 1174ce38815dSXudong Chen 1175f2326401SJun Gao ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div); 1176ce38815dSXudong Chen if (ret < 0) 1177ce38815dSXudong Chen return ret; 1178ce38815dSXudong Chen 1179f2326401SJun Gao if (i2c->clk_src_div == 0) 1180ce38815dSXudong Chen return -EINVAL; 1181ce38815dSXudong Chen 1182ce38815dSXudong Chen i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); 1183ce38815dSXudong Chen i2c->use_push_pull = 1184ce38815dSXudong Chen of_property_read_bool(np, "mediatek,use-push-pull"); 1185ce38815dSXudong Chen 1186a80f2494SQii Wang i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); 1187a80f2494SQii Wang 1188ce38815dSXudong Chen return 0; 1189ce38815dSXudong Chen } 1190ce38815dSXudong Chen 1191ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev) 1192ce38815dSXudong Chen { 1193ce38815dSXudong Chen int ret = 0; 1194ce38815dSXudong Chen struct mtk_i2c *i2c; 1195ce38815dSXudong Chen struct clk *clk; 1196ce38815dSXudong Chen struct resource *res; 1197ce38815dSXudong Chen int irq; 1198ce38815dSXudong Chen 1199ce38815dSXudong Chen i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 1200ce38815dSXudong Chen if (!i2c) 1201ce38815dSXudong Chen return -ENOMEM; 1202ce38815dSXudong Chen 1203ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1204ce38815dSXudong Chen i2c->base = devm_ioremap_resource(&pdev->dev, res); 1205ce38815dSXudong Chen if (IS_ERR(i2c->base)) 1206ce38815dSXudong Chen return PTR_ERR(i2c->base); 1207ce38815dSXudong Chen 1208ce38815dSXudong Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1209ce38815dSXudong Chen i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); 1210ce38815dSXudong Chen if (IS_ERR(i2c->pdmabase)) 1211ce38815dSXudong Chen return PTR_ERR(i2c->pdmabase); 1212ce38815dSXudong Chen 1213ce38815dSXudong Chen irq = platform_get_irq(pdev, 0); 1214*58fb7c64SSergey Shtylyov if (irq < 0) 1215ce38815dSXudong Chen return irq; 1216ce38815dSXudong Chen 1217ce38815dSXudong Chen init_completion(&i2c->msg_complete); 1218ce38815dSXudong Chen 12196e29577fSRyder Lee i2c->dev_comp = of_device_get_match_data(&pdev->dev); 1220ce38815dSXudong Chen i2c->adap.dev.of_node = pdev->dev.of_node; 1221ce38815dSXudong Chen i2c->dev = &pdev->dev; 1222ce38815dSXudong Chen i2c->adap.dev.parent = &pdev->dev; 1223ce38815dSXudong Chen i2c->adap.owner = THIS_MODULE; 1224ce38815dSXudong Chen i2c->adap.algo = &mtk_i2c_algorithm; 1225ce38815dSXudong Chen i2c->adap.quirks = i2c->dev_comp->quirks; 1226ce38815dSXudong Chen i2c->adap.timeout = 2 * HZ; 1227ce38815dSXudong Chen i2c->adap.retries = 1; 12289029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = devm_regulator_get_optional(&pdev->dev, "vbus"); 12299029b9b2SHsin-Yi Wang if (IS_ERR(i2c->adap.bus_regulator)) { 12309029b9b2SHsin-Yi Wang if (PTR_ERR(i2c->adap.bus_regulator) == -ENODEV) 12319029b9b2SHsin-Yi Wang i2c->adap.bus_regulator = NULL; 12329029b9b2SHsin-Yi Wang else 12339029b9b2SHsin-Yi Wang return PTR_ERR(i2c->adap.bus_regulator); 12349029b9b2SHsin-Yi Wang } 1235ce38815dSXudong Chen 12365a10e7d7SJun Gao ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c); 12375a10e7d7SJun Gao if (ret) 12385a10e7d7SJun Gao return -EINVAL; 12395a10e7d7SJun Gao 1240ce38815dSXudong Chen if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c) 1241ce38815dSXudong Chen return -EINVAL; 1242ce38815dSXudong Chen 1243ce38815dSXudong Chen i2c->clk_main = devm_clk_get(&pdev->dev, "main"); 1244ce38815dSXudong Chen if (IS_ERR(i2c->clk_main)) { 1245ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get main clock\n"); 1246ce38815dSXudong Chen return PTR_ERR(i2c->clk_main); 1247ce38815dSXudong Chen } 1248ce38815dSXudong Chen 1249ce38815dSXudong Chen i2c->clk_dma = devm_clk_get(&pdev->dev, "dma"); 1250ce38815dSXudong Chen if (IS_ERR(i2c->clk_dma)) { 1251ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get dma clock\n"); 1252ce38815dSXudong Chen return PTR_ERR(i2c->clk_dma); 1253ce38815dSXudong Chen } 1254ce38815dSXudong Chen 1255cad6dc5dSQii Wang i2c->clk_arb = devm_clk_get(&pdev->dev, "arb"); 1256cad6dc5dSQii Wang if (IS_ERR(i2c->clk_arb)) 1257cad6dc5dSQii Wang i2c->clk_arb = NULL; 1258cad6dc5dSQii Wang 1259ce38815dSXudong Chen clk = i2c->clk_main; 1260ce38815dSXudong Chen if (i2c->have_pmic) { 1261ce38815dSXudong Chen i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic"); 1262ce38815dSXudong Chen if (IS_ERR(i2c->clk_pmic)) { 1263ce38815dSXudong Chen dev_err(&pdev->dev, "cannot get pmic clock\n"); 1264ce38815dSXudong Chen return PTR_ERR(i2c->clk_pmic); 1265ce38815dSXudong Chen } 1266ce38815dSXudong Chen clk = i2c->clk_pmic; 1267ce38815dSXudong Chen } 1268ce38815dSXudong Chen 1269ce38815dSXudong Chen strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1270ce38815dSXudong Chen 1271f2326401SJun Gao ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk)); 1272ce38815dSXudong Chen if (ret) { 1273ce38815dSXudong Chen dev_err(&pdev->dev, "Failed to set the speed.\n"); 1274ce38815dSXudong Chen return -EINVAL; 1275ce38815dSXudong Chen } 1276ce38815dSXudong Chen 1277908d9843SQii Wang if (i2c->dev_comp->max_dma_support > 32) { 1278908d9843SQii Wang ret = dma_set_mask(&pdev->dev, 1279908d9843SQii Wang DMA_BIT_MASK(i2c->dev_comp->max_dma_support)); 1280f4f4fed6SLiguo Zhang if (ret) { 1281f4f4fed6SLiguo Zhang dev_err(&pdev->dev, "dma_set_mask return error.\n"); 1282f4f4fed6SLiguo Zhang return ret; 1283f4f4fed6SLiguo Zhang } 1284f4f4fed6SLiguo Zhang } 1285f4f4fed6SLiguo Zhang 1286ce38815dSXudong Chen ret = mtk_i2c_clock_enable(i2c); 1287ce38815dSXudong Chen if (ret) { 1288ce38815dSXudong Chen dev_err(&pdev->dev, "clock enable failed!\n"); 1289ce38815dSXudong Chen return ret; 1290ce38815dSXudong Chen } 1291ce38815dSXudong Chen mtk_i2c_init_hw(i2c); 1292ce38815dSXudong Chen mtk_i2c_clock_disable(i2c); 1293ce38815dSXudong Chen 1294ce38815dSXudong Chen ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq, 1295de96c394SQii Wang IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, 12967fb9dc81SQii Wang dev_name(&pdev->dev), i2c); 1297ce38815dSXudong Chen if (ret < 0) { 1298ce38815dSXudong Chen dev_err(&pdev->dev, 1299ce38815dSXudong Chen "Request I2C IRQ %d fail\n", irq); 1300ce38815dSXudong Chen return ret; 1301ce38815dSXudong Chen } 1302ce38815dSXudong Chen 1303ce38815dSXudong Chen i2c_set_adapdata(&i2c->adap, i2c); 1304ce38815dSXudong Chen ret = i2c_add_adapter(&i2c->adap); 1305ea734404SWolfram Sang if (ret) 1306ce38815dSXudong Chen return ret; 1307ce38815dSXudong Chen 1308ce38815dSXudong Chen platform_set_drvdata(pdev, i2c); 1309ce38815dSXudong Chen 1310ce38815dSXudong Chen return 0; 1311ce38815dSXudong Chen } 1312ce38815dSXudong Chen 1313ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev) 1314ce38815dSXudong Chen { 1315ce38815dSXudong Chen struct mtk_i2c *i2c = platform_get_drvdata(pdev); 1316ce38815dSXudong Chen 1317ce38815dSXudong Chen i2c_del_adapter(&i2c->adap); 1318ce38815dSXudong Chen 1319ce38815dSXudong Chen return 0; 1320ce38815dSXudong Chen } 1321ce38815dSXudong Chen 132209027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP 1323de96c394SQii Wang static int mtk_i2c_suspend_noirq(struct device *dev) 1324de96c394SQii Wang { 1325de96c394SQii Wang struct mtk_i2c *i2c = dev_get_drvdata(dev); 1326de96c394SQii Wang 1327de96c394SQii Wang i2c_mark_adapter_suspended(&i2c->adap); 1328de96c394SQii Wang 1329de96c394SQii Wang return 0; 1330de96c394SQii Wang } 1331de96c394SQii Wang 1332de96c394SQii Wang static int mtk_i2c_resume_noirq(struct device *dev) 133309027e08SLiguo Zhang { 1334f6762cedSJun Gao int ret; 133509027e08SLiguo Zhang struct mtk_i2c *i2c = dev_get_drvdata(dev); 133609027e08SLiguo Zhang 1337f6762cedSJun Gao ret = mtk_i2c_clock_enable(i2c); 1338f6762cedSJun Gao if (ret) { 1339f6762cedSJun Gao dev_err(dev, "clock enable failed!\n"); 1340f6762cedSJun Gao return ret; 1341f6762cedSJun Gao } 1342f6762cedSJun Gao 134309027e08SLiguo Zhang mtk_i2c_init_hw(i2c); 134409027e08SLiguo Zhang 1345f6762cedSJun Gao mtk_i2c_clock_disable(i2c); 1346f6762cedSJun Gao 1347de96c394SQii Wang i2c_mark_adapter_resumed(&i2c->adap); 1348de96c394SQii Wang 134909027e08SLiguo Zhang return 0; 135009027e08SLiguo Zhang } 135109027e08SLiguo Zhang #endif 135209027e08SLiguo Zhang 135309027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = { 1354de96c394SQii Wang SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq, 1355de96c394SQii Wang mtk_i2c_resume_noirq) 135609027e08SLiguo Zhang }; 135709027e08SLiguo Zhang 1358ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = { 1359ce38815dSXudong Chen .probe = mtk_i2c_probe, 1360ce38815dSXudong Chen .remove = mtk_i2c_remove, 1361ce38815dSXudong Chen .driver = { 1362ce38815dSXudong Chen .name = I2C_DRV_NAME, 136309027e08SLiguo Zhang .pm = &mtk_i2c_pm, 1364ce38815dSXudong Chen .of_match_table = of_match_ptr(mtk_i2c_of_match), 1365ce38815dSXudong Chen }, 1366ce38815dSXudong Chen }; 1367ce38815dSXudong Chen 1368ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver); 1369ce38815dSXudong Chen 1370ce38815dSXudong Chen MODULE_LICENSE("GPL v2"); 1371ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); 1372ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); 1373