blob: bac157ccd109781b94923489863f00c87f50bdf3 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2014 MediaTek Inc.
3 * Author: Xudong.chen <xudong.chen@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef __I2C_MTK_H__
15#define __I2C_MTK_H__
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/i2c.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/sched/clock.h>
24//#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/errno.h>
27#include <linux/err.h>
28#include <linux/device.h>
29#include <linux/platform_device.h>
30#include <linux/wait.h>
31#include <linux/mm.h>
32#include <linux/dma-mapping.h>
33#include <linux/scatterlist.h>
34#include <linux/io.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
37#include <linux/clk.h>
38
39#define I2C_DEBUG_FS
40
41#define I3C_EN (0x01 << 15)
42#define I3C_UNLOCK_HFIFO (0x01 << 15)
43#define I3C_NINTH_BIT (0x02 << 8)
44#define MASTER_CODE 0x08
45#define I2C_HFIFO_ADDR_CLR 0x2
46
47#define I2C_HS_HOLD_SEL (0x01 << 15)
48#define I2C_HS_HOLD_TIME (0x01 << 2)
49
50#define I2C_BUS_ERR (0x01 << 8)
51#define I2C_IBI (0x01 << 7)
52#define I2C_DMAERR (0x01 << 6)
53#define I2C_TIMEOUT (0x01 << 5)
54#define I2C_RS_MULTI (0x01 << 4)
55#define I2C_ARB_LOST (0x01 << 3)
56#define I2C_HS_NACKERR (0x01 << 2)
57#define I2C_ACKERR (0x01 << 1)
58#define I2C_TRANSAC_COMP (0x01 << 0)
59#define I2C_INTR_ALL (I2C_BUS_ERR | I2C_IBI | I2C_DMAERR | \
60 I2C_TIMEOUT | I2C_RS_MULTI | \
61 I2C_ARB_LOST | I2C_HS_NACKERR | \
62 I2C_ACKERR | I2C_TRANSAC_COMP)
63#define I2C_TRANSAC_START (0x01 << 0)
64#define I2C_RESUME_ARBIT (0x01 << 1)
65#define I2C_TIMING_STEP_DIV_MASK (0x3f << 0)
66#define I2C_TIMING_SAMPLE_COUNT_MASK (0x7 << 0)
67#define I2C_TIMING_SAMPLE_DIV_MASK (0x7 << 8)
68#define I2C_TIMING_DATA_READ_MASK (0x7 << 12)
69#define I2C_DCM_DISABLE 0x0000
70#define I2C_DCM_ENABLE 0x0007
71#define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
72#define I2C_IO_CONFIG_PUSH_PULL 0x0000
73#define I2C_IO_CONFIG_OPEN_DRAIN_AED 0x0000
74#define I2C_IO_CONFIG_PUSH_PULL_AED 0x0000
75#define I2C_IO_CONFIG_AED_MASK (0xfff << 4)
76#define I2C_SOFT_RST 0x0001
77#define I2C_FIFO_ADDR_CLR 0x0001
78#define I2C_FIFO_ADDR_CLR_MCH 0x0004
79#define I2C_DELAY_LEN 0x000A/* not use 0x02 */
80#define I2C_ST_START_CON 0x8001
81#define I2C_FS_START_CON 0x1800
82#define I2C_TIME_CLR_VALUE 0x0000
83#define I2C_TIME_DEFAULT_VALUE 0x0001
84#define I2C_HS_SPEED 0x0080
85#define I2C_TIMEOUT_EN 0x0001
86#define I2C_ROLLBACK 0x0001
87#define I2C_SHADOW_REG_MODE 0x0002
88
89#define I2C_HS_NACK_DET_EN (0x1 << 1)
90
91#define I2C_DMA_CON_TX 0x0000
92#define I2C_DMA_CON_RX 0x0001
93#define I2C_DMA_START_EN 0x0001
94#define I2C_DMA_INT_FLAG_NONE 0x0000
95#define I2C_DMA_CLR_FLAG 0x0000
96#define I2C_DMA_WARM_RST 0x0001
97#define I2C_DMA_4G_MODE 0x0001
98
99#define I2C_DEFAUT_SPEED 100000/* hz */
100#define MAX_FS_MODE_SPEED 400000/* hz */
101#define MAX_FS_PLUS_MODE_SPEED 1000000/* hz */
102#define MAX_HS_MODE_SPEED 3400000/* hz */
103#define MAX_DMA_TRANS_SIZE 4096/* 255 */
104#define MAX_CLOCK_DIV 8
105#define MAX_SAMPLE_CNT_DIV 8
106#define MAX_STEP_CNT_DIV 64
107#define MAX_HS_STEP_CNT_DIV 8
108
109#define HALF_DUTY_CYCLE 50
110#define DUTY_CYCLE 45
111
112#define I2C_CONTROL_RS (0x1 << 1)
113#define I2C_CONTROL_DMA_EN (0x1 << 2)
114#define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
115#define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
116#define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
117#define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
118#define I2C_CONTROL_DMAACK_EN (0x1 << 8)
119#define I2C_CONTROL_ASYNC_MODE (0x1 << 9)
120#define I2C_CONTROL_WRAPPER (0x1 << 0)
121#define I2C_MCU_INTR_EN 0x1
122#define I2C_CCU_INTR_EN 0x2
123
124#define I2C_RECORD_LEN 10
125#define I2C_MAX_CHANNEL 10
126
127#define MAX_SCL_LOW_TIME 2/* unit: milli-second */
128#define LSAMPLE_MSK 0x1C0
129#define LSTEP_MSK 0x3F
130
131#define I2C_DRV_NAME "mt-i2c"
132#define I2CTAG "[I2C]"
133
134enum DMA_REGS_OFFSET {
135 OFFSET_INT_FLAG = 0x0,
136 OFFSET_INT_EN = 0x04,
137 OFFSET_EN = 0x08,
138 OFFSET_RST = 0x0C,
139 OFFSET_STOP = 0x10,
140 OFFSET_FLUSH = 0x14,
141 OFFSET_CON = 0x18,
142 OFFSET_TX_MEM_ADDR = 0x1C,
143 OFFSET_RX_MEM_ADDR = 0x20,
144 OFFSET_TX_LEN = 0x24,
145 OFFSET_RX_LEN = 0x28,
146 OFFSET_INT_BUF_SIZE = 0x38,
147 OFFSET_DEBUG_STA = 0x50,
148 OFFSET_TX_MEM_ADDR2 = 0x54,
149 OFFSET_RX_MEM_ADDR2 = 0x58,
150 OFFSET_USR_DEF_ADDR = 0x5C,
151 OFFSET_USR_DEF_CTRL = 0x60,
152};
153
154struct i2c_dma_info {
155 unsigned long base;
156 unsigned int int_flag;
157 unsigned int int_en;
158 unsigned int en;
159 unsigned int rst;
160 unsigned int stop;
161 unsigned int flush;
162 unsigned int con;
163 unsigned int tx_mem_addr;
164 unsigned int rx_mem_addr;
165 unsigned int tx_len;
166 unsigned int rx_len;
167 unsigned int int_buf_size;
168 unsigned int debug_sta;
169 unsigned int tx_mem_addr2;
170 unsigned int rx_mem_addr2;
171 unsigned int usr_def_addr;
172 unsigned int use_def_addr;
173};
174
175enum i2c_trans_st_rs {
176 I2C_TRANS_STOP = 0,
177 I2C_TRANS_REPEATED_START,
178};
179
180enum {
181 FS_MODE,
182 HS_MODE,
183};
184
185enum mt_trans_op {
186 I2C_MASTER_WR = 1,
187 I2C_MASTER_RD,
188 I2C_MASTER_WRRD,
189 I2C_MASTER_MULTI_WR,
190};
191
192enum I2C_REGS_OFFSET {
193 OFFSET_DATA_PORT = 0x0,
194 OFFSET_SLAVE_ADDR = 0x04,
195 OFFSET_INTR_MASK = 0x08,
196 OFFSET_INTR_STAT = 0x0c,
197 OFFSET_CONTROL = 0x10,
198 OFFSET_TRANSFER_LEN = 0x14,
199 OFFSET_TRANSAC_LEN = 0x18,
200 OFFSET_DELAY_LEN = 0x1c,
201 OFFSET_TIMING = 0x20,
202 OFFSET_START = 0x24,
203 OFFSET_EXT_CONF = 0x28,
204 OFFSET_LTIMING = 0x2c,
205 OFFSET_FIFO_STAT = 0x30,
206 OFFSET_FIFO_THRESH = 0x34,
207 OFFSET_FIFO_ADDR_CLR = 0x38,
208 OFFSET_IO_CONFIG = 0x40,
209 OFFSET_RSV_DEBUG = 0x44,
210 OFFSET_HS = 0x48,
211 OFFSET_SOFTRESET = 0x50,
212 OFFSET_DCM_EN = 0x54,
213 OFFSET_PATH_DIR = 0x60,
214 OFFSET_DEBUGSTAT = 0x64,
215 OFFSET_DEBUGCTRL = 0x68,
216 OFFSET_TRANSFER_LEN_AUX = 0x6c,
217 OFFSET_CLOCK_DIV = 0x70,
218
219 /* v2 add */
220 OFFSET_HW_TIMEOUT = 0xfff,
221 OFFSET_MCU_INTR = 0xfff,
222 OFFSET_TRAFFIC = 0xfff,
223 OFFSET_COMMAND = 0xfff,
224 OFFSET_CRC_CODE_ = 0xfff,
225 OFFSET_TERNARY = 0xfff,
226 OFFSET_IBI_TIMING = 0xfff,
227 OFFSET_SHAPE = 0xfff,
228 OFFSET_HFIFO_DATA = 0xfff,
229 OFFSET_ERROR = 0xfff,
230 OFFSET_DELAY_STEP = 0xfff,
231 OFFSET_DELAY_SAMPLE = 0xfff,
232 OFFSET_DMA_INFO = 0xfff,
233 OFFSET_IRQ_INFO = 0xfff,
234 OFFSET_DMA_FSM_DEBUG = 0xfff,
235 OFFSET_HFIFO_STAT = 0xfff,
236 OFFSET_MULTI_DMA = 0xfff,
237 OFFSET_ROLLBACK = 0xfff,
238};
239
240enum I2C_REGS_OFFSET_V2 {
241 V2_OFFSET_DATA_PORT = 0x0,
242 V2_OFFSET_SLAVE_ADDR = 0x04,
243 V2_OFFSET_INTR_MASK = 0x08,
244 V2_OFFSET_INTR_STAT = 0x0c,
245 V2_OFFSET_CONTROL = 0x10,
246 V2_OFFSET_TRANSFER_LEN = 0x14,
247 V2_OFFSET_TRANSAC_LEN = 0x18,
248 V2_OFFSET_DELAY_LEN = 0x1c,
249 V2_OFFSET_TIMING = 0x20,
250 V2_OFFSET_START = 0x24,
251 V2_OFFSET_EXT_CONF = 0x28,
252 V2_OFFSET_LTIMING = 0x2c,
253 V2_OFFSET_FIFO_ADDR_CLR = 0x38,
254 V2_OFFSET_SOFTRESET = 0x50,
255
256 /* v2 use different offset */
257 V2_OFFSET_HS = 0x30,
258 V2_OFFSET_IO_CONFIG = 0x34,
259 V2_OFFSET_TRANSFER_LEN_AUX = 0x44,
260 V2_OFFSET_CLOCK_DIV = 0x48,
261 V2_OFFSET_HW_TIMEOUT = 0x4c,
262 V2_OFFSET_DEBUGSTAT = 0xe4,
263 V2_OFFSET_DEBUGCTRL = 0xe8,
264 V2_OFFSET_FIFO_STAT = 0xf4,
265 V2_OFFSET_FIFO_THRESH = 0xf8,
266 V2_OFFSET_AED_PATCH = 0x80,
267
268 /* v2 add */
269 V2_OFFSET_MCU_INTR = 0x40,
270 V2_OFFSET_TRAFFIC = 0x54,
271 V2_OFFSET_COMMAND = 0x58,
272 V2_OFFSET_CRC_CODE_ = 0x5c,
273 V2_OFFSET_TERNARY = 0x60,
274 V2_OFFSET_IBI_TIMING = 0x64,
275 V2_OFFSET_SHAPE = 0x6c,
276 V2_OFFSET_HFIFO_DATA = 0x70,
277 V2_OFFSET_ERROR = 0x84,
278 V2_OFFSET_DELAY_STEP = 0xd4,
279 V2_OFFSET_DELAY_SAMPLE = 0xd8,
280 V2_OFFSET_DMA_INFO = 0xdc,
281 V2_OFFSET_IRQ_INFO = 0xe0,
282 V2_OFFSET_DMA_FSM_DEBUG = 0xec,
283 V2_OFFSET_HFIFO_STAT = 0xfc,
284 V2_OFFSET_MULTI_DMA = 0xf8c,
285 V2_OFFSET_ROLLBACK = 0xf98,
286
287 /* not in v2 */
288 V2_OFFSET_DCM_EN = 0xfff,/*0x54*/
289 V2_OFFSET_PATH_DIR = 0xfff,/*0x60*/
290};
291
292struct i2c_info {
293 unsigned int slave_addr;
294 unsigned int intr_stat;
295 unsigned int control;
296 unsigned int fifo_stat;
297 unsigned int debug_stat;
298 unsigned int tmo;
299 unsigned long long end_time;
300};
301
302enum PERICFG_OFFSET {
303 OFFSET_PERI_I2C_MODE_ENABLE = 0x0410,
304};
305
306struct mt_i2c_data {
307 unsigned int clk_frequency; /* bus speed in Hz */
308 unsigned int flags;
309 unsigned int clk_src_div;
310};
311
312struct i2c_dma_buf {
313 u8 *vaddr;
314 dma_addr_t paddr;
315};
316
317struct mt_i2c_ext {
318#define I2C_A_FILTER_MSG 0x00000001
319 bool isEnable;
320 bool isFilterMsg;
321 bool is_ch_offset;
322 u32 timing;
323 u16 ch_offset;
324 u16 ch_offset_dma;
325};
326
327struct mtk_i2c_compatible {
328 unsigned char dma_support;
329 /* 0 : original; 1: 4gb support 2: 33bit support; 3: 36 bit support */
330 unsigned char idvfs_i2c;
331 /* compatible before chip, set 1 if no TRANSFER_LEN_AUX */
332 unsigned char set_dt_div;/* use dt to set div */
333 unsigned char check_max_freq;/* check max freq */
334 unsigned char set_ltiming;/* need to set LTIMING */
335 unsigned char set_aed;/* need to set AED */
336 unsigned char ver;/* controller version */
337 /* for constraint of SAMPLE_CNT_DIV and STEP_CNT_DIV of mt6765 */
338 /* 1, has-a-constraint; 0, no constraint */
339 unsigned char cnt_constraint;
340 u16 ext_time_config;
341 char clk_compatible[128];
342 u16 clk_sta_offset[I2C_MAX_CHANNEL];/* I2C clock status register */
343 u8 cg_bit[I2C_MAX_CHANNEL];/* i2c clock bit */
344 u32 clk_sel_offset;
345 u32 arbit_offset;
346};
347
348struct mt_i2c {
349 struct i2c_adapter adap;/* i2c host adapter */
350 struct device *dev;
351 wait_queue_head_t wait;/* i2c transfer wait queue */
352 /* set in i2c probe */
353 void __iomem *base;/* i2c base addr */
354 void __iomem *pdmabase;/* dma base address*/
355 void __iomem *gpiobase;/* gpio base address */
356 int irqnr; /* i2c interrupt number */
357 int id;
358 int scl_gpio_id; /* SCL GPIO number */
359 int sda_gpio_id; /* SDA GPIO number */
360 unsigned int gpio_start;
361 unsigned int mem_len;
362 unsigned int offset_eh_cfg;
363 unsigned int offset_pu_cfg;
364 unsigned int offset_rsel_cfg;
365 struct i2c_dma_buf dma_buf;/* memory alloc for DMA mode */
366 struct clk *clk_main;/* main clock for i2c bus */
367 struct clk *clk_dma;/* DMA clock for i2c via DMA */
368 struct clk *clk_pmic;/* PMIC clock for i2c from PMIC */
369 struct clk *clk_arb;/* Arbitrator clock for i2c */
370 struct clk *clk_pal;
371 bool have_pmic;/* can use i2c pins form PMIC */
372 bool have_dcm;/* HW DCM function */
373 bool use_push_pull;/* IO config push-pull mode */
374 bool appm;/* I2C for APPM */
375 bool gpupm;/* I2C for GPUPM */
376 bool buffermode; /* I2C Buffer mode support */
377 bool hs_only; /* I2C HS only */
378 /* set when doing the transfer */
379 u16 irq_stat; /* interrupt status */
380 u16 i3c_en; /* i3c enalbe */
381 unsigned int speed_hz;/* The speed in transfer */
382 unsigned int clk_src_div;
383 unsigned int aed;/* aed value from dt */
384 spinlock_t cg_lock;
385 int cg_cnt;
386 bool trans_stop;/* i2c transfer stop */
387 enum mt_trans_op op;
388 u16 total_len;
389 u16 msg_len;
390 u8 *msg_buf; /* pointer to msg data */
391 u16 msg_aux_len;/* WRRD mode to set AUX_LEN register */
392 u16 addr;/* 7bit slave address, without read/write bit */
393 u16 timing_reg;
394 u16 ltiming_reg;
395 u16 high_speed_reg;
396 u16 clk_sta_offset;
397 u8 cg_bit;
398 bool is_hw_trig;
399 bool is_ccu_trig;
400 bool suspended;
401 int rec_idx;/* next record idx */
402 u32 ch_offset_default;
403 u32 ch_offset;
404 u32 ch_offset_dma_default;
405 u32 ch_offset_dma;
406 bool skip_scp_sema;
407 bool has_ccu;
408 u32 ccu_offset;
409 unsigned long main_clk;
410 struct mutex i2c_mutex;
411 struct mt_i2c_ext ext_data;
412 const struct mtk_i2c_compatible *dev_comp;
413 struct i2c_info rec_info[I2C_RECORD_LEN];
414};
415
416#if defined(CONFIG_MTK_FPGA) || defined(CONFIG_FPGA_EARLY_PORTING)
417#define CONFIG_MT_I2C_FPGA_ENABLE
418#endif
419
420#if (defined(CONFIG_MT_I2C_FPGA_ENABLE))
421#define FPGA_CLOCK 12000/* FPGA crystal frequency (KHz) */
422#define I2C_CLK_DIV (5)/* frequency divider */
423#define I2C_CLK_RATE ((FPGA_CLOCK / I2C_CLK_DIV) * 1000)
424/* Hz for FPGA I2C work frequency */
425#endif
426
427/* extern void gpio_dump_regs_range(int start, int end); */
428extern void i2c_dump_info(struct mt_i2c *i2c);
429#if defined(CONFIG_MTK_GIC_EXT)
430extern void mt_irq_dump_status(unsigned int irq);
431#endif
432extern unsigned int enable_4G(void);
433extern int mtk_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
434 int num, u32 ext_flag, u32 timing);
435extern void mt_irq_dump_status(unsigned int irq);
436extern int hw_trig_i2c_enable(struct i2c_adapter *adap);
437extern int hw_trig_i2c_disable(struct i2c_adapter *adap);
438extern int hw_trig_i2c_transfer(struct i2c_adapter *adap,
439 struct i2c_msg *msgs, int num);
440extern int i2c_ccu_enable(struct i2c_adapter *adap, u16 ch_offset);
441extern int i2c_ccu_disable(struct i2c_adapter *adap);
442
443#endif