i2c-bfin-twi.c 19 KB

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