i2c-davinci.c 21 KB

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