i2c-davinci.c 17 KB

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