Skip to content

Commit dc9424f

Browse files
author
Number 27
committed
forgotten files....
1 parent 6d0f2d3 commit dc9424f

2 files changed

Lines changed: 990 additions & 0 deletions

File tree

‎drivers/i2c/busses/i2c-bcm2708.c‎

Lines changed: 396 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,396 @@
1+
/*
2+
* Driver for Broadcom BCM2708 BSC Controllers
3+
*
4+
* Copyright (C) 2012 Chris Boot & Frank Buss
5+
*
6+
* This driver is inspired by:
7+
* i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22+
*/
23+
24+
#include <linux/kernel.h>
25+
#include <linux/module.h>
26+
#include <linux/spinlock.h>
27+
#include <linux/clk.h>
28+
#include <linux/err.h>
29+
#include <linux/platform_device.h>
30+
#include <linux/io.h>
31+
#include <linux/slab.h>
32+
#include <linux/i2c.h>
33+
#include <linux/interrupt.h>
34+
#include <linux/sched.h>
35+
#include <linux/wait.h>
36+
37+
/* BSC register offsets */
38+
#define BSC_C 0x00
39+
#define BSC_S 0x04
40+
#define BSC_DLEN 0x08
41+
#define BSC_A 0x0c
42+
#define BSC_FIFO 0x10
43+
#define BSC_DIV 0x14
44+
#define BSC_DEL 0x18
45+
#define BSC_CLKT 0x1c
46+
47+
/* Bitfields in BSC_C */
48+
#define BSC_C_I2CEN 0x00008000
49+
#define BSC_C_INTR 0x00000400
50+
#define BSC_C_INTT 0x00000200
51+
#define BSC_C_INTD 0x00000100
52+
#define BSC_C_ST 0x00000080
53+
#define BSC_C_CLEAR_1 0x00000020
54+
#define BSC_C_CLEAR_2 0x00000010
55+
#define BSC_C_READ 0x00000001
56+
57+
/* Bitfields in BSC_S */
58+
#define BSC_S_CLKT 0x00000200
59+
#define BSC_S_ERR 0x00000100
60+
#define BSC_S_RXF 0x00000080
61+
#define BSC_S_TXE 0x00000040
62+
#define BSC_S_RXD 0x00000020
63+
#define BSC_S_TXD 0x00000010
64+
#define BSC_S_RXR 0x00000008
65+
#define BSC_S_TXW 0x00000004
66+
#define BSC_S_DONE 0x00000002
67+
#define BSC_S_TA 0x00000001
68+
69+
#define I2C_CLOCK_HZ 100000 /* FIXME: get from DT */
70+
#define I2C_TIMEOUT_MS 150
71+
72+
#define DRV_NAME "bcm2708_i2c"
73+
74+
struct bcm2708_i2c {
75+
struct i2c_adapter adapter;
76+
77+
spinlock_t lock;
78+
void __iomem *base;
79+
int irq;
80+
struct clk *clk;
81+
82+
struct completion done;
83+
84+
struct i2c_msg *msg;
85+
int pos;
86+
int nmsgs;
87+
bool error;
88+
};
89+
90+
/*
91+
* This function sets the ALT mode on the I2C pins so that we can use them with
92+
* the BSC hardware.
93+
*
94+
* FIXME: This is a hack. Use pinmux / pinctrl.
95+
*/
96+
static void bcm2708_i2c_init_pinmode(void)
97+
{
98+
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
99+
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
100+
101+
int pin;
102+
u32 *gpio = ioremap(0x20200000, SZ_16K);
103+
104+
/* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
105+
for (pin = 0; pin <= 3; pin++) {
106+
INP_GPIO(pin); /* set mode to GPIO input first */
107+
SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
108+
}
109+
110+
iounmap(gpio);
111+
112+
#undef INP_GPIO
113+
#undef SET_GPIO_ALT
114+
}
115+
116+
static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
117+
{
118+
return readl(bi->base + reg);
119+
}
120+
121+
static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
122+
{
123+
writel(val, bi->base + reg);
124+
}
125+
126+
static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
127+
{
128+
bcm2708_wr(bi, BSC_C, 0);
129+
bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
130+
}
131+
132+
static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
133+
{
134+
while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
135+
bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
136+
}
137+
138+
static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
139+
{
140+
while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
141+
bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
142+
}
143+
144+
static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
145+
{
146+
unsigned long bus_hz;
147+
u32 cdiv;
148+
u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
149+
150+
bus_hz = clk_get_rate(bi->clk);
151+
cdiv = bus_hz / I2C_CLOCK_HZ;
152+
153+
if (bi->msg->flags & I2C_M_RD)
154+
c |= BSC_C_INTR | BSC_C_READ;
155+
else
156+
c |= BSC_C_INTT;
157+
158+
bcm2708_wr(bi, BSC_DIV, cdiv);
159+
bcm2708_wr(bi, BSC_A, bi->msg->addr);
160+
bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
161+
bcm2708_wr(bi, BSC_C, c);
162+
}
163+
164+
static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
165+
{
166+
struct bcm2708_i2c *bi = dev_id;
167+
bool handled = true;
168+
u32 s;
169+
170+
spin_lock(&bi->lock);
171+
172+
s = bcm2708_rd(bi, BSC_S);
173+
174+
if (s & (BSC_S_CLKT | BSC_S_ERR)) {
175+
bcm2708_bsc_reset(bi);
176+
bi->error = true;
177+
178+
/* wake up our bh */
179+
complete(&bi->done);
180+
} else if (s & BSC_S_DONE) {
181+
bi->nmsgs--;
182+
183+
if (bi->msg->flags & I2C_M_RD)
184+
bcm2708_bsc_fifo_drain(bi);
185+
186+
bcm2708_bsc_reset(bi);
187+
188+
if (bi->nmsgs) {
189+
/* advance to next message */
190+
bi->msg++;
191+
bi->pos = 0;
192+
bcm2708_bsc_setup(bi);
193+
} else {
194+
/* wake up our bh */
195+
complete(&bi->done);
196+
}
197+
} else if (s & BSC_S_TXW) {
198+
bcm2708_bsc_fifo_fill(bi);
199+
} else if (s & BSC_S_RXR) {
200+
bcm2708_bsc_fifo_drain(bi);
201+
} else {
202+
handled = false;
203+
}
204+
205+
spin_unlock(&bi->lock);
206+
207+
return handled ? IRQ_HANDLED : IRQ_NONE;
208+
}
209+
210+
static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
211+
struct i2c_msg *msgs, int num)
212+
{
213+
struct bcm2708_i2c *bi = adap->algo_data;
214+
unsigned long flags;
215+
int ret;
216+
217+
spin_lock_irqsave(&bi->lock, flags);
218+
219+
INIT_COMPLETION(bi->done);
220+
bi->msg = msgs;
221+
bi->pos = 0;
222+
bi->nmsgs = num;
223+
bi->error = false;
224+
225+
spin_unlock_irqrestore(&bi->lock, flags);
226+
227+
bcm2708_bsc_setup(bi);
228+
229+
ret = wait_for_completion_timeout(&bi->done,
230+
msecs_to_jiffies(I2C_TIMEOUT_MS));
231+
if (ret == 0) {
232+
dev_err(&adap->dev, "transfer timed out\n");
233+
spin_lock_irqsave(&bi->lock, flags);
234+
bcm2708_bsc_reset(bi);
235+
spin_unlock_irqrestore(&bi->lock, flags);
236+
return -ETIMEDOUT;
237+
}
238+
239+
return bi->error ? -EIO : num;
240+
}
241+
242+
static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
243+
{
244+
return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
245+
}
246+
247+
static struct i2c_algorithm bcm2708_i2c_algorithm = {
248+
.master_xfer = bcm2708_i2c_master_xfer,
249+
.functionality = bcm2708_i2c_functionality,
250+
};
251+
252+
static int __devinit bcm2708_i2c_probe(struct platform_device *pdev)
253+
{
254+
struct resource *regs;
255+
int irq, err = -ENOMEM;
256+
struct clk *clk;
257+
struct bcm2708_i2c *bi;
258+
struct i2c_adapter *adap;
259+
260+
regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
261+
if (!regs) {
262+
dev_err(&pdev->dev, "could not get IO memory\n");
263+
return -ENXIO;
264+
}
265+
266+
irq = platform_get_irq(pdev, 0);
267+
if (irq < 0) {
268+
dev_err(&pdev->dev, "could not get IRQ\n");
269+
return irq;
270+
}
271+
272+
clk = clk_get(&pdev->dev, NULL);
273+
if (IS_ERR(clk)) {
274+
dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
275+
return PTR_ERR(clk);
276+
}
277+
278+
bcm2708_i2c_init_pinmode();
279+
280+
bi = kzalloc(sizeof(*bi), GFP_KERNEL);
281+
if (!bi)
282+
goto out_clk_put;
283+
284+
platform_set_drvdata(pdev, bi);
285+
286+
adap = &bi->adapter;
287+
adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
288+
adap->algo = &bcm2708_i2c_algorithm;
289+
adap->algo_data = bi;
290+
adap->dev.parent = &pdev->dev;
291+
adap->nr = pdev->id;
292+
strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
293+
294+
switch (pdev->id) {
295+
case 0:
296+
adap->class = I2C_CLASS_HWMON;
297+
break;
298+
case 1:
299+
adap->class = I2C_CLASS_DDC;
300+
break;
301+
default:
302+
dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
303+
err = -ENXIO;
304+
goto out_free_bi;
305+
}
306+
307+
spin_lock_init(&bi->lock);
308+
init_completion(&bi->done);
309+
310+
bi->base = ioremap(regs->start, resource_size(regs));
311+
if (!bi->base) {
312+
dev_err(&pdev->dev, "could not remap memory\n");
313+
goto out_free_bi;
314+
}
315+
316+
bi->irq = irq;
317+
bi->clk = clk;
318+
319+
err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
320+
dev_name(&pdev->dev), bi);
321+
if (err) {
322+
dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
323+
goto out_iounmap;
324+
}
325+
326+
bcm2708_bsc_reset(bi);
327+
328+
err = i2c_add_numbered_adapter(adap);
329+
if (err < 0) {
330+
dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
331+
goto out_free_irq;
332+
}
333+
334+
dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d)\n",
335+
pdev->id, (unsigned long)regs->start, irq);
336+
337+
return 0;
338+
339+
out_free_irq:
340+
free_irq(bi->irq, bi);
341+
out_iounmap:
342+
iounmap(bi->base);
343+
out_free_bi:
344+
kfree(bi);
345+
out_clk_put:
346+
clk_put(clk);
347+
return err;
348+
}
349+
350+
static int __devexit bcm2708_i2c_remove(struct platform_device *pdev)
351+
{
352+
struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
353+
354+
platform_set_drvdata(pdev, NULL);
355+
356+
i2c_del_adapter(&bi->adapter);
357+
free_irq(bi->irq, bi);
358+
iounmap(bi->base);
359+
clk_disable(bi->clk);
360+
clk_put(bi->clk);
361+
kfree(bi);
362+
363+
return 0;
364+
}
365+
366+
static struct platform_driver bcm2708_i2c_driver = {
367+
.driver = {
368+
.name = DRV_NAME,
369+
.owner = THIS_MODULE,
370+
},
371+
.probe = bcm2708_i2c_probe,
372+
.remove = __devexit_p(bcm2708_i2c_remove),
373+
};
374+
375+
// module_platform_driver(bcm2708_i2c_driver);
376+
377+
378+
static int __init bcm2708_i2c_init(void)
379+
{
380+
return platform_driver_register(&bcm2708_i2c_driver);
381+
}
382+
383+
static void __exit bcm2708_i2c_exit(void)
384+
{
385+
platform_driver_unregister(&bcm2708_i2c_driver);
386+
}
387+
388+
module_init(bcm2708_i2c_init);
389+
module_exit(bcm2708_i2c_exit);
390+
391+
392+
393+
MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
394+
MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
395+
MODULE_LICENSE("GPL v2");
396+
MODULE_ALIAS("platform:" DRV_NAME);

0 commit comments

Comments
 (0)