i2c-au1550.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  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. #include <linux/delay.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/errno.h>
  34. #include <linux/i2c.h>
  35. #include <asm/mach-au1x00/au1xxx.h>
  36. #include <asm/mach-au1x00/au1xxx_psc.h>
  37. #include "i2c-au1550.h"
  38. static int
  39. wait_xfer_done(struct i2c_au1550_data *adap)
  40. {
  41. u32 stat;
  42. int i;
  43. volatile psc_smb_t *sp;
  44. sp = (volatile psc_smb_t *)(adap->psc_base);
  45. /* Wait for Tx FIFO Underflow.
  46. */
  47. for (i = 0; i < adap->xfer_timeout; i++) {
  48. stat = sp->psc_smbevnt;
  49. au_sync();
  50. if ((stat & PSC_SMBEVNT_TU) != 0) {
  51. /* Clear it. */
  52. sp->psc_smbevnt = PSC_SMBEVNT_TU;
  53. au_sync();
  54. return 0;
  55. }
  56. udelay(1);
  57. }
  58. return -ETIMEDOUT;
  59. }
  60. static int
  61. wait_ack(struct i2c_au1550_data *adap)
  62. {
  63. u32 stat;
  64. volatile psc_smb_t *sp;
  65. if (wait_xfer_done(adap))
  66. return -ETIMEDOUT;
  67. sp = (volatile psc_smb_t *)(adap->psc_base);
  68. stat = sp->psc_smbevnt;
  69. au_sync();
  70. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  71. return -ETIMEDOUT;
  72. return 0;
  73. }
  74. static int
  75. wait_master_done(struct i2c_au1550_data *adap)
  76. {
  77. u32 stat;
  78. int i;
  79. volatile psc_smb_t *sp;
  80. sp = (volatile psc_smb_t *)(adap->psc_base);
  81. /* Wait for Master Done.
  82. */
  83. for (i = 0; i < adap->xfer_timeout; i++) {
  84. stat = sp->psc_smbevnt;
  85. au_sync();
  86. if ((stat & PSC_SMBEVNT_MD) != 0)
  87. return 0;
  88. udelay(1);
  89. }
  90. return -ETIMEDOUT;
  91. }
  92. static int
  93. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd)
  94. {
  95. volatile psc_smb_t *sp;
  96. u32 stat;
  97. sp = (volatile psc_smb_t *)(adap->psc_base);
  98. /* Reset the FIFOs, clear events.
  99. */
  100. stat = sp->psc_smbstat;
  101. sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
  102. au_sync();
  103. if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
  104. sp->psc_smbpcr = PSC_SMBPCR_DC;
  105. au_sync();
  106. do {
  107. stat = sp->psc_smbpcr;
  108. au_sync();
  109. } while ((stat & PSC_SMBPCR_DC) != 0);
  110. udelay(50);
  111. }
  112. /* Write out the i2c chip address and specify operation
  113. */
  114. addr <<= 1;
  115. if (rd)
  116. addr |= 1;
  117. /* Put byte into fifo, start up master.
  118. */
  119. sp->psc_smbtxrx = addr;
  120. au_sync();
  121. sp->psc_smbpcr = PSC_SMBPCR_MS;
  122. au_sync();
  123. if (wait_ack(adap))
  124. return -EIO;
  125. return 0;
  126. }
  127. static u32
  128. wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
  129. {
  130. int j;
  131. u32 data, stat;
  132. volatile psc_smb_t *sp;
  133. if (wait_xfer_done(adap))
  134. return -EIO;
  135. sp = (volatile psc_smb_t *)(adap->psc_base);
  136. j = adap->xfer_timeout * 100;
  137. do {
  138. j--;
  139. if (j <= 0)
  140. return -EIO;
  141. stat = sp->psc_smbstat;
  142. au_sync();
  143. if ((stat & PSC_SMBSTAT_RE) == 0)
  144. j = 0;
  145. else
  146. udelay(1);
  147. } while (j > 0);
  148. data = sp->psc_smbtxrx;
  149. au_sync();
  150. *ret_data = data;
  151. return 0;
  152. }
  153. static int
  154. i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  155. unsigned int len)
  156. {
  157. int i;
  158. u32 data;
  159. volatile psc_smb_t *sp;
  160. if (len == 0)
  161. return 0;
  162. /* A read is performed by stuffing the transmit fifo with
  163. * zero bytes for timing, waiting for bytes to appear in the
  164. * receive fifo, then reading the bytes.
  165. */
  166. sp = (volatile psc_smb_t *)(adap->psc_base);
  167. i = 0;
  168. while (i < (len-1)) {
  169. sp->psc_smbtxrx = 0;
  170. au_sync();
  171. if (wait_for_rx_byte(adap, &data))
  172. return -EIO;
  173. buf[i] = data;
  174. i++;
  175. }
  176. /* The last byte has to indicate transfer done.
  177. */
  178. sp->psc_smbtxrx = PSC_SMBTXRX_STP;
  179. au_sync();
  180. if (wait_master_done(adap))
  181. return -EIO;
  182. data = sp->psc_smbtxrx;
  183. au_sync();
  184. buf[i] = data;
  185. return 0;
  186. }
  187. static int
  188. i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  189. unsigned int len)
  190. {
  191. int i;
  192. u32 data;
  193. volatile psc_smb_t *sp;
  194. if (len == 0)
  195. return 0;
  196. sp = (volatile psc_smb_t *)(adap->psc_base);
  197. i = 0;
  198. while (i < (len-1)) {
  199. data = buf[i];
  200. sp->psc_smbtxrx = data;
  201. au_sync();
  202. if (wait_ack(adap))
  203. return -EIO;
  204. i++;
  205. }
  206. /* The last byte has to indicate transfer done.
  207. */
  208. data = buf[i];
  209. data |= PSC_SMBTXRX_STP;
  210. sp->psc_smbtxrx = data;
  211. au_sync();
  212. if (wait_master_done(adap))
  213. return -EIO;
  214. return 0;
  215. }
  216. static int
  217. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  218. {
  219. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  220. struct i2c_msg *p;
  221. int i, err = 0;
  222. for (i = 0; !err && i < num; i++) {
  223. p = &msgs[i];
  224. err = do_address(adap, p->addr, p->flags & I2C_M_RD);
  225. if (err || !p->len)
  226. continue;
  227. if (p->flags & I2C_M_RD)
  228. err = i2c_read(adap, p->buf, p->len);
  229. else
  230. err = i2c_write(adap, p->buf, p->len);
  231. }
  232. /* Return the number of messages processed, or the error code.
  233. */
  234. if (err == 0)
  235. err = num;
  236. return err;
  237. }
  238. static u32
  239. au1550_func(struct i2c_adapter *adap)
  240. {
  241. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  242. }
  243. static const struct i2c_algorithm au1550_algo = {
  244. .master_xfer = au1550_xfer,
  245. .functionality = au1550_func,
  246. };
  247. /*
  248. * registering functions to load algorithms at runtime
  249. * Prior to calling us, the 50MHz clock frequency and routing
  250. * must have been set up for the PSC indicated by the adapter.
  251. */
  252. int
  253. i2c_au1550_add_bus(struct i2c_adapter *i2c_adap)
  254. {
  255. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  256. volatile psc_smb_t *sp;
  257. u32 stat;
  258. i2c_adap->algo = &au1550_algo;
  259. /* Now, set up the PSC for SMBus PIO mode.
  260. */
  261. sp = (volatile psc_smb_t *)(adap->psc_base);
  262. sp->psc_ctrl = PSC_CTRL_DISABLE;
  263. au_sync();
  264. sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
  265. sp->psc_smbcfg = 0;
  266. au_sync();
  267. sp->psc_ctrl = PSC_CTRL_ENABLE;
  268. au_sync();
  269. do {
  270. stat = sp->psc_smbstat;
  271. au_sync();
  272. } while ((stat & PSC_SMBSTAT_SR) == 0);
  273. sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
  274. PSC_SMBCFG_DD_DISABLE);
  275. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  276. * timings are based on this clock.
  277. */
  278. sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  279. sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
  280. au_sync();
  281. /* Set the protocol timer values. See Table 71 in the
  282. * Au1550 Data Book for standard timing values.
  283. */
  284. sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  285. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  286. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  287. PSC_SMBTMR_SET_CH(15);
  288. au_sync();
  289. sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
  290. do {
  291. stat = sp->psc_smbstat;
  292. au_sync();
  293. } while ((stat & PSC_SMBSTAT_DR) == 0);
  294. return i2c_add_adapter(i2c_adap);
  295. }
  296. int
  297. i2c_au1550_del_bus(struct i2c_adapter *adap)
  298. {
  299. return i2c_del_adapter(adap);
  300. }
  301. static int
  302. pb1550_reg(struct i2c_client *client)
  303. {
  304. return 0;
  305. }
  306. static int
  307. pb1550_unreg(struct i2c_client *client)
  308. {
  309. return 0;
  310. }
  311. static struct i2c_au1550_data pb1550_i2c_info = {
  312. SMBUS_PSC_BASE, 200, 200
  313. };
  314. static struct i2c_adapter pb1550_board_adapter = {
  315. name: "pb1550 adapter",
  316. id: I2C_HW_AU1550_PSC,
  317. algo: NULL,
  318. algo_data: &pb1550_i2c_info,
  319. client_register: pb1550_reg,
  320. client_unregister: pb1550_unreg,
  321. };
  322. /* BIG hack to support the control interface on the Wolfson WM8731
  323. * audio codec on the Pb1550 board. We get an address and two data
  324. * bytes to write, create an i2c message, and send it across the
  325. * i2c transfer function. We do this here because we have access to
  326. * the i2c adapter structure.
  327. */
  328. static struct i2c_msg wm_i2c_msg; /* We don't want this stuff on the stack */
  329. static u8 i2cbuf[2];
  330. int
  331. pb1550_wm_codec_write(u8 addr, u8 reg, u8 val)
  332. {
  333. wm_i2c_msg.addr = addr;
  334. wm_i2c_msg.flags = 0;
  335. wm_i2c_msg.buf = i2cbuf;
  336. wm_i2c_msg.len = 2;
  337. i2cbuf[0] = reg;
  338. i2cbuf[1] = val;
  339. return pb1550_board_adapter.algo->master_xfer(&pb1550_board_adapter, &wm_i2c_msg, 1);
  340. }
  341. static int __init
  342. i2c_au1550_init(void)
  343. {
  344. printk(KERN_INFO "Au1550 I2C: ");
  345. /* This is where we would set up a 50MHz clock source
  346. * and routing. On the Pb1550, the SMBus is PSC2, which
  347. * uses a shared clock with USB. This has been already
  348. * configured by Yamon as a 48MHz clock, close enough
  349. * for our work.
  350. */
  351. if (i2c_au1550_add_bus(&pb1550_board_adapter) < 0) {
  352. printk("failed to initialize.\n");
  353. return -ENODEV;
  354. }
  355. printk("initialized.\n");
  356. return 0;
  357. }
  358. static void __exit
  359. i2c_au1550_exit(void)
  360. {
  361. i2c_au1550_del_bus(&pb1550_board_adapter);
  362. }
  363. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  364. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  365. MODULE_LICENSE("GPL");
  366. module_init (i2c_au1550_init);
  367. module_exit (i2c_au1550_exit);