i2c-bfin-twi.c 19 KB

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