blob: 7d51a43b38d3d5f1f97ab305141079cd4eb588c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
3 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
4 *
5 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
6 *
7 * The documentation describes this as an SMBus controller, but it doesn't
8 * understand any of the SMBus protocol in hardware. It's really an I2C
9 * controller that could emulate most of the SMBus in software.
10 *
11 * This is just a skeleton adapter to use with the Au1550 PSC
12 * algorithm. It was developed for the Pb1550, but will work with
13 * any Au1550 board that has a similar PSC configuration.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/errno.h>
35#include <linux/i2c.h>
36
Domen Puncera294de42006-08-13 23:37:13 +020037#include <asm/mach-au1x00/au1xxx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/mach-au1x00/au1xxx_psc.h>
39
40#include "i2c-au1550.h"
41
42static int
43wait_xfer_done(struct i2c_au1550_data *adap)
44{
45 u32 stat;
46 int i;
47 volatile psc_smb_t *sp;
48
49 sp = (volatile psc_smb_t *)(adap->psc_base);
50
Chris Davida2027072007-10-13 23:56:33 +020051 /* Wait for Tx Buffer Empty
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53 for (i = 0; i < adap->xfer_timeout; i++) {
Chris Davida2027072007-10-13 23:56:33 +020054 stat = sp->psc_smbstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 au_sync();
Chris Davida2027072007-10-13 23:56:33 +020056 if ((stat & PSC_SMBSTAT_TE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return 0;
Chris Davida2027072007-10-13 23:56:33 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 udelay(1);
60 }
61
62 return -ETIMEDOUT;
63}
64
65static int
66wait_ack(struct i2c_au1550_data *adap)
67{
68 u32 stat;
69 volatile psc_smb_t *sp;
70
71 if (wait_xfer_done(adap))
72 return -ETIMEDOUT;
73
74 sp = (volatile psc_smb_t *)(adap->psc_base);
75
76 stat = sp->psc_smbevnt;
77 au_sync();
78
79 if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
80 return -ETIMEDOUT;
81
82 return 0;
83}
84
85static int
86wait_master_done(struct i2c_au1550_data *adap)
87{
88 u32 stat;
89 int i;
90 volatile psc_smb_t *sp;
91
92 sp = (volatile psc_smb_t *)(adap->psc_base);
93
94 /* Wait for Master Done.
95 */
96 for (i = 0; i < adap->xfer_timeout; i++) {
97 stat = sp->psc_smbevnt;
98 au_sync();
99 if ((stat & PSC_SMBEVNT_MD) != 0)
100 return 0;
101 udelay(1);
102 }
103
104 return -ETIMEDOUT;
105}
106
107static int
Manuel Lauss91f27952008-01-27 18:14:52 +0100108do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 volatile psc_smb_t *sp;
111 u32 stat;
112
113 sp = (volatile psc_smb_t *)(adap->psc_base);
114
115 /* Reset the FIFOs, clear events.
116 */
Domen Puncer88599422006-08-13 23:35:40 +0200117 stat = sp->psc_smbstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
119 au_sync();
Domen Puncer88599422006-08-13 23:35:40 +0200120
121 if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
122 sp->psc_smbpcr = PSC_SMBPCR_DC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 au_sync();
Domen Puncer88599422006-08-13 23:35:40 +0200124 do {
125 stat = sp->psc_smbpcr;
126 au_sync();
127 } while ((stat & PSC_SMBPCR_DC) != 0);
128 udelay(50);
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* Write out the i2c chip address and specify operation
132 */
133 addr <<= 1;
134 if (rd)
135 addr |= 1;
136
Manuel Lauss91f27952008-01-27 18:14:52 +0100137 /* zero-byte xfers stop immediately */
138 if (q)
139 addr |= PSC_SMBTXRX_STP;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* Put byte into fifo, start up master.
142 */
143 sp->psc_smbtxrx = addr;
144 au_sync();
145 sp->psc_smbpcr = PSC_SMBPCR_MS;
146 au_sync();
147 if (wait_ack(adap))
148 return -EIO;
Manuel Lauss91f27952008-01-27 18:14:52 +0100149 return (q) ? wait_master_done(adap) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static u32
153wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
154{
155 int j;
156 u32 data, stat;
157 volatile psc_smb_t *sp;
158
159 if (wait_xfer_done(adap))
160 return -EIO;
161
162 sp = (volatile psc_smb_t *)(adap->psc_base);
163
164 j = adap->xfer_timeout * 100;
165 do {
166 j--;
167 if (j <= 0)
168 return -EIO;
169
170 stat = sp->psc_smbstat;
171 au_sync();
172 if ((stat & PSC_SMBSTAT_RE) == 0)
173 j = 0;
174 else
175 udelay(1);
176 } while (j > 0);
177 data = sp->psc_smbtxrx;
178 au_sync();
179 *ret_data = data;
180
181 return 0;
182}
183
184static int
185i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
186 unsigned int len)
187{
188 int i;
189 u32 data;
190 volatile psc_smb_t *sp;
191
192 if (len == 0)
193 return 0;
194
195 /* A read is performed by stuffing the transmit fifo with
196 * zero bytes for timing, waiting for bytes to appear in the
197 * receive fifo, then reading the bytes.
198 */
199
200 sp = (volatile psc_smb_t *)(adap->psc_base);
201
202 i = 0;
203 while (i < (len-1)) {
204 sp->psc_smbtxrx = 0;
205 au_sync();
206 if (wait_for_rx_byte(adap, &data))
207 return -EIO;
208
209 buf[i] = data;
210 i++;
211 }
212
213 /* The last byte has to indicate transfer done.
214 */
215 sp->psc_smbtxrx = PSC_SMBTXRX_STP;
216 au_sync();
217 if (wait_master_done(adap))
218 return -EIO;
219
220 data = sp->psc_smbtxrx;
221 au_sync();
222 buf[i] = data;
223 return 0;
224}
225
226static int
227i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
228 unsigned int len)
229{
230 int i;
231 u32 data;
232 volatile psc_smb_t *sp;
233
234 if (len == 0)
235 return 0;
236
237 sp = (volatile psc_smb_t *)(adap->psc_base);
238
239 i = 0;
240 while (i < (len-1)) {
241 data = buf[i];
242 sp->psc_smbtxrx = data;
243 au_sync();
244 if (wait_ack(adap))
245 return -EIO;
246 i++;
247 }
248
249 /* The last byte has to indicate transfer done.
250 */
251 data = buf[i];
252 data |= PSC_SMBTXRX_STP;
253 sp->psc_smbtxrx = data;
254 au_sync();
255 if (wait_master_done(adap))
256 return -EIO;
257 return 0;
258}
259
260static int
261au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
262{
263 struct i2c_au1550_data *adap = i2c_adap->algo_data;
264 struct i2c_msg *p;
265 int i, err = 0;
266
267 for (i = 0; !err && i < num; i++) {
268 p = &msgs[i];
Manuel Lauss91f27952008-01-27 18:14:52 +0100269 err = do_address(adap, p->addr, p->flags & I2C_M_RD,
270 (p->len == 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (err || !p->len)
272 continue;
273 if (p->flags & I2C_M_RD)
274 err = i2c_read(adap, p->buf, p->len);
275 else
276 err = i2c_write(adap, p->buf, p->len);
277 }
278
279 /* Return the number of messages processed, or the error code.
280 */
281 if (err == 0)
282 err = num;
283 return err;
284}
285
286static u32
287au1550_func(struct i2c_adapter *adap)
288{
Domen Puncer6ed07132006-08-13 23:36:27 +0200289 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Jean Delvare8f9082c2006-09-03 22:39:46 +0200292static const struct i2c_algorithm au1550_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .master_xfer = au1550_xfer,
294 .functionality = au1550_func,
295};
296
297/*
298 * registering functions to load algorithms at runtime
299 * Prior to calling us, the 50MHz clock frequency and routing
300 * must have been set up for the PSC indicated by the adapter.
301 */
302int
303i2c_au1550_add_bus(struct i2c_adapter *i2c_adap)
304{
305 struct i2c_au1550_data *adap = i2c_adap->algo_data;
306 volatile psc_smb_t *sp;
307 u32 stat;
308
309 i2c_adap->algo = &au1550_algo;
310
311 /* Now, set up the PSC for SMBus PIO mode.
312 */
313 sp = (volatile psc_smb_t *)(adap->psc_base);
314 sp->psc_ctrl = PSC_CTRL_DISABLE;
315 au_sync();
316 sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
317 sp->psc_smbcfg = 0;
318 au_sync();
319 sp->psc_ctrl = PSC_CTRL_ENABLE;
320 au_sync();
321 do {
322 stat = sp->psc_smbstat;
323 au_sync();
324 } while ((stat & PSC_SMBSTAT_SR) == 0);
325
326 sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
327 PSC_SMBCFG_DD_DISABLE);
328
329 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
330 * timings are based on this clock.
331 */
332 sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
333 sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
334 au_sync();
335
336 /* Set the protocol timer values. See Table 71 in the
337 * Au1550 Data Book for standard timing values.
338 */
339 sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
340 PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
341 PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
342 PSC_SMBTMR_SET_CH(15);
343 au_sync();
344
345 sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
346 do {
347 stat = sp->psc_smbstat;
348 au_sync();
349 } while ((stat & PSC_SMBSTAT_DR) == 0);
350
351 return i2c_add_adapter(i2c_adap);
352}
353
354
355int
356i2c_au1550_del_bus(struct i2c_adapter *adap)
357{
358 return i2c_del_adapter(adap);
359}
360
361static int
362pb1550_reg(struct i2c_client *client)
363{
364 return 0;
365}
366
367static int
368pb1550_unreg(struct i2c_client *client)
369{
370 return 0;
371}
372
373static struct i2c_au1550_data pb1550_i2c_info = {
374 SMBUS_PSC_BASE, 200, 200
375};
376
377static struct i2c_adapter pb1550_board_adapter = {
378 name: "pb1550 adapter",
379 id: I2C_HW_AU1550_PSC,
380 algo: NULL,
381 algo_data: &pb1550_i2c_info,
382 client_register: pb1550_reg,
383 client_unregister: pb1550_unreg,
384};
385
386/* BIG hack to support the control interface on the Wolfson WM8731
387 * audio codec on the Pb1550 board. We get an address and two data
388 * bytes to write, create an i2c message, and send it across the
389 * i2c transfer function. We do this here because we have access to
390 * the i2c adapter structure.
391 */
392static struct i2c_msg wm_i2c_msg; /* We don't want this stuff on the stack */
393static u8 i2cbuf[2];
394
395int
396pb1550_wm_codec_write(u8 addr, u8 reg, u8 val)
397{
398 wm_i2c_msg.addr = addr;
399 wm_i2c_msg.flags = 0;
400 wm_i2c_msg.buf = i2cbuf;
401 wm_i2c_msg.len = 2;
402 i2cbuf[0] = reg;
403 i2cbuf[1] = val;
404
405 return pb1550_board_adapter.algo->master_xfer(&pb1550_board_adapter, &wm_i2c_msg, 1);
406}
407
408static int __init
409i2c_au1550_init(void)
410{
411 printk(KERN_INFO "Au1550 I2C: ");
412
413 /* This is where we would set up a 50MHz clock source
414 * and routing. On the Pb1550, the SMBus is PSC2, which
415 * uses a shared clock with USB. This has been already
416 * configured by Yamon as a 48MHz clock, close enough
417 * for our work.
418 */
419 if (i2c_au1550_add_bus(&pb1550_board_adapter) < 0) {
420 printk("failed to initialize.\n");
421 return -ENODEV;
422 }
423
424 printk("initialized.\n");
425 return 0;
426}
427
428static void __exit
429i2c_au1550_exit(void)
430{
431 i2c_au1550_del_bus(&pb1550_board_adapter);
432}
433
434MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
435MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
436MODULE_LICENSE("GPL");
437
438module_init (i2c_au1550_init);
439module_exit (i2c_au1550_exit);