i2c-bfin-twi.c 20 KB

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