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> |
| 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/of_i2c.h> |
| 24 | #include <linux/clk.h> |
| 25 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* Register defines */ |
| 28 | #define MV64XXX_I2C_REG_SLAVE_ADDR 0x00 |
| 29 | #define MV64XXX_I2C_REG_DATA 0x04 |
| 30 | #define MV64XXX_I2C_REG_CONTROL 0x08 |
| 31 | #define MV64XXX_I2C_REG_STATUS 0x0c |
| 32 | #define MV64XXX_I2C_REG_BAUD 0x0c |
| 33 | #define MV64XXX_I2C_REG_EXT_SLAVE_ADDR 0x10 |
| 34 | #define MV64XXX_I2C_REG_SOFT_RESET 0x1c |
| 35 | |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame^] | 36 | #define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1) |
| 37 | #define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7) |
| 38 | #define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3) |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004 |
| 41 | #define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008 |
| 42 | #define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010 |
| 43 | #define MV64XXX_I2C_REG_CONTROL_START 0x00000020 |
| 44 | #define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040 |
| 45 | #define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080 |
| 46 | |
| 47 | /* Ctlr status values */ |
| 48 | #define MV64XXX_I2C_STATUS_BUS_ERR 0x00 |
| 49 | #define MV64XXX_I2C_STATUS_MAST_START 0x08 |
| 50 | #define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10 |
| 51 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18 |
| 52 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20 |
| 53 | #define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28 |
| 54 | #define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30 |
| 55 | #define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38 |
| 56 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40 |
| 57 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48 |
| 58 | #define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50 |
| 59 | #define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58 |
| 60 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0 |
| 61 | #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8 |
| 62 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0 |
| 63 | #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8 |
| 64 | #define MV64XXX_I2C_STATUS_NO_STATUS 0xf8 |
| 65 | |
| 66 | /* Driver states */ |
| 67 | enum { |
| 68 | MV64XXX_I2C_STATE_INVALID, |
| 69 | MV64XXX_I2C_STATE_IDLE, |
| 70 | MV64XXX_I2C_STATE_WAITING_FOR_START_COND, |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 71 | MV64XXX_I2C_STATE_WAITING_FOR_RESTART, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK, |
| 73 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK, |
| 74 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK, |
| 75 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* Driver actions */ |
| 79 | enum { |
| 80 | MV64XXX_I2C_ACTION_INVALID, |
| 81 | MV64XXX_I2C_ACTION_CONTINUE, |
| 82 | MV64XXX_I2C_ACTION_SEND_START, |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 83 | MV64XXX_I2C_ACTION_SEND_RESTART, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | MV64XXX_I2C_ACTION_SEND_ADDR_1, |
| 85 | MV64XXX_I2C_ACTION_SEND_ADDR_2, |
| 86 | MV64XXX_I2C_ACTION_SEND_DATA, |
| 87 | MV64XXX_I2C_ACTION_RCV_DATA, |
| 88 | MV64XXX_I2C_ACTION_RCV_DATA_STOP, |
| 89 | MV64XXX_I2C_ACTION_SEND_STOP, |
| 90 | }; |
| 91 | |
| 92 | struct mv64xxx_i2c_data { |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 93 | struct i2c_msg *msgs; |
| 94 | int num_msgs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | int irq; |
| 96 | u32 state; |
| 97 | u32 action; |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 98 | u32 aborting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | u32 cntl_bits; |
| 100 | void __iomem *reg_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | u32 addr1; |
| 102 | u32 addr2; |
| 103 | u32 bytes_left; |
| 104 | u32 byte_posn; |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 105 | u32 send_stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | u32 block; |
| 107 | int rc; |
| 108 | u32 freq_m; |
| 109 | u32 freq_n; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 110 | #if defined(CONFIG_HAVE_CLK) |
| 111 | struct clk *clk; |
| 112 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | wait_queue_head_t waitq; |
| 114 | spinlock_t lock; |
| 115 | struct i2c_msg *msg; |
| 116 | struct i2c_adapter adapter; |
| 117 | }; |
| 118 | |
Russell King | 3420afb | 2013-05-16 21:38:11 +0100 | [diff] [blame] | 119 | static void |
| 120 | mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data, |
| 121 | struct i2c_msg *msg) |
| 122 | { |
| 123 | u32 dir = 0; |
| 124 | |
| 125 | drv_data->msg = msg; |
| 126 | drv_data->byte_posn = 0; |
| 127 | drv_data->bytes_left = msg->len; |
| 128 | drv_data->aborting = 0; |
| 129 | drv_data->rc = 0; |
| 130 | drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK | |
| 131 | MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN; |
| 132 | |
| 133 | if (msg->flags & I2C_M_RD) |
| 134 | dir = 1; |
| 135 | |
| 136 | if (msg->flags & I2C_M_TEN) { |
| 137 | drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir; |
| 138 | drv_data->addr2 = (u32)msg->addr & 0xff; |
| 139 | } else { |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame^] | 140 | drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir; |
Russell King | 3420afb | 2013-05-16 21:38:11 +0100 | [diff] [blame] | 141 | drv_data->addr2 = 0; |
| 142 | } |
| 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* |
| 146 | ***************************************************************************** |
| 147 | * |
| 148 | * Finite State Machine & Interrupt Routines |
| 149 | * |
| 150 | ***************************************************************************** |
| 151 | */ |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 152 | |
| 153 | /* Reset hardware and initialize FSM */ |
| 154 | static void |
| 155 | mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data) |
| 156 | { |
| 157 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SOFT_RESET); |
Maxime Ripard | 683e69b | 2013-06-12 18:53:30 +0200 | [diff] [blame^] | 158 | writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n), |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 159 | drv_data->reg_base + MV64XXX_I2C_REG_BAUD); |
| 160 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SLAVE_ADDR); |
| 161 | writel(0, drv_data->reg_base + MV64XXX_I2C_REG_EXT_SLAVE_ADDR); |
| 162 | writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP, |
| 163 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 164 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static void |
| 168 | mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status) |
| 169 | { |
| 170 | /* |
| 171 | * If state is idle, then this is likely the remnants of an old |
| 172 | * operation that driver has given up on or the user has killed. |
| 173 | * If so, issue the stop condition and go to idle. |
| 174 | */ |
| 175 | if (drv_data->state == MV64XXX_I2C_STATE_IDLE) { |
| 176 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 177 | return; |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* The status from the ctlr [mostly] tells us what to do next */ |
| 181 | switch (status) { |
| 182 | /* Start condition interrupt */ |
| 183 | case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */ |
| 184 | case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */ |
| 185 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1; |
| 186 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK; |
| 187 | break; |
| 188 | |
| 189 | /* Performing a write */ |
| 190 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */ |
| 191 | if (drv_data->msg->flags & I2C_M_TEN) { |
| 192 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2; |
| 193 | drv_data->state = |
| 194 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK; |
| 195 | break; |
| 196 | } |
| 197 | /* FALLTHRU */ |
| 198 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */ |
| 199 | case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */ |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 200 | if ((drv_data->bytes_left == 0) |
| 201 | || (drv_data->aborting |
| 202 | && (drv_data->byte_posn != 0))) { |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 203 | if (drv_data->send_stop || drv_data->aborting) { |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 204 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 205 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 206 | } else { |
| 207 | drv_data->action = |
| 208 | MV64XXX_I2C_ACTION_SEND_RESTART; |
| 209 | drv_data->state = |
| 210 | MV64XXX_I2C_STATE_WAITING_FOR_RESTART; |
| 211 | } |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 212 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA; |
| 214 | drv_data->state = |
| 215 | MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK; |
| 216 | drv_data->bytes_left--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | break; |
| 219 | |
| 220 | /* Performing a read */ |
| 221 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */ |
| 222 | if (drv_data->msg->flags & I2C_M_TEN) { |
| 223 | drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2; |
| 224 | drv_data->state = |
| 225 | MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK; |
| 226 | break; |
| 227 | } |
| 228 | /* FALLTHRU */ |
| 229 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */ |
| 230 | if (drv_data->bytes_left == 0) { |
| 231 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 232 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 233 | break; |
| 234 | } |
| 235 | /* FALLTHRU */ |
| 236 | case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */ |
| 237 | if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK) |
| 238 | drv_data->action = MV64XXX_I2C_ACTION_CONTINUE; |
| 239 | else { |
| 240 | drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA; |
| 241 | drv_data->bytes_left--; |
| 242 | } |
| 243 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA; |
| 244 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 245 | if ((drv_data->bytes_left == 1) || drv_data->aborting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK; |
| 247 | break; |
| 248 | |
| 249 | case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */ |
| 250 | drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP; |
| 251 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 252 | break; |
| 253 | |
| 254 | case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */ |
| 255 | case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */ |
| 256 | case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */ |
| 257 | /* Doesn't seem to be a device at other end */ |
| 258 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
| 259 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 260 | drv_data->rc = -ENODEV; |
| 261 | break; |
| 262 | |
| 263 | default: |
| 264 | dev_err(&drv_data->adapter.dev, |
| 265 | "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, " |
| 266 | "status: 0x%x, addr: 0x%x, flags: 0x%x\n", |
| 267 | drv_data->state, status, drv_data->msg->addr, |
| 268 | drv_data->msg->flags); |
| 269 | drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP; |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 270 | mv64xxx_i2c_hw_init(drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | drv_data->rc = -EIO; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data) |
| 277 | { |
| 278 | switch(drv_data->action) { |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 279 | case MV64XXX_I2C_ACTION_SEND_RESTART: |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 280 | /* We should only get here if we have further messages */ |
| 281 | BUG_ON(drv_data->num_msgs == 0); |
| 282 | |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 283 | drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START; |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 284 | writel(drv_data->cntl_bits, |
| 285 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 286 | |
| 287 | drv_data->msgs++; |
| 288 | drv_data->num_msgs--; |
| 289 | |
| 290 | /* Setup for the next message */ |
| 291 | mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs); |
| 292 | |
| 293 | /* |
| 294 | * We're never at the start of the message here, and by this |
| 295 | * time it's already too late to do any protocol mangling. |
| 296 | * Thankfully, do not advertise support for that feature. |
| 297 | */ |
| 298 | drv_data->send_stop = drv_data->num_msgs == 1; |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 299 | break; |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | case MV64XXX_I2C_ACTION_CONTINUE: |
| 302 | writel(drv_data->cntl_bits, |
| 303 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 304 | break; |
| 305 | |
| 306 | case MV64XXX_I2C_ACTION_SEND_START: |
| 307 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START, |
| 308 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 309 | break; |
| 310 | |
| 311 | case MV64XXX_I2C_ACTION_SEND_ADDR_1: |
| 312 | writel(drv_data->addr1, |
| 313 | drv_data->reg_base + MV64XXX_I2C_REG_DATA); |
| 314 | writel(drv_data->cntl_bits, |
| 315 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 316 | break; |
| 317 | |
| 318 | case MV64XXX_I2C_ACTION_SEND_ADDR_2: |
| 319 | writel(drv_data->addr2, |
| 320 | drv_data->reg_base + MV64XXX_I2C_REG_DATA); |
| 321 | writel(drv_data->cntl_bits, |
| 322 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 323 | break; |
| 324 | |
| 325 | case MV64XXX_I2C_ACTION_SEND_DATA: |
| 326 | writel(drv_data->msg->buf[drv_data->byte_posn++], |
| 327 | drv_data->reg_base + MV64XXX_I2C_REG_DATA); |
| 328 | writel(drv_data->cntl_bits, |
| 329 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 330 | break; |
| 331 | |
| 332 | case MV64XXX_I2C_ACTION_RCV_DATA: |
| 333 | drv_data->msg->buf[drv_data->byte_posn++] = |
| 334 | readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA); |
| 335 | writel(drv_data->cntl_bits, |
| 336 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 337 | break; |
| 338 | |
| 339 | case MV64XXX_I2C_ACTION_RCV_DATA_STOP: |
| 340 | drv_data->msg->buf[drv_data->byte_posn++] = |
| 341 | readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA); |
| 342 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN; |
| 343 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP, |
| 344 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 345 | drv_data->block = 0; |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 346 | wake_up(&drv_data->waitq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | break; |
| 348 | |
| 349 | case MV64XXX_I2C_ACTION_INVALID: |
| 350 | default: |
| 351 | dev_err(&drv_data->adapter.dev, |
| 352 | "mv64xxx_i2c_do_action: Invalid action: %d\n", |
| 353 | drv_data->action); |
| 354 | drv_data->rc = -EIO; |
| 355 | /* FALLTHRU */ |
| 356 | case MV64XXX_I2C_ACTION_SEND_STOP: |
| 357 | drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN; |
| 358 | writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP, |
| 359 | drv_data->reg_base + MV64XXX_I2C_REG_CONTROL); |
| 360 | drv_data->block = 0; |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 361 | wake_up(&drv_data->waitq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
Mikael Pettersson | b0999cc | 2009-09-07 12:00:13 +0200 | [diff] [blame] | 366 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 367 | mv64xxx_i2c_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 369 | struct mv64xxx_i2c_data *drv_data = dev_id; |
| 370 | unsigned long flags; |
| 371 | u32 status; |
Mikael Pettersson | b0999cc | 2009-09-07 12:00:13 +0200 | [diff] [blame] | 372 | irqreturn_t rc = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | spin_lock_irqsave(&drv_data->lock, flags); |
| 375 | while (readl(drv_data->reg_base + MV64XXX_I2C_REG_CONTROL) & |
| 376 | MV64XXX_I2C_REG_CONTROL_IFLG) { |
| 377 | status = readl(drv_data->reg_base + MV64XXX_I2C_REG_STATUS); |
| 378 | mv64xxx_i2c_fsm(drv_data, status); |
| 379 | mv64xxx_i2c_do_action(drv_data); |
| 380 | rc = IRQ_HANDLED; |
| 381 | } |
| 382 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 383 | |
| 384 | return rc; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | ***************************************************************************** |
| 389 | * |
| 390 | * I2C Msg Execution Routines |
| 391 | * |
| 392 | ***************************************************************************** |
| 393 | */ |
| 394 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data) |
| 396 | { |
| 397 | long time_left; |
| 398 | unsigned long flags; |
| 399 | char abort = 0; |
| 400 | |
Russell King | d295a86 | 2013-05-16 10:30:59 +0000 | [diff] [blame] | 401 | time_left = wait_event_timeout(drv_data->waitq, |
Jean Delvare | 8a52c6b | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 402 | !drv_data->block, drv_data->adapter.timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | spin_lock_irqsave(&drv_data->lock, flags); |
| 405 | if (!time_left) { /* Timed out */ |
| 406 | drv_data->rc = -ETIMEDOUT; |
| 407 | abort = 1; |
| 408 | } else if (time_left < 0) { /* Interrupted/Error */ |
| 409 | drv_data->rc = time_left; /* errno value */ |
| 410 | abort = 1; |
| 411 | } |
| 412 | |
| 413 | if (abort && drv_data->block) { |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 414 | drv_data->aborting = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 416 | |
| 417 | time_left = wait_event_timeout(drv_data->waitq, |
Jean Delvare | 8a52c6b | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 418 | !drv_data->block, drv_data->adapter.timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 420 | if ((time_left <= 0) && drv_data->block) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | drv_data->state = MV64XXX_I2C_STATE_IDLE; |
| 422 | dev_err(&drv_data->adapter.dev, |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 423 | "mv64xxx: I2C bus locked, block: %d, " |
| 424 | "time_left: %d\n", drv_data->block, |
| 425 | (int)time_left); |
Dale Farnsworth | a07ad1c | 2007-08-14 18:37:14 +0200 | [diff] [blame] | 426 | mv64xxx_i2c_hw_init(drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | } else |
| 429 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 430 | } |
| 431 | |
| 432 | static int |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 433 | 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] | 434 | int is_last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | unsigned long flags; |
| 437 | |
| 438 | spin_lock_irqsave(&drv_data->lock, flags); |
| 439 | mv64xxx_i2c_prepare_for_io(drv_data, msg); |
| 440 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 441 | drv_data->action = MV64XXX_I2C_ACTION_SEND_START; |
| 442 | drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Rodolfo Giometti | eda6bee | 2010-11-26 17:06:56 +0100 | [diff] [blame] | 444 | drv_data->send_stop = is_last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | drv_data->block = 1; |
| 446 | mv64xxx_i2c_do_action(drv_data); |
| 447 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 448 | |
| 449 | mv64xxx_i2c_wait_for_completion(drv_data); |
| 450 | return drv_data->rc; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | ***************************************************************************** |
| 455 | * |
| 456 | * I2C Core Support Routines (Interface to higher level I2C code) |
| 457 | * |
| 458 | ***************************************************************************** |
| 459 | */ |
| 460 | static u32 |
| 461 | mv64xxx_i2c_functionality(struct i2c_adapter *adap) |
| 462 | { |
| 463 | return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL; |
| 464 | } |
| 465 | |
| 466 | static int |
| 467 | mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 468 | { |
| 469 | struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap); |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 470 | int rc, ret = num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 472 | BUG_ON(drv_data->msgs != NULL); |
| 473 | drv_data->msgs = msgs; |
| 474 | drv_data->num_msgs = num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Russell King | 4243fa0 | 2013-05-16 21:39:12 +0100 | [diff] [blame] | 476 | rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1); |
| 477 | if (rc < 0) |
| 478 | ret = rc; |
| 479 | |
| 480 | drv_data->num_msgs = 0; |
| 481 | drv_data->msgs = NULL; |
| 482 | |
| 483 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 486 | static const struct i2c_algorithm mv64xxx_i2c_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | .master_xfer = mv64xxx_i2c_xfer, |
| 488 | .functionality = mv64xxx_i2c_functionality, |
| 489 | }; |
| 490 | |
| 491 | /* |
| 492 | ***************************************************************************** |
| 493 | * |
| 494 | * Driver Interface & Early Init Routines |
| 495 | * |
| 496 | ***************************************************************************** |
| 497 | */ |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 498 | #ifdef CONFIG_OF |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 499 | static int |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 500 | mv64xxx_calc_freq(const int tclk, const int n, const int m) |
| 501 | { |
| 502 | return tclk / (10 * (m + 1) * (2 << n)); |
| 503 | } |
| 504 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 505 | static bool |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 506 | mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n, |
| 507 | u32 *best_m) |
| 508 | { |
| 509 | int freq, delta, best_delta = INT_MAX; |
| 510 | int m, n; |
| 511 | |
| 512 | for (n = 0; n <= 7; n++) |
| 513 | for (m = 0; m <= 15; m++) { |
| 514 | freq = mv64xxx_calc_freq(tclk, n, m); |
| 515 | delta = req_freq - freq; |
| 516 | if (delta >= 0 && delta < best_delta) { |
| 517 | *best_m = m; |
| 518 | *best_n = n; |
| 519 | best_delta = delta; |
| 520 | } |
| 521 | if (best_delta == 0) |
| 522 | return true; |
| 523 | } |
| 524 | if (best_delta == INT_MAX) |
| 525 | return false; |
| 526 | return true; |
| 527 | } |
| 528 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 529 | static int |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 530 | mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data, |
| 531 | struct device_node *np) |
| 532 | { |
| 533 | u32 bus_freq, tclk; |
| 534 | int rc = 0; |
| 535 | |
| 536 | /* CLK is mandatory when using DT to describe the i2c bus. We |
| 537 | * need to know tclk in order to calculate bus clock |
| 538 | * factors. |
| 539 | */ |
| 540 | #if !defined(CONFIG_HAVE_CLK) |
| 541 | /* Have OF but no CLK */ |
| 542 | return -ENODEV; |
| 543 | #else |
| 544 | if (IS_ERR(drv_data->clk)) { |
| 545 | rc = -ENODEV; |
| 546 | goto out; |
| 547 | } |
| 548 | tclk = clk_get_rate(drv_data->clk); |
| 549 | of_property_read_u32(np, "clock-frequency", &bus_freq); |
| 550 | if (!mv64xxx_find_baud_factors(bus_freq, tclk, |
| 551 | &drv_data->freq_n, &drv_data->freq_m)) { |
| 552 | rc = -EINVAL; |
| 553 | goto out; |
| 554 | } |
| 555 | drv_data->irq = irq_of_parse_and_map(np, 0); |
| 556 | |
| 557 | /* Its not yet defined how timeouts will be specified in device tree. |
| 558 | * So hard code the value to 1 second. |
| 559 | */ |
| 560 | drv_data->adapter.timeout = HZ; |
| 561 | out: |
| 562 | return rc; |
| 563 | #endif |
| 564 | } |
| 565 | #else /* CONFIG_OF */ |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 566 | static int |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 567 | mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data, |
| 568 | struct device_node *np) |
| 569 | { |
| 570 | return -ENODEV; |
| 571 | } |
| 572 | #endif /* CONFIG_OF */ |
| 573 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 574 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 575 | mv64xxx_i2c_probe(struct platform_device *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | struct mv64xxx_i2c_data *drv_data; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 578 | struct mv64xxx_i2c_pdata *pdata = pd->dev.platform_data; |
Russell King | 16874b0 | 2013-05-16 21:33:09 +0100 | [diff] [blame] | 579 | struct resource *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | int rc; |
| 581 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 582 | if ((!pdata && !pd->dev.of_node)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return -ENODEV; |
| 584 | |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 585 | drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data), |
| 586 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (!drv_data) |
| 588 | return -ENOMEM; |
| 589 | |
Russell King | 16874b0 | 2013-05-16 21:33:09 +0100 | [diff] [blame] | 590 | r = platform_get_resource(pd, IORESOURCE_MEM, 0); |
| 591 | drv_data->reg_base = devm_ioremap_resource(&pd->dev, r); |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 592 | if (IS_ERR(drv_data->reg_base)) |
| 593 | return PTR_ERR(drv_data->reg_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Mark A. Greer | e91c021 | 2005-12-18 17:22:01 +0100 | [diff] [blame] | 595 | strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter", |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 596 | sizeof(drv_data->adapter.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | init_waitqueue_head(&drv_data->waitq); |
| 599 | spin_lock_init(&drv_data->lock); |
| 600 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 601 | #if defined(CONFIG_HAVE_CLK) |
| 602 | /* Not all platforms have a clk */ |
Russell King | 4c5c95f | 2013-05-16 21:34:10 +0100 | [diff] [blame] | 603 | drv_data->clk = devm_clk_get(&pd->dev, NULL); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 604 | if (!IS_ERR(drv_data->clk)) { |
| 605 | clk_prepare(drv_data->clk); |
| 606 | clk_enable(drv_data->clk); |
| 607 | } |
| 608 | #endif |
| 609 | if (pdata) { |
| 610 | drv_data->freq_m = pdata->freq_m; |
| 611 | drv_data->freq_n = pdata->freq_n; |
| 612 | drv_data->irq = platform_get_irq(pd, 0); |
| 613 | drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout); |
| 614 | } else if (pd->dev.of_node) { |
| 615 | rc = mv64xxx_of_config(drv_data, pd->dev.of_node); |
| 616 | if (rc) |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 617 | goto exit_clk; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 618 | } |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 619 | if (drv_data->irq < 0) { |
| 620 | rc = -ENXIO; |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 621 | goto exit_clk; |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 622 | } |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 623 | |
Jean Delvare | 12a917f | 2007-02-13 22:09:03 +0100 | [diff] [blame] | 624 | drv_data->adapter.dev.parent = &pd->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | drv_data->adapter.algo = &mv64xxx_i2c_algo; |
| 626 | drv_data->adapter.owner = THIS_MODULE; |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 627 | drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Dale Farnsworth | 65b22ad | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 628 | drv_data->adapter.nr = pd->id; |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 629 | drv_data->adapter.dev.of_node = pd->dev.of_node; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 630 | platform_set_drvdata(pd, drv_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | i2c_set_adapdata(&drv_data->adapter, drv_data); |
| 632 | |
Maxime Bizon | 3269bb6 | 2007-01-05 17:54:05 +0100 | [diff] [blame] | 633 | mv64xxx_i2c_hw_init(drv_data); |
| 634 | |
Russell King | 0c195af | 2013-05-16 21:36:11 +0100 | [diff] [blame] | 635 | rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0, |
| 636 | MV64XXX_I2C_CTLR_NAME, drv_data); |
| 637 | if (rc) { |
Mark A. Greer | dfded4a | 2005-12-16 11:08:43 -0800 | [diff] [blame] | 638 | dev_err(&drv_data->adapter.dev, |
Russell King | 0c195af | 2013-05-16 21:36:11 +0100 | [diff] [blame] | 639 | "mv64xxx: Can't register intr handler irq%d: %d\n", |
| 640 | drv_data->irq, rc); |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 641 | goto exit_clk; |
Dale Farnsworth | 65b22ad | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 642 | } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) { |
Mark A. Greer | dfded4a | 2005-12-16 11:08:43 -0800 | [diff] [blame] | 643 | dev_err(&drv_data->adapter.dev, |
| 644 | "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | goto exit_free_irq; |
| 646 | } |
| 647 | |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 648 | of_i2c_register_devices(&drv_data->adapter); |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return 0; |
| 651 | |
Russell King | 2c91110 | 2013-05-16 21:35:10 +0100 | [diff] [blame] | 652 | exit_free_irq: |
| 653 | free_irq(drv_data->irq, drv_data); |
| 654 | exit_clk: |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 655 | #if defined(CONFIG_HAVE_CLK) |
| 656 | /* Not all platforms have a clk */ |
| 657 | if (!IS_ERR(drv_data->clk)) { |
| 658 | clk_disable(drv_data->clk); |
| 659 | clk_unprepare(drv_data->clk); |
| 660 | } |
| 661 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return rc; |
| 663 | } |
| 664 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 665 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 666 | mv64xxx_i2c_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 668 | struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 670 | i2c_del_adapter(&drv_data->adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | free_irq(drv_data->irq, drv_data); |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 672 | #if defined(CONFIG_HAVE_CLK) |
| 673 | /* Not all platforms have a clk */ |
| 674 | if (!IS_ERR(drv_data->clk)) { |
| 675 | clk_disable(drv_data->clk); |
| 676 | clk_unprepare(drv_data->clk); |
| 677 | } |
| 678 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 683 | static const struct of_device_id mv64xxx_i2c_of_match_table[] = { |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 684 | { .compatible = "marvell,mv64xxx-i2c", }, |
| 685 | {} |
| 686 | }; |
| 687 | MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table); |
| 688 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 689 | static struct platform_driver mv64xxx_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | .probe = mv64xxx_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 691 | .remove = mv64xxx_i2c_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 692 | .driver = { |
| 693 | .owner = THIS_MODULE, |
| 694 | .name = MV64XXX_I2C_CTLR_NAME, |
Andrew Lunn | b61d157 | 2012-07-22 12:51:35 +0200 | [diff] [blame] | 695 | .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 696 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | }; |
| 698 | |
Axel Lin | a3664b5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 699 | module_platform_driver(mv64xxx_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>"); |
| 702 | MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver"); |
| 703 | MODULE_LICENSE("GPL"); |