blob: c214e29cb19bb24831b244aedd164e9de99db36f [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
Max Filippove961a092015-02-05 22:55:01 +030015#include <linux/clk.h>
Thierry Reding84dbf802013-01-21 11:09:03 +010016#include <linux/err.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020017#include <linux/kernel.h>
18#include <linux/module.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020019#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>
Wolfram Sang985ecf02018-04-19 22:00:09 +020024#include <linux/platform_data/i2c-ocores.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020026#include <linux/io.h>
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +053027#include <linux/log2.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020028
29struct ocores_i2c {
30 void __iomem *base;
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +053031 u32 reg_shift;
Ganesan Ramalingam7326e382012-07-13 19:14:25 +053032 u32 reg_io_width;
Peter Korsgaard18f98b12006-06-04 20:01:08 +020033 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 Filippove961a092015-02-05 22:55:01 +030039 struct clk *clk;
Max Filippov3a33a852015-02-02 18:28:12 +030040 int ip_clock_khz;
41 int bus_clock_khz;
Andreas Larssona000b8c12012-11-15 16:50:59 +010042 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
43 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
Peter Korsgaard18f98b12006-06-04 20:01:08 +020044};
45
46/* registers */
47#define OCI2C_PRELOW 0
48#define OCI2C_PREHIGH 1
49#define OCI2C_CONTROL 2
50#define OCI2C_DATA 3
Peter Korsgaard1ded9692006-06-12 21:40:53 +020051#define OCI2C_CMD 4 /* write only */
52#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
Peter Korsgaard18f98b12006-06-04 20:01:08 +020053
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 Larssona000b8c12012-11-15 16:50:59 +010077#define TYPE_OCORES 0
78#define TYPE_GRLIB 1
79
80static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
81{
82 iowrite8(value, i2c->base + (reg << i2c->reg_shift));
83}
84
85static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
86{
87 iowrite16(value, i2c->base + (reg << i2c->reg_shift));
88}
89
90static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
91{
92 iowrite32(value, i2c->base + (reg << i2c->reg_shift));
93}
94
Max Filippovb2991672015-10-07 02:45:11 +030095static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value)
96{
97 iowrite16be(value, i2c->base + (reg << i2c->reg_shift));
98}
99
100static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value)
101{
102 iowrite32be(value, i2c->base + (reg << i2c->reg_shift));
103}
104
Andreas Larssona000b8c12012-11-15 16:50:59 +0100105static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
106{
107 return ioread8(i2c->base + (reg << i2c->reg_shift));
108}
109
110static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
111{
112 return ioread16(i2c->base + (reg << i2c->reg_shift));
113}
114
115static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
116{
117 return ioread32(i2c->base + (reg << i2c->reg_shift));
118}
119
Max Filippovb2991672015-10-07 02:45:11 +0300120static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg)
121{
122 return ioread16be(i2c->base + (reg << i2c->reg_shift));
123}
124
125static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg)
126{
127 return ioread32be(i2c->base + (reg << i2c->reg_shift));
128}
129
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200130static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
131{
Andreas Larssona000b8c12012-11-15 16:50:59 +0100132 i2c->setreg(i2c, reg, value);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200133}
134
135static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
136{
Andreas Larssona000b8c12012-11-15 16:50:59 +0100137 return i2c->getreg(i2c, reg);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200138}
139
140static void ocores_process(struct ocores_i2c *i2c)
141{
142 struct i2c_msg *msg = i2c->msg;
143 u8 stat = oc_getreg(i2c, OCI2C_STATUS);
144
145 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
146 /* stop has been sent */
147 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
148 wake_up(&i2c->wait);
149 return;
150 }
151
152 /* error? */
153 if (stat & OCI2C_STAT_ARBLOST) {
154 i2c->state = STATE_ERROR;
155 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
156 return;
157 }
158
159 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
160 i2c->state =
161 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
162
163 if (stat & OCI2C_STAT_NACK) {
164 i2c->state = STATE_ERROR;
165 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
166 return;
167 }
168 } else
169 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
170
171 /* end of msg? */
172 if (i2c->pos == msg->len) {
173 i2c->nmsgs--;
174 i2c->msg++;
175 i2c->pos = 0;
176 msg = i2c->msg;
177
178 if (i2c->nmsgs) { /* end? */
179 /* send start? */
180 if (!(msg->flags & I2C_M_NOSTART)) {
Wolfram Sang380a2952016-04-03 20:44:56 +0200181 u8 addr = i2c_8bit_addr_from_msg(msg);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200182
183 i2c->state = STATE_START;
184
185 oc_setreg(i2c, OCI2C_DATA, addr);
186 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
187 return;
188 } else
189 i2c->state = (msg->flags & I2C_M_RD)
190 ? STATE_READ : STATE_WRITE;
191 } else {
192 i2c->state = STATE_DONE;
193 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
194 return;
195 }
196 }
197
198 if (i2c->state == STATE_READ) {
199 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
200 OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
201 } else {
202 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
203 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
204 }
205}
206
David Howells7d12e782006-10-05 14:55:46 +0100207static irqreturn_t ocores_isr(int irq, void *dev_id)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200208{
209 struct ocores_i2c *i2c = dev_id;
210
211 ocores_process(i2c);
212
213 return IRQ_HANDLED;
214}
215
216static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
217{
218 struct ocores_i2c *i2c = i2c_get_adapdata(adap);
219
220 i2c->msg = msgs;
221 i2c->pos = 0;
222 i2c->nmsgs = num;
223 i2c->state = STATE_START;
224
Peter Rosin30a64752018-05-16 09:16:47 +0200225 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200226 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
227
228 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
229 (i2c->state == STATE_DONE), HZ))
230 return (i2c->state == STATE_DONE) ? num : -EIO;
231 else
232 return -ETIMEDOUT;
233}
234
Max Filippov3a33a852015-02-02 18:28:12 +0300235static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200236{
237 int prescale;
Max Filippov3a33a852015-02-02 18:28:12 +0300238 int diff;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200239 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
240
241 /* make sure the device is disabled */
242 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
243
Max Filippov3a33a852015-02-02 18:28:12 +0300244 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
245 prescale = clamp(prescale, 0, 0xffff);
246
247 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
248 if (abs(diff) > i2c->bus_clock_khz / 10) {
249 dev_err(dev,
250 "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
251 i2c->ip_clock_khz, i2c->bus_clock_khz);
252 return -EINVAL;
253 }
254
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200255 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
256 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
257
258 /* Init the device */
259 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
260 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
Max Filippov3a33a852015-02-02 18:28:12 +0300261
262 return 0;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200263}
264
265
266static u32 ocores_func(struct i2c_adapter *adap)
267{
268 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
269}
270
Jean Delvare8f9082c2006-09-03 22:39:46 +0200271static const struct i2c_algorithm ocores_algorithm = {
Wolfram Sang1ce97e02014-07-10 13:46:29 +0200272 .master_xfer = ocores_xfer,
273 .functionality = ocores_func,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200274};
275
Bhumika Goyal329430c2017-08-19 16:04:12 +0530276static const struct i2c_adapter ocores_adapter = {
Wolfram Sang1ce97e02014-07-10 13:46:29 +0200277 .owner = THIS_MODULE,
278 .name = "i2c-ocores",
279 .class = I2C_CLASS_DEPRECATED,
280 .algo = &ocores_algorithm,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200281};
282
Jingoo Haneae45e52014-05-15 15:46:11 +0900283static const struct of_device_id ocores_i2c_match[] = {
Andreas Larssona000b8c12012-11-15 16:50:59 +0100284 {
285 .compatible = "opencores,i2c-ocores",
286 .data = (void *)TYPE_OCORES,
287 },
288 {
289 .compatible = "aeroflexgaisler,i2cmst",
290 .data = (void *)TYPE_GRLIB,
291 },
292 {},
293};
294MODULE_DEVICE_TABLE(of, ocores_i2c_match);
295
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100296#ifdef CONFIG_OF
Andreas Larssonc5d54742012-11-19 13:17:48 +0100297/* Read and write functions for the GRLIB port of the controller. Registers are
298 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
299 * register. The subsequent registers has their offset decreased accordingly. */
300static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
301{
302 u32 rd;
303 int rreg = reg;
304 if (reg != OCI2C_PRELOW)
305 rreg--;
306 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
307 if (reg == OCI2C_PREHIGH)
308 return (u8)(rd >> 8);
309 else
310 return (u8)rd;
311}
312
313static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
314{
315 u32 curr, wr;
316 int rreg = reg;
317 if (reg != OCI2C_PRELOW)
318 rreg--;
319 if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
320 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
321 if (reg == OCI2C_PRELOW)
322 wr = (curr & 0xff00) | value;
323 else
324 wr = (((u32)value) << 8) | (curr & 0xff);
325 } else {
326 wr = value;
327 }
328 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
329}
330
Jayachandran C9ae97a82012-07-13 19:14:22 +0530331static int ocores_i2c_of_probe(struct platform_device *pdev,
332 struct ocores_i2c *i2c)
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100333{
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530334 struct device_node *np = pdev->dev.of_node;
Andreas Larssona000b8c12012-11-15 16:50:59 +0100335 const struct of_device_id *match;
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530336 u32 val;
Max Filippov3a33a852015-02-02 18:28:12 +0300337 u32 clock_frequency;
338 bool clock_frequency_present;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100339
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530340 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
341 /* no 'reg-shift', check for deprecated 'regstep' */
342 if (!of_property_read_u32(np, "regstep", &val)) {
343 if (!is_power_of_2(val)) {
344 dev_err(&pdev->dev, "invalid regstep %d\n",
345 val);
346 return -EINVAL;
347 }
348 i2c->reg_shift = ilog2(val);
349 dev_warn(&pdev->dev,
350 "regstep property deprecated, use reg-shift\n");
351 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100352 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100353
Max Filippov3a33a852015-02-02 18:28:12 +0300354 clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
355 &clock_frequency);
356 i2c->bus_clock_khz = 100;
357
Max Filippove961a092015-02-05 22:55:01 +0300358 i2c->clk = devm_clk_get(&pdev->dev, NULL);
359
360 if (!IS_ERR(i2c->clk)) {
361 int ret = clk_prepare_enable(i2c->clk);
362
363 if (ret) {
364 dev_err(&pdev->dev,
365 "clk_prepare_enable failed: %d\n", ret);
366 return ret;
367 }
368 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000;
369 if (clock_frequency_present)
370 i2c->bus_clock_khz = clock_frequency / 1000;
Wolfram Sang0d8fb592015-02-19 17:22:34 +0100371 }
372
373 if (i2c->ip_clock_khz == 0) {
374 if (of_property_read_u32(np, "opencores,ip-clock-frequency",
375 &val)) {
376 if (!clock_frequency_present) {
377 dev_err(&pdev->dev,
378 "Missing required parameter 'opencores,ip-clock-frequency'\n");
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300379 clk_disable_unprepare(i2c->clk);
Wolfram Sang0d8fb592015-02-19 17:22:34 +0100380 return -ENODEV;
381 }
382 i2c->ip_clock_khz = clock_frequency / 1000;
383 dev_warn(&pdev->dev,
384 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
385 } else {
386 i2c->ip_clock_khz = val / 1000;
387 if (clock_frequency_present)
388 i2c->bus_clock_khz = clock_frequency / 1000;
Max Filippov3a33a852015-02-02 18:28:12 +0300389 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100390 }
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100391
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530392 of_property_read_u32(pdev->dev.of_node, "reg-io-width",
393 &i2c->reg_io_width);
Andreas Larssona000b8c12012-11-15 16:50:59 +0100394
395 match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
Jayachandran C6beaddf2013-02-18 21:33:19 +0000396 if (match && (long)match->data == TYPE_GRLIB) {
Andreas Larssona000b8c12012-11-15 16:50:59 +0100397 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
398 i2c->setreg = oc_setreg_grlib;
399 i2c->getreg = oc_getreg_grlib;
400 }
401
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100402 return 0;
403}
404#else
405#define ocores_i2c_of_probe(pdev,i2c) -ENODEV
406#endif
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200407
Bill Pemberton0b255e92012-11-27 15:59:38 -0500408static int ocores_i2c_probe(struct platform_device *pdev)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200409{
410 struct ocores_i2c *i2c;
411 struct ocores_i2c_platform_data *pdata;
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100412 struct resource *res;
413 int irq;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200414 int ret;
Richard Röjforsdd14be42009-06-05 15:40:32 +0200415 int i;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200416
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100417 irq = platform_get_irq(pdev, 0);
418 if (irq < 0)
419 return irq;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200420
Jonas Bonn47def5b2010-11-24 17:26:21 +0100421 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200422 if (!i2c)
423 return -ENOMEM;
424
Julia Lawallb7d12a82013-08-14 11:11:27 +0200425 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100426 i2c->base = devm_ioremap_resource(&pdev->dev, res);
427 if (IS_ERR(i2c->base))
428 return PTR_ERR(i2c->base);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200429
Jingoo Han6d4028c2013-07-30 16:59:33 +0900430 pdata = dev_get_platdata(&pdev->dev);
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100431 if (pdata) {
Ganesan Ramalingam8bb986a2012-07-13 19:14:23 +0530432 i2c->reg_shift = pdata->reg_shift;
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530433 i2c->reg_io_width = pdata->reg_io_width;
Max Filippov3a33a852015-02-02 18:28:12 +0300434 i2c->ip_clock_khz = pdata->clock_khz;
435 i2c->bus_clock_khz = 100;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100436 } else {
437 ret = ocores_i2c_of_probe(pdev, i2c);
438 if (ret)
439 return ret;
440 }
441
Ganesan Ramalingam7326e382012-07-13 19:14:25 +0530442 if (i2c->reg_io_width == 0)
443 i2c->reg_io_width = 1; /* Set to default value */
444
Andreas Larssona000b8c12012-11-15 16:50:59 +0100445 if (!i2c->setreg || !i2c->getreg) {
Max Filippovb2991672015-10-07 02:45:11 +0300446 bool be = pdata ? pdata->big_endian :
447 of_device_is_big_endian(pdev->dev.of_node);
448
Andreas Larssona000b8c12012-11-15 16:50:59 +0100449 switch (i2c->reg_io_width) {
450 case 1:
451 i2c->setreg = oc_setreg_8;
452 i2c->getreg = oc_getreg_8;
453 break;
454
455 case 2:
Max Filippovb2991672015-10-07 02:45:11 +0300456 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16;
457 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16;
Andreas Larssona000b8c12012-11-15 16:50:59 +0100458 break;
459
460 case 4:
Max Filippovb2991672015-10-07 02:45:11 +0300461 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32;
462 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32;
Andreas Larssona000b8c12012-11-15 16:50:59 +0100463 break;
464
465 default:
466 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
467 i2c->reg_io_width);
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300468 ret = -EINVAL;
469 goto err_clk;
Andreas Larssona000b8c12012-11-15 16:50:59 +0100470 }
471 }
472
Max Filippov3a33a852015-02-02 18:28:12 +0300473 ret = ocores_init(&pdev->dev, i2c);
474 if (ret)
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300475 goto err_clk;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200476
477 init_waitqueue_head(&i2c->wait);
Andreas Larssonf5f35a92012-11-15 16:50:58 +0100478 ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
Jonas Bonn47def5b2010-11-24 17:26:21 +0100479 pdev->name, i2c);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200480 if (ret) {
481 dev_err(&pdev->dev, "Cannot claim IRQ\n");
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300482 goto err_clk;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200483 }
484
485 /* hook up driver to tree */
486 platform_set_drvdata(pdev, i2c);
487 i2c->adap = ocores_adapter;
488 i2c_set_adapdata(&i2c->adap, i2c);
489 i2c->adap.dev.parent = &pdev->dev;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100490 i2c->adap.dev.of_node = pdev->dev.of_node;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200491
492 /* add i2c adapter to i2c tree */
493 ret = i2c_add_adapter(&i2c->adap);
Wolfram Sangea734402016-08-09 13:36:17 +0200494 if (ret)
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300495 goto err_clk;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200496
Richard Röjforsdd14be42009-06-05 15:40:32 +0200497 /* add in known devices to the bus */
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100498 if (pdata) {
499 for (i = 0; i < pdata->num_devices; i++)
500 i2c_new_device(&i2c->adap, pdata->devices + i);
501 }
Richard Röjforsdd14be42009-06-05 15:40:32 +0200502
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200503 return 0;
Alexey Khoroshilov97ccd4a2016-08-04 02:38:44 +0300504
505err_clk:
506 clk_disable_unprepare(i2c->clk);
507 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200508}
509
Bill Pemberton0b255e92012-11-27 15:59:38 -0500510static int ocores_i2c_remove(struct platform_device *pdev)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200511{
512 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200513
514 /* disable i2c logic */
515 oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
516 & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
517
518 /* remove adapter & data */
519 i2c_del_adapter(&i2c->adap);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200520
Max Filippove961a092015-02-05 22:55:01 +0300521 if (!IS_ERR(i2c->clk))
522 clk_disable_unprepare(i2c->clk);
523
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200524 return 0;
525}
526
Jingoo Hanf076e912013-07-15 11:29:35 +0900527#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200528static int ocores_i2c_suspend(struct device *dev)
Manuel Lauss2373c182008-07-14 22:38:33 +0200529{
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200530 struct ocores_i2c *i2c = dev_get_drvdata(dev);
Manuel Lauss2373c182008-07-14 22:38:33 +0200531 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
532
533 /* make sure the device is disabled */
534 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
535
Max Filippove961a092015-02-05 22:55:01 +0300536 if (!IS_ERR(i2c->clk))
537 clk_disable_unprepare(i2c->clk);
Manuel Lauss2373c182008-07-14 22:38:33 +0200538 return 0;
539}
540
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200541static int ocores_i2c_resume(struct device *dev)
Manuel Lauss2373c182008-07-14 22:38:33 +0200542{
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200543 struct ocores_i2c *i2c = dev_get_drvdata(dev);
Manuel Lauss2373c182008-07-14 22:38:33 +0200544
Max Filippove961a092015-02-05 22:55:01 +0300545 if (!IS_ERR(i2c->clk)) {
Wolfram Sang0d8fb592015-02-19 17:22:34 +0100546 unsigned long rate;
Max Filippove961a092015-02-05 22:55:01 +0300547 int ret = clk_prepare_enable(i2c->clk);
548
549 if (ret) {
550 dev_err(dev,
551 "clk_prepare_enable failed: %d\n", ret);
552 return ret;
553 }
Wolfram Sang0d8fb592015-02-19 17:22:34 +0100554 rate = clk_get_rate(i2c->clk) / 1000;
555 if (rate)
556 i2c->ip_clock_khz = rate;
Max Filippove961a092015-02-05 22:55:01 +0300557 }
Max Filippov3a33a852015-02-02 18:28:12 +0300558 return ocores_init(dev, i2c);
Manuel Lauss2373c182008-07-14 22:38:33 +0200559}
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200560
561static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
562#define OCORES_I2C_PM (&ocores_i2c_pm)
Manuel Lauss2373c182008-07-14 22:38:33 +0200563#else
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200564#define OCORES_I2C_PM NULL
Manuel Lauss2373c182008-07-14 22:38:33 +0200565#endif
566
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200567static struct platform_driver ocores_i2c_driver = {
Manuel Lauss2373c182008-07-14 22:38:33 +0200568 .probe = ocores_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500569 .remove = ocores_i2c_remove,
Manuel Lauss2373c182008-07-14 22:38:33 +0200570 .driver = {
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200571 .name = "ocores-i2c",
Grant Likelyc9e358d2011-01-21 09:24:48 -0700572 .of_match_table = ocores_i2c_match,
Rafael J. Wysocki84603c72012-07-11 21:24:15 +0200573 .pm = OCORES_I2C_PM,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200574 },
575};
576
Axel Lina3664b52012-01-12 20:32:04 +0100577module_platform_driver(ocores_i2c_driver);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200578
579MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
580MODULE_DESCRIPTION("OpenCores I2C bus driver");
581MODULE_LICENSE("GPL");
Axel Lina3664b52012-01-12 20:32:04 +0100582MODULE_ALIAS("platform:ocores-i2c");