i2c-davinci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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 <mach/hardware.h>
  42. #include <mach/i2c.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. };
  109. /* default platform data to use if not supplied in the platform_device */
  110. static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
  111. .bus_freq = 100,
  112. .bus_delay = 0,
  113. };
  114. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  115. int reg, u16 val)
  116. {
  117. __raw_writew(val, i2c_dev->base + reg);
  118. }
  119. static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  120. {
  121. return __raw_readw(i2c_dev->base + reg);
  122. }
  123. /* Generate a pulse on the i2c clock pin. */
  124. static void generic_i2c_clock_pulse(unsigned int scl_pin)
  125. {
  126. u16 i;
  127. if (scl_pin) {
  128. /* Send high and low on the SCL line */
  129. for (i = 0; i < 9; i++) {
  130. gpio_set_value(scl_pin, 0);
  131. udelay(20);
  132. gpio_set_value(scl_pin, 1);
  133. udelay(20);
  134. }
  135. }
  136. }
  137. /* This routine does i2c bus recovery as specified in the
  138. * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
  139. */
  140. static void i2c_recover_bus(struct davinci_i2c_dev *dev)
  141. {
  142. u32 flag = 0;
  143. struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
  144. dev_err(dev->dev, "initiating i2c bus recovery\n");
  145. /* Send NACK to the slave */
  146. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  147. flag |= DAVINCI_I2C_MDR_NACK;
  148. /* write the data into mode register */
  149. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  150. if (pdata)
  151. generic_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->dev->platform_data;
  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->dev->platform_data;
  213. if (!pdata)
  214. pdata = &davinci_i2c_platform_data_default;
  215. /* put I2C into reset */
  216. davinci_i2c_reset_ctrl(dev, 0);
  217. /* compute clock dividers */
  218. i2c_davinci_calc_clk_dividers(dev);
  219. /* Respond at reserved "SMBus Host" slave address" (and zero);
  220. * we seem to have no option to not respond...
  221. */
  222. davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
  223. dev_dbg(dev->dev, "PSC = %d\n",
  224. davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
  225. dev_dbg(dev->dev, "CLKL = %d\n",
  226. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
  227. dev_dbg(dev->dev, "CLKH = %d\n",
  228. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
  229. dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
  230. pdata->bus_freq, pdata->bus_delay);
  231. /* Take the I2C module out of reset: */
  232. davinci_i2c_reset_ctrl(dev, 1);
  233. /* Enable interrupts */
  234. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
  235. return 0;
  236. }
  237. /*
  238. * Waiting for bus not busy
  239. */
  240. static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
  241. char allow_sleep)
  242. {
  243. unsigned long timeout;
  244. static u16 to_cnt;
  245. timeout = jiffies + dev->adapter.timeout;
  246. while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
  247. & DAVINCI_I2C_STR_BB) {
  248. if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
  249. if (time_after(jiffies, timeout)) {
  250. dev_warn(dev->dev,
  251. "timeout waiting for bus ready\n");
  252. to_cnt++;
  253. return -ETIMEDOUT;
  254. } else {
  255. to_cnt = 0;
  256. i2c_recover_bus(dev);
  257. i2c_davinci_init(dev);
  258. }
  259. }
  260. if (allow_sleep)
  261. schedule_timeout(1);
  262. }
  263. return 0;
  264. }
  265. /*
  266. * Low level master read/write transaction. This function is called
  267. * from i2c_davinci_xfer.
  268. */
  269. static int
  270. i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
  271. {
  272. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  273. struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
  274. u32 flag;
  275. u16 w;
  276. int r;
  277. if (!pdata)
  278. pdata = &davinci_i2c_platform_data_default;
  279. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  280. if (pdata->bus_delay)
  281. udelay(pdata->bus_delay);
  282. /* set the slave address */
  283. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  284. dev->buf = msg->buf;
  285. dev->buf_len = msg->len;
  286. dev->stop = stop;
  287. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  288. INIT_COMPLETION(dev->cmd_complete);
  289. dev->cmd_err = 0;
  290. /* Take I2C out of reset and configure it as master */
  291. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
  292. /* if the slave address is ten bit address, enable XA bit */
  293. if (msg->flags & I2C_M_TEN)
  294. flag |= DAVINCI_I2C_MDR_XA;
  295. if (!(msg->flags & I2C_M_RD))
  296. flag |= DAVINCI_I2C_MDR_TRX;
  297. if (msg->len == 0)
  298. flag |= DAVINCI_I2C_MDR_RM;
  299. /* Enable receive or transmit interrupts */
  300. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  301. if (msg->flags & I2C_M_RD)
  302. w |= DAVINCI_I2C_IMR_RRDY;
  303. else
  304. w |= DAVINCI_I2C_IMR_XRDY;
  305. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  306. dev->terminate = 0;
  307. /*
  308. * Write mode register first as needed for correct behaviour
  309. * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
  310. * occuring before we have loaded DXR
  311. */
  312. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  313. /*
  314. * First byte should be set here, not after interrupt,
  315. * because transmit-data-ready interrupt can come before
  316. * NACK-interrupt during sending of previous message and
  317. * ICDXR may have wrong data
  318. * It also saves us one interrupt, slightly faster
  319. */
  320. if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
  321. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
  322. dev->buf_len--;
  323. }
  324. /* Set STT to begin transmit now DXR is loaded */
  325. flag |= DAVINCI_I2C_MDR_STT;
  326. if (stop && msg->len != 0)
  327. flag |= DAVINCI_I2C_MDR_STP;
  328. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  329. r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  330. dev->adapter.timeout);
  331. if (r == 0) {
  332. dev_err(dev->dev, "controller timed out\n");
  333. i2c_recover_bus(dev);
  334. i2c_davinci_init(dev);
  335. dev->buf_len = 0;
  336. return -ETIMEDOUT;
  337. }
  338. if (dev->buf_len) {
  339. /* This should be 0 if all bytes were transferred
  340. * or dev->cmd_err denotes an error.
  341. * A signal may have aborted the transfer.
  342. */
  343. if (r >= 0) {
  344. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  345. dev->buf_len);
  346. r = -EREMOTEIO;
  347. }
  348. dev->terminate = 1;
  349. wmb();
  350. dev->buf_len = 0;
  351. }
  352. if (r < 0)
  353. return r;
  354. /* no error */
  355. if (likely(!dev->cmd_err))
  356. return msg->len;
  357. /* We have an error */
  358. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  359. i2c_davinci_init(dev);
  360. return -EIO;
  361. }
  362. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  363. if (msg->flags & I2C_M_IGNORE_NAK)
  364. return msg->len;
  365. if (stop) {
  366. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  367. w |= DAVINCI_I2C_MDR_STP;
  368. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  369. }
  370. return -EREMOTEIO;
  371. }
  372. return -EIO;
  373. }
  374. /*
  375. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  376. */
  377. static int
  378. i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  379. {
  380. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  381. int i;
  382. int ret;
  383. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  384. ret = i2c_davinci_wait_bus_not_busy(dev, 1);
  385. if (ret < 0) {
  386. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  387. return ret;
  388. }
  389. for (i = 0; i < num; i++) {
  390. ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  391. dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  392. ret);
  393. if (ret < 0)
  394. return ret;
  395. }
  396. #ifdef CONFIG_CPU_FREQ
  397. complete(&dev->xfr_complete);
  398. #endif
  399. return num;
  400. }
  401. static u32 i2c_davinci_func(struct i2c_adapter *adap)
  402. {
  403. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  404. }
  405. static void terminate_read(struct davinci_i2c_dev *dev)
  406. {
  407. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  408. w |= DAVINCI_I2C_MDR_NACK;
  409. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  410. /* Throw away data */
  411. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  412. if (!dev->terminate)
  413. dev_err(dev->dev, "RDR IRQ while no data requested\n");
  414. }
  415. static void terminate_write(struct davinci_i2c_dev *dev)
  416. {
  417. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  418. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  419. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  420. if (!dev->terminate)
  421. dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
  422. }
  423. /*
  424. * Interrupt service routine. This gets called whenever an I2C interrupt
  425. * occurs.
  426. */
  427. static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
  428. {
  429. struct davinci_i2c_dev *dev = dev_id;
  430. u32 stat;
  431. int count = 0;
  432. u16 w;
  433. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  434. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  435. if (count++ == 100) {
  436. dev_warn(dev->dev, "Too much work in one IRQ\n");
  437. break;
  438. }
  439. switch (stat) {
  440. case DAVINCI_I2C_IVR_AL:
  441. /* Arbitration lost, must retry */
  442. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  443. dev->buf_len = 0;
  444. complete(&dev->cmd_complete);
  445. break;
  446. case DAVINCI_I2C_IVR_NACK:
  447. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  448. dev->buf_len = 0;
  449. complete(&dev->cmd_complete);
  450. break;
  451. case DAVINCI_I2C_IVR_ARDY:
  452. davinci_i2c_write_reg(dev,
  453. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  454. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  455. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  456. w = davinci_i2c_read_reg(dev,
  457. DAVINCI_I2C_MDR_REG);
  458. w |= DAVINCI_I2C_MDR_STP;
  459. davinci_i2c_write_reg(dev,
  460. DAVINCI_I2C_MDR_REG, w);
  461. }
  462. complete(&dev->cmd_complete);
  463. break;
  464. case DAVINCI_I2C_IVR_RDR:
  465. if (dev->buf_len) {
  466. *dev->buf++ =
  467. davinci_i2c_read_reg(dev,
  468. DAVINCI_I2C_DRR_REG);
  469. dev->buf_len--;
  470. if (dev->buf_len)
  471. continue;
  472. davinci_i2c_write_reg(dev,
  473. DAVINCI_I2C_STR_REG,
  474. DAVINCI_I2C_IMR_RRDY);
  475. } else {
  476. /* signal can terminate transfer */
  477. terminate_read(dev);
  478. }
  479. break;
  480. case DAVINCI_I2C_IVR_XRDY:
  481. if (dev->buf_len) {
  482. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  483. *dev->buf++);
  484. dev->buf_len--;
  485. if (dev->buf_len)
  486. continue;
  487. w = davinci_i2c_read_reg(dev,
  488. DAVINCI_I2C_IMR_REG);
  489. w &= ~DAVINCI_I2C_IMR_XRDY;
  490. davinci_i2c_write_reg(dev,
  491. DAVINCI_I2C_IMR_REG,
  492. w);
  493. } else {
  494. /* signal can terminate transfer */
  495. terminate_write(dev);
  496. }
  497. break;
  498. case DAVINCI_I2C_IVR_SCD:
  499. davinci_i2c_write_reg(dev,
  500. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  501. complete(&dev->cmd_complete);
  502. break;
  503. case DAVINCI_I2C_IVR_AAS:
  504. dev_dbg(dev->dev, "Address as slave interrupt\n");
  505. break;
  506. default:
  507. dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
  508. break;
  509. }
  510. }
  511. return count ? IRQ_HANDLED : IRQ_NONE;
  512. }
  513. #ifdef CONFIG_CPU_FREQ
  514. static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
  515. unsigned long val, void *data)
  516. {
  517. struct davinci_i2c_dev *dev;
  518. dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
  519. if (val == CPUFREQ_PRECHANGE) {
  520. wait_for_completion(&dev->xfr_complete);
  521. davinci_i2c_reset_ctrl(dev, 0);
  522. } else if (val == CPUFREQ_POSTCHANGE) {
  523. i2c_davinci_calc_clk_dividers(dev);
  524. davinci_i2c_reset_ctrl(dev, 1);
  525. }
  526. return 0;
  527. }
  528. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  529. {
  530. dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
  531. return cpufreq_register_notifier(&dev->freq_transition,
  532. CPUFREQ_TRANSITION_NOTIFIER);
  533. }
  534. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  535. {
  536. cpufreq_unregister_notifier(&dev->freq_transition,
  537. CPUFREQ_TRANSITION_NOTIFIER);
  538. }
  539. #else
  540. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  541. {
  542. return 0;
  543. }
  544. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  545. {
  546. }
  547. #endif
  548. static struct i2c_algorithm i2c_davinci_algo = {
  549. .master_xfer = i2c_davinci_xfer,
  550. .functionality = i2c_davinci_func,
  551. };
  552. static int davinci_i2c_probe(struct platform_device *pdev)
  553. {
  554. struct davinci_i2c_dev *dev;
  555. struct i2c_adapter *adap;
  556. struct resource *mem, *irq, *ioarea;
  557. int r;
  558. /* NOTE: driver uses the static register mapping */
  559. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  560. if (!mem) {
  561. dev_err(&pdev->dev, "no mem resource?\n");
  562. return -ENODEV;
  563. }
  564. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  565. if (!irq) {
  566. dev_err(&pdev->dev, "no irq resource?\n");
  567. return -ENODEV;
  568. }
  569. ioarea = request_mem_region(mem->start, resource_size(mem),
  570. pdev->name);
  571. if (!ioarea) {
  572. dev_err(&pdev->dev, "I2C region already claimed\n");
  573. return -EBUSY;
  574. }
  575. dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
  576. if (!dev) {
  577. r = -ENOMEM;
  578. goto err_release_region;
  579. }
  580. init_completion(&dev->cmd_complete);
  581. #ifdef CONFIG_CPU_FREQ
  582. init_completion(&dev->xfr_complete);
  583. #endif
  584. dev->dev = get_device(&pdev->dev);
  585. dev->irq = irq->start;
  586. platform_set_drvdata(pdev, dev);
  587. dev->clk = clk_get(&pdev->dev, NULL);
  588. if (IS_ERR(dev->clk)) {
  589. r = -ENODEV;
  590. goto err_free_mem;
  591. }
  592. clk_enable(dev->clk);
  593. dev->base = ioremap(mem->start, resource_size(mem));
  594. if (!dev->base) {
  595. r = -EBUSY;
  596. goto err_mem_ioremap;
  597. }
  598. i2c_davinci_init(dev);
  599. r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
  600. if (r) {
  601. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  602. goto err_unuse_clocks;
  603. }
  604. r = i2c_davinci_cpufreq_register(dev);
  605. if (r) {
  606. dev_err(&pdev->dev, "failed to register cpufreq\n");
  607. goto err_free_irq;
  608. }
  609. adap = &dev->adapter;
  610. i2c_set_adapdata(adap, dev);
  611. adap->owner = THIS_MODULE;
  612. adap->class = I2C_CLASS_HWMON;
  613. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  614. adap->algo = &i2c_davinci_algo;
  615. adap->dev.parent = &pdev->dev;
  616. adap->timeout = DAVINCI_I2C_TIMEOUT;
  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_free_irq;
  622. }
  623. return 0;
  624. err_free_irq:
  625. free_irq(dev->irq, dev);
  626. err_unuse_clocks:
  627. iounmap(dev->base);
  628. err_mem_ioremap:
  629. clk_disable(dev->clk);
  630. clk_put(dev->clk);
  631. dev->clk = NULL;
  632. err_free_mem:
  633. platform_set_drvdata(pdev, NULL);
  634. put_device(&pdev->dev);
  635. kfree(dev);
  636. err_release_region:
  637. release_mem_region(mem->start, resource_size(mem));
  638. return r;
  639. }
  640. static int davinci_i2c_remove(struct platform_device *pdev)
  641. {
  642. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  643. struct resource *mem;
  644. i2c_davinci_cpufreq_deregister(dev);
  645. platform_set_drvdata(pdev, NULL);
  646. i2c_del_adapter(&dev->adapter);
  647. put_device(&pdev->dev);
  648. clk_disable(dev->clk);
  649. clk_put(dev->clk);
  650. dev->clk = NULL;
  651. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  652. free_irq(IRQ_I2C, dev);
  653. iounmap(dev->base);
  654. kfree(dev);
  655. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  656. release_mem_region(mem->start, resource_size(mem));
  657. return 0;
  658. }
  659. #ifdef CONFIG_PM
  660. static int davinci_i2c_suspend(struct device *dev)
  661. {
  662. struct platform_device *pdev = to_platform_device(dev);
  663. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  664. /* put I2C into reset */
  665. davinci_i2c_reset_ctrl(i2c_dev, 0);
  666. clk_disable(i2c_dev->clk);
  667. return 0;
  668. }
  669. static int davinci_i2c_resume(struct device *dev)
  670. {
  671. struct platform_device *pdev = to_platform_device(dev);
  672. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  673. clk_enable(i2c_dev->clk);
  674. /* take I2C out of reset */
  675. davinci_i2c_reset_ctrl(i2c_dev, 1);
  676. return 0;
  677. }
  678. static const struct dev_pm_ops davinci_i2c_pm = {
  679. .suspend = davinci_i2c_suspend,
  680. .resume = davinci_i2c_resume,
  681. };
  682. #define davinci_i2c_pm_ops (&davinci_i2c_pm)
  683. #else
  684. #define davinci_i2c_pm_ops NULL
  685. #endif
  686. /* work with hotplug and coldplug */
  687. MODULE_ALIAS("platform:i2c_davinci");
  688. static struct platform_driver davinci_i2c_driver = {
  689. .probe = davinci_i2c_probe,
  690. .remove = davinci_i2c_remove,
  691. .driver = {
  692. .name = "i2c_davinci",
  693. .owner = THIS_MODULE,
  694. .pm = davinci_i2c_pm_ops,
  695. },
  696. };
  697. /* I2C may be needed to bring up other drivers */
  698. static int __init davinci_i2c_init_driver(void)
  699. {
  700. return platform_driver_register(&davinci_i2c_driver);
  701. }
  702. subsys_initcall(davinci_i2c_init_driver);
  703. static void __exit davinci_i2c_exit_driver(void)
  704. {
  705. platform_driver_unregister(&davinci_i2c_driver);
  706. }
  707. module_exit(davinci_i2c_exit_driver);
  708. MODULE_AUTHOR("Texas Instruments India");
  709. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  710. MODULE_LICENSE("GPL");