i2c-davinci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 <mach/hardware.h>
  44. #include <linux/platform_data/i2c-davinci.h>
  45. /* ----- global defines ----------------------------------------------- */
  46. #define DAVINCI_I2C_TIMEOUT (1*HZ)
  47. #define DAVINCI_I2C_MAX_TRIES 2
  48. #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
  49. DAVINCI_I2C_IMR_SCD | \
  50. DAVINCI_I2C_IMR_ARDY | \
  51. DAVINCI_I2C_IMR_NACK | \
  52. DAVINCI_I2C_IMR_AL)
  53. #define DAVINCI_I2C_OAR_REG 0x00
  54. #define DAVINCI_I2C_IMR_REG 0x04
  55. #define DAVINCI_I2C_STR_REG 0x08
  56. #define DAVINCI_I2C_CLKL_REG 0x0c
  57. #define DAVINCI_I2C_CLKH_REG 0x10
  58. #define DAVINCI_I2C_CNT_REG 0x14
  59. #define DAVINCI_I2C_DRR_REG 0x18
  60. #define DAVINCI_I2C_SAR_REG 0x1c
  61. #define DAVINCI_I2C_DXR_REG 0x20
  62. #define DAVINCI_I2C_MDR_REG 0x24
  63. #define DAVINCI_I2C_IVR_REG 0x28
  64. #define DAVINCI_I2C_EMDR_REG 0x2c
  65. #define DAVINCI_I2C_PSC_REG 0x30
  66. #define DAVINCI_I2C_IVR_AAS 0x07
  67. #define DAVINCI_I2C_IVR_SCD 0x06
  68. #define DAVINCI_I2C_IVR_XRDY 0x05
  69. #define DAVINCI_I2C_IVR_RDR 0x04
  70. #define DAVINCI_I2C_IVR_ARDY 0x03
  71. #define DAVINCI_I2C_IVR_NACK 0x02
  72. #define DAVINCI_I2C_IVR_AL 0x01
  73. #define DAVINCI_I2C_STR_BB BIT(12)
  74. #define DAVINCI_I2C_STR_RSFULL BIT(11)
  75. #define DAVINCI_I2C_STR_SCD BIT(5)
  76. #define DAVINCI_I2C_STR_ARDY BIT(2)
  77. #define DAVINCI_I2C_STR_NACK BIT(1)
  78. #define DAVINCI_I2C_STR_AL BIT(0)
  79. #define DAVINCI_I2C_MDR_NACK BIT(15)
  80. #define DAVINCI_I2C_MDR_STT BIT(13)
  81. #define DAVINCI_I2C_MDR_STP BIT(11)
  82. #define DAVINCI_I2C_MDR_MST BIT(10)
  83. #define DAVINCI_I2C_MDR_TRX BIT(9)
  84. #define DAVINCI_I2C_MDR_XA BIT(8)
  85. #define DAVINCI_I2C_MDR_RM BIT(7)
  86. #define DAVINCI_I2C_MDR_IRS BIT(5)
  87. #define DAVINCI_I2C_IMR_AAS BIT(6)
  88. #define DAVINCI_I2C_IMR_SCD BIT(5)
  89. #define DAVINCI_I2C_IMR_XRDY BIT(4)
  90. #define DAVINCI_I2C_IMR_RRDY BIT(3)
  91. #define DAVINCI_I2C_IMR_ARDY BIT(2)
  92. #define DAVINCI_I2C_IMR_NACK BIT(1)
  93. #define DAVINCI_I2C_IMR_AL BIT(0)
  94. struct davinci_i2c_dev {
  95. struct device *dev;
  96. void __iomem *base;
  97. struct completion cmd_complete;
  98. struct clk *clk;
  99. int cmd_err;
  100. u8 *buf;
  101. size_t buf_len;
  102. int irq;
  103. int stop;
  104. u8 terminate;
  105. struct i2c_adapter adapter;
  106. #ifdef CONFIG_CPU_FREQ
  107. struct completion xfr_complete;
  108. struct notifier_block freq_transition;
  109. #endif
  110. struct davinci_i2c_platform_data *pdata;
  111. };
  112. /* default platform data to use if not supplied in the platform_device */
  113. static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
  114. .bus_freq = 100,
  115. .bus_delay = 0,
  116. };
  117. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  118. int reg, u16 val)
  119. {
  120. __raw_writew(val, i2c_dev->base + reg);
  121. }
  122. static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  123. {
  124. return __raw_readw(i2c_dev->base + reg);
  125. }
  126. /* Generate a pulse on the i2c clock pin. */
  127. static void generic_i2c_clock_pulse(unsigned int scl_pin)
  128. {
  129. u16 i;
  130. if (scl_pin) {
  131. /* Send high and low on the SCL line */
  132. for (i = 0; i < 9; i++) {
  133. gpio_set_value(scl_pin, 0);
  134. udelay(20);
  135. gpio_set_value(scl_pin, 1);
  136. udelay(20);
  137. }
  138. }
  139. }
  140. /* This routine does i2c bus recovery as specified in the
  141. * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
  142. */
  143. static void i2c_recover_bus(struct davinci_i2c_dev *dev)
  144. {
  145. u32 flag = 0;
  146. struct davinci_i2c_platform_data *pdata = dev->pdata;
  147. dev_err(dev->dev, "initiating i2c bus recovery\n");
  148. /* Send NACK to the slave */
  149. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  150. flag |= DAVINCI_I2C_MDR_NACK;
  151. /* write the data into mode register */
  152. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  153. generic_i2c_clock_pulse(pdata->scl_pin);
  154. /* Send STOP */
  155. flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  156. flag |= DAVINCI_I2C_MDR_STP;
  157. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  158. }
  159. static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
  160. int val)
  161. {
  162. u16 w;
  163. w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
  164. if (!val) /* put I2C into reset */
  165. w &= ~DAVINCI_I2C_MDR_IRS;
  166. else /* take I2C out of reset */
  167. w |= DAVINCI_I2C_MDR_IRS;
  168. davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
  169. }
  170. static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
  171. {
  172. struct davinci_i2c_platform_data *pdata = dev->pdata;
  173. u16 psc;
  174. u32 clk;
  175. u32 d;
  176. u32 clkh;
  177. u32 clkl;
  178. u32 input_clock = clk_get_rate(dev->clk);
  179. /* NOTE: I2C Clock divider programming info
  180. * As per I2C specs the following formulas provide prescaler
  181. * and low/high divider values
  182. * input clk --> PSC Div -----------> ICCL/H Div --> output clock
  183. * module clk
  184. *
  185. * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
  186. *
  187. * Thus,
  188. * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
  189. *
  190. * where if PSC == 0, d = 7,
  191. * if PSC == 1, d = 6
  192. * if PSC > 1 , d = 5
  193. */
  194. /* get minimum of 7 MHz clock, but max of 12 MHz */
  195. psc = (input_clock / 7000000) - 1;
  196. if ((input_clock / (psc + 1)) > 12000000)
  197. psc++; /* better to run under spec than over */
  198. d = (psc >= 2) ? 5 : 7 - psc;
  199. clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
  200. clkh = clk >> 1;
  201. clkl = clk - clkh;
  202. davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
  203. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
  204. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
  205. dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
  206. }
  207. /*
  208. * This function configures I2C and brings I2C out of reset.
  209. * This function is called during I2C init function. This function
  210. * also gets called if I2C encounters any errors.
  211. */
  212. static int i2c_davinci_init(struct davinci_i2c_dev *dev)
  213. {
  214. struct davinci_i2c_platform_data *pdata = dev->pdata;
  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->pdata;
  274. u32 flag;
  275. u16 w;
  276. int r;
  277. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  278. if (pdata->bus_delay)
  279. udelay(pdata->bus_delay);
  280. /* set the slave address */
  281. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  282. dev->buf = msg->buf;
  283. dev->buf_len = msg->len;
  284. dev->stop = stop;
  285. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  286. INIT_COMPLETION(dev->cmd_complete);
  287. dev->cmd_err = 0;
  288. /* Take I2C out of reset and configure it as master */
  289. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
  290. /* if the slave address is ten bit address, enable XA bit */
  291. if (msg->flags & I2C_M_TEN)
  292. flag |= DAVINCI_I2C_MDR_XA;
  293. if (!(msg->flags & I2C_M_RD))
  294. flag |= DAVINCI_I2C_MDR_TRX;
  295. if (msg->len == 0)
  296. flag |= DAVINCI_I2C_MDR_RM;
  297. /* Enable receive or transmit interrupts */
  298. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  299. if (msg->flags & I2C_M_RD)
  300. w |= DAVINCI_I2C_IMR_RRDY;
  301. else
  302. w |= DAVINCI_I2C_IMR_XRDY;
  303. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  304. dev->terminate = 0;
  305. /*
  306. * Write mode register first as needed for correct behaviour
  307. * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
  308. * occurring before we have loaded DXR
  309. */
  310. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  311. /*
  312. * First byte should be set here, not after interrupt,
  313. * because transmit-data-ready interrupt can come before
  314. * NACK-interrupt during sending of previous message and
  315. * ICDXR may have wrong data
  316. * It also saves us one interrupt, slightly faster
  317. */
  318. if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
  319. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
  320. dev->buf_len--;
  321. }
  322. /* Set STT to begin transmit now DXR is loaded */
  323. flag |= DAVINCI_I2C_MDR_STT;
  324. if (stop && msg->len != 0)
  325. flag |= DAVINCI_I2C_MDR_STP;
  326. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  327. r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  328. dev->adapter.timeout);
  329. if (r == 0) {
  330. dev_err(dev->dev, "controller timed out\n");
  331. i2c_recover_bus(dev);
  332. i2c_davinci_init(dev);
  333. dev->buf_len = 0;
  334. return -ETIMEDOUT;
  335. }
  336. if (dev->buf_len) {
  337. /* This should be 0 if all bytes were transferred
  338. * or dev->cmd_err denotes an error.
  339. * A signal may have aborted the transfer.
  340. */
  341. if (r >= 0) {
  342. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  343. dev->buf_len);
  344. r = -EREMOTEIO;
  345. }
  346. dev->terminate = 1;
  347. wmb();
  348. dev->buf_len = 0;
  349. }
  350. if (r < 0)
  351. return r;
  352. /* no error */
  353. if (likely(!dev->cmd_err))
  354. return msg->len;
  355. /* We have an error */
  356. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  357. i2c_davinci_init(dev);
  358. return -EIO;
  359. }
  360. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  361. if (msg->flags & I2C_M_IGNORE_NAK)
  362. return msg->len;
  363. if (stop) {
  364. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  365. w |= DAVINCI_I2C_MDR_STP;
  366. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  367. }
  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 const struct of_device_id davinci_i2c_of_match[] = {
  551. {.compatible = "ti,davinci-i2c", },
  552. {},
  553. };
  554. MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
  555. static int davinci_i2c_probe(struct platform_device *pdev)
  556. {
  557. struct davinci_i2c_dev *dev;
  558. struct i2c_adapter *adap;
  559. struct resource *mem, *irq, *ioarea;
  560. int r;
  561. /* NOTE: driver uses the static register mapping */
  562. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  563. if (!mem) {
  564. dev_err(&pdev->dev, "no mem resource?\n");
  565. return -ENODEV;
  566. }
  567. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  568. if (!irq) {
  569. dev_err(&pdev->dev, "no irq resource?\n");
  570. return -ENODEV;
  571. }
  572. ioarea = request_mem_region(mem->start, resource_size(mem),
  573. pdev->name);
  574. if (!ioarea) {
  575. dev_err(&pdev->dev, "I2C region already claimed\n");
  576. return -EBUSY;
  577. }
  578. dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
  579. if (!dev) {
  580. r = -ENOMEM;
  581. goto err_release_region;
  582. }
  583. init_completion(&dev->cmd_complete);
  584. #ifdef CONFIG_CPU_FREQ
  585. init_completion(&dev->xfr_complete);
  586. #endif
  587. dev->dev = get_device(&pdev->dev);
  588. dev->irq = irq->start;
  589. dev->pdata = dev->dev->platform_data;
  590. platform_set_drvdata(pdev, dev);
  591. if (!dev->pdata && pdev->dev.of_node) {
  592. u32 prop;
  593. dev->pdata = devm_kzalloc(&pdev->dev,
  594. sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
  595. if (!dev->pdata) {
  596. r = -ENOMEM;
  597. goto err_free_mem;
  598. }
  599. memcpy(dev->pdata, &davinci_i2c_platform_data_default,
  600. sizeof(struct davinci_i2c_platform_data));
  601. if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  602. &prop))
  603. dev->pdata->bus_freq = prop / 1000;
  604. } else if (!dev->pdata) {
  605. dev->pdata = &davinci_i2c_platform_data_default;
  606. }
  607. dev->clk = clk_get(&pdev->dev, NULL);
  608. if (IS_ERR(dev->clk)) {
  609. r = -ENODEV;
  610. goto err_free_mem;
  611. }
  612. clk_prepare_enable(dev->clk);
  613. dev->base = ioremap(mem->start, resource_size(mem));
  614. if (!dev->base) {
  615. r = -EBUSY;
  616. goto err_mem_ioremap;
  617. }
  618. i2c_davinci_init(dev);
  619. r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
  620. if (r) {
  621. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  622. goto err_unuse_clocks;
  623. }
  624. r = i2c_davinci_cpufreq_register(dev);
  625. if (r) {
  626. dev_err(&pdev->dev, "failed to register cpufreq\n");
  627. goto err_free_irq;
  628. }
  629. adap = &dev->adapter;
  630. i2c_set_adapdata(adap, dev);
  631. adap->owner = THIS_MODULE;
  632. adap->class = I2C_CLASS_HWMON;
  633. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  634. adap->algo = &i2c_davinci_algo;
  635. adap->dev.parent = &pdev->dev;
  636. adap->timeout = DAVINCI_I2C_TIMEOUT;
  637. adap->dev.of_node = pdev->dev.of_node;
  638. adap->nr = pdev->id;
  639. r = i2c_add_numbered_adapter(adap);
  640. if (r) {
  641. dev_err(&pdev->dev, "failure adding adapter\n");
  642. goto err_free_irq;
  643. }
  644. of_i2c_register_devices(adap);
  645. return 0;
  646. err_free_irq:
  647. free_irq(dev->irq, dev);
  648. err_unuse_clocks:
  649. iounmap(dev->base);
  650. err_mem_ioremap:
  651. clk_disable_unprepare(dev->clk);
  652. clk_put(dev->clk);
  653. dev->clk = NULL;
  654. err_free_mem:
  655. put_device(&pdev->dev);
  656. kfree(dev);
  657. err_release_region:
  658. release_mem_region(mem->start, resource_size(mem));
  659. return r;
  660. }
  661. static int davinci_i2c_remove(struct platform_device *pdev)
  662. {
  663. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  664. struct resource *mem;
  665. i2c_davinci_cpufreq_deregister(dev);
  666. i2c_del_adapter(&dev->adapter);
  667. put_device(&pdev->dev);
  668. clk_disable_unprepare(dev->clk);
  669. clk_put(dev->clk);
  670. dev->clk = NULL;
  671. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  672. free_irq(dev->irq, dev);
  673. iounmap(dev->base);
  674. kfree(dev);
  675. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  676. release_mem_region(mem->start, resource_size(mem));
  677. return 0;
  678. }
  679. #ifdef CONFIG_PM
  680. static int davinci_i2c_suspend(struct device *dev)
  681. {
  682. struct platform_device *pdev = to_platform_device(dev);
  683. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  684. /* put I2C into reset */
  685. davinci_i2c_reset_ctrl(i2c_dev, 0);
  686. clk_disable_unprepare(i2c_dev->clk);
  687. return 0;
  688. }
  689. static int davinci_i2c_resume(struct device *dev)
  690. {
  691. struct platform_device *pdev = to_platform_device(dev);
  692. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  693. clk_prepare_enable(i2c_dev->clk);
  694. /* take I2C out of reset */
  695. davinci_i2c_reset_ctrl(i2c_dev, 1);
  696. return 0;
  697. }
  698. static const struct dev_pm_ops davinci_i2c_pm = {
  699. .suspend = davinci_i2c_suspend,
  700. .resume = davinci_i2c_resume,
  701. };
  702. #define davinci_i2c_pm_ops (&davinci_i2c_pm)
  703. #else
  704. #define davinci_i2c_pm_ops NULL
  705. #endif
  706. /* work with hotplug and coldplug */
  707. MODULE_ALIAS("platform:i2c_davinci");
  708. static struct platform_driver davinci_i2c_driver = {
  709. .probe = davinci_i2c_probe,
  710. .remove = davinci_i2c_remove,
  711. .driver = {
  712. .name = "i2c_davinci",
  713. .owner = THIS_MODULE,
  714. .pm = davinci_i2c_pm_ops,
  715. .of_match_table = of_match_ptr(davinci_i2c_of_match),
  716. },
  717. };
  718. /* I2C may be needed to bring up other drivers */
  719. static int __init davinci_i2c_init_driver(void)
  720. {
  721. return platform_driver_register(&davinci_i2c_driver);
  722. }
  723. subsys_initcall(davinci_i2c_init_driver);
  724. static void __exit davinci_i2c_exit_driver(void)
  725. {
  726. platform_driver_unregister(&davinci_i2c_driver);
  727. }
  728. module_exit(davinci_i2c_exit_driver);
  729. MODULE_AUTHOR("Texas Instruments India");
  730. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  731. MODULE_LICENSE("GPL");