i2c-bfin-twi.c 19 KB

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