i2c-bfin-twi.c 19 KB

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