aboutsummaryrefslogtreecommitdiff
path: root/drivers/i2c/busses/i2c-s6000.c
blob: cadc0216e02f9837448a31211453a8c8e9bf76ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * drivers/i2c/busses/i2c-s6000.c
 *
 * Description: Driver for S6000 Family I2C Interface
 * Copyright (c) 2008 emlix GmbH
 * Author:	Oskar Schirmer <os@emlix.com>
 *
 * Partially based on i2c-bfin-twi.c driver by <sonic.zhang@analog.com>
 * Copyright (c) 2005-2007 Analog Devices, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <linux/clk.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/i2c/s6000.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/io.h>

#include "i2c-s6000.h"

#define DRV_NAME "i2c-s6000"

#define POLL_TIMEOUT	(2 * HZ)

struct s6i2c_if {
	u8 __iomem		*reg; /* memory mapped registers */
	int			irq;
	spinlock_t		lock;
	struct i2c_msg		*msgs; /* messages currently handled */
	int			msgs_num; /* nb of msgs to do */
	int			msgs_push; /* nb of msgs read/written */
	int			msgs_done; /* nb of msgs finally handled */
	unsigned		push; /* nb of bytes read/written in msg */
	unsigned		done; /* nb of bytes finally handled */
	int			timeout_count; /* timeout retries left */
	struct timer_list	timeout_timer;
	struct i2c_adapter	adap;
	struct completion	complete;
	struct clk		*clk;
	struct resource		*res;
};

static inline u16 i2c_rd16(struct s6i2c_if *iface, unsigned n)
{
	return readw(iface->reg + (n));
}

static inline void i2c_wr16(struct s6i2c_if *iface, unsigned n, u16 v)
{
	writew(v, iface->reg + (n));
}

static inline u32 i2c_rd32(struct s6i2c_if *iface, unsigned n)
{
	return readl(iface->reg + (n));
}

static inline void i2c_wr32(struct s6i2c_if *iface, unsigned n, u32 v)
{
	writel(v, iface->reg + (n));
}

static struct s6i2c_if s6i2c_if;

static void s6i2c_handle_interrupt(struct s6i2c_if *iface)
{
	if (i2c_rd16(iface, S6_I2C_INTRSTAT) & (1 << S6_I2C_INTR_TXABRT)) {
		i2c_rd16(iface, S6_I2C_CLRTXABRT);
		i2c_wr16(iface, S6_I2C_INTRMASK, 0);
		complete(&iface->complete);
		return;
	}
	if (iface->msgs_done >= iface->msgs_num) {
		dev_err(&iface->adap.dev, "s6i2c: spurious I2C irq: %04x\n",
			i2c_rd16(iface, S6_I2C_INTRSTAT));
		i2c_wr16(iface, S6_I2C_INTRMASK, 0);
		return;
	}
	while ((iface->msgs_push < iface->msgs_num)
	    && (i2c_rd16(iface, S6_I2C_STATUS) & (1 << S6_I2C_STATUS_TFNF))) {
		struct i2c_msg *m = &iface->msgs[iface->msgs_push];
		if (!(m->flags & I2C_M_RD))
			i2c_wr16(iface, S6_I2C_DATACMD, m->buf[iface->push]);
		else
			i2c_wr16(iface, S6_I2C_DATACMD,
				 1 << S6_I2C_DATACMD_READ);
		if (++iface->push >= m->len) {
			iface->push = 0;
			iface->msgs_push += 1;
		}
	}
	do {
		struct i2c_msg *m = &iface->msgs[iface->msgs_done];
		if (!(m->flags & I2C_M_RD)) {
			if (iface->msgs_done < iface->msgs_push)
				iface->msgs_done += 1;
			else
				break;
		} else if (i2c_rd16(iface, S6_I2C_STATUS)
				& (1 << S6_I2C_STATUS_RFNE)) {
			m->buf[iface->done] = i2c_rd16(iface, S6_I2C_DATACMD);
			if (++iface->done >= m->len) {
				iface->done = 0;
				iface->msgs_done += 1;
			}
		} else{
			break;
		}
	} while (iface->msgs_done < iface->msgs_num);
	if (iface->msgs_done >= iface->msgs_num) {
		i2c_wr16(iface, S6_I2C_INTRMASK, 1 << S6_I2C_INTR_TXABRT);
		complete(&iface->complete);
	} else if (iface->msgs_push >= iface->msgs_num) {
		i2c_wr16(iface, S6_I2C_INTRMASK, (1 << S6_I2C_INTR_TXABRT) |
						 (1 << S6_I2C_INTR_RXFULL));
	} else {
		i2c_wr16(iface, S6_I2C_INTRMASK, (1 << S6_I2C_INTR_TXABRT) |
						 (1 << S6_I2C_INTR_TXEMPTY) |
						 (1 << S6_I2C_INTR_RXFULL));
	}
}

