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