Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tzachi Perelstein | a083279 | 2007-11-12 19:38:51 +0200 | [diff] [blame] | 2 | * Driver for the i2c controller on the Marvell line of host bridges |
| 3 | * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Author: Mark A. Greer <mgreer@mvista.com> |
| 6 | * |
| 7 | * 2005 (c) MontaVista, Software, Inc. This file is licensed under |
| 8 | * the terms of the GNU General Public License version 2. This program |
| 9 | * is licensed "as is" without any warranty of any kind, whether express |
| 10 | * or implied. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/interrupt.h> |
Tzachi Perelstein | a083279 | 2007-11-12 19:38:51 +0200 | [diff] [blame] | 18 | #include <linux/mv643xx_i2c.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 20 | #include <linux/reset.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 22 | #include <linux/of.h> |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 23 | #include <linux/of_device.h> |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 24 | #include <linux/of_irq.h> |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 25 | #include <linux/clk.h> |
| 26 | #include <linux/err.h> |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 27 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame] | 29 | #define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1) |
| 30 | #define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7) |
| 31 | #define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3) |
| 32 | |
Thomas Petazzoni | 1259869 | 2014-12-11 17:33:45 +0100 | [diff] [blame] | 33 | #define MV64XXX_I2C_REG_CONTROL_ACK BIT(2) |
| 34 | #define MV64XXX_I2C_REG_CONTROL_IFLG BIT(3) |
| 35 | #define MV64XXX_I2C_REG_CONTROL_STOP BIT(4) |
| 36 | #define MV64XXX_I2C_REG_CONTROL_START BIT(5) |
| 37 | #define MV64XXX_I2C_REG_CONTROL_TWSIEN BIT(6) |
| 38 | #define MV64XXX_I2C_REG_CONTROL_INTEN BIT(7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* Ctlr status values */ |
| 41 | #define MV64XXX_I2C_STATUS_BUS_ERR 0x00 |
| 42 | #define MV64XXX_I2C_STATUS_MAST_START 0x08 |
| 43 | #define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10 |
| 44 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18 |
| 45 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20 |
| 46 | #define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28 |
| 47 | #define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30 |
| 48 | #define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38 |
| 49 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40 |
| 50 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48 |
| 51 | #define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50 |
| 52 | #define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58 |
| 53 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0 |
| 54 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8 |
| 55 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0 |
| 56 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8 |
| 57 | #define MV64XXX_I2C_STATUS_NO_STATUS 0xf8 |
| 58 | |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 59 | /* Register defines (I2C bridge) */ |
| 60 | #define MV64XXX_I2C_REG_TX_DATA_LO 0xc0 |
| 61 | #define MV64XXX_I2C_REG_TX_DATA_HI 0xc4 |
| 62 | #define MV64XXX_I2C_REG_RX_DATA_LO 0xc8 |
| 63 | #define MV64XXX_I2C_REG_RX_DATA_HI 0xcc |
| 64 | #define MV64XXX_I2C_REG_BRIDGE_CONTROL 0xd0 |
| 65 | #define MV64XXX_I2C_REG_BRIDGE_STATUS 0xd4 |
| 66 | #define MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE 0xd8 |
| 67 | #define MV64XXX_I2C_REG_BRIDGE_INTR_MASK 0xdC |
| 68 | #define MV64XXX_I2C_REG_BRIDGE_TIMING 0xe0 |
| 69 | |
| 70 | /* Bridge Control values */ |
Thomas Petazzoni | 1259869 | 2014-12-11 17:33:45 +0100 | [diff] [blame] | 71 | #define MV64XXX_I2C_BRIDGE_CONTROL_WR BIT(0) |
| 72 | #define MV64XXX_I2C_BRIDGE_CONTROL_RD BIT(1) |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 73 | #define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT 2 |
Thomas Petazzoni | 1259869 | 2014-12-11 17:33:45 +0100 | [diff] [blame] | 74 | #define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT BIT(12) |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 75 | #define MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT 13 |
| 76 | #define MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT 16 |
Thomas Petazzoni | 1259869 | 2014-12-11 17:33:45 +0100 | [diff] [blame] | 77 | #define MV64XXX_I2C_BRIDGE_CONTROL_ENABLE BIT(19) |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 78 | #define MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START BIT(20) |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 79 | |
| 80 | /* Bridge Status values */ |
Thomas Petazzoni | 1259869 | 2014-12-11 17:33:45 +0100 | [diff] [blame] | 81 | #define MV64XXX_I2C_BRIDGE_STATUS_ERROR BIT(0) |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* Driver states */ |
| 84 | enum { |
| 85 | MV64XXX_I2C_STATE_INVALID, |
| 86 | MV64XXX_I2C_STATE_IDLE, |
| 87 | MV64XXX_I2C_STATE_WAITING_FOR_START_COND, |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 88 | MV64XXX_I2C_STATE_WAITING_FOR_RESTART, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK, |
| 90 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK, |
| 91 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK, |
| 92 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* Driver actions */ |
| 96 | enum { |
| 97 | MV64XXX_I2C_ACTION_INVALID, |
| 98 | MV64XXX_I2C_ACTION_CONTINUE, |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 99 | MV64XXX_I2C_ACTION_SEND_RESTART, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | MV64XXX_I2C_ACTION_SEND_ADDR_1, |
| 101 | MV64XXX_I2C_ACTION_SEND_ADDR_2, |
| 102 | MV64XXX_I2C_ACTION_SEND_DATA, |
| 103 | MV64XXX_I2C_ACTION_RCV_DATA, |
| 104 | MV64XXX_I2C_ACTION_RCV_DATA_STOP, |
| 105 | MV64XXX_I2C_ACTION_SEND_STOP, |
| 106 | }; |
| 107 | |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 108 | struct mv64xxx_i2c_regs { |
| 109 | u8 addr; |
| 110 | u8 ext_addr; |
| 111 | u8 data; |
| 112 | u8 control; |
| 113 | u8 status; |
| 114 | u8 clock; |
| 115 | u8 soft_reset; |
| 116 | }; |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | struct mv64xxx_i2c_data { |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 119 | struct i2c_msg *msgs; |
| 120 | int num_msgs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | int irq; |
| 122 | u32 state; |
| 123 | u32 action; |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 124 | u32 aborting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | u32 cntl_bits; |
| 126 | void __iomem *reg_base; |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 127 | struct mv64xxx_i2c_regs reg_offsets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | u32 addr1; |
| 129 | u32 addr2; |
| 130 | u32 bytes_left; |
| 131 | u32 byte_posn; |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 132 | u32 send_stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | u32 block; |
| 134 | int rc; |
| 135 | u32 freq_m; |
| 136 | u32 freq_n; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 137 | #if defined(CONFIG_HAVE_CLK) |
| 138 | struct clk *clk; |
| 139 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | wait_queue_head_t waitq; |
| 141 | spinlock_t lock; |
| 142 | struct i2c_msg *msg; |
| 143 | struct i2c_adapter adapter; |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 144 | bool offload_enabled; |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 145 | /* 5us delay in order to avoid repeated start timing violation */ |
| 146 | bool errata_delay; |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 147 | struct reset_control *rstc; |
Maxime Ripard | c7dcb1f | 2014-03-04 17:28:38 +0100 | [diff] [blame] | 148 | bool irq_clear_inverted; |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 149 | /* Clk div is 2 to the power n, not 2 to the power n + 1 */ |
| 150 | bool clk_n_base_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 153 | static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = { |
| 154 | .addr = 0x00, |
| 155 | .ext_addr = 0x10, |
| 156 | .data = 0x04, |
| 157 | .control = 0x08, |
| 158 | .status = 0x0c, |
| 159 | .clock = 0x0c, |
| 160 | .soft_reset = 0x1c, |
| 161 | }; |
| 162 | |
Maxime Ripard | 3d66ac7 | 2013-06-12 18:53:32 +0200 | [diff] [blame] | 163 | static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = { |
| 164 | .addr = 0x00, |
| 165 | .ext_addr = 0x04, |
| 166 | .data = 0x08, |
| 167 | .control = 0x0c, |
| 168 | .status = 0x10, |
| 169 | .clock = 0x14, |
| 170 | .soft_reset = 0x18, |
| 171 | }; |
| 172 | |
Russell King | 3420afb | 2013-05-16 21:38:11 +0100 | [diff] [blame] | 173 | static void |
| 174 | mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data, |
| 175 | struct i2c_msg *msg) |
| 176 | { |
| 177 | u32 dir = 0; |
| 178 | |
Russell King | 3420afb | 2013-05-16 21:38:11 +0100 | [diff] [blame] | 179 | drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK | |
| 180 | MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN; |
| 181 | |
| 182 | if (msg->flags & I2C_M_RD) |
| 183 | dir = 1; |
| 184 | |
| 185 | if (msg->flags & I2C_M_TEN) { |
| 186 | drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir; |
| 187 | drv_data->addr2 = (u32)msg->addr & 0xff; |
| 188 | } else { |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame] | 189 | drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir; |
Russell King | 3420afb | 2013-05-16 21:38:11 +0100 | [diff] [blame] | 190 | drv_data->addr2 = 0; |
| 191 | } |
| 192 | } |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* |
| 195 | ***************************************************************************** |
| 196 | * |
| 197 | * Finite State Machine & Interrupt Routines |
| 198 | * |
| 199 | ***************************************************************************** |
| 200 | */ |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 201 | |
| 202 | /* Reset hardware and initialize FSM */ |
| 203 | static void |
| 204 | mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data) |
| 205 | { |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 206 | if (drv_data->offload_enabled) { |
| 207 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL); |
| 208 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_TIMING); |
| 209 | writel(0, drv_data->reg_base + |
| 210 | MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE); |
| 211 | writel(0, drv_data->reg_base + |
| 212 | MV64XXX_I2C_REG_BRIDGE_INTR_MASK); |
| 213 | } |
| 214 | |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 215 | writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset); |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame] | 216 | writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n), |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 217 | drv_data->reg_base + drv_data->reg_offsets.clock); |
| 218 | writel(0, drv_data->reg_base + drv_data->reg_offsets.addr); |
| 219 | writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr); |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 220 | writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 221 | drv_data->reg_base + drv_data->reg_offsets.control); |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 222 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 223 | } |
| 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | static void |
| 226 | mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status) |
| 227 | { |
| 228 | /* |
| 229 | * If state is idle, then this is likely the remnants of an old |
| 230 | * operation that driver has given up on or the user has killed. |
| 231 | * If so, issue the stop condition and go to idle. |
| 232 | */ |
| 233 | if (drv_data->state == MV64XXX_I2C_STATE_IDLE) { |
| 234 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 235 | return; |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* The status from the ctlr [mostly] tells us what to do next */ |
| 239 | switch (status) { |
| 240 | /* Start condition interrupt */ |
| 241 | case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */ |
| 242 | case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */ |
| 243 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1; |
| 244 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK; |
| 245 | break; |
| 246 | |
| 247 | /* Performing a write */ |
| 248 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */ |
| 249 | if (drv_data->msg->flags & I2C_M_TEN) { |
| 250 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2; |
| 251 | drv_data->state = |
| 252 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK; |
| 253 | break; |
| 254 | } |
| 255 | /* FALLTHRU */ |
| 256 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */ |
| 257 | case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */ |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 258 | if ((drv_data->bytes_left == 0) |
| 259 | || (drv_data->aborting |
| 260 | && (drv_data->byte_posn != 0))) { |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 261 | if (drv_data->send_stop || drv_data->aborting) { |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 262 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 263 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 264 | } else { |
| 265 | drv_data->action = |
| 266 | MV64XXX_I2C_ACTION_SEND_RESTART; |
| 267 | drv_data->state = |
| 268 | MV64XXX_I2C_STATE_WAITING_FOR_RESTART; |
| 269 | } |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 270 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA; |
| 272 | drv_data->state = |
| 273 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK; |
| 274 | drv_data->bytes_left--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | break; |
| 277 | |
| 278 | /* Performing a read */ |
| 279 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */ |
| 280 | if (drv_data->msg->flags & I2C_M_TEN) { |
| 281 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2; |
| 282 | drv_data->state = |
| 283 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK; |
| 284 | break; |
| 285 | } |
| 286 | /* FALLTHRU */ |
| 287 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */ |
| 288 | if (drv_data->bytes_left == 0) { |
| 289 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 290 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 291 | break; |
| 292 | } |
| 293 | /* FALLTHRU */ |
| 294 | case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */ |
| 295 | if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK) |
| 296 | drv_data->action = MV64XXX_I2C_ACTION_CONTINUE; |
| 297 | else { |
| 298 | drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA; |
| 299 | drv_data->bytes_left--; |
| 300 | } |
| 301 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA; |
| 302 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 303 | if ((drv_data->bytes_left == 1) || drv_data->aborting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK; |
| 305 | break; |
| 306 | |
| 307 | case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */ |
| 308 | drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP; |
| 309 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 310 | break; |
| 311 | |
| 312 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */ |
| 313 | case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */ |
| 314 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */ |
| 315 | /* Doesn't seem to be a device at other end */ |
| 316 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 317 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
Guenter Roeck | 6faa353 | 2013-06-19 14:53:52 -0700 | [diff] [blame] | 318 | drv_data->rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | break; |
| 320 | |
| 321 | default: |
| 322 | dev_err(&drv_data->adapter.dev, |
| 323 | "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, " |
| 324 | "status: 0x%x, addr: 0x%x, flags: 0x%x\n", |
| 325 | drv_data->state, status, drv_data->msg->addr, |
| 326 | drv_data->msg->flags); |
| 327 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 328 | mv64xxx_i2c_hw_init(drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | drv_data->rc = -EIO; |
| 330 | } |
| 331 | } |
| 332 | |
Wolfram Sang | 4c5b38e | 2014-02-13 21:36:31 +0100 | [diff] [blame] | 333 | static void mv64xxx_i2c_send_start(struct mv64xxx_i2c_data *drv_data) |
| 334 | { |
Wolfram Sang | 485ecdf | 2014-02-13 21:36:33 +0100 | [diff] [blame] | 335 | drv_data->msg = drv_data->msgs; |
| 336 | drv_data->byte_posn = 0; |
| 337 | drv_data->bytes_left = drv_data->msg->len; |
| 338 | drv_data->aborting = 0; |
| 339 | drv_data->rc = 0; |
| 340 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 341 | mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs); |
| 342 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START, |
| 343 | drv_data->reg_base + drv_data->reg_offsets.control); |
Wolfram Sang | 4c5b38e | 2014-02-13 21:36:31 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | static void |
| 347 | mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data) |
| 348 | { |
| 349 | switch(drv_data->action) { |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 350 | case MV64XXX_I2C_ACTION_SEND_RESTART: |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 351 | /* We should only get here if we have further messages */ |
| 352 | BUG_ON(drv_data->num_msgs == 0); |
| 353 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 354 | drv_data->msgs++; |
| 355 | drv_data->num_msgs--; |
Wolfram Sang | 4c5b38e | 2014-02-13 21:36:31 +0100 | [diff] [blame] | 356 | mv64xxx_i2c_send_start(drv_data); |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 357 | |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 358 | if (drv_data->errata_delay) |
| 359 | udelay(5); |
| 360 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 361 | /* |
| 362 | * We're never at the start of the message here, and by this |
| 363 | * time it's already too late to do any protocol mangling. |
| 364 | * Thankfully, do not advertise support for that feature. |
| 365 | */ |
| 366 | drv_data->send_stop = drv_data->num_msgs == 1; |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 367 | break; |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | case MV64XXX_I2C_ACTION_CONTINUE: |
| 370 | writel(drv_data->cntl_bits, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 371 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | break; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | case MV64XXX_I2C_ACTION_SEND_ADDR_1: |
| 375 | writel(drv_data->addr1, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 376 | drv_data->reg_base + drv_data->reg_offsets.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | writel(drv_data->cntl_bits, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 378 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case MV64XXX_I2C_ACTION_SEND_ADDR_2: |
| 382 | writel(drv_data->addr2, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 383 | drv_data->reg_base + drv_data->reg_offsets.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | writel(drv_data->cntl_bits, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 385 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case MV64XXX_I2C_ACTION_SEND_DATA: |
| 389 | writel(drv_data->msg->buf[drv_data->byte_posn++], |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 390 | drv_data->reg_base + drv_data->reg_offsets.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | writel(drv_data->cntl_bits, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 392 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | case MV64XXX_I2C_ACTION_RCV_DATA: |
| 396 | drv_data->msg->buf[drv_data->byte_posn++] = |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 397 | readl(drv_data->reg_base + drv_data->reg_offsets.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | writel(drv_data->cntl_bits, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 399 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case MV64XXX_I2C_ACTION_RCV_DATA_STOP: |
| 403 | drv_data->msg->buf[drv_data->byte_posn++] = |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 404 | readl(drv_data->reg_base + drv_data->reg_offsets.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN; |
| 406 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 407 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | drv_data->block = 0; |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 409 | if (drv_data->errata_delay) |
| 410 | udelay(5); |
| 411 | |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 412 | wake_up(&drv_data->waitq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | break; |
| 414 | |
| 415 | case MV64XXX_I2C_ACTION_INVALID: |
| 416 | default: |
| 417 | dev_err(&drv_data->adapter.dev, |
| 418 | "mv64xxx_i2c_do_action: Invalid action: %d\n", |
| 419 | drv_data->action); |
| 420 | drv_data->rc = -EIO; |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | /* FALLTHRU */ |
| 423 | case MV64XXX_I2C_ACTION_SEND_STOP: |
| 424 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN; |
| 425 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 426 | drv_data->reg_base + drv_data->reg_offsets.control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | drv_data->block = 0; |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 428 | wake_up(&drv_data->waitq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 433 | static void |
| 434 | mv64xxx_i2c_read_offload_rx_data(struct mv64xxx_i2c_data *drv_data, |
| 435 | struct i2c_msg *msg) |
| 436 | { |
| 437 | u32 buf[2]; |
| 438 | |
| 439 | buf[0] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_LO); |
| 440 | buf[1] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_HI); |
| 441 | |
| 442 | memcpy(msg->buf, buf, msg->len); |
| 443 | } |
| 444 | |
| 445 | static int |
| 446 | mv64xxx_i2c_intr_offload(struct mv64xxx_i2c_data *drv_data) |
| 447 | { |
| 448 | u32 cause, status; |
| 449 | |
| 450 | cause = readl(drv_data->reg_base + |
| 451 | MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE); |
| 452 | if (!cause) |
| 453 | return IRQ_NONE; |
| 454 | |
| 455 | status = readl(drv_data->reg_base + |
| 456 | MV64XXX_I2C_REG_BRIDGE_STATUS); |
| 457 | |
| 458 | if (status & MV64XXX_I2C_BRIDGE_STATUS_ERROR) { |
| 459 | drv_data->rc = -EIO; |
| 460 | goto out; |
| 461 | } |
| 462 | |
| 463 | drv_data->rc = 0; |
| 464 | |
| 465 | /* |
| 466 | * Transaction is a one message read transaction, read data |
| 467 | * for this message. |
| 468 | */ |
| 469 | if (drv_data->num_msgs == 1 && drv_data->msgs[0].flags & I2C_M_RD) { |
| 470 | mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs); |
| 471 | drv_data->msgs++; |
| 472 | drv_data->num_msgs--; |
| 473 | } |
| 474 | /* |
| 475 | * Transaction is a two messages write/read transaction, read |
| 476 | * data for the second (read) message. |
| 477 | */ |
| 478 | else if (drv_data->num_msgs == 2 && |
| 479 | !(drv_data->msgs[0].flags & I2C_M_RD) && |
| 480 | drv_data->msgs[1].flags & I2C_M_RD) { |
| 481 | mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs + 1); |
| 482 | drv_data->msgs += 2; |
| 483 | drv_data->num_msgs -= 2; |
| 484 | } |
| 485 | |
| 486 | out: |
| 487 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL); |
| 488 | writel(0, drv_data->reg_base + |
| 489 | MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE); |
| 490 | drv_data->block = 0; |
| 491 | |
| 492 | wake_up(&drv_data->waitq); |
| 493 | |
| 494 | return IRQ_HANDLED; |
| 495 | } |
| 496 | |
Mikael Pettersson | b0999cc | 2009-09-07 12:00:13 +0200 | [diff] [blame] | 497 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 498 | mv64xxx_i2c_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | struct mv64xxx_i2c_data *drv_data = dev_id; |
| 501 | unsigned long flags; |
| 502 | u32 status; |
Mikael Pettersson | b0999cc | 2009-09-07 12:00:13 +0200 | [diff] [blame] | 503 | irqreturn_t rc = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | spin_lock_irqsave(&drv_data->lock, flags); |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 506 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 507 | if (drv_data->offload_enabled) |
| 508 | rc = mv64xxx_i2c_intr_offload(drv_data); |
| 509 | |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 510 | while (readl(drv_data->reg_base + drv_data->reg_offsets.control) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | MV64XXX_I2C_REG_CONTROL_IFLG) { |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 512 | status = readl(drv_data->reg_base + drv_data->reg_offsets.status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | mv64xxx_i2c_fsm(drv_data, status); |
| 514 | mv64xxx_i2c_do_action(drv_data); |
Maxime Ripard | c7dcb1f | 2014-03-04 17:28:38 +0100 | [diff] [blame] | 515 | |
| 516 | if (drv_data->irq_clear_inverted) |
| 517 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_IFLG, |
| 518 | drv_data->reg_base + drv_data->reg_offsets.control); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | rc = IRQ_HANDLED; |
| 521 | } |
| 522 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 523 | |
| 524 | return rc; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | ***************************************************************************** |
| 529 | * |
| 530 | * I2C Msg Execution Routines |
| 531 | * |
| 532 | ***************************************************************************** |
| 533 | */ |
| 534 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data) |
| 536 | { |
| 537 | long time_left; |
| 538 | unsigned long flags; |
| 539 | char abort = 0; |
| 540 | |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 541 | time_left = wait_event_timeout(drv_data->waitq, |
Jean Delvare | 8a52c6b | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 542 | !drv_data->block, drv_data->adapter.timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | spin_lock_irqsave(&drv_data->lock, flags); |
| 545 | if (!time_left) { /* Timed out */ |
| 546 | drv_data->rc = -ETIMEDOUT; |
| 547 | abort = 1; |
| 548 | } else if (time_left < 0) { /* Interrupted/Error */ |
| 549 | drv_data->rc = time_left; /* errno value */ |
| 550 | abort = 1; |
| 551 | } |
| 552 | |
| 553 | if (abort && drv_data->block) { |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 554 | drv_data->aborting = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 556 | |
| 557 | time_left = wait_event_timeout(drv_data->waitq, |
Jean Delvare | 8a52c6b | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 558 | !drv_data->block, drv_data->adapter.timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 560 | if ((time_left <= 0) && drv_data->block) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 562 | dev_err(&drv_data->adapter.dev, |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 563 | "mv64xxx: I2C bus locked, block: %d, " |
| 564 | "time_left: %d\n", drv_data->block, |
| 565 | (int)time_left); |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 566 | mv64xxx_i2c_hw_init(drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | } else |
| 569 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 570 | } |
| 571 | |
| 572 | static int |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 573 | mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg, |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 574 | int is_last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
| 576 | unsigned long flags; |
| 577 | |
| 578 | spin_lock_irqsave(&drv_data->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Wolfram Sang | 79970db | 2014-02-13 21:36:29 +0100 | [diff] [blame] | 580 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND; |
| 581 | |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 582 | drv_data->send_stop = is_last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | drv_data->block = 1; |
Wolfram Sang | b0200ab | 2014-02-13 21:36:32 +0100 | [diff] [blame] | 584 | mv64xxx_i2c_send_start(drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 586 | |
| 587 | mv64xxx_i2c_wait_for_completion(drv_data); |
| 588 | return drv_data->rc; |
| 589 | } |
| 590 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 591 | static void |
| 592 | mv64xxx_i2c_prepare_tx(struct mv64xxx_i2c_data *drv_data) |
| 593 | { |
| 594 | struct i2c_msg *msg = drv_data->msgs; |
| 595 | u32 buf[2]; |
| 596 | |
| 597 | memcpy(buf, msg->buf, msg->len); |
| 598 | |
| 599 | writel(buf[0], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO); |
| 600 | writel(buf[1], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI); |
| 601 | } |
| 602 | |
| 603 | static int |
| 604 | mv64xxx_i2c_offload_xfer(struct mv64xxx_i2c_data *drv_data) |
| 605 | { |
| 606 | struct i2c_msg *msgs = drv_data->msgs; |
| 607 | int num = drv_data->num_msgs; |
| 608 | unsigned long ctrl_reg; |
| 609 | unsigned long flags; |
| 610 | |
| 611 | spin_lock_irqsave(&drv_data->lock, flags); |
| 612 | |
| 613 | /* Build transaction */ |
| 614 | ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE | |
| 615 | (msgs[0].addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT); |
| 616 | |
| 617 | if (msgs[0].flags & I2C_M_TEN) |
| 618 | ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT; |
| 619 | |
| 620 | /* Single write message transaction */ |
| 621 | if (num == 1 && !(msgs[0].flags & I2C_M_RD)) { |
| 622 | size_t len = msgs[0].len - 1; |
| 623 | |
| 624 | ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR | |
| 625 | (len << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT); |
| 626 | mv64xxx_i2c_prepare_tx(drv_data); |
| 627 | } |
| 628 | /* Single read message transaction */ |
| 629 | else if (num == 1 && msgs[0].flags & I2C_M_RD) { |
| 630 | size_t len = msgs[0].len - 1; |
| 631 | |
| 632 | ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD | |
| 633 | (len << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT); |
| 634 | } |
| 635 | /* |
| 636 | * Transaction with one write and one read message. This is |
| 637 | * guaranteed by the mv64xx_i2c_can_offload() checks. |
| 638 | */ |
| 639 | else if (num == 2) { |
| 640 | size_t lentx = msgs[0].len - 1; |
| 641 | size_t lenrx = msgs[1].len - 1; |
| 642 | |
| 643 | ctrl_reg |= |
| 644 | MV64XXX_I2C_BRIDGE_CONTROL_RD | |
| 645 | MV64XXX_I2C_BRIDGE_CONTROL_WR | |
| 646 | (lentx << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT) | |
| 647 | (lenrx << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT) | |
| 648 | MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START; |
| 649 | mv64xxx_i2c_prepare_tx(drv_data); |
| 650 | } |
| 651 | |
| 652 | /* Execute transaction */ |
| 653 | drv_data->block = 1; |
| 654 | writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL); |
| 655 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 656 | |
| 657 | mv64xxx_i2c_wait_for_completion(drv_data); |
| 658 | |
| 659 | return drv_data->rc; |
| 660 | } |
| 661 | |
| 662 | static bool |
| 663 | mv64xxx_i2c_valid_offload_sz(struct i2c_msg *msg) |
| 664 | { |
| 665 | return msg->len <= 8 && msg->len >= 1; |
| 666 | } |
| 667 | |
| 668 | static bool |
| 669 | mv64xxx_i2c_can_offload(struct mv64xxx_i2c_data *drv_data) |
| 670 | { |
| 671 | struct i2c_msg *msgs = drv_data->msgs; |
| 672 | int num = drv_data->num_msgs; |
| 673 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 674 | if (!drv_data->offload_enabled) |
| 675 | return false; |
| 676 | |
| 677 | /* |
| 678 | * We can offload a transaction consisting of a single |
| 679 | * message, as long as the message has a length between 1 and |
| 680 | * 8 bytes. |
| 681 | */ |
| 682 | if (num == 1 && mv64xxx_i2c_valid_offload_sz(msgs)) |
| 683 | return true; |
| 684 | |
| 685 | /* |
| 686 | * We can offload a transaction consisting of two messages, if |
| 687 | * the first is a write and a second is a read, and both have |
| 688 | * a length between 1 and 8 bytes. |
| 689 | */ |
| 690 | if (num == 2 && |
| 691 | mv64xxx_i2c_valid_offload_sz(msgs) && |
| 692 | mv64xxx_i2c_valid_offload_sz(msgs + 1) && |
| 693 | !(msgs[0].flags & I2C_M_RD) && |
| 694 | msgs[1].flags & I2C_M_RD) |
| 695 | return true; |
| 696 | |
| 697 | return false; |
| 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | /* |
| 701 | ***************************************************************************** |
| 702 | * |
| 703 | * I2C Core Support Routines (Interface to higher level I2C code) |
| 704 | * |
| 705 | ***************************************************************************** |
| 706 | */ |
| 707 | static u32 |
| 708 | mv64xxx_i2c_functionality(struct i2c_adapter *adap) |
| 709 | { |
| 710 | return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL; |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 715 | { |
| 716 | struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap); |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 717 | int rc, ret = num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 719 | BUG_ON(drv_data->msgs != NULL); |
| 720 | drv_data->msgs = msgs; |
| 721 | drv_data->num_msgs = num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Thomas Petazzoni | 00d8689 | 2014-12-11 17:33:46 +0100 | [diff] [blame] | 723 | if (mv64xxx_i2c_can_offload(drv_data)) |
| 724 | rc = mv64xxx_i2c_offload_xfer(drv_data); |
| 725 | else |
| 726 | rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1); |
| 727 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 728 | if (rc < 0) |
| 729 | ret = rc; |
| 730 | |
| 731 | drv_data->num_msgs = 0; |
| 732 | drv_data->msgs = NULL; |
| 733 | |
| 734 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 737 | static const struct i2c_algorithm mv64xxx_i2c_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | .master_xfer = mv64xxx_i2c_xfer, |
| 739 | .functionality = mv64xxx_i2c_functionality, |
| 740 | }; |
| 741 | |
| 742 | /* |
| 743 | ***************************************************************************** |
| 744 | * |
| 745 | * Driver Interface & Early Init Routines |
| 746 | * |
| 747 | ***************************************************************************** |
| 748 | */ |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 749 | static const struct of_device_id mv64xxx_i2c_of_match_table[] = { |
Maxime Ripard | 5ed9d92 | 2014-03-31 14:54:57 +0200 | [diff] [blame] | 750 | { .compatible = "allwinner,sun4i-a10-i2c", .data = &mv64xxx_i2c_regs_sun4i}, |
Maxime Ripard | c7dcb1f | 2014-03-04 17:28:38 +0100 | [diff] [blame] | 751 | { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i}, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 752 | { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx}, |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 753 | { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx}, |
Gregory CLEMENT | 6cf70ae | 2013-12-31 16:59:33 +0100 | [diff] [blame] | 754 | { .compatible = "marvell,mv78230-a0-i2c", .data = &mv64xxx_i2c_regs_mv64xxx}, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 755 | {} |
| 756 | }; |
| 757 | MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table); |
| 758 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 759 | #ifdef CONFIG_OF |
Thierry Reding | c1a9946 | 2013-09-18 14:50:52 +0200 | [diff] [blame] | 760 | #ifdef CONFIG_HAVE_CLK |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 761 | static int |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 762 | mv64xxx_calc_freq(struct mv64xxx_i2c_data *drv_data, |
| 763 | const int tclk, const int n, const int m) |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 764 | { |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 765 | if (drv_data->clk_n_base_0) |
| 766 | return tclk / (10 * (m + 1) * (1 << n)); |
| 767 | else |
| 768 | return tclk / (10 * (m + 1) * (2 << n)); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 771 | static bool |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 772 | mv64xxx_find_baud_factors(struct mv64xxx_i2c_data *drv_data, |
| 773 | const u32 req_freq, const u32 tclk) |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 774 | { |
| 775 | int freq, delta, best_delta = INT_MAX; |
| 776 | int m, n; |
| 777 | |
| 778 | for (n = 0; n <= 7; n++) |
| 779 | for (m = 0; m <= 15; m++) { |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 780 | freq = mv64xxx_calc_freq(drv_data, tclk, n, m); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 781 | delta = req_freq - freq; |
| 782 | if (delta >= 0 && delta < best_delta) { |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 783 | drv_data->freq_m = m; |
| 784 | drv_data->freq_n = n; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 785 | best_delta = delta; |
| 786 | } |
| 787 | if (best_delta == 0) |
| 788 | return true; |
| 789 | } |
| 790 | if (best_delta == INT_MAX) |
| 791 | return false; |
| 792 | return true; |
| 793 | } |
Thierry Reding | c1a9946 | 2013-09-18 14:50:52 +0200 | [diff] [blame] | 794 | #endif /* CONFIG_HAVE_CLK */ |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 795 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 796 | static int |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 797 | mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 798 | struct device *dev) |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 799 | { |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 800 | /* CLK is mandatory when using DT to describe the i2c bus. We |
| 801 | * need to know tclk in order to calculate bus clock |
| 802 | * factors. |
| 803 | */ |
| 804 | #if !defined(CONFIG_HAVE_CLK) |
| 805 | /* Have OF but no CLK */ |
| 806 | return -ENODEV; |
| 807 | #else |
Thierry Reding | c1a9946 | 2013-09-18 14:50:52 +0200 | [diff] [blame] | 808 | const struct of_device_id *device; |
| 809 | struct device_node *np = dev->of_node; |
| 810 | u32 bus_freq, tclk; |
| 811 | int rc = 0; |
| 812 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 813 | if (IS_ERR(drv_data->clk)) { |
| 814 | rc = -ENODEV; |
| 815 | goto out; |
| 816 | } |
| 817 | tclk = clk_get_rate(drv_data->clk); |
Gregory CLEMENT | 4c730a0 | 2013-06-21 15:32:06 +0200 | [diff] [blame] | 818 | |
Chen-Yu Tsai | 0ce4bc1 | 2014-09-01 22:28:13 +0800 | [diff] [blame] | 819 | if (of_property_read_u32(np, "clock-frequency", &bus_freq)) |
Gregory CLEMENT | 4c730a0 | 2013-06-21 15:32:06 +0200 | [diff] [blame] | 820 | bus_freq = 100000; /* 100kHz by default */ |
| 821 | |
Hans de Goede | bba61f5 | 2015-09-27 16:57:08 +0200 | [diff] [blame] | 822 | if (of_device_is_compatible(np, "allwinner,sun4i-a10-i2c") || |
| 823 | of_device_is_compatible(np, "allwinner,sun6i-a31-i2c")) |
| 824 | drv_data->clk_n_base_0 = true; |
| 825 | |
| 826 | if (!mv64xxx_find_baud_factors(drv_data, bus_freq, tclk)) { |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 827 | rc = -EINVAL; |
| 828 | goto out; |
| 829 | } |
| 830 | drv_data->irq = irq_of_parse_and_map(np, 0); |
| 831 | |
Maxime Ripard | f2a67d0 | 2014-03-10 12:12:10 +0100 | [diff] [blame] | 832 | drv_data->rstc = devm_reset_control_get_optional(dev, NULL); |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 833 | if (IS_ERR(drv_data->rstc)) { |
| 834 | if (PTR_ERR(drv_data->rstc) == -EPROBE_DEFER) { |
| 835 | rc = -EPROBE_DEFER; |
| 836 | goto out; |
| 837 | } |
| 838 | } else { |
| 839 | reset_control_deassert(drv_data->rstc); |
| 840 | } |
| 841 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 842 | /* Its not yet defined how timeouts will be specified in device tree. |
| 843 | * So hard code the value to 1 second. |
| 844 | */ |
| 845 | drv_data->adapter.timeout = HZ; |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 846 | |
| 847 | device = of_match_device(mv64xxx_i2c_of_match_table, dev); |
| 848 | if (!device) |
| 849 | return -ENODEV; |
| 850 | |
| 851 | memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets)); |
| 852 | |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 853 | /* |
| 854 | * For controllers embedded in new SoCs activate the |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 855 | * Transaction Generator support and the errata fix. |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 856 | */ |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 857 | if (of_device_is_compatible(np, "marvell,mv78230-i2c")) { |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 858 | drv_data->offload_enabled = true; |
Gregory CLEMENT | c1d15b6 | 2013-08-22 16:19:06 +0200 | [diff] [blame] | 859 | drv_data->errata_delay = true; |
| 860 | } |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 861 | |
Gregory CLEMENT | 6cf70ae | 2013-12-31 16:59:33 +0100 | [diff] [blame] | 862 | if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) { |
| 863 | drv_data->offload_enabled = false; |
| 864 | drv_data->errata_delay = true; |
| 865 | } |
Maxime Ripard | c7dcb1f | 2014-03-04 17:28:38 +0100 | [diff] [blame] | 866 | |
| 867 | if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c")) |
| 868 | drv_data->irq_clear_inverted = true; |
| 869 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 870 | out: |
| 871 | return rc; |
| 872 | #endif |
| 873 | } |
| 874 | #else /* CONFIG_OF */ |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 875 | static int |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 876 | mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data, |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 877 | struct device *dev) |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 878 | { |
| 879 | return -ENODEV; |
| 880 | } |
| 881 | #endif /* CONFIG_OF */ |
| 882 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 883 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 884 | mv64xxx_i2c_probe(struct platform_device *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | struct mv64xxx_i2c_data *drv_data; |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 887 | struct mv64xxx_i2c_pdata *pdata = dev_get_platdata(&pd->dev); |
Russell King | 16874b0 | 2013-05-16 21:33:09 +0100 | [diff] [blame] | 888 | struct resource *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | int rc; |
| 890 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 891 | if ((!pdata && !pd->dev.of_node)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return -ENODEV; |
| 893 | |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 894 | drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data), |
| 895 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (!drv_data) |
| 897 | return -ENOMEM; |
| 898 | |
Russell King | 16874b0 | 2013-05-16 21:33:09 +0100 | [diff] [blame] | 899 | r = platform_get_resource(pd, IORESOURCE_MEM, 0); |
| 900 | drv_data->reg_base = devm_ioremap_resource(&pd->dev, r); |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 901 | if (IS_ERR(drv_data->reg_base)) |
| 902 | return PTR_ERR(drv_data->reg_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 904 | strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter", |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 905 | sizeof(drv_data->adapter.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | init_waitqueue_head(&drv_data->waitq); |
| 908 | spin_lock_init(&drv_data->lock); |
| 909 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 910 | #if defined(CONFIG_HAVE_CLK) |
| 911 | /* Not all platforms have a clk */ |
Russell King | 4c5c95f | 2013-05-16 21:34:10 +0100 | [diff] [blame] | 912 | drv_data->clk = devm_clk_get(&pd->dev, NULL); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 913 | if (!IS_ERR(drv_data->clk)) { |
| 914 | clk_prepare(drv_data->clk); |
| 915 | clk_enable(drv_data->clk); |
| 916 | } |
| 917 | #endif |
| 918 | if (pdata) { |
| 919 | drv_data->freq_m = pdata->freq_m; |
| 920 | drv_data->freq_n = pdata->freq_n; |
| 921 | drv_data->irq = platform_get_irq(pd, 0); |
| 922 | drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout); |
Gregory CLEMENT | 930ab3d | 2013-08-22 16:19:05 +0200 | [diff] [blame] | 923 | drv_data->offload_enabled = false; |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 924 | memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets)); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 925 | } else if (pd->dev.of_node) { |
Maxime Ripard | 004e8ed | 2013-06-12 18:53:31 +0200 | [diff] [blame] | 926 | rc = mv64xxx_of_config(drv_data, &pd->dev); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 927 | if (rc) |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 928 | goto exit_clk; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 929 | } |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 930 | if (drv_data->irq < 0) { |
| 931 | rc = -ENXIO; |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 932 | goto exit_reset; |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 933 | } |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 934 | |
Jean Delvare | 12a917f | 2007-02-13 22:09:03 +0100 | [diff] [blame] | 935 | drv_data->adapter.dev.parent = &pd->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | drv_data->adapter.algo = &mv64xxx_i2c_algo; |
| 937 | drv_data->adapter.owner = THIS_MODULE; |
Wolfram Sang | 8c49086 | 2014-07-10 13:46:27 +0200 | [diff] [blame] | 938 | drv_data->adapter.class = I2C_CLASS_DEPRECATED; |
Dale Farnsworth | 65b22ad | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 939 | drv_data->adapter.nr = pd->id; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 940 | drv_data->adapter.dev.of_node = pd->dev.of_node; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 941 | platform_set_drvdata(pd, drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | i2c_set_adapdata(&drv_data->adapter, drv_data); |
| 943 | |
Maxime Bizon | 3269bb6 | 2007-01-05 17:54:05 +0100 | [diff] [blame] | 944 | mv64xxx_i2c_hw_init(drv_data); |
| 945 | |
Russell King | 0c195af | 2013-05-16 21:36:11 +0100 | [diff] [blame] | 946 | rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0, |
| 947 | MV64XXX_I2C_CTLR_NAME, drv_data); |
| 948 | if (rc) { |
Mark A. Greer | dfded4a | 2005-12-16 11:08:43 -0800 | [diff] [blame] | 949 | dev_err(&drv_data->adapter.dev, |
Russell King | 0c195af | 2013-05-16 21:36:11 +0100 | [diff] [blame] | 950 | "mv64xxx: Can't register intr handler irq%d: %d\n", |
| 951 | drv_data->irq, rc); |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 952 | goto exit_reset; |
Dale Farnsworth | 65b22ad | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 953 | } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) { |
Mark A. Greer | dfded4a | 2005-12-16 11:08:43 -0800 | [diff] [blame] | 954 | dev_err(&drv_data->adapter.dev, |
| 955 | "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | goto exit_free_irq; |
| 957 | } |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | return 0; |
| 960 | |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 961 | exit_free_irq: |
| 962 | free_irq(drv_data->irq, drv_data); |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 963 | exit_reset: |
Maxime Ripard | f2a67d0 | 2014-03-10 12:12:10 +0100 | [diff] [blame] | 964 | if (!IS_ERR_OR_NULL(drv_data->rstc)) |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 965 | reset_control_assert(drv_data->rstc); |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 966 | exit_clk: |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 967 | #if defined(CONFIG_HAVE_CLK) |
| 968 | /* Not all platforms have a clk */ |
| 969 | if (!IS_ERR(drv_data->clk)) { |
| 970 | clk_disable(drv_data->clk); |
| 971 | clk_unprepare(drv_data->clk); |
| 972 | } |
| 973 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return rc; |
| 975 | } |
| 976 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 977 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 978 | mv64xxx_i2c_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 980 | struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 982 | i2c_del_adapter(&drv_data->adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | free_irq(drv_data->irq, drv_data); |
Maxime Ripard | f2a67d0 | 2014-03-10 12:12:10 +0100 | [diff] [blame] | 984 | if (!IS_ERR_OR_NULL(drv_data->rstc)) |
Maxime Ripard | 370136b | 2014-03-04 17:28:37 +0100 | [diff] [blame] | 985 | reset_control_assert(drv_data->rstc); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 986 | #if defined(CONFIG_HAVE_CLK) |
| 987 | /* Not all platforms have a clk */ |
| 988 | if (!IS_ERR(drv_data->clk)) { |
| 989 | clk_disable(drv_data->clk); |
| 990 | clk_unprepare(drv_data->clk); |
| 991 | } |
| 992 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 994 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 997 | static struct platform_driver mv64xxx_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | .probe = mv64xxx_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 999 | .remove = mv64xxx_i2c_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1000 | .driver = { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1001 | .name = MV64XXX_I2C_CTLR_NAME, |
Sachin Kamat | 4e90532 | 2013-09-30 09:04:25 +0530 | [diff] [blame] | 1002 | .of_match_table = mv64xxx_i2c_of_match_table, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1003 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | }; |
| 1005 | |
Axel Lin | a3664b5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 1006 | module_platform_driver(mv64xxx_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>"); |
| 1009 | MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver"); |
| 1010 | MODULE_LICENSE("GPL"); |