i2c-davinci.c 21 KB

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