i2c-bfin-twi.c 19 KB

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