i2c-bfin-twi.c 19 KB

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