i2c-bfin-twi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Blackfin On-Chip Two Wire Interface Driver
  3. *
  4. * Copyright 2005-2007 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/i2c.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/mm.h>
  17. #include <linux/timer.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/completion.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/delay.h>
  23. #include <asm/blackfin.h>
  24. #include <asm/portmux.h>
  25. #include <asm/irq.h>
  26. /* SMBus mode*/
  27. #define TWI_I2C_MODE_STANDARD 1
  28. #define TWI_I2C_MODE_STANDARDSUB 2
  29. #define TWI_I2C_MODE_COMBINED 3
  30. #define TWI_I2C_MODE_REPEAT 4
  31. struct bfin_twi_iface {
  32. int irq;
  33. spinlock_t lock;
  34. char read_write;
  35. u8 command;
  36. u8 *transPtr;
  37. int readNum;
  38. int writeNum;
  39. int cur_mode;
  40. int manual_stop;
  41. int result;
  42. struct i2c_adapter adap;
  43. struct completion complete;
  44. struct i2c_msg *pmsg;
  45. int msg_num;
  46. int cur_msg;
  47. u16 saved_clkdiv;
  48. u16 saved_control;
  49. void __iomem *regs_base;
  50. };
  51. #define DEFINE_TWI_REG(reg, off) \
  52. static inline u16 read_##reg(struct bfin_twi_iface *iface) \
  53. { return bfin_read16(iface->regs_base + (off)); } \
  54. static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
  55. { bfin_write16(iface->regs_base + (off), v); }
  56. DEFINE_TWI_REG(CLKDIV, 0x00)
  57. DEFINE_TWI_REG(CONTROL, 0x04)
  58. DEFINE_TWI_REG(SLAVE_CTL, 0x08)
  59. DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
  60. DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
  61. DEFINE_TWI_REG(MASTER_CTL, 0x14)
  62. DEFINE_TWI_REG(MASTER_STAT, 0x18)
  63. DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
  64. DEFINE_TWI_REG(INT_STAT, 0x20)
  65. DEFINE_TWI_REG(INT_MASK, 0x24)
  66. DEFINE_TWI_REG(FIFO_CTL, 0x28)
  67. DEFINE_TWI_REG(FIFO_STAT, 0x2C)
  68. DEFINE_TWI_REG(XMT_DATA8, 0x80)
  69. DEFINE_TWI_REG(XMT_DATA16, 0x84)
  70. DEFINE_TWI_REG(RCV_DATA8, 0x88)
  71. DEFINE_TWI_REG(RCV_DATA16, 0x8C)
  72. static const u16 pin_req[2][3] = {
  73. {P_TWI0_SCL, P_TWI0_SDA, 0},
  74. {P_TWI1_SCL, P_TWI1_SDA, 0},
  75. };
  76. static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
  77. unsigned short twi_int_status)
  78. {
  79. unsigned short mast_stat = read_MASTER_STAT(iface);
  80. if (twi_int_status & XMTSERV) {
  81. /* Transmit next data */
  82. if (iface->writeNum > 0) {
  83. SSYNC();
  84. write_XMT_DATA8(iface, *(iface->transPtr++));
  85. iface->writeNum--;
  86. }
  87. /* start receive immediately after complete sending in
  88. * combine mode.
  89. */
  90. else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
  91. write_MASTER_CTL(iface,
  92. read_MASTER_CTL(iface) | MDIR | RSTART);
  93. else if (iface->manual_stop)
  94. write_MASTER_CTL(iface,
  95. read_MASTER_CTL(iface) | STOP);
  96. else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
  97. iface->cur_msg + 1 < iface->msg_num) {
  98. if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
  99. write_MASTER_CTL(iface,
  100. read_MASTER_CTL(iface) | RSTART | MDIR);
  101. else
  102. write_MASTER_CTL(iface,
  103. (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
  104. }
  105. }
  106. if (twi_int_status & RCVSERV) {
  107. if (iface->readNum > 0) {
  108. /* Receive next data */
  109. *(iface->transPtr) = read_RCV_DATA8(iface);
  110. if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
  111. /* Change combine mode into sub mode after
  112. * read first data.
  113. */
  114. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  115. /* Get read number from first byte in block
  116. * combine mode.
  117. */
  118. if (iface->readNum == 1 && iface->manual_stop)
  119. iface->readNum = *iface->transPtr + 1;
  120. }
  121. iface->transPtr++;
  122. iface->readNum--;
  123. } else if (iface->manual_stop) {
  124. write_MASTER_CTL(iface,
  125. read_MASTER_CTL(iface) | STOP);
  126. } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
  127. iface->cur_msg + 1 < iface->msg_num) {
  128. if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
  129. write_MASTER_CTL(iface,
  130. read_MASTER_CTL(iface) | RSTART | MDIR);
  131. else
  132. write_MASTER_CTL(iface,
  133. (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
  134. }
  135. }
  136. if (twi_int_status & MERR) {
  137. write_INT_MASK(iface, 0);
  138. write_MASTER_STAT(iface, 0x3e);
  139. write_MASTER_CTL(iface, 0);
  140. iface->result = -EIO;
  141. if (mast_stat & LOSTARB)
  142. dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
  143. if (mast_stat & ANAK)
  144. dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
  145. if (mast_stat & DNAK)
  146. dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
  147. if (mast_stat & BUFRDERR)
  148. dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
  149. if (mast_stat & BUFWRERR)
  150. dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
  151. /* Faulty slave devices, may drive SDA low after a transfer
  152. * finishes. To release the bus this code generates up to 9
  153. * extra clocks until SDA is released.
  154. */
  155. if (read_MASTER_STAT(iface) & SDASEN) {
  156. int cnt = 9;
  157. do {
  158. write_MASTER_CTL(iface, SCLOVR);
  159. udelay(6);
  160. write_MASTER_CTL(iface, 0);
  161. udelay(6);
  162. } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);
  163. write_MASTER_CTL(iface, SDAOVR | SCLOVR);
  164. udelay(6);
  165. write_MASTER_CTL(iface, SDAOVR);
  166. udelay(6);
  167. write_MASTER_CTL(iface, 0);
  168. }
  169. /* If it is a quick transfer, only address without data,
  170. * not an err, return 1.
  171. */
  172. if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
  173. iface->transPtr == NULL &&
  174. (twi_int_status & MCOMP) && (mast_stat & DNAK))
  175. iface->result = 1;
  176. complete(&iface->complete);
  177. return;
  178. }
  179. if (twi_int_status & MCOMP) {
  180. if ((read_MASTER_CTL(iface) & MEN) == 0 &&
  181. (iface->cur_mode == TWI_I2C_MODE_REPEAT ||
  182. iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
  183. iface->result = -1;
  184. write_INT_MASK(iface, 0);
  185. write_MASTER_CTL(iface, 0);
  186. } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
  187. if (iface->readNum == 0) {
  188. /* set the read number to 1 and ask for manual
  189. * stop in block combine mode
  190. */
  191. iface->readNum = 1;
  192. iface->manual_stop = 1;
  193. write_MASTER_CTL(iface,
  194. read_MASTER_CTL(iface) | (0xff << 6));
  195. } else {
  196. /* set the readd number in other
  197. * combine mode.
  198. */
  199. write_MASTER_CTL(iface,
  200. (read_MASTER_CTL(iface) &
  201. (~(0xff << 6))) |
  202. (iface->readNum << 6));
  203. }
  204. /* remove restart bit and enable master receive */
  205. write_MASTER_CTL(iface,
  206. read_MASTER_CTL(iface) & ~RSTART);
  207. } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
  208. iface->cur_msg+1 < iface->msg_num) {
  209. iface->cur_msg++;
  210. iface->transPtr = iface->pmsg[iface->cur_msg].buf;
  211. iface->writeNum = iface->readNum =
  212. iface->pmsg[iface->cur_msg].len;
  213. /* Set Transmit device address */
  214. write_MASTER_ADDR(iface,
  215. iface->pmsg[iface->cur_msg].addr);
  216. if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
  217. iface->read_write = I2C_SMBUS_READ;
  218. else {
  219. iface->read_write = I2C_SMBUS_WRITE;
  220. /* Transmit first data */
  221. if (iface->writeNum > 0) {
  222. write_XMT_DATA8(iface,
  223. *(iface->transPtr++));
  224. iface->writeNum--;
  225. }
  226. }
  227. if (iface->pmsg[iface->cur_msg].len <= 255)
  228. write_MASTER_CTL(iface,
  229. (read_MASTER_CTL(iface) &
  230. (~(0xff << 6))) |
  231. (iface->pmsg[iface->cur_msg].len << 6));
  232. else {
  233. write_MASTER_CTL(iface,
  234. (read_MASTER_CTL(iface) |
  235. (0xff << 6)));
  236. iface->manual_stop = 1;
  237. }
  238. /* remove restart bit and enable master receive */
  239. write_MASTER_CTL(iface,
  240. read_MASTER_CTL(iface) & ~RSTART);
  241. } else {
  242. iface->result = 1;
  243. write_INT_MASK(iface, 0);
  244. write_MASTER_CTL(iface, 0);
  245. }
  246. }
  247. complete(&iface->complete);
  248. }
  249. /* Interrupt handler */
  250. static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
  251. {
  252. struct bfin_twi_iface *iface = dev_id;
  253. unsigned long flags;
  254. unsigned short twi_int_status;
  255. spin_lock_irqsave(&iface->lock, flags);
  256. while (1) {
  257. twi_int_status = read_INT_STAT(iface);
  258. if (!twi_int_status)
  259. break;
  260. /* Clear interrupt status */
  261. write_INT_STAT(iface, twi_int_status);
  262. bfin_twi_handle_interrupt(iface, twi_int_status);
  263. SSYNC();
  264. }
  265. spin_unlock_irqrestore(&iface->lock, flags);
  266. return IRQ_HANDLED;
  267. }
  268. /*
  269. * One i2c master transfer
  270. */
  271. static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
  272. struct i2c_msg *msgs, int num)
  273. {
  274. struct bfin_twi_iface *iface = adap->algo_data;
  275. struct i2c_msg *pmsg;
  276. int rc = 0;
  277. if (!(read_CONTROL(iface) & TWI_ENA))
  278. return -ENXIO;
  279. while (read_MASTER_STAT(iface) & BUSBUSY)
  280. yield();
  281. iface->pmsg = msgs;
  282. iface->msg_num = num;
  283. iface->cur_msg = 0;
  284. pmsg = &msgs[0];
  285. if (pmsg->flags & I2C_M_TEN) {
  286. dev_err(&adap->dev, "10 bits addr not supported!\n");
  287. return -EINVAL;
  288. }
  289. iface->cur_mode = TWI_I2C_MODE_REPEAT;
  290. iface->manual_stop = 0;
  291. iface->transPtr = pmsg->buf;
  292. iface->writeNum = iface->readNum = pmsg->len;
  293. iface->result = 0;
  294. init_completion(&(iface->complete));
  295. /* Set Transmit device address */
  296. write_MASTER_ADDR(iface, pmsg->addr);
  297. /* FIFO Initiation. Data in FIFO should be
  298. * discarded before start a new operation.
  299. */
  300. write_FIFO_CTL(iface, 0x3);
  301. SSYNC();
  302. write_FIFO_CTL(iface, 0);
  303. SSYNC();
  304. if (pmsg->flags & I2C_M_RD)
  305. iface->read_write = I2C_SMBUS_READ;
  306. else {
  307. iface->read_write = I2C_SMBUS_WRITE;
  308. /* Transmit first data */
  309. if (iface->writeNum > 0) {
  310. write_XMT_DATA8(iface, *(iface->transPtr++));
  311. iface->writeNum--;
  312. SSYNC();
  313. }
  314. }
  315. /* clear int stat */
  316. write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
  317. /* Interrupt mask . Enable XMT, RCV interrupt */
  318. write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
  319. SSYNC();
  320. if (pmsg->len <= 255)
  321. write_MASTER_CTL(iface, pmsg->len << 6);
  322. else {
  323. write_MASTER_CTL(iface, 0xff << 6);
  324. iface->manual_stop = 1;
  325. }
  326. /* Master enable */
  327. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  328. ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
  329. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
  330. SSYNC();
  331. while (!iface->result) {
  332. if (!wait_for_completion_timeout(&iface->complete,
  333. adap->timeout)) {
  334. iface->result = -1;
  335. dev_err(&adap->dev, "master transfer timeout\n");
  336. }
  337. }
  338. if (iface->result == 1)
  339. rc = iface->cur_msg + 1;
  340. else
  341. rc = iface->result;
  342. return rc;
  343. }
  344. /*
  345. * Generic i2c master transfer entrypoint
  346. */
  347. static int bfin_twi_master_xfer(struct i2c_adapter *adap,
  348. struct i2c_msg *msgs, int num)
  349. {
  350. return bfin_twi_do_master_xfer(adap, msgs, num);
  351. }
  352. /*
  353. * One I2C SMBus transfer
  354. */
  355. int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  356. unsigned short flags, char read_write,
  357. u8 command, int size, union i2c_smbus_data *data)
  358. {
  359. struct bfin_twi_iface *iface = adap->algo_data;
  360. int rc = 0;
  361. if (!(read_CONTROL(iface) & TWI_ENA))
  362. return -ENXIO;
  363. while (read_MASTER_STAT(iface) & BUSBUSY)
  364. yield();
  365. iface->writeNum = 0;
  366. iface->readNum = 0;
  367. /* Prepare datas & select mode */
  368. switch (size) {
  369. case I2C_SMBUS_QUICK:
  370. iface->transPtr = NULL;
  371. iface->cur_mode = TWI_I2C_MODE_STANDARD;
  372. break;
  373. case I2C_SMBUS_BYTE:
  374. if (data == NULL)
  375. iface->transPtr = NULL;
  376. else {
  377. if (read_write == I2C_SMBUS_READ)
  378. iface->readNum = 1;
  379. else
  380. iface->writeNum = 1;
  381. iface->transPtr = &data->byte;
  382. }
  383. iface->cur_mode = TWI_I2C_MODE_STANDARD;
  384. break;
  385. case I2C_SMBUS_BYTE_DATA:
  386. if (read_write == I2C_SMBUS_READ) {
  387. iface->readNum = 1;
  388. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  389. } else {
  390. iface->writeNum = 1;
  391. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  392. }
  393. iface->transPtr = &data->byte;
  394. break;
  395. case I2C_SMBUS_WORD_DATA:
  396. if (read_write == I2C_SMBUS_READ) {
  397. iface->readNum = 2;
  398. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  399. } else {
  400. iface->writeNum = 2;
  401. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  402. }
  403. iface->transPtr = (u8 *)&data->word;
  404. break;
  405. case I2C_SMBUS_PROC_CALL:
  406. iface->writeNum = 2;
  407. iface->readNum = 2;
  408. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  409. iface->transPtr = (u8 *)&data->word;
  410. break;
  411. case I2C_SMBUS_BLOCK_DATA:
  412. if (read_write == I2C_SMBUS_READ) {
  413. iface->readNum = 0;
  414. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  415. } else {
  416. iface->writeNum = data->block[0] + 1;
  417. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  418. }
  419. iface->transPtr = data->block;
  420. break;
  421. case I2C_SMBUS_I2C_BLOCK_DATA:
  422. if (read_write == I2C_SMBUS_READ) {
  423. iface->readNum = data->block[0];
  424. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  425. } else {
  426. iface->writeNum = data->block[0];
  427. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  428. }
  429. iface->transPtr = (u8 *)&data->block[1];
  430. break;
  431. default:
  432. return -1;
  433. }
  434. iface->result = 0;
  435. iface->manual_stop = 0;
  436. iface->read_write = read_write;
  437. iface->command = command;
  438. init_completion(&(iface->complete));
  439. /* FIFO Initiation. Data in FIFO should be discarded before
  440. * start a new operation.
  441. */
  442. write_FIFO_CTL(iface, 0x3);
  443. SSYNC();
  444. write_FIFO_CTL(iface, 0);
  445. /* clear int stat */
  446. write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
  447. /* Set Transmit device address */
  448. write_MASTER_ADDR(iface, addr);
  449. SSYNC();
  450. switch (iface->cur_mode) {
  451. case TWI_I2C_MODE_STANDARDSUB:
  452. write_XMT_DATA8(iface, iface->command);
  453. write_INT_MASK(iface, MCOMP | MERR |
  454. ((iface->read_write == I2C_SMBUS_READ) ?
  455. RCVSERV : XMTSERV));
  456. SSYNC();
  457. if (iface->writeNum + 1 <= 255)
  458. write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
  459. else {
  460. write_MASTER_CTL(iface, 0xff << 6);
  461. iface->manual_stop = 1;
  462. }
  463. /* Master enable */
  464. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  465. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
  466. break;
  467. case TWI_I2C_MODE_COMBINED:
  468. write_XMT_DATA8(iface, iface->command);
  469. write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
  470. SSYNC();
  471. if (iface->writeNum > 0)
  472. write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
  473. else
  474. write_MASTER_CTL(iface, 0x1 << 6);
  475. /* Master enable */
  476. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  477. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
  478. break;
  479. default:
  480. write_MASTER_CTL(iface, 0);
  481. if (size != I2C_SMBUS_QUICK) {
  482. /* Don't access xmit data register when this is a
  483. * read operation.
  484. */
  485. if (iface->read_write != I2C_SMBUS_READ) {
  486. if (iface->writeNum > 0) {
  487. write_XMT_DATA8(iface,
  488. *(iface->transPtr++));
  489. if (iface->writeNum <= 255)
  490. write_MASTER_CTL(iface,
  491. iface->writeNum << 6);
  492. else {
  493. write_MASTER_CTL(iface,
  494. 0xff << 6);
  495. iface->manual_stop = 1;
  496. }
  497. iface->writeNum--;
  498. } else {
  499. write_XMT_DATA8(iface, iface->command);
  500. write_MASTER_CTL(iface, 1 << 6);
  501. }
  502. } else {
  503. if (iface->readNum > 0 && iface->readNum <= 255)
  504. write_MASTER_CTL(iface,
  505. iface->readNum << 6);
  506. else if (iface->readNum > 255) {
  507. write_MASTER_CTL(iface, 0xff << 6);
  508. iface->manual_stop = 1;
  509. } else
  510. break;
  511. }
  512. }
  513. write_INT_MASK(iface, MCOMP | MERR |
  514. ((iface->read_write == I2C_SMBUS_READ) ?
  515. RCVSERV : XMTSERV));
  516. SSYNC();
  517. /* Master enable */
  518. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  519. ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
  520. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
  521. break;
  522. }
  523. SSYNC();
  524. while (!iface->result) {
  525. if (!wait_for_completion_timeout(&iface->complete,
  526. adap->timeout)) {
  527. iface->result = -1;
  528. dev_err(&adap->dev, "smbus transfer timeout\n");
  529. }
  530. }
  531. rc = (iface->result >= 0) ? 0 : -1;
  532. return rc;
  533. }
  534. /*
  535. * Generic I2C SMBus transfer entrypoint
  536. */
  537. int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  538. unsigned short flags, char read_write,
  539. u8 command, int size, union i2c_smbus_data *data)
  540. {
  541. return bfin_twi_do_smbus_xfer(adap, addr, flags,
  542. read_write, command, size, data);
  543. }
  544. /*
  545. * Return what the adapter supports
  546. */
  547. static u32 bfin_twi_functionality(struct i2c_adapter *adap)
  548. {
  549. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  550. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  551. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
  552. I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
  553. }
  554. static struct i2c_algorithm bfin_twi_algorithm = {
  555. .master_xfer = bfin_twi_master_xfer,
  556. .smbus_xfer = bfin_twi_smbus_xfer,
  557. .functionality = bfin_twi_functionality,
  558. };
  559. static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
  560. {
  561. struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
  562. iface->saved_clkdiv = read_CLKDIV(iface);
  563. iface->saved_control = read_CONTROL(iface);
  564. free_irq(iface->irq, iface);
  565. /* Disable TWI */
  566. write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
  567. return 0;
  568. }
  569. static int i2c_bfin_twi_resume(struct platform_device *pdev)
  570. {
  571. struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
  572. int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
  573. 0, pdev->name, iface);
  574. if (rc) {
  575. dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
  576. return -ENODEV;
  577. }
  578. /* Resume TWI interface clock as specified */
  579. write_CLKDIV(iface, iface->saved_clkdiv);
  580. /* Resume TWI */
  581. write_CONTROL(iface, iface->saved_control);
  582. return 0;
  583. }
  584. static int i2c_bfin_twi_probe(struct platform_device *pdev)
  585. {
  586. struct bfin_twi_iface *iface;
  587. struct i2c_adapter *p_adap;
  588. struct resource *res;
  589. int rc;
  590. unsigned int clkhilow;
  591. iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
  592. if (!iface) {
  593. dev_err(&pdev->dev, "Cannot allocate memory\n");
  594. rc = -ENOMEM;
  595. goto out_error_nomem;
  596. }
  597. spin_lock_init(&(iface->lock));
  598. /* Find and map our resources */
  599. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  600. if (res == NULL) {
  601. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  602. rc = -ENOENT;
  603. goto out_error_get_res;
  604. }
  605. iface->regs_base = ioremap(res->start, resource_size(res));
  606. if (iface->regs_base == NULL) {
  607. dev_err(&pdev->dev, "Cannot map IO\n");
  608. rc = -ENXIO;
  609. goto out_error_ioremap;
  610. }
  611. iface->irq = platform_get_irq(pdev, 0);
  612. if (iface->irq < 0) {
  613. dev_err(&pdev->dev, "No IRQ specified\n");
  614. rc = -ENOENT;
  615. goto out_error_no_irq;
  616. }
  617. p_adap = &iface->adap;
  618. p_adap->nr = pdev->id;
  619. strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
  620. p_adap->algo = &bfin_twi_algorithm;
  621. p_adap->algo_data = iface;
  622. p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  623. p_adap->dev.parent = &pdev->dev;
  624. p_adap->timeout = 5 * HZ;
  625. p_adap->retries = 3;
  626. rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
  627. if (rc) {
  628. dev_err(&pdev->dev, "Can't setup pin mux!\n");
  629. goto out_error_pin_mux;
  630. }
  631. rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
  632. 0, pdev->name, iface);
  633. if (rc) {
  634. dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
  635. rc = -ENODEV;
  636. goto out_error_req_irq;
  637. }
  638. /* Set TWI internal clock as 10MHz */
  639. write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
  640. /*
  641. * We will not end up with a CLKDIV=0 because no one will specify
  642. * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
  643. */
  644. clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
  645. /* Set Twi interface clock as specified */
  646. write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
  647. /* Enable TWI */
  648. write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
  649. SSYNC();
  650. rc = i2c_add_numbered_adapter(p_adap);
  651. if (rc < 0) {
  652. dev_err(&pdev->dev, "Can't add i2c adapter!\n");
  653. goto out_error_add_adapter;
  654. }
  655. platform_set_drvdata(pdev, iface);
  656. dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
  657. "regs_base@%p\n", iface->regs_base);
  658. return 0;
  659. out_error_add_adapter:
  660. free_irq(iface->irq, iface);
  661. out_error_req_irq:
  662. out_error_no_irq:
  663. peripheral_free_list(pin_req[pdev->id]);
  664. out_error_pin_mux:
  665. iounmap(iface->regs_base);
  666. out_error_ioremap:
  667. out_error_get_res:
  668. kfree(iface);
  669. out_error_nomem:
  670. return rc;
  671. }
  672. static int i2c_bfin_twi_remove(struct platform_device *pdev)
  673. {
  674. struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
  675. platform_set_drvdata(pdev, NULL);
  676. i2c_del_adapter(&(iface->adap));
  677. free_irq(iface->irq, iface);
  678. peripheral_free_list(pin_req[pdev->id]);
  679. iounmap(iface->regs_base);
  680. kfree(iface);
  681. return 0;
  682. }
  683. static struct platform_driver i2c_bfin_twi_driver = {
  684. .probe = i2c_bfin_twi_probe,
  685. .remove = i2c_bfin_twi_remove,
  686. .suspend = i2c_bfin_twi_suspend,
  687. .resume = i2c_bfin_twi_resume,
  688. .driver = {
  689. .name = "i2c-bfin-twi",
  690. .owner = THIS_MODULE,
  691. },
  692. };
  693. static int __init i2c_bfin_twi_init(void)
  694. {
  695. return platform_driver_register(&i2c_bfin_twi_driver);
  696. }
  697. static void __exit i2c_bfin_twi_exit(void)
  698. {
  699. platform_driver_unregister(&i2c_bfin_twi_driver);
  700. }
  701. subsys_initcall(i2c_bfin_twi_init);
  702. module_exit(i2c_bfin_twi_exit);
  703. MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
  704. MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
  705. MODULE_LICENSE("GPL");
  706. MODULE_ALIAS("platform:i2c-bfin-twi");