i2c-bfin-twi.c 18 KB

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