blob: 6cbbb134cfb71744a0738b40e24be75ace78e607 [file] [log] [blame]
Peter Korsgaard18f98b12006-06-04 20:01:08 +02001/*
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 Larssona000b8c12012-11-15 16:50:59 +01007 * Support for the GRLIB port of the controller by
8 * Andreas Larsson <andreas@gaisler.com>
9 *
Peter Korsgaard18f98b12006-06-04 20:01:08 +020010 * 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
Thierry Reding84dbf802013-01-21 11:09:03 +010015#include <linux/err.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020016#include <linux/kernel.h>
17#include <linux/module.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020018#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 Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020025#include <linux/io.h>
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +053026#include <linux/log2.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020027
28struct ocores_i2c {
29 void __iomem *base;
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +053030 u32 reg_shift;
Ganesan Ramalingam7326e382012-07-13 19:14:25 +053031 u32 reg_io_width;
Peter Korsgaard18f98b12006-06-04 20:01:08 +020032 wait_queue_head_t wait;
33 struct i2c_adapter adap;
34 struct i2c_msg *msg;
35 int pos;
36 int nmsgs;
37 int state; /* see STATE_ */
Max Filippov3a33a852015-02-02 18:28:12 +030038 int ip_clock_khz;
39 int bus_clock_khz;
Andreas Larssona000b8c12012-11-15 16:50:59 +010040 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
41 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
Peter Korsgaard18f98b12006-06-04 20:01:08 +020042};
43
44/* registers */
45#define OCI2C_PRELOW 0
46#define OCI2C_PREHIGH 1
47#define OCI2C_CONTROL 2
48#define OCI2C_DATA 3
Peter Korsgaard1ded9692006-06-12 21:40:53 +020049#define OCI2C_CMD 4 /* write only */
50#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
Peter Korsgaard18f98b12006-06-04 20:01:08 +020051
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 Larssona000b8c12012-11-15 16:50:59 +010075#define TYPE_OCORES 0
76#define TYPE_GRLIB 1
77
78static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
79{
80 iowrite8(value, i2c->base + (reg << i2c->reg_shift));
81}
82
83static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
84{
85 iowrite16(value, i2c->base + (reg << i2c->reg_shift));
86}
87
88static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
89{
90 iowrite32(value, i2c->base + (reg << i2c->reg_shift));
91}
92
93static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
94{
95 return ioread8(i2c->base + (reg << i2c->reg_shift));
96}
97
98static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
99{
100 return ioread16(i2c->base + (reg << i2c->reg_shift));
101}
102
103static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
104{
105 return ioread32(i2c->base + (reg << i2c->reg_shift));
106}
107
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200108static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
109{
Andreas Larssona000b8c12012-11-15 16:50:59 +0100110 i2c->setreg(i2c, reg, value);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200111}
112
113static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
114{
Andreas Larssona000b8c12012-11-15 16:50:59 +0100115 return i2c->getreg(i2c, reg);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200116}
117
118static 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 Howells7d12e782006-10-05 14:55:46 +0100188static irqreturn_t ocores_isr(int irq, void *dev_id)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200189{
190 struct ocores_i2c *i2c = dev_id;
191
192 ocores_process(i2c);
193
194 return IRQ_HANDLED;
195}
196
197static 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
Max Filippov3a33a852015-02-02 18:28:12 +0300219static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200220{
221 int prescale;
Max Filippov3a33a852015-02-02 18:28:12 +0300222 int diff;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200223 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
224
225 /* make sure the device is disabled */
226 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
227
Max Filippov3a33a852015-02-02 18:28:12 +0300228 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
229 prescale = clamp(prescale, 0, 0xffff);
230
231 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
232 if (abs(diff) > i2c->bus_clock_khz / 10) {
233 dev_err(dev,
234 "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
235 i2c->ip_clock_khz, i2c->bus_clock_khz);
236 return -EINVAL;
237 }
238
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200239 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
240 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
241
242 /* Init the device */
243 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
244 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
Max Filippov3a33a852015-02-02 18:28:12 +0300245
246 return 0;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200247}
248
249
250static u32 ocores_func(struct i2c_adapter *adap)
251{
252 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
253}
254
Jean Delvare8f9082c2006-09-03 22:39:46 +0200255static const struct i2c_algorithm ocores_algorithm = {
Wolfram Sang1ce97e02014-07-10 13:46:29 +0200256 .master_xfer = ocores_xfer,
257 .functionality = ocores_func,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200258};
259
260static struct i2c_adapter ocores_adapter = {
Wolfram Sang1ce97e02014-07-10 13:46:29 +0200261 .owner = THIS_MODULE,
262 .name = "i2c-ocores",
263 .class = I2C_CLASS_DEPRECATED,
264 .algo = &ocores_algorithm,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200265};
266
Jingoo Haneae45e52014-05-15 15:46:11 +0900267static const struct of_device_id ocores_i2c_match[] = {
Andreas Larssona000b8c12012-11-15 16:50:59 +0100268 {
269 .compatible = "opencores,i2c-ocores",
270 .data = (void *)TYPE_OCORES,
271 },
272 {
273 .compatible = "aeroflexgaisler,i2cmst",
274 .data = (void *)TYPE_GRLIB,
275 },
276 {},
277};
278MODULE_DEVICE_TABLE(of, ocores_i2c_match);
279
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100280#ifdef CONFIG_OF
Andreas Larssonc5d54742012-11-19 13:17:48 +0100281/* Read and write functions for the GRLIB port of the controller. Registers are
282 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
283 * register. The subsequent registers has their offset decreased accordingly. */
284static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
285{
286 u32 rd;
287 int rreg = reg;
288 if (reg != OCI2C_PRELOW)
289 rreg--;
290 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
291 if (reg == OCI2C_PREHIGH)
292 return (u8)(rd >> 8);
293 else
294 return (u8)rd;
295}
296
297static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
298{
299 u32 curr, wr;
300 int rreg = reg;
301 if (reg != OCI2C_PRELOW)
302 rreg--;
303 if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
304 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
305 if (reg == OCI2C_PRELOW)
306 wr = (curr & 0xff00) | value;
307 else
308 wr = (((u32)value) << 8) | (curr & 0xff);
309 } else {
310 wr = value;
311 }
312 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
313}
314
Jayachandran C9ae97a82012-07-13 19:14:22 +0530315static int ocores_i2c_of_probe(struct platform_device *pdev,
316 struct ocores_i2c *i2c)
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100317{
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530318 struct device_node *np = pdev->dev.of_node;
Andreas Larssona000b8c12012-11-15 16:50:59 +0100319 const struct of_device_id *match;
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530320 u32 val;
Max Filippov3a33a852015-02-02 18:28:12 +0300321 u32 clock_frequency;
322 bool clock_frequency_present;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100323
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530324 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
325 /* no 'reg-shift', check for deprecated 'regstep' */
326 if (!of_property_read_u32(np, "regstep", &val)) {
327 if (!is_power_of_2(val)) {
328 dev_err(&pdev->dev, "invalid regstep %d\n",
329 val);
330 return -EINVAL;
331 }
332 i2c->reg_shift = ilog2(val);
333 dev_warn(&pdev->dev,
334 "regstep property deprecated, use reg-shift\n");
335 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100336 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100337
Max Filippov3a33a852015-02-02 18:28:12 +0300338 clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
339 &clock_frequency);
340 i2c->bus_clock_khz = 100;
341
342 if (of_property_read_u32(np, "opencores,ip-clock-frequency", &val)) {
343 if (!clock_frequency_present) {
344 dev_err(&pdev->dev,
345 "Missing required parameter 'opencores,ip-clock-frequency'\n");
346 return -ENODEV;
347 }
348 i2c->ip_clock_khz = clock_frequency / 1000;
349 dev_warn(&pdev->dev,
350 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
351 } else {
352 i2c->ip_clock_khz = val / 1000;
353 if (clock_frequency_present)
354 i2c->bus_clock_khz = clock_frequency / 1000;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100355 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100356
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530357 of_property_read_u32(pdev->dev.of_node, "reg-io-width",
358 &i2c->reg_io_width);
Andreas Larssona000b8c12012-11-15 16:50:59 +0100359
360 match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
Jayachandran C6beaddf2013-02-18 21:33:19 +0000361 if (match && (long)match->data == TYPE_GRLIB) {
Andreas Larssona000b8c12012-11-15 16:50:59 +0100362 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
363 i2c->setreg = oc_setreg_grlib;
364 i2c->getreg = oc_getreg_grlib;
365 }
366
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100367 return 0;
368}
369#else
370#define ocores_i2c_of_probe(pdev,i2c) -ENODEV
371#endif
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200372
Bill Pemberton0b255e92012-11-27 15:59:38 -0500373static int ocores_i2c_probe(struct platform_device *pdev)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200374{
375 struct ocores_i2c *i2c;
376 struct ocores_i2c_platform_data *pdata;
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100377 struct resource *res;
378 int irq;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200379 int ret;
Richard Röjforsdd14be42009-06-05 15:40:32 +0200380 int i;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200381
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100382 irq = platform_get_irq(pdev, 0);
383 if (irq < 0)
384 return irq;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200385
Jonas Bonn47def5b2010-11-24 17:26:21 +0100386 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200387 if (!i2c)
388 return -ENOMEM;
389
Julia Lawallb7d12a82013-08-14 11:11:27 +0200390 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100391 i2c->base = devm_ioremap_resource(&pdev->dev, res);
392 if (IS_ERR(i2c->base))
393 return PTR_ERR(i2c->base);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200394
Jingoo Han6d4028c2013-07-30 16:59:33 +0900395 pdata = dev_get_platdata(&pdev->dev);
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100396 if (pdata) {
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530397 i2c->reg_shift = pdata->reg_shift;
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530398 i2c->reg_io_width = pdata->reg_io_width;
Max Filippov3a33a852015-02-02 18:28:12 +0300399 i2c->ip_clock_khz = pdata->clock_khz;
400 i2c->bus_clock_khz = 100;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100401 } else {
402 ret = ocores_i2c_of_probe(pdev, i2c);
403 if (ret)
404 return ret;
405 }
406
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530407 if (i2c->reg_io_width == 0)
408 i2c->reg_io_width = 1; /* Set to default value */
409
Andreas Larssona000b8c12012-11-15 16:50:59 +0100410 if (!i2c->setreg || !i2c->getreg) {
411 switch (i2c->reg_io_width) {
412 case 1:
413 i2c->setreg = oc_setreg_8;
414 i2c->getreg = oc_getreg_8;
415 break;
416
417 case 2:
418 i2c->setreg = oc_setreg_16;
419 i2c->getreg = oc_getreg_16;
420 break;
421
422 case 4:
423 i2c->setreg = oc_setreg_32;
424 i2c->getreg = oc_getreg_32;
425 break;
426
427 default:
428 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
429 i2c->reg_io_width);
430 return -EINVAL;
431 }
432 }
433
Max Filippov3a33a852015-02-02 18:28:12 +0300434 ret = ocores_init(&pdev->dev, i2c);
435 if (ret)
436 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200437
438 init_waitqueue_head(&i2c->wait);
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100439 ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
Jonas Bonn47def5b2010-11-24 17:26:21 +0100440 pdev->name, i2c);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200441 if (ret) {
442 dev_err(&pdev->dev, "Cannot claim IRQ\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100443 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200444 }
445
446 /* hook up driver to tree */
447 platform_set_drvdata(pdev, i2c);
448 i2c->adap = ocores_adapter;
449 i2c_set_adapdata(&i2c->adap, i2c);
450 i2c->adap.dev.parent = &pdev->dev;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100451 i2c->adap.dev.of_node = pdev->dev.of_node;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200452
453 /* add i2c adapter to i2c tree */
454 ret = i2c_add_adapter(&i2c->adap);
455 if (ret) {
456 dev_err(&pdev->dev, "Failed to add adapter\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100457 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200458 }
459
Richard Röjforsdd14be42009-06-05 15:40:32 +0200460 /* add in known devices to the bus */
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100461 if (pdata) {
462 for (i = 0; i < pdata->num_devices; i++)
463 i2c_new_device(&i2c->adap, pdata->devices + i);
464 }
Richard Röjforsdd14be42009-06-05 15:40:32 +0200465
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200466 return 0;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200467}
468
Bill Pemberton0b255e92012-11-27 15:59:38 -0500469static int ocores_i2c_remove(struct platform_device *pdev)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200470{
471 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200472
473 /* disable i2c logic */
474 oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
475 & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
476
477 /* remove adapter & data */
478 i2c_del_adapter(&i2c->adap);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200479
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200480 return 0;
481}
482
Jingoo Hanf076e912013-07-15 11:29:35 +0900483#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200484static int ocores_i2c_suspend(struct device *dev)
Manuel Lauss2373c182008-07-14 22:38:33 +0200485{
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200486 struct ocores_i2c *i2c = dev_get_drvdata(dev);
Manuel Lauss2373c182008-07-14 22:38:33 +0200487 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
488
489 /* make sure the device is disabled */
490 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
491
492 return 0;
493}
494
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200495static int ocores_i2c_resume(struct device *dev)
Manuel Lauss2373c182008-07-14 22:38:33 +0200496{
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200497 struct ocores_i2c *i2c = dev_get_drvdata(dev);
Manuel Lauss2373c182008-07-14 22:38:33 +0200498
Max Filippov3a33a852015-02-02 18:28:12 +0300499 return ocores_init(dev, i2c);
Manuel Lauss2373c182008-07-14 22:38:33 +0200500}
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200501
502static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
503#define OCORES_I2C_PM (&ocores_i2c_pm)
Manuel Lauss2373c182008-07-14 22:38:33 +0200504#else
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200505#define OCORES_I2C_PM NULL
Manuel Lauss2373c182008-07-14 22:38:33 +0200506#endif
507
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200508static struct platform_driver ocores_i2c_driver = {
Manuel Lauss2373c182008-07-14 22:38:33 +0200509 .probe = ocores_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500510 .remove = ocores_i2c_remove,
Manuel Lauss2373c182008-07-14 22:38:33 +0200511 .driver = {
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200512 .name = "ocores-i2c",
Grant Likelyc9e358d2011-01-21 09:24:48 -0700513 .of_match_table = ocores_i2c_match,
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200514 .pm = OCORES_I2C_PM,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200515 },
516};
517
Axel Lina3664b52012-01-12 20:32:04 +0100518module_platform_driver(ocores_i2c_driver);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200519
520MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
521MODULE_DESCRIPTION("OpenCores I2C bus driver");
522MODULE_LICENSE("GPL");
Axel Lina3664b52012-01-12 20:32:04 +0100523MODULE_ALIAS("platform:ocores-i2c");