i2c-bfin-twi.c 20 KB

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