i2c-bfin-twi.c 20 KB

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