i2c-bfin-twi.c 20 KB

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