i2c-bfin-twi.c 20 KB

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