i2c-designware-common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare I2C adapter driver.
  4. *
  5. * Based on the TI DAVINCI I2C adapter driver.
  6. *
  7. * Copyright (C) 2006 Texas Instruments.
  8. * Copyright (C) 2007 MontaVista Software Inc.
  9. * Copyright (C) 2009 Provigent Ltd.
  10. */
  11. #define DEFAULT_SYMBOL_NAMESPACE "I2C_DW_COMMON"
  12. #include <linux/acpi.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/export.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/property.h>
  28. #include <linux/regmap.h>
  29. #include <linux/swab.h>
  30. #include <linux/types.h>
  31. #include <linux/units.h>
  32. #include "i2c-designware-core.h"
  33. static char *abort_sources[] = {
  34. [ABRT_7B_ADDR_NOACK] =
  35. "slave address not acknowledged (7bit mode)",
  36. [ABRT_10ADDR1_NOACK] =
  37. "first address byte not acknowledged (10bit mode)",
  38. [ABRT_10ADDR2_NOACK] =
  39. "second address byte not acknowledged (10bit mode)",
  40. [ABRT_TXDATA_NOACK] =
  41. "data not acknowledged",
  42. [ABRT_GCALL_NOACK] =
  43. "no acknowledgement for a general call",
  44. [ABRT_GCALL_READ] =
  45. "read after general call",
  46. [ABRT_SBYTE_ACKDET] =
  47. "start byte acknowledged",
  48. [ABRT_SBYTE_NORSTRT] =
  49. "trying to send start byte when restart is disabled",
  50. [ABRT_10B_RD_NORSTRT] =
  51. "trying to read when restart is disabled (10bit mode)",
  52. [ABRT_MASTER_DIS] =
  53. "trying to use disabled adapter",
  54. [ARB_LOST] =
  55. "lost arbitration",
  56. [ABRT_SLAVE_FLUSH_TXFIFO] =
  57. "read command so flush old data in the TX FIFO",
  58. [ABRT_SLAVE_ARBLOST] =
  59. "slave lost the bus while transmitting data to a remote master",
  60. [ABRT_SLAVE_RD_INTX] =
  61. "incorrect slave-transmitter mode configuration",
  62. };
  63. static int dw_reg_read(void *context, unsigned int reg, unsigned int *val)
  64. {
  65. struct dw_i2c_dev *dev = context;
  66. *val = readl(dev->base + reg);
  67. return 0;
  68. }
  69. static int dw_reg_write(void *context, unsigned int reg, unsigned int val)
  70. {
  71. struct dw_i2c_dev *dev = context;
  72. writel(val, dev->base + reg);
  73. return 0;
  74. }
  75. static int dw_reg_read_swab(void *context, unsigned int reg, unsigned int *val)
  76. {
  77. struct dw_i2c_dev *dev = context;
  78. *val = swab32(readl(dev->base + reg));
  79. return 0;
  80. }
  81. static int dw_reg_write_swab(void *context, unsigned int reg, unsigned int val)
  82. {
  83. struct dw_i2c_dev *dev = context;
  84. writel(swab32(val), dev->base + reg);
  85. return 0;
  86. }
  87. static int dw_reg_read_word(void *context, unsigned int reg, unsigned int *val)
  88. {
  89. struct dw_i2c_dev *dev = context;
  90. *val = readw(dev->base + reg) |
  91. (readw(dev->base + reg + 2) << 16);
  92. return 0;
  93. }
  94. static int dw_reg_write_word(void *context, unsigned int reg, unsigned int val)
  95. {
  96. struct dw_i2c_dev *dev = context;
  97. writew(val, dev->base + reg);
  98. writew(val >> 16, dev->base + reg + 2);
  99. return 0;
  100. }
  101. /**
  102. * i2c_dw_init_regmap() - Initialize registers map
  103. * @dev: device private data
  104. *
  105. * Autodetects needed register access mode and creates the regmap with
  106. * corresponding read/write callbacks. This must be called before doing any
  107. * other register access.
  108. */
  109. int i2c_dw_init_regmap(struct dw_i2c_dev *dev)
  110. {
  111. struct regmap_config map_cfg = {
  112. .reg_bits = 32,
  113. .val_bits = 32,
  114. .reg_stride = 4,
  115. .disable_locking = true,
  116. .reg_read = dw_reg_read,
  117. .reg_write = dw_reg_write,
  118. .max_register = DW_IC_COMP_TYPE,
  119. };
  120. u32 reg;
  121. int ret;
  122. /*
  123. * Skip detecting the registers map configuration if the regmap has
  124. * already been provided by a higher code.
  125. */
  126. if (dev->map)
  127. return 0;
  128. ret = i2c_dw_acquire_lock(dev);
  129. if (ret)
  130. return ret;
  131. reg = readl(dev->base + DW_IC_COMP_TYPE);
  132. i2c_dw_release_lock(dev);
  133. if ((dev->flags & MODEL_MASK) == MODEL_AMD_NAVI_GPU)
  134. map_cfg.max_register = AMD_UCSI_INTR_REG;
  135. if (reg == swab32(DW_IC_COMP_TYPE_VALUE)) {
  136. map_cfg.reg_read = dw_reg_read_swab;
  137. map_cfg.reg_write = dw_reg_write_swab;
  138. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  139. map_cfg.reg_read = dw_reg_read_word;
  140. map_cfg.reg_write = dw_reg_write_word;
  141. } else if (reg != DW_IC_COMP_TYPE_VALUE) {
  142. dev_err(dev->dev,
  143. "Unknown Synopsys component type: 0x%08x\n", reg);
  144. return -ENODEV;
  145. }
  146. /*
  147. * Note we'll check the return value of the regmap IO accessors only
  148. * at the probe stage. The rest of the code won't do this because
  149. * basically we have MMIO-based regmap so non of the read/write methods
  150. * can fail.
  151. */
  152. dev->map = devm_regmap_init(dev->dev, NULL, dev, &map_cfg);
  153. if (IS_ERR(dev->map)) {
  154. dev_err(dev->dev, "Failed to init the registers map\n");
  155. return PTR_ERR(dev->map);
  156. }
  157. return 0;
  158. }
  159. static const u32 supported_speeds[] = {
  160. I2C_MAX_HIGH_SPEED_MODE_FREQ,
  161. I2C_MAX_FAST_MODE_PLUS_FREQ,
  162. I2C_MAX_FAST_MODE_FREQ,
  163. I2C_MAX_STANDARD_MODE_FREQ,
  164. };
  165. static int i2c_dw_validate_speed(struct dw_i2c_dev *dev)
  166. {
  167. struct i2c_timings *t = &dev->timings;
  168. unsigned int i;
  169. /*
  170. * Only standard mode at 100kHz, fast mode at 400kHz,
  171. * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported.
  172. */
  173. for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
  174. if (t->bus_freq_hz == supported_speeds[i])
  175. return 0;
  176. }
  177. dev_err(dev->dev,
  178. "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
  179. t->bus_freq_hz);
  180. return -EINVAL;
  181. }
  182. #ifdef CONFIG_OF
  183. #include <linux/platform_device.h>
  184. #define MSCC_ICPU_CFG_TWI_DELAY 0x0
  185. #define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0)
  186. #define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4
  187. static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev)
  188. {
  189. writel((dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE,
  190. dev->ext + MSCC_ICPU_CFG_TWI_DELAY);
  191. return 0;
  192. }
  193. static void i2c_dw_of_configure(struct device *device)
  194. {
  195. struct platform_device *pdev = to_platform_device(device);
  196. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  197. switch (dev->flags & MODEL_MASK) {
  198. case MODEL_MSCC_OCELOT:
  199. dev->ext = devm_platform_ioremap_resource(pdev, 1);
  200. if (!IS_ERR(dev->ext))
  201. dev->set_sda_hold_time = mscc_twi_set_sda_hold_time;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. #else /* CONFIG_OF */
  208. static inline void i2c_dw_of_configure(struct device *device) { }
  209. #endif /* CONFIG_OF */
  210. #ifdef CONFIG_ACPI
  211. #include <linux/dmi.h>
  212. /*
  213. * The HCNT/LCNT information coming from ACPI should be the most accurate
  214. * for given platform. However, some systems get it wrong. On such systems
  215. * we get better results by calculating those based on the input clock.
  216. */
  217. static const struct dmi_system_id i2c_dw_no_acpi_params[] = {
  218. {
  219. .ident = "Dell Inspiron 7348",
  220. .matches = {
  221. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  222. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"),
  223. },
  224. },
  225. {}
  226. };
  227. static void i2c_dw_acpi_params(struct device *device, char method[],
  228. u16 *hcnt, u16 *lcnt, u32 *sda_hold)
  229. {
  230. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  231. acpi_handle handle = ACPI_HANDLE(device);
  232. union acpi_object *obj;
  233. if (dmi_check_system(i2c_dw_no_acpi_params))
  234. return;
  235. if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf)))
  236. return;
  237. obj = (union acpi_object *)buf.pointer;
  238. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) {
  239. const union acpi_object *objs = obj->package.elements;
  240. *hcnt = (u16)objs[0].integer.value;
  241. *lcnt = (u16)objs[1].integer.value;
  242. *sda_hold = (u32)objs[2].integer.value;
  243. }
  244. kfree(buf.pointer);
  245. }
  246. static void i2c_dw_acpi_configure(struct device *device)
  247. {
  248. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  249. struct i2c_timings *t = &dev->timings;
  250. u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0;
  251. /*
  252. * Try to get SDA hold time and *CNT values from an ACPI method for
  253. * selected speed modes.
  254. */
  255. i2c_dw_acpi_params(device, "SSCN", &dev->ss_hcnt, &dev->ss_lcnt, &ss_ht);
  256. i2c_dw_acpi_params(device, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, &fs_ht);
  257. i2c_dw_acpi_params(device, "FPCN", &dev->fp_hcnt, &dev->fp_lcnt, &fp_ht);
  258. i2c_dw_acpi_params(device, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, &hs_ht);
  259. switch (t->bus_freq_hz) {
  260. case I2C_MAX_STANDARD_MODE_FREQ:
  261. dev->sda_hold_time = ss_ht;
  262. break;
  263. case I2C_MAX_FAST_MODE_PLUS_FREQ:
  264. dev->sda_hold_time = fp_ht;
  265. break;
  266. case I2C_MAX_HIGH_SPEED_MODE_FREQ:
  267. dev->sda_hold_time = hs_ht;
  268. break;
  269. case I2C_MAX_FAST_MODE_FREQ:
  270. default:
  271. dev->sda_hold_time = fs_ht;
  272. break;
  273. }
  274. }
  275. static u32 i2c_dw_acpi_round_bus_speed(struct device *device)
  276. {
  277. u32 acpi_speed;
  278. int i;
  279. acpi_speed = i2c_acpi_find_bus_speed(device);
  280. /*
  281. * Some DSTDs use a non standard speed, round down to the lowest
  282. * standard speed.
  283. */
  284. for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
  285. if (acpi_speed >= supported_speeds[i])
  286. return supported_speeds[i];
  287. }
  288. return 0;
  289. }
  290. #else /* CONFIG_ACPI */
  291. static inline void i2c_dw_acpi_configure(struct device *device) { }
  292. static inline u32 i2c_dw_acpi_round_bus_speed(struct device *device) { return 0; }
  293. #endif /* CONFIG_ACPI */
  294. static void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
  295. {
  296. u32 acpi_speed = i2c_dw_acpi_round_bus_speed(dev->dev);
  297. struct i2c_timings *t = &dev->timings;
  298. /*
  299. * Find bus speed from the "clock-frequency" device property, ACPI
  300. * or by using fast mode if neither is set.
  301. */
  302. if (acpi_speed && t->bus_freq_hz)
  303. t->bus_freq_hz = min(t->bus_freq_hz, acpi_speed);
  304. else if (acpi_speed || t->bus_freq_hz)
  305. t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
  306. else
  307. t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
  308. }
  309. int i2c_dw_fw_parse_and_configure(struct dw_i2c_dev *dev)
  310. {
  311. struct i2c_timings *t = &dev->timings;
  312. struct device *device = dev->dev;
  313. struct fwnode_handle *fwnode = dev_fwnode(device);
  314. i2c_parse_fw_timings(device, t, false);
  315. i2c_dw_adjust_bus_speed(dev);
  316. if (is_of_node(fwnode))
  317. i2c_dw_of_configure(device);
  318. else if (is_acpi_node(fwnode))
  319. i2c_dw_acpi_configure(device);
  320. return i2c_dw_validate_speed(dev);
  321. }
  322. EXPORT_SYMBOL_GPL(i2c_dw_fw_parse_and_configure);
  323. static u32 i2c_dw_read_scl_reg(struct dw_i2c_dev *dev, u32 reg)
  324. {
  325. u32 val;
  326. int ret;
  327. ret = i2c_dw_acquire_lock(dev);
  328. if (ret)
  329. return 0;
  330. ret = regmap_read(dev->map, reg, &val);
  331. i2c_dw_release_lock(dev);
  332. return ret ? 0 : val;
  333. }
  334. u32 i2c_dw_scl_hcnt(struct dw_i2c_dev *dev, unsigned int reg, u32 ic_clk,
  335. u32 tSYMBOL, u32 tf, int cond, int offset)
  336. {
  337. if (!ic_clk)
  338. return i2c_dw_read_scl_reg(dev, reg);
  339. /*
  340. * DesignWare I2C core doesn't seem to have solid strategy to meet
  341. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  342. * will result in violation of the tHD;STA spec.
  343. */
  344. if (cond)
  345. /*
  346. * Conditional expression:
  347. *
  348. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  349. *
  350. * This is based on the DW manuals, and represents an ideal
  351. * configuration. The resulting I2C bus speed will be
  352. * faster than any of the others.
  353. *
  354. * If your hardware is free from tHD;STA issue, try this one.
  355. */
  356. return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * tSYMBOL, MICRO) -
  357. 8 + offset;
  358. else
  359. /*
  360. * Conditional expression:
  361. *
  362. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  363. *
  364. * This is just experimental rule; the tHD;STA period turned
  365. * out to be proportinal to (_HCNT + 3). With this setting,
  366. * we could meet both tHIGH and tHD;STA timing specs.
  367. *
  368. * If unsure, you'd better to take this alternative.
  369. *
  370. * The reason why we need to take into account "tf" here,
  371. * is the same as described in i2c_dw_scl_lcnt().
  372. */
  373. return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tSYMBOL + tf), MICRO) -
  374. 3 + offset;
  375. }
  376. u32 i2c_dw_scl_lcnt(struct dw_i2c_dev *dev, unsigned int reg, u32 ic_clk,
  377. u32 tLOW, u32 tf, int offset)
  378. {
  379. if (!ic_clk)
  380. return i2c_dw_read_scl_reg(dev, reg);
  381. /*
  382. * Conditional expression:
  383. *
  384. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  385. *
  386. * DW I2C core starts counting the SCL CNTs for the LOW period
  387. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  388. * In order to meet the tLOW timing spec, we need to take into
  389. * account the fall time of SCL signal (tf). Default tf value
  390. * should be 0.3 us, for safety.
  391. */
  392. return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tLOW + tf), MICRO) -
  393. 1 + offset;
  394. }
  395. int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)
  396. {
  397. unsigned int reg;
  398. int ret;
  399. ret = i2c_dw_acquire_lock(dev);
  400. if (ret)
  401. return ret;
  402. /* Configure SDA Hold Time if required */
  403. ret = regmap_read(dev->map, DW_IC_COMP_VERSION, &reg);
  404. if (ret)
  405. goto err_release_lock;
  406. if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
  407. if (!dev->sda_hold_time) {
  408. /* Keep previous hold time setting if no one set it */
  409. ret = regmap_read(dev->map, DW_IC_SDA_HOLD,
  410. &dev->sda_hold_time);
  411. if (ret)
  412. goto err_release_lock;
  413. }
  414. /*
  415. * Workaround for avoiding TX arbitration lost in case I2C
  416. * slave pulls SDA down "too quickly" after falling edge of
  417. * SCL by enabling non-zero SDA RX hold. Specification says it
  418. * extends incoming SDA low to high transition while SCL is
  419. * high but it appears to help also above issue.
  420. */
  421. if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
  422. dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
  423. dev_dbg(dev->dev, "SDA Hold Time TX:RX = %d:%d\n",
  424. dev->sda_hold_time & ~(u32)DW_IC_SDA_HOLD_RX_MASK,
  425. dev->sda_hold_time >> DW_IC_SDA_HOLD_RX_SHIFT);
  426. } else if (dev->set_sda_hold_time) {
  427. dev->set_sda_hold_time(dev);
  428. } else if (dev->sda_hold_time) {
  429. dev_warn(dev->dev,
  430. "Hardware too old to adjust SDA hold time.\n");
  431. dev->sda_hold_time = 0;
  432. }
  433. err_release_lock:
  434. i2c_dw_release_lock(dev);
  435. return ret;
  436. }
  437. void __i2c_dw_disable(struct dw_i2c_dev *dev)
  438. {
  439. struct i2c_timings *t = &dev->timings;
  440. unsigned int raw_intr_stats, ic_stats;
  441. unsigned int enable;
  442. int timeout = 100;
  443. bool abort_needed;
  444. unsigned int status;
  445. int ret;
  446. regmap_read(dev->map, DW_IC_RAW_INTR_STAT, &raw_intr_stats);
  447. regmap_read(dev->map, DW_IC_STATUS, &ic_stats);
  448. regmap_read(dev->map, DW_IC_ENABLE, &enable);
  449. abort_needed = (raw_intr_stats & DW_IC_INTR_MST_ON_HOLD) ||
  450. (ic_stats & DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY);
  451. if (abort_needed) {
  452. if (!(enable & DW_IC_ENABLE_ENABLE)) {
  453. regmap_write(dev->map, DW_IC_ENABLE, DW_IC_ENABLE_ENABLE);
  454. /*
  455. * Wait 10 times the signaling period of the highest I2C
  456. * transfer supported by the driver (for 400KHz this is
  457. * 25us) to ensure the I2C ENABLE bit is already set
  458. * as described in the DesignWare I2C databook.
  459. */
  460. fsleep(DIV_ROUND_CLOSEST_ULL(10 * MICRO, t->bus_freq_hz));
  461. /* Set ENABLE bit before setting ABORT */
  462. enable |= DW_IC_ENABLE_ENABLE;
  463. }
  464. regmap_write(dev->map, DW_IC_ENABLE, enable | DW_IC_ENABLE_ABORT);
  465. ret = regmap_read_poll_timeout(dev->map, DW_IC_ENABLE, enable,
  466. !(enable & DW_IC_ENABLE_ABORT), 10,
  467. 100);
  468. if (ret)
  469. dev_err(dev->dev, "timeout while trying to abort current transfer\n");
  470. }
  471. do {
  472. __i2c_dw_disable_nowait(dev);
  473. /*
  474. * The enable status register may be unimplemented, but
  475. * in that case this test reads zero and exits the loop.
  476. */
  477. regmap_read(dev->map, DW_IC_ENABLE_STATUS, &status);
  478. if ((status & 1) == 0)
  479. return;
  480. /*
  481. * Wait 10 times the signaling period of the highest I2C
  482. * transfer supported by the driver (for 400KHz this is
  483. * 25us) as described in the DesignWare I2C databook.
  484. */
  485. usleep_range(25, 250);
  486. } while (timeout--);
  487. dev_warn(dev->dev, "timeout in disabling adapter\n");
  488. }
  489. u32 i2c_dw_clk_rate(struct dw_i2c_dev *dev)
  490. {
  491. /*
  492. * Clock is not necessary if we got LCNT/HCNT values directly from
  493. * the platform code.
  494. */
  495. if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
  496. return 0;
  497. return dev->get_clk_rate_khz(dev);
  498. }
  499. int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare)
  500. {
  501. int ret;
  502. if (prepare) {
  503. /* Optional interface clock */
  504. ret = clk_prepare_enable(dev->pclk);
  505. if (ret)
  506. return ret;
  507. ret = clk_prepare_enable(dev->clk);
  508. if (ret)
  509. clk_disable_unprepare(dev->pclk);
  510. return ret;
  511. }
  512. clk_disable_unprepare(dev->clk);
  513. clk_disable_unprepare(dev->pclk);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL_GPL(i2c_dw_prepare_clk);
  517. int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
  518. {
  519. int ret;
  520. if (!dev->acquire_lock)
  521. return 0;
  522. ret = dev->acquire_lock();
  523. if (!ret)
  524. return 0;
  525. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  526. return ret;
  527. }
  528. void i2c_dw_release_lock(struct dw_i2c_dev *dev)
  529. {
  530. if (dev->release_lock)
  531. dev->release_lock();
  532. }
  533. /*
  534. * Waiting for bus not busy
  535. */
  536. int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  537. {
  538. unsigned int status;
  539. int ret;
  540. ret = regmap_read_poll_timeout(dev->map, DW_IC_STATUS, status,
  541. !(status & DW_IC_STATUS_ACTIVITY),
  542. 1100, 20000);
  543. if (ret) {
  544. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  545. i2c_recover_bus(&dev->adapter);
  546. regmap_read(dev->map, DW_IC_STATUS, &status);
  547. if (!(status & DW_IC_STATUS_ACTIVITY))
  548. ret = 0;
  549. }
  550. return ret;
  551. }
  552. int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  553. {
  554. unsigned long abort_source = dev->abort_source;
  555. int i;
  556. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  557. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  558. dev_dbg(dev->dev,
  559. "%s: %s\n", __func__, abort_sources[i]);
  560. return -EREMOTEIO;
  561. }
  562. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  563. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  564. if (abort_source & DW_IC_TX_ARB_LOST)
  565. return -EAGAIN;
  566. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  567. return -EINVAL; /* wrong msgs[] data */
  568. else
  569. return -EIO;
  570. }
  571. int i2c_dw_set_fifo_size(struct dw_i2c_dev *dev)
  572. {
  573. u32 tx_fifo_depth, rx_fifo_depth;
  574. unsigned int param;
  575. int ret;
  576. /* DW_IC_COMP_PARAM_1 not implement for IP issue */
  577. if ((dev->flags & MODEL_MASK) == MODEL_WANGXUN_SP) {
  578. dev->tx_fifo_depth = TXGBE_TX_FIFO_DEPTH;
  579. dev->rx_fifo_depth = TXGBE_RX_FIFO_DEPTH;
  580. return 0;
  581. }
  582. /*
  583. * Try to detect the FIFO depth if not set by interface driver,
  584. * the depth could be from 2 to 256 from HW spec.
  585. */
  586. ret = i2c_dw_acquire_lock(dev);
  587. if (ret)
  588. return ret;
  589. ret = regmap_read(dev->map, DW_IC_COMP_PARAM_1, &param);
  590. i2c_dw_release_lock(dev);
  591. if (ret)
  592. return ret;
  593. tx_fifo_depth = ((param >> 16) & 0xff) + 1;
  594. rx_fifo_depth = ((param >> 8) & 0xff) + 1;
  595. if (!dev->tx_fifo_depth) {
  596. dev->tx_fifo_depth = tx_fifo_depth;
  597. dev->rx_fifo_depth = rx_fifo_depth;
  598. } else if (tx_fifo_depth >= 2) {
  599. dev->tx_fifo_depth = min_t(u32, dev->tx_fifo_depth,
  600. tx_fifo_depth);
  601. dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth,
  602. rx_fifo_depth);
  603. }
  604. return 0;
  605. }
  606. u32 i2c_dw_func(struct i2c_adapter *adap)
  607. {
  608. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  609. return dev->functionality;
  610. }
  611. void i2c_dw_disable(struct dw_i2c_dev *dev)
  612. {
  613. unsigned int dummy;
  614. int ret;
  615. ret = i2c_dw_acquire_lock(dev);
  616. if (ret)
  617. return;
  618. /* Disable controller */
  619. __i2c_dw_disable(dev);
  620. /* Disable all interrupts */
  621. __i2c_dw_write_intr_mask(dev, 0);
  622. regmap_read(dev->map, DW_IC_CLR_INTR, &dummy);
  623. i2c_dw_release_lock(dev);
  624. }
  625. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  626. int i2c_dw_probe(struct dw_i2c_dev *dev)
  627. {
  628. device_set_node(&dev->adapter.dev, dev_fwnode(dev->dev));
  629. switch (dev->mode) {
  630. case DW_IC_SLAVE:
  631. return i2c_dw_probe_slave(dev);
  632. case DW_IC_MASTER:
  633. return i2c_dw_probe_master(dev);
  634. default:
  635. dev_err(dev->dev, "Wrong operation mode: %d\n", dev->mode);
  636. return -EINVAL;
  637. }
  638. }
  639. EXPORT_SYMBOL_GPL(i2c_dw_probe);
  640. static int i2c_dw_prepare(struct device *device)
  641. {
  642. /*
  643. * If the ACPI companion device object is present for this device,
  644. * it may be accessed during suspend and resume of other devices via
  645. * I2C operation regions, so tell the PM core and middle layers to
  646. * avoid skipping system suspend/resume callbacks for it in that case.
  647. */
  648. return !has_acpi_companion(device);
  649. }
  650. static int i2c_dw_runtime_suspend(struct device *device)
  651. {
  652. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  653. if (dev->shared_with_punit)
  654. return 0;
  655. i2c_dw_disable(dev);
  656. i2c_dw_prepare_clk(dev, false);
  657. return 0;
  658. }
  659. static int i2c_dw_suspend(struct device *device)
  660. {
  661. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  662. i2c_mark_adapter_suspended(&dev->adapter);
  663. return i2c_dw_runtime_suspend(device);
  664. }
  665. static int i2c_dw_runtime_resume(struct device *device)
  666. {
  667. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  668. if (!dev->shared_with_punit)
  669. i2c_dw_prepare_clk(dev, true);
  670. dev->init(dev);
  671. return 0;
  672. }
  673. static int i2c_dw_resume(struct device *device)
  674. {
  675. struct dw_i2c_dev *dev = dev_get_drvdata(device);
  676. i2c_dw_runtime_resume(device);
  677. i2c_mark_adapter_resumed(&dev->adapter);
  678. return 0;
  679. }
  680. EXPORT_GPL_DEV_PM_OPS(i2c_dw_dev_pm_ops) = {
  681. .prepare = pm_sleep_ptr(i2c_dw_prepare),
  682. LATE_SYSTEM_SLEEP_PM_OPS(i2c_dw_suspend, i2c_dw_resume)
  683. RUNTIME_PM_OPS(i2c_dw_runtime_suspend, i2c_dw_runtime_resume, NULL)
  684. };
  685. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  686. MODULE_LICENSE("GPL");