i2c-davinci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/i2c.h>
  27. #include <linux/clk.h>
  28. #include <linux/errno.h>
  29. #include <linux/sched.h>
  30. #include <linux/err.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/cpufreq.h>
  36. #include <linux/gpio.h>
  37. #include <linux/of_device.h>
  38. #include <linux/platform_data/i2c-davinci.h>
  39. /* ----- global defines ----------------------------------------------- */
  40. #define DAVINCI_I2C_TIMEOUT (1*HZ)
  41. #define DAVINCI_I2C_MAX_TRIES 2
  42. #define DAVINCI_I2C_OWN_ADDRESS 0x08
  43. #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_SCD | \
  44. DAVINCI_I2C_IMR_ARDY | \
  45. DAVINCI_I2C_IMR_NACK | \
  46. DAVINCI_I2C_IMR_AL)
  47. #define DAVINCI_I2C_OAR_REG 0x00
  48. #define DAVINCI_I2C_IMR_REG 0x04
  49. #define DAVINCI_I2C_STR_REG 0x08
  50. #define DAVINCI_I2C_CLKL_REG 0x0c
  51. #define DAVINCI_I2C_CLKH_REG 0x10
  52. #define DAVINCI_I2C_CNT_REG 0x14
  53. #define DAVINCI_I2C_DRR_REG 0x18
  54. #define DAVINCI_I2C_SAR_REG 0x1c
  55. #define DAVINCI_I2C_DXR_REG 0x20
  56. #define DAVINCI_I2C_MDR_REG 0x24
  57. #define DAVINCI_I2C_IVR_REG 0x28
  58. #define DAVINCI_I2C_EMDR_REG 0x2c
  59. #define DAVINCI_I2C_PSC_REG 0x30
  60. #define DAVINCI_I2C_FUNC_REG 0x48
  61. #define DAVINCI_I2C_DIR_REG 0x4c
  62. #define DAVINCI_I2C_DIN_REG 0x50
  63. #define DAVINCI_I2C_DOUT_REG 0x54
  64. #define DAVINCI_I2C_DSET_REG 0x58
  65. #define DAVINCI_I2C_DCLR_REG 0x5c
  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. /* set SDA and SCL as GPIO */
  95. #define DAVINCI_I2C_FUNC_PFUNC0 BIT(0)
  96. /* set SCL as output when used as GPIO*/
  97. #define DAVINCI_I2C_DIR_PDIR0 BIT(0)
  98. /* set SDA as output when used as GPIO*/
  99. #define DAVINCI_I2C_DIR_PDIR1 BIT(1)
  100. /* read SCL GPIO level */
  101. #define DAVINCI_I2C_DIN_PDIN0 BIT(0)
  102. /* read SDA GPIO level */
  103. #define DAVINCI_I2C_DIN_PDIN1 BIT(1)
  104. /*set the SCL GPIO high */
  105. #define DAVINCI_I2C_DSET_PDSET0 BIT(0)
  106. /*set the SDA GPIO high */
  107. #define DAVINCI_I2C_DSET_PDSET1 BIT(1)
  108. /* set the SCL GPIO low */
  109. #define DAVINCI_I2C_DCLR_PDCLR0 BIT(0)
  110. /* set the SDA GPIO low */
  111. #define DAVINCI_I2C_DCLR_PDCLR1 BIT(1)
  112. struct davinci_i2c_dev {
  113. struct device *dev;
  114. void __iomem *base;
  115. struct completion cmd_complete;
  116. struct clk *clk;
  117. int cmd_err;
  118. u8 *buf;
  119. size_t buf_len;
  120. int irq;
  121. int stop;
  122. u8 terminate;
  123. struct i2c_adapter adapter;
  124. #ifdef CONFIG_CPU_FREQ
  125. struct completion xfr_complete;
  126. struct notifier_block freq_transition;
  127. #endif
  128. struct davinci_i2c_platform_data *pdata;
  129. };
  130. /* default platform data to use if not supplied in the platform_device */
  131. static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
  132. .bus_freq = 100,
  133. .bus_delay = 0,
  134. };
  135. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  136. int reg, u16 val)
  137. {
  138. writew_relaxed(val, i2c_dev->base + reg);
  139. }
  140. static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  141. {
  142. return readw_relaxed(i2c_dev->base + reg);
  143. }
  144. static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
  145. int val)
  146. {
  147. u16 w;
  148. w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
  149. if (!val) /* put I2C into reset */
  150. w &= ~DAVINCI_I2C_MDR_IRS;
  151. else /* take I2C out of reset */
  152. w |= DAVINCI_I2C_MDR_IRS;
  153. davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
  154. }
  155. static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
  156. {
  157. struct davinci_i2c_platform_data *pdata = dev->pdata;
  158. u16 psc;
  159. u32 clk;
  160. u32 d;
  161. u32 clkh;
  162. u32 clkl;
  163. u32 input_clock = clk_get_rate(dev->clk);
  164. struct device_node *of_node = dev->dev->of_node;
  165. /* NOTE: I2C Clock divider programming info
  166. * As per I2C specs the following formulas provide prescaler
  167. * and low/high divider values
  168. * input clk --> PSC Div -----------> ICCL/H Div --> output clock
  169. * module clk
  170. *
  171. * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
  172. *
  173. * Thus,
  174. * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
  175. *
  176. * where if PSC == 0, d = 7,
  177. * if PSC == 1, d = 6
  178. * if PSC > 1 , d = 5
  179. *
  180. * Note:
  181. * d is always 6 on Keystone I2C controller
  182. */
  183. /*
  184. * Both Davinci and current Keystone User Guides recommend a value
  185. * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
  186. * always produce enough margin between SDA and SCL transitions.
  187. * Measurements show that the higher the module clock is, the
  188. * bigger is the margin, providing more reliable communication.
  189. * So we better target for 12MHz.
  190. */
  191. psc = (input_clock / 12000000) - 1;
  192. if ((input_clock / (psc + 1)) > 12000000)
  193. psc++; /* better to run under spec than over */
  194. d = (psc >= 2) ? 5 : 7 - psc;
  195. if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c"))
  196. d = 6;
  197. clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000));
  198. /* Avoid driving the bus too fast because of rounding errors above */
  199. if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000)
  200. clk++;
  201. /*
  202. * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
  203. * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
  204. * to LOW ratio as 1 to 2 is more safe.
  205. */
  206. if (pdata->bus_freq > 100)
  207. clkl = (clk << 1) / 3;
  208. else
  209. clkl = (clk >> 1);
  210. /*
  211. * It's not always possible to have 1 to 2 ratio when d=7, so fall back
  212. * to minimal possible clkh in this case.
  213. *
  214. * Note:
  215. * CLKH is not allowed to be 0, in this case I2C clock is not generated
  216. * at all
  217. */
  218. if (clk > clkl + d) {
  219. clkh = clk - clkl - d;
  220. clkl -= d;
  221. } else {
  222. clkh = 1;
  223. clkl = clk - (d << 1);
  224. }
  225. davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
  226. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
  227. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
  228. dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
  229. }
  230. /*
  231. * This function configures I2C and brings I2C out of reset.
  232. * This function is called during I2C init function. This function
  233. * also gets called if I2C encounters any errors.
  234. */
  235. static int i2c_davinci_init(struct davinci_i2c_dev *dev)
  236. {
  237. struct davinci_i2c_platform_data *pdata = dev->pdata;
  238. /* put I2C into reset */
  239. davinci_i2c_reset_ctrl(dev, 0);
  240. /* compute clock dividers */
  241. i2c_davinci_calc_clk_dividers(dev);
  242. /* Respond at reserved "SMBus Host" slave address" (and zero);
  243. * we seem to have no option to not respond...
  244. */
  245. davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS);
  246. dev_dbg(dev->dev, "PSC = %d\n",
  247. davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
  248. dev_dbg(dev->dev, "CLKL = %d\n",
  249. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
  250. dev_dbg(dev->dev, "CLKH = %d\n",
  251. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
  252. dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
  253. pdata->bus_freq, pdata->bus_delay);
  254. /* Take the I2C module out of reset: */
  255. davinci_i2c_reset_ctrl(dev, 1);
  256. /* Enable interrupts */
  257. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
  258. return 0;
  259. }
  260. /*
  261. * This routine does i2c bus recovery by using i2c_generic_gpio_recovery
  262. * which is provided by I2C Bus recovery infrastructure.
  263. */
  264. static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
  265. {
  266. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  267. /* Disable interrupts */
  268. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);
  269. /* put I2C into reset */
  270. davinci_i2c_reset_ctrl(dev, 0);
  271. }
  272. static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
  273. {
  274. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  275. i2c_davinci_init(dev);
  276. }
  277. static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
  278. .recover_bus = i2c_generic_gpio_recovery,
  279. .prepare_recovery = davinci_i2c_prepare_recovery,
  280. .unprepare_recovery = davinci_i2c_unprepare_recovery,
  281. };
  282. static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
  283. {
  284. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  285. if (val)
  286. davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
  287. DAVINCI_I2C_DSET_PDSET0);
  288. else
  289. davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
  290. DAVINCI_I2C_DCLR_PDCLR0);
  291. }
  292. static int davinci_i2c_get_scl(struct i2c_adapter *adap)
  293. {
  294. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  295. int val;
  296. /* read the state of SCL */
  297. val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
  298. return val & DAVINCI_I2C_DIN_PDIN0;
  299. }
  300. static int davinci_i2c_get_sda(struct i2c_adapter *adap)
  301. {
  302. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  303. int val;
  304. /* read the state of SDA */
  305. val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
  306. return val & DAVINCI_I2C_DIN_PDIN1;
  307. }
  308. static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
  309. {
  310. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  311. davinci_i2c_prepare_recovery(adap);
  312. /* SCL output, SDA input */
  313. davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);
  314. /* change to GPIO mode */
  315. davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
  316. DAVINCI_I2C_FUNC_PFUNC0);
  317. }
  318. static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
  319. {
  320. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  321. /* change back to I2C mode */
  322. davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);
  323. davinci_i2c_unprepare_recovery(adap);
  324. }
  325. static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
  326. .recover_bus = i2c_generic_scl_recovery,
  327. .set_scl = davinci_i2c_set_scl,
  328. .get_scl = davinci_i2c_get_scl,
  329. .get_sda = davinci_i2c_get_sda,
  330. .prepare_recovery = davinci_i2c_scl_prepare_recovery,
  331. .unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
  332. };
  333. /*
  334. * Waiting for bus not busy
  335. */
  336. static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
  337. {
  338. unsigned long timeout = jiffies + dev->adapter.timeout;
  339. do {
  340. if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
  341. return 0;
  342. schedule_timeout_uninterruptible(1);
  343. } while (time_before_eq(jiffies, timeout));
  344. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  345. i2c_recover_bus(&dev->adapter);
  346. /*
  347. * if bus is still "busy" here, it's most probably a HW problem like
  348. * short-circuit
  349. */
  350. if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
  351. return -EIO;
  352. return 0;
  353. }
  354. /*
  355. * Low level master read/write transaction. This function is called
  356. * from i2c_davinci_xfer.
  357. */
  358. static int
  359. i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
  360. {
  361. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  362. struct davinci_i2c_platform_data *pdata = dev->pdata;
  363. u32 flag;
  364. u16 w;
  365. unsigned long time_left;
  366. if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) {
  367. dev_warn(dev->dev, "transfer to own address aborted\n");
  368. return -EADDRNOTAVAIL;
  369. }
  370. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  371. if (pdata->bus_delay)
  372. udelay(pdata->bus_delay);
  373. /* set the slave address */
  374. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  375. dev->buf = msg->buf;
  376. dev->buf_len = msg->len;
  377. dev->stop = stop;
  378. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  379. reinit_completion(&dev->cmd_complete);
  380. dev->cmd_err = 0;
  381. /* Take I2C out of reset and configure it as master */
  382. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
  383. /* if the slave address is ten bit address, enable XA bit */
  384. if (msg->flags & I2C_M_TEN)
  385. flag |= DAVINCI_I2C_MDR_XA;
  386. if (!(msg->flags & I2C_M_RD))
  387. flag |= DAVINCI_I2C_MDR_TRX;
  388. if (msg->len == 0)
  389. flag |= DAVINCI_I2C_MDR_RM;
  390. /* Enable receive or transmit interrupts */
  391. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  392. if (msg->flags & I2C_M_RD)
  393. w |= DAVINCI_I2C_IMR_RRDY;
  394. else
  395. w |= DAVINCI_I2C_IMR_XRDY;
  396. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  397. dev->terminate = 0;
  398. /*
  399. * Write mode register first as needed for correct behaviour
  400. * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
  401. * occurring before we have loaded DXR
  402. */
  403. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  404. /*
  405. * First byte should be set here, not after interrupt,
  406. * because transmit-data-ready interrupt can come before
  407. * NACK-interrupt during sending of previous message and
  408. * ICDXR may have wrong data
  409. * It also saves us one interrupt, slightly faster
  410. */
  411. if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
  412. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
  413. dev->buf_len--;
  414. }
  415. /* Set STT to begin transmit now DXR is loaded */
  416. flag |= DAVINCI_I2C_MDR_STT;
  417. if (stop && msg->len != 0)
  418. flag |= DAVINCI_I2C_MDR_STP;
  419. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  420. time_left = wait_for_completion_timeout(&dev->cmd_complete,
  421. dev->adapter.timeout);
  422. if (!time_left) {
  423. dev_err(dev->dev, "controller timed out\n");
  424. i2c_recover_bus(adap);
  425. dev->buf_len = 0;
  426. return -ETIMEDOUT;
  427. }
  428. if (dev->buf_len) {
  429. /* This should be 0 if all bytes were transferred
  430. * or dev->cmd_err denotes an error.
  431. */
  432. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  433. dev->buf_len);
  434. dev->terminate = 1;
  435. wmb();
  436. dev->buf_len = 0;
  437. return -EREMOTEIO;
  438. }
  439. /* no error */
  440. if (likely(!dev->cmd_err))
  441. return msg->len;
  442. /* We have an error */
  443. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  444. i2c_davinci_init(dev);
  445. return -EIO;
  446. }
  447. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  448. if (msg->flags & I2C_M_IGNORE_NAK)
  449. return msg->len;
  450. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  451. w |= DAVINCI_I2C_MDR_STP;
  452. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  453. return -EREMOTEIO;
  454. }
  455. return -EIO;
  456. }
  457. /*
  458. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  459. */
  460. static int
  461. i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  462. {
  463. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  464. int i;
  465. int ret;
  466. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  467. ret = i2c_davinci_wait_bus_not_busy(dev);
  468. if (ret < 0) {
  469. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  470. return ret;
  471. }
  472. for (i = 0; i < num; i++) {
  473. ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  474. dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  475. ret);
  476. if (ret < 0)
  477. return ret;
  478. }
  479. #ifdef CONFIG_CPU_FREQ
  480. complete(&dev->xfr_complete);
  481. #endif
  482. return num;
  483. }
  484. static u32 i2c_davinci_func(struct i2c_adapter *adap)
  485. {
  486. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  487. }
  488. static void terminate_read(struct davinci_i2c_dev *dev)
  489. {
  490. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  491. w |= DAVINCI_I2C_MDR_NACK;
  492. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  493. /* Throw away data */
  494. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  495. if (!dev->terminate)
  496. dev_err(dev->dev, "RDR IRQ while no data requested\n");
  497. }
  498. static void terminate_write(struct davinci_i2c_dev *dev)
  499. {
  500. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  501. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  502. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  503. if (!dev->terminate)
  504. dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
  505. }
  506. /*
  507. * Interrupt service routine. This gets called whenever an I2C interrupt
  508. * occurs.
  509. */
  510. static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
  511. {
  512. struct davinci_i2c_dev *dev = dev_id;
  513. u32 stat;
  514. int count = 0;
  515. u16 w;
  516. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  517. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  518. if (count++ == 100) {
  519. dev_warn(dev->dev, "Too much work in one IRQ\n");
  520. break;
  521. }
  522. switch (stat) {
  523. case DAVINCI_I2C_IVR_AL:
  524. /* Arbitration lost, must retry */
  525. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  526. dev->buf_len = 0;
  527. complete(&dev->cmd_complete);
  528. break;
  529. case DAVINCI_I2C_IVR_NACK:
  530. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  531. dev->buf_len = 0;
  532. complete(&dev->cmd_complete);
  533. break;
  534. case DAVINCI_I2C_IVR_ARDY:
  535. davinci_i2c_write_reg(dev,
  536. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  537. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  538. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  539. w = davinci_i2c_read_reg(dev,
  540. DAVINCI_I2C_MDR_REG);
  541. w |= DAVINCI_I2C_MDR_STP;
  542. davinci_i2c_write_reg(dev,
  543. DAVINCI_I2C_MDR_REG, w);
  544. }
  545. complete(&dev->cmd_complete);
  546. break;
  547. case DAVINCI_I2C_IVR_RDR:
  548. if (dev->buf_len) {
  549. *dev->buf++ =
  550. davinci_i2c_read_reg(dev,
  551. DAVINCI_I2C_DRR_REG);
  552. dev->buf_len--;
  553. if (dev->buf_len)
  554. continue;
  555. davinci_i2c_write_reg(dev,
  556. DAVINCI_I2C_STR_REG,
  557. DAVINCI_I2C_IMR_RRDY);
  558. } else {
  559. /* signal can terminate transfer */
  560. terminate_read(dev);
  561. }
  562. break;
  563. case DAVINCI_I2C_IVR_XRDY:
  564. if (dev->buf_len) {
  565. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  566. *dev->buf++);
  567. dev->buf_len--;
  568. if (dev->buf_len)
  569. continue;
  570. w = davinci_i2c_read_reg(dev,
  571. DAVINCI_I2C_IMR_REG);
  572. w &= ~DAVINCI_I2C_IMR_XRDY;
  573. davinci_i2c_write_reg(dev,
  574. DAVINCI_I2C_IMR_REG,
  575. w);
  576. } else {
  577. /* signal can terminate transfer */
  578. terminate_write(dev);
  579. }
  580. break;
  581. case DAVINCI_I2C_IVR_SCD:
  582. davinci_i2c_write_reg(dev,
  583. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  584. complete(&dev->cmd_complete);
  585. break;
  586. case DAVINCI_I2C_IVR_AAS:
  587. dev_dbg(dev->dev, "Address as slave interrupt\n");
  588. break;
  589. default:
  590. dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
  591. break;
  592. }
  593. }
  594. return count ? IRQ_HANDLED : IRQ_NONE;
  595. }
  596. #ifdef CONFIG_CPU_FREQ
  597. static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
  598. unsigned long val, void *data)
  599. {
  600. struct davinci_i2c_dev *dev;
  601. dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
  602. if (val == CPUFREQ_PRECHANGE) {
  603. wait_for_completion(&dev->xfr_complete);
  604. davinci_i2c_reset_ctrl(dev, 0);
  605. } else if (val == CPUFREQ_POSTCHANGE) {
  606. i2c_davinci_calc_clk_dividers(dev);
  607. davinci_i2c_reset_ctrl(dev, 1);
  608. }
  609. return 0;
  610. }
  611. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  612. {
  613. dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
  614. return cpufreq_register_notifier(&dev->freq_transition,
  615. CPUFREQ_TRANSITION_NOTIFIER);
  616. }
  617. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  618. {
  619. cpufreq_unregister_notifier(&dev->freq_transition,
  620. CPUFREQ_TRANSITION_NOTIFIER);
  621. }
  622. #else
  623. static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
  624. {
  625. return 0;
  626. }
  627. static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
  628. {
  629. }
  630. #endif
  631. static struct i2c_algorithm i2c_davinci_algo = {
  632. .master_xfer = i2c_davinci_xfer,
  633. .functionality = i2c_davinci_func,
  634. };
  635. static const struct of_device_id davinci_i2c_of_match[] = {
  636. {.compatible = "ti,davinci-i2c", },
  637. {.compatible = "ti,keystone-i2c", },
  638. {},
  639. };
  640. MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
  641. static int davinci_i2c_probe(struct platform_device *pdev)
  642. {
  643. struct davinci_i2c_dev *dev;
  644. struct i2c_adapter *adap;
  645. struct resource *mem;
  646. int r, irq;
  647. irq = platform_get_irq(pdev, 0);
  648. if (irq <= 0) {
  649. if (!irq)
  650. irq = -ENXIO;
  651. if (irq != -EPROBE_DEFER)
  652. dev_err(&pdev->dev,
  653. "can't get irq resource ret=%d\n", irq);
  654. return irq;
  655. }
  656. dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
  657. GFP_KERNEL);
  658. if (!dev) {
  659. dev_err(&pdev->dev, "Memory allocation failed\n");
  660. return -ENOMEM;
  661. }
  662. init_completion(&dev->cmd_complete);
  663. #ifdef CONFIG_CPU_FREQ
  664. init_completion(&dev->xfr_complete);
  665. #endif
  666. dev->dev = &pdev->dev;
  667. dev->irq = irq;
  668. dev->pdata = dev_get_platdata(&pdev->dev);
  669. platform_set_drvdata(pdev, dev);
  670. if (!dev->pdata && pdev->dev.of_node) {
  671. u32 prop;
  672. dev->pdata = devm_kzalloc(&pdev->dev,
  673. sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
  674. if (!dev->pdata)
  675. return -ENOMEM;
  676. memcpy(dev->pdata, &davinci_i2c_platform_data_default,
  677. sizeof(struct davinci_i2c_platform_data));
  678. if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  679. &prop))
  680. dev->pdata->bus_freq = prop / 1000;
  681. dev->pdata->has_pfunc =
  682. of_property_read_bool(pdev->dev.of_node,
  683. "ti,has-pfunc");
  684. } else if (!dev->pdata) {
  685. dev->pdata = &davinci_i2c_platform_data_default;
  686. }
  687. dev->clk = devm_clk_get(&pdev->dev, NULL);
  688. if (IS_ERR(dev->clk))
  689. return -ENODEV;
  690. clk_prepare_enable(dev->clk);
  691. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  692. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  693. if (IS_ERR(dev->base)) {
  694. r = PTR_ERR(dev->base);
  695. goto err_unuse_clocks;
  696. }
  697. i2c_davinci_init(dev);
  698. r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
  699. pdev->name, dev);
  700. if (r) {
  701. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  702. goto err_unuse_clocks;
  703. }
  704. r = i2c_davinci_cpufreq_register(dev);
  705. if (r) {
  706. dev_err(&pdev->dev, "failed to register cpufreq\n");
  707. goto err_unuse_clocks;
  708. }
  709. adap = &dev->adapter;
  710. i2c_set_adapdata(adap, dev);
  711. adap->owner = THIS_MODULE;
  712. adap->class = I2C_CLASS_DEPRECATED;
  713. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  714. adap->algo = &i2c_davinci_algo;
  715. adap->dev.parent = &pdev->dev;
  716. adap->timeout = DAVINCI_I2C_TIMEOUT;
  717. adap->dev.of_node = pdev->dev.of_node;
  718. if (dev->pdata->has_pfunc)
  719. adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
  720. else if (dev->pdata->scl_pin) {
  721. adap->bus_recovery_info = &davinci_i2c_gpio_recovery_info;
  722. adap->bus_recovery_info->scl_gpio = dev->pdata->scl_pin;
  723. adap->bus_recovery_info->sda_gpio = dev->pdata->sda_pin;
  724. }
  725. adap->nr = pdev->id;
  726. r = i2c_add_numbered_adapter(adap);
  727. if (r) {
  728. dev_err(&pdev->dev, "failure adding adapter\n");
  729. goto err_unuse_clocks;
  730. }
  731. return 0;
  732. err_unuse_clocks:
  733. clk_disable_unprepare(dev->clk);
  734. dev->clk = NULL;
  735. return r;
  736. }
  737. static int davinci_i2c_remove(struct platform_device *pdev)
  738. {
  739. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  740. i2c_davinci_cpufreq_deregister(dev);
  741. i2c_del_adapter(&dev->adapter);
  742. clk_disable_unprepare(dev->clk);
  743. dev->clk = NULL;
  744. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  745. return 0;
  746. }
  747. #ifdef CONFIG_PM
  748. static int davinci_i2c_suspend(struct device *dev)
  749. {
  750. struct platform_device *pdev = to_platform_device(dev);
  751. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  752. /* put I2C into reset */
  753. davinci_i2c_reset_ctrl(i2c_dev, 0);
  754. clk_disable_unprepare(i2c_dev->clk);
  755. return 0;
  756. }
  757. static int davinci_i2c_resume(struct device *dev)
  758. {
  759. struct platform_device *pdev = to_platform_device(dev);
  760. struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  761. clk_prepare_enable(i2c_dev->clk);
  762. /* take I2C out of reset */
  763. davinci_i2c_reset_ctrl(i2c_dev, 1);
  764. return 0;
  765. }
  766. static const struct dev_pm_ops davinci_i2c_pm = {
  767. .suspend = davinci_i2c_suspend,
  768. .resume = davinci_i2c_resume,
  769. };
  770. #define davinci_i2c_pm_ops (&davinci_i2c_pm)
  771. #else
  772. #define davinci_i2c_pm_ops NULL
  773. #endif
  774. /* work with hotplug and coldplug */
  775. MODULE_ALIAS("platform:i2c_davinci");
  776. static struct platform_driver davinci_i2c_driver = {
  777. .probe = davinci_i2c_probe,
  778. .remove = davinci_i2c_remove,
  779. .driver = {
  780. .name = "i2c_davinci",
  781. .pm = davinci_i2c_pm_ops,
  782. .of_match_table = davinci_i2c_of_match,
  783. },
  784. };
  785. /* I2C may be needed to bring up other drivers */
  786. static int __init davinci_i2c_init_driver(void)
  787. {
  788. return platform_driver_register(&davinci_i2c_driver);
  789. }
  790. subsys_initcall(davinci_i2c_init_driver);
  791. static void __exit davinci_i2c_exit_driver(void)
  792. {
  793. platform_driver_unregister(&davinci_i2c_driver);
  794. }
  795. module_exit(davinci_i2c_exit_driver);
  796. MODULE_AUTHOR("Texas Instruments India");
  797. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  798. MODULE_LICENSE("GPL");