i2c-davinci.c 25 KB

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