i2c-davinci.c 21 KB

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