| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2020 MediaTek Inc. |
| 4 | * Author: Camo Xiao <camo.xiao@mediatek.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/scatterlist.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_irq.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/clk-provider.h> |
| 29 | |
| 30 | #if 0 |
| 31 | #include <mtk_cpufreq_hybrid.h> |
| 32 | #endif |
| 33 | #ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT |
| 34 | #include <mtk_kbase_spm.h> |
| 35 | #endif |
| 36 | #ifdef CONFIG_MTK_TINYSYS_SCP_SUPPORT |
| 37 | #include <scp_helper.h> |
| 38 | #endif |
| 39 | //#include "mtk_secure_api.h" |
| 40 | #include "i2c-mtk.h" |
| 41 | |
| 42 | static struct i2c_dma_info g_dma_regs[I2C_MAX_CHANNEL]; |
| 43 | static struct mt_i2c *g_mt_i2c[I2C_MAX_CHANNEL]; |
| 44 | static struct mtk_i2c_compatible i2c_common_compat; |
| 45 | |
| 46 | |
| 47 | static inline void _i2c_writew(u16 value, struct mt_i2c *i2c, u16 offset) |
| 48 | { |
| 49 | writew(value, i2c->base + offset); |
| 50 | } |
| 51 | |
| 52 | static inline u16 _i2c_readw(struct mt_i2c *i2c, u16 offset) |
| 53 | { |
| 54 | return readw(i2c->base + offset); |
| 55 | } |
| 56 | |
| 57 | #define raw_i2c_writew(val, i2c, ch_ofs, ofs) \ |
| 58 | do { \ |
| 59 | if (((i2c)->dev_comp->ver != 0x2) || (V2_##ofs != 0xfff)) \ |
| 60 | _i2c_writew(val, i2c, ch_ofs + \ |
| 61 | (((i2c)->dev_comp->ver == 0x2) ? \ |
| 62 | (V2_##ofs) : ofs)); \ |
| 63 | } while (0) |
| 64 | |
| 65 | #define raw_i2c_readw(i2c, ch_ofs, ofs) \ |
| 66 | ({ \ |
| 67 | u16 value = 0; \ |
| 68 | if (((i2c)->dev_comp->ver != 0x2) || (V2_##ofs != 0xfff)) \ |
| 69 | value = _i2c_readw(i2c, ch_ofs + \ |
| 70 | (((i2c)->dev_comp->ver == 0x2) ? \ |
| 71 | (V2_##ofs) : ofs)); \ |
| 72 | value; \ |
| 73 | }) |
| 74 | |
| 75 | #define i2c_writew(val, i2c, ofs) raw_i2c_writew(val, i2c, i2c->ch_offset, ofs) |
| 76 | |
| 77 | #define i2c_readw(i2c, ofs) raw_i2c_readw(i2c, i2c->ch_offset, ofs) |
| 78 | |
| 79 | #define i2c_writew_shadow(val, i2c, ofs) raw_i2c_writew(val, i2c, 0, ofs) |
| 80 | |
| 81 | #define i2c_readw_shadow(i2c, ofs) raw_i2c_readw(i2c, 0, ofs) |
| 82 | |
| 83 | void __iomem *cg_base; |
| 84 | |
| 85 | s32 map_cg_regs(struct mt_i2c *i2c) |
| 86 | { |
| 87 | struct device_node *cg_node; |
| 88 | int ret = -1; |
| 89 | |
| 90 | if (!cg_base && i2c->dev_comp->clk_compatible[0]) { |
| 91 | cg_node = of_find_compatible_node(NULL, NULL, |
| 92 | i2c->dev_comp->clk_compatible); |
| 93 | if (!cg_node) { |
| 94 | pr_info("Cannot find cg_node\n"); |
| 95 | return -ENODEV; |
| 96 | } |
| 97 | cg_base = of_iomap(cg_node, 0); |
| 98 | if (!cg_base) { |
| 99 | pr_info("cg_base iomap failed\n"); |
| 100 | return -ENOMEM; |
| 101 | } |
| 102 | ret = 0; |
| 103 | } |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | void dump_cg_regs(struct mt_i2c *i2c) |
| 109 | { |
| 110 | u32 clk_sta_val, clk_sta_offs, cg_bit; |
| 111 | u32 clk_sel_val, arbit_val, clk_sel_offs, arbit_offs; |
| 112 | |
| 113 | if (!cg_base || i2c->id >= I2C_MAX_CHANNEL) { |
| 114 | pr_info("cg_base %p, i2c id = %d\n", cg_base, i2c->id); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | clk_sta_offs = i2c->clk_sta_offset; |
| 119 | clk_sta_val = readl(cg_base + clk_sta_offs); |
| 120 | cg_bit = i2c->cg_bit; |
| 121 | |
| 122 | pr_info("[I2C] cg regs dump:\n" |
| 123 | "name %s, offset 0x%x: value = 0x%08x, bit %d, clock %s\n", |
| 124 | i2c->dev_comp->clk_compatible, |
| 125 | clk_sta_offs, clk_sta_val, cg_bit, |
| 126 | clk_sta_val & (1 << cg_bit) ? "off":"on"); |
| 127 | |
| 128 | /* Dump clk source & arbit bit */ |
| 129 | clk_sel_offs = i2c->dev_comp->clk_sel_offset; |
| 130 | clk_sel_val = readl(cg_base + clk_sel_offs); |
| 131 | arbit_offs = i2c->dev_comp->arbit_offset; |
| 132 | arbit_val = readl(cg_base + arbit_offs); |
| 133 | pr_info("[I2C] clk src & arbit dump:\n" |
| 134 | "name: %s, clk_sel_offs: 0x%x, val=0x%08x, arbit_offs: 0x%x, val=0x%08x\n", |
| 135 | i2c->dev_comp->clk_compatible, |
| 136 | clk_sel_offs, clk_sel_val, |
| 137 | arbit_offs, arbit_val); |
| 138 | } |
| 139 | |
| 140 | void __iomem *dma_base; |
| 141 | |
| 142 | s32 map_dma_regs(void) |
| 143 | { |
| 144 | struct device_node *dma_node; |
| 145 | |
| 146 | dma_node = of_find_compatible_node(NULL, NULL, "mediatek,ap_dma"); |
| 147 | if (!dma_node) { |
| 148 | pr_info("Cannot find dma_node\n"); |
| 149 | return -ENODEV; |
| 150 | } |
| 151 | dma_base = of_iomap(dma_node, 0); |
| 152 | if (!dma_base) { |
| 153 | pr_info("dma_base iomap failed\n"); |
| 154 | return -ENOMEM; |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void dump_dma_regs(void) |
| 160 | { |
| 161 | int status; |
| 162 | int i; |
| 163 | |
| 164 | if (!dma_base) { |
| 165 | pr_info("dma_base NULL\n"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | status = readl(dma_base + 8); |
| 170 | pr_info("DMA RUNNING STATUS : 0x%x .\n", status); |
| 171 | for (i = 0; i < 21 ; i++) { |
| 172 | if (status & (0x1 << i)) |
| 173 | pr_info("DMA[%d] CONTROL REG : 0x%x, DEBUG : 0x%x .\n", |
| 174 | i, |
| 175 | readl(dma_base + 0x80 + 0x80 * i + 0x18), |
| 176 | readl(dma_base + 0x80 + 0x80 * i + 0x50)); |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | static inline void i2c_writel_dma(u32 value, struct mt_i2c *i2c, u8 offset) |
| 182 | { |
| 183 | if (!i2c->fifo_only) |
| 184 | writel(value, i2c->pdmabase + i2c->ch_offset_dma + offset); |
| 185 | } |
| 186 | |
| 187 | static inline u32 i2c_readl_dma(struct mt_i2c *i2c, u8 offset) |
| 188 | { |
| 189 | if (!i2c->fifo_only) |
| 190 | return readl(i2c->pdmabase + i2c->ch_offset_dma + offset); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void record_i2c_dma_info(struct mt_i2c *i2c) |
| 195 | { |
| 196 | g_dma_regs[i2c->id].base = |
| 197 | (unsigned long)i2c->pdmabase; |
| 198 | g_dma_regs[i2c->id].int_flag = |
| 199 | i2c_readl_dma(i2c, OFFSET_INT_FLAG); |
| 200 | g_dma_regs[i2c->id].int_en = |
| 201 | i2c_readl_dma(i2c, OFFSET_INT_EN); |
| 202 | g_dma_regs[i2c->id].en = |
| 203 | i2c_readl_dma(i2c, OFFSET_EN); |
| 204 | g_dma_regs[i2c->id].rst = |
| 205 | i2c_readl_dma(i2c, OFFSET_RST); |
| 206 | g_dma_regs[i2c->id].stop = |
| 207 | i2c_readl_dma(i2c, OFFSET_STOP); |
| 208 | g_dma_regs[i2c->id].flush = |
| 209 | i2c_readl_dma(i2c, OFFSET_FLUSH); |
| 210 | g_dma_regs[i2c->id].con = |
| 211 | i2c_readl_dma(i2c, OFFSET_CON); |
| 212 | g_dma_regs[i2c->id].tx_mem_addr = |
| 213 | i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR); |
| 214 | g_dma_regs[i2c->id].rx_mem_addr = |
| 215 | i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR); |
| 216 | g_dma_regs[i2c->id].tx_len = |
| 217 | i2c_readl_dma(i2c, OFFSET_TX_LEN); |
| 218 | g_dma_regs[i2c->id].rx_len = |
| 219 | i2c_readl_dma(i2c, OFFSET_RX_LEN); |
| 220 | g_dma_regs[i2c->id].int_buf_size = |
| 221 | i2c_readl_dma(i2c, OFFSET_INT_BUF_SIZE); |
| 222 | g_dma_regs[i2c->id].debug_sta = |
| 223 | i2c_readl_dma(i2c, OFFSET_DEBUG_STA); |
| 224 | g_dma_regs[i2c->id].tx_mem_addr2 = |
| 225 | i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR2); |
| 226 | g_dma_regs[i2c->id].rx_mem_addr2 = |
| 227 | i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR2); |
| 228 | } |
| 229 | |
| 230 | static void record_i2c_info(struct mt_i2c *i2c, int tmo) |
| 231 | { |
| 232 | int idx = i2c->rec_idx; |
| 233 | |
| 234 | i2c->rec_info[idx].slave_addr = i2c_readw(i2c, OFFSET_SLAVE_ADDR); |
| 235 | i2c->rec_info[idx].intr_stat = i2c->irq_stat; |
| 236 | i2c->rec_info[idx].control = i2c_readw(i2c, OFFSET_CONTROL); |
| 237 | i2c->rec_info[idx].fifo_stat = i2c_readw(i2c, OFFSET_FIFO_STAT); |
| 238 | i2c->rec_info[idx].debug_stat = i2c_readw(i2c, OFFSET_DEBUGSTAT); |
| 239 | i2c->rec_info[idx].tmo = tmo; |
| 240 | i2c->rec_info[idx].end_time = sched_clock(); |
| 241 | |
| 242 | i2c->rec_idx++; |
| 243 | if (i2c->rec_idx == I2C_RECORD_LEN) |
| 244 | i2c->rec_idx = 0; |
| 245 | } |
| 246 | |
| 247 | static void dump_i2c_info(struct mt_i2c *i2c) |
| 248 | { |
| 249 | int i; |
| 250 | int idx = i2c->rec_idx; |
| 251 | unsigned long long endtime; |
| 252 | unsigned long ns; |
| 253 | |
| 254 | if (i2c->buffermode) /* no i2c history @ buffermode */ |
| 255 | return; |
| 256 | |
| 257 | dev_info(i2c->dev, "last transfer info:\n"); |
| 258 | |
| 259 | for (i = 0; i < I2C_RECORD_LEN; i++) { |
| 260 | if (idx == 0) |
| 261 | idx = I2C_RECORD_LEN; |
| 262 | idx--; |
| 263 | endtime = i2c->rec_info[idx].end_time; |
| 264 | ns = do_div(endtime, 1000000000); |
| 265 | dev_info(i2c->dev, |
| 266 | "[%02d] [%5lu.%06lu] SLAVE_ADDR=%x,INTR_STAT=%x,CONTROL=%x,FIFO_STAT=%x,DEBUGSTAT=%x, tmo=%d\n", |
| 267 | i, |
| 268 | (unsigned long)endtime, |
| 269 | ns/1000, |
| 270 | i2c->rec_info[idx].slave_addr, |
| 271 | i2c->rec_info[idx].intr_stat, |
| 272 | i2c->rec_info[idx].control, |
| 273 | i2c->rec_info[idx].fifo_stat, |
| 274 | i2c->rec_info[idx].debug_stat, |
| 275 | i2c->rec_info[idx].tmo |
| 276 | ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static int mt_i2c_clock_enable(struct mt_i2c *i2c) |
| 281 | { |
| 282 | #if !defined(CONFIG_MT_I2C_FPGA_ENABLE) |
| 283 | int ret = 0; |
| 284 | |
| 285 | ret = clk_prepare_enable(i2c->clk_dma); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
| 289 | if (i2c->clk_pal != NULL) { |
| 290 | ret = clk_prepare_enable(i2c->clk_pal); |
| 291 | if (ret) |
| 292 | goto err_main; |
| 293 | } |
| 294 | |
| 295 | if (i2c->clk_arb != NULL) { |
| 296 | ret = clk_prepare_enable(i2c->clk_arb); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | } |
| 300 | ret = clk_prepare_enable(i2c->clk_main); |
| 301 | if (ret) |
| 302 | goto err_main; |
| 303 | |
| 304 | if (i2c->have_pmic) { |
| 305 | ret = clk_prepare_enable(i2c->clk_pmic); |
| 306 | if (ret) |
| 307 | goto err_pmic; |
| 308 | } |
| 309 | spin_lock(&i2c->cg_lock); |
| 310 | if (i2c->suspended) |
| 311 | ret = -EIO; |
| 312 | else |
| 313 | i2c->cg_cnt++; |
| 314 | spin_unlock(&i2c->cg_lock); |
| 315 | if (ret) { |
| 316 | dev_info(i2c->dev, "err, access at suspend no irq stage\n"); |
| 317 | goto err_cg; |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | |
| 322 | err_cg: |
| 323 | if (i2c->have_pmic) |
| 324 | clk_disable_unprepare(i2c->clk_pmic); |
| 325 | err_pmic: |
| 326 | clk_disable_unprepare(i2c->clk_main); |
| 327 | err_main: |
| 328 | if (i2c->clk_arb) |
| 329 | clk_disable_unprepare(i2c->clk_arb); |
| 330 | clk_disable_unprepare(i2c->clk_dma); |
| 331 | return ret; |
| 332 | #else |
| 333 | return 0; |
| 334 | #endif |
| 335 | } |
| 336 | |
| 337 | static void mt_i2c_clock_disable(struct mt_i2c *i2c) |
| 338 | { |
| 339 | #if !defined(CONFIG_MT_I2C_FPGA_ENABLE) |
| 340 | if (i2c->have_pmic) |
| 341 | clk_disable_unprepare(i2c->clk_pmic); |
| 342 | |
| 343 | clk_disable_unprepare(i2c->clk_main); |
| 344 | if (i2c->clk_pal != NULL) |
| 345 | clk_disable_unprepare(i2c->clk_pal); |
| 346 | |
| 347 | if (i2c->clk_arb != NULL) |
| 348 | clk_disable_unprepare(i2c->clk_arb); |
| 349 | |
| 350 | clk_disable_unprepare(i2c->clk_dma); |
| 351 | spin_lock(&i2c->cg_lock); |
| 352 | i2c->cg_cnt--; |
| 353 | spin_unlock(&i2c->cg_lock); |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | static int i2c_get_semaphore(struct mt_i2c *i2c) |
| 358 | { |
| 359 | #if 0 |
| 360 | int count = 100; |
| 361 | #endif |
| 362 | #if 0 |
| 363 | if (i2c->appm) { |
| 364 | if (cpuhvfs_get_dvfsp_semaphore(SEMA_I2C_DRV) != 0) { |
| 365 | dev_info(i2c->dev, "sema time out 2ms\n"); |
| 366 | if (cpuhvfs_get_dvfsp_semaphore(SEMA_I2C_DRV) != 0) { |
| 367 | dev_info(i2c->dev, "sema time out 4ms\n"); |
| 368 | i2c_dump_info(i2c); |
| 369 | WARN_ON(1); |
| 370 | return -EBUSY; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | #ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT |
| 377 | if (i2c->gpupm) { |
| 378 | if (dvfs_gpu_pm_spin_lock_for_vgpu() != 0) { |
| 379 | dev_info(i2c->dev, "sema time out.\n"); |
| 380 | return -EBUSY; |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | |
| 385 | switch (i2c->id) { |
| 386 | #if 0 |
| 387 | case 0: |
| 388 | while ((get_scp_semaphore(SEMAPHORE_I2C0) != 1) && count > 0) |
| 389 | count--; |
| 390 | return count > 0 ? 0 : -EBUSY; |
| 391 | case 1: |
| 392 | while ((get_scp_semaphore(SEMAPHORE_I2C1) != 1) && count > 0) |
| 393 | count--; |
| 394 | return count > 0 ? 0 : -EBUSY; |
| 395 | #endif |
| 396 | default: |
| 397 | return 0; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static int i2c_release_semaphore(struct mt_i2c *i2c) |
| 402 | { |
| 403 | #if 0 |
| 404 | if (i2c->appm) |
| 405 | cpuhvfs_release_dvfsp_semaphore(SEMA_I2C_DRV); |
| 406 | #endif |
| 407 | #ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT |
| 408 | if (i2c->gpupm) |
| 409 | dvfs_gpu_pm_spin_unlock_for_vgpu(); |
| 410 | #endif |
| 411 | |
| 412 | switch (i2c->id) { |
| 413 | #if 0 |
| 414 | case 0: |
| 415 | return release_scp_semaphore(SEMAPHORE_I2C0) == 1 ? 0 : -EBUSY; |
| 416 | case 1: |
| 417 | return release_scp_semaphore(SEMAPHORE_I2C1) == 1 ? 0 : -EBUSY; |
| 418 | #endif |
| 419 | default: |
| 420 | return 0; |
| 421 | } |
| 422 | } |
| 423 | static void free_i2c_dma_bufs(struct mt_i2c *i2c) |
| 424 | { |
| 425 | dma_free_coherent(i2c->adap.dev.parent, PAGE_SIZE, |
| 426 | i2c->dma_buf.vaddr, i2c->dma_buf.paddr); |
| 427 | } |
| 428 | |
| 429 | static inline void mt_i2c_wait_done(struct mt_i2c *i2c, u16 ch_off) |
| 430 | { |
| 431 | u16 start, tmo; |
| 432 | |
| 433 | start = raw_i2c_readw(i2c, ch_off, OFFSET_START) & I2C_TRANSAC_START; |
| 434 | if (start) { |
| 435 | dev_info(i2c->dev, "wait transfer done before cg off.\n"); |
| 436 | |
| 437 | tmo = 100; |
| 438 | do { |
| 439 | msleep(20); |
| 440 | start = raw_i2c_readw(i2c, ch_off, OFFSET_START) & |
| 441 | I2C_TRANSAC_START; |
| 442 | tmo--; |
| 443 | } while (start && tmo); |
| 444 | |
| 445 | if (start && !tmo) { |
| 446 | dev_info(i2c->dev, "wait transfer timeout.\n"); |
| 447 | i2c_dump_info(i2c); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static inline void mt_i2c_init_hw(struct mt_i2c *i2c) |
| 453 | { |
| 454 | /* clear interrupt status */ |
| 455 | i2c_writew_shadow(0, i2c, OFFSET_INTR_MASK); |
| 456 | i2c->irq_stat = i2c_readw_shadow(i2c, OFFSET_INTR_STAT); |
| 457 | i2c_writew_shadow(i2c->irq_stat, i2c, OFFSET_INTR_STAT); |
| 458 | |
| 459 | i2c_writew_shadow(I2C_SOFT_RST, i2c, OFFSET_SOFTRESET); |
| 460 | /* Set ioconfig */ |
| 461 | if (i2c->use_push_pull) |
| 462 | i2c_writew_shadow(I2C_IO_CONFIG_PUSH_PULL, |
| 463 | i2c, OFFSET_IO_CONFIG); |
| 464 | else |
| 465 | i2c_writew_shadow(I2C_IO_CONFIG_OPEN_DRAIN, |
| 466 | i2c, OFFSET_IO_CONFIG); |
| 467 | if (i2c->have_dcm) |
| 468 | i2c_writew_shadow(I2C_DCM_DISABLE, i2c, OFFSET_DCM_EN); |
| 469 | |
| 470 | if (i2c->dev_comp->ver != 0x2) |
| 471 | i2c_writew_shadow(i2c->timing_reg, i2c, OFFSET_TIMING); |
| 472 | else |
| 473 | i2c_writew_shadow(i2c->timing_reg | I2C_TIMEOUT_EN, i2c, |
| 474 | OFFSET_TIMING); |
| 475 | |
| 476 | if (i2c->dev_comp->set_ltiming) |
| 477 | i2c_writew_shadow(i2c->ltiming_reg, i2c, OFFSET_LTIMING); |
| 478 | i2c_writew_shadow(i2c->high_speed_reg, i2c, OFFSET_HS); |
| 479 | /* DMA warm reset, and waits for EN to become 0 */ |
| 480 | i2c_writel_dma(I2C_DMA_WARM_RST, i2c, OFFSET_RST); |
| 481 | udelay(5); |
| 482 | if (i2c_readl_dma(i2c, OFFSET_EN) != 0) { |
| 483 | dev_info(i2c->dev, "DMA bus hang .\n"); |
| 484 | dump_dma_regs(); |
| 485 | WARN_ON(1); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* calculate i2c port speed */ |
| 490 | static int mtk_i2c_calculate_speed(struct mt_i2c *i2c, |
| 491 | unsigned int clk_src_in_hz, |
| 492 | unsigned int speed_hz, |
| 493 | unsigned int *timing_step_cnt, |
| 494 | unsigned int *timing_sample_cnt) |
| 495 | { |
| 496 | unsigned int khz; |
| 497 | unsigned int step_cnt; |
| 498 | unsigned int sample_cnt; |
| 499 | unsigned int sclk; |
| 500 | unsigned int hclk; |
| 501 | unsigned int max_step_cnt; |
| 502 | unsigned int sample_div = MAX_SAMPLE_CNT_DIV; |
| 503 | unsigned int step_div; |
| 504 | unsigned int min_div; |
| 505 | unsigned int best_mul; |
| 506 | unsigned int cnt_mul; |
| 507 | |
| 508 | if (speed_hz > MAX_HS_MODE_SPEED) { |
| 509 | if (i2c->dev_comp->check_max_freq) |
| 510 | return -EINVAL; |
| 511 | max_step_cnt = MAX_HS_STEP_CNT_DIV; |
| 512 | } else if (speed_hz > MAX_FS_MODE_SPEED) { |
| 513 | max_step_cnt = MAX_HS_STEP_CNT_DIV; |
| 514 | } else { |
| 515 | max_step_cnt = MAX_STEP_CNT_DIV; |
| 516 | } |
| 517 | step_div = max_step_cnt; |
| 518 | |
| 519 | /* Find the best combination */ |
| 520 | khz = speed_hz / 1000; |
| 521 | hclk = clk_src_in_hz / 1000; |
| 522 | min_div = ((hclk >> 1) + khz - 1) / khz; |
| 523 | best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; |
| 524 | for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) { |
| 525 | step_cnt = (min_div + sample_cnt - 1) / sample_cnt; |
| 526 | cnt_mul = step_cnt * sample_cnt; |
| 527 | if (step_cnt > max_step_cnt) |
| 528 | continue; |
| 529 | if (cnt_mul < best_mul) { |
| 530 | best_mul = cnt_mul; |
| 531 | sample_div = sample_cnt; |
| 532 | step_div = step_cnt; |
| 533 | if (best_mul == min_div) |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | sample_cnt = sample_div; |
| 538 | step_cnt = step_div; |
| 539 | sclk = hclk / (2 * sample_cnt * step_cnt); |
| 540 | if (sclk > khz) { |
| 541 | dev_dbg(i2c->dev, "%s mode: unsupported speed (%ldkhz)\n", |
| 542 | (speed_hz > MAX_FS_MODE_SPEED) ? "HS" : "ST/FT", |
| 543 | (long int)khz); |
| 544 | return -ENOTSUPP; |
| 545 | } |
| 546 | |
| 547 | if (i2c->dev_comp->cnt_constraint) { |
| 548 | if (--sample_cnt) {/* --sample_cnt = 0, setp_cnt needn't -1 */ |
| 549 | step_cnt--; |
| 550 | } |
| 551 | } else { |
| 552 | sample_cnt--; |
| 553 | step_cnt--; |
| 554 | } |
| 555 | |
| 556 | *timing_step_cnt = step_cnt; |
| 557 | *timing_sample_cnt = sample_cnt; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int i2c_set_speed(struct mt_i2c *i2c, unsigned int clk_src_in_hz) |
| 563 | { |
| 564 | int ret; |
| 565 | unsigned int step_cnt = 0; |
| 566 | unsigned int sample_cnt = 0; |
| 567 | unsigned int l_step_cnt = 0; |
| 568 | unsigned int l_sample_cnt = 0; |
| 569 | unsigned int speed_hz; |
| 570 | unsigned int duty = HALF_DUTY_CYCLE; |
| 571 | |
| 572 | if (i2c->ext_data.isEnable && i2c->ext_data.timing) |
| 573 | speed_hz = i2c->ext_data.timing; |
| 574 | else |
| 575 | speed_hz = i2c->speed_hz; |
| 576 | |
| 577 | if (speed_hz > MAX_FS_PLUS_MODE_SPEED && !i2c->hs_only) { |
| 578 | /* Set the hign speed mode register */ |
| 579 | ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz, |
| 580 | MAX_FS_MODE_SPEED, &l_step_cnt, &l_sample_cnt); |
| 581 | if (ret < 0) |
| 582 | return ret; |
| 583 | |
| 584 | ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz, |
| 585 | speed_hz, &step_cnt, &sample_cnt); |
| 586 | if (ret < 0) |
| 587 | return ret; |
| 588 | |
| 589 | i2c->high_speed_reg = I2C_HS_HOLD_TIME | |
| 590 | I2C_TIME_DEFAULT_VALUE | I2C_HS_SPEED | |
| 591 | (sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 12 | |
| 592 | ((step_cnt - 1) & I2C_TIMING_SAMPLE_COUNT_MASK) << 8; |
| 593 | |
| 594 | i2c->timing_reg = |
| 595 | (l_sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 8 | |
| 596 | (l_step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0; |
| 597 | |
| 598 | if (i2c->dev_comp->set_ltiming) { |
| 599 | i2c->ltiming_reg = I2C_HS_HOLD_SEL | (l_sample_cnt << 6) |
| 600 | | (l_step_cnt << 0) | |
| 601 | (sample_cnt & |
| 602 | I2C_TIMING_SAMPLE_COUNT_MASK) << 12 | |
| 603 | ((step_cnt + 1) & |
| 604 | I2C_TIMING_SAMPLE_COUNT_MASK) << 9; |
| 605 | } |
| 606 | } else { |
| 607 | if (speed_hz > I2C_DEFAUT_SPEED |
| 608 | && speed_hz <= MAX_FS_MODE_SPEED |
| 609 | && i2c->dev_comp->set_ltiming) |
| 610 | duty = DUTY_CYCLE; |
| 611 | |
| 612 | ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz, |
| 613 | (speed_hz * 50 / duty), &step_cnt, &sample_cnt); |
| 614 | if (ret < 0) |
| 615 | return ret; |
| 616 | |
| 617 | i2c->timing_reg = |
| 618 | (sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 8 | |
| 619 | (step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0; |
| 620 | |
| 621 | if (i2c->dev_comp->set_ltiming) { |
| 622 | ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz, |
| 623 | (speed_hz * 50 / (100 - duty)), |
| 624 | &l_step_cnt, &l_sample_cnt); |
| 625 | if (ret < 0) |
| 626 | return ret; |
| 627 | |
| 628 | i2c->ltiming_reg = |
| 629 | (l_sample_cnt & |
| 630 | I2C_TIMING_SAMPLE_COUNT_MASK) << 6 | |
| 631 | (l_step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0; |
| 632 | } |
| 633 | /* Disable the high speed transaction */ |
| 634 | i2c->high_speed_reg = I2C_TIME_CLR_VALUE; |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | #ifdef I2C_DEBUG_FS |
| 642 | void i2c_dump_info1(struct mt_i2c *i2c) |
| 643 | { |
| 644 | if (i2c->ext_data.isEnable && i2c->ext_data.timing) |
| 645 | dev_info(i2c->dev, "I2C structure:\nspeed %d\n", |
| 646 | i2c->ext_data.timing); |
| 647 | else |
| 648 | dev_info(i2c->dev, "I2C structure:\nspeed %d\n", |
| 649 | i2c->speed_hz); |
| 650 | dev_info(i2c->dev, "I2C structure:\nOp %x\n", i2c->op); |
| 651 | dev_info(i2c->dev, |
| 652 | "I2C structure:\nData_size %x\nIrq_stat %x\nTrans_stop %d\n", |
| 653 | i2c->msg_len, i2c->irq_stat, i2c->trans_stop); |
| 654 | dev_info(i2c->dev, "base address %p\n", i2c->base); |
| 655 | dev_info(i2c->dev, |
| 656 | "I2C register:\nSLAVE_ADDR %x\nINTR_MASK %x\n", |
| 657 | (i2c_readw(i2c, OFFSET_SLAVE_ADDR)), |
| 658 | (i2c_readw(i2c, OFFSET_INTR_MASK))); |
| 659 | dev_info(i2c->dev, |
| 660 | "I2C register:\nINTR_STAT %x\nCONTROL %x\n", |
| 661 | (i2c_readw(i2c, OFFSET_INTR_STAT)), |
| 662 | (i2c_readw(i2c, OFFSET_CONTROL))); |
| 663 | dev_info(i2c->dev, |
| 664 | "I2C register:\nTRANSFER_LEN %x\nTRANSAC_LEN %x\n", |
| 665 | (i2c_readw(i2c, OFFSET_TRANSFER_LEN)), |
| 666 | (i2c_readw(i2c, OFFSET_TRANSAC_LEN))); |
| 667 | dev_info(i2c->dev, |
| 668 | "I2C register:\nDELAY_LEN %x\nTIMING %x\n", |
| 669 | (i2c_readw(i2c, OFFSET_DELAY_LEN)), |
| 670 | (i2c_readw(i2c, OFFSET_TIMING))); |
| 671 | dev_info(i2c->dev, |
| 672 | "I2C register:\nSTART %x\nFIFO_STAT %x\n", |
| 673 | (i2c_readw(i2c, OFFSET_START)), |
| 674 | (i2c_readw(i2c, OFFSET_FIFO_STAT))); |
| 675 | dev_info(i2c->dev, |
| 676 | "I2C register:\nIO_CONFIG %x\nHS %x\n", |
| 677 | (i2c_readw(i2c, OFFSET_IO_CONFIG)), |
| 678 | (i2c_readw(i2c, OFFSET_HS))); |
| 679 | dev_info(i2c->dev, |
| 680 | "I2C register:\nDEBUGSTAT %x\nEXT_CONF %x\nPATH_DIR %x\n", |
| 681 | (i2c_readw(i2c, OFFSET_DEBUGSTAT)), |
| 682 | (i2c_readw(i2c, OFFSET_EXT_CONF)), |
| 683 | (i2c_readw(i2c, OFFSET_PATH_DIR))); |
| 684 | } |
| 685 | |
| 686 | void i2c_dump_info(struct mt_i2c *i2c) |
| 687 | { |
| 688 | /* I2CFUC(); */ |
| 689 | /* int val=0; */ |
| 690 | pr_info_ratelimited("%s: +++++++++++++++++++\n", __func__); |
| 691 | pr_info_ratelimited("I2C structure:\n" |
| 692 | I2CTAG "Clk=%ld,Id=%d,Op=0x%x,Irq_stat=0x%x,Total_len=0x%x\n" |
| 693 | I2CTAG "Trans_len=0x%x,Trans_num=0x%x,Trans_auxlen=0x%x,\n" |
| 694 | I2CTAG "speed=%d,Trans_stop=%u,cg_cnt=%d,hs_only=%d,\n" |
| 695 | I2CTAG "ch_offset=0x%x,ch_offset_default=0x%x\n", |
| 696 | i2c->main_clk, i2c->id, i2c->op, i2c->irq_stat, i2c->total_len, |
| 697 | i2c->msg_len, 1, i2c->msg_aux_len, i2c->speed_hz, |
| 698 | i2c->trans_stop, i2c->cg_cnt, |
| 699 | i2c->hs_only, i2c->ch_offset, i2c->ch_offset_default); |
| 700 | |
| 701 | pr_info_ratelimited("base addr:0x%p\n", i2c->base); |
| 702 | pr_info_ratelimited("I2C register:\n" |
| 703 | I2CTAG "SLAVE_ADDR=0x%x,INTR_MASK=0x%x,INTR_STAT=0x%x,\n" |
| 704 | I2CTAG "CONTROL=0x%x,TRANSFER_LEN=0x%x,TRANSAC_LEN=0x%x,\n" |
| 705 | I2CTAG "DELAY_LEN=0x%x,TIMING=0x%x,LTIMING=0x%x,START=0x%x\n" |
| 706 | I2CTAG "FIFO_STAT=0x%x,IO_CONFIG=0x%x,HS=0x%x\n" |
| 707 | I2CTAG "DCM_EN=0x%x,DEBUGSTAT=0x%x,EXT_CONF=0x%x\n" |
| 708 | I2CTAG "TRANSFER_LEN_AUX=0x%x,OFFSET_DMA_FSM_DEBUG=0x%x\n" |
| 709 | I2CTAG "OFFSET_MCU_INTR=0x%x\n", |
| 710 | (i2c_readw(i2c, OFFSET_SLAVE_ADDR)), |
| 711 | (i2c_readw(i2c, OFFSET_INTR_MASK)), |
| 712 | (i2c_readw(i2c, OFFSET_INTR_STAT)), |
| 713 | (i2c_readw(i2c, OFFSET_CONTROL)), |
| 714 | (i2c_readw(i2c, OFFSET_TRANSFER_LEN)), |
| 715 | (i2c_readw(i2c, OFFSET_TRANSAC_LEN)), |
| 716 | (i2c_readw(i2c, OFFSET_DELAY_LEN)), |
| 717 | (i2c_readw(i2c, OFFSET_TIMING)), |
| 718 | (i2c_readw(i2c, OFFSET_LTIMING)), |
| 719 | (i2c_readw(i2c, OFFSET_START)), |
| 720 | (i2c_readw(i2c, OFFSET_FIFO_STAT)), |
| 721 | (i2c_readw(i2c, OFFSET_IO_CONFIG)), |
| 722 | (i2c_readw(i2c, OFFSET_HS)), |
| 723 | (i2c_readw(i2c, OFFSET_DCM_EN)), |
| 724 | (i2c_readw(i2c, OFFSET_DEBUGSTAT)), |
| 725 | (i2c_readw(i2c, OFFSET_EXT_CONF)), |
| 726 | (i2c_readw(i2c, OFFSET_TRANSFER_LEN_AUX)), |
| 727 | (i2c_readw(i2c, OFFSET_DMA_FSM_DEBUG)), |
| 728 | (i2c_readw(i2c, OFFSET_MCU_INTR))); |
| 729 | |
| 730 | pr_info_ratelimited("before enable DMA register(0x%lx):\n" |
| 731 | I2CTAG "INT_FLAG=0x%x,INT_EN=0x%x,EN=0x%x,RST=0x%x,\n" |
| 732 | I2CTAG "STOP=0x%x,FLUSH=0x%x,CON=0x%x,\n" |
| 733 | I2CTAG "TX_MEM_ADDR=0x%x, RX_MEM_ADDR=0x%x\n" |
| 734 | I2CTAG "TX_LEN=0x%x,RX_LEN=0x%x,INT_BUF_SIZE=0x%x,\n" |
| 735 | I2CTAG "DEBUGSTA=0x%x,TX_MEM_ADDR2=0x%x, RX_MEM_ADDR2=0x%x\n", |
| 736 | g_dma_regs[i2c->id].base, |
| 737 | g_dma_regs[i2c->id].int_flag, |
| 738 | g_dma_regs[i2c->id].int_en, |
| 739 | g_dma_regs[i2c->id].en, |
| 740 | g_dma_regs[i2c->id].rst, |
| 741 | g_dma_regs[i2c->id].stop, |
| 742 | g_dma_regs[i2c->id].flush, |
| 743 | g_dma_regs[i2c->id].con, |
| 744 | g_dma_regs[i2c->id].tx_mem_addr, |
| 745 | g_dma_regs[i2c->id].rx_mem_addr, |
| 746 | g_dma_regs[i2c->id].tx_len, |
| 747 | g_dma_regs[i2c->id].rx_len, |
| 748 | g_dma_regs[i2c->id].int_buf_size, g_dma_regs[i2c->id].debug_sta, |
| 749 | g_dma_regs[i2c->id].tx_mem_addr2, |
| 750 | g_dma_regs[i2c->id].rx_mem_addr2); |
| 751 | pr_info_ratelimited("DMA register(0x%p):\n" |
| 752 | I2CTAG "INT_FLAG=0x%x,INT_EN=0x%x,EN=0x%x,RST=0x%x,\n" |
| 753 | I2CTAG "STOP=0x%x,FLUSH=0x%x,CON=0x%x,\n" |
| 754 | I2CTAG "TX_MEM_ADDR=0x%x, RX_MEM_ADDR=0x%x,\n" |
| 755 | I2CTAG "TX_LEN=0x%x,RX_LEN=x%x,INT_BUF_SIZE=0x%x,\n" |
| 756 | I2CTAG "DEBUGSTA=0x%x,TX_MEM_ADDR2=0x%x, RX_MEM_ADDR2=0x%x\n", |
| 757 | i2c->pdmabase, |
| 758 | (i2c_readl_dma(i2c, OFFSET_INT_FLAG)), |
| 759 | (i2c_readl_dma(i2c, OFFSET_INT_EN)), |
| 760 | (i2c_readl_dma(i2c, OFFSET_EN)), |
| 761 | (i2c_readl_dma(i2c, OFFSET_RST)), |
| 762 | (i2c_readl_dma(i2c, OFFSET_STOP)), |
| 763 | (i2c_readl_dma(i2c, OFFSET_FLUSH)), |
| 764 | (i2c_readl_dma(i2c, OFFSET_CON)), |
| 765 | (i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR)), |
| 766 | (i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR)), |
| 767 | (i2c_readl_dma(i2c, OFFSET_TX_LEN)), |
| 768 | (i2c_readl_dma(i2c, OFFSET_RX_LEN)), |
| 769 | (i2c_readl_dma(i2c, OFFSET_INT_BUF_SIZE)), |
| 770 | (i2c_readl_dma(i2c, OFFSET_DEBUG_STA)), |
| 771 | (i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR2)), |
| 772 | (i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR2))); |
| 773 | pr_info_ratelimited("%s: -----------------------\n", __func__); |
| 774 | |
| 775 | dump_i2c_info(i2c); |
| 776 | if (i2c->ccu_offset) { |
| 777 | dev_info(i2c->dev, "I2C CCU register:\n" |
| 778 | I2CTAG "SLAVE_ADDR=0x%x,INTR_MASK=0x%x,\n" |
| 779 | I2CTAG "INTR_STAT=0x%x,CONTROL=0x%x,\n" |
| 780 | I2CTAG "TRANSFER_LEN=0x%x, TRANSAC_LEN=0x%x,DELAY_LEN=0x%x\n" |
| 781 | I2CTAG "TIMING=0x%x,LTIMING=0x%x,START=0x%x,FIFO_STAT=0x%x,\n" |
| 782 | I2CTAG "IO_CONFIG=0x%x,HS=0x%x,DCM_EN=0x%x,DEBUGSTAT=0x%x,\n" |
| 783 | I2CTAG "EXT_CONF=0x%x,TRANSFER_LEN_AUX=0x%x\n" |
| 784 | I2CTAG "OFFSET_DMA_FSM_DEBUG=0x%x,OFFSET_MCU_INTR=0x%x\n", |
| 785 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_SLAVE_ADDR)), |
| 786 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_INTR_MASK)), |
| 787 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_INTR_STAT)), |
| 788 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_CONTROL)), |
| 789 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSFER_LEN)), |
| 790 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSAC_LEN)), |
| 791 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DELAY_LEN)), |
| 792 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TIMING)), |
| 793 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_LTIMING)), |
| 794 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_START)), |
| 795 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_FIFO_STAT)), |
| 796 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_IO_CONFIG)), |
| 797 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_HS)), |
| 798 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DCM_EN)), |
| 799 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DEBUGSTAT)), |
| 800 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_EXT_CONF)), |
| 801 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSFER_LEN_AUX)), |
| 802 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DMA_FSM_DEBUG)), |
| 803 | (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_MCU_INTR))); |
| 804 | } |
| 805 | } |
| 806 | #else |
| 807 | void i2c_dump_info(struct mt_i2c *i2c) |
| 808 | { |
| 809 | } |
| 810 | #endif |
| 811 | |
| 812 | void i2c_gpio_dump_info(struct mt_i2c *i2c) |
| 813 | { |
| 814 | #ifndef CONFIG_MT_I2C_FPGA_ENABLE |
| 815 | if (i2c->gpiobase) { |
| 816 | dev_info(i2c->dev, "%s +++++++++++++++++++\n", __func__); |
| 817 | gpio_dump_regs_range(i2c->scl_gpio_id, i2c->sda_gpio_id); |
| 818 | dev_info(i2c->dev, "I2C gpio structure:\n" |
| 819 | I2CTAG "EH_CFG=0x%x,PU_CFG=0x%x,RSEL_CFG=0x%x\n", |
| 820 | readl(i2c->gpiobase + i2c->offset_eh_cfg), |
| 821 | readl(i2c->gpiobase + i2c->offset_pu_cfg), |
| 822 | readl(i2c->gpiobase + i2c->offset_rsel_cfg)); |
| 823 | } else |
| 824 | dev_info(i2c->dev, "i2c gpiobase is NULL\n"); |
| 825 | #endif |
| 826 | } |
| 827 | |
| 828 | void dump_i2c_status(int id) |
| 829 | { |
| 830 | if (id >= I2C_MAX_CHANNEL) { |
| 831 | pr_info("error %s, id = %d\n", __func__, id); |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | if (!g_mt_i2c[id]) { |
| 836 | pr_info("error %s, g_mt_i2c[%d] == NULL\n", __func__, id); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | dump_cg_regs(g_mt_i2c[id]); |
| 841 | (void)mt_i2c_clock_enable(g_mt_i2c[id]); |
| 842 | i2c_dump_info(g_mt_i2c[id]); |
| 843 | mt_i2c_clock_disable(g_mt_i2c[id]); |
| 844 | } |
| 845 | EXPORT_SYMBOL(dump_i2c_status); |
| 846 | |
| 847 | static int mt_i2c_do_transfer(struct mt_i2c *i2c) |
| 848 | { |
| 849 | u16 addr_reg = 0; |
| 850 | u16 control_reg = 0; |
| 851 | u16 ioconfig_reg = 0; |
| 852 | u16 start_reg = 0; |
| 853 | u16 int_reg = 0; |
| 854 | int tmo = i2c->adap.timeout; |
| 855 | unsigned int speed_hz = 0; |
| 856 | bool isDMA = false; |
| 857 | int data_size = 0; |
| 858 | u8 *ptr; |
| 859 | int ret = 0; |
| 860 | /* u16 ch_offset; */ |
| 861 | |
| 862 | i2c->trans_stop = false; |
| 863 | i2c->irq_stat = 0; |
| 864 | if (i2c->total_len > 8 || i2c->msg_aux_len > 8) { |
| 865 | if (!i2c->fifo_only) |
| 866 | isDMA = true; |
| 867 | else { |
| 868 | dev_info(i2c->dev, "i2c does not support dma mode\n"); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (i2c->ext_data.isEnable && i2c->ext_data.timing) |
| 874 | speed_hz = i2c->ext_data.timing; |
| 875 | else |
| 876 | speed_hz = i2c->speed_hz; |
| 877 | if (i2c->ext_data.is_ch_offset) { |
| 878 | i2c->ch_offset = i2c->ext_data.ch_offset; |
| 879 | i2c->ch_offset_dma = i2c->ext_data.ch_offset_dma; |
| 880 | if (i2c->ext_data.ch_offset == 0) { |
| 881 | dev_info(i2c->dev, "Wrong channel offset for multi-channel\n"); |
| 882 | i2c->ch_offset = i2c->ccu_offset; |
| 883 | } |
| 884 | } else { |
| 885 | i2c->ch_offset = i2c->ch_offset_default; |
| 886 | i2c->ch_offset_dma = i2c->ch_offset_dma_default; |
| 887 | } |
| 888 | |
| 889 | #if defined(CONFIG_ARCH_MT6765) |
| 890 | i2c_writel(i2c, OFFSET_DEBUGCTRL, 0x28); |
| 891 | #endif |
| 892 | #if !defined(CONFIG_MT_I2C_FPGA_ENABLE) |
| 893 | ret = i2c_set_speed(i2c, |
| 894 | clk_get_rate(i2c->clk_main) / i2c->clk_src_div); |
| 895 | #else |
| 896 | ret = i2c_set_speed(i2c, I2C_CLK_RATE); |
| 897 | #endif |
| 898 | if (ret) { |
| 899 | dev_info(i2c->dev, "Failed to set the speed\n"); |
| 900 | return -EINVAL; |
| 901 | } |
| 902 | |
| 903 | if ((i2c->dev_comp->ver == 0x2) && i2c->ltiming_reg) { |
| 904 | u32 tv1, tv2, tv; |
| 905 | #if !defined(CONFIG_MT_I2C_FPGA_ENABLE) |
| 906 | tv1 = (clk_get_rate(i2c->clk_main) / i2c->clk_src_div) / 1000; |
| 907 | #else |
| 908 | tv1 = I2C_CLK_RATE / 1000; |
| 909 | #endif |
| 910 | tv1 = tv1 * MAX_SCL_LOW_TIME; |
| 911 | tv2 = (((i2c->ltiming_reg & LSTEP_MSK) + 1) * |
| 912 | (((i2c->ltiming_reg & LSAMPLE_MSK) >> 6) + 1)); |
| 913 | tv = DIV_ROUND_UP(tv1, tv2); |
| 914 | i2c_writew(tv & 0xFFFF, i2c, OFFSET_HW_TIMEOUT); |
| 915 | /* dev_info(i2c->dev, "scl time out value %04X\n", */ |
| 916 | /* (u16)(tv & 0xFFFF)); */ |
| 917 | } |
| 918 | if (i2c->dev_comp->set_dt_div) { |
| 919 | if (i2c->clk_src_div > MAX_CLOCK_DIV) { |
| 920 | dev_info(i2c->dev, "Clock div error\n"); |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | i2c_writew(((i2c->clk_src_div - 1) << 8) + |
| 924 | (i2c->clk_src_div - 1), |
| 925 | i2c, OFFSET_CLOCK_DIV); |
| 926 | } |
| 927 | |
| 928 | /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */ |
| 929 | if (i2c->have_pmic) |
| 930 | i2c_writew(I2C_CONTROL_WRAPPER, i2c, OFFSET_PATH_DIR); |
| 931 | if (speed_hz > 400000) |
| 932 | control_reg = I2C_CONTROL_ACKERR_DET_EN; |
| 933 | else |
| 934 | control_reg = I2C_CONTROL_ACKERR_DET_EN | |
| 935 | I2C_CONTROL_CLK_EXT_EN; |
| 936 | if (isDMA == true) /* DMA */ |
| 937 | control_reg |= |
| 938 | I2C_CONTROL_DMA_EN | |
| 939 | I2C_CONTROL_DMAACK_EN | |
| 940 | I2C_CONTROL_ASYNC_MODE; |
| 941 | |
| 942 | if (speed_hz > 400000) |
| 943 | control_reg |= I2C_CONTROL_RS; |
| 944 | if (i2c->op == I2C_MASTER_WRRD) |
| 945 | control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS; |
| 946 | if (i2c->dev_comp->control_irq_sel == 1) |
| 947 | control_reg |= I2C_CONTROL_IRQ_SEL; |
| 948 | i2c_writew(control_reg, i2c, OFFSET_CONTROL); |
| 949 | |
| 950 | /* set start condition */ |
| 951 | if (speed_hz <= 100000) |
| 952 | i2c_writew(I2C_ST_START_CON, i2c, OFFSET_EXT_CONF); |
| 953 | else if (speed_hz > MAX_FS_PLUS_MODE_SPEED) { |
| 954 | i2c_writew(I2C_FS_PLUS_START_CON, i2c, OFFSET_EXT_CONF); |
| 955 | } |
| 956 | else { |
| 957 | if (i2c->dev_comp->ext_time_config != 0) |
| 958 | i2c_writew(i2c->dev_comp->ext_time_config, |
| 959 | i2c, OFFSET_EXT_CONF); |
| 960 | else |
| 961 | i2c_writew(I2C_FS_START_CON, i2c, OFFSET_EXT_CONF); |
| 962 | } |
| 963 | |
| 964 | /* delay 5 scl_clk time between two transaction */ |
| 965 | /* if (~control_reg & I2C_CONTROL_RS) */ |
| 966 | i2c_writew(I2C_DELAY_LEN, i2c, OFFSET_DELAY_LEN); |
| 967 | |
| 968 | /* Set ioconfig */ |
| 969 | if (i2c->use_push_pull) { |
| 970 | ioconfig_reg = I2C_IO_CONFIG_PUSH_PULL; |
| 971 | } else { |
| 972 | ioconfig_reg = I2C_IO_CONFIG_OPEN_DRAIN; |
| 973 | if (i2c->dev_comp->set_aed) |
| 974 | ioconfig_reg |= ((i2c->aed<<4) & |
| 975 | I2C_IO_CONFIG_AED_MASK); |
| 976 | } |
| 977 | i2c_writew(ioconfig_reg, i2c, OFFSET_IO_CONFIG); |
| 978 | |
| 979 | if (i2c->dev_comp->ver != 0x2) |
| 980 | i2c_writew(i2c->timing_reg, i2c, OFFSET_TIMING); |
| 981 | else |
| 982 | i2c_writew(i2c->timing_reg | I2C_TIMEOUT_EN, |
| 983 | i2c, OFFSET_TIMING); |
| 984 | if (i2c->dev_comp->set_ltiming) |
| 985 | i2c_writew(i2c->ltiming_reg, i2c, OFFSET_LTIMING); |
| 986 | i2c_writew(i2c->high_speed_reg, i2c, OFFSET_HS); |
| 987 | |
| 988 | if (i2c->have_dcm) |
| 989 | i2c_writew(I2C_DCM_ENABLE, i2c, OFFSET_DCM_EN); |
| 990 | |
| 991 | addr_reg = i2c->addr << 1; |
| 992 | if (i2c->op == I2C_MASTER_RD) |
| 993 | addr_reg |= 0x1; |
| 994 | i2c_writew(addr_reg, i2c, OFFSET_SLAVE_ADDR); |
| 995 | int_reg = I2C_HS_NACKERR | I2C_ACKERR | |
| 996 | I2C_TRANSAC_COMP | I2C_ARB_LOST; |
| 997 | if (i2c->dev_comp->ver == 0x2) |
| 998 | int_reg |= I2C_BUS_ERR | I2C_TIMEOUT; |
| 999 | if (i2c->ch_offset || (i2c->dev_comp->control_irq_sel == 1)) |
| 1000 | int_reg &= ~(I2C_HS_NACKERR | I2C_ACKERR); |
| 1001 | /* Clear interrupt status */ |
| 1002 | i2c_writew(I2C_INTR_ALL, i2c, OFFSET_INTR_STAT); |
| 1003 | if (i2c->ch_offset != 0) |
| 1004 | i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR | I2C_HFIFO_ADDR_CLR, |
| 1005 | i2c, OFFSET_FIFO_ADDR_CLR); |
| 1006 | else |
| 1007 | i2c_writew(I2C_FIFO_ADDR_CLR | I2C_HFIFO_ADDR_CLR, i2c, OFFSET_FIFO_ADDR_CLR); |
| 1008 | |
| 1009 | /* set i3c high speed master code */ |
| 1010 | i2c->i3c_en = (i2c_readw(i2c, OFFSET_DMA_FSM_DEBUG)) & I3C_EN; |
| 1011 | if (i2c->i3c_en && (i2c->speed_hz > MAX_FS_PLUS_MODE_SPEED) |
| 1012 | && (!i2c->hs_only)) { |
| 1013 | i2c_writew(I3C_UNLOCK_HFIFO | I3C_NINTH_BIT | MASTER_CODE, |
| 1014 | i2c, OFFSET_HFIFO_DATA); |
| 1015 | } |
| 1016 | |
| 1017 | /* Enable interrupt */ |
| 1018 | i2c_writew(int_reg, i2c, OFFSET_INTR_MASK); |
| 1019 | |
| 1020 | /* Set transfer and transaction len */ |
| 1021 | if (i2c->op == I2C_MASTER_WRRD) { |
| 1022 | if ((i2c->appm) && (i2c->dev_comp->idvfs_i2c)) { |
| 1023 | i2c_writew( |
| 1024 | (i2c->msg_len & 0xFF) | |
| 1025 | ((i2c->msg_aux_len<<8) & 0x1F00), |
| 1026 | i2c, OFFSET_TRANSFER_LEN); |
| 1027 | } else { |
| 1028 | i2c_writew(i2c->msg_len, i2c, |
| 1029 | OFFSET_TRANSFER_LEN); |
| 1030 | i2c_writew(i2c->msg_aux_len, i2c, |
| 1031 | OFFSET_TRANSFER_LEN_AUX); |
| 1032 | } |
| 1033 | i2c_writew(0x02, i2c, OFFSET_TRANSAC_LEN); |
| 1034 | } else if (i2c->op == I2C_MASTER_MULTI_WR) { |
| 1035 | i2c_writew(i2c->msg_len, i2c, OFFSET_TRANSFER_LEN); |
| 1036 | i2c_writew(i2c->total_len / i2c->msg_len, |
| 1037 | i2c, OFFSET_TRANSAC_LEN); |
| 1038 | } else { |
| 1039 | i2c_writew(i2c->msg_len, i2c, OFFSET_TRANSFER_LEN); |
| 1040 | i2c_writew(0x01, i2c, OFFSET_TRANSAC_LEN); |
| 1041 | } |
| 1042 | |
| 1043 | /* Prepare buffer data to start transfer */ |
| 1044 | if (isDMA == true && (!i2c->is_ccu_trig)) { |
| 1045 | if (i2c_readl_dma(i2c, OFFSET_EN)) { |
| 1046 | i2c_writel_dma(I2C_DMA_WARM_RST, i2c, OFFSET_RST); |
| 1047 | udelay(5); |
| 1048 | } |
| 1049 | #ifdef CONFIG_MTK_LM_MODE |
| 1050 | if ((i2c->dev_comp->dma_support == 1) && (enable_4G())) { |
| 1051 | i2c_writel_dma(0x1, i2c, OFFSET_TX_MEM_ADDR2); |
| 1052 | i2c_writel_dma(0x1, i2c, OFFSET_RX_MEM_ADDR2); |
| 1053 | } |
| 1054 | #endif |
| 1055 | if (i2c->op == I2C_MASTER_RD) { |
| 1056 | i2c_writel_dma(I2C_DMA_INT_FLAG_NONE, |
| 1057 | i2c, OFFSET_INT_FLAG); |
| 1058 | if (i2c->dev_comp->dma_ver == DMA_HW_VERSION1) |
| 1059 | i2c_writel_dma(I2C_DMA_CON_RX | |
| 1060 | I2C_DMA_SKIP_CONFIG | |
| 1061 | I2C_DMA_ASYNC_MODE, i2c, OFFSET_CON); |
| 1062 | else |
| 1063 | i2c_writel_dma(I2C_DMA_CON_RX, i2c, OFFSET_CON); |
| 1064 | i2c_writel_dma( |
| 1065 | lower_32_bits(i2c->dma_buf.paddr), |
| 1066 | i2c, OFFSET_RX_MEM_ADDR); |
| 1067 | if ((i2c->dev_comp->dma_support >= 2)) |
| 1068 | i2c_writel_dma( |
| 1069 | upper_32_bits(i2c->dma_buf.paddr), |
| 1070 | i2c, OFFSET_RX_MEM_ADDR2); |
| 1071 | i2c_writel_dma(i2c->msg_len, i2c, OFFSET_RX_LEN); |
| 1072 | } else if (i2c->op == I2C_MASTER_WR || |
| 1073 | i2c->op == I2C_MASTER_MULTI_WR) { |
| 1074 | i2c_writel_dma(I2C_DMA_INT_FLAG_NONE, |
| 1075 | i2c, OFFSET_INT_FLAG); |
| 1076 | if (i2c->dev_comp->dma_ver == DMA_HW_VERSION1) |
| 1077 | i2c_writel_dma(I2C_DMA_CON_TX | |
| 1078 | I2C_DMA_SKIP_CONFIG | |
| 1079 | I2C_DMA_ASYNC_MODE, i2c, OFFSET_CON); |
| 1080 | else |
| 1081 | i2c_writel_dma(I2C_DMA_CON_TX, i2c, OFFSET_CON); |
| 1082 | i2c_writel_dma( |
| 1083 | lower_32_bits(i2c->dma_buf.paddr), |
| 1084 | i2c, OFFSET_TX_MEM_ADDR); |
| 1085 | if ((i2c->dev_comp->dma_support >= 2)) |
| 1086 | i2c_writel_dma( |
| 1087 | upper_32_bits(i2c->dma_buf.paddr), |
| 1088 | i2c, OFFSET_TX_MEM_ADDR2); |
| 1089 | |
| 1090 | i2c_writel_dma(i2c->total_len, i2c, OFFSET_TX_LEN); |
| 1091 | } else { |
| 1092 | i2c_writel_dma(0x0000, i2c, OFFSET_INT_FLAG); |
| 1093 | if (i2c->dev_comp->dma_ver == DMA_HW_VERSION1) |
| 1094 | i2c_writel_dma(0x0000 | I2C_DMA_SKIP_CONFIG | |
| 1095 | I2C_DMA_ASYNC_MODE | I2C_DMA_DIR_CHANGE, |
| 1096 | i2c, OFFSET_CON); |
| 1097 | else |
| 1098 | i2c_writel_dma(0x0000, i2c, OFFSET_CON); |
| 1099 | i2c_writel_dma(lower_32_bits(i2c->dma_buf.paddr), |
| 1100 | i2c, OFFSET_TX_MEM_ADDR); |
| 1101 | i2c_writel_dma(lower_32_bits(i2c->dma_buf.paddr), |
| 1102 | i2c, OFFSET_RX_MEM_ADDR); |
| 1103 | if ((i2c->dev_comp->dma_support >= 2)) { |
| 1104 | i2c_writel_dma( |
| 1105 | upper_32_bits(i2c->dma_buf.paddr), |
| 1106 | i2c, OFFSET_TX_MEM_ADDR2); |
| 1107 | i2c_writel_dma( |
| 1108 | upper_32_bits(i2c->dma_buf.paddr), |
| 1109 | i2c, OFFSET_RX_MEM_ADDR2); |
| 1110 | } |
| 1111 | i2c_writel_dma(i2c->msg_len, i2c, OFFSET_TX_LEN); |
| 1112 | i2c_writel_dma(i2c->msg_aux_len, i2c, OFFSET_RX_LEN); |
| 1113 | } |
| 1114 | record_i2c_dma_info(i2c); |
| 1115 | /* flush before sending DMA start */ |
| 1116 | mb(); |
| 1117 | i2c_writel_dma(I2C_DMA_START_EN, i2c, OFFSET_EN); |
| 1118 | } else { |
| 1119 | if (i2c->op != I2C_MASTER_RD && (!i2c->is_ccu_trig)) { |
| 1120 | data_size = i2c->total_len; |
| 1121 | ptr = i2c->dma_buf.vaddr; |
| 1122 | while (data_size--) { |
| 1123 | i2c_writew(*ptr, i2c, OFFSET_DATA_PORT); |
| 1124 | ptr++; |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | if (i2c->dev_comp->ver == 0x2) { |
| 1129 | if (!i2c->is_ccu_trig) |
| 1130 | i2c_writew(I2C_MCU_INTR_EN, i2c, OFFSET_MCU_INTR); |
| 1131 | else { |
| 1132 | dev_info(i2c->dev, "I2C CCU trig.\n"); |
| 1133 | return 0; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /* flush before sending start */ |
| 1138 | mb(); |
| 1139 | if (!i2c->is_hw_trig) |
| 1140 | i2c_writew(I2C_TRANSAC_START, i2c, OFFSET_START); |
| 1141 | else { |
| 1142 | dev_info(i2c->dev, "I2C hw trig.\n"); |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | tmo = wait_event_timeout(i2c->wait, i2c->trans_stop, tmo); |
| 1147 | |
| 1148 | record_i2c_info(i2c, tmo); |
| 1149 | |
| 1150 | if (tmo == 0) { |
| 1151 | dev_info(i2c->dev, "addr:0x%x,transfer timeout\n", |
| 1152 | i2c->addr); |
| 1153 | start_reg = i2c_readw(i2c, OFFSET_START); |
| 1154 | dev_info(i2c->dev, |
| 1155 | "timeout:start=0x%x,ch_err=0x%x\n", |
| 1156 | start_reg, i2c_readw(i2c, OFFSET_ERROR)); |
| 1157 | |
| 1158 | i2c_dump_info(i2c); |
| 1159 | i2c_gpio_dump_info(i2c); |
| 1160 | #if defined(CONFIG_MTK_GIC_EXT) |
| 1161 | mt_irq_dump_status(i2c->irqnr); |
| 1162 | #endif |
| 1163 | dump_cg_regs(i2c); |
| 1164 | if (i2c->ch_offset != 0) |
| 1165 | i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR, |
| 1166 | i2c, OFFSET_FIFO_ADDR_CLR); |
| 1167 | else |
| 1168 | i2c_writew(I2C_FIFO_ADDR_CLR, i2c, |
| 1169 | OFFSET_FIFO_ADDR_CLR); |
| 1170 | |
| 1171 | /* This slave addr is used to check whether the shadow RG is */ |
| 1172 | /* mapped normally or not */ |
| 1173 | dev_info(i2c->dev, "SLAVE_ADDR=0x%x (shadow RG)", |
| 1174 | i2c_readw_shadow(i2c, OFFSET_SLAVE_ADDR)); |
| 1175 | mt_i2c_init_hw(i2c); |
| 1176 | if ((i2c->ch_offset) && (start_reg & I2C_RESUME_ARBIT)) { |
| 1177 | i2c_writew_shadow(I2C_RESUME_ARBIT, i2c, OFFSET_START); |
| 1178 | dev_info(i2c->dev, "bus channel transferred\n"); |
| 1179 | } |
| 1180 | |
| 1181 | if (start_reg & I2C_TRANSAC_START) { |
| 1182 | dev_info(i2c->dev, "bus tied low/high(0x%x)\n", |
| 1183 | start_reg); |
| 1184 | return -EIO; |
| 1185 | } |
| 1186 | return -ETIMEDOUT; |
| 1187 | } |
| 1188 | if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR | |
| 1189 | I2C_TIMEOUT | I2C_BUS_ERR | I2C_IBI)) { |
| 1190 | dev_info(i2c->dev, |
| 1191 | "error:addr=0x%x,irq_stat=0x%x,ch_offset=0x%x,mask:0x%x\n", |
| 1192 | i2c->addr, i2c->irq_stat, i2c->ch_offset, int_reg); |
| 1193 | |
| 1194 | /* clear fifo addr:bit2,multi-chn;bit0,normal */ |
| 1195 | i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR, |
| 1196 | i2c, OFFSET_FIFO_ADDR_CLR); |
| 1197 | |
| 1198 | if (i2c->ext_data.isEnable == false || |
| 1199 | i2c->ext_data.isFilterMsg == false) { |
| 1200 | i2c_dump_info(i2c); |
| 1201 | i2c_gpio_dump_info(i2c); |
| 1202 | } else |
| 1203 | dev_info(i2c->dev, "addr:0x%x,ext_data skip more log\n", |
| 1204 | i2c->addr); |
| 1205 | |
| 1206 | if ((i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR))) |
| 1207 | dev_info(i2c->dev, "addr:0x%x,ACK error\n", i2c->addr); |
| 1208 | |
| 1209 | if (i2c->irq_stat & I2C_TIMEOUT) |
| 1210 | dev_info(i2c->dev, "addr:0x%x,SCL tied low timeout error\n", |
| 1211 | i2c->addr); |
| 1212 | |
| 1213 | if ((i2c->irq_stat & I2C_BUS_ERR)) |
| 1214 | dev_info(i2c->dev, |
| 1215 | "bus error:start=0x%x,ch_err=0x%x,dbg_stat=0x%x\n", |
| 1216 | i2c_readw(i2c, OFFSET_START), |
| 1217 | i2c_readw(i2c, OFFSET_ERROR), |
| 1218 | i2c_readw(i2c, OFFSET_DEBUGSTAT)); |
| 1219 | |
| 1220 | if ((i2c->irq_stat & I2C_IBI)) { |
| 1221 | dev_info(i2c->dev, |
| 1222 | "IBI error:start=0x%x,ch_err=0x%x,dbg_stat=0x%x\n", |
| 1223 | i2c_readw(i2c, OFFSET_START), |
| 1224 | i2c_readw(i2c, OFFSET_ERROR), |
| 1225 | i2c_readw(i2c, OFFSET_DEBUGSTAT)); |
| 1226 | } |
| 1227 | |
| 1228 | if ((i2c->irq_stat & I2C_TRANSAC_COMP) && i2c->ch_offset && |
| 1229 | (!(i2c->irq_stat & I2C_BUS_ERR))) { |
| 1230 | dev_info(i2c->dev, "trans done with error"); |
| 1231 | return -EREMOTEIO; |
| 1232 | } |
| 1233 | |
| 1234 | /* Need init&kick if (intr_state&intr_mask) is greater than 1 */ |
| 1235 | if ((i2c->irq_stat & int_reg) > 1) { |
| 1236 | mt_i2c_init_hw(i2c); |
| 1237 | if (i2c->ch_offset) { |
| 1238 | i2c_writew_shadow(I2C_RESUME_ARBIT, |
| 1239 | i2c, OFFSET_START); |
| 1240 | dev_info(i2c->dev, "bus channel transferred\n"); |
| 1241 | } |
| 1242 | } |
| 1243 | return -EREMOTEIO; |
| 1244 | } |
| 1245 | if (i2c->op != I2C_MASTER_WR && isDMA == false) { |
| 1246 | if (i2c->op == I2C_MASTER_WRRD) |
| 1247 | data_size = i2c->msg_aux_len; |
| 1248 | else |
| 1249 | data_size = i2c->msg_len; |
| 1250 | ptr = i2c->dma_buf.vaddr; |
| 1251 | while (data_size--) { |
| 1252 | *ptr = i2c_readw(i2c, OFFSET_DATA_PORT); |
| 1253 | ptr++; |
| 1254 | } |
| 1255 | } |
| 1256 | dev_dbg(i2c->dev, "i2c transferred done.\n"); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static inline void mt_i2c_copy_to_dma(struct mt_i2c *i2c, struct i2c_msg *msg) |
| 1262 | { |
| 1263 | /* |
| 1264 | * if the operate is write, write-read, multi-write, |
| 1265 | * need to copy the data to DMA memory. |
| 1266 | */ |
| 1267 | if (!(msg->flags & I2C_M_RD)) |
| 1268 | memcpy(i2c->dma_buf.vaddr + i2c->total_len - msg->len, |
| 1269 | msg->buf, msg->len); |
| 1270 | } |
| 1271 | |
| 1272 | static inline void mt_i2c_copy_from_dma(struct mt_i2c *i2c, |
| 1273 | struct i2c_msg *msg) |
| 1274 | { |
| 1275 | /* if the operate is read, need to copy the data from DMA memory */ |
| 1276 | if (msg->flags & I2C_M_RD) |
| 1277 | memcpy(msg->buf, i2c->dma_buf.vaddr, msg->len); |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * In MTK platform the STOP will be issued after each |
| 1282 | * message was transferred which is not flow the clarify |
| 1283 | * for i2c_transfer(), several I2C devices tolerate the STOP, |
| 1284 | * but some device need Repeat-Start and do not compatible with STOP |
| 1285 | * MTK platform has WRRD mode which can write then read with |
| 1286 | * Repeat-Start between two message, so we combined two |
| 1287 | * messages into one transaction. |
| 1288 | * The max read length is 4096 |
| 1289 | */ |
| 1290 | static bool mt_i2c_should_combine(struct i2c_msg *msg) |
| 1291 | { |
| 1292 | struct i2c_msg *next_msg = msg + 1; |
| 1293 | |
| 1294 | if ((next_msg->len < 4096) && |
| 1295 | msg->addr == next_msg->addr && |
| 1296 | !(msg->flags & I2C_M_RD) && |
| 1297 | (next_msg->flags & I2C_M_RD) == I2C_M_RD) { |
| 1298 | return true; |
| 1299 | } |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | static bool mt_i2c_should_batch(struct i2c_msg *prev, struct i2c_msg *next) |
| 1304 | { |
| 1305 | if ((prev == NULL) || (next == NULL) || |
| 1306 | (prev->flags & I2C_M_RD) || (next->flags & I2C_M_RD)) |
| 1307 | return false; |
| 1308 | if ((next != NULL) && (prev != NULL) && |
| 1309 | (prev->len == next->len && prev->addr == next->addr)) |
| 1310 | return true; |
| 1311 | return false; |
| 1312 | } |
| 1313 | |
| 1314 | static int __mt_i2c_transfer(struct mt_i2c *i2c, |
| 1315 | struct i2c_msg msgs[], int num) |
| 1316 | { |
| 1317 | int ret; |
| 1318 | int left_num = num; |
| 1319 | |
| 1320 | while (left_num--) { |
| 1321 | /* In MTK platform the max transfer number is 4096 */ |
| 1322 | if (msgs->len > MAX_DMA_TRANS_SIZE) { |
| 1323 | dev_dbg(i2c->dev, |
| 1324 | " message data length is more than 255\n"); |
| 1325 | ret = -EINVAL; |
| 1326 | goto err_exit; |
| 1327 | } |
| 1328 | if (msgs->addr == 0) { |
| 1329 | dev_dbg(i2c->dev, " addr is invalid.\n"); |
| 1330 | ret = -EINVAL; |
| 1331 | goto err_exit; |
| 1332 | } |
| 1333 | if (msgs->buf == NULL) { |
| 1334 | dev_dbg(i2c->dev, " data buffer is NULL.\n"); |
| 1335 | ret = -EINVAL; |
| 1336 | goto err_exit; |
| 1337 | } |
| 1338 | |
| 1339 | i2c->addr = msgs->addr; |
| 1340 | i2c->msg_len = msgs->len; |
| 1341 | i2c->msg_aux_len = 0; |
| 1342 | |
| 1343 | if ((left_num + 1 == num) || |
| 1344 | !mt_i2c_should_batch(msgs - 1, msgs)) { |
| 1345 | i2c->total_len = msgs->len; |
| 1346 | if (msgs->flags & I2C_M_RD) |
| 1347 | i2c->op = I2C_MASTER_RD; |
| 1348 | else |
| 1349 | i2c->op = I2C_MASTER_WR; |
| 1350 | } else { |
| 1351 | i2c->total_len += msgs->len; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * always use DMA mode. |
| 1356 | * 1st when write need copy the data of message to dma memory |
| 1357 | * 2nd when read need copy the DMA data to the message buffer. |
| 1358 | * The length should be less than 255. |
| 1359 | */ |
| 1360 | mt_i2c_copy_to_dma(i2c, msgs); |
| 1361 | |
| 1362 | if (left_num >= 1) { |
| 1363 | if (mt_i2c_should_batch(msgs, msgs + 1)) { |
| 1364 | i2c->op = I2C_MASTER_MULTI_WR; |
| 1365 | msgs++; |
| 1366 | continue; |
| 1367 | } |
| 1368 | if (mt_i2c_should_combine(msgs)) { |
| 1369 | i2c->msg_aux_len = (msgs + 1)->len; |
| 1370 | i2c->op = I2C_MASTER_WRRD; |
| 1371 | left_num--; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | /* Use HW semaphore to protect device access between |
| 1376 | * AP and SPM, or SCP |
| 1377 | */ |
| 1378 | if (i2c_get_semaphore(i2c) != 0) { |
| 1379 | dev_info(i2c->dev, "get hw semaphore failed.\n"); |
| 1380 | return -EBUSY; |
| 1381 | } |
| 1382 | ret = mt_i2c_do_transfer(i2c); |
| 1383 | /* Use HW semaphore to protect device access between |
| 1384 | * AP and SPM, or SCP |
| 1385 | */ |
| 1386 | if (i2c_release_semaphore(i2c) != 0) { |
| 1387 | dev_info(i2c->dev, "release hw semaphore failed.\n"); |
| 1388 | ret = -EBUSY; |
| 1389 | } |
| 1390 | if (ret < 0) |
| 1391 | goto err_exit; |
| 1392 | if (i2c->op == I2C_MASTER_WRRD) |
| 1393 | mt_i2c_copy_from_dma(i2c, msgs + 1); |
| 1394 | else |
| 1395 | mt_i2c_copy_from_dma(i2c, msgs); |
| 1396 | |
| 1397 | msgs++; |
| 1398 | /* after combined two messages so we need ignore one */ |
| 1399 | if (left_num > 0 && i2c->op == I2C_MASTER_WRRD) |
| 1400 | msgs++; |
| 1401 | } |
| 1402 | /* the return value is number of executed messages */ |
| 1403 | ret = num; |
| 1404 | err_exit: |
| 1405 | return ret; |
| 1406 | } |
| 1407 | |
| 1408 | #if (defined(CONFIG_TRUSTONIC_TEE_SUPPORT) || \ |
| 1409 | defined(CONFIG_MICROTRUST_TEE_SUPPORT)) |
| 1410 | int i2c_tui_enable_clock(int id) |
| 1411 | { |
| 1412 | struct i2c_adapter *adap; |
| 1413 | struct mt_i2c *i2c; |
| 1414 | int ret; |
| 1415 | |
| 1416 | adap = i2c_get_adapter(id); |
| 1417 | if (!adap) { |
| 1418 | pr_info("Cannot get adapter\n"); |
| 1419 | return -1; |
| 1420 | } |
| 1421 | |
| 1422 | i2c = i2c_get_adapdata(adap); |
| 1423 | ret = clk_prepare_enable(i2c->clk_main); |
| 1424 | if (ret) { |
| 1425 | pr_info("Cannot enable main clk\n"); |
| 1426 | return ret; |
| 1427 | } |
| 1428 | ret = clk_prepare_enable(i2c->clk_dma); |
| 1429 | if (ret) { |
| 1430 | pr_info("Cannot enable dma clk\n"); |
| 1431 | clk_disable_unprepare(i2c->clk_main); |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | int i2c_tui_disable_clock(int id) |
| 1439 | { |
| 1440 | struct i2c_adapter *adap; |
| 1441 | struct mt_i2c *i2c; |
| 1442 | |
| 1443 | adap = i2c_get_adapter(id); |
| 1444 | if (!adap) { |
| 1445 | pr_info("Cannot get adapter\n"); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | |
| 1449 | i2c = i2c_get_adapdata(adap); |
| 1450 | clk_disable_unprepare(i2c->clk_dma); |
| 1451 | clk_disable_unprepare(i2c->clk_main); |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | #endif |
| 1456 | |
| 1457 | static int mt_i2c_transfer(struct i2c_adapter *adap, |
| 1458 | struct i2c_msg msgs[], int num) |
| 1459 | { |
| 1460 | int ret; |
| 1461 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1462 | |
| 1463 | ret = mt_i2c_clock_enable(i2c); |
| 1464 | if (ret) |
| 1465 | return -EBUSY; |
| 1466 | |
| 1467 | mutex_lock(&i2c->i2c_mutex); |
| 1468 | ret = __mt_i2c_transfer(i2c, msgs, num); |
| 1469 | mutex_unlock(&i2c->i2c_mutex); |
| 1470 | |
| 1471 | mt_i2c_clock_disable(i2c); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | |
| 1476 | static void mt_i2c_parse_extension(struct mt_i2c_ext *pext, u32 ext_flag, |
| 1477 | u32 timing) |
| 1478 | { |
| 1479 | if (ext_flag & I2C_A_FILTER_MSG) |
| 1480 | pext->isFilterMsg = true; |
| 1481 | if (timing) |
| 1482 | pext->timing = timing; |
| 1483 | } |
| 1484 | |
| 1485 | int mtk_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num, |
| 1486 | u32 ext_flag, u32 timing) |
| 1487 | { |
| 1488 | int ret; |
| 1489 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1490 | |
| 1491 | ret = mt_i2c_clock_enable(i2c); |
| 1492 | if (ret) |
| 1493 | return -EBUSY; |
| 1494 | |
| 1495 | mutex_lock(&i2c->i2c_mutex); |
| 1496 | i2c->ext_data.isEnable = true; |
| 1497 | |
| 1498 | mt_i2c_parse_extension(&i2c->ext_data, ext_flag, timing); |
| 1499 | ret = __mt_i2c_transfer(i2c, msgs, num); |
| 1500 | |
| 1501 | i2c->ext_data.isEnable = false; |
| 1502 | mutex_unlock(&i2c->i2c_mutex); |
| 1503 | |
| 1504 | mt_i2c_clock_disable(i2c); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | EXPORT_SYMBOL(mtk_i2c_transfer); |
| 1508 | |
| 1509 | int hw_trig_i2c_enable(struct i2c_adapter *adap) |
| 1510 | { |
| 1511 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1512 | |
| 1513 | if (!i2c->buffermode) |
| 1514 | return -1; |
| 1515 | if (mt_i2c_clock_enable(i2c)) |
| 1516 | return -EBUSY; |
| 1517 | |
| 1518 | mutex_lock(&i2c->i2c_mutex); |
| 1519 | i2c->is_hw_trig = true; |
| 1520 | mutex_unlock(&i2c->i2c_mutex); |
| 1521 | return 0; |
| 1522 | } |
| 1523 | EXPORT_SYMBOL(hw_trig_i2c_enable); |
| 1524 | |
| 1525 | int hw_trig_i2c_disable(struct i2c_adapter *adap) |
| 1526 | { |
| 1527 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1528 | |
| 1529 | if (!i2c->buffermode) |
| 1530 | return -1; |
| 1531 | mutex_lock(&i2c->i2c_mutex); |
| 1532 | i2c->is_hw_trig = false; |
| 1533 | mutex_unlock(&i2c->i2c_mutex); |
| 1534 | mt_i2c_wait_done(i2c, 0); |
| 1535 | mt_i2c_clock_disable(i2c); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | EXPORT_SYMBOL(hw_trig_i2c_disable); |
| 1539 | |
| 1540 | int hw_trig_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, |
| 1541 | int num) |
| 1542 | { |
| 1543 | int ret; |
| 1544 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1545 | |
| 1546 | if (!i2c->buffermode) |
| 1547 | return -1; |
| 1548 | mutex_lock(&i2c->i2c_mutex); |
| 1549 | ret = __mt_i2c_transfer(i2c, msgs, num); |
| 1550 | mutex_unlock(&i2c->i2c_mutex); |
| 1551 | return ret; |
| 1552 | } |
| 1553 | EXPORT_SYMBOL(hw_trig_i2c_transfer); |
| 1554 | |
| 1555 | int i2c_ccu_enable(struct i2c_adapter *adap, u16 ch_offset) |
| 1556 | { |
| 1557 | int ret; |
| 1558 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1559 | char buf[1]; |
| 1560 | /*This is just a dummy msg which is meaningless since these parameter |
| 1561 | * is actually not used. |
| 1562 | */ |
| 1563 | struct i2c_msg dummy_msg = { |
| 1564 | .addr = 0x1, |
| 1565 | .flags = I2C_MASTER_RD, |
| 1566 | .len = 1, |
| 1567 | .buf = (char *)buf, |
| 1568 | }; |
| 1569 | if (mt_i2c_clock_enable(i2c)) |
| 1570 | return -EBUSY; |
| 1571 | mutex_lock(&i2c->i2c_mutex); |
| 1572 | i2c->is_ccu_trig = true; |
| 1573 | i2c->ext_data.ch_offset = ch_offset; |
| 1574 | i2c->ext_data.is_ch_offset = true; |
| 1575 | ret = __mt_i2c_transfer(i2c, &dummy_msg, 1); |
| 1576 | i2c->is_ccu_trig = false; |
| 1577 | i2c->ext_data.is_ch_offset = false; |
| 1578 | mutex_unlock(&i2c->i2c_mutex); |
| 1579 | return ret; |
| 1580 | } |
| 1581 | EXPORT_SYMBOL(i2c_ccu_enable); |
| 1582 | |
| 1583 | int i2c_ccu_disable(struct i2c_adapter *adap) |
| 1584 | { |
| 1585 | struct mt_i2c *i2c = i2c_get_adapdata(adap); |
| 1586 | |
| 1587 | mt_i2c_wait_done(i2c, i2c->ccu_offset); |
| 1588 | mt_i2c_clock_disable(i2c); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | EXPORT_SYMBOL(i2c_ccu_disable); |
| 1592 | |
| 1593 | static irqreturn_t mt_i2c_irq(int irqno, void *dev_id) |
| 1594 | { |
| 1595 | struct mt_i2c *i2c = dev_id; |
| 1596 | |
| 1597 | #if 0 |
| 1598 | /* Clear interrupt mask */ |
| 1599 | i2c_writew(~(I2C_HS_NACKERR | I2C_ACKERR | I2C_TRANSAC_COMP), |
| 1600 | i2c, OFFSET_INTR_MASK); |
| 1601 | i2c->irq_stat = i2c_readw(i2c, OFFSET_INTR_STAT); |
| 1602 | i2c_writew(I2C_HS_NACKERR | I2C_ACKERR | I2C_TRANSAC_COMP, |
| 1603 | i2c, OFFSET_INTR_STAT); |
| 1604 | #endif |
| 1605 | |
| 1606 | /* mask and clear all interrupt for i2c, need think of i3c~~ */ |
| 1607 | i2c_writew(~(I2C_INTR_ALL), i2c, OFFSET_INTR_MASK); |
| 1608 | i2c->irq_stat = i2c_readw(i2c, OFFSET_INTR_STAT); |
| 1609 | i2c_writew(I2C_INTR_ALL, i2c, OFFSET_INTR_STAT); |
| 1610 | i2c->trans_stop = true; |
| 1611 | if (!i2c->is_hw_trig) { |
| 1612 | wake_up(&i2c->wait); |
| 1613 | if (!i2c->irq_stat) { |
| 1614 | dev_info(i2c->dev, "addr: 0x%x, irq stat 0\n", |
| 1615 | i2c->addr); |
| 1616 | |
| 1617 | i2c_dump_info(i2c); |
| 1618 | #if defined(CONFIG_MTK_GIC_EXT) |
| 1619 | mt_irq_dump_status(i2c->irqnr); |
| 1620 | #endif |
| 1621 | } else { |
| 1622 | /* for bxx debug start */ |
| 1623 | if ((i2c->irq_stat & (I2C_IBI | I2C_BUS_ERR))) { |
| 1624 | dev_info(i2c->dev, "[bxx]cg_cnt:%d,irq_stat:0x%x\n", |
| 1625 | i2c->cg_cnt, i2c->irq_stat); |
| 1626 | #if 0 |
| 1627 | dev_info(i2c->dev, "0x84=0x%x\n", |
| 1628 | i2c_readw(i2c, OFFSET_ERROR)); |
| 1629 | |
| 1630 | pr_info("[bxx]0xE0=0x%x,0x1E0=0x%x,0x2E0=0x%x\n", |
| 1631 | _i2c_readw(i2c, 0xE0), |
| 1632 | _i2c_readw(i2c, 0x1E0), |
| 1633 | _i2c_readw(i2c, 0x2E0)); |
| 1634 | pr_info("[bxx]0xE4=0x%x,0x1E4=0x%x,0x2E4=0x%x\n", |
| 1635 | _i2c_readw(i2c, 0xE4), |
| 1636 | _i2c_readw(i2c, 0x1E4), |
| 1637 | _i2c_readw(i2c, 0x2E4)); |
| 1638 | pr_info("[bxx]0xE8=0x%x,0x1E8=0x%x,0x2E8=0x%x\n", |
| 1639 | _i2c_readw(i2c, 0xE8), |
| 1640 | _i2c_readw(i2c, 0x1E8), |
| 1641 | _i2c_readw(i2c, 0x2E8)); |
| 1642 | pr_info("[bxx]0xEC=0x%x,0x1EC=0x%x,0x2EC=0x%x\n", |
| 1643 | _i2c_readw(i2c, 0xEC), |
| 1644 | _i2c_readw(i2c, 0x1EC), |
| 1645 | _i2c_readw(i2c, 0x2EC)); |
| 1646 | pr_info("[bxx]0x58=0x%x,0x158=0x%x,0x258=0x%x\n", |
| 1647 | _i2c_readw(i2c, 0x58), |
| 1648 | _i2c_readw(i2c, 0x158), |
| 1649 | _i2c_readw(i2c, 0x258)); |
| 1650 | /* IBI triggered */ |
| 1651 | pr_info("[bxx]0x54=0x%x,0x154=0x%x,0x254=0x%x\n", |
| 1652 | _i2c_readw(i2c, 0x54), |
| 1653 | _i2c_readw(i2c, 0x154), |
| 1654 | _i2c_readw(i2c, 0x254)); |
| 1655 | /* for bxx debug end */ |
| 1656 | #endif |
| 1657 | } |
| 1658 | } |
| 1659 | } else {/* dump regs info for hw trig i2c if ACK err */ |
| 1660 | if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) { |
| 1661 | dev_info(i2c->dev, "addr:0x%x,irq_stat:0x%x,transfer ACK error\n", |
| 1662 | i2c->addr, i2c->irq_stat); |
| 1663 | i2c_dump_info(i2c); |
| 1664 | mt_i2c_init_hw(i2c); |
| 1665 | } else { |
| 1666 | dev_info(i2c->dev, "addr:0x%x, other irq_stat:0x%x\n", |
| 1667 | i2c->addr, i2c->irq_stat); |
| 1668 | } |
| 1669 | } |
| 1670 | return IRQ_HANDLED; |
| 1671 | } |
| 1672 | |
| 1673 | static u32 mt_i2c_functionality(struct i2c_adapter *adap) |
| 1674 | { |
| 1675 | return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL; |
| 1676 | } |
| 1677 | |
| 1678 | static const struct i2c_algorithm mt_i2c_algorithm = { |
| 1679 | .master_xfer = mt_i2c_transfer, |
| 1680 | .functionality = mt_i2c_functionality, |
| 1681 | }; |
| 1682 | |
| 1683 | static int mt_i2c_parse_dt(struct device_node *np, struct mt_i2c *i2c) |
| 1684 | { |
| 1685 | int ret = -1; |
| 1686 | |
| 1687 | i2c->speed_hz = I2C_DEFAUT_SPEED; |
| 1688 | of_property_read_u32(np, "clock-frequency", &i2c->speed_hz); |
| 1689 | of_property_read_u32(np, "clock-div", &i2c->clk_src_div); |
| 1690 | of_property_read_u32(np, "scl-gpio-id", &i2c->scl_gpio_id); |
| 1691 | of_property_read_u32(np, "sda-gpio-id", &i2c->sda_gpio_id); |
| 1692 | of_property_read_u32(np, "gpio_start", &i2c->gpio_start); |
| 1693 | of_property_read_u32(np, "mem_len", &i2c->mem_len); |
| 1694 | of_property_read_u32(np, "eh_cfg", &i2c->offset_eh_cfg); |
| 1695 | of_property_read_u32(np, "pu_cfg", &i2c->offset_pu_cfg); |
| 1696 | of_property_read_u32(np, "rsel_cfg", &i2c->offset_rsel_cfg); |
| 1697 | of_property_read_u32(np, "id", (u32 *)&i2c->id); |
| 1698 | of_property_read_u16(np, "clk_sta_offset", |
| 1699 | (u16 *)&i2c->clk_sta_offset); |
| 1700 | of_property_read_u8(np, "cg_bit", (u8 *)&i2c->cg_bit); |
| 1701 | of_property_read_u32(np, "aed", &i2c->aed); |
| 1702 | of_property_read_u32(np, "ch_offset_default", |
| 1703 | &i2c->ch_offset_default); |
| 1704 | of_property_read_u32(np, "ch_offset_dma_default", |
| 1705 | &i2c->ch_offset_dma_default); |
| 1706 | ret = of_property_read_u32(np, "ch_offset_ccu", &i2c->ccu_offset); |
| 1707 | if (!ret) |
| 1708 | i2c->has_ccu = true; |
| 1709 | else |
| 1710 | i2c->has_ccu = false; |
| 1711 | |
| 1712 | i2c->have_pmic |
| 1713 | = of_property_read_bool(np, "mediatek,have-pmic"); |
| 1714 | i2c->have_dcm |
| 1715 | = of_property_read_bool(np, "mediatek,have-dcm"); |
| 1716 | i2c->use_push_pull |
| 1717 | = of_property_read_bool(np, "mediatek,use-push-pull"); |
| 1718 | i2c->appm |
| 1719 | = of_property_read_bool(np, "mediatek,appm_used"); |
| 1720 | i2c->gpupm |
| 1721 | = of_property_read_bool(np, "mediatek,gpupm_used"); |
| 1722 | i2c->buffermode = of_property_read_bool(np, "mediatek,buffermode_used"); |
| 1723 | i2c->hs_only = of_property_read_bool(np, "mediatek,hs_only"); |
| 1724 | i2c->fifo_only = of_property_read_bool(np, "mediatek,fifo_only"); |
| 1725 | pr_info("[I2C]id:%d,freq:%d,div:%d,ch_offset:0x%x,offset_dma:0x%x,offset_ccu:0x%x\n", |
| 1726 | i2c->id, i2c->speed_hz, i2c->clk_src_div, |
| 1727 | i2c->ch_offset_default, |
| 1728 | i2c->ch_offset_dma_default, i2c->ccu_offset); |
| 1729 | if (i2c->clk_src_div == 0) |
| 1730 | return -EINVAL; |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | int mt_i2c_parse_comp_data(void) |
| 1736 | { |
| 1737 | int ret = -1; |
| 1738 | struct device_node *comp_node; |
| 1739 | |
| 1740 | comp_node = of_find_compatible_node(NULL, NULL, "mediatek,i2c_common"); |
| 1741 | if (!comp_node) { |
| 1742 | pr_info("Cannot find i2c_common node\n"); |
| 1743 | return -ENODEV; |
| 1744 | } |
| 1745 | of_property_read_u8(comp_node, "dma_support", |
| 1746 | (u8 *)&i2c_common_compat.dma_support); |
| 1747 | of_property_read_u8(comp_node, "idvfs", |
| 1748 | (u8 *)&i2c_common_compat.idvfs_i2c); |
| 1749 | of_property_read_u8(comp_node, "set_dt_div", |
| 1750 | (u8 *)&i2c_common_compat.set_dt_div); |
| 1751 | of_property_read_u8(comp_node, "check_max_freq", |
| 1752 | (u8 *)&i2c_common_compat.check_max_freq); |
| 1753 | of_property_read_u8(comp_node, "set_ltiming", |
| 1754 | (u8 *)&i2c_common_compat.set_ltiming); |
| 1755 | of_property_read_u8(comp_node, "set_aed", |
| 1756 | (u8 *)&i2c_common_compat.set_aed); |
| 1757 | of_property_read_u16(comp_node, "ext_time_config", |
| 1758 | (u16 *)&i2c_common_compat.ext_time_config); |
| 1759 | ret = of_property_count_u8_elems(comp_node, "clk_compatible"); |
| 1760 | if (ret > 0) |
| 1761 | of_property_read_u8_array(comp_node, "clk_compatible", |
| 1762 | (u8 *)i2c_common_compat.clk_compatible, ret); |
| 1763 | else |
| 1764 | pr_info("[I2C]No clk_compatible(%d)\n", ret); |
| 1765 | of_property_read_u32(comp_node, "clk_sel_offset", |
| 1766 | (u32 *)&i2c_common_compat.clk_sel_offset); |
| 1767 | of_property_read_u32(comp_node, "arbit_offset", |
| 1768 | (u32 *)&i2c_common_compat.arbit_offset); |
| 1769 | of_property_read_u8(comp_node, "ver", |
| 1770 | (u8 *)&i2c_common_compat.ver); |
| 1771 | of_property_read_u8(comp_node, "dma_ver", |
| 1772 | (u8 *)&i2c_common_compat.dma_ver); |
| 1773 | of_property_read_u8(comp_node, "cnt_constraint", |
| 1774 | (u8 *)&i2c_common_compat.cnt_constraint); |
| 1775 | of_property_read_u8(comp_node, "control_irq_sel", |
| 1776 | (u8 *)&i2c_common_compat.control_irq_sel); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | static const struct of_device_id mtk_i2c_of_match[] = { |
| 1781 | { .compatible = "mediatek,i2c", .data = &i2c_common_compat}, |
| 1782 | {}, |
| 1783 | }; |
| 1784 | |
| 1785 | MODULE_DEVICE_TABLE(of, mtk_i2c_of_match); |
| 1786 | |
| 1787 | static int mt_i2c_probe(struct platform_device *pdev) |
| 1788 | { |
| 1789 | int ret = 0; |
| 1790 | struct mt_i2c *i2c; |
| 1791 | unsigned int clk_src_in_hz; |
| 1792 | struct resource *res; |
| 1793 | const struct of_device_id *of_id; |
| 1794 | |
| 1795 | i2c = devm_kzalloc(&pdev->dev, sizeof(struct mt_i2c), GFP_KERNEL); |
| 1796 | if (i2c == NULL) |
| 1797 | return -ENOMEM; |
| 1798 | |
| 1799 | ret = mt_i2c_parse_dt(pdev->dev.of_node, i2c); |
| 1800 | if (ret) |
| 1801 | return -EINVAL; |
| 1802 | |
| 1803 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1804 | |
| 1805 | i2c->base = devm_ioremap_resource(&pdev->dev, res); |
| 1806 | if (IS_ERR(i2c->base)) |
| 1807 | return PTR_ERR(i2c->base); |
| 1808 | |
| 1809 | if (i2c->id < I2C_MAX_CHANNEL) |
| 1810 | g_mt_i2c[i2c->id] = i2c; |
| 1811 | |
| 1812 | if (!i2c->fifo_only) { |
| 1813 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1814 | |
| 1815 | i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res); |
| 1816 | if (IS_ERR(i2c->pdmabase)) |
| 1817 | return PTR_ERR(i2c->pdmabase); |
| 1818 | } |
| 1819 | |
| 1820 | i2c->gpiobase = devm_ioremap(&pdev->dev, i2c->gpio_start, i2c->mem_len); |
| 1821 | if (IS_ERR(i2c->gpiobase)) { |
| 1822 | i2c->gpiobase = NULL; |
| 1823 | dev_info(&pdev->dev, "do not have gpio baseaddress node\n"); |
| 1824 | } |
| 1825 | |
| 1826 | i2c->irqnr = platform_get_irq(pdev, 0); |
| 1827 | if (i2c->irqnr <= 0) |
| 1828 | return -EINVAL; |
| 1829 | init_waitqueue_head(&i2c->wait); |
| 1830 | |
| 1831 | ret = devm_request_irq(&pdev->dev, i2c->irqnr, mt_i2c_irq, |
| 1832 | IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, I2C_DRV_NAME, i2c); |
| 1833 | if (ret < 0) { |
| 1834 | dev_info(&pdev->dev, |
| 1835 | "Request I2C IRQ %d fail\n", i2c->irqnr); |
| 1836 | return ret; |
| 1837 | } |
| 1838 | of_id = of_match_node(mtk_i2c_of_match, pdev->dev.of_node); |
| 1839 | if (!of_id) |
| 1840 | return -EINVAL; |
| 1841 | |
| 1842 | i2c->dev_comp = of_id->data; |
| 1843 | i2c->adap.dev.of_node = pdev->dev.of_node; |
| 1844 | i2c->dev = &i2c->adap.dev; |
| 1845 | i2c->adap.dev.parent = &pdev->dev; |
| 1846 | i2c->adap.owner = THIS_MODULE; |
| 1847 | i2c->adap.algo = &mt_i2c_algorithm; |
| 1848 | i2c->adap.algo_data = NULL; |
| 1849 | i2c->adap.timeout = 2 * HZ; |
| 1850 | i2c->adap.retries = 1; |
| 1851 | i2c->adap.nr = i2c->id; |
| 1852 | spin_lock_init(&i2c->cg_lock); |
| 1853 | |
| 1854 | if (i2c->dev_comp->dma_support == MDA_SUPPORT_8G) { |
| 1855 | if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(33))) { |
| 1856 | dev_info(&pdev->dev, "dma_set_mask return error.\n"); |
| 1857 | return -EINVAL; |
| 1858 | } |
| 1859 | } else if (i2c->dev_comp->dma_support == DMA_SUPPORT_64G) { |
| 1860 | if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(36))) { |
| 1861 | dev_info(&pdev->dev, "dma_set_mask return error.\n"); |
| 1862 | return -EINVAL; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | #if !defined(CONFIG_MT_I2C_FPGA_ENABLE) |
| 1867 | i2c->clk_main = devm_clk_get(&pdev->dev, "main"); |
| 1868 | if (IS_ERR(i2c->clk_main)) { |
| 1869 | dev_info(&pdev->dev, "cannot get main clock\n"); |
| 1870 | return PTR_ERR(i2c->clk_main); |
| 1871 | } |
| 1872 | i2c->clk_dma = devm_clk_get(&pdev->dev, "dma"); |
| 1873 | if (IS_ERR(i2c->clk_dma)) { |
| 1874 | if (!i2c->fifo_only) { |
| 1875 | dev_info(&pdev->dev, "cannot get dma clock\n"); |
| 1876 | return PTR_ERR(i2c->clk_dma); |
| 1877 | } |
| 1878 | i2c->clk_dma = NULL; |
| 1879 | } |
| 1880 | i2c->clk_arb = devm_clk_get(&pdev->dev, "arb"); |
| 1881 | if (IS_ERR(i2c->clk_arb)) |
| 1882 | i2c->clk_arb = NULL; |
| 1883 | else |
| 1884 | dev_dbg(&pdev->dev, "i2c%d has the relevant arbitrator clk.\n", |
| 1885 | i2c->id); |
| 1886 | i2c->clk_pal = devm_clk_get(&pdev->dev, "pal"); |
| 1887 | if (IS_ERR(i2c->clk_pal)) |
| 1888 | i2c->clk_pal = NULL; |
| 1889 | else |
| 1890 | dev_dbg(&pdev->dev, "i2c%d has the relevant pal clk.\n", |
| 1891 | i2c->id); |
| 1892 | #endif |
| 1893 | |
| 1894 | if (i2c->have_pmic) { |
| 1895 | i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic"); |
| 1896 | if (IS_ERR(i2c->clk_pmic)) { |
| 1897 | dev_info(&pdev->dev, "cannot get pmic clock\n"); |
| 1898 | return PTR_ERR(i2c->clk_pmic); |
| 1899 | } |
| 1900 | clk_src_in_hz = clk_get_rate(i2c->clk_pmic) / i2c->clk_src_div; |
| 1901 | } else { |
| 1902 | clk_src_in_hz = clk_get_rate(i2c->clk_main) / i2c->clk_src_div; |
| 1903 | } |
| 1904 | i2c->main_clk = clk_src_in_hz; |
| 1905 | dev_info(&pdev->dev, "i2c%d clock source %p,clock src frequency %d\n", |
| 1906 | i2c->id, i2c->clk_main, clk_src_in_hz); |
| 1907 | |
| 1908 | strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); |
| 1909 | mutex_init(&i2c->i2c_mutex); |
| 1910 | ret = i2c_set_speed(i2c, clk_src_in_hz); |
| 1911 | if (ret) { |
| 1912 | dev_info(&pdev->dev, "Failed to set the speed\n"); |
| 1913 | return -EINVAL; |
| 1914 | } |
| 1915 | ret = mt_i2c_clock_enable(i2c); |
| 1916 | if (ret) { |
| 1917 | dev_info(&pdev->dev, "clock enable failed!\n"); |
| 1918 | return ret; |
| 1919 | } |
| 1920 | mt_i2c_init_hw(i2c); |
| 1921 | |
| 1922 | mt_i2c_clock_disable(i2c); |
| 1923 | if (i2c->ch_offset_default) |
| 1924 | i2c->dma_buf.vaddr = dma_alloc_coherent(&pdev->dev, |
| 1925 | (PAGE_SIZE * 2), &i2c->dma_buf.paddr, GFP_KERNEL); |
| 1926 | else |
| 1927 | i2c->dma_buf.vaddr = dma_alloc_coherent(&pdev->dev, |
| 1928 | PAGE_SIZE, &i2c->dma_buf.paddr, GFP_KERNEL); |
| 1929 | |
| 1930 | if (i2c->dma_buf.vaddr == NULL) { |
| 1931 | dev_info(&pdev->dev, "dma_alloc_coherent fail\n"); |
| 1932 | return -ENOMEM; |
| 1933 | } |
| 1934 | i2c_set_adapdata(&i2c->adap, i2c); |
| 1935 | /* ret = i2c_add_adapter(&i2c->adap); */ |
| 1936 | ret = i2c_add_numbered_adapter(&i2c->adap); |
| 1937 | if (ret) { |
| 1938 | dev_info(&pdev->dev, "Failed to add i2c bus to i2c core\n"); |
| 1939 | free_i2c_dma_bufs(i2c); |
| 1940 | return ret; |
| 1941 | } |
| 1942 | platform_set_drvdata(pdev, i2c); |
| 1943 | |
| 1944 | if (!map_cg_regs(i2c)) |
| 1945 | pr_info("Map cg regs successfully.\n"); |
| 1946 | |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
| 1950 | static int mt_i2c_remove(struct platform_device *pdev) |
| 1951 | { |
| 1952 | struct mt_i2c *i2c = platform_get_drvdata(pdev); |
| 1953 | |
| 1954 | i2c_del_adapter(&i2c->adap); |
| 1955 | free_i2c_dma_bufs(i2c); |
| 1956 | platform_set_drvdata(pdev, NULL); |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | MODULE_DEVICE_TABLE(of, mt_i2c_match); |
| 1962 | |
| 1963 | #ifdef CONFIG_PM_SLEEP |
| 1964 | static int mt_i2c_suspend_noirq(struct device *dev) |
| 1965 | { |
| 1966 | struct platform_device *pdev = to_platform_device(dev); |
| 1967 | struct mt_i2c *i2c = platform_get_drvdata(pdev); |
| 1968 | int ret = 0; |
| 1969 | |
| 1970 | spin_lock(&i2c->cg_lock); |
| 1971 | if (i2c->cg_cnt > 0) { |
| 1972 | ret = -EBUSY; |
| 1973 | dev_info(i2c->dev, "%s(%d) busy\n", __func__, i2c->cg_cnt); |
| 1974 | } else |
| 1975 | i2c->suspended = true; |
| 1976 | spin_unlock(&i2c->cg_lock); |
| 1977 | |
| 1978 | return ret; |
| 1979 | } |
| 1980 | |
| 1981 | static int mt_i2c_resume_noirq(struct device *dev) |
| 1982 | { |
| 1983 | struct platform_device *pdev = to_platform_device(dev); |
| 1984 | struct mt_i2c *i2c = platform_get_drvdata(pdev); |
| 1985 | |
| 1986 | spin_lock(&i2c->cg_lock); |
| 1987 | i2c->suspended = false; |
| 1988 | spin_unlock(&i2c->cg_lock); |
| 1989 | |
| 1990 | if (i2c->ch_offset_default) { |
| 1991 | if (mt_i2c_clock_enable(i2c)) |
| 1992 | dev_info(i2c->dev, "%s enable clock failed\n", |
| 1993 | __func__); |
| 1994 | #if 0 |
| 1995 | /* Disable rollback mode for multi-channel */ |
| 1996 | // mt_secure_call(MTK_SIP_KERNEL_I2C_SEC_WRITE, |
| 1997 | // i2c->id, V2_OFFSET_ROLLBACK, 0); |
| 1998 | |
| 1999 | /* Enable multi-channel DMA mode at ATF */ |
| 2000 | mt_secure_call(MTK_SIP_KERNEL_I2C_SEC_WRITE, i2c->id, |
| 2001 | V2_OFFSET_MULTI_DMA, I2C_SHADOW_REG_MODE, 0); |
| 2002 | #endif |
| 2003 | |
| 2004 | mt_i2c_clock_disable(i2c); |
| 2005 | } |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | #endif |
| 2010 | |
| 2011 | static const struct dev_pm_ops mt_i2c_dev_pm_ops = { |
| 2012 | #ifdef CONFIG_PM_SLEEP |
| 2013 | .suspend_noirq = mt_i2c_suspend_noirq, |
| 2014 | .resume_noirq = mt_i2c_resume_noirq, |
| 2015 | #endif |
| 2016 | }; |
| 2017 | |
| 2018 | static struct platform_driver mt_i2c_driver = { |
| 2019 | .probe = mt_i2c_probe, |
| 2020 | .remove = mt_i2c_remove, |
| 2021 | .driver = { |
| 2022 | .name = I2C_DRV_NAME, |
| 2023 | .owner = THIS_MODULE, |
| 2024 | .pm = &mt_i2c_dev_pm_ops, |
| 2025 | .of_match_table = of_match_ptr(mtk_i2c_of_match), |
| 2026 | }, |
| 2027 | }; |
| 2028 | |
| 2029 | #ifdef CONFIG_MTK_I2C_ARBITRATION |
| 2030 | static s32 enable_arbitration(void) |
| 2031 | { |
| 2032 | struct device_node *pericfg_node; |
| 2033 | void __iomem *pericfg_base; |
| 2034 | |
| 2035 | pericfg_node = of_find_compatible_node(NULL, NULL, "mediatek,pericfg"); |
| 2036 | if (!pericfg_node) { |
| 2037 | pr_info("Cannot find pericfg node\n"); |
| 2038 | return -ENODEV; |
| 2039 | } |
| 2040 | pericfg_base = of_iomap(pericfg_node, 0); |
| 2041 | if (!pericfg_base) { |
| 2042 | pr_info("pericfg iomap failed\n"); |
| 2043 | return -ENOMEM; |
| 2044 | } |
| 2045 | /* Enable the I2C arbitration */ |
| 2046 | writew(0x3, pericfg_base + OFFSET_PERI_I2C_MODE_ENABLE); |
| 2047 | return 0; |
| 2048 | } |
| 2049 | #endif |
| 2050 | |
| 2051 | static s32 __init mt_i2c_init(void) |
| 2052 | { |
| 2053 | #ifdef CONFIG_MTK_I2C_ARBITRATION |
| 2054 | int ret; |
| 2055 | |
| 2056 | ret = enable_arbitration(); |
| 2057 | if (ret) { |
| 2058 | pr_info("Cannot enalbe arbitration.\n"); |
| 2059 | return ret; |
| 2060 | } |
| 2061 | #endif |
| 2062 | |
| 2063 | if (!map_dma_regs()) |
| 2064 | pr_info("Mapp dma regs successfully.\n"); |
| 2065 | if (!mt_i2c_parse_comp_data()) |
| 2066 | pr_info("Get compatible data from dts successfully.\n"); |
| 2067 | |
| 2068 | pr_info("%s: driver as platform device\n", __func__); |
| 2069 | return platform_driver_register(&mt_i2c_driver); |
| 2070 | } |
| 2071 | |
| 2072 | static void __exit mt_i2c_exit(void) |
| 2073 | { |
| 2074 | platform_driver_unregister(&mt_i2c_driver); |
| 2075 | } |
| 2076 | |
| 2077 | module_init(mt_i2c_init); |
| 2078 | module_exit(mt_i2c_exit); |
| 2079 | |
| 2080 | /* module_platform_driver(mt_i2c_driver); */ |
| 2081 | |
| 2082 | MODULE_LICENSE("GPL"); |
| 2083 | MODULE_DESCRIPTION("MediaTek I2C Bus Driver"); |
| 2084 | MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>"); |