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