i2c-davinci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * File : i2c-davinci.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include <drivers/i2c.h>
  26. #include <dm36x.h>
  27. /* ----- global defines ----------------------------------------------- */
  28. #define BIT(nr) (1UL << (nr))
  29. #define DAVINCI_I2C_TIMEOUT (1*RT_TICK_PER_SECOND)
  30. #define DAVINCI_I2C_MAX_TRIES 2
  31. #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
  32. DAVINCI_I2C_IMR_SCD | \
  33. DAVINCI_I2C_IMR_ARDY | \
  34. DAVINCI_I2C_IMR_NACK | \
  35. DAVINCI_I2C_IMR_AL)
  36. #define DAVINCI_I2C_OAR_REG 0x00
  37. #define DAVINCI_I2C_IMR_REG 0x04
  38. #define DAVINCI_I2C_STR_REG 0x08
  39. #define DAVINCI_I2C_CLKL_REG 0x0c
  40. #define DAVINCI_I2C_CLKH_REG 0x10
  41. #define DAVINCI_I2C_CNT_REG 0x14
  42. #define DAVINCI_I2C_DRR_REG 0x18
  43. #define DAVINCI_I2C_SAR_REG 0x1c
  44. #define DAVINCI_I2C_DXR_REG 0x20
  45. #define DAVINCI_I2C_MDR_REG 0x24
  46. #define DAVINCI_I2C_IVR_REG 0x28
  47. #define DAVINCI_I2C_EMDR_REG 0x2c
  48. #define DAVINCI_I2C_PSC_REG 0x30
  49. #define DAVINCI_I2C_IVR_AAS 0x07
  50. #define DAVINCI_I2C_IVR_SCD 0x06
  51. #define DAVINCI_I2C_IVR_XRDY 0x05
  52. #define DAVINCI_I2C_IVR_RDR 0x04
  53. #define DAVINCI_I2C_IVR_ARDY 0x03
  54. #define DAVINCI_I2C_IVR_NACK 0x02
  55. #define DAVINCI_I2C_IVR_AL 0x01
  56. #define DAVINCI_I2C_STR_BB BIT(12)
  57. #define DAVINCI_I2C_STR_RSFULL BIT(11)
  58. #define DAVINCI_I2C_STR_SCD BIT(5)
  59. #define DAVINCI_I2C_STR_ARDY BIT(2)
  60. #define DAVINCI_I2C_STR_NACK BIT(1)
  61. #define DAVINCI_I2C_STR_AL BIT(0)
  62. #define DAVINCI_I2C_MDR_NACK BIT(15)
  63. #define DAVINCI_I2C_MDR_STT BIT(13)
  64. #define DAVINCI_I2C_MDR_STP BIT(11)
  65. #define DAVINCI_I2C_MDR_MST BIT(10)
  66. #define DAVINCI_I2C_MDR_TRX BIT(9)
  67. #define DAVINCI_I2C_MDR_XA BIT(8)
  68. #define DAVINCI_I2C_MDR_RM BIT(7)
  69. #define DAVINCI_I2C_MDR_IRS BIT(5)
  70. #define DAVINCI_I2C_IMR_AAS BIT(6)
  71. #define DAVINCI_I2C_IMR_SCD BIT(5)
  72. #define DAVINCI_I2C_IMR_XRDY BIT(4)
  73. #define DAVINCI_I2C_IMR_RRDY BIT(3)
  74. #define DAVINCI_I2C_IMR_ARDY BIT(2)
  75. #define DAVINCI_I2C_IMR_NACK BIT(1)
  76. #define DAVINCI_I2C_IMR_AL BIT(0)
  77. #ifdef RT_EDMA_DEBUG
  78. #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  79. #else
  80. #define i2c_dbg(fmt, ...)
  81. #endif
  82. struct davinci_i2c_dev {
  83. void *base;
  84. struct rt_semaphore completion;
  85. struct clk *clk;
  86. int cmd_err;
  87. rt_uint8_t *buf;
  88. rt_uint32_t buf_len;
  89. int irq;
  90. int stop;
  91. rt_uint8_t terminate;
  92. rt_uint32_t bus_freq;
  93. rt_uint32_t bus_delay;
  94. struct rt_i2c_bus_device *bus;
  95. };
  96. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  97. int reg, rt_uint16_t val)
  98. {
  99. davinci_writew(val, i2c_dev->base + reg);
  100. }
  101. static inline rt_uint16_t davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  102. {
  103. return davinci_readw(i2c_dev->base + reg);
  104. }
  105. static void udelay (rt_uint32_t us)
  106. {
  107. rt_int32_t i;
  108. for (; us > 0; us--)
  109. {
  110. i = 50000;
  111. while(i > 0)
  112. {
  113. i--;
  114. }
  115. }
  116. }
  117. #if 0
  118. /* Generate a pulse on the i2c clock pin. */
  119. static void generic_i2c_clock_pulse(unsigned int scl_pin)
  120. {
  121. rt_uint16_t i;
  122. if (scl_pin) {
  123. /* Send high and low on the SCL line */
  124. for (i = 0; i < 9; i++) {
  125. gpio_set_value(scl_pin, 0);
  126. udelay(20);
  127. gpio_set_value(scl_pin, 1);
  128. udelay(20);
  129. }
  130. }
  131. }
  132. #endif
  133. /* This routine does i2c bus recovery as specified in the
  134. * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
  135. */
  136. static void i2c_recover_bus(struct davinci_i2c_dev *dev)
  137. {
  138. rt_uint32_t flag = 0;
  139. i2c_dbg("initiating i2c bus recovery\n");
  140. /* Send NACK to the slave */
  141. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  142. flag |= DAVINCI_I2C_MDR_NACK;
  143. /* write the data into mode register */
  144. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  145. #if 0
  146. if (pdata)
  147. generic_i2c_clock_pulse(pdata->scl_pin);
  148. #endif
  149. /* Send STOP */
  150. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  151. flag |= DAVINCI_I2C_MDR_STP;
  152. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  153. }
  154. static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
  155. int val)
  156. {
  157. rt_uint16_t w;
  158. w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
  159. if (!val) /* put I2C into reset */
  160. w &= ~DAVINCI_I2C_MDR_IRS;
  161. else /* take I2C out of reset */
  162. w |= DAVINCI_I2C_MDR_IRS;
  163. davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
  164. }
  165. static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
  166. {
  167. rt_uint16_t psc;
  168. rt_uint32_t clk;
  169. rt_uint32_t d;
  170. rt_uint32_t clkh;
  171. rt_uint32_t clkl;
  172. rt_uint32_t input_clock = clk_get_rate(dev->clk);
  173. /* NOTE: I2C Clock divider programming info
  174. * As per I2C specs the following formulas provide prescaler
  175. * and low/high divider values
  176. * input clk --> PSC Div -----------> ICCL/H Div --> output clock
  177. * module clk
  178. *
  179. * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
  180. *
  181. * Thus,
  182. * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
  183. *
  184. * where if PSC == 0, d = 7,
  185. * if PSC == 1, d = 6
  186. * if PSC > 1 , d = 5
  187. */
  188. /* get minimum of 7 MHz clock, but max of 12 MHz */
  189. psc = (input_clock / 7000000) - 1;
  190. if ((input_clock / (psc + 1)) > 12000000)
  191. psc++; /* better to run under spec than over */
  192. d = (psc >= 2) ? 5 : 7 - psc;
  193. clk = ((input_clock / (psc + 1)) / (dev->bus_freq * 1000)) - (d << 1);
  194. clkh = clk >> 1;
  195. clkl = clk - clkh;
  196. davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
  197. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
  198. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
  199. i2c_dbg("input_clock = %d, CLK = %d\n", input_clock, clk);
  200. }
  201. /*
  202. * This function configures I2C and brings I2C out of reset.
  203. * This function is called during I2C init function. This function
  204. * also gets called if I2C encounters any errors.
  205. */
  206. static int i2c_davinci_init(struct davinci_i2c_dev *dev)
  207. {
  208. /* put I2C into reset */
  209. davinci_i2c_reset_ctrl(dev, 0);
  210. /* compute clock dividers */
  211. i2c_davinci_calc_clk_dividers(dev);
  212. /* Respond at reserved "SMBus Host" slave address" (and zero);
  213. * we seem to have no option to not respond...
  214. */
  215. davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
  216. i2c_dbg("PSC = %d\n",
  217. davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
  218. i2c_dbg("CLKL = %d\n",
  219. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
  220. i2c_dbg("CLKH = %d\n",
  221. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
  222. i2c_dbg("bus_freq = %dkHz, bus_delay = %d\n",
  223. dev->bus_freq, dev->bus_delay);
  224. /* Take the I2C module out of reset: */
  225. davinci_i2c_reset_ctrl(dev, 1);
  226. /* Enable interrupts */
  227. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
  228. return 0;
  229. }
  230. /*
  231. * Waiting for bus not busy
  232. */
  233. static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
  234. char allow_sleep)
  235. {
  236. unsigned long timeout;
  237. static rt_uint16_t to_cnt;
  238. RT_ASSERT(dev != RT_NULL);
  239. RT_ASSERT(dev->bus != RT_NULL);
  240. timeout = rt_tick_get() + dev->bus->timeout;
  241. while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
  242. & DAVINCI_I2C_STR_BB) {
  243. if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
  244. if (rt_tick_get() >= timeout) {
  245. rt_kprintf("timeout waiting for bus ready\n");
  246. to_cnt++;
  247. return -RT_ETIMEOUT;
  248. } else {
  249. to_cnt = 0;
  250. i2c_recover_bus(dev);
  251. i2c_davinci_init(dev);
  252. }
  253. }
  254. if (allow_sleep)
  255. rt_thread_delay(2);
  256. }
  257. return 0;
  258. }
  259. /*
  260. * Low level master read/write transaction. This function is called
  261. * from i2c_davinci_xfer.
  262. */
  263. static int
  264. i2c_davinci_xfer_msg(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msg, int stop)
  265. {
  266. struct davinci_i2c_dev *dev = bus->priv;
  267. rt_uint32_t flag;
  268. rt_uint16_t w;
  269. int r;
  270. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  271. if (dev->bus_delay)
  272. udelay(dev->bus_delay);
  273. /* set the slave address */
  274. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  275. dev->buf = msg->buf;
  276. dev->buf_len = msg->len;
  277. dev->stop = stop;
  278. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  279. //INIT_COMPLETION(dev->cmd_complete);
  280. dev->cmd_err = 0;
  281. /* Take I2C out of reset and configure it as master */
  282. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
  283. /* if the slave address is ten bit address, enable XA bit */
  284. if (msg->flags & RT_I2C_ADDR_10BIT)
  285. flag |= DAVINCI_I2C_MDR_XA;
  286. if (!(msg->flags & RT_I2C_RD))
  287. flag |= DAVINCI_I2C_MDR_TRX;
  288. if (msg->len == 0)
  289. flag |= DAVINCI_I2C_MDR_RM;
  290. /* Enable receive or transmit interrupts */
  291. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  292. if (msg->flags & RT_I2C_RD)
  293. w |= DAVINCI_I2C_IMR_RRDY;
  294. else
  295. w |= DAVINCI_I2C_IMR_XRDY;
  296. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  297. dev->terminate = 0;
  298. /*
  299. * Write mode register first as needed for correct behaviour
  300. * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
  301. * occurring before we have loaded DXR
  302. */
  303. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  304. /*
  305. * First byte should be set here, not after interrupt,
  306. * because transmit-data-ready interrupt can come before
  307. * NACK-interrupt during sending of previous message and
  308. * ICDXR may have wrong data
  309. * It also saves us one interrupt, slightly faster
  310. */
  311. if ((!(msg->flags & RT_I2C_RD)) && dev->buf_len)
  312. {
  313. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
  314. dev->buf_len--;
  315. }
  316. /* Set STT to begin transmit now DXR is loaded */
  317. flag |= DAVINCI_I2C_MDR_STT;
  318. if (stop && msg->len != 0)
  319. flag |= DAVINCI_I2C_MDR_STP;
  320. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  321. r = rt_sem_take(&dev->completion, dev->bus->timeout);
  322. if (r == -RT_ETIMEOUT)
  323. {
  324. rt_kprintf("controller timed out\n");
  325. i2c_recover_bus(dev);
  326. i2c_davinci_init(dev);
  327. dev->buf_len = 0;
  328. return -RT_ETIMEOUT;
  329. }
  330. if (dev->buf_len)
  331. {
  332. /* This should be 0 if all bytes were transferred
  333. * or dev->cmd_err denotes an error.
  334. * A signal may have aborted the transfer.
  335. */
  336. if (r == RT_EOK)
  337. {
  338. rt_kprintf("abnormal termination buf_len=%i\n",
  339. dev->buf_len);
  340. r = -RT_EIO;
  341. }
  342. dev->terminate = 1;
  343. dev->buf_len = 0;
  344. }
  345. if (r < 0)
  346. return r;
  347. /* no error */
  348. if (!dev->cmd_err)
  349. return msg->len;
  350. /* We have an error */
  351. if (dev->cmd_err & DAVINCI_I2C_STR_AL)
  352. {
  353. i2c_davinci_init(dev);
  354. return -RT_EIO;
  355. }
  356. if (dev->cmd_err & DAVINCI_I2C_STR_NACK)
  357. {
  358. if (msg->flags & RT_I2C_IGNORE_NACK)
  359. return msg->len;
  360. if (stop)
  361. {
  362. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  363. w |= DAVINCI_I2C_MDR_STP;
  364. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  365. }
  366. return -RT_EIO;
  367. }
  368. return -RT_EIO;
  369. }
  370. /*
  371. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  372. */
  373. static int
  374. i2c_davinci_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], int num)
  375. {
  376. struct davinci_i2c_dev *dev = bus->priv;
  377. int i;
  378. int ret;
  379. i2c_dbg("%s: msgs: %d\n", __func__, num);
  380. ret = i2c_davinci_wait_bus_not_busy(dev, 1);
  381. if (ret < 0)
  382. {
  383. i2c_dbg("timeout waiting for bus ready\n");
  384. return ret;
  385. }
  386. for (i = 0; i < num; i++)
  387. {
  388. ret = i2c_davinci_xfer_msg(bus, &msgs[i], (i == (num - 1)));
  389. i2c_dbg("%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  390. ret);
  391. if (ret < 0)
  392. return ret;
  393. }
  394. return num;
  395. }
  396. static void terminate_read(struct davinci_i2c_dev *dev)
  397. {
  398. rt_uint16_t w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  399. w |= DAVINCI_I2C_MDR_NACK;
  400. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  401. /* Throw away data */
  402. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  403. if (!dev->terminate)
  404. rt_kprintf("RDR IRQ while no data requested\n");
  405. }
  406. static void terminate_write(struct davinci_i2c_dev *dev)
  407. {
  408. rt_uint16_t w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  409. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  410. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  411. if (!dev->terminate)
  412. i2c_dbg("TDR IRQ while no data to send\n");
  413. }
  414. /*
  415. * Interrupt service routine. This gets called whenever an I2C interrupt
  416. * occurs.
  417. */
  418. static void i2c_davinci_isr(int irq, void *param)
  419. {
  420. struct davinci_i2c_dev *dev = (struct davinci_i2c_dev *)param;
  421. rt_uint32_t stat;
  422. int count = 0;
  423. rt_uint16_t w;
  424. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  425. i2c_dbg("%s: stat=0x%x\n", __func__, stat);
  426. if (count++ == 100) {
  427. rt_kprintf("Too much work in one IRQ\n");
  428. break;
  429. }
  430. switch (stat) {
  431. case DAVINCI_I2C_IVR_AL:
  432. /* Arbitration lost, must retry */
  433. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  434. dev->buf_len = 0;
  435. rt_sem_release(&dev->completion);
  436. break;
  437. case DAVINCI_I2C_IVR_NACK:
  438. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  439. dev->buf_len = 0;
  440. rt_sem_release(&dev->completion);
  441. break;
  442. case DAVINCI_I2C_IVR_ARDY:
  443. davinci_i2c_write_reg(dev,
  444. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  445. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  446. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  447. w = davinci_i2c_read_reg(dev,
  448. DAVINCI_I2C_MDR_REG);
  449. w |= DAVINCI_I2C_MDR_STP;
  450. davinci_i2c_write_reg(dev,
  451. DAVINCI_I2C_MDR_REG, w);
  452. }
  453. rt_sem_release(&dev->completion);
  454. break;
  455. case DAVINCI_I2C_IVR_RDR:
  456. if (dev->buf_len) {
  457. *dev->buf++ =
  458. davinci_i2c_read_reg(dev,
  459. DAVINCI_I2C_DRR_REG);
  460. dev->buf_len--;
  461. if (dev->buf_len)
  462. continue;
  463. davinci_i2c_write_reg(dev,
  464. DAVINCI_I2C_STR_REG,
  465. DAVINCI_I2C_IMR_RRDY);
  466. } else {
  467. /* signal can terminate transfer */
  468. terminate_read(dev);
  469. }
  470. break;
  471. case DAVINCI_I2C_IVR_XRDY:
  472. if (dev->buf_len) {
  473. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  474. *dev->buf++);
  475. dev->buf_len--;
  476. if (dev->buf_len)
  477. continue;
  478. w = davinci_i2c_read_reg(dev,
  479. DAVINCI_I2C_IMR_REG);
  480. w &= ~DAVINCI_I2C_IMR_XRDY;
  481. davinci_i2c_write_reg(dev,
  482. DAVINCI_I2C_IMR_REG,
  483. w);
  484. } else {
  485. /* signal can terminate transfer */
  486. terminate_write(dev);
  487. }
  488. break;
  489. case DAVINCI_I2C_IVR_SCD:
  490. davinci_i2c_write_reg(dev,
  491. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  492. rt_sem_release(&dev->completion);
  493. break;
  494. case DAVINCI_I2C_IVR_AAS:
  495. i2c_dbg("Address as slave interrupt\n");
  496. break;
  497. default:
  498. i2c_dbg("Unrecognized irq stat %d\n", stat);
  499. break;
  500. }
  501. }
  502. }
  503. static struct rt_i2c_bus_device_ops bus_ops = {
  504. .master_xfer = i2c_davinci_xfer,
  505. };
  506. int davinci_i2c_init(char *bus_name)
  507. {
  508. struct rt_i2c_bus_device *bus;
  509. struct davinci_i2c_dev *dev;
  510. int r;
  511. bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
  512. if (bus == RT_NULL)
  513. {
  514. rt_kprintf("rt_malloc failed\n");
  515. return -RT_ENOMEM;
  516. }
  517. rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));
  518. bus->ops = &bus_ops;
  519. bus->timeout = DAVINCI_I2C_TIMEOUT;
  520. dev = rt_malloc(sizeof(struct davinci_i2c_dev));
  521. if (!dev)
  522. {
  523. r = -RT_ENOMEM;
  524. goto err;
  525. }
  526. rt_memset((void *)dev, 0, sizeof(struct davinci_i2c_dev));
  527. rt_sem_init(&dev->completion, "i2c_ack", 0, RT_IPC_FLAG_FIFO);
  528. dev->irq = IRQ_I2C;
  529. dev->clk = clk_get("I2CCLK");
  530. if (dev->clk == RT_NULL) {
  531. r = -RT_ERROR;
  532. goto err1;
  533. }
  534. psc_change_state(DAVINCI_DM365_LPSC_I2C, 3);
  535. dev->base = DAVINCI_I2C_BASE;
  536. dev->bus_freq = 100;
  537. dev->bus_delay = 0;
  538. dev->bus = bus;
  539. bus->priv = dev;
  540. i2c_davinci_init(dev);
  541. rt_hw_interrupt_install(dev->irq, i2c_davinci_isr, (void *)dev, "I2C");
  542. rt_hw_interrupt_umask(dev->irq);
  543. return rt_i2c_bus_device_register(bus, bus_name);
  544. err1:
  545. rt_free(dev);
  546. err:
  547. rt_free(bus);
  548. return r;
  549. }