yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* linux/drivers/i2c/busses/i2c-zx29.c |
| 2 | * |
| 3 | * Copyright (C) 2015 Sanechips-TSP |
| 4 | * |
| 5 | * ----------NOTICE------------- |
| 6 | * ZX29 serials I2C Controller driver used by zx297520, this driver |
| 7 | * do not support slave mode. bus clock rate should between 400KHz--100KHz. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/time.h> |
| 32 | #include <linux/hrtimer.h> |
| 33 | #include <linux/ktime.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/err.h> |
| 38 | #include <linux/platform_device.h> |
| 39 | #include <linux/clk.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/io.h> |
| 42 | //#include <linux/of_i2c.h> |
| 43 | #include <linux/of_gpio.h> |
| 44 | #include <linux/clk/zx29-clk.h> |
| 45 | |
| 46 | |
| 47 | #include <asm/irq.h> |
| 48 | #include <mach/i2c_private.h> |
| 49 | #include <mach/spinlock.h> |
| 50 | #include <linux/soc/zte/pm/drv_idle.h> |
| 51 | |
| 52 | /*#define DEBUG_I2C_ADAPTER*/ |
| 53 | |
| 54 | #ifdef DEBUG_I2C_ADAPTER |
| 55 | #define drv_printk(fmt, arg...) printk(KERN_DEBUG fmt, ##arg) |
| 56 | #pragma GCC optimize("O0") |
| 57 | #else |
| 58 | #define drv_printk(fmt, arg...) |
| 59 | #endif |
| 60 | |
| 61 | #define I2C_PSM_CONTROL (1) |
| 62 | #define I2C_LONG_TRANSFER (1) |
| 63 | |
| 64 | #define I2C_FIFO_DEPTH 32 |
| 65 | #define I2C_TIMEOUT (msecs_to_jiffies(1000)) |
| 66 | #define MAX_BUS_CLK 400000 |
| 67 | #define MIN_BUS_CLK 100000 |
| 68 | |
| 69 | #define I2C_WCLK_FREQ (26*1000*1000) |
| 70 | |
| 71 | #if I2C_PSM_CONTROL |
| 72 | static volatile unsigned int i2c_active_count = 0; |
| 73 | #endif |
| 74 | |
| 75 | #if I2C_LONG_TRANSFER |
| 76 | /* functions for hrtimer */ |
| 77 | |
| 78 | static enum hrtimer_restart zx29_i2c_timer_callback(struct hrtimer *timer) |
| 79 | { |
| 80 | struct zx29_i2c *i2c; |
| 81 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 82 | |
| 83 | i2c = container_of(timer, struct zx29_i2c, hr_timer); |
| 84 | |
| 85 | if ((i2c->state == STATE_READ) && (i2c->buf_remaining > I2C_FIFO_DEPTH)) { |
| 86 | unsigned int len_rx = 0; |
| 87 | unsigned int len_diff = 0; |
| 88 | u8 *buf = i2c->msg->buf; |
| 89 | u8 val = 0; |
| 90 | |
| 91 | len_rx = i2c_get_rx_fifo_length(i2c); |
| 92 | len_diff = i2c->buf_remaining - I2C_FIFO_DEPTH; |
| 93 | if (len_rx < len_diff) { |
| 94 | ret = HRTIMER_RESTART; |
| 95 | hrtimer_forward_now(timer, i2c->ktime); |
| 96 | } else { |
| 97 | len_rx = len_diff; |
| 98 | } |
| 99 | |
| 100 | i2c->buf_remaining -= len_rx; |
| 101 | |
| 102 | /*read data from fifo*/ |
| 103 | while (len_rx--) { |
| 104 | val = (u8)i2c_read_data(i2c); |
| 105 | *(buf + i2c->msg_ptr) = val; |
| 106 | i2c->msg_ptr++; |
| 107 | } |
| 108 | } else if ((i2c->state == STATE_WRITE) && (i2c->buf_remaining > 0)) { |
| 109 | unsigned int len_tx = 0; |
| 110 | unsigned int len_diff = 0; |
| 111 | u8 *buf = i2c->msg->buf; |
| 112 | u8 val = 0; |
| 113 | |
| 114 | len_tx = i2c_get_tx_fifo_length(i2c); |
| 115 | len_tx = I2C_FIFO_DEPTH - len_tx; |
| 116 | len_diff = i2c->buf_remaining; |
| 117 | if (len_tx < len_diff) { |
| 118 | ret = HRTIMER_RESTART; |
| 119 | hrtimer_forward_now(timer, i2c->ktime); |
| 120 | } else { |
| 121 | len_tx = len_diff; |
| 122 | } |
| 123 | |
| 124 | i2c->buf_remaining -= len_tx; |
| 125 | |
| 126 | /* write data to fifo */ |
| 127 | while (len_tx--) { |
| 128 | val = *(buf + i2c->msg_ptr); |
| 129 | i2c_write_data(i2c, val); |
| 130 | i2c->msg_ptr++; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static void zx29_i2c_start_timer( struct zx29_i2c *i2c ) |
| 138 | { |
| 139 | unsigned long delay_in_us = (9*I2C_FIFO_DEPTH/2)*1000*1000 / i2c->clkrate; |
| 140 | |
| 141 | i2c->ktime = ktime_set(0, delay_in_us * 1000); |
| 142 | |
| 143 | hrtimer_init( &i2c->hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL ); |
| 144 | |
| 145 | i2c->hr_timer.function = zx29_i2c_timer_callback; |
| 146 | |
| 147 | hrtimer_start( &i2c->hr_timer, i2c->ktime, HRTIMER_MODE_REL ); |
| 148 | } |
| 149 | |
| 150 | static void zx29_i2c_stop_timer( struct zx29_i2c *i2c ) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | ret = hrtimer_cancel(&i2c->hr_timer); |
| 155 | if(ret){ |
| 156 | pr_info("%s return %d\n", __FUNCTION__, ret); |
| 157 | } |
| 158 | |
| 159 | return; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | /* functions for hrtimer end */ |
| 164 | |
| 165 | #if I2C_PSM_CONTROL |
| 166 | static void zx29_i2c_set_active(struct wake_lock *lock) |
| 167 | { |
| 168 | unsigned long flags; |
| 169 | |
| 170 | local_irq_save(flags); |
| 171 | |
| 172 | if(i2c_active_count == 0) |
| 173 | { |
| 174 | zx_cpuidle_set_busy(IDLE_FLAG_I2C); |
| 175 | } |
| 176 | i2c_active_count++; |
| 177 | |
| 178 | local_irq_restore(flags); |
| 179 | |
| 180 | wake_lock(lock); |
| 181 | } |
| 182 | |
| 183 | static void zx29_i2c_set_idle(struct wake_lock *lock) |
| 184 | { |
| 185 | unsigned long flags; |
| 186 | |
| 187 | local_irq_save(flags); |
| 188 | |
| 189 | i2c_active_count--; |
| 190 | if(i2c_active_count == 0) |
| 191 | { |
| 192 | zx_cpuidle_set_free(IDLE_FLAG_I2C); |
| 193 | } |
| 194 | |
| 195 | local_irq_restore(flags); |
| 196 | |
| 197 | wake_unlock(lock); |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | #ifdef CONFIG_SYSFS |
| 202 | static ssize_t zx29_i2c_sysfs_show(struct device *dev, |
| 203 | struct device_attribute *attr, char *buf); |
| 204 | static ssize_t zx29_i2c_sysfs_store(struct device *dev, |
| 205 | struct device_attribute *attr, const char *buf, size_t count); |
| 206 | |
| 207 | |
| 208 | DEVICE_ATTR(clk_rate, S_IWUSR | S_IRUGO, zx29_i2c_sysfs_show, zx29_i2c_sysfs_store); |
| 209 | |
| 210 | static struct attribute * zx29_i2c_sysfs_attrs[] = { |
| 211 | &dev_attr_clk_rate.attr, |
| 212 | NULL |
| 213 | }; |
| 214 | |
| 215 | static const struct attribute_group zx29_i2c_sysfs_attr_group = { |
| 216 | .attrs = zx29_i2c_sysfs_attrs, |
| 217 | }; |
| 218 | |
| 219 | static int zx29_i2c_sysfs_create_group(struct zx29_i2c *i2c) |
| 220 | { |
| 221 | return sysfs_create_group(&i2c->dev->kobj, &zx29_i2c_sysfs_attr_group); |
| 222 | } |
| 223 | |
| 224 | static void zx29_i2c_sysfs_remove_group(struct zx29_i2c *i2c) |
| 225 | { |
| 226 | sysfs_remove_group(&i2c->dev->kobj, &zx29_i2c_sysfs_attr_group); |
| 227 | } |
| 228 | |
| 229 | static ssize_t zx29_i2c_sysfs_show(struct device *dev, |
| 230 | struct device_attribute *attr, char *buf) |
| 231 | { |
| 232 | struct zx29_i2c *i2c = dev_get_drvdata(dev); |
| 233 | |
| 234 | if (!i2c) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | return scnprintf(buf, PAGE_SIZE, "%ld\n", i2c->clkrate); |
| 238 | } |
| 239 | |
| 240 | static ssize_t zx29_i2c_sysfs_store(struct device *dev, |
| 241 | struct device_attribute *attr, const char *buf, size_t count) |
| 242 | { |
| 243 | struct zx29_i2c *i2c = dev_get_drvdata(dev); |
| 244 | unsigned long clock_rate = 0; |
| 245 | |
| 246 | if(strict_strtoul(buf, 0, &clock_rate)) |
| 247 | return -EINVAL; |
| 248 | if((clock_rate < MIN_BUS_CLK) || (clock_rate > MAX_BUS_CLK) || (NULL == i2c)) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | i2c_lock_adapter(&i2c->adap); |
| 252 | i2c->clkrate = clock_rate; |
| 253 | i2c_unlock_adapter(&i2c->adap); |
| 254 | |
| 255 | pr_info("new i2c%d scl rate: %ld\n", i2c->id, i2c->clkrate); |
| 256 | |
| 257 | return count; |
| 258 | } |
| 259 | #else |
| 260 | static int zx29_i2c_sysfs_create_group(struct zx29_i2c *i2c) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static void zx29_i2c_sysfs_remove_group(struct zx29_i2c *i2c) |
| 266 | { |
| 267 | return; |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | static emsf_lock_id i2c_get_sflock(unsigned int i2c_id) |
| 272 | { |
| 273 | emsf_lock_id softLock = I2C2_SFLOCK; |
| 274 | |
| 275 | switch(i2c_id) |
| 276 | { |
| 277 | case 0: //pmic i2c |
| 278 | softLock = I2C2_SFLOCK; |
| 279 | break; |
| 280 | case 1: |
| 281 | softLock = I2C1_SFLOCK; |
| 282 | break; |
| 283 | default: |
| 284 | BUG(); |
| 285 | } |
| 286 | |
| 287 | return softLock; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * zx29_i2c_init |
| 292 | * |
| 293 | * initialise the controller, set frequency |
| 294 | */ |
| 295 | static int zx29_i2c_init(struct zx29_i2c *i2c) |
| 296 | { |
| 297 | unsigned int bus_clk; |
| 298 | unsigned int pdiv; |
| 299 | emsf_lock_id softLock = i2c_get_sflock(i2c->id); |
| 300 | unsigned long actual_clk = 0; |
| 301 | |
| 302 | soft_spin_lock(softLock); |
| 303 | |
| 304 | /*reset*/ |
| 305 | zx29_i2c_reset_fifo(i2c); |
| 306 | udelay(1); |
| 307 | |
| 308 | /*calculate bus clock division, fix input clock is 26M*/ |
| 309 | bus_clk=i2c->clkrate/1000; /*in KHz*/ |
| 310 | pdiv=((I2C_WCLK_FREQ/1000)/4)/bus_clk - 1; |
| 311 | if(((I2C_WCLK_FREQ/1000)/4) % bus_clk != 0){ |
| 312 | pdiv++; |
| 313 | } |
| 314 | |
| 315 | actual_clk = (I2C_WCLK_FREQ/4)/(pdiv+1); /*actul bus clock rate*/ |
| 316 | if ((actual_clk > MAX_BUS_CLK) || (actual_clk < MIN_BUS_CLK)){ |
| 317 | pr_warn("bus clock rate error %ld!\n", actual_clk); |
| 318 | BUG(); |
| 319 | } |
| 320 | i2c_set_clk_div(i2c, pdiv); |
| 321 | |
| 322 | /*master mode, disable interrupt, clear interrupt*/ |
| 323 | i2c_set_cmd(i2c, 0x1); |
| 324 | |
| 325 | soft_spin_unlock(softLock); |
| 326 | |
| 327 | i2c->state = STATE_IDLE; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | *zx29_i2c_start |
| 334 | * put the start of a message onto the bus |
| 335 | */ |
| 336 | static void zx29_i2c_start(struct zx29_i2c *i2c, uint8_t fmt) |
| 337 | { |
| 338 | unsigned int addr = i2c->msg->addr; |
| 339 | unsigned long ctrl = 0; |
| 340 | unsigned int addr_mode = 0; |
| 341 | |
| 342 | /*set slave device address*/ |
| 343 | addr_mode = i2c->msg->flags & I2C_M_TEN; |
| 344 | |
| 345 | i2c_set_addr(i2c, addr, addr_mode); |
| 346 | |
| 347 | i2c_clr_irq_ack(i2c); |
| 348 | |
| 349 | /*set cmd register for rx or tx*/ |
| 350 | ctrl = i2c_get_cmd(i2c); |
| 351 | ctrl &= ~CMD_I2C_FMT_MSK; |
| 352 | ctrl |= fmt; |
| 353 | |
| 354 | if (addr_mode){ |
| 355 | /*10 bit address mode :clear interrupt conditions, set master mode,set 10bit address mode and start*/ |
| 356 | ctrl |= CMD_I2C_START|CMD_I2C_MODE|CMD_TENBIT_MODE; |
| 357 | } |
| 358 | else{ |
| 359 | /*7 bit address mode:clear interrupt conditions, set master mode and start*/ |
| 360 | ctrl &= ~CMD_TENBIT_MODE; |
| 361 | ctrl |= CMD_I2C_START|CMD_I2C_MODE; |
| 362 | } |
| 363 | |
| 364 | i2c_set_cmd(i2c, ctrl); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* |
| 369 | * zx29_i2c_stop |
| 370 | */ |
| 371 | |
| 372 | static inline void zx29_i2c_stop(struct zx29_i2c *i2c) |
| 373 | { |
| 374 | //unsigned long ctrl = ioread32(i2c->regs + I2C_CMD); |
| 375 | |
| 376 | /* stop the transfer ,clear interrupt conditions */ |
| 377 | //ctrl |= CMD_I2C_STOP|CMD_IRQ_ACK; |
| 378 | //iowrite32(ctrl, i2c->regs + I2C_CMD); |
| 379 | |
| 380 | i2c->state = STATE_STOP; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * check i2c busy or not |
| 385 | */ |
| 386 | static inline int zx29_i2c_is_busy(struct zx29_i2c *i2c) |
| 387 | { |
| 388 | unsigned int time_out=0xff; |
| 389 | |
| 390 | while(time_out--){ |
| 391 | if(i2c_get_bus_status(i2c)==0) |
| 392 | return 0; |
| 393 | else |
| 394 | udelay(1); |
| 395 | } |
| 396 | return -ETIMEDOUT; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * i2c fifo write |
| 401 | */ |
| 402 | static void zx29_i2c_prepare_write(struct zx29_i2c *i2c) |
| 403 | { |
| 404 | u8 val=0; |
| 405 | u8 *buf=i2c->msg->buf; |
| 406 | u16 len =i2c->buf_remaining; |
| 407 | u16 len_tx = 0; |
| 408 | unsigned int ret=0; |
| 409 | |
| 410 | /*this step must be done. because FIFO empty interrupt does not mean bus is idle*/ |
| 411 | ret=zx29_i2c_is_busy(i2c); |
| 412 | if(ret) |
| 413 | BUG(); |
| 414 | |
| 415 | zx29_i2c_reset_tx_fifo(i2c); |
| 416 | |
| 417 | if(len > I2C_FIFO_DEPTH) |
| 418 | len_tx = I2C_FIFO_DEPTH; |
| 419 | else |
| 420 | len_tx = len; |
| 421 | i2c->buf_remaining -= len_tx; |
| 422 | |
| 423 | i2c_set_tx_fifo_depth(i2c, len_tx-1); |
| 424 | |
| 425 | /*write data to fifo*/ |
| 426 | while(len_tx--){ |
| 427 | val = *(buf + i2c->msg_ptr); |
| 428 | i2c_write_data(i2c, val); |
| 429 | i2c->msg_ptr++; |
| 430 | } |
| 431 | barrier(); |
| 432 | |
| 433 | // zx29_i2c_start(i2c); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * i2c start read |
| 438 | */ |
| 439 | static void zx29_i2c_prepare_read(struct zx29_i2c *i2c) |
| 440 | { |
| 441 | u16 len =i2c->buf_remaining; |
| 442 | u16 len_rx = 0; |
| 443 | unsigned int ret=0; |
| 444 | |
| 445 | /*i2c can only be operated when bus is idle*/ |
| 446 | ret=zx29_i2c_is_busy(i2c); |
| 447 | if(ret) |
| 448 | BUG(); |
| 449 | |
| 450 | zx29_i2c_reset_rx_fifo(i2c); |
| 451 | |
| 452 | if(len > I2C_FIFO_DEPTH) |
| 453 | len_rx = I2C_FIFO_DEPTH; |
| 454 | else |
| 455 | len_rx = len; |
| 456 | |
| 457 | i2c_set_rx_fifo_depth(i2c, len_rx-1); |
| 458 | |
| 459 | // zx29_i2c_start(i2c); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * i2c fifo read |
| 464 | */ |
| 465 | static void zx29_i2c_read_fifo(struct zx29_i2c *i2c) |
| 466 | { |
| 467 | unsigned int len_rx=0; |
| 468 | u8 *buf=i2c->msg->buf; |
| 469 | u8 val=0; |
| 470 | |
| 471 | len_rx = i2c_get_rx_fifo_length(i2c); |
| 472 | |
| 473 | i2c->buf_remaining -= len_rx; |
| 474 | |
| 475 | /*read data from fifo*/ |
| 476 | while(len_rx--){ |
| 477 | val =(u8) i2c_read_data(i2c); |
| 478 | *(buf + i2c->msg_ptr) = val; |
| 479 | i2c->msg_ptr++; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* zx29_i2c_irq |
| 484 | * |
| 485 | * top level IRQ servicing routine |
| 486 | */ |
| 487 | static irqreturn_t zx29_i2c_irq(int irqno, void *dev_id) |
| 488 | { |
| 489 | struct zx29_i2c *i2c = dev_id; |
| 490 | unsigned long status; |
| 491 | |
| 492 | status = i2c_get_irq_status(i2c); |
| 493 | i2c->reg_status = (i2c->reg_status << 8) | status; |
| 494 | |
| 495 | /* |
| 496 | * if state is idle or stop, maybe other core on 7510 platform trigger interrupt |
| 497 | * it will be looked as spurious interrupt, nothing need to be done |
| 498 | */ |
| 499 | if ((i2c->state == STATE_IDLE) || (i2c->state == STATE_STOP)) |
| 500 | return IRQ_HANDLED; |
| 501 | |
| 502 | if (status & (IRQ_ERR_DEVICE | IRQ_ERR_DATA | IRQ_TRANS_DONE)) { |
| 503 | i2c_disable_irq(i2c); |
| 504 | disable_irq_nosync(i2c->irq); |
| 505 | |
| 506 | up(&i2c->msg_complete); |
| 507 | |
| 508 | return IRQ_HANDLED; |
| 509 | } |
| 510 | |
| 511 | if (status & IRQ_TIME_OUT) { |
| 512 | i2c_clr_irq_ack(i2c); |
| 513 | return IRQ_HANDLED; |
| 514 | } |
| 515 | |
| 516 | return IRQ_HANDLED; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | /* zx29_i2c_get_bus |
| 521 | * |
| 522 | * get the i2c bus for a master transaction |
| 523 | */ |
| 524 | static int zx29_i2c_get_bus(struct zx29_i2c *i2c) |
| 525 | { |
| 526 | int timeout = 400; |
| 527 | |
| 528 | while (timeout-- > 0) { |
| 529 | |
| 530 | if (!i2c_get_bus_status(i2c)) |
| 531 | return 0; |
| 532 | |
| 533 | msleep(1); |
| 534 | } |
| 535 | |
| 536 | return -ETIMEDOUT; |
| 537 | } |
| 538 | |
| 539 | /* zx29_i2c_doxfer |
| 540 | * |
| 541 | * this starts an i2c transfer |
| 542 | */ |
| 543 | static int zx29_i2c_doxfer(struct zx29_i2c *i2c, struct i2c_msg *msgs, bool cmb_rw) |
| 544 | { |
| 545 | int ret; |
| 546 | |
| 547 | if (i2c->suspended) |
| 548 | return -EIO; |
| 549 | |
| 550 | i2c->msg = msgs; |
| 551 | i2c->msg_ptr = 0; |
| 552 | i2c->buf_remaining = msgs->len; |
| 553 | // i2c->msg_idx = 0; |
| 554 | |
| 555 | // init_completion(&(i2c->msg_complete)); |
| 556 | if (unlikely(i2c->msg_complete.count)) { |
| 557 | pr_err("i2c sem %d\n", i2c->msg_complete.count); |
| 558 | } |
| 559 | i2c->reg_status = 0; |
| 560 | i2c_clr_irq_ack(i2c); |
| 561 | i2c_enable_irq(i2c); |
| 562 | enable_irq(i2c->irq); |
| 563 | |
| 564 | if (cmb_rw) { |
| 565 | zx29_i2c_prepare_write(i2c); |
| 566 | |
| 567 | i2c->msg = &msgs[1]; |
| 568 | i2c->msg_ptr = 0; |
| 569 | i2c->buf_remaining = msgs[1].len; |
| 570 | |
| 571 | i2c->state = STATE_READ; |
| 572 | memset(msgs[1].buf, 0, msgs[1].len); |
| 573 | zx29_i2c_prepare_read(i2c); |
| 574 | |
| 575 | zx29_i2c_start(i2c, CMD_I2C_FMT_CMB); |
| 576 | } else if (i2c->msg->flags & I2C_M_RD) { |
| 577 | i2c->state = STATE_READ; |
| 578 | memset(msgs->buf, 0, msgs->len); |
| 579 | zx29_i2c_prepare_read(i2c); |
| 580 | zx29_i2c_start(i2c, CMD_I2C_FMT_RD); |
| 581 | } else { |
| 582 | i2c->state = STATE_WRITE; |
| 583 | zx29_i2c_prepare_write(i2c); |
| 584 | zx29_i2c_start(i2c, CMD_I2C_FMT_WR); |
| 585 | } |
| 586 | |
| 587 | #if I2C_LONG_TRANSFER |
| 588 | if (i2c->msg->len > I2C_FIFO_DEPTH) { |
| 589 | zx29_i2c_start_timer(i2c); |
| 590 | } |
| 591 | #endif |
| 592 | |
| 593 | /* waiting for tranfer finished */ |
| 594 | ret = down_timeout(&i2c->msg_complete, I2C_TIMEOUT); |
| 595 | |
| 596 | #if I2C_LONG_TRANSFER |
| 597 | if (i2c->msg->len > I2C_FIFO_DEPTH) { |
| 598 | zx29_i2c_stop_timer(i2c); |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | if (ret < 0) { |
| 603 | i2c_disable_irq(i2c); |
| 604 | disable_irq_nosync(i2c->irq); |
| 605 | |
| 606 | pr_err("i2c transfer timeout\n"); |
| 607 | |
| 608 | zx29_i2c_stop(i2c); |
| 609 | zx29_i2c_init(i2c); |
| 610 | |
| 611 | return -ETIMEDOUT; |
| 612 | } |
| 613 | |
| 614 | if (i2c->state == STATE_READ) { |
| 615 | zx29_i2c_read_fifo(i2c); |
| 616 | } |
| 617 | |
| 618 | /* slave no ack on address phase or data phase */ |
| 619 | if (i2c_get_irq_status(i2c) & (IRQ_ERR_DEVICE | IRQ_ERR_DATA)) { |
| 620 | pr_err("NACK by slave 0x%X, status %X\n", i2c->msg->addr, i2c_get_irq_status(i2c)); |
| 621 | |
| 622 | while (~ i2c_get_irq_status(i2c) & IRQ_TRANS_DONE); |
| 623 | i2c_clr_irq_ack(i2c); |
| 624 | |
| 625 | zx29_i2c_init(i2c); |
| 626 | |
| 627 | return -ECOMM; |
| 628 | } |
| 629 | |
| 630 | i2c_clr_irq_ack(i2c); |
| 631 | i2c->state = STATE_IDLE; |
| 632 | |
| 633 | #if I2C_LONG_TRANSFER |
| 634 | if(i2c->buf_remaining) { |
| 635 | pr_err("%s: %d/%d isn't transferred\n", __FUNCTION__, i2c->buf_remaining, i2c->msg->len); |
| 636 | return -EAGAIN; |
| 637 | } |
| 638 | #endif |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* zx29_i2c_xfer |
| 644 | * |
| 645 | * first port of call from the i2c bus code when an message needs |
| 646 | * transferring across the i2c bus. |
| 647 | */ |
| 648 | static int zx29_i2c_xfer(struct i2c_adapter *adap, |
| 649 | struct i2c_msg *msgs, int num) |
| 650 | { |
| 651 | struct zx29_i2c *i2c = (struct zx29_i2c *)adap->algo_data; |
| 652 | int ret; |
| 653 | int i = 0; |
| 654 | bool combie_rw_applicable; |
| 655 | unsigned int pdiv; |
| 656 | emsf_lock_id softLock = i2c_get_sflock(i2c->id); |
| 657 | |
| 658 | if (i2c->suspended) |
| 659 | return -EIO; |
| 660 | |
| 661 | if (msgs->len == 0) |
| 662 | return -EINVAL; |
| 663 | |
| 664 | #if I2C_PSM_CONTROL |
| 665 | zx29_i2c_set_active(&i2c->psm_lock); |
| 666 | #endif |
| 667 | |
| 668 | soft_spin_lock(softLock); |
| 669 | |
| 670 | #ifdef CONFIG_ARCH_ZX297502 |
| 671 | clk_enable(i2c->clk); |
| 672 | #endif |
| 673 | |
| 674 | ret = zx29_i2c_get_bus(i2c); |
| 675 | if (ret != 0) { |
| 676 | dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); |
| 677 | |
| 678 | //soft_spin_unlock(I2C_SFLOCK); /* zhouqi */ |
| 679 | |
| 680 | ret = -EAGAIN; |
| 681 | goto exit; |
| 682 | } |
| 683 | |
| 684 | /* calculate bus clock division, fixed input clock is 26M */ |
| 685 | pdiv = (I2C_WCLK_FREQ / 4 / i2c->clkrate) - 1; |
| 686 | if((I2C_WCLK_FREQ / 4) % i2c->clkrate != 0) |
| 687 | pdiv++; |
| 688 | i2c_set_clk_div(i2c, pdiv); |
| 689 | |
| 690 | // i2c->msg_num = num; |
| 691 | /* check whether combined rw is applicable */ |
| 692 | if ((num == 2) && (msgs[0].addr == msgs[1].addr)) { |
| 693 | combie_rw_applicable = ((msgs[0].flags & I2C_M_TEN) == (msgs[1].flags & I2C_M_TEN)) \ |
| 694 | && (~msgs[0].flags & I2C_M_RD) \ |
| 695 | && (msgs[1].flags & I2C_M_RD) \ |
| 696 | && (msgs[0].len <= I2C_FIFO_DEPTH); |
| 697 | } else |
| 698 | combie_rw_applicable = 0; |
| 699 | |
| 700 | if (combie_rw_applicable) { |
| 701 | ret = zx29_i2c_doxfer(i2c, msgs, 1); |
| 702 | } else for (i = 0; i < num; i++) { |
| 703 | ret = zx29_i2c_doxfer(i2c, &msgs[i], 0); |
| 704 | // i2c->msg_idx=i; |
| 705 | if (ret) { |
| 706 | //printk(KERN_INFO "%s err code=%d\n", __FUNCTION__, ret); |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | exit: |
| 712 | #ifdef CONFIG_ARCH_ZX297502 |
| 713 | clk_disable(i2c->clk); |
| 714 | #endif |
| 715 | |
| 716 | soft_spin_unlock(softLock); |
| 717 | |
| 718 | #if I2C_PSM_CONTROL |
| 719 | zx29_i2c_set_idle(&i2c->psm_lock); |
| 720 | #endif |
| 721 | |
| 722 | return ret ? ret : num; |
| 723 | } |
| 724 | |
| 725 | /* declare our i2c functionality */ |
| 726 | static u32 zx29_i2c_func(struct i2c_adapter *adap) |
| 727 | { |
| 728 | return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK)| I2C_FUNC_10BIT_ADDR; |
| 729 | } |
| 730 | |
| 731 | /* i2c bus registration info */ |
| 732 | static const struct i2c_algorithm zx29_i2c_algorithm = { |
| 733 | .master_xfer = zx29_i2c_xfer, |
| 734 | .functionality = zx29_i2c_func, |
| 735 | }; |
| 736 | |
| 737 | #ifdef CONFIG_OF |
| 738 | /* zx29_i2c_parse_dt |
| 739 | * |
| 740 | * Parse the device tree node and retreive the platform data. |
| 741 | */ |
| 742 | static void |
| 743 | zx29_i2c_parse_dt(struct device_node *np, struct zx29_i2c *i2c) |
| 744 | { |
| 745 | struct zx29_i2c_platform_data *pdata = i2c->pdata; |
| 746 | |
| 747 | if (!np) |
| 748 | return; |
| 749 | of_property_read_u32(np, "Sanechips-tsp,i2c-slave-addr", &pdata->slave_addr); |
| 750 | of_property_read_u32(np, "Sanechips-tsp,i2c-max-bus-freq",(u32 *)&pdata->max_bus_clk); |
| 751 | } |
| 752 | #else |
| 753 | static void |
| 754 | zx29_i2c_parse_dt(struct device_node *np, struct zx29_i2c *i2c) |
| 755 | { |
| 756 | return; |
| 757 | } |
| 758 | #endif |
| 759 | |
| 760 | static int i2c_init_clks(struct platform_device *pdev, struct zx29_i2c *i2c) |
| 761 | { |
| 762 | /* find the clock and enable it */ |
| 763 | i2c->clk = clk_get(&pdev->dev, "work_clk"); |
| 764 | if (IS_ERR(i2c->clk)) { |
| 765 | dev_err(&pdev->dev, "cannot get work clock\n"); |
| 766 | return -ENOENT; |
| 767 | } |
| 768 | |
| 769 | /* set i2c work clock at 26MHz/1 */ |
| 770 | clk_set_rate(i2c->clk, I2C_WCLK_FREQ); |
| 771 | clk_enable(i2c->clk); |
| 772 | |
| 773 | i2c->pclk = clk_get(&pdev->dev, "apb_clk"); |
| 774 | if (IS_ERR(i2c->pclk)) { |
| 775 | dev_err(&pdev->dev, "cannot get apb clock\n"); |
| 776 | return -ENOENT; |
| 777 | } |
| 778 | clk_enable(i2c->pclk); |
| 779 | |
| 780 | clk_set_auto_gate(i2c->clk, true); |
| 781 | clk_set_auto_gate(i2c->pclk, true); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | /* zx29_i2c_probe |
| 787 | * |
| 788 | * called by the bus driver when a suitable device is found |
| 789 | */ |
| 790 | static int zx29_i2c_probe(struct platform_device *pdev) |
| 791 | { |
| 792 | struct zx29_i2c *i2c=NULL; |
| 793 | struct zx29_i2c_platform_data *pdata = NULL; |
| 794 | struct resource *res=NULL; |
| 795 | void __iomem *base=NULL; |
| 796 | const unsigned int *prop=NULL; |
| 797 | int ret=0; |
| 798 | emsf_lock_id softLock; |
| 799 | |
| 800 | /* irq®ister */ |
| 801 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 802 | if (!res) { |
| 803 | dev_err(&pdev->dev, "no memory resource\n"); |
| 804 | ret = -EINVAL; |
| 805 | goto err_iomap; |
| 806 | } |
| 807 | base = (void __iomem *)(res->start); |
| 808 | |
| 809 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 810 | if (!res) { |
| 811 | dev_err(&pdev->dev, "no irq resource\n"); |
| 812 | ret = -EINVAL; |
| 813 | goto err_iomap; |
| 814 | } |
| 815 | |
| 816 | i2c = devm_kzalloc(&pdev->dev, sizeof(struct zx29_i2c), GFP_KERNEL); |
| 817 | if (!i2c) { |
| 818 | dev_err(&pdev->dev, "no memory for state\n"); |
| 819 | ret = -ENOMEM; |
| 820 | goto err_iomap; |
| 821 | } |
| 822 | |
| 823 | if (!pdev->dev.of_node) { |
| 824 | pdata = pdev->dev.platform_data; |
| 825 | if (!pdata) { |
| 826 | dev_err(&pdev->dev, "no platform data\n"); |
| 827 | return -EINVAL; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | snprintf(i2c->name, sizeof(i2c->name), "zx29-i2c%d", pdev->id); |
| 832 | i2c->pdata = pdata; |
| 833 | i2c->id = pdev->id; |
| 834 | i2c->regs = base; |
| 835 | i2c->irq = res->start; |
| 836 | i2c->dev = &pdev->dev; |
| 837 | i2c->state = STATE_IDLE; |
| 838 | |
| 839 | i2c->clkrate = MIN_BUS_CLK; /* default clock rate */ |
| 840 | if (pdata) { |
| 841 | i2c->clkrate = pdata->bus_clk_rate; |
| 842 | } |
| 843 | else if (i2c->dev->of_node) { /* if there is a device tree node ... */ |
| 844 | prop = of_get_property(i2c->dev->of_node, |
| 845 | "clock-frequency", NULL); |
| 846 | if (prop) |
| 847 | i2c->clkrate = be32_to_cpup(prop); |
| 848 | } |
| 849 | |
| 850 | spin_lock_init(&i2c->lock); |
| 851 | |
| 852 | sema_init(&i2c->msg_complete, 0); |
| 853 | |
| 854 | softLock = i2c_get_sflock(i2c->id); |
| 855 | soft_spin_lock(softLock); |
| 856 | |
| 857 | i2c_config_pins(i2c); |
| 858 | |
| 859 | ret = i2c_init_clks(pdev, i2c); |
| 860 | if(ret < 0) |
| 861 | { |
| 862 | soft_spin_unlock(softLock); |
| 863 | goto err_free; |
| 864 | } |
| 865 | |
| 866 | /* initialise the i2c controller */ |
| 867 | ret = zx29_i2c_init(i2c); |
| 868 | if (ret != 0){ |
| 869 | soft_spin_unlock(softLock); |
| 870 | goto err_free; |
| 871 | } |
| 872 | |
| 873 | i2c_disable_irq(i2c); |
| 874 | /* find the IRQ for this unit (note, this relies on the init call to |
| 875 | * ensure no current IRQs pending |
| 876 | */ |
| 877 | ret = devm_request_irq(&pdev->dev, i2c->irq, zx29_i2c_irq, |
| 878 | IRQF_TRIGGER_HIGH | IRQF_NO_THREAD | IRQF_ONESHOT, |
| 879 | dev_name(&pdev->dev), i2c); |
| 880 | if (ret != 0) { |
| 881 | dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); |
| 882 | soft_spin_unlock(softLock); |
| 883 | goto err_clk; |
| 884 | } |
| 885 | disable_irq_nosync(i2c->irq); |
| 886 | |
| 887 | soft_spin_unlock(softLock); |
| 888 | |
| 889 | /* Note, previous versions of the driver used i2c_add_adapter() |
| 890 | * to add the bus at any number. We now pass the bus number via |
| 891 | * the platform data, so if unset it will now default to always |
| 892 | * being bus 0. |
| 893 | */ |
| 894 | zx29_i2c_parse_dt(pdev->dev.of_node, i2c); |
| 895 | strlcpy(i2c->adap.name, "zx29-i2c", sizeof(i2c->adap.name)); |
| 896 | i2c->adap.owner = THIS_MODULE; |
| 897 | i2c->adap.algo = &zx29_i2c_algorithm; |
| 898 | i2c->adap.retries = 3; |
| 899 | i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
| 900 | i2c->adap.algo_data = i2c; |
| 901 | i2c->adap.dev.parent = &pdev->dev; |
| 902 | i2c->adap.nr = pdev->id; |
| 903 | i2c->adap.dev.of_node = pdev->dev.of_node; |
| 904 | |
| 905 | #if I2C_PSM_CONTROL |
| 906 | wake_lock_init(&i2c->psm_lock, WAKE_LOCK_SUSPEND, i2c->name); |
| 907 | #endif |
| 908 | |
| 909 | ret = i2c_add_numbered_adapter(&i2c->adap); |
| 910 | if (ret < 0) { |
| 911 | dev_err(&pdev->dev, "failed to add bus to i2c core\n"); |
| 912 | goto err_irq; |
| 913 | } |
| 914 | |
| 915 | // of_i2c_register_devices(&i2c->adap); |
| 916 | platform_set_drvdata(pdev, i2c); |
| 917 | |
| 918 | zx29_i2c_sysfs_create_group(i2c); |
| 919 | |
| 920 | dev_info(&pdev->dev, "%s: ZX29 I2C adapter\n", dev_name(&i2c->adap.dev)); |
| 921 | |
| 922 | #ifdef CONFIG_ARCH_ZX297502 |
| 923 | clk_disable(i2c->clk); |
| 924 | #endif |
| 925 | return 0; |
| 926 | |
| 927 | /*err_cpufreq: |
| 928 | zx29_i2c_deregister_cpufreq(i2c);*/ |
| 929 | |
| 930 | err_irq: |
| 931 | #if I2C_PSM_CONTROL |
| 932 | wake_lock_destroy(&i2c->psm_lock); |
| 933 | #endif |
| 934 | devm_free_irq(&pdev->dev, i2c->irq, i2c); |
| 935 | |
| 936 | err_clk: |
| 937 | clk_disable(i2c->clk); |
| 938 | clk_put(i2c->clk); |
| 939 | clk_disable(i2c->pclk); |
| 940 | clk_put(i2c->pclk); |
| 941 | |
| 942 | |
| 943 | err_free: |
| 944 | kfree(i2c); |
| 945 | |
| 946 | err_iomap: |
| 947 | // iounmap(base); |
| 948 | |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | /* zx29_i2c_remove |
| 953 | * |
| 954 | * called when device is removed from the bus |
| 955 | */ |
| 956 | static int zx29_i2c_remove(struct platform_device *pdev) |
| 957 | { |
| 958 | struct zx29_i2c *i2c = platform_get_drvdata(pdev); |
| 959 | |
| 960 | zx29_i2c_sysfs_remove_group(i2c); |
| 961 | |
| 962 | i2c_del_adapter(&i2c->adap); |
| 963 | |
| 964 | clk_disable(i2c->clk); |
| 965 | clk_put(i2c->clk); |
| 966 | clk_disable(i2c->pclk); |
| 967 | clk_put(i2c->pclk); |
| 968 | |
| 969 | #if I2C_PSM_CONTROL |
| 970 | wake_lock_destroy(&i2c->psm_lock); |
| 971 | #endif |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | #ifdef CONFIG_PM |
| 977 | static int zx29_i2c_suspend(struct platform_device *pdev, pm_message_t state) |
| 978 | { |
| 979 | |
| 980 | #ifdef CONFIG_ARCH_ZX297502 |
| 981 | struct zx29_i2c *i2c = platform_get_drvdata(pdev); |
| 982 | |
| 983 | i2c_lock_adapter(&i2c->adap); |
| 984 | i2c->suspended = 1; |
| 985 | i2c_unlock_adapter(&i2c->adap); |
| 986 | #endif |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | static int zx29_i2c_resume(struct platform_device *pdev) |
| 992 | { |
| 993 | |
| 994 | #ifdef CONFIG_ARCH_ZX297502 |
| 995 | struct zx29_i2c *i2c = platform_get_drvdata(pdev); |
| 996 | |
| 997 | i2c_lock_adapter(&i2c->adap); |
| 998 | clk_enable(i2c->clk); |
| 999 | zx29_i2c_init(i2c); |
| 1000 | clk_disable(i2c->clk); |
| 1001 | i2c->suspended = 0; |
| 1002 | i2c_unlock_adapter(&i2c->adap); |
| 1003 | #endif |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | #endif |
| 1008 | |
| 1009 | |
| 1010 | #ifdef CONFIG_OF |
| 1011 | static const struct of_device_id zx29_i2c_match[] = { |
| 1012 | { .compatible = "Sanechips-TSP, zx29_i2c0" }, |
| 1013 | {}, |
| 1014 | }; |
| 1015 | MODULE_DEVICE_TABLE(of, zx29_i2c_match); |
| 1016 | #else |
| 1017 | #define zx29_i2c_match NULL |
| 1018 | #endif |
| 1019 | |
| 1020 | static struct platform_driver zx29_i2c_driver = { |
| 1021 | .probe = zx29_i2c_probe, |
| 1022 | .remove = zx29_i2c_remove, |
| 1023 | #ifdef CONFIG_PM |
| 1024 | .suspend = zx29_i2c_suspend, |
| 1025 | .resume = zx29_i2c_resume, |
| 1026 | #endif |
| 1027 | .driver = { |
| 1028 | .owner = THIS_MODULE, |
| 1029 | .name = "zx29_i2c", |
| 1030 | .of_match_table = zx29_i2c_match, |
| 1031 | }, |
| 1032 | }; |
| 1033 | |
| 1034 | static int __init i2c_adap_zx29_init(void) |
| 1035 | { |
| 1036 | int ret; |
| 1037 | |
| 1038 | ret=platform_driver_register(&zx29_i2c_driver); |
| 1039 | if (ret<0) { |
| 1040 | pr_err("zx29 i2c driver register fail\n"); |
| 1041 | } |
| 1042 | return ret; |
| 1043 | } |
| 1044 | subsys_initcall(i2c_adap_zx29_init); |
| 1045 | |
| 1046 | static void __exit i2c_adap_zx29_exit(void) |
| 1047 | { |
| 1048 | platform_driver_unregister(&zx29_i2c_driver); |
| 1049 | } |
| 1050 | module_exit(i2c_adap_zx29_exit); |
| 1051 | |