i2c-bfin-twi.c 19 KB

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