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