i2c-bfin-twi.c 19 KB

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