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