static irqreturn_t s6i2c_interrupt_entry(int irq, void *dev_id)
{
	struct s6i2c_if *iface = dev_id;
	if (!(i2c_rd16(iface, S6_I2C_STATUS) & ((1 << S6_I2C_INTR_RXUNDER)
					      | (1 << S6_I2C_INTR_RXOVER)
					      | (1 << S6_I2C_INTR_RXFULL)
					      | (1 << S6_I2C_INTR_TXOVER)
					      | (1 << S6_I2C_INTR_TXEMPTY)
					      | (1 << S6_I2C_INTR_RDREQ)
					      | (1 << S6_I2C_INTR_TXABRT)
					      | (1 << S6_I2C_INTR_RXDONE)
					      | (1 << S6_I2C_INTR_ACTIVITY)
					      | (1 << S6_I2C_INTR_STOPDET)
					      | (1 << S6_I2C_INTR_STARTDET)
					      | (1 << S6_I2C_INTR_GENCALL))))
		return IRQ_NONE;

	spin_lock(&iface->lock);
	del_timer(&iface->timeout_timer);
	s6i2c_handle_interrupt(iface);
	spin_unlock(&iface->lock);
	return IRQ_HANDLED;
}

static void s6i2c_timeout(unsigned long data)
{
	struct s6i2c_if *iface = (struct s6i2c_if *)data;
	unsigned long flags;

	spin_lock_irqsave(&iface->lock, flags);
	s6i2c_handle_interrupt(iface);
	if (--iface->timeout_count > 0) {
		iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
		add_timer(&iface->timeout_timer);
	} else {
		complete(&iface->complete);
		i2c_wr16(iface, S6_I2C_INTRMASK, 0);
	}
	spin_unlock_irqrestore(&iface->lock, flags);
}

static int s6i2c_master_xfer(struct i2c_adapter *adap,
				struct i2c_msg *msgs, int num)
{
	struct s6i2c_if *iface = adap->algo_data;
	int i;
	if (num == 0)
		return 0;
	if (i2c_rd16(iface, S6_I2C_STATUS) & (1 << S6_I2C_STATUS_ACTIVITY))
		yield();
	i2c_wr16(iface, S6_I2C_INTRMASK, 0);
	i2c_rd16(iface, S6_I2C_CLRINTR);
	for (i = 0; i < num; i++) {
		if (msgs[i].flags & I2C_M_TEN) {
			dev_err(&adap->dev,
				"s6i2c: 10 bits addr not supported\n");
			return -EINVAL;
		}
		if (msgs[i].len == 0) {
			dev_err(&adap->dev,
				"s6i2c: zero length message not supported\n");
			return -EINVAL;
		}
		if (msgs[i].addr != msgs[0].addr) {
			dev_err(&adap->dev,
				"s6i2c: multiple xfer cannot change target\n");
			return -EINVAL;
		}
	}

	iface->msgs = msgs;
	iface->msgs_num = num;
	iface->msgs_push = 0;
	iface->msgs_done = 0;
	iface->push = 0;
	iface->done = 0;
	iface->timeout_count = 10;
	i2c_wr16(iface, S6_I2C_TAR, msgs[0].addr);
	i2c_wr16(iface, S6_I2C_ENABLE, 1);
	i2c_wr16(iface, S6_I2C_INTRMASK, (1 << S6_I2C_INTR_TXEMPTY) |
					 (1 << S6_I2C_INTR_TXABRT));

	iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
	add_timer(&iface->timeout_timer);
	wait_for_completion(&iface->complete);
	del_timer_sync(&iface->timeout_timer);
	while (i2c_rd32(iface, S6_I2C_TXFLR) > 0)
		schedule();
	while (i2c_rd16(iface, S6_I2C_STATUS) & (1 << S6_I2C_STATUS_ACTIVITY))
		schedule();

	i2c_wr16(iface, S6_I2C_INTRMASK, 0);
	i2c_wr16(iface, S6_I2C_ENABLE, 0);
	return iface->msgs_done;
}

static u32 s6i2c_functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

static struct i2c_algorithm s6i2c_algorithm = {
	.master_xfer   = s6i2c_master_xfer,
	.functionality = s6i2c_functionality,
};

static u16 __devinit nanoseconds_on_clk(struct s6i2c_if *iface, u32 ns)
{
	u32 dividend = ((clk_get_rate(iface->clk) / 1000) * ns) / 1000000;
	if (dividend > 0xffff)
		return 0xffff;
	return dividend;
}

