i2c-davinci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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_interruptible_timeout(&dev->cmd_complete,
  322. dev->adapter.timeout);
  323. if (r == 0) {
  324. dev_err(dev->dev, "controller timed out\n");
  325. davinci_i2c_recover_bus(dev);
  326. i2c_davinci_init(dev);
  327. dev->buf_len = 0;
  328. return -ETIMEDOUT;
  329. }
  330. if (dev->buf_len) {
  331. /* This should be 0 if all bytes were transferred
  332. * or dev->cmd_err denotes an error.
  333. * A signal may have aborted the transfer.
  334. */
  335. if (r >= 0) {
  336. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  337. dev->buf_len);
  338. r = -EREMOTEIO;
  339. }
  340. dev->terminate = 1;
  341. wmb();
  342. dev->buf_len = 0;
  343. }
  344. if (r < 0)
  345. return r;
  346. /* no error */
  347. if (likely(!dev->cmd_err))
  348. return msg->len;
  349. /* We have an error */
  350. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  351. i2c_davinci_init(dev);
  352. return -EIO;
  353. }
  354. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  355. if (msg->flags & I2C_M_IGNORE_NAK)
  356. return msg->len;
  357. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  358. w |= DAVINCI_I2C_MDR_STP;
  359. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  360. return -EREMOTEIO;
  361. }
  362. return -EIO;
  363. }
  364. /*
  365. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  366. */
  367. static int
  368. i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  369. {
  370. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  371. int i;
  372. int ret;
  373. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  374. ret = i2c_davinci_wait_bus_not_busy(dev, 1);
  375. if (ret < 0) {
  376. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  377. return ret;
  378. }
  379. for (i = 0; i < num; i++) {
  380. ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  381. dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  382. ret);
  383. if (ret < 0)
  384. return ret;
  385. }
  386. #ifdef CONFIG_CPU_FREQ
  387. complete(&dev->xfr_complete);
  388. #endif
  389. return num;
  390. }
  391. static u32 i2c_davinci_func(struct i2c_adapter *adap)
  392. {
  393. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  394. }
  395. static void terminate_read(struct davinci_i2c_dev *dev)
  396. {
  397. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  398. w |= DAVINCI_I2C_MDR_NACK;
  399. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  400. /* Throw away data */
  401. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  402. if (!dev->terminate)
  403. dev_err(dev->dev, "RDR IRQ while no data requested\n");
  404. }
  405. static void terminate_write(struct davinci_i2c_dev *dev)
  406. {
  407. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  408. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  409. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  410. if (!dev->terminate)
  411. dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
  412. }
  413. /*
  414. * Interrupt service routine. This gets called whenever an I2C interrupt
  415. * occurs.
  416. */
  417. static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
  418. {
  419. struct davinci_i2c_dev *dev = dev_id;
  420. u32 stat;
  421. int count = 0;
  422. u16 w;
  423. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  424. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  425. if (count++ == 100) {
  426. dev_warn(dev->dev, "Too much work in one IRQ\n");
  427. break;
  428. }
  429. switch (stat) {
  430. case DAVINCI_I2C_IVR_AL:
  431. /* Arbitration lost, must retry */
  432. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  433. dev->buf_len = 0;
  434. complete(&dev->cmd_complete);
  435. break;
  436. case DAVINCI_I2C_IVR_NACK:
  437. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  438. dev->buf_len = 0;
  439. complete(&dev->cmd_complete);
  440. break;
  441. case DAVINCI_I2C_IVR_ARDY:
  442. davinci_i2c_write_reg(dev,
  443. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  444. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  445. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  446. w = davinci_i2c_read_reg(dev,
  447. DAVINCI_I2C_MDR_REG);
  448. w |= DAVINCI_I2C_MDR_STP;
  449. davinci_i2c_write_reg(dev,
  450. DAVINCI_I2C_MDR_REG, w);
  451. }
  452. complete(&dev->cmd_complete);
  453. break;
  454. case DAVINCI_I2C_IVR_RDR:
  455. if (dev->buf_len) {
  456. *dev->buf++ =
  457. davinci_i2c_read_reg(dev,
  458. DAVINCI_I2C_DRR_REG);
  459. dev->buf_len--;
  460. if (dev->buf_len)
  461. continue;
  462. davinci_i2c_write_reg(dev,
  463. DAVINCI_I2C_STR_REG,
  464. DAVINCI_I2C_IMR_RRDY);
  465. } else {
  466. /* signal can terminate transfer */
  467. terminate_read(dev);
  468. }
  469. break;
  470. case DAVINCI_I2C_IVR_XRDY:
  471. if (dev->buf_len) {
  472. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  473. *dev->buf++);
  474. dev->buf_len--;
  475. if (dev->buf_len)
  476. continue;
  477. w = davinci_i2c_read_reg(dev,
  478. DAVINCI_I2C_IMR_REG);
  479. w &= ~DAVINCI_I2C_IMR_XRDY;
  480. davinci_i2c_write_reg(dev,
  481. DAVINCI_I2C_IMR_REG,
  482. w);
  483. } else {
  484. /* signal can terminate transfer */
  485. terminate_write(dev);
  486. }
  487. break;
  488. case DAVINCI_I2C_IVR_SCD:
  489. davinci_i2c_write_reg(dev,
  490. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  491. complete(&dev->cmd_complete);
  492. break;
  493. case DAVINCI_I2C_IVR_AAS:
  494. dev_dbg(dev->dev, "Address as slave interrupt\n");
  495. break;
  496. default:
  497. dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
  498. break;
  499. }
  500. }
  501. return count ? IRQ_HANDLED : IRQ_NONE;
  502. }
  503. #ifdef CONFIG_CPU_FREQ
  504. static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
  505. unsigned long val, void *data)
  506. {
  507. struct davinci_i2c_dev *dev;
  508. dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
  509. if (val == CPUFREQ_PRECHANGE) {
  510. wait_for_completion(&dev->xfr_complete);
  511. davinci_i2c_reset_ctrl(dev, 0);
  512. } else if (val == CPUFREQ_POSTCHANGE) {
  513. i2c_davinci_calc_clk_dividers(dev);
  514. davinci_i2c_reset_ctrl(dev, 1);
  515. }
  516. return 0;
  517. }
  518. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  519. {
  520. dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
  521. return cpufreq_register_notifier(&dev->freq_transition,
  522. CPUFREQ_TRANSITION_NOTIFIER);
  523. }
  524. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  525. {
  526. cpufreq_unregister_notifier(&dev->freq_transition,
  527. CPUFREQ_TRANSITION_NOTIFIER);
  528. }
  529. #else
  530. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  531. {
  532. return 0;
  533. }
  534. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  535. {
  536. }
  537. #endif
  538. static struct i2c_algorithm i2c_davinci_algo = {
  539. .master_xfer = i2c_davinci_xfer,
  540. .functionality = i2c_davinci_func,
  541. };
  542. static const struct of_device_id davinci_i2c_of_match[] = {
  543. {.compatible = "ti,davinci-i2c", },
  544. {},
  545. };
  546. MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
  547. static int davinci_i2c_probe(struct platform_device *pdev)
  548. {
  549. struct davinci_i2c_dev *dev;
  550. struct i2c_adapter *adap;
  551. struct resource *mem, *irq;
  552. int r;
  553. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  554. if (!irq) {
  555. dev_err(&pdev->dev, "no irq resource?\n");
  556. return -ENODEV;
  557. }
  558. dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
  559. GFP_KERNEL);
  560. if (!dev) {
  561. dev_err(&pdev->dev, "Memory allocation failed\n");
  562. return -ENOMEM;
  563. }
  564. init_completion(&dev->cmd_complete);
  565. #ifdef CONFIG_CPU_FREQ
  566. init_completion(&dev->xfr_complete);
  567. #endif
  568. dev->dev = &pdev->dev;
  569. dev->irq = irq->start;
  570. dev->pdata = dev_get_platdata(&pdev->dev);
  571. platform_set_drvdata(pdev, dev);
  572. if (!dev->pdata && pdev->dev.of_node) {
  573. u32 prop;
  574. dev->pdata = devm_kzalloc(&pdev->dev,
  575. sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
  576. if (!dev->pdata)
  577. return -ENOMEM;
  578. memcpy(dev->pdata, &davinci_i2c_platform_data_default,
  579. sizeof(struct davinci_i2c_platform_data));
  580. if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  581. &prop))
  582. dev->pdata->bus_freq = prop / 1000;
  583. } else if (!dev->pdata) {
  584. dev->pdata = &davinci_i2c_platform_data_default;
  585. }
  586. dev->clk = devm_clk_get(&pdev->dev, NULL);
  587. if (IS_ERR(dev->clk))
  588. return -ENODEV;
  589. clk_prepare_enable(dev->clk);
  590. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  591. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  592. if (IS_ERR(dev->base)) {
  593. r = PTR_ERR(dev->base);
  594. goto err_unuse_clocks;
  595. }
  596. i2c_davinci_init(dev);
  597. r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
  598. pdev->name, dev);
  599. if (r) {
  600. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  601. goto err_unuse_clocks;
  602. }
  603. r = i2c_davinci_cpufreq_register(dev);
  604. if (r) {
  605. dev_err(&pdev->dev, "failed to register cpufreq\n");
  606. goto err_unuse_clocks;
  607. }
  608. adap = &dev->adapter;
  609. i2c_set_adapdata(adap, dev);
  610. adap->owner = THIS_MODULE;
  611. adap->class = I2C_CLASS_DEPRECATED;
  612. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  613. adap->algo = &i2c_davinci_algo;
  614. adap->dev.parent = &pdev->dev;
  615. adap->timeout = DAVINCI_I2C_TIMEOUT;
  616. adap->dev.of_node = pdev->dev.of_node;
  617. adap->nr = pdev->id;
  618. r = i2c_add_numbered_adapter(adap);
  619. if (r) {
  620. dev_err(&pdev->dev, "failure adding adapter\n");
  621. goto err_unuse_clocks;
  622. }
  623. return 0;
  624. err_unuse_clocks:
  625. clk_disable_unprepare(dev->clk);
  626. dev->clk = NULL;
  627. return r;
  628. }
  629. static int davinci_i2c_remove(struct platform_device *pdev)
  630. {
  631. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  632. i2c_davinci_cpufreq_deregister(dev);
  633. i2c_del_adapter(&dev->adapter);
  634. clk_disable_unprepare(dev->clk);
  635. dev->clk = NULL;
  636. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  637. return 0;
  638. }
  639. #ifdef CONFIG_PM
  640. static int davinci_i2c_suspend(struct device *dev)
  641. {
  642. struct platform_device *pdev = to_platform_device(dev);
  643. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  644. /* put I2C into reset */
  645. davinci_i2c_reset_ctrl(i2c_dev, 0);
  646. clk_disable_unprepare(i2c_dev->clk);
  647. return 0;
  648. }
  649. static int davinci_i2c_resume(struct device *dev)
  650. {
  651. struct platform_device *pdev = to_platform_device(dev);
  652. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  653. clk_prepare_enable(i2c_dev->clk);
  654. /* take I2C out of reset */
  655. davinci_i2c_reset_ctrl(i2c_dev, 1);
  656. return 0;
  657. }
  658. static const struct dev_pm_ops davinci_i2c_pm = {
  659. .suspend = davinci_i2c_suspend,
  660. .resume = davinci_i2c_resume,
  661. };
  662. #define davinci_i2c_pm_ops (&davinci_i2c_pm)
  663. #else
  664. #define davinci_i2c_pm_ops NULL
  665. #endif
  666. /* work with hotplug and coldplug */
  667. MODULE_ALIAS("platform:i2c_davinci");
  668. static struct platform_driver davinci_i2c_driver = {
  669. .probe = davinci_i2c_probe,
  670. .remove = davinci_i2c_remove,
  671. .driver = {
  672. .name = "i2c_davinci",
  673. .owner = THIS_MODULE,
  674. .pm = davinci_i2c_pm_ops,
  675. .of_match_table = davinci_i2c_of_match,
  676. },
  677. };
  678. /* I2C may be needed to bring up other drivers */
  679. static int __init davinci_i2c_init_driver(void)
  680. {
  681. return platform_driver_register(&davinci_i2c_driver);
  682. }
  683. subsys_initcall(davinci_i2c_init_driver);
  684. static void __exit davinci_i2c_exit_driver(void)
  685. {
  686. platform_driver_unregister(&davinci_i2c_driver);
  687. }
  688. module_exit(davinci_i2c_exit_driver);
  689. MODULE_AUTHOR("Texas Instruments India");
  690. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  691. MODULE_LICENSE("GPL");