i2c-bfin-twi.c 19 KB

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