static int __devinit s6i2c_probe(struct platform_device *dev)
{
	struct s6i2c_if *iface = &s6i2c_if;
	struct i2c_adapter *p_adap;
	const char *clock;
	int bus_num, rc;
	spin_lock_init(&iface->lock);
	init_completion(&iface->complete);
	iface->irq = platform_get_irq(dev, 0);
	if (iface->irq < 0) {
		rc = iface->irq;
		goto err_out;
	}
	iface->res = platform_get_resource(dev, IORESOURCE_MEM, 0);
	if (!iface->res) {
		rc = -ENXIO;
		goto err_out;
	}
	iface->res = request_mem_region(iface->res->start,
					resource_size(iface->res),
					dev->dev.bus_id);
	if (!iface->res) {
		rc = -EBUSY;
		goto err_out;
	}
	iface->reg = ioremap_nocache(iface->res->start,
				     resource_size(iface->res));
	if (!iface->reg) {
		rc = -ENOMEM;
		goto err_reg;
	}

	clock = 0;
	bus_num = -1;
	if (dev->dev.platform_data) {
		struct s6_i2c_platform_data *pdata = dev->dev.platform_data;
		bus_num = pdata->bus_num;
		clock = pdata->clock;
	}
	iface->clk = clk_get(&dev->dev, clock);
	if (IS_ERR(iface->clk)) {
		rc = PTR_ERR(iface->clk);
		goto err_map;
	}
	rc = clk_enable(iface->clk);
	if (rc < 0)
		goto err_clk_put;
	init_timer(&iface->timeout_timer);
	iface->timeout_timer.function = s6i2c_timeout;
	iface->timeout_timer.data = (unsigned long)iface;

	p_adap = &iface->adap;
	strlcpy(p_adap->name, dev->name, sizeof(p_adap->name));
	p_adap->algo = &s6i2c_algorithm;
	p_adap->algo_data = iface;
	p_adap->nr = bus_num;
	p_adap->class = 0;
	p_adap->dev.parent = &dev->dev;
	i2c_wr16(iface, S6_I2C_INTRMASK, 0);
	rc = request_irq(iface->irq, s6i2c_interrupt_entry,
			 IRQF_SHARED, dev->name, iface);
	if (rc) {
		dev_err(&p_adap->dev, "s6i2c: cant get IRQ %d\n", iface->irq);
		goto err_clk_dis;
	}

	i2c_wr16(iface, S6_I2C_ENABLE, 0);
	udelay(1);
	i2c_wr32(iface, S6_I2C_SRESET, 1 << S6_I2C_SRESET_IC_SRST);
	i2c_wr16(iface, S6_I2C_CLRTXABRT, 1);
	i2c_wr16(iface, S6_I2C_CON,
			(1 << S6_I2C_CON_MASTER) |
			(S6_I2C_CON_SPEED_NORMAL << S6_I2C_CON_SPEED) |
			(0 << S6_I2C_CON_10BITSLAVE) |
			(0 << S6_I2C_CON_10BITMASTER) |
			(1 << S6_I2C_CON_RESTARTENA) |
			(1 << S6_I2C_CON_SLAVEDISABLE));
	i2c_wr16(iface, S6_I2C_SSHCNT, nanoseconds_on_clk(iface, 4000));
	i2c_wr16(iface, S6_I2C_SSLCNT, nanoseconds_on_clk(iface, 4700));
	i2c_wr16(iface, S6_I2C_FSHCNT, nanoseconds_on_clk(iface, 600));
	i2c_wr16(iface, S6_I2C_FSLCNT, nanoseconds_on_clk(iface, 1300));
	i2c_wr16(iface, S6_I2C_RXTL, 0);
	i2c_wr16(iface, S6_I2C_TXTL, 0);

	platform_set_drvdata(dev, iface);
	if (bus_num < 0)
		rc = i2c_add_adapter(p_adap);
	else
		rc = i2c_add_numbered_adapter(p_adap);
	if (rc)
		goto err_irq_free;
	return 0;

err_irq_free:
	free_irq(iface->irq, iface);
err_clk_dis:
	clk_disable(iface->clk);
err_clk_put:
	clk_put(iface->clk);
err_map:
	iounmap(iface->reg);
err_reg:
	release_mem_region(iface->res->start,
			   resource_size(iface->res));
err_out:
	return rc;
}

static int __devexit s6i2c_remove(struct platform_device *pdev)
{
	struct s6i2c_if *iface = platform_get_drvdata(pdev);
	i2c_wr16(iface, S6_I2C_ENABLE, 0);
	platform_set_drvdata(pdev, NULL);
	i2c_del_adapter(&iface->adap);
	free_irq(iface->irq, iface);
	clk_disable(iface->clk);
	clk_put(iface->clk);
	iounmap(iface->reg);
	release_mem_region(iface->res->start,
			   resource_size(iface->res));
	return 0;
}

static struct platform_driver s6i2c_driver = {
	.probe		= s6i2c_probe,
	.remove		= __devexit_p(s6i2c_remove),
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init s6i2c_init(void)
{
	pr_info("I2C: S6000 I2C driver\n");
	return platform_driver_register(&s6i2c_driver);
}

static void __exit s6i2c_exit(void)
{
	platform_driver_unregister(&s6i2c_driver);
}

MODULE_DESCRIPTION("I2C-Bus adapter routines for S6000 I2C");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);

subsys_initcall(s6i2c_init);
module_exit(s6i2c_exit);