i2c-davinci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * TI DAVINCI I2C adapter driver.
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Copyright (C) 2007 MontaVista Software Inc.
  6. *
  7. * Updated by Vinod & Sudhakar Feb 2005
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. * ----------------------------------------------------------------------------
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/i2c.h>
  27. #include <linux/clk.h>
  28. #include <linux/errno.h>
  29. #include <linux/sched.h>
  30. #include <linux/err.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/cpufreq.h>
  36. #include <linux/gpio.h>
  37. #include <linux/of_device.h>
  38. #include <linux/platform_data/i2c-davinci.h>
  39. /* ----- global defines ----------------------------------------------- */
  40. #define DAVINCI_I2C_TIMEOUT (1*HZ)
  41. #define DAVINCI_I2C_MAX_TRIES 2
  42. #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
  43. DAVINCI_I2C_IMR_SCD | \
  44. DAVINCI_I2C_IMR_ARDY | \
  45. DAVINCI_I2C_IMR_NACK | \
  46. DAVINCI_I2C_IMR_AL)
  47. #define DAVINCI_I2C_OAR_REG 0x00
  48. #define DAVINCI_I2C_IMR_REG 0x04
  49. #define DAVINCI_I2C_STR_REG 0x08
  50. #define DAVINCI_I2C_CLKL_REG 0x0c
  51. #define DAVINCI_I2C_CLKH_REG 0x10
  52. #define DAVINCI_I2C_CNT_REG 0x14
  53. #define DAVINCI_I2C_DRR_REG 0x18
  54. #define DAVINCI_I2C_SAR_REG 0x1c
  55. #define DAVINCI_I2C_DXR_REG 0x20
  56. #define DAVINCI_I2C_MDR_REG 0x24
  57. #define DAVINCI_I2C_IVR_REG 0x28
  58. #define DAVINCI_I2C_EMDR_REG 0x2c
  59. #define DAVINCI_I2C_PSC_REG 0x30
  60. #define DAVINCI_I2C_IVR_AAS 0x07
  61. #define DAVINCI_I2C_IVR_SCD 0x06
  62. #define DAVINCI_I2C_IVR_XRDY 0x05
  63. #define DAVINCI_I2C_IVR_RDR 0x04
  64. #define DAVINCI_I2C_IVR_ARDY 0x03
  65. #define DAVINCI_I2C_IVR_NACK 0x02
  66. #define DAVINCI_I2C_IVR_AL 0x01
  67. #define DAVINCI_I2C_STR_BB BIT(12)
  68. #define DAVINCI_I2C_STR_RSFULL BIT(11)
  69. #define DAVINCI_I2C_STR_SCD BIT(5)
  70. #define DAVINCI_I2C_STR_ARDY BIT(2)
  71. #define DAVINCI_I2C_STR_NACK BIT(1)
  72. #define DAVINCI_I2C_STR_AL BIT(0)
  73. #define DAVINCI_I2C_MDR_NACK BIT(15)
  74. #define DAVINCI_I2C_MDR_STT BIT(13)
  75. #define DAVINCI_I2C_MDR_STP BIT(11)
  76. #define DAVINCI_I2C_MDR_MST BIT(10)
  77. #define DAVINCI_I2C_MDR_TRX BIT(9)
  78. #define DAVINCI_I2C_MDR_XA BIT(8)
  79. #define DAVINCI_I2C_MDR_RM BIT(7)
  80. #define DAVINCI_I2C_MDR_IRS BIT(5)
  81. #define DAVINCI_I2C_IMR_AAS BIT(6)
  82. #define DAVINCI_I2C_IMR_SCD BIT(5)
  83. #define DAVINCI_I2C_IMR_XRDY BIT(4)
  84. #define DAVINCI_I2C_IMR_RRDY BIT(3)
  85. #define DAVINCI_I2C_IMR_ARDY BIT(2)
  86. #define DAVINCI_I2C_IMR_NACK BIT(1)
  87. #define DAVINCI_I2C_IMR_AL BIT(0)
  88. struct davinci_i2c_dev {
  89. struct device *dev;
  90. void __iomem *base;
  91. struct completion cmd_complete;
  92. struct clk *clk;
  93. int cmd_err;
  94. u8 *buf;
  95. size_t buf_len;
  96. int irq;
  97. int stop;
  98. u8 terminate;
  99. struct i2c_adapter adapter;
  100. #ifdef CONFIG_CPU_FREQ
  101. struct completion xfr_complete;
  102. struct notifier_block freq_transition;
  103. #endif
  104. struct davinci_i2c_platform_data *pdata;
  105. };
  106. /* default platform data to use if not supplied in the platform_device */
  107. static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
  108. .bus_freq = 100,
  109. .bus_delay = 0,
  110. };
  111. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  112. int reg, u16 val)
  113. {
  114. writew_relaxed(val, i2c_dev->base + reg);
  115. }
  116. static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  117. {
  118. return readw_relaxed(i2c_dev->base + reg);
  119. }
  120. /* Generate a pulse on the i2c clock pin. */
  121. static void davinci_i2c_clock_pulse(unsigned int scl_pin)
  122. {
  123. u16 i;
  124. if (scl_pin) {
  125. /* Send high and low on the SCL line */
  126. for (i = 0; i < 9; i++) {
  127. gpio_set_value(scl_pin, 0);
  128. udelay(20);
  129. gpio_set_value(scl_pin, 1);
  130. udelay(20);
  131. }
  132. }
  133. }
  134. /* This routine does i2c bus recovery as specified in the
  135. * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
  136. */
  137. static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev)
  138. {
  139. u32 flag = 0;
  140. struct davinci_i2c_platform_data *pdata = dev->pdata;
  141. dev_err(dev->dev, "initiating i2c bus recovery\n");
  142. /* Send NACK to the slave */
  143. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  144. flag |= DAVINCI_I2C_MDR_NACK;
  145. /* write the data into mode register */
  146. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  147. davinci_i2c_clock_pulse(pdata->scl_pin);
  148. /* Send STOP */
  149. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  150. flag |= DAVINCI_I2C_MDR_STP;
  151. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  152. }
  153. static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
  154. int val)
  155. {
  156. u16 w;
  157. w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
  158. if (!val) /* put I2C into reset */
  159. w &= ~DAVINCI_I2C_MDR_IRS;
  160. else /* take I2C out of reset */
  161. w |= DAVINCI_I2C_MDR_IRS;
  162. davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
  163. }
  164. static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
  165. {
  166. struct davinci_i2c_platform_data *pdata = dev->pdata;
  167. u16 psc;
  168. u32 clk;
  169. u32 d;
  170. u32 clkh;
  171. u32 clkl;
  172. u32 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)) / (pdata->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. dev_dbg(dev->dev, "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. struct davinci_i2c_platform_data *pdata = dev->pdata;
  209. /* put I2C into reset */
  210. davinci_i2c_reset_ctrl(dev, 0);
  211. /* compute clock dividers */
  212. i2c_davinci_calc_clk_dividers(dev);
  213. /* Respond at reserved "SMBus Host" slave address" (and zero);
  214. * we seem to have no option to not respond...
  215. */
  216. davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
  217. dev_dbg(dev->dev, "PSC = %d\n",
  218. davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
  219. dev_dbg(dev->dev, "CLKL = %d\n",
  220. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
  221. dev_dbg(dev->dev, "CLKH = %d\n",
  222. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
  223. dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
  224. pdata->bus_freq, pdata->bus_delay);
  225. /* Take the I2C module out of reset: */
  226. davinci_i2c_reset_ctrl(dev, 1);
  227. /* Enable interrupts */
  228. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
  229. return 0;
  230. }
  231. /*
  232. * Waiting for bus not busy
  233. */
  234. static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
  235. char allow_sleep)
  236. {
  237. unsigned long timeout;
  238. static u16 to_cnt;
  239. timeout = jiffies + dev->adapter.timeout;
  240. while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
  241. & DAVINCI_I2C_STR_BB) {
  242. if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
  243. if (time_after(jiffies, timeout)) {
  244. dev_warn(dev->dev,
  245. "timeout waiting for bus ready\n");
  246. to_cnt++;
  247. return -ETIMEDOUT;
  248. } else {
  249. to_cnt = 0;
  250. davinci_i2c_recover_bus(dev);
  251. i2c_davinci_init(dev);
  252. }
  253. }
  254. if (allow_sleep)
  255. schedule_timeout(1);
  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 i2c_adapter *adap, struct i2c_msg *msg, int stop)
  265. {
  266. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  267. struct davinci_i2c_platform_data *pdata = dev->pdata;
  268. u32 flag;
  269. u16 w;
  270. int r;
  271. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  272. if (pdata->bus_delay)
  273. udelay(pdata->bus_delay);
  274. /* set the slave address */
  275. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  276. dev->buf = msg->buf;
  277. dev->buf_len = msg->len;
  278. dev->stop = stop;
  279. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  280. reinit_completion(&dev->cmd_complete);
  281. dev->cmd_err = 0;
  282. /* Take I2C out of reset and configure it as master */
  283. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
  284. /* if the slave address is ten bit address, enable XA bit */
  285. if (msg->flags & I2C_M_TEN)
  286. flag |= DAVINCI_I2C_MDR_XA;
  287. if (!(msg->flags & I2C_M_RD))
  288. flag |= DAVINCI_I2C_MDR_TRX;
  289. if (msg->len == 0)
  290. flag |= DAVINCI_I2C_MDR_RM;
  291. /* Enable receive or transmit interrupts */
  292. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  293. if (msg->flags & I2C_M_RD)
  294. w |= DAVINCI_I2C_IMR_RRDY;
  295. else
  296. w |= DAVINCI_I2C_IMR_XRDY;
  297. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  298. dev->terminate = 0;
  299. /*
  300. * Write mode register first as needed for correct behaviour
  301. * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
  302. * occurring before we have loaded DXR
  303. */
  304. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  305. /*
  306. * First byte should be set here, not after interrupt,
  307. * because transmit-data-ready interrupt can come before
  308. * NACK-interrupt during sending of previous message and
  309. * ICDXR may have wrong data
  310. * It also saves us one interrupt, slightly faster
  311. */
  312. if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
  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 = wait_for_completion_timeout(&dev->cmd_complete, dev->adapter.timeout);
  322. if (r == 0) {
  323. dev_err(dev->dev, "controller timed out\n");
  324. davinci_i2c_recover_bus(dev);
  325. i2c_davinci_init(dev);
  326. dev->buf_len = 0;
  327. return -ETIMEDOUT;
  328. }
  329. if (dev->buf_len) {
  330. /* This should be 0 if all bytes were transferred
  331. * or dev->cmd_err denotes an error.
  332. */
  333. if (r >= 0) {
  334. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  335. dev->buf_len);
  336. r = -EREMOTEIO;
  337. }
  338. dev->terminate = 1;
  339. wmb();
  340. dev->buf_len = 0;
  341. }
  342. if (r < 0)
  343. return r;
  344. /* no error */
  345. if (likely(!dev->cmd_err))
  346. return msg->len;
  347. /* We have an error */
  348. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  349. i2c_davinci_init(dev);
  350. return -EIO;
  351. }
  352. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  353. if (msg->flags & I2C_M_IGNORE_NAK)
  354. return msg->len;
  355. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  356. w |= DAVINCI_I2C_MDR_STP;
  357. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  358. return -EREMOTEIO;
  359. }
  360. return -EIO;
  361. }
  362. /*
  363. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  364. */
  365. static int
  366. i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  367. {
  368. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  369. int i;
  370. int ret;
  371. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  372. ret = i2c_davinci_wait_bus_not_busy(dev, 1);
  373. if (ret < 0) {
  374. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  375. return ret;
  376. }
  377. for (i = 0; i < num; i++) {
  378. ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  379. dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  380. ret);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. #ifdef CONFIG_CPU_FREQ
  385. complete(&dev->xfr_complete);
  386. #endif
  387. return num;
  388. }
  389. static u32 i2c_davinci_func(struct i2c_adapter *adap)
  390. {
  391. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  392. }
  393. static void terminate_read(struct davinci_i2c_dev *dev)
  394. {
  395. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  396. w |= DAVINCI_I2C_MDR_NACK;
  397. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  398. /* Throw away data */
  399. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  400. if (!dev->terminate)
  401. dev_err(dev->dev, "RDR IRQ while no data requested\n");
  402. }
  403. static void terminate_write(struct davinci_i2c_dev *dev)
  404. {
  405. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  406. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  407. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  408. if (!dev->terminate)
  409. dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
  410. }
  411. /*
  412. * Interrupt service routine. This gets called whenever an I2C interrupt
  413. * occurs.
  414. */
  415. static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
  416. {
  417. struct davinci_i2c_dev *dev = dev_id;
  418. u32 stat;
  419. int count = 0;
  420. u16 w;
  421. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  422. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  423. if (count++ == 100) {
  424. dev_warn(dev->dev, "Too much work in one IRQ\n");
  425. break;
  426. }
  427. switch (stat) {
  428. case DAVINCI_I2C_IVR_AL:
  429. /* Arbitration lost, must retry */
  430. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  431. dev->buf_len = 0;
  432. complete(&dev->cmd_complete);
  433. break;
  434. case DAVINCI_I2C_IVR_NACK:
  435. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  436. dev->buf_len = 0;
  437. complete(&dev->cmd_complete);
  438. break;
  439. case DAVINCI_I2C_IVR_ARDY:
  440. davinci_i2c_write_reg(dev,
  441. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  442. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  443. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  444. w = davinci_i2c_read_reg(dev,
  445. DAVINCI_I2C_MDR_REG);
  446. w |= DAVINCI_I2C_MDR_STP;
  447. davinci_i2c_write_reg(dev,
  448. DAVINCI_I2C_MDR_REG, w);
  449. }
  450. complete(&dev->cmd_complete);
  451. break;
  452. case DAVINCI_I2C_IVR_RDR:
  453. if (dev->buf_len) {
  454. *dev->buf++ =
  455. davinci_i2c_read_reg(dev,
  456. DAVINCI_I2C_DRR_REG);
  457. dev->buf_len--;
  458. if (dev->buf_len)
  459. continue;
  460. davinci_i2c_write_reg(dev,
  461. DAVINCI_I2C_STR_REG,
  462. DAVINCI_I2C_IMR_RRDY);
  463. } else {
  464. /* signal can terminate transfer */
  465. terminate_read(dev);
  466. }
  467. break;
  468. case DAVINCI_I2C_IVR_XRDY:
  469. if (dev->buf_len) {
  470. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  471. *dev->buf++);
  472. dev->buf_len--;
  473. if (dev->buf_len)
  474. continue;
  475. w = davinci_i2c_read_reg(dev,
  476. DAVINCI_I2C_IMR_REG);
  477. w &= ~DAVINCI_I2C_IMR_XRDY;
  478. davinci_i2c_write_reg(dev,
  479. DAVINCI_I2C_IMR_REG,
  480. w);
  481. } else {
  482. /* signal can terminate transfer */
  483. terminate_write(dev);
  484. }
  485. break;
  486. case DAVINCI_I2C_IVR_SCD:
  487. davinci_i2c_write_reg(dev,
  488. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  489. complete(&dev->cmd_complete);
  490. break;
  491. case DAVINCI_I2C_IVR_AAS:
  492. dev_dbg(dev->dev, "Address as slave interrupt\n");
  493. break;
  494. default:
  495. dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
  496. break;
  497. }
  498. }
  499. return count ? IRQ_HANDLED : IRQ_NONE;
  500. }
  501. #ifdef CONFIG_CPU_FREQ
  502. static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
  503. unsigned long val, void *data)
  504. {
  505. struct davinci_i2c_dev *dev;
  506. dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
  507. if (val == CPUFREQ_PRECHANGE) {
  508. wait_for_completion(&dev->xfr_complete);
  509. davinci_i2c_reset_ctrl(dev, 0);
  510. } else if (val == CPUFREQ_POSTCHANGE) {
  511. i2c_davinci_calc_clk_dividers(dev);
  512. davinci_i2c_reset_ctrl(dev, 1);
  513. }
  514. return 0;
  515. }
  516. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  517. {
  518. dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
  519. return cpufreq_register_notifier(&dev->freq_transition,
  520. CPUFREQ_TRANSITION_NOTIFIER);
  521. }
  522. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  523. {
  524. cpufreq_unregister_notifier(&dev->freq_transition,
  525. CPUFREQ_TRANSITION_NOTIFIER);
  526. }
  527. #else
  528. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  529. {
  530. return 0;
  531. }
  532. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  533. {
  534. }
  535. #endif
  536. static struct i2c_algorithm i2c_davinci_algo = {
  537. .master_xfer = i2c_davinci_xfer,
  538. .functionality = i2c_davinci_func,
  539. };
  540. static const struct of_device_id davinci_i2c_of_match[] = {
  541. {.compatible = "ti,davinci-i2c", },
  542. {},
  543. };
  544. MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
  545. static int davinci_i2c_probe(struct platform_device *pdev)
  546. {
  547. struct davinci_i2c_dev *dev;
  548. struct i2c_adapter *adap;
  549. struct resource *mem;
  550. int r, irq;
  551. irq = platform_get_irq(pdev, 0);
  552. if (irq <= 0) {
  553. if (!irq)
  554. irq = -ENXIO;
  555. if (irq != -EPROBE_DEFER)
  556. dev_err(&pdev->dev,
  557. "can't get irq resource ret=%d\n", irq);
  558. return irq;
  559. }
  560. dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
  561. GFP_KERNEL);
  562. if (!dev) {
  563. dev_err(&pdev->dev, "Memory allocation failed\n");
  564. return -ENOMEM;
  565. }
  566. init_completion(&dev->cmd_complete);
  567. #ifdef CONFIG_CPU_FREQ
  568. init_completion(&dev->xfr_complete);
  569. #endif
  570. dev->dev = &pdev->dev;
  571. dev->irq = irq;
  572. dev->pdata = dev_get_platdata(&pdev->dev);
  573. platform_set_drvdata(pdev, dev);
  574. if (!dev->pdata && pdev->dev.of_node) {
  575. u32 prop;
  576. dev->pdata = devm_kzalloc(&pdev->dev,
  577. sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
  578. if (!dev->pdata)
  579. return -ENOMEM;
  580. memcpy(dev->pdata, &davinci_i2c_platform_data_default,
  581. sizeof(struct davinci_i2c_platform_data));
  582. if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  583. &prop))
  584. dev->pdata->bus_freq = prop / 1000;
  585. } else if (!dev->pdata) {
  586. dev->pdata = &davinci_i2c_platform_data_default;
  587. }
  588. dev->clk = devm_clk_get(&pdev->dev, NULL);
  589. if (IS_ERR(dev->clk))
  590. return -ENODEV;
  591. clk_prepare_enable(dev->clk);
  592. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  593. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  594. if (IS_ERR(dev->base)) {
  595. r = PTR_ERR(dev->base);
  596. goto err_unuse_clocks;
  597. }
  598. i2c_davinci_init(dev);
  599. r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
  600. pdev->name, dev);
  601. if (r) {
  602. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  603. goto err_unuse_clocks;
  604. }
  605. r = i2c_davinci_cpufreq_register(dev);
  606. if (r) {
  607. dev_err(&pdev->dev, "failed to register cpufreq\n");
  608. goto err_unuse_clocks;
  609. }
  610. adap = &dev->adapter;
  611. i2c_set_adapdata(adap, dev);
  612. adap->owner = THIS_MODULE;
  613. adap->class = I2C_CLASS_DEPRECATED;
  614. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  615. adap->algo = &i2c_davinci_algo;
  616. adap->dev.parent = &pdev->dev;
  617. adap->timeout = DAVINCI_I2C_TIMEOUT;
  618. adap->dev.of_node = pdev->dev.of_node;
  619. adap->nr = pdev->id;
  620. r = i2c_add_numbered_adapter(adap);
  621. if (r) {
  622. dev_err(&pdev->dev, "failure adding adapter\n");
  623. goto err_unuse_clocks;
  624. }
  625. return 0;
  626. err_unuse_clocks:
  627. clk_disable_unprepare(dev->clk);
  628. dev->clk = NULL;
  629. return r;
  630. }
  631. static int davinci_i2c_remove(struct platform_device *pdev)
  632. {
  633. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  634. i2c_davinci_cpufreq_deregister(dev);
  635. i2c_del_adapter(&dev->adapter);
  636. clk_disable_unprepare(dev->clk);
  637. dev->clk = NULL;
  638. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  639. return 0;
  640. }
  641. #ifdef CONFIG_PM
  642. static int davinci_i2c_suspend(struct device *dev)
  643. {
  644. struct platform_device *pdev = to_platform_device(dev);
  645. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  646. /* put I2C into reset */
  647. davinci_i2c_reset_ctrl(i2c_dev, 0);
  648. clk_disable_unprepare(i2c_dev->clk);
  649. return 0;
  650. }
  651. static int davinci_i2c_resume(struct device *dev)
  652. {
  653. struct platform_device *pdev = to_platform_device(dev);
  654. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  655. clk_prepare_enable(i2c_dev->clk);
  656. /* take I2C out of reset */
  657. davinci_i2c_reset_ctrl(i2c_dev, 1);
  658. return 0;
  659. }
  660. static const struct dev_pm_ops davinci_i2c_pm = {
  661. .suspend = davinci_i2c_suspend,
  662. .resume = davinci_i2c_resume,
  663. };
  664. #define davinci_i2c_pm_ops (&davinci_i2c_pm)
  665. #else
  666. #define davinci_i2c_pm_ops NULL
  667. #endif
  668. /* work with hotplug and coldplug */
  669. MODULE_ALIAS("platform:i2c_davinci");
  670. static struct platform_driver davinci_i2c_driver = {
  671. .probe = davinci_i2c_probe,
  672. .remove = davinci_i2c_remove,
  673. .driver = {
  674. .name = "i2c_davinci",
  675. .pm = davinci_i2c_pm_ops,
  676. .of_match_table = davinci_i2c_of_match,
  677. },
  678. };
  679. /* I2C may be needed to bring up other drivers */
  680. static int __init davinci_i2c_init_driver(void)
  681. {
  682. return platform_driver_register(&davinci_i2c_driver);
  683. }
  684. subsys_initcall(davinci_i2c_init_driver);
  685. static void __exit davinci_i2c_exit_driver(void)
  686. {
  687. platform_driver_unregister(&davinci_i2c_driver);
  688. }
  689. module_exit(davinci_i2c_exit_driver);
  690. MODULE_AUTHOR("Texas Instruments India");
  691. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  692. MODULE_LICENSE("GPL");