1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c41aa3ceSMax Schwarz /* 3c41aa3ceSMax Schwarz * Driver for I2C adapter in Rockchip RK3xxx SoC 4c41aa3ceSMax Schwarz * 5c41aa3ceSMax Schwarz * Max Schwarz <max.schwarz@online.de> 6c41aa3ceSMax Schwarz * based on the patches by Rockchip Inc. 7c41aa3ceSMax Schwarz */ 8c41aa3ceSMax Schwarz 9c41aa3ceSMax Schwarz #include <linux/kernel.h> 10c41aa3ceSMax Schwarz #include <linux/module.h> 11c41aa3ceSMax Schwarz #include <linux/i2c.h> 12c41aa3ceSMax Schwarz #include <linux/interrupt.h> 13c41aa3ceSMax Schwarz #include <linux/errno.h> 14c41aa3ceSMax Schwarz #include <linux/err.h> 15c41aa3ceSMax Schwarz #include <linux/platform_device.h> 16c41aa3ceSMax Schwarz #include <linux/io.h> 17c41aa3ceSMax Schwarz #include <linux/of_address.h> 18c41aa3ceSMax Schwarz #include <linux/of_irq.h> 19c41aa3ceSMax Schwarz #include <linux/spinlock.h> 20c41aa3ceSMax Schwarz #include <linux/clk.h> 21c41aa3ceSMax Schwarz #include <linux/wait.h> 22c41aa3ceSMax Schwarz #include <linux/mfd/syscon.h> 23c41aa3ceSMax Schwarz #include <linux/regmap.h> 240285f8f5Saddy ke #include <linux/math64.h> 25c41aa3ceSMax Schwarz 26c41aa3ceSMax Schwarz 27c41aa3ceSMax Schwarz /* Register Map */ 28c41aa3ceSMax Schwarz #define REG_CON 0x00 /* control register */ 29c41aa3ceSMax Schwarz #define REG_CLKDIV 0x04 /* clock divisor register */ 30c41aa3ceSMax Schwarz #define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */ 31c41aa3ceSMax Schwarz #define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */ 32c41aa3ceSMax Schwarz #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */ 33c41aa3ceSMax Schwarz #define REG_MRXCNT 0x14 /* number of bytes to be received */ 34c41aa3ceSMax Schwarz #define REG_IEN 0x18 /* interrupt enable */ 35c41aa3ceSMax Schwarz #define REG_IPD 0x1c /* interrupt pending */ 36c41aa3ceSMax Schwarz #define REG_FCNT 0x20 /* finished count */ 37c41aa3ceSMax Schwarz 38c41aa3ceSMax Schwarz /* Data buffer offsets */ 39c41aa3ceSMax Schwarz #define TXBUFFER_BASE 0x100 40c41aa3ceSMax Schwarz #define RXBUFFER_BASE 0x200 41c41aa3ceSMax Schwarz 42c41aa3ceSMax Schwarz /* REG_CON bits */ 43c41aa3ceSMax Schwarz #define REG_CON_EN BIT(0) 44c41aa3ceSMax Schwarz enum { 45c41aa3ceSMax Schwarz REG_CON_MOD_TX = 0, /* transmit data */ 46c41aa3ceSMax Schwarz REG_CON_MOD_REGISTER_TX, /* select register and restart */ 47c41aa3ceSMax Schwarz REG_CON_MOD_RX, /* receive data */ 48c41aa3ceSMax Schwarz REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes 49c41aa3ceSMax Schwarz * register addr */ 50c41aa3ceSMax Schwarz }; 51c41aa3ceSMax Schwarz #define REG_CON_MOD(mod) ((mod) << 1) 52c41aa3ceSMax Schwarz #define REG_CON_MOD_MASK (BIT(1) | BIT(2)) 53c41aa3ceSMax Schwarz #define REG_CON_START BIT(3) 54c41aa3ceSMax Schwarz #define REG_CON_STOP BIT(4) 55c41aa3ceSMax Schwarz #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */ 56c41aa3ceSMax Schwarz #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */ 57c41aa3ceSMax Schwarz 58a8a7d09eSDavid Wu #define REG_CON_TUNING_MASK GENMASK_ULL(15, 8) 597e086c3fSDavid Wu 607e086c3fSDavid Wu #define REG_CON_SDA_CFG(cfg) ((cfg) << 8) 617e086c3fSDavid Wu #define REG_CON_STA_CFG(cfg) ((cfg) << 12) 627e086c3fSDavid Wu #define REG_CON_STO_CFG(cfg) ((cfg) << 14) 637e086c3fSDavid Wu 64c41aa3ceSMax Schwarz /* REG_MRXADDR bits */ 65c41aa3ceSMax Schwarz #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */ 66c41aa3ceSMax Schwarz 67c41aa3ceSMax Schwarz /* REG_IEN/REG_IPD bits */ 68c41aa3ceSMax Schwarz #define REG_INT_BTF BIT(0) /* a byte was transmitted */ 69c41aa3ceSMax Schwarz #define REG_INT_BRF BIT(1) /* a byte was received */ 70c41aa3ceSMax Schwarz #define REG_INT_MBTF BIT(2) /* master data transmit finished */ 71c41aa3ceSMax Schwarz #define REG_INT_MBRF BIT(3) /* master data receive finished */ 72c41aa3ceSMax Schwarz #define REG_INT_START BIT(4) /* START condition generated */ 73c41aa3ceSMax Schwarz #define REG_INT_STOP BIT(5) /* STOP condition generated */ 74c41aa3ceSMax Schwarz #define REG_INT_NAKRCV BIT(6) /* NACK received */ 75c41aa3ceSMax Schwarz #define REG_INT_ALL 0x7f 76c41aa3ceSMax Schwarz 77c41aa3ceSMax Schwarz /* Constants */ 784489750fSDoug Anderson #define WAIT_TIMEOUT 1000 /* ms */ 79c41aa3ceSMax Schwarz #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */ 80c41aa3ceSMax Schwarz 81e26747bfSDavid Wu /** 82b58fd3beSDavid Wu * struct i2c_spec_values: 837e086c3fSDavid Wu * @min_hold_start_ns: min hold time (repeated) START condition 84b58fd3beSDavid Wu * @min_low_ns: min LOW period of the SCL clock 85b58fd3beSDavid Wu * @min_high_ns: min HIGH period of the SCL cloc 86b58fd3beSDavid Wu * @min_setup_start_ns: min set-up time for a repeated START conditio 87b58fd3beSDavid Wu * @max_data_hold_ns: max data hold time 887e086c3fSDavid Wu * @min_data_setup_ns: min data set-up time 897e086c3fSDavid Wu * @min_setup_stop_ns: min set-up time for STOP condition 907e086c3fSDavid Wu * @min_hold_buffer_ns: min bus free time between a STOP and 917e086c3fSDavid Wu * START condition 92b58fd3beSDavid Wu */ 93b58fd3beSDavid Wu struct i2c_spec_values { 947e086c3fSDavid Wu unsigned long min_hold_start_ns; 95b58fd3beSDavid Wu unsigned long min_low_ns; 96b58fd3beSDavid Wu unsigned long min_high_ns; 97b58fd3beSDavid Wu unsigned long min_setup_start_ns; 98b58fd3beSDavid Wu unsigned long max_data_hold_ns; 997e086c3fSDavid Wu unsigned long min_data_setup_ns; 1007e086c3fSDavid Wu unsigned long min_setup_stop_ns; 1017e086c3fSDavid Wu unsigned long min_hold_buffer_ns; 102b58fd3beSDavid Wu }; 103b58fd3beSDavid Wu 104b58fd3beSDavid Wu static const struct i2c_spec_values standard_mode_spec = { 1057e086c3fSDavid Wu .min_hold_start_ns = 4000, 106b58fd3beSDavid Wu .min_low_ns = 4700, 107b58fd3beSDavid Wu .min_high_ns = 4000, 108b58fd3beSDavid Wu .min_setup_start_ns = 4700, 109b58fd3beSDavid Wu .max_data_hold_ns = 3450, 1107e086c3fSDavid Wu .min_data_setup_ns = 250, 1117e086c3fSDavid Wu .min_setup_stop_ns = 4000, 1127e086c3fSDavid Wu .min_hold_buffer_ns = 4700, 113b58fd3beSDavid Wu }; 114b58fd3beSDavid Wu 115b58fd3beSDavid Wu static const struct i2c_spec_values fast_mode_spec = { 1167e086c3fSDavid Wu .min_hold_start_ns = 600, 117b58fd3beSDavid Wu .min_low_ns = 1300, 118b58fd3beSDavid Wu .min_high_ns = 600, 119b58fd3beSDavid Wu .min_setup_start_ns = 600, 120b58fd3beSDavid Wu .max_data_hold_ns = 900, 1217e086c3fSDavid Wu .min_data_setup_ns = 100, 1227e086c3fSDavid Wu .min_setup_stop_ns = 600, 1237e086c3fSDavid Wu .min_hold_buffer_ns = 1300, 124b58fd3beSDavid Wu }; 125b58fd3beSDavid Wu 126a02f3d08SDavid Wu static const struct i2c_spec_values fast_mode_plus_spec = { 127a02f3d08SDavid Wu .min_hold_start_ns = 260, 128a02f3d08SDavid Wu .min_low_ns = 500, 129a02f3d08SDavid Wu .min_high_ns = 260, 130a02f3d08SDavid Wu .min_setup_start_ns = 260, 131a02f3d08SDavid Wu .max_data_hold_ns = 400, 132a02f3d08SDavid Wu .min_data_setup_ns = 50, 133a02f3d08SDavid Wu .min_setup_stop_ns = 260, 134a02f3d08SDavid Wu .min_hold_buffer_ns = 500, 135a02f3d08SDavid Wu }; 136a02f3d08SDavid Wu 137b58fd3beSDavid Wu /** 138e26747bfSDavid Wu * struct rk3x_i2c_calced_timings: 139e26747bfSDavid Wu * @div_low: Divider output for low 140e26747bfSDavid Wu * @div_high: Divider output for high 1417e086c3fSDavid Wu * @tuning: Used to adjust setup/hold data time, 1427e086c3fSDavid Wu * setup/hold start time and setup stop time for 1437e086c3fSDavid Wu * v1's calc_timings, the tuning should all be 0 1447e086c3fSDavid Wu * for old hardware anyone using v0's calc_timings. 145e26747bfSDavid Wu */ 146e26747bfSDavid Wu struct rk3x_i2c_calced_timings { 147e26747bfSDavid Wu unsigned long div_low; 148e26747bfSDavid Wu unsigned long div_high; 1497e086c3fSDavid Wu unsigned int tuning; 150e26747bfSDavid Wu }; 151e26747bfSDavid Wu 152c41aa3ceSMax Schwarz enum rk3x_i2c_state { 153c41aa3ceSMax Schwarz STATE_IDLE, 154c41aa3ceSMax Schwarz STATE_START, 155c41aa3ceSMax Schwarz STATE_READ, 156c41aa3ceSMax Schwarz STATE_WRITE, 157c41aa3ceSMax Schwarz STATE_STOP 158c41aa3ceSMax Schwarz }; 159c41aa3ceSMax Schwarz 160c41aa3ceSMax Schwarz /** 1615bacb56bSWolfram Sang * struct rk3x_i2c_soc_data: 162c41aa3ceSMax Schwarz * @grf_offset: offset inside the grf regmap for setting the i2c type 1637e086c3fSDavid Wu * @calc_timings: Callback function for i2c timing information calculated 164c41aa3ceSMax Schwarz */ 165c41aa3ceSMax Schwarz struct rk3x_i2c_soc_data { 166c41aa3ceSMax Schwarz int grf_offset; 1677e086c3fSDavid Wu int (*calc_timings)(unsigned long, struct i2c_timings *, 1687e086c3fSDavid Wu struct rk3x_i2c_calced_timings *); 169c41aa3ceSMax Schwarz }; 170c41aa3ceSMax Schwarz 1710a6ad2f9SDavid Wu /** 1720a6ad2f9SDavid Wu * struct rk3x_i2c - private data of the controller 1730a6ad2f9SDavid Wu * @adap: corresponding I2C adapter 1740a6ad2f9SDavid Wu * @dev: device for this controller 1750a6ad2f9SDavid Wu * @soc_data: related soc data struct 1760a6ad2f9SDavid Wu * @regs: virtual memory area 1777e086c3fSDavid Wu * @clk: function clk for rk3399 or function & Bus clks for others 1787e086c3fSDavid Wu * @pclk: Bus clk for rk3399 1790a6ad2f9SDavid Wu * @clk_rate_nb: i2c clk rate change notify 1800a6ad2f9SDavid Wu * @t: I2C known timing information 1810a6ad2f9SDavid Wu * @lock: spinlock for the i2c bus 1820a6ad2f9SDavid Wu * @wait: the waitqueue to wait for i2c transfer 1830a6ad2f9SDavid Wu * @busy: the condition for the event to wait for 1840a6ad2f9SDavid Wu * @msg: current i2c message 1850a6ad2f9SDavid Wu * @addr: addr of i2c slave device 1860a6ad2f9SDavid Wu * @mode: mode of i2c transfer 1870a6ad2f9SDavid Wu * @is_last_msg: flag determines whether it is the last msg in this transfer 1880a6ad2f9SDavid Wu * @state: state of i2c transfer 1890a6ad2f9SDavid Wu * @processed: byte length which has been send or received 1900a6ad2f9SDavid Wu * @error: error code for i2c transfer 1910a6ad2f9SDavid Wu */ 192c41aa3ceSMax Schwarz struct rk3x_i2c { 193c41aa3ceSMax Schwarz struct i2c_adapter adap; 194c41aa3ceSMax Schwarz struct device *dev; 195d032a2ebSJulia Lawall const struct rk3x_i2c_soc_data *soc_data; 196c41aa3ceSMax Schwarz 197c41aa3ceSMax Schwarz /* Hardware resources */ 198c41aa3ceSMax Schwarz void __iomem *regs; 199c41aa3ceSMax Schwarz struct clk *clk; 2007e086c3fSDavid Wu struct clk *pclk; 201249051f4SMax Schwarz struct notifier_block clk_rate_nb; 202c41aa3ceSMax Schwarz 203c41aa3ceSMax Schwarz /* Settings */ 2041ab92956SDavid Wu struct i2c_timings t; 205c41aa3ceSMax Schwarz 206c41aa3ceSMax Schwarz /* Synchronization & notification */ 207c41aa3ceSMax Schwarz spinlock_t lock; 208c41aa3ceSMax Schwarz wait_queue_head_t wait; 209c41aa3ceSMax Schwarz bool busy; 210c41aa3ceSMax Schwarz 211c41aa3ceSMax Schwarz /* Current message */ 212c41aa3ceSMax Schwarz struct i2c_msg *msg; 213c41aa3ceSMax Schwarz u8 addr; 214c41aa3ceSMax Schwarz unsigned int mode; 215c41aa3ceSMax Schwarz bool is_last_msg; 216c41aa3ceSMax Schwarz 217c41aa3ceSMax Schwarz /* I2C state machine */ 218c41aa3ceSMax Schwarz enum rk3x_i2c_state state; 2190a6ad2f9SDavid Wu unsigned int processed; 220c41aa3ceSMax Schwarz int error; 221c41aa3ceSMax Schwarz }; 222c41aa3ceSMax Schwarz 223c41aa3ceSMax Schwarz static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value, 224c41aa3ceSMax Schwarz unsigned int offset) 225c41aa3ceSMax Schwarz { 226c41aa3ceSMax Schwarz writel(value, i2c->regs + offset); 227c41aa3ceSMax Schwarz } 228c41aa3ceSMax Schwarz 229c41aa3ceSMax Schwarz static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset) 230c41aa3ceSMax Schwarz { 231c41aa3ceSMax Schwarz return readl(i2c->regs + offset); 232c41aa3ceSMax Schwarz } 233c41aa3ceSMax Schwarz 234c41aa3ceSMax Schwarz /* Reset all interrupt pending bits */ 235c41aa3ceSMax Schwarz static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c) 236c41aa3ceSMax Schwarz { 237c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_ALL, REG_IPD); 238c41aa3ceSMax Schwarz } 239c41aa3ceSMax Schwarz 240c41aa3ceSMax Schwarz /** 241c41aa3ceSMax Schwarz * Generate a START condition, which triggers a REG_INT_START interrupt. 242c41aa3ceSMax Schwarz */ 243c41aa3ceSMax Schwarz static void rk3x_i2c_start(struct rk3x_i2c *i2c) 244c41aa3ceSMax Schwarz { 2457e086c3fSDavid Wu u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; 246c41aa3ceSMax Schwarz 247c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_START, REG_IEN); 248c41aa3ceSMax Schwarz 249c41aa3ceSMax Schwarz /* enable adapter with correct mode, send START condition */ 2507e086c3fSDavid Wu val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START; 251c41aa3ceSMax Schwarz 252c41aa3ceSMax Schwarz /* if we want to react to NACK, set ACTACK bit */ 253c41aa3ceSMax Schwarz if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) 254c41aa3ceSMax Schwarz val |= REG_CON_ACTACK; 255c41aa3ceSMax Schwarz 256c41aa3ceSMax Schwarz i2c_writel(i2c, val, REG_CON); 257c41aa3ceSMax Schwarz } 258c41aa3ceSMax Schwarz 259c41aa3ceSMax Schwarz /** 260c41aa3ceSMax Schwarz * Generate a STOP condition, which triggers a REG_INT_STOP interrupt. 261c41aa3ceSMax Schwarz * 262c41aa3ceSMax Schwarz * @error: Error code to return in rk3x_i2c_xfer 263c41aa3ceSMax Schwarz */ 264c41aa3ceSMax Schwarz static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error) 265c41aa3ceSMax Schwarz { 266c41aa3ceSMax Schwarz unsigned int ctrl; 267c41aa3ceSMax Schwarz 268c41aa3ceSMax Schwarz i2c->processed = 0; 269c41aa3ceSMax Schwarz i2c->msg = NULL; 270c41aa3ceSMax Schwarz i2c->error = error; 271c41aa3ceSMax Schwarz 272c41aa3ceSMax Schwarz if (i2c->is_last_msg) { 273c41aa3ceSMax Schwarz /* Enable stop interrupt */ 274c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_STOP, REG_IEN); 275c41aa3ceSMax Schwarz 276c41aa3ceSMax Schwarz i2c->state = STATE_STOP; 277c41aa3ceSMax Schwarz 278c41aa3ceSMax Schwarz ctrl = i2c_readl(i2c, REG_CON); 279c41aa3ceSMax Schwarz ctrl |= REG_CON_STOP; 280c41aa3ceSMax Schwarz i2c_writel(i2c, ctrl, REG_CON); 281c41aa3ceSMax Schwarz } else { 282c41aa3ceSMax Schwarz /* Signal rk3x_i2c_xfer to start the next message. */ 283c41aa3ceSMax Schwarz i2c->busy = false; 284c41aa3ceSMax Schwarz i2c->state = STATE_IDLE; 285c41aa3ceSMax Schwarz 286c41aa3ceSMax Schwarz /* 287c41aa3ceSMax Schwarz * The HW is actually not capable of REPEATED START. But we can 288c41aa3ceSMax Schwarz * get the intended effect by resetting its internal state 289c41aa3ceSMax Schwarz * and issuing an ordinary START. 290c41aa3ceSMax Schwarz */ 2917e086c3fSDavid Wu ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; 2927e086c3fSDavid Wu i2c_writel(i2c, ctrl, REG_CON); 293c41aa3ceSMax Schwarz 294c41aa3ceSMax Schwarz /* signal that we are finished with the current msg */ 295c41aa3ceSMax Schwarz wake_up(&i2c->wait); 296c41aa3ceSMax Schwarz } 297c41aa3ceSMax Schwarz } 298c41aa3ceSMax Schwarz 299c41aa3ceSMax Schwarz /** 300c41aa3ceSMax Schwarz * Setup a read according to i2c->msg 301c41aa3ceSMax Schwarz */ 302c41aa3ceSMax Schwarz static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c) 303c41aa3ceSMax Schwarz { 304c41aa3ceSMax Schwarz unsigned int len = i2c->msg->len - i2c->processed; 305c41aa3ceSMax Schwarz u32 con; 306c41aa3ceSMax Schwarz 307c41aa3ceSMax Schwarz con = i2c_readl(i2c, REG_CON); 308c41aa3ceSMax Schwarz 309c41aa3ceSMax Schwarz /* 310c41aa3ceSMax Schwarz * The hw can read up to 32 bytes at a time. If we need more than one 311c41aa3ceSMax Schwarz * chunk, send an ACK after the last byte of the current chunk. 312c41aa3ceSMax Schwarz */ 31329209338SDoug Anderson if (len > 32) { 314c41aa3ceSMax Schwarz len = 32; 315c41aa3ceSMax Schwarz con &= ~REG_CON_LASTACK; 316c41aa3ceSMax Schwarz } else { 317c41aa3ceSMax Schwarz con |= REG_CON_LASTACK; 318c41aa3ceSMax Schwarz } 319c41aa3ceSMax Schwarz 320c41aa3ceSMax Schwarz /* make sure we are in plain RX mode if we read a second chunk */ 321c41aa3ceSMax Schwarz if (i2c->processed != 0) { 322c41aa3ceSMax Schwarz con &= ~REG_CON_MOD_MASK; 323c41aa3ceSMax Schwarz con |= REG_CON_MOD(REG_CON_MOD_RX); 324c41aa3ceSMax Schwarz } 325c41aa3ceSMax Schwarz 326c41aa3ceSMax Schwarz i2c_writel(i2c, con, REG_CON); 327c41aa3ceSMax Schwarz i2c_writel(i2c, len, REG_MRXCNT); 328c41aa3ceSMax Schwarz } 329c41aa3ceSMax Schwarz 330c41aa3ceSMax Schwarz /** 331c41aa3ceSMax Schwarz * Fill the transmit buffer with data from i2c->msg 332c41aa3ceSMax Schwarz */ 333c41aa3ceSMax Schwarz static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c) 334c41aa3ceSMax Schwarz { 335c41aa3ceSMax Schwarz unsigned int i, j; 336c41aa3ceSMax Schwarz u32 cnt = 0; 337c41aa3ceSMax Schwarz u32 val; 338c41aa3ceSMax Schwarz u8 byte; 339c41aa3ceSMax Schwarz 340c41aa3ceSMax Schwarz for (i = 0; i < 8; ++i) { 341c41aa3ceSMax Schwarz val = 0; 342c41aa3ceSMax Schwarz for (j = 0; j < 4; ++j) { 343cf27020dSAlexandru M Stan if ((i2c->processed == i2c->msg->len) && (cnt != 0)) 344c41aa3ceSMax Schwarz break; 345c41aa3ceSMax Schwarz 346c41aa3ceSMax Schwarz if (i2c->processed == 0 && cnt == 0) 347c41aa3ceSMax Schwarz byte = (i2c->addr & 0x7f) << 1; 348c41aa3ceSMax Schwarz else 349c41aa3ceSMax Schwarz byte = i2c->msg->buf[i2c->processed++]; 350c41aa3ceSMax Schwarz 351c41aa3ceSMax Schwarz val |= byte << (j * 8); 352c41aa3ceSMax Schwarz cnt++; 353c41aa3ceSMax Schwarz } 354c41aa3ceSMax Schwarz 355c41aa3ceSMax Schwarz i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i); 356c41aa3ceSMax Schwarz 357c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len) 358c41aa3ceSMax Schwarz break; 359c41aa3ceSMax Schwarz } 360c41aa3ceSMax Schwarz 361c41aa3ceSMax Schwarz i2c_writel(i2c, cnt, REG_MTXCNT); 362c41aa3ceSMax Schwarz } 363c41aa3ceSMax Schwarz 364c41aa3ceSMax Schwarz 365c41aa3ceSMax Schwarz /* IRQ handlers for individual states */ 366c41aa3ceSMax Schwarz 367c41aa3ceSMax Schwarz static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd) 368c41aa3ceSMax Schwarz { 369c41aa3ceSMax Schwarz if (!(ipd & REG_INT_START)) { 370c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO); 371c41aa3ceSMax Schwarz dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd); 372c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c); 373c41aa3ceSMax Schwarz return; 374c41aa3ceSMax Schwarz } 375c41aa3ceSMax Schwarz 376c41aa3ceSMax Schwarz /* ack interrupt */ 377c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_START, REG_IPD); 378c41aa3ceSMax Schwarz 379c41aa3ceSMax Schwarz /* disable start bit */ 380c41aa3ceSMax Schwarz i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON); 381c41aa3ceSMax Schwarz 382c41aa3ceSMax Schwarz /* enable appropriate interrupts and transition */ 383c41aa3ceSMax Schwarz if (i2c->mode == REG_CON_MOD_TX) { 384c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN); 385c41aa3ceSMax Schwarz i2c->state = STATE_WRITE; 386c41aa3ceSMax Schwarz rk3x_i2c_fill_transmit_buf(i2c); 387c41aa3ceSMax Schwarz } else { 388c41aa3ceSMax Schwarz /* in any other case, we are going to be reading. */ 389c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN); 390c41aa3ceSMax Schwarz i2c->state = STATE_READ; 391c41aa3ceSMax Schwarz rk3x_i2c_prepare_read(i2c); 392c41aa3ceSMax Schwarz } 393c41aa3ceSMax Schwarz } 394c41aa3ceSMax Schwarz 395c41aa3ceSMax Schwarz static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd) 396c41aa3ceSMax Schwarz { 397c41aa3ceSMax Schwarz if (!(ipd & REG_INT_MBTF)) { 398c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO); 399c41aa3ceSMax Schwarz dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd); 400c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c); 401c41aa3ceSMax Schwarz return; 402c41aa3ceSMax Schwarz } 403c41aa3ceSMax Schwarz 404c41aa3ceSMax Schwarz /* ack interrupt */ 405c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBTF, REG_IPD); 406c41aa3ceSMax Schwarz 407c41aa3ceSMax Schwarz /* are we finished? */ 408c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len) 409c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, i2c->error); 410c41aa3ceSMax Schwarz else 411c41aa3ceSMax Schwarz rk3x_i2c_fill_transmit_buf(i2c); 412c41aa3ceSMax Schwarz } 413c41aa3ceSMax Schwarz 414c41aa3ceSMax Schwarz static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd) 415c41aa3ceSMax Schwarz { 416c41aa3ceSMax Schwarz unsigned int i; 417c41aa3ceSMax Schwarz unsigned int len = i2c->msg->len - i2c->processed; 418c41aa3ceSMax Schwarz u32 uninitialized_var(val); 419c41aa3ceSMax Schwarz u8 byte; 420c41aa3ceSMax Schwarz 421c41aa3ceSMax Schwarz /* we only care for MBRF here. */ 422c41aa3ceSMax Schwarz if (!(ipd & REG_INT_MBRF)) 423c41aa3ceSMax Schwarz return; 424c41aa3ceSMax Schwarz 425c41aa3ceSMax Schwarz /* ack interrupt */ 426c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBRF, REG_IPD); 427c41aa3ceSMax Schwarz 4285da4309fSaddy ke /* Can only handle a maximum of 32 bytes at a time */ 4295da4309fSaddy ke if (len > 32) 4305da4309fSaddy ke len = 32; 4315da4309fSaddy ke 432c41aa3ceSMax Schwarz /* read the data from receive buffer */ 433c41aa3ceSMax Schwarz for (i = 0; i < len; ++i) { 434c41aa3ceSMax Schwarz if (i % 4 == 0) 435c41aa3ceSMax Schwarz val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4); 436c41aa3ceSMax Schwarz 437c41aa3ceSMax Schwarz byte = (val >> ((i % 4) * 8)) & 0xff; 438c41aa3ceSMax Schwarz i2c->msg->buf[i2c->processed++] = byte; 439c41aa3ceSMax Schwarz } 440c41aa3ceSMax Schwarz 441c41aa3ceSMax Schwarz /* are we finished? */ 442c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len) 443c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, i2c->error); 444c41aa3ceSMax Schwarz else 445c41aa3ceSMax Schwarz rk3x_i2c_prepare_read(i2c); 446c41aa3ceSMax Schwarz } 447c41aa3ceSMax Schwarz 448c41aa3ceSMax Schwarz static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd) 449c41aa3ceSMax Schwarz { 450c41aa3ceSMax Schwarz unsigned int con; 451c41aa3ceSMax Schwarz 452c41aa3ceSMax Schwarz if (!(ipd & REG_INT_STOP)) { 453c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO); 454c41aa3ceSMax Schwarz dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd); 455c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c); 456c41aa3ceSMax Schwarz return; 457c41aa3ceSMax Schwarz } 458c41aa3ceSMax Schwarz 459c41aa3ceSMax Schwarz /* ack interrupt */ 460c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_STOP, REG_IPD); 461c41aa3ceSMax Schwarz 462c41aa3ceSMax Schwarz /* disable STOP bit */ 463c41aa3ceSMax Schwarz con = i2c_readl(i2c, REG_CON); 464c41aa3ceSMax Schwarz con &= ~REG_CON_STOP; 465c41aa3ceSMax Schwarz i2c_writel(i2c, con, REG_CON); 466c41aa3ceSMax Schwarz 467c41aa3ceSMax Schwarz i2c->busy = false; 468c41aa3ceSMax Schwarz i2c->state = STATE_IDLE; 469c41aa3ceSMax Schwarz 470c41aa3ceSMax Schwarz /* signal rk3x_i2c_xfer that we are finished */ 471c41aa3ceSMax Schwarz wake_up(&i2c->wait); 472c41aa3ceSMax Schwarz } 473c41aa3ceSMax Schwarz 474c41aa3ceSMax Schwarz static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id) 475c41aa3ceSMax Schwarz { 476c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = dev_id; 477c41aa3ceSMax Schwarz unsigned int ipd; 478c41aa3ceSMax Schwarz 479c41aa3ceSMax Schwarz spin_lock(&i2c->lock); 480c41aa3ceSMax Schwarz 481c41aa3ceSMax Schwarz ipd = i2c_readl(i2c, REG_IPD); 482c41aa3ceSMax Schwarz if (i2c->state == STATE_IDLE) { 483c41aa3ceSMax Schwarz dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd); 484c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c); 485c41aa3ceSMax Schwarz goto out; 486c41aa3ceSMax Schwarz } 487c41aa3ceSMax Schwarz 488c41aa3ceSMax Schwarz dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd); 489c41aa3ceSMax Schwarz 490c41aa3ceSMax Schwarz /* Clean interrupt bits we don't care about */ 491c41aa3ceSMax Schwarz ipd &= ~(REG_INT_BRF | REG_INT_BTF); 492c41aa3ceSMax Schwarz 493c41aa3ceSMax Schwarz if (ipd & REG_INT_NAKRCV) { 494c41aa3ceSMax Schwarz /* 495c41aa3ceSMax Schwarz * We got a NACK in the last operation. Depending on whether 496c41aa3ceSMax Schwarz * IGNORE_NAK is set, we have to stop the operation and report 497c41aa3ceSMax Schwarz * an error. 498c41aa3ceSMax Schwarz */ 499c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD); 500c41aa3ceSMax Schwarz 501c41aa3ceSMax Schwarz ipd &= ~REG_INT_NAKRCV; 502c41aa3ceSMax Schwarz 503c41aa3ceSMax Schwarz if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) 504c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -ENXIO); 505c41aa3ceSMax Schwarz } 506c41aa3ceSMax Schwarz 507c41aa3ceSMax Schwarz /* is there anything left to handle? */ 50829209338SDoug Anderson if ((ipd & REG_INT_ALL) == 0) 509c41aa3ceSMax Schwarz goto out; 510c41aa3ceSMax Schwarz 511c41aa3ceSMax Schwarz switch (i2c->state) { 512c41aa3ceSMax Schwarz case STATE_START: 513c41aa3ceSMax Schwarz rk3x_i2c_handle_start(i2c, ipd); 514c41aa3ceSMax Schwarz break; 515c41aa3ceSMax Schwarz case STATE_WRITE: 516c41aa3ceSMax Schwarz rk3x_i2c_handle_write(i2c, ipd); 517c41aa3ceSMax Schwarz break; 518c41aa3ceSMax Schwarz case STATE_READ: 519c41aa3ceSMax Schwarz rk3x_i2c_handle_read(i2c, ipd); 520c41aa3ceSMax Schwarz break; 521c41aa3ceSMax Schwarz case STATE_STOP: 522c41aa3ceSMax Schwarz rk3x_i2c_handle_stop(i2c, ipd); 523c41aa3ceSMax Schwarz break; 524c41aa3ceSMax Schwarz case STATE_IDLE: 525c41aa3ceSMax Schwarz break; 526c41aa3ceSMax Schwarz } 527c41aa3ceSMax Schwarz 528c41aa3ceSMax Schwarz out: 529c41aa3ceSMax Schwarz spin_unlock(&i2c->lock); 530c41aa3ceSMax Schwarz return IRQ_HANDLED; 531c41aa3ceSMax Schwarz } 532c41aa3ceSMax Schwarz 533249051f4SMax Schwarz /** 534b58fd3beSDavid Wu * Get timing values of I2C specification 535b58fd3beSDavid Wu * 536b58fd3beSDavid Wu * @speed: Desired SCL frequency 537b58fd3beSDavid Wu * 538b58fd3beSDavid Wu * Returns: Matched i2c spec values. 539b58fd3beSDavid Wu */ 540b58fd3beSDavid Wu static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed) 541b58fd3beSDavid Wu { 54290224e64SAndy Shevchenko if (speed <= I2C_MAX_STANDARD_MODE_FREQ) 543b58fd3beSDavid Wu return &standard_mode_spec; 54490224e64SAndy Shevchenko else if (speed <= I2C_MAX_FAST_MODE_FREQ) 545b58fd3beSDavid Wu return &fast_mode_spec; 546a02f3d08SDavid Wu else 547a02f3d08SDavid Wu return &fast_mode_plus_spec; 548b58fd3beSDavid Wu } 549b58fd3beSDavid Wu 550b58fd3beSDavid Wu /** 551249051f4SMax Schwarz * Calculate divider values for desired SCL frequency 552249051f4SMax Schwarz * 553249051f4SMax Schwarz * @clk_rate: I2C input clock rate 554e26747bfSDavid Wu * @t: Known I2C timing information 555e26747bfSDavid Wu * @t_calc: Caculated rk3x private timings that would be written into regs 556249051f4SMax Schwarz * 557249051f4SMax Schwarz * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case 558249051f4SMax Schwarz * a best-effort divider value is returned in divs. If the target rate is 559249051f4SMax Schwarz * too high, we silently use the highest possible rate. 560249051f4SMax Schwarz */ 5617e086c3fSDavid Wu static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate, 5621ab92956SDavid Wu struct i2c_timings *t, 563e26747bfSDavid Wu struct rk3x_i2c_calced_timings *t_calc) 5640285f8f5Saddy ke { 5651330e291Saddy ke unsigned long min_low_ns, min_high_ns; 5660285f8f5Saddy ke unsigned long max_low_ns, min_total_ns; 5670285f8f5Saddy ke 568249051f4SMax Schwarz unsigned long clk_rate_khz, scl_rate_khz; 5690285f8f5Saddy ke 5700285f8f5Saddy ke unsigned long min_low_div, min_high_div; 5710285f8f5Saddy ke unsigned long max_low_div; 5720285f8f5Saddy ke 5730285f8f5Saddy ke unsigned long min_div_for_hold, min_total_div; 5740285f8f5Saddy ke unsigned long extra_div, extra_low_div, ideal_low_div; 5750285f8f5Saddy ke 576b58fd3beSDavid Wu unsigned long data_hold_buffer_ns = 50; 577b58fd3beSDavid Wu const struct i2c_spec_values *spec; 578249051f4SMax Schwarz int ret = 0; 579249051f4SMax Schwarz 5800285f8f5Saddy ke /* Only support standard-mode and fast-mode */ 58190224e64SAndy Shevchenko if (WARN_ON(t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ)) 58290224e64SAndy Shevchenko t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ; 5830285f8f5Saddy ke 5840285f8f5Saddy ke /* prevent scl_rate_khz from becoming 0 */ 5851ab92956SDavid Wu if (WARN_ON(t->bus_freq_hz < 1000)) 5861ab92956SDavid Wu t->bus_freq_hz = 1000; 5870285f8f5Saddy ke 5880285f8f5Saddy ke /* 5891330e291Saddy ke * min_low_ns: The minimum number of ns we need to hold low to 5901330e291Saddy ke * meet I2C specification, should include fall time. 5911330e291Saddy ke * min_high_ns: The minimum number of ns we need to hold high to 5921330e291Saddy ke * meet I2C specification, should include rise time. 5931330e291Saddy ke * max_low_ns: The maximum number of ns we can hold low to meet 5941330e291Saddy ke * I2C specification. 5950285f8f5Saddy ke * 5961330e291Saddy ke * Note: max_low_ns should be (maximum data hold time * 2 - buffer) 5970285f8f5Saddy ke * This is because the i2c host on Rockchip holds the data line 5980285f8f5Saddy ke * for half the low time. 5990285f8f5Saddy ke */ 600b58fd3beSDavid Wu spec = rk3x_i2c_get_spec(t->bus_freq_hz); 601b58fd3beSDavid Wu min_high_ns = t->scl_rise_ns + spec->min_high_ns; 602387f0de6SDoug Anderson 603387f0de6SDoug Anderson /* 604387f0de6SDoug Anderson * Timings for repeated start: 605387f0de6SDoug Anderson * - controller appears to drop SDA at .875x (7/8) programmed clk high. 606387f0de6SDoug Anderson * - controller appears to keep SCL high for 2x programmed clk high. 607387f0de6SDoug Anderson * 608387f0de6SDoug Anderson * We need to account for those rules in picking our "high" time so 609387f0de6SDoug Anderson * we meet tSU;STA and tHD;STA times. 610387f0de6SDoug Anderson */ 611b58fd3beSDavid Wu min_high_ns = max(min_high_ns, DIV_ROUND_UP( 612b58fd3beSDavid Wu (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875)); 613b58fd3beSDavid Wu min_high_ns = max(min_high_ns, DIV_ROUND_UP( 614b58fd3beSDavid Wu (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns + 615b58fd3beSDavid Wu spec->min_high_ns), 2)); 616387f0de6SDoug Anderson 617b58fd3beSDavid Wu min_low_ns = t->scl_fall_ns + spec->min_low_ns; 618b58fd3beSDavid Wu max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns; 6190285f8f5Saddy ke min_total_ns = min_low_ns + min_high_ns; 6200285f8f5Saddy ke 6210285f8f5Saddy ke /* Adjust to avoid overflow */ 622249051f4SMax Schwarz clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000); 6231ab92956SDavid Wu scl_rate_khz = t->bus_freq_hz / 1000; 6240285f8f5Saddy ke 6250285f8f5Saddy ke /* 6260285f8f5Saddy ke * We need the total div to be >= this number 6270285f8f5Saddy ke * so we don't clock too fast. 6280285f8f5Saddy ke */ 629249051f4SMax Schwarz min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8); 6300285f8f5Saddy ke 6310285f8f5Saddy ke /* These are the min dividers needed for min hold times. */ 632249051f4SMax Schwarz min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000); 633249051f4SMax Schwarz min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000); 6340285f8f5Saddy ke min_div_for_hold = (min_low_div + min_high_div); 6350285f8f5Saddy ke 6360285f8f5Saddy ke /* 6371330e291Saddy ke * This is the maximum divider so we don't go over the maximum. 6381330e291Saddy ke * We don't round up here (we round down) since this is a maximum. 6390285f8f5Saddy ke */ 640249051f4SMax Schwarz max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000); 6410285f8f5Saddy ke 6420285f8f5Saddy ke if (min_low_div > max_low_div) { 6430285f8f5Saddy ke WARN_ONCE(true, 6440285f8f5Saddy ke "Conflicting, min_low_div %lu, max_low_div %lu\n", 6450285f8f5Saddy ke min_low_div, max_low_div); 6460285f8f5Saddy ke max_low_div = min_low_div; 6470285f8f5Saddy ke } 6480285f8f5Saddy ke 6490285f8f5Saddy ke if (min_div_for_hold > min_total_div) { 6500285f8f5Saddy ke /* 6510285f8f5Saddy ke * Time needed to meet hold requirements is important. 6520285f8f5Saddy ke * Just use that. 6530285f8f5Saddy ke */ 654e26747bfSDavid Wu t_calc->div_low = min_low_div; 655e26747bfSDavid Wu t_calc->div_high = min_high_div; 6560285f8f5Saddy ke } else { 6570285f8f5Saddy ke /* 6580285f8f5Saddy ke * We've got to distribute some time among the low and high 6590285f8f5Saddy ke * so we don't run too fast. 6600285f8f5Saddy ke */ 6610285f8f5Saddy ke extra_div = min_total_div - min_div_for_hold; 6620285f8f5Saddy ke 6630285f8f5Saddy ke /* 6640285f8f5Saddy ke * We'll try to split things up perfectly evenly, 6650285f8f5Saddy ke * biasing slightly towards having a higher div 6660285f8f5Saddy ke * for low (spend more time low). 6670285f8f5Saddy ke */ 668249051f4SMax Schwarz ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 6690285f8f5Saddy ke scl_rate_khz * 8 * min_total_ns); 6700285f8f5Saddy ke 6711330e291Saddy ke /* Don't allow it to go over the maximum */ 6720285f8f5Saddy ke if (ideal_low_div > max_low_div) 6730285f8f5Saddy ke ideal_low_div = max_low_div; 6740285f8f5Saddy ke 6750285f8f5Saddy ke /* 6760285f8f5Saddy ke * Handle when the ideal low div is going to take up 6770285f8f5Saddy ke * more than we have. 6780285f8f5Saddy ke */ 6790285f8f5Saddy ke if (ideal_low_div > min_low_div + extra_div) 6800285f8f5Saddy ke ideal_low_div = min_low_div + extra_div; 6810285f8f5Saddy ke 6820285f8f5Saddy ke /* Give low the "ideal" and give high whatever extra is left */ 6830285f8f5Saddy ke extra_low_div = ideal_low_div - min_low_div; 684e26747bfSDavid Wu t_calc->div_low = ideal_low_div; 685e26747bfSDavid Wu t_calc->div_high = min_high_div + (extra_div - extra_low_div); 6860285f8f5Saddy ke } 6870285f8f5Saddy ke 6880285f8f5Saddy ke /* 6890285f8f5Saddy ke * Adjust to the fact that the hardware has an implicit "+1". 6900285f8f5Saddy ke * NOTE: Above calculations always produce div_low > 0 and div_high > 0. 6910285f8f5Saddy ke */ 692e26747bfSDavid Wu t_calc->div_low--; 693e26747bfSDavid Wu t_calc->div_high--; 6940285f8f5Saddy ke 695399c168aSDavid Wu /* Give the tuning value 0, that would not update con register */ 696399c168aSDavid Wu t_calc->tuning = 0; 697249051f4SMax Schwarz /* Maximum divider supported by hw is 0xffff */ 698e26747bfSDavid Wu if (t_calc->div_low > 0xffff) { 699e26747bfSDavid Wu t_calc->div_low = 0xffff; 700249051f4SMax Schwarz ret = -EINVAL; 7010285f8f5Saddy ke } 7020285f8f5Saddy ke 703e26747bfSDavid Wu if (t_calc->div_high > 0xffff) { 704e26747bfSDavid Wu t_calc->div_high = 0xffff; 705249051f4SMax Schwarz ret = -EINVAL; 706249051f4SMax Schwarz } 707249051f4SMax Schwarz 708249051f4SMax Schwarz return ret; 709249051f4SMax Schwarz } 710249051f4SMax Schwarz 7117e086c3fSDavid Wu /** 7127e086c3fSDavid Wu * Calculate timing values for desired SCL frequency 7137e086c3fSDavid Wu * 7147e086c3fSDavid Wu * @clk_rate: I2C input clock rate 7157e086c3fSDavid Wu * @t: Known I2C timing information 7167e086c3fSDavid Wu * @t_calc: Caculated rk3x private timings that would be written into regs 7177e086c3fSDavid Wu * 7187e086c3fSDavid Wu * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case 7197e086c3fSDavid Wu * a best-effort divider value is returned in divs. If the target rate is 7207e086c3fSDavid Wu * too high, we silently use the highest possible rate. 7217e086c3fSDavid Wu * The following formulas are v1's method to calculate timings. 7227e086c3fSDavid Wu * 7237e086c3fSDavid Wu * l = divl + 1; 7247e086c3fSDavid Wu * h = divh + 1; 7257e086c3fSDavid Wu * s = sda_update_config + 1; 7267e086c3fSDavid Wu * u = start_setup_config + 1; 7277e086c3fSDavid Wu * p = stop_setup_config + 1; 7287e086c3fSDavid Wu * T = Tclk_i2c; 7297e086c3fSDavid Wu * 7307e086c3fSDavid Wu * tHigh = 8 * h * T; 7317e086c3fSDavid Wu * tLow = 8 * l * T; 7327e086c3fSDavid Wu * 7337e086c3fSDavid Wu * tHD;sda = (l * s + 1) * T; 7347e086c3fSDavid Wu * tSU;sda = [(8 - s) * l + 1] * T; 7357e086c3fSDavid Wu * tI2C = 8 * (l + h) * T; 7367e086c3fSDavid Wu * 7377e086c3fSDavid Wu * tSU;sta = (8h * u + 1) * T; 7387e086c3fSDavid Wu * tHD;sta = [8h * (u + 1) - 1] * T; 7397e086c3fSDavid Wu * tSU;sto = (8h * p + 1) * T; 7407e086c3fSDavid Wu */ 7417e086c3fSDavid Wu static int rk3x_i2c_v1_calc_timings(unsigned long clk_rate, 7427e086c3fSDavid Wu struct i2c_timings *t, 7437e086c3fSDavid Wu struct rk3x_i2c_calced_timings *t_calc) 7447e086c3fSDavid Wu { 74572cf8c56SDavid Wu unsigned long min_low_ns, min_high_ns; 7467e086c3fSDavid Wu unsigned long min_setup_start_ns, min_setup_data_ns; 7477e086c3fSDavid Wu unsigned long min_setup_stop_ns, max_hold_data_ns; 7487e086c3fSDavid Wu 7497e086c3fSDavid Wu unsigned long clk_rate_khz, scl_rate_khz; 7507e086c3fSDavid Wu 7517e086c3fSDavid Wu unsigned long min_low_div, min_high_div; 7527e086c3fSDavid Wu 7537e086c3fSDavid Wu unsigned long min_div_for_hold, min_total_div; 7547e086c3fSDavid Wu unsigned long extra_div, extra_low_div; 7557e086c3fSDavid Wu unsigned long sda_update_cfg, stp_sta_cfg, stp_sto_cfg; 7567e086c3fSDavid Wu 7577e086c3fSDavid Wu const struct i2c_spec_values *spec; 7587e086c3fSDavid Wu int ret = 0; 7597e086c3fSDavid Wu 760a02f3d08SDavid Wu /* Support standard-mode, fast-mode and fast-mode plus */ 76190224e64SAndy Shevchenko if (WARN_ON(t->bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ)) 76290224e64SAndy Shevchenko t->bus_freq_hz = I2C_MAX_FAST_MODE_PLUS_FREQ; 7637e086c3fSDavid Wu 7647e086c3fSDavid Wu /* prevent scl_rate_khz from becoming 0 */ 7657e086c3fSDavid Wu if (WARN_ON(t->bus_freq_hz < 1000)) 7667e086c3fSDavid Wu t->bus_freq_hz = 1000; 7677e086c3fSDavid Wu 7687e086c3fSDavid Wu /* 7697e086c3fSDavid Wu * min_low_ns: The minimum number of ns we need to hold low to 7707e086c3fSDavid Wu * meet I2C specification, should include fall time. 7717e086c3fSDavid Wu * min_high_ns: The minimum number of ns we need to hold high to 7727e086c3fSDavid Wu * meet I2C specification, should include rise time. 7737e086c3fSDavid Wu */ 7747e086c3fSDavid Wu spec = rk3x_i2c_get_spec(t->bus_freq_hz); 7757e086c3fSDavid Wu 7767e086c3fSDavid Wu /* calculate min-divh and min-divl */ 7777e086c3fSDavid Wu clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000); 7787e086c3fSDavid Wu scl_rate_khz = t->bus_freq_hz / 1000; 7797e086c3fSDavid Wu min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8); 7807e086c3fSDavid Wu 7817e086c3fSDavid Wu min_high_ns = t->scl_rise_ns + spec->min_high_ns; 7827e086c3fSDavid Wu min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000); 7837e086c3fSDavid Wu 7847e086c3fSDavid Wu min_low_ns = t->scl_fall_ns + spec->min_low_ns; 7857e086c3fSDavid Wu min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000); 7867e086c3fSDavid Wu 7877e086c3fSDavid Wu /* 7887e086c3fSDavid Wu * Final divh and divl must be greater than 0, otherwise the 7897e086c3fSDavid Wu * hardware would not output the i2c clk. 7907e086c3fSDavid Wu */ 7917e086c3fSDavid Wu min_high_div = (min_high_div < 1) ? 2 : min_high_div; 7927e086c3fSDavid Wu min_low_div = (min_low_div < 1) ? 2 : min_low_div; 7937e086c3fSDavid Wu 7947e086c3fSDavid Wu /* These are the min dividers needed for min hold times. */ 7957e086c3fSDavid Wu min_div_for_hold = (min_low_div + min_high_div); 7967e086c3fSDavid Wu 7977e086c3fSDavid Wu /* 7987e086c3fSDavid Wu * This is the maximum divider so we don't go over the maximum. 7997e086c3fSDavid Wu * We don't round up here (we round down) since this is a maximum. 8007e086c3fSDavid Wu */ 8017e086c3fSDavid Wu if (min_div_for_hold >= min_total_div) { 8027e086c3fSDavid Wu /* 8037e086c3fSDavid Wu * Time needed to meet hold requirements is important. 8047e086c3fSDavid Wu * Just use that. 8057e086c3fSDavid Wu */ 8067e086c3fSDavid Wu t_calc->div_low = min_low_div; 8077e086c3fSDavid Wu t_calc->div_high = min_high_div; 8087e086c3fSDavid Wu } else { 8097e086c3fSDavid Wu /* 8107e086c3fSDavid Wu * We've got to distribute some time among the low and high 8117e086c3fSDavid Wu * so we don't run too fast. 8127e086c3fSDavid Wu * We'll try to split things up by the scale of min_low_div and 8137e086c3fSDavid Wu * min_high_div, biasing slightly towards having a higher div 8147e086c3fSDavid Wu * for low (spend more time low). 8157e086c3fSDavid Wu */ 8167e086c3fSDavid Wu extra_div = min_total_div - min_div_for_hold; 8177e086c3fSDavid Wu extra_low_div = DIV_ROUND_UP(min_low_div * extra_div, 8187e086c3fSDavid Wu min_div_for_hold); 8197e086c3fSDavid Wu 8207e086c3fSDavid Wu t_calc->div_low = min_low_div + extra_low_div; 8217e086c3fSDavid Wu t_calc->div_high = min_high_div + (extra_div - extra_low_div); 8227e086c3fSDavid Wu } 8237e086c3fSDavid Wu 8247e086c3fSDavid Wu /* 8257e086c3fSDavid Wu * calculate sda data hold count by the rules, data_upd_st:3 8267e086c3fSDavid Wu * is a appropriate value to reduce calculated times. 8277e086c3fSDavid Wu */ 8287e086c3fSDavid Wu for (sda_update_cfg = 3; sda_update_cfg > 0; sda_update_cfg--) { 8297e086c3fSDavid Wu max_hold_data_ns = DIV_ROUND_UP((sda_update_cfg 8307e086c3fSDavid Wu * (t_calc->div_low) + 1) 8317e086c3fSDavid Wu * 1000000, clk_rate_khz); 8327e086c3fSDavid Wu min_setup_data_ns = DIV_ROUND_UP(((8 - sda_update_cfg) 8337e086c3fSDavid Wu * (t_calc->div_low) + 1) 8347e086c3fSDavid Wu * 1000000, clk_rate_khz); 8357e086c3fSDavid Wu if ((max_hold_data_ns < spec->max_data_hold_ns) && 8367e086c3fSDavid Wu (min_setup_data_ns > spec->min_data_setup_ns)) 8377e086c3fSDavid Wu break; 8387e086c3fSDavid Wu } 8397e086c3fSDavid Wu 8407e086c3fSDavid Wu /* calculate setup start config */ 8417e086c3fSDavid Wu min_setup_start_ns = t->scl_rise_ns + spec->min_setup_start_ns; 8427e086c3fSDavid Wu stp_sta_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_start_ns 8437e086c3fSDavid Wu - 1000000, 8 * 1000000 * (t_calc->div_high)); 8447e086c3fSDavid Wu 8457e086c3fSDavid Wu /* calculate setup stop config */ 8467e086c3fSDavid Wu min_setup_stop_ns = t->scl_rise_ns + spec->min_setup_stop_ns; 8477e086c3fSDavid Wu stp_sto_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_stop_ns 8487e086c3fSDavid Wu - 1000000, 8 * 1000000 * (t_calc->div_high)); 8497e086c3fSDavid Wu 8507e086c3fSDavid Wu t_calc->tuning = REG_CON_SDA_CFG(--sda_update_cfg) | 8517e086c3fSDavid Wu REG_CON_STA_CFG(--stp_sta_cfg) | 8527e086c3fSDavid Wu REG_CON_STO_CFG(--stp_sto_cfg); 8537e086c3fSDavid Wu 8547e086c3fSDavid Wu t_calc->div_low--; 8557e086c3fSDavid Wu t_calc->div_high--; 8567e086c3fSDavid Wu 8577e086c3fSDavid Wu /* Maximum divider supported by hw is 0xffff */ 8587e086c3fSDavid Wu if (t_calc->div_low > 0xffff) { 8597e086c3fSDavid Wu t_calc->div_low = 0xffff; 8607e086c3fSDavid Wu ret = -EINVAL; 8617e086c3fSDavid Wu } 8627e086c3fSDavid Wu 8637e086c3fSDavid Wu if (t_calc->div_high > 0xffff) { 8647e086c3fSDavid Wu t_calc->div_high = 0xffff; 8657e086c3fSDavid Wu ret = -EINVAL; 8667e086c3fSDavid Wu } 8677e086c3fSDavid Wu 8687e086c3fSDavid Wu return ret; 8697e086c3fSDavid Wu } 8707e086c3fSDavid Wu 871249051f4SMax Schwarz static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate) 872c41aa3ceSMax Schwarz { 8731ab92956SDavid Wu struct i2c_timings *t = &i2c->t; 874e26747bfSDavid Wu struct rk3x_i2c_calced_timings calc; 8750285f8f5Saddy ke u64 t_low_ns, t_high_ns; 8767e086c3fSDavid Wu unsigned long flags; 8777e086c3fSDavid Wu u32 val; 878249051f4SMax Schwarz int ret; 879c41aa3ceSMax Schwarz 8807e086c3fSDavid Wu ret = i2c->soc_data->calc_timings(clk_rate, t, &calc); 8811ab92956SDavid Wu WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz); 882249051f4SMax Schwarz 8837e086c3fSDavid Wu clk_enable(i2c->pclk); 8847e086c3fSDavid Wu 8857e086c3fSDavid Wu spin_lock_irqsave(&i2c->lock, flags); 8867e086c3fSDavid Wu val = i2c_readl(i2c, REG_CON); 8877e086c3fSDavid Wu val &= ~REG_CON_TUNING_MASK; 8887e086c3fSDavid Wu val |= calc.tuning; 8897e086c3fSDavid Wu i2c_writel(i2c, val, REG_CON); 890e26747bfSDavid Wu i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff), 891e26747bfSDavid Wu REG_CLKDIV); 8927e086c3fSDavid Wu spin_unlock_irqrestore(&i2c->lock, flags); 8937e086c3fSDavid Wu 8947e086c3fSDavid Wu clk_disable(i2c->pclk); 8950285f8f5Saddy ke 896e26747bfSDavid Wu t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate); 897e26747bfSDavid Wu t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000, 898e26747bfSDavid Wu clk_rate); 8990285f8f5Saddy ke dev_dbg(i2c->dev, 900249051f4SMax Schwarz "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n", 901249051f4SMax Schwarz clk_rate / 1000, 9021ab92956SDavid Wu 1000000000 / t->bus_freq_hz, 9030285f8f5Saddy ke t_low_ns, t_high_ns); 904249051f4SMax Schwarz } 9050285f8f5Saddy ke 906249051f4SMax Schwarz /** 907249051f4SMax Schwarz * rk3x_i2c_clk_notifier_cb - Clock rate change callback 908249051f4SMax Schwarz * @nb: Pointer to notifier block 909249051f4SMax Schwarz * @event: Notification reason 910249051f4SMax Schwarz * @data: Pointer to notification data object 911249051f4SMax Schwarz * 912249051f4SMax Schwarz * The callback checks whether a valid bus frequency can be generated after the 913249051f4SMax Schwarz * change. If so, the change is acknowledged, otherwise the change is aborted. 914249051f4SMax Schwarz * New dividers are written to the HW in the pre- or post change notification 915249051f4SMax Schwarz * depending on the scaling direction. 916249051f4SMax Schwarz * 917249051f4SMax Schwarz * Code adapted from i2c-cadence.c. 918249051f4SMax Schwarz * 919249051f4SMax Schwarz * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK 920e0603c8dSGeert Uytterhoeven * to acknowledge the change, NOTIFY_DONE if the notification is 921249051f4SMax Schwarz * considered irrelevant. 922249051f4SMax Schwarz */ 923249051f4SMax Schwarz static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long 924249051f4SMax Schwarz event, void *data) 925249051f4SMax Schwarz { 926249051f4SMax Schwarz struct clk_notifier_data *ndata = data; 927249051f4SMax Schwarz struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb); 928e26747bfSDavid Wu struct rk3x_i2c_calced_timings calc; 929249051f4SMax Schwarz 930249051f4SMax Schwarz switch (event) { 931249051f4SMax Schwarz case PRE_RATE_CHANGE: 9327e086c3fSDavid Wu /* 9337e086c3fSDavid Wu * Try the calculation (but don't store the result) ahead of 9347e086c3fSDavid Wu * time to see if we need to block the clock change. Timings 9357e086c3fSDavid Wu * shouldn't actually take effect until rk3x_i2c_adapt_div(). 9367e086c3fSDavid Wu */ 9377e086c3fSDavid Wu if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t, 9387e086c3fSDavid Wu &calc) != 0) 939249051f4SMax Schwarz return NOTIFY_STOP; 940249051f4SMax Schwarz 941249051f4SMax Schwarz /* scale up */ 942249051f4SMax Schwarz if (ndata->new_rate > ndata->old_rate) 943249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->new_rate); 944249051f4SMax Schwarz 945249051f4SMax Schwarz return NOTIFY_OK; 946249051f4SMax Schwarz case POST_RATE_CHANGE: 947249051f4SMax Schwarz /* scale down */ 948249051f4SMax Schwarz if (ndata->new_rate < ndata->old_rate) 949249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->new_rate); 950249051f4SMax Schwarz return NOTIFY_OK; 951249051f4SMax Schwarz case ABORT_RATE_CHANGE: 952249051f4SMax Schwarz /* scale up */ 953249051f4SMax Schwarz if (ndata->new_rate > ndata->old_rate) 954249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->old_rate); 955249051f4SMax Schwarz return NOTIFY_OK; 956249051f4SMax Schwarz default: 957249051f4SMax Schwarz return NOTIFY_DONE; 958249051f4SMax Schwarz } 959c41aa3ceSMax Schwarz } 960c41aa3ceSMax Schwarz 961c41aa3ceSMax Schwarz /** 962c41aa3ceSMax Schwarz * Setup I2C registers for an I2C operation specified by msgs, num. 963c41aa3ceSMax Schwarz * 964c41aa3ceSMax Schwarz * Must be called with i2c->lock held. 965c41aa3ceSMax Schwarz * 966c41aa3ceSMax Schwarz * @msgs: I2C msgs to process 967c41aa3ceSMax Schwarz * @num: Number of msgs 968c41aa3ceSMax Schwarz * 969c41aa3ceSMax Schwarz * returns: Number of I2C msgs processed or negative in case of error 970c41aa3ceSMax Schwarz */ 971c41aa3ceSMax Schwarz static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num) 972c41aa3ceSMax Schwarz { 973c41aa3ceSMax Schwarz u32 addr = (msgs[0].addr & 0x7f) << 1; 974c41aa3ceSMax Schwarz int ret = 0; 975c41aa3ceSMax Schwarz 976c41aa3ceSMax Schwarz /* 977c41aa3ceSMax Schwarz * The I2C adapter can issue a small (len < 4) write packet before 978c41aa3ceSMax Schwarz * reading. This speeds up SMBus-style register reads. 979c41aa3ceSMax Schwarz * The MRXADDR/MRXRADDR hold the slave address and the slave register 980c41aa3ceSMax Schwarz * address in this case. 981c41aa3ceSMax Schwarz */ 982c41aa3ceSMax Schwarz 983c41aa3ceSMax Schwarz if (num >= 2 && msgs[0].len < 4 && 984c41aa3ceSMax Schwarz !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) { 985c41aa3ceSMax Schwarz u32 reg_addr = 0; 986c41aa3ceSMax Schwarz int i; 987c41aa3ceSMax Schwarz 988c41aa3ceSMax Schwarz dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n", 989c41aa3ceSMax Schwarz addr >> 1); 990c41aa3ceSMax Schwarz 991c41aa3ceSMax Schwarz /* Fill MRXRADDR with the register address(es) */ 992c41aa3ceSMax Schwarz for (i = 0; i < msgs[0].len; ++i) { 993c41aa3ceSMax Schwarz reg_addr |= msgs[0].buf[i] << (i * 8); 994c41aa3ceSMax Schwarz reg_addr |= REG_MRXADDR_VALID(i); 995c41aa3ceSMax Schwarz } 996c41aa3ceSMax Schwarz 997c41aa3ceSMax Schwarz /* msgs[0] is handled by hw. */ 998c41aa3ceSMax Schwarz i2c->msg = &msgs[1]; 999c41aa3ceSMax Schwarz 1000c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_REGISTER_TX; 1001c41aa3ceSMax Schwarz 1002c41aa3ceSMax Schwarz i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR); 1003c41aa3ceSMax Schwarz i2c_writel(i2c, reg_addr, REG_MRXRADDR); 1004c41aa3ceSMax Schwarz 1005c41aa3ceSMax Schwarz ret = 2; 1006c41aa3ceSMax Schwarz } else { 1007c41aa3ceSMax Schwarz /* 1008c41aa3ceSMax Schwarz * We'll have to do it the boring way and process the msgs 1009c41aa3ceSMax Schwarz * one-by-one. 1010c41aa3ceSMax Schwarz */ 1011c41aa3ceSMax Schwarz 1012c41aa3ceSMax Schwarz if (msgs[0].flags & I2C_M_RD) { 1013c41aa3ceSMax Schwarz addr |= 1; /* set read bit */ 1014c41aa3ceSMax Schwarz 1015c41aa3ceSMax Schwarz /* 1016c41aa3ceSMax Schwarz * We have to transmit the slave addr first. Use 1017c41aa3ceSMax Schwarz * MOD_REGISTER_TX for that purpose. 1018c41aa3ceSMax Schwarz */ 1019c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_REGISTER_TX; 1020c41aa3ceSMax Schwarz i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), 1021c41aa3ceSMax Schwarz REG_MRXADDR); 1022c41aa3ceSMax Schwarz i2c_writel(i2c, 0, REG_MRXRADDR); 1023c41aa3ceSMax Schwarz } else { 1024c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_TX; 1025c41aa3ceSMax Schwarz } 1026c41aa3ceSMax Schwarz 1027c41aa3ceSMax Schwarz i2c->msg = &msgs[0]; 1028c41aa3ceSMax Schwarz 1029c41aa3ceSMax Schwarz ret = 1; 1030c41aa3ceSMax Schwarz } 1031c41aa3ceSMax Schwarz 1032c41aa3ceSMax Schwarz i2c->addr = msgs[0].addr; 1033c41aa3ceSMax Schwarz i2c->busy = true; 1034c41aa3ceSMax Schwarz i2c->state = STATE_START; 1035c41aa3ceSMax Schwarz i2c->processed = 0; 1036c41aa3ceSMax Schwarz i2c->error = 0; 1037c41aa3ceSMax Schwarz 1038c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c); 1039c41aa3ceSMax Schwarz 1040c41aa3ceSMax Schwarz return ret; 1041c41aa3ceSMax Schwarz } 1042c41aa3ceSMax Schwarz 1043c41aa3ceSMax Schwarz static int rk3x_i2c_xfer(struct i2c_adapter *adap, 1044c41aa3ceSMax Schwarz struct i2c_msg *msgs, int num) 1045c41aa3ceSMax Schwarz { 1046c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data; 1047c41aa3ceSMax Schwarz unsigned long timeout, flags; 10487e086c3fSDavid Wu u32 val; 1049c41aa3ceSMax Schwarz int ret = 0; 1050c41aa3ceSMax Schwarz int i; 1051c41aa3ceSMax Schwarz 1052c41aa3ceSMax Schwarz spin_lock_irqsave(&i2c->lock, flags); 1053c41aa3ceSMax Schwarz 1054c41aa3ceSMax Schwarz clk_enable(i2c->clk); 10557e086c3fSDavid Wu clk_enable(i2c->pclk); 1056c41aa3ceSMax Schwarz 1057c41aa3ceSMax Schwarz i2c->is_last_msg = false; 1058c41aa3ceSMax Schwarz 1059c41aa3ceSMax Schwarz /* 1060c41aa3ceSMax Schwarz * Process msgs. We can handle more than one message at once (see 1061c41aa3ceSMax Schwarz * rk3x_i2c_setup()). 1062c41aa3ceSMax Schwarz */ 1063c41aa3ceSMax Schwarz for (i = 0; i < num; i += ret) { 1064c41aa3ceSMax Schwarz ret = rk3x_i2c_setup(i2c, msgs + i, num - i); 1065c41aa3ceSMax Schwarz 1066c41aa3ceSMax Schwarz if (ret < 0) { 1067c41aa3ceSMax Schwarz dev_err(i2c->dev, "rk3x_i2c_setup() failed\n"); 1068c41aa3ceSMax Schwarz break; 1069c41aa3ceSMax Schwarz } 1070c41aa3ceSMax Schwarz 1071c41aa3ceSMax Schwarz if (i + ret >= num) 1072c41aa3ceSMax Schwarz i2c->is_last_msg = true; 1073c41aa3ceSMax Schwarz 1074c41aa3ceSMax Schwarz spin_unlock_irqrestore(&i2c->lock, flags); 1075c41aa3ceSMax Schwarz 1076c41aa3ceSMax Schwarz rk3x_i2c_start(i2c); 1077c41aa3ceSMax Schwarz 1078c41aa3ceSMax Schwarz timeout = wait_event_timeout(i2c->wait, !i2c->busy, 1079c41aa3ceSMax Schwarz msecs_to_jiffies(WAIT_TIMEOUT)); 1080c41aa3ceSMax Schwarz 1081c41aa3ceSMax Schwarz spin_lock_irqsave(&i2c->lock, flags); 1082c41aa3ceSMax Schwarz 1083c41aa3ceSMax Schwarz if (timeout == 0) { 1084c41aa3ceSMax Schwarz dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n", 1085c41aa3ceSMax Schwarz i2c_readl(i2c, REG_IPD), i2c->state); 1086c41aa3ceSMax Schwarz 1087c41aa3ceSMax Schwarz /* Force a STOP condition without interrupt */ 1088c41aa3ceSMax Schwarz i2c_writel(i2c, 0, REG_IEN); 10897e086c3fSDavid Wu val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; 10907e086c3fSDavid Wu val |= REG_CON_EN | REG_CON_STOP; 10917e086c3fSDavid Wu i2c_writel(i2c, val, REG_CON); 1092c41aa3ceSMax Schwarz 1093c41aa3ceSMax Schwarz i2c->state = STATE_IDLE; 1094c41aa3ceSMax Schwarz 1095c41aa3ceSMax Schwarz ret = -ETIMEDOUT; 1096c41aa3ceSMax Schwarz break; 1097c41aa3ceSMax Schwarz } 1098c41aa3ceSMax Schwarz 1099c41aa3ceSMax Schwarz if (i2c->error) { 1100c41aa3ceSMax Schwarz ret = i2c->error; 1101c41aa3ceSMax Schwarz break; 1102c41aa3ceSMax Schwarz } 1103c41aa3ceSMax Schwarz } 1104c41aa3ceSMax Schwarz 11057e086c3fSDavid Wu clk_disable(i2c->pclk); 1106c41aa3ceSMax Schwarz clk_disable(i2c->clk); 11077e086c3fSDavid Wu 1108c41aa3ceSMax Schwarz spin_unlock_irqrestore(&i2c->lock, flags); 1109c41aa3ceSMax Schwarz 1110c6cbfb91SDmitry Torokhov return ret < 0 ? ret : num; 1111c41aa3ceSMax Schwarz } 1112c41aa3ceSMax Schwarz 1113cbfff439SDoug Anderson static __maybe_unused int rk3x_i2c_resume(struct device *dev) 1114cbfff439SDoug Anderson { 1115cbfff439SDoug Anderson struct rk3x_i2c *i2c = dev_get_drvdata(dev); 1116cbfff439SDoug Anderson 1117cbfff439SDoug Anderson rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk)); 1118cbfff439SDoug Anderson 1119cbfff439SDoug Anderson return 0; 1120cbfff439SDoug Anderson } 1121cbfff439SDoug Anderson 1122c41aa3ceSMax Schwarz static u32 rk3x_i2c_func(struct i2c_adapter *adap) 1123c41aa3ceSMax Schwarz { 1124c41aa3ceSMax Schwarz return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; 1125c41aa3ceSMax Schwarz } 1126c41aa3ceSMax Schwarz 1127c41aa3ceSMax Schwarz static const struct i2c_algorithm rk3x_i2c_algorithm = { 1128c41aa3ceSMax Schwarz .master_xfer = rk3x_i2c_xfer, 1129c41aa3ceSMax Schwarz .functionality = rk3x_i2c_func, 1130c41aa3ceSMax Schwarz }; 1131c41aa3ceSMax Schwarz 11320dbb9634SAndy Yan static const struct rk3x_i2c_soc_data rv1108_soc_data = { 11330dbb9634SAndy Yan .grf_offset = -1, 11340dbb9634SAndy Yan .calc_timings = rk3x_i2c_v1_calc_timings, 11350dbb9634SAndy Yan }; 11360dbb9634SAndy Yan 1137bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3066_soc_data = { 1138bef358c4SDavid Wu .grf_offset = 0x154, 11397e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings, 1140bef358c4SDavid Wu }; 1141bef358c4SDavid Wu 1142bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3188_soc_data = { 1143bef358c4SDavid Wu .grf_offset = 0x0a4, 11447e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings, 1145bef358c4SDavid Wu }; 1146bef358c4SDavid Wu 1147bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3228_soc_data = { 1148bef358c4SDavid Wu .grf_offset = -1, 11497e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings, 1150bef358c4SDavid Wu }; 1151bef358c4SDavid Wu 1152bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3288_soc_data = { 1153bef358c4SDavid Wu .grf_offset = -1, 11547e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings, 11557e086c3fSDavid Wu }; 11567e086c3fSDavid Wu 11577e086c3fSDavid Wu static const struct rk3x_i2c_soc_data rk3399_soc_data = { 11587e086c3fSDavid Wu .grf_offset = -1, 11597e086c3fSDavid Wu .calc_timings = rk3x_i2c_v1_calc_timings, 1160c41aa3ceSMax Schwarz }; 1161c41aa3ceSMax Schwarz 1162c41aa3ceSMax Schwarz static const struct of_device_id rk3x_i2c_match[] = { 1163bef358c4SDavid Wu { 11640dbb9634SAndy Yan .compatible = "rockchip,rv1108-i2c", 1165d032a2ebSJulia Lawall .data = &rv1108_soc_data 11660dbb9634SAndy Yan }, 11670dbb9634SAndy Yan { 1168bef358c4SDavid Wu .compatible = "rockchip,rk3066-i2c", 1169d032a2ebSJulia Lawall .data = &rk3066_soc_data 1170bef358c4SDavid Wu }, 1171bef358c4SDavid Wu { 1172bef358c4SDavid Wu .compatible = "rockchip,rk3188-i2c", 1173d032a2ebSJulia Lawall .data = &rk3188_soc_data 1174bef358c4SDavid Wu }, 1175bef358c4SDavid Wu { 1176bef358c4SDavid Wu .compatible = "rockchip,rk3228-i2c", 1177d032a2ebSJulia Lawall .data = &rk3228_soc_data 1178bef358c4SDavid Wu }, 1179bef358c4SDavid Wu { 1180bef358c4SDavid Wu .compatible = "rockchip,rk3288-i2c", 1181d032a2ebSJulia Lawall .data = &rk3288_soc_data 1182bef358c4SDavid Wu }, 11837e086c3fSDavid Wu { 11847e086c3fSDavid Wu .compatible = "rockchip,rk3399-i2c", 1185d032a2ebSJulia Lawall .data = &rk3399_soc_data 11867e086c3fSDavid Wu }, 1187c51bd6acSDan Carpenter {}, 1188c41aa3ceSMax Schwarz }; 1189598cf161SLuis de Bethencourt MODULE_DEVICE_TABLE(of, rk3x_i2c_match); 1190c41aa3ceSMax Schwarz 1191c41aa3ceSMax Schwarz static int rk3x_i2c_probe(struct platform_device *pdev) 1192c41aa3ceSMax Schwarz { 1193c41aa3ceSMax Schwarz struct device_node *np = pdev->dev.of_node; 1194c41aa3ceSMax Schwarz const struct of_device_id *match; 1195c41aa3ceSMax Schwarz struct rk3x_i2c *i2c; 1196c41aa3ceSMax Schwarz int ret = 0; 1197c41aa3ceSMax Schwarz int bus_nr; 1198c41aa3ceSMax Schwarz u32 value; 1199c41aa3ceSMax Schwarz int irq; 1200249051f4SMax Schwarz unsigned long clk_rate; 1201c41aa3ceSMax Schwarz 1202c41aa3ceSMax Schwarz i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL); 1203c41aa3ceSMax Schwarz if (!i2c) 1204c41aa3ceSMax Schwarz return -ENOMEM; 1205c41aa3ceSMax Schwarz 1206c41aa3ceSMax Schwarz match = of_match_node(rk3x_i2c_match, np); 1207d032a2ebSJulia Lawall i2c->soc_data = match->data; 1208c41aa3ceSMax Schwarz 12091ab92956SDavid Wu /* use common interface to get I2C timing properties */ 12101ab92956SDavid Wu i2c_parse_fw_timings(&pdev->dev, &i2c->t, true); 12111330e291Saddy ke 1212c41aa3ceSMax Schwarz strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name)); 1213c41aa3ceSMax Schwarz i2c->adap.owner = THIS_MODULE; 1214c41aa3ceSMax Schwarz i2c->adap.algo = &rk3x_i2c_algorithm; 1215c41aa3ceSMax Schwarz i2c->adap.retries = 3; 1216c41aa3ceSMax Schwarz i2c->adap.dev.of_node = np; 1217c41aa3ceSMax Schwarz i2c->adap.algo_data = i2c; 1218c41aa3ceSMax Schwarz i2c->adap.dev.parent = &pdev->dev; 1219c41aa3ceSMax Schwarz 1220c41aa3ceSMax Schwarz i2c->dev = &pdev->dev; 1221c41aa3ceSMax Schwarz 1222c41aa3ceSMax Schwarz spin_lock_init(&i2c->lock); 1223c41aa3ceSMax Schwarz init_waitqueue_head(&i2c->wait); 1224c41aa3ceSMax Schwarz 1225*e0442d76SDejin Zheng i2c->regs = devm_platform_ioremap_resource(pdev, 0); 1226c41aa3ceSMax Schwarz if (IS_ERR(i2c->regs)) 1227c41aa3ceSMax Schwarz return PTR_ERR(i2c->regs); 1228c41aa3ceSMax Schwarz 1229c41aa3ceSMax Schwarz /* Try to set the I2C adapter number from dt */ 1230c41aa3ceSMax Schwarz bus_nr = of_alias_get_id(np, "i2c"); 1231c41aa3ceSMax Schwarz 1232c41aa3ceSMax Schwarz /* 1233c41aa3ceSMax Schwarz * Switch to new interface if the SoC also offers the old one. 1234c41aa3ceSMax Schwarz * The control bit is located in the GRF register space. 1235c41aa3ceSMax Schwarz */ 1236c41aa3ceSMax Schwarz if (i2c->soc_data->grf_offset >= 0) { 1237c41aa3ceSMax Schwarz struct regmap *grf; 1238c41aa3ceSMax Schwarz 1239c41aa3ceSMax Schwarz grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); 1240c41aa3ceSMax Schwarz if (IS_ERR(grf)) { 1241c41aa3ceSMax Schwarz dev_err(&pdev->dev, 1242c41aa3ceSMax Schwarz "rk3x-i2c needs 'rockchip,grf' property\n"); 1243c41aa3ceSMax Schwarz return PTR_ERR(grf); 1244c41aa3ceSMax Schwarz } 1245c41aa3ceSMax Schwarz 1246c41aa3ceSMax Schwarz if (bus_nr < 0) { 1247c41aa3ceSMax Schwarz dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias"); 1248c41aa3ceSMax Schwarz return -EINVAL; 1249c41aa3ceSMax Schwarz } 1250c41aa3ceSMax Schwarz 1251c41aa3ceSMax Schwarz /* 27+i: write mask, 11+i: value */ 1252c41aa3ceSMax Schwarz value = BIT(27 + bus_nr) | BIT(11 + bus_nr); 1253c41aa3ceSMax Schwarz 1254c41aa3ceSMax Schwarz ret = regmap_write(grf, i2c->soc_data->grf_offset, value); 1255c41aa3ceSMax Schwarz if (ret != 0) { 1256c41aa3ceSMax Schwarz dev_err(i2c->dev, "Could not write to GRF: %d\n", ret); 1257c41aa3ceSMax Schwarz return ret; 1258c41aa3ceSMax Schwarz } 1259c41aa3ceSMax Schwarz } 1260c41aa3ceSMax Schwarz 1261c41aa3ceSMax Schwarz /* IRQ setup */ 1262c41aa3ceSMax Schwarz irq = platform_get_irq(pdev, 0); 1263c41aa3ceSMax Schwarz if (irq < 0) { 1264c41aa3ceSMax Schwarz dev_err(&pdev->dev, "cannot find rk3x IRQ\n"); 1265c41aa3ceSMax Schwarz return irq; 1266c41aa3ceSMax Schwarz } 1267c41aa3ceSMax Schwarz 1268c41aa3ceSMax Schwarz ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq, 1269c41aa3ceSMax Schwarz 0, dev_name(&pdev->dev), i2c); 1270c41aa3ceSMax Schwarz if (ret < 0) { 1271c41aa3ceSMax Schwarz dev_err(&pdev->dev, "cannot request IRQ\n"); 1272c41aa3ceSMax Schwarz return ret; 1273c41aa3ceSMax Schwarz } 1274c41aa3ceSMax Schwarz 1275c41aa3ceSMax Schwarz platform_set_drvdata(pdev, i2c); 1276c41aa3ceSMax Schwarz 12777e086c3fSDavid Wu if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) { 12787e086c3fSDavid Wu /* Only one clock to use for bus clock and peripheral clock */ 12797e086c3fSDavid Wu i2c->clk = devm_clk_get(&pdev->dev, NULL); 12807e086c3fSDavid Wu i2c->pclk = i2c->clk; 12817e086c3fSDavid Wu } else { 12827e086c3fSDavid Wu i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 12837e086c3fSDavid Wu i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); 12847e086c3fSDavid Wu } 12857e086c3fSDavid Wu 12867e086c3fSDavid Wu if (IS_ERR(i2c->clk)) { 12877e086c3fSDavid Wu ret = PTR_ERR(i2c->clk); 12887e086c3fSDavid Wu if (ret != -EPROBE_DEFER) 12897e086c3fSDavid Wu dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret); 12907e086c3fSDavid Wu return ret; 12917e086c3fSDavid Wu } 12927e086c3fSDavid Wu if (IS_ERR(i2c->pclk)) { 12937e086c3fSDavid Wu ret = PTR_ERR(i2c->pclk); 12947e086c3fSDavid Wu if (ret != -EPROBE_DEFER) 12957e086c3fSDavid Wu dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret); 12967e086c3fSDavid Wu return ret; 12977e086c3fSDavid Wu } 12987e086c3fSDavid Wu 1299c41aa3ceSMax Schwarz ret = clk_prepare(i2c->clk); 1300c41aa3ceSMax Schwarz if (ret < 0) { 13017e086c3fSDavid Wu dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret); 1302c41aa3ceSMax Schwarz return ret; 1303c41aa3ceSMax Schwarz } 13047e086c3fSDavid Wu ret = clk_prepare(i2c->pclk); 13057e086c3fSDavid Wu if (ret < 0) { 13067e086c3fSDavid Wu dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret); 13077e086c3fSDavid Wu goto err_clk; 13087e086c3fSDavid Wu } 1309c41aa3ceSMax Schwarz 1310249051f4SMax Schwarz i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb; 1311249051f4SMax Schwarz ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb); 1312249051f4SMax Schwarz if (ret != 0) { 1313249051f4SMax Schwarz dev_err(&pdev->dev, "Unable to register clock notifier\n"); 13147e086c3fSDavid Wu goto err_pclk; 1315249051f4SMax Schwarz } 1316249051f4SMax Schwarz 1317249051f4SMax Schwarz clk_rate = clk_get_rate(i2c->clk); 1318249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, clk_rate); 1319249051f4SMax Schwarz 1320c41aa3ceSMax Schwarz ret = i2c_add_adapter(&i2c->adap); 1321ea734404SWolfram Sang if (ret < 0) 1322249051f4SMax Schwarz goto err_clk_notifier; 1323c41aa3ceSMax Schwarz 1324c41aa3ceSMax Schwarz return 0; 1325c41aa3ceSMax Schwarz 1326249051f4SMax Schwarz err_clk_notifier: 1327249051f4SMax Schwarz clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); 13287e086c3fSDavid Wu err_pclk: 13297e086c3fSDavid Wu clk_unprepare(i2c->pclk); 1330c41aa3ceSMax Schwarz err_clk: 1331c41aa3ceSMax Schwarz clk_unprepare(i2c->clk); 1332c41aa3ceSMax Schwarz return ret; 1333c41aa3ceSMax Schwarz } 1334c41aa3ceSMax Schwarz 1335c41aa3ceSMax Schwarz static int rk3x_i2c_remove(struct platform_device *pdev) 1336c41aa3ceSMax Schwarz { 1337c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = platform_get_drvdata(pdev); 1338c41aa3ceSMax Schwarz 1339c41aa3ceSMax Schwarz i2c_del_adapter(&i2c->adap); 1340249051f4SMax Schwarz 1341249051f4SMax Schwarz clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); 13427e086c3fSDavid Wu clk_unprepare(i2c->pclk); 1343c41aa3ceSMax Schwarz clk_unprepare(i2c->clk); 1344c41aa3ceSMax Schwarz 1345c41aa3ceSMax Schwarz return 0; 1346c41aa3ceSMax Schwarz } 1347c41aa3ceSMax Schwarz 1348cbfff439SDoug Anderson static SIMPLE_DEV_PM_OPS(rk3x_i2c_pm_ops, NULL, rk3x_i2c_resume); 1349cbfff439SDoug Anderson 1350c41aa3ceSMax Schwarz static struct platform_driver rk3x_i2c_driver = { 1351c41aa3ceSMax Schwarz .probe = rk3x_i2c_probe, 1352c41aa3ceSMax Schwarz .remove = rk3x_i2c_remove, 1353c41aa3ceSMax Schwarz .driver = { 1354c41aa3ceSMax Schwarz .name = "rk3x-i2c", 1355c41aa3ceSMax Schwarz .of_match_table = rk3x_i2c_match, 1356cbfff439SDoug Anderson .pm = &rk3x_i2c_pm_ops, 1357c41aa3ceSMax Schwarz }, 1358c41aa3ceSMax Schwarz }; 1359c41aa3ceSMax Schwarz 1360c41aa3ceSMax Schwarz module_platform_driver(rk3x_i2c_driver); 1361c41aa3ceSMax Schwarz 1362c41aa3ceSMax Schwarz MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver"); 1363c41aa3ceSMax Schwarz MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>"); 1364c41aa3ceSMax Schwarz MODULE_LICENSE("GPL v2"); 1365