i2c-davinci.c 26 KB

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