i2c-bfin-twi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 | RSTART);
  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) | RSTART | MDIR);
  101. else
  102. write_MASTER_CTL(iface,
  103. (read_MASTER_CTL(iface) | RSTART) & ~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) | RSTART | MDIR);
  137. else
  138. write_MASTER_CTL(iface,
  139. (read_MASTER_CTL(iface) | RSTART) & ~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 ((read_MASTER_CTL(iface) & MEN) == 0 &&
  188. (iface->cur_mode == TWI_I2C_MODE_REPEAT ||
  189. iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
  190. iface->result = -1;
  191. write_INT_MASK(iface, 0);
  192. write_MASTER_CTL(iface, 0);
  193. } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
  194. if (iface->readNum == 0) {
  195. /* set the read number to 1 and ask for manual
  196. * stop in block combine mode
  197. */
  198. iface->readNum = 1;
  199. iface->manual_stop = 1;
  200. write_MASTER_CTL(iface,
  201. read_MASTER_CTL(iface) | (0xff << 6));
  202. } else {
  203. /* set the readd number in other
  204. * combine mode.
  205. */
  206. write_MASTER_CTL(iface,
  207. (read_MASTER_CTL(iface) &
  208. (~(0xff << 6))) |
  209. (iface->readNum << 6));
  210. }
  211. /* remove restart bit and enable master receive */
  212. write_MASTER_CTL(iface,
  213. read_MASTER_CTL(iface) & ~RSTART);
  214. } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
  215. iface->cur_msg+1 < iface->msg_num) {
  216. iface->cur_msg++;
  217. iface->transPtr = iface->pmsg[iface->cur_msg].buf;
  218. iface->writeNum = iface->readNum =
  219. iface->pmsg[iface->cur_msg].len;
  220. /* Set Transmit device address */
  221. write_MASTER_ADDR(iface,
  222. iface->pmsg[iface->cur_msg].addr);
  223. if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
  224. iface->read_write = I2C_SMBUS_READ;
  225. else {
  226. iface->read_write = I2C_SMBUS_WRITE;
  227. /* Transmit first data */
  228. if (iface->writeNum > 0) {
  229. write_XMT_DATA8(iface,
  230. *(iface->transPtr++));
  231. iface->writeNum--;
  232. }
  233. }
  234. if (iface->pmsg[iface->cur_msg].len <= 255) {
  235. write_MASTER_CTL(iface,
  236. (read_MASTER_CTL(iface) &
  237. (~(0xff << 6))) |
  238. (iface->pmsg[iface->cur_msg].len << 6));
  239. iface->manual_stop = 0;
  240. } else {
  241. write_MASTER_CTL(iface,
  242. (read_MASTER_CTL(iface) |
  243. (0xff << 6)));
  244. iface->manual_stop = 1;
  245. }
  246. /* remove restart bit and enable master receive */
  247. write_MASTER_CTL(iface,
  248. read_MASTER_CTL(iface) & ~RSTART);
  249. } else {
  250. iface->result = 1;
  251. write_INT_MASK(iface, 0);
  252. write_MASTER_CTL(iface, 0);
  253. }
  254. complete(&iface->complete);
  255. }
  256. }
  257. /* Interrupt handler */
  258. static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
  259. {
  260. struct bfin_twi_iface *iface = dev_id;
  261. unsigned long flags;
  262. unsigned short twi_int_status;
  263. spin_lock_irqsave(&iface->lock, flags);
  264. while (1) {
  265. twi_int_status = read_INT_STAT(iface);
  266. if (!twi_int_status)
  267. break;
  268. /* Clear interrupt status */
  269. write_INT_STAT(iface, twi_int_status);
  270. bfin_twi_handle_interrupt(iface, twi_int_status);
  271. SSYNC();
  272. }
  273. spin_unlock_irqrestore(&iface->lock, flags);
  274. return IRQ_HANDLED;
  275. }
  276. /*
  277. * One i2c master transfer
  278. */
  279. static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
  280. struct i2c_msg *msgs, int num)
  281. {
  282. struct bfin_twi_iface *iface = adap->algo_data;
  283. struct i2c_msg *pmsg;
  284. int rc = 0;
  285. if (!(read_CONTROL(iface) & TWI_ENA))
  286. return -ENXIO;
  287. if (read_MASTER_STAT(iface) & BUSBUSY)
  288. return -EAGAIN;
  289. iface->pmsg = msgs;
  290. iface->msg_num = num;
  291. iface->cur_msg = 0;
  292. pmsg = &msgs[0];
  293. if (pmsg->flags & I2C_M_TEN) {
  294. dev_err(&adap->dev, "10 bits addr not supported!\n");
  295. return -EINVAL;
  296. }
  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->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
  337. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
  338. SSYNC();
  339. while (!iface->result) {
  340. if (!wait_for_completion_timeout(&iface->complete,
  341. adap->timeout)) {
  342. iface->result = -1;
  343. dev_err(&adap->dev, "master transfer timeout\n");
  344. }
  345. }
  346. if (iface->result == 1)
  347. rc = iface->cur_msg + 1;
  348. else
  349. rc = iface->result;
  350. return rc;
  351. }
  352. /*
  353. * Generic i2c master transfer entrypoint
  354. */
  355. static int bfin_twi_master_xfer(struct i2c_adapter *adap,
  356. struct i2c_msg *msgs, int num)
  357. {
  358. return bfin_twi_do_master_xfer(adap, msgs, num);
  359. }
  360. /*
  361. * One I2C SMBus transfer
  362. */
  363. int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  364. unsigned short flags, char read_write,
  365. u8 command, int size, union i2c_smbus_data *data)
  366. {
  367. struct bfin_twi_iface *iface = adap->algo_data;
  368. int rc = 0;
  369. if (!(read_CONTROL(iface) & TWI_ENA))
  370. return -ENXIO;
  371. if (read_MASTER_STAT(iface) & BUSBUSY)
  372. return -EAGAIN;
  373. iface->writeNum = 0;
  374. iface->readNum = 0;
  375. /* Prepare datas & select mode */
  376. switch (size) {
  377. case I2C_SMBUS_QUICK:
  378. iface->transPtr = NULL;
  379. iface->cur_mode = TWI_I2C_MODE_STANDARD;
  380. break;
  381. case I2C_SMBUS_BYTE:
  382. if (data == NULL)
  383. iface->transPtr = NULL;
  384. else {
  385. if (read_write == I2C_SMBUS_READ)
  386. iface->readNum = 1;
  387. else
  388. iface->writeNum = 1;
  389. iface->transPtr = &data->byte;
  390. }
  391. iface->cur_mode = TWI_I2C_MODE_STANDARD;
  392. break;
  393. case I2C_SMBUS_BYTE_DATA:
  394. if (read_write == I2C_SMBUS_READ) {
  395. iface->readNum = 1;
  396. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  397. } else {
  398. iface->writeNum = 1;
  399. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  400. }
  401. iface->transPtr = &data->byte;
  402. break;
  403. case I2C_SMBUS_WORD_DATA:
  404. if (read_write == I2C_SMBUS_READ) {
  405. iface->readNum = 2;
  406. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  407. } else {
  408. iface->writeNum = 2;
  409. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  410. }
  411. iface->transPtr = (u8 *)&data->word;
  412. break;
  413. case I2C_SMBUS_PROC_CALL:
  414. iface->writeNum = 2;
  415. iface->readNum = 2;
  416. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  417. iface->transPtr = (u8 *)&data->word;
  418. break;
  419. case I2C_SMBUS_BLOCK_DATA:
  420. if (read_write == I2C_SMBUS_READ) {
  421. iface->readNum = 0;
  422. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  423. } else {
  424. iface->writeNum = data->block[0] + 1;
  425. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  426. }
  427. iface->transPtr = data->block;
  428. break;
  429. case I2C_SMBUS_I2C_BLOCK_DATA:
  430. if (read_write == I2C_SMBUS_READ) {
  431. iface->readNum = data->block[0];
  432. iface->cur_mode = TWI_I2C_MODE_COMBINED;
  433. } else {
  434. iface->writeNum = data->block[0];
  435. iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
  436. }
  437. iface->transPtr = (u8 *)&data->block[1];
  438. break;
  439. default:
  440. return -1;
  441. }
  442. iface->result = 0;
  443. iface->manual_stop = 0;
  444. iface->read_write = read_write;
  445. iface->command = command;
  446. init_completion(&(iface->complete));
  447. /* FIFO Initiation. Data in FIFO should be discarded before
  448. * start a new operation.
  449. */
  450. write_FIFO_CTL(iface, 0x3);
  451. SSYNC();
  452. write_FIFO_CTL(iface, 0);
  453. /* clear int stat */
  454. write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
  455. /* Set Transmit device address */
  456. write_MASTER_ADDR(iface, addr);
  457. SSYNC();
  458. switch (iface->cur_mode) {
  459. case TWI_I2C_MODE_STANDARDSUB:
  460. write_XMT_DATA8(iface, iface->command);
  461. write_INT_MASK(iface, MCOMP | MERR |
  462. ((iface->read_write == I2C_SMBUS_READ) ?
  463. RCVSERV : XMTSERV));
  464. SSYNC();
  465. if (iface->writeNum + 1 <= 255)
  466. write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
  467. else {
  468. write_MASTER_CTL(iface, 0xff << 6);
  469. iface->manual_stop = 1;
  470. }
  471. /* Master enable */
  472. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  473. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
  474. break;
  475. case TWI_I2C_MODE_COMBINED:
  476. write_XMT_DATA8(iface, iface->command);
  477. write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
  478. SSYNC();
  479. if (iface->writeNum > 0)
  480. write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
  481. else
  482. write_MASTER_CTL(iface, 0x1 << 6);
  483. /* Master enable */
  484. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  485. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
  486. break;
  487. default:
  488. write_MASTER_CTL(iface, 0);
  489. if (size != I2C_SMBUS_QUICK) {
  490. /* Don't access xmit data register when this is a
  491. * read operation.
  492. */
  493. if (iface->read_write != I2C_SMBUS_READ) {
  494. if (iface->writeNum > 0) {
  495. write_XMT_DATA8(iface,
  496. *(iface->transPtr++));
  497. if (iface->writeNum <= 255)
  498. write_MASTER_CTL(iface,
  499. iface->writeNum << 6);
  500. else {
  501. write_MASTER_CTL(iface,
  502. 0xff << 6);
  503. iface->manual_stop = 1;
  504. }
  505. iface->writeNum--;
  506. } else {
  507. write_XMT_DATA8(iface, iface->command);
  508. write_MASTER_CTL(iface, 1 << 6);
  509. }
  510. } else {
  511. if (iface->readNum > 0 && iface->readNum <= 255)
  512. write_MASTER_CTL(iface,
  513. iface->readNum << 6);
  514. else if (iface->readNum > 255) {
  515. write_MASTER_CTL(iface, 0xff << 6);
  516. iface->manual_stop = 1;
  517. } else
  518. break;
  519. }
  520. }
  521. write_INT_MASK(iface, MCOMP | MERR |
  522. ((iface->read_write == I2C_SMBUS_READ) ?
  523. RCVSERV : XMTSERV));
  524. SSYNC();
  525. /* Master enable */
  526. write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
  527. ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
  528. ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
  529. break;
  530. }
  531. SSYNC();
  532. while (!iface->result) {
  533. if (!wait_for_completion_timeout(&iface->complete,
  534. adap->timeout)) {
  535. iface->result = -1;
  536. dev_err(&adap->dev, "smbus transfer timeout\n");
  537. }
  538. }
  539. rc = (iface->result >= 0) ? 0 : -1;
  540. return rc;
  541. }
  542. /*
  543. * Generic I2C SMBus transfer entrypoint
  544. */
  545. int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  546. unsigned short flags, char read_write,
  547. u8 command, int size, union i2c_smbus_data *data)
  548. {
  549. return bfin_twi_do_smbus_xfer(adap, addr, flags,
  550. read_write, command, size, data);
  551. }
  552. /*
  553. * Return what the adapter supports
  554. */
  555. static u32 bfin_twi_functionality(struct i2c_adapter *adap)
  556. {
  557. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  558. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  559. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
  560. I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
  561. }
  562. static struct i2c_algorithm bfin_twi_algorithm = {
  563. .master_xfer = bfin_twi_master_xfer,
  564. .smbus_xfer = bfin_twi_smbus_xfer,
  565. .functionality = bfin_twi_functionality,
  566. };
  567. static int i2c_bfin_twi_suspend(struct device *dev)
  568. {
  569. struct bfin_twi_iface *iface = dev_get_drvdata(dev);
  570. iface->saved_clkdiv = read_CLKDIV(iface);
  571. iface->saved_control = read_CONTROL(iface);
  572. free_irq(iface->irq, iface);
  573. /* Disable TWI */
  574. write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
  575. return 0;
  576. }
  577. static int i2c_bfin_twi_resume(struct device *dev)
  578. {
  579. struct bfin_twi_iface *iface = dev_get_drvdata(dev);
  580. int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
  581. 0, to_platform_device(dev)->name, iface);
  582. if (rc) {
  583. dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
  584. return -ENODEV;
  585. }
  586. /* Resume TWI interface clock as specified */
  587. write_CLKDIV(iface, iface->saved_clkdiv);
  588. /* Resume TWI */
  589. write_CONTROL(iface, iface->saved_control);
  590. return 0;
  591. }
  592. static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
  593. i2c_bfin_twi_suspend, i2c_bfin_twi_resume);
  594. static int i2c_bfin_twi_probe(struct platform_device *pdev)
  595. {
  596. struct bfin_twi_iface *iface;
  597. struct i2c_adapter *p_adap;
  598. struct resource *res;
  599. int rc;
  600. unsigned int clkhilow;
  601. iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
  602. if (!iface) {
  603. dev_err(&pdev->dev, "Cannot allocate memory\n");
  604. rc = -ENOMEM;
  605. goto out_error_nomem;
  606. }
  607. spin_lock_init(&(iface->lock));
  608. /* Find and map our resources */
  609. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  610. if (res == NULL) {
  611. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  612. rc = -ENOENT;
  613. goto out_error_get_res;
  614. }
  615. iface->regs_base = ioremap(res->start, resource_size(res));
  616. if (iface->regs_base == NULL) {
  617. dev_err(&pdev->dev, "Cannot map IO\n");
  618. rc = -ENXIO;
  619. goto out_error_ioremap;
  620. }
  621. iface->irq = platform_get_irq(pdev, 0);
  622. if (iface->irq < 0) {
  623. dev_err(&pdev->dev, "No IRQ specified\n");
  624. rc = -ENOENT;
  625. goto out_error_no_irq;
  626. }
  627. p_adap = &iface->adap;
  628. p_adap->nr = pdev->id;
  629. strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
  630. p_adap->algo = &bfin_twi_algorithm;
  631. p_adap->algo_data = iface;
  632. p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  633. p_adap->dev.parent = &pdev->dev;
  634. p_adap->timeout = 5 * HZ;
  635. p_adap->retries = 3;
  636. rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
  637. if (rc) {
  638. dev_err(&pdev->dev, "Can't setup pin mux!\n");
  639. goto out_error_pin_mux;
  640. }
  641. rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
  642. 0, pdev->name, iface);
  643. if (rc) {
  644. dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
  645. rc = -ENODEV;
  646. goto out_error_req_irq;
  647. }
  648. /* Set TWI internal clock as 10MHz */
  649. write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
  650. /*
  651. * We will not end up with a CLKDIV=0 because no one will specify
  652. * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
  653. */
  654. clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
  655. /* Set Twi interface clock as specified */
  656. write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
  657. /* Enable TWI */
  658. write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
  659. SSYNC();
  660. rc = i2c_add_numbered_adapter(p_adap);
  661. if (rc < 0) {
  662. dev_err(&pdev->dev, "Can't add i2c adapter!\n");
  663. goto out_error_add_adapter;
  664. }
  665. platform_set_drvdata(pdev, iface);
  666. dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
  667. "regs_base@%p\n", iface->regs_base);
  668. return 0;
  669. out_error_add_adapter:
  670. free_irq(iface->irq, iface);
  671. out_error_req_irq:
  672. out_error_no_irq:
  673. peripheral_free_list(pin_req[pdev->id]);
  674. out_error_pin_mux:
  675. iounmap(iface->regs_base);
  676. out_error_ioremap:
  677. out_error_get_res:
  678. kfree(iface);
  679. out_error_nomem:
  680. return rc;
  681. }
  682. static int i2c_bfin_twi_remove(struct platform_device *pdev)
  683. {
  684. struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
  685. platform_set_drvdata(pdev, NULL);
  686. i2c_del_adapter(&(iface->adap));
  687. free_irq(iface->irq, iface);
  688. peripheral_free_list(pin_req[pdev->id]);
  689. iounmap(iface->regs_base);
  690. kfree(iface);
  691. return 0;
  692. }
  693. static struct platform_driver i2c_bfin_twi_driver = {
  694. .probe = i2c_bfin_twi_probe,
  695. .remove = i2c_bfin_twi_remove,
  696. .driver = {
  697. .name = "i2c-bfin-twi",
  698. .owner = THIS_MODULE,
  699. .pm = &i2c_bfin_twi_pm,
  700. },
  701. };
  702. static int __init i2c_bfin_twi_init(void)
  703. {
  704. return platform_driver_register(&i2c_bfin_twi_driver);
  705. }
  706. static void __exit i2c_bfin_twi_exit(void)
  707. {
  708. platform_driver_unregister(&i2c_bfin_twi_driver);
  709. }
  710. subsys_initcall(i2c_bfin_twi_init);
  711. module_exit(i2c_bfin_twi_exit);
  712. MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
  713. MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
  714. MODULE_LICENSE("GPL");
  715. MODULE_ALIAS("platform:i2c-bfin-twi");