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