Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * i2c-ocores.c: I2C bus driver for OpenCores I2C controller |
| 3 | * (http://www.opencores.org/projects.cgi/web/i2c/overview). |
| 4 | * |
| 5 | * Peter Korsgaard <jacmet@sunsite.dk> |
| 6 | * |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 7 | * Support for the GRLIB port of the controller by |
| 8 | * Andreas Larsson <andreas@gaisler.com> |
| 9 | * |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 10 | * This file is licensed under the terms of the GNU General Public License |
| 11 | * version 2. This program is licensed "as is" without any warranty of any |
| 12 | * kind, whether express or implied. |
| 13 | */ |
| 14 | |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <linux/i2c-ocores.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 25 | #include <linux/io.h> |
Ganesan Ramalingam | a5063f1 | 2012-05-08 18:55:28 +0530 | [diff] [blame] | 26 | #include <linux/of_i2c.h> |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 27 | #include <linux/log2.h> |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 28 | |
| 29 | struct ocores_i2c { |
| 30 | void __iomem *base; |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 31 | u32 reg_shift; |
Ganesan Ramalingam | 7326e38 | 2012-07-13 19:14:25 +0530 | [diff] [blame] | 32 | u32 reg_io_width; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 33 | wait_queue_head_t wait; |
| 34 | struct i2c_adapter adap; |
| 35 | struct i2c_msg *msg; |
| 36 | int pos; |
| 37 | int nmsgs; |
| 38 | int state; /* see STATE_ */ |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 39 | int clock_khz; |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 40 | void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value); |
| 41 | u8 (*getreg)(struct ocores_i2c *i2c, int reg); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /* registers */ |
| 45 | #define OCI2C_PRELOW 0 |
| 46 | #define OCI2C_PREHIGH 1 |
| 47 | #define OCI2C_CONTROL 2 |
| 48 | #define OCI2C_DATA 3 |
Peter Korsgaard | 1ded969 | 2006-06-12 21:40:53 +0200 | [diff] [blame] | 49 | #define OCI2C_CMD 4 /* write only */ |
| 50 | #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */ |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 51 | |
| 52 | #define OCI2C_CTRL_IEN 0x40 |
| 53 | #define OCI2C_CTRL_EN 0x80 |
| 54 | |
| 55 | #define OCI2C_CMD_START 0x91 |
| 56 | #define OCI2C_CMD_STOP 0x41 |
| 57 | #define OCI2C_CMD_READ 0x21 |
| 58 | #define OCI2C_CMD_WRITE 0x11 |
| 59 | #define OCI2C_CMD_READ_ACK 0x21 |
| 60 | #define OCI2C_CMD_READ_NACK 0x29 |
| 61 | #define OCI2C_CMD_IACK 0x01 |
| 62 | |
| 63 | #define OCI2C_STAT_IF 0x01 |
| 64 | #define OCI2C_STAT_TIP 0x02 |
| 65 | #define OCI2C_STAT_ARBLOST 0x20 |
| 66 | #define OCI2C_STAT_BUSY 0x40 |
| 67 | #define OCI2C_STAT_NACK 0x80 |
| 68 | |
| 69 | #define STATE_DONE 0 |
| 70 | #define STATE_START 1 |
| 71 | #define STATE_WRITE 2 |
| 72 | #define STATE_READ 3 |
| 73 | #define STATE_ERROR 4 |
| 74 | |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 75 | #define TYPE_OCORES 0 |
| 76 | #define TYPE_GRLIB 1 |
| 77 | |
| 78 | static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) |
| 79 | { |
| 80 | iowrite8(value, i2c->base + (reg << i2c->reg_shift)); |
| 81 | } |
| 82 | |
| 83 | static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) |
| 84 | { |
| 85 | iowrite16(value, i2c->base + (reg << i2c->reg_shift)); |
| 86 | } |
| 87 | |
| 88 | static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) |
| 89 | { |
| 90 | iowrite32(value, i2c->base + (reg << i2c->reg_shift)); |
| 91 | } |
| 92 | |
| 93 | static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) |
| 94 | { |
| 95 | return ioread8(i2c->base + (reg << i2c->reg_shift)); |
| 96 | } |
| 97 | |
| 98 | static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) |
| 99 | { |
| 100 | return ioread16(i2c->base + (reg << i2c->reg_shift)); |
| 101 | } |
| 102 | |
| 103 | static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) |
| 104 | { |
| 105 | return ioread32(i2c->base + (reg << i2c->reg_shift)); |
| 106 | } |
| 107 | |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 108 | static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) |
| 109 | { |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 110 | i2c->setreg(i2c, reg, value); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) |
| 114 | { |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 115 | return i2c->getreg(i2c, reg); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void ocores_process(struct ocores_i2c *i2c) |
| 119 | { |
| 120 | struct i2c_msg *msg = i2c->msg; |
| 121 | u8 stat = oc_getreg(i2c, OCI2C_STATUS); |
| 122 | |
| 123 | if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { |
| 124 | /* stop has been sent */ |
| 125 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); |
| 126 | wake_up(&i2c->wait); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | /* error? */ |
| 131 | if (stat & OCI2C_STAT_ARBLOST) { |
| 132 | i2c->state = STATE_ERROR; |
| 133 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { |
| 138 | i2c->state = |
| 139 | (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; |
| 140 | |
| 141 | if (stat & OCI2C_STAT_NACK) { |
| 142 | i2c->state = STATE_ERROR; |
| 143 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); |
| 144 | return; |
| 145 | } |
| 146 | } else |
| 147 | msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); |
| 148 | |
| 149 | /* end of msg? */ |
| 150 | if (i2c->pos == msg->len) { |
| 151 | i2c->nmsgs--; |
| 152 | i2c->msg++; |
| 153 | i2c->pos = 0; |
| 154 | msg = i2c->msg; |
| 155 | |
| 156 | if (i2c->nmsgs) { /* end? */ |
| 157 | /* send start? */ |
| 158 | if (!(msg->flags & I2C_M_NOSTART)) { |
| 159 | u8 addr = (msg->addr << 1); |
| 160 | |
| 161 | if (msg->flags & I2C_M_RD) |
| 162 | addr |= 1; |
| 163 | |
| 164 | i2c->state = STATE_START; |
| 165 | |
| 166 | oc_setreg(i2c, OCI2C_DATA, addr); |
| 167 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); |
| 168 | return; |
| 169 | } else |
| 170 | i2c->state = (msg->flags & I2C_M_RD) |
| 171 | ? STATE_READ : STATE_WRITE; |
| 172 | } else { |
| 173 | i2c->state = STATE_DONE; |
| 174 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (i2c->state == STATE_READ) { |
| 180 | oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? |
| 181 | OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK); |
| 182 | } else { |
| 183 | oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); |
| 184 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); |
| 185 | } |
| 186 | } |
| 187 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 188 | static irqreturn_t ocores_isr(int irq, void *dev_id) |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 189 | { |
| 190 | struct ocores_i2c *i2c = dev_id; |
| 191 | |
| 192 | ocores_process(i2c); |
| 193 | |
| 194 | return IRQ_HANDLED; |
| 195 | } |
| 196 | |
| 197 | static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 198 | { |
| 199 | struct ocores_i2c *i2c = i2c_get_adapdata(adap); |
| 200 | |
| 201 | i2c->msg = msgs; |
| 202 | i2c->pos = 0; |
| 203 | i2c->nmsgs = num; |
| 204 | i2c->state = STATE_START; |
| 205 | |
| 206 | oc_setreg(i2c, OCI2C_DATA, |
| 207 | (i2c->msg->addr << 1) | |
| 208 | ((i2c->msg->flags & I2C_M_RD) ? 1:0)); |
| 209 | |
| 210 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); |
| 211 | |
| 212 | if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) || |
| 213 | (i2c->state == STATE_DONE), HZ)) |
| 214 | return (i2c->state == STATE_DONE) ? num : -EIO; |
| 215 | else |
| 216 | return -ETIMEDOUT; |
| 217 | } |
| 218 | |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 219 | static void ocores_init(struct ocores_i2c *i2c) |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 220 | { |
| 221 | int prescale; |
| 222 | u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); |
| 223 | |
| 224 | /* make sure the device is disabled */ |
| 225 | oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN)); |
| 226 | |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 227 | prescale = (i2c->clock_khz / (5*100)) - 1; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 228 | oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); |
| 229 | oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); |
| 230 | |
| 231 | /* Init the device */ |
| 232 | oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); |
| 233 | oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | static u32 ocores_func(struct i2c_adapter *adap) |
| 238 | { |
| 239 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 240 | } |
| 241 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 242 | static const struct i2c_algorithm ocores_algorithm = { |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 243 | .master_xfer = ocores_xfer, |
| 244 | .functionality = ocores_func, |
| 245 | }; |
| 246 | |
| 247 | static struct i2c_adapter ocores_adapter = { |
| 248 | .owner = THIS_MODULE, |
| 249 | .name = "i2c-ocores", |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 250 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 251 | .algo = &ocores_algorithm, |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 252 | }; |
| 253 | |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 254 | static struct of_device_id ocores_i2c_match[] = { |
| 255 | { |
| 256 | .compatible = "opencores,i2c-ocores", |
| 257 | .data = (void *)TYPE_OCORES, |
| 258 | }, |
| 259 | { |
| 260 | .compatible = "aeroflexgaisler,i2cmst", |
| 261 | .data = (void *)TYPE_GRLIB, |
| 262 | }, |
| 263 | {}, |
| 264 | }; |
| 265 | MODULE_DEVICE_TABLE(of, ocores_i2c_match); |
| 266 | |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 267 | #ifdef CONFIG_OF |
Andreas Larsson | c5d5474 | 2012-11-19 13:17:48 +0100 | [diff] [blame] | 268 | /* Read and write functions for the GRLIB port of the controller. Registers are |
| 269 | * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one |
| 270 | * register. The subsequent registers has their offset decreased accordingly. */ |
| 271 | static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) |
| 272 | { |
| 273 | u32 rd; |
| 274 | int rreg = reg; |
| 275 | if (reg != OCI2C_PRELOW) |
| 276 | rreg--; |
| 277 | rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); |
| 278 | if (reg == OCI2C_PREHIGH) |
| 279 | return (u8)(rd >> 8); |
| 280 | else |
| 281 | return (u8)rd; |
| 282 | } |
| 283 | |
| 284 | static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) |
| 285 | { |
| 286 | u32 curr, wr; |
| 287 | int rreg = reg; |
| 288 | if (reg != OCI2C_PRELOW) |
| 289 | rreg--; |
| 290 | if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) { |
| 291 | curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); |
| 292 | if (reg == OCI2C_PRELOW) |
| 293 | wr = (curr & 0xff00) | value; |
| 294 | else |
| 295 | wr = (((u32)value) << 8) | (curr & 0xff); |
| 296 | } else { |
| 297 | wr = value; |
| 298 | } |
| 299 | iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); |
| 300 | } |
| 301 | |
Jayachandran C | 9ae97a8 | 2012-07-13 19:14:22 +0530 | [diff] [blame] | 302 | static int ocores_i2c_of_probe(struct platform_device *pdev, |
| 303 | struct ocores_i2c *i2c) |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 304 | { |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 305 | struct device_node *np = pdev->dev.of_node; |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 306 | const struct of_device_id *match; |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 307 | u32 val; |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 308 | |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 309 | if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { |
| 310 | /* no 'reg-shift', check for deprecated 'regstep' */ |
| 311 | if (!of_property_read_u32(np, "regstep", &val)) { |
| 312 | if (!is_power_of_2(val)) { |
| 313 | dev_err(&pdev->dev, "invalid regstep %d\n", |
| 314 | val); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | i2c->reg_shift = ilog2(val); |
| 318 | dev_warn(&pdev->dev, |
| 319 | "regstep property deprecated, use reg-shift\n"); |
| 320 | } |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 321 | } |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 322 | |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 323 | if (of_property_read_u32(np, "clock-frequency", &val)) { |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 324 | dev_err(&pdev->dev, |
Jayachandran C | 9ae97a8 | 2012-07-13 19:14:22 +0530 | [diff] [blame] | 325 | "Missing required parameter 'clock-frequency'\n"); |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 326 | return -ENODEV; |
| 327 | } |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 328 | i2c->clock_khz = val / 1000; |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 329 | |
Ganesan Ramalingam | 7326e38 | 2012-07-13 19:14:25 +0530 | [diff] [blame] | 330 | of_property_read_u32(pdev->dev.of_node, "reg-io-width", |
| 331 | &i2c->reg_io_width); |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 332 | |
| 333 | match = of_match_node(ocores_i2c_match, pdev->dev.of_node); |
| 334 | if (match && (int)match->data == TYPE_GRLIB) { |
| 335 | dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); |
| 336 | i2c->setreg = oc_setreg_grlib; |
| 337 | i2c->getreg = oc_getreg_grlib; |
| 338 | } |
| 339 | |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | #else |
| 343 | #define ocores_i2c_of_probe(pdev,i2c) -ENODEV |
| 344 | #endif |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 345 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame^] | 346 | static int ocores_i2c_probe(struct platform_device *pdev) |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 347 | { |
| 348 | struct ocores_i2c *i2c; |
| 349 | struct ocores_i2c_platform_data *pdata; |
Andreas Larsson | f5f35a9 | 2012-11-15 16:50:58 +0100 | [diff] [blame] | 350 | struct resource *res; |
| 351 | int irq; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 352 | int ret; |
Richard Röjfors | dd14be4 | 2009-06-05 15:40:32 +0200 | [diff] [blame] | 353 | int i; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 354 | |
| 355 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 356 | if (!res) |
| 357 | return -ENODEV; |
| 358 | |
Andreas Larsson | f5f35a9 | 2012-11-15 16:50:58 +0100 | [diff] [blame] | 359 | irq = platform_get_irq(pdev, 0); |
| 360 | if (irq < 0) |
| 361 | return irq; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 362 | |
Jonas Bonn | 47def5b | 2010-11-24 17:26:21 +0100 | [diff] [blame] | 363 | i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 364 | if (!i2c) |
| 365 | return -ENOMEM; |
| 366 | |
Thierry Reding | 970d494 | 2012-08-08 08:54:32 +0200 | [diff] [blame] | 367 | i2c->base = devm_request_and_ioremap(&pdev->dev, res); |
| 368 | if (!i2c->base) |
| 369 | return -EADDRNOTAVAIL; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 370 | |
Samuel Ortiz | 3271d38 | 2011-04-08 01:23:57 +0200 | [diff] [blame] | 371 | pdata = pdev->dev.platform_data; |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 372 | if (pdata) { |
Ganesan Ramalingam | 8bb986a | 2012-07-13 19:14:23 +0530 | [diff] [blame] | 373 | i2c->reg_shift = pdata->reg_shift; |
Ganesan Ramalingam | 7326e38 | 2012-07-13 19:14:25 +0530 | [diff] [blame] | 374 | i2c->reg_io_width = pdata->reg_io_width; |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 375 | i2c->clock_khz = pdata->clock_khz; |
| 376 | } else { |
| 377 | ret = ocores_i2c_of_probe(pdev, i2c); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | } |
| 381 | |
Ganesan Ramalingam | 7326e38 | 2012-07-13 19:14:25 +0530 | [diff] [blame] | 382 | if (i2c->reg_io_width == 0) |
| 383 | i2c->reg_io_width = 1; /* Set to default value */ |
| 384 | |
Andreas Larsson | a000b8c1 | 2012-11-15 16:50:59 +0100 | [diff] [blame] | 385 | if (!i2c->setreg || !i2c->getreg) { |
| 386 | switch (i2c->reg_io_width) { |
| 387 | case 1: |
| 388 | i2c->setreg = oc_setreg_8; |
| 389 | i2c->getreg = oc_getreg_8; |
| 390 | break; |
| 391 | |
| 392 | case 2: |
| 393 | i2c->setreg = oc_setreg_16; |
| 394 | i2c->getreg = oc_getreg_16; |
| 395 | break; |
| 396 | |
| 397 | case 4: |
| 398 | i2c->setreg = oc_setreg_32; |
| 399 | i2c->getreg = oc_getreg_32; |
| 400 | break; |
| 401 | |
| 402 | default: |
| 403 | dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", |
| 404 | i2c->reg_io_width); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | } |
| 408 | |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 409 | ocores_init(i2c); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 410 | |
| 411 | init_waitqueue_head(&i2c->wait); |
Andreas Larsson | f5f35a9 | 2012-11-15 16:50:58 +0100 | [diff] [blame] | 412 | ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0, |
Jonas Bonn | 47def5b | 2010-11-24 17:26:21 +0100 | [diff] [blame] | 413 | pdev->name, i2c); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 414 | if (ret) { |
| 415 | dev_err(&pdev->dev, "Cannot claim IRQ\n"); |
Jonas Bonn | 47def5b | 2010-11-24 17:26:21 +0100 | [diff] [blame] | 416 | return ret; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* hook up driver to tree */ |
| 420 | platform_set_drvdata(pdev, i2c); |
| 421 | i2c->adap = ocores_adapter; |
| 422 | i2c_set_adapdata(&i2c->adap, i2c); |
| 423 | i2c->adap.dev.parent = &pdev->dev; |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 424 | i2c->adap.dev.of_node = pdev->dev.of_node; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 425 | |
| 426 | /* add i2c adapter to i2c tree */ |
| 427 | ret = i2c_add_adapter(&i2c->adap); |
| 428 | if (ret) { |
| 429 | dev_err(&pdev->dev, "Failed to add adapter\n"); |
Jonas Bonn | 47def5b | 2010-11-24 17:26:21 +0100 | [diff] [blame] | 430 | return ret; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Richard Röjfors | dd14be4 | 2009-06-05 15:40:32 +0200 | [diff] [blame] | 433 | /* add in known devices to the bus */ |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 434 | if (pdata) { |
| 435 | for (i = 0; i < pdata->num_devices; i++) |
| 436 | i2c_new_device(&i2c->adap, pdata->devices + i); |
Ganesan Ramalingam | a5063f1 | 2012-05-08 18:55:28 +0530 | [diff] [blame] | 437 | } else { |
| 438 | of_i2c_register_devices(&i2c->adap); |
Jonas Bonn | 049bb69d | 2010-11-24 17:26:20 +0100 | [diff] [blame] | 439 | } |
Richard Röjfors | dd14be4 | 2009-06-05 15:40:32 +0200 | [diff] [blame] | 440 | |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 441 | return 0; |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame^] | 444 | static int ocores_i2c_remove(struct platform_device *pdev) |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 445 | { |
| 446 | struct ocores_i2c *i2c = platform_get_drvdata(pdev); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 447 | |
| 448 | /* disable i2c logic */ |
| 449 | oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL) |
| 450 | & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN)); |
| 451 | |
| 452 | /* remove adapter & data */ |
| 453 | i2c_del_adapter(&i2c->adap); |
| 454 | platform_set_drvdata(pdev, NULL); |
| 455 | |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 459 | #ifdef CONFIG_PM |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 460 | static int ocores_i2c_suspend(struct device *dev) |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 461 | { |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 462 | struct ocores_i2c *i2c = dev_get_drvdata(dev); |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 463 | u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); |
| 464 | |
| 465 | /* make sure the device is disabled */ |
| 466 | oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN)); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 471 | static int ocores_i2c_resume(struct device *dev) |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 472 | { |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 473 | struct ocores_i2c *i2c = dev_get_drvdata(dev); |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 474 | |
| 475 | ocores_init(i2c); |
| 476 | |
| 477 | return 0; |
| 478 | } |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 479 | |
| 480 | static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume); |
| 481 | #define OCORES_I2C_PM (&ocores_i2c_pm) |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 482 | #else |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 483 | #define OCORES_I2C_PM NULL |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 484 | #endif |
| 485 | |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 486 | static struct platform_driver ocores_i2c_driver = { |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 487 | .probe = ocores_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame^] | 488 | .remove = ocores_i2c_remove, |
Manuel Lauss | 2373c18 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 489 | .driver = { |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 490 | .owner = THIS_MODULE, |
| 491 | .name = "ocores-i2c", |
Grant Likely | c9e358d | 2011-01-21 09:24:48 -0700 | [diff] [blame] | 492 | .of_match_table = ocores_i2c_match, |
Rafael J. Wysocki | 84603c7 | 2012-07-11 21:24:15 +0200 | [diff] [blame] | 493 | .pm = OCORES_I2C_PM, |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 494 | }, |
| 495 | }; |
| 496 | |
Axel Lin | a3664b5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 497 | module_platform_driver(ocores_i2c_driver); |
Peter Korsgaard | 18f98b1 | 2006-06-04 20:01:08 +0200 | [diff] [blame] | 498 | |
| 499 | MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>"); |
| 500 | MODULE_DESCRIPTION("OpenCores I2C bus driver"); |
| 501 | MODULE_LICENSE("GPL"); |
Axel Lin | a3664b5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 502 | MODULE_ALIAS("platform:ocores-i2c"); |