i2c-bfin-twi.c 19 KB

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