.. | .. |
---|
18 | 18 | #include <linux/io.h> |
---|
19 | 19 | #include <linux/of_address.h> |
---|
20 | 20 | #include <linux/of_irq.h> |
---|
| 21 | +#include <linux/reset.h> |
---|
21 | 22 | #include <linux/spinlock.h> |
---|
22 | 23 | #include <linux/clk.h> |
---|
23 | 24 | #include <linux/wait.h> |
---|
.. | .. |
---|
39 | 40 | #define REG_IEN 0x18 /* interrupt enable */ |
---|
40 | 41 | #define REG_IPD 0x1c /* interrupt pending */ |
---|
41 | 42 | #define REG_FCNT 0x20 /* finished count */ |
---|
| 43 | +#define REG_SCL_OE_DB 0x24 /* Slave hold scl debounce */ |
---|
42 | 44 | #define REG_CON1 0x228 /* control register1 */ |
---|
43 | 45 | |
---|
44 | 46 | /* Data buffer offsets */ |
---|
.. | .. |
---|
87 | 89 | #define REG_INT_START BIT(4) /* START condition generated */ |
---|
88 | 90 | #define REG_INT_STOP BIT(5) /* STOP condition generated */ |
---|
89 | 91 | #define REG_INT_NAKRCV BIT(6) /* NACK received */ |
---|
| 92 | +#define REG_INT_SLV_HDSCL BIT(7) /* slave hold scl */ |
---|
90 | 93 | #define REG_INT_ALL 0xff |
---|
91 | 94 | |
---|
92 | 95 | /* Disable i2c all irqs */ |
---|
.. | .. |
---|
97 | 100 | #define REG_CON1_NACK_AUTO_STOP BIT(2) |
---|
98 | 101 | |
---|
99 | 102 | /* Constants */ |
---|
100 | | -#define WAIT_TIMEOUT 1000 /* ms */ |
---|
| 103 | +#define WAIT_TIMEOUT 200 /* ms */ |
---|
101 | 104 | #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */ |
---|
102 | 105 | |
---|
103 | 106 | /** |
---|
104 | | - * struct i2c_spec_values: |
---|
| 107 | + * struct i2c_spec_values - I2C specification values for various modes |
---|
105 | 108 | * @min_hold_start_ns: min hold time (repeated) START condition |
---|
106 | 109 | * @min_low_ns: min LOW period of the SCL clock |
---|
107 | 110 | * @min_high_ns: min HIGH period of the SCL cloc |
---|
.. | .. |
---|
157 | 160 | }; |
---|
158 | 161 | |
---|
159 | 162 | /** |
---|
160 | | - * struct rk3x_i2c_calced_timings: |
---|
| 163 | + * struct rk3x_i2c_calced_timings - calculated V1 timings |
---|
161 | 164 | * @div_low: Divider output for low |
---|
162 | 165 | * @div_high: Divider output for high |
---|
163 | 166 | * @tuning: Used to adjust setup/hold data time, |
---|
.. | .. |
---|
179 | 182 | }; |
---|
180 | 183 | |
---|
181 | 184 | /** |
---|
182 | | - * struct rk3x_i2c_soc_data: |
---|
| 185 | + * struct rk3x_i2c_soc_data - SOC-specific data |
---|
183 | 186 | * @grf_offset: offset inside the grf regmap for setting the i2c type |
---|
184 | 187 | * @calc_timings: Callback function for i2c timing information calculated |
---|
185 | 188 | */ |
---|
.. | .. |
---|
224 | 227 | struct clk *pclk; |
---|
225 | 228 | struct notifier_block clk_rate_nb; |
---|
226 | 229 | bool autostop_supported; |
---|
| 230 | + |
---|
| 231 | + struct reset_control *reset; |
---|
| 232 | + struct reset_control *reset_apb; |
---|
227 | 233 | |
---|
228 | 234 | /* Settings */ |
---|
229 | 235 | struct i2c_timings t; |
---|
.. | .. |
---|
307 | 313 | if (len > 32) |
---|
308 | 314 | goto out; |
---|
309 | 315 | |
---|
| 316 | + /* For tx mode, one byte of the device address also needs to be counted, |
---|
| 317 | + * if the data length is equal to 32, which is actually 33 bytes, it would |
---|
| 318 | + * need to be divided into two parts, and needs to jump out of autostop. |
---|
| 319 | + */ |
---|
| 320 | + if (i2c->msg->len == 32 && i2c->mode == REG_CON_MOD_TX && !i2c->processed) |
---|
| 321 | + goto out; |
---|
| 322 | + |
---|
310 | 323 | i2c->state = STATE_STOP; |
---|
311 | 324 | |
---|
312 | 325 | con1 |= REG_CON1_TRANSFER_AUTO_STOP | REG_CON1_AUTO_STOP; |
---|
.. | .. |
---|
324 | 337 | } |
---|
325 | 338 | |
---|
326 | 339 | /** |
---|
327 | | - * Generate a START condition, which triggers a REG_INT_START interrupt. |
---|
| 340 | + * rk3x_i2c_start - Generate a START condition, which triggers a REG_INT_START interrupt. |
---|
| 341 | + * @i2c: target controller data |
---|
328 | 342 | */ |
---|
329 | 343 | static void rk3x_i2c_start(struct rk3x_i2c *i2c) |
---|
330 | 344 | { |
---|
.. | .. |
---|
364 | 378 | } |
---|
365 | 379 | |
---|
366 | 380 | /** |
---|
367 | | - * Generate a STOP condition, which triggers a REG_INT_STOP interrupt. |
---|
368 | | - * |
---|
| 381 | + * rk3x_i2c_stop - Generate a STOP condition, which triggers a REG_INT_STOP interrupt. |
---|
| 382 | + * @i2c: target controller data |
---|
369 | 383 | * @error: Error code to return in rk3x_i2c_xfer |
---|
370 | 384 | */ |
---|
371 | 385 | static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error) |
---|
.. | .. |
---|
373 | 387 | unsigned int ctrl; |
---|
374 | 388 | |
---|
375 | 389 | i2c->processed = 0; |
---|
376 | | - i2c->msg = NULL; |
---|
377 | 390 | i2c->error = error; |
---|
378 | 391 | |
---|
379 | 392 | if (i2c->is_last_msg) { |
---|
.. | .. |
---|
390 | 403 | /* Signal rk3x_i2c_xfer to start the next message. */ |
---|
391 | 404 | i2c->busy = false; |
---|
392 | 405 | i2c->state = STATE_IDLE; |
---|
| 406 | + i2c->msg = NULL; |
---|
393 | 407 | |
---|
394 | 408 | /* |
---|
395 | 409 | * The HW is actually not capable of REPEATED START. But we can |
---|
.. | .. |
---|
405 | 419 | } |
---|
406 | 420 | |
---|
407 | 421 | /** |
---|
408 | | - * Setup a read according to i2c->msg |
---|
| 422 | + * rk3x_i2c_prepare_read - Setup a read according to i2c->msg |
---|
| 423 | + * @i2c: target controller data |
---|
409 | 424 | */ |
---|
410 | 425 | static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c) |
---|
411 | 426 | { |
---|
.. | .. |
---|
438 | 453 | } |
---|
439 | 454 | |
---|
440 | 455 | /** |
---|
441 | | - * Fill the transmit buffer with data from i2c->msg |
---|
| 456 | + * rk3x_i2c_fill_transmit_buf - Fill the transmit buffer with data from i2c->msg |
---|
| 457 | + * @i2c: target controller data |
---|
442 | 458 | */ |
---|
443 | 459 | static int rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c, bool sendend) |
---|
444 | 460 | { |
---|
.. | .. |
---|
556 | 572 | } |
---|
557 | 573 | |
---|
558 | 574 | i2c->processed = 0; |
---|
559 | | - i2c->msg = NULL; |
---|
560 | 575 | } |
---|
561 | 576 | |
---|
562 | 577 | /* ack interrupt */ |
---|
.. | .. |
---|
571 | 586 | |
---|
572 | 587 | i2c->busy = false; |
---|
573 | 588 | i2c->state = STATE_IDLE; |
---|
| 589 | + i2c->msg = NULL; |
---|
574 | 590 | |
---|
575 | 591 | /* signal rk3x_i2c_xfer that we are finished */ |
---|
576 | 592 | rk3x_i2c_wake_up(i2c); |
---|
.. | .. |
---|
595 | 611 | dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd); |
---|
596 | 612 | |
---|
597 | 613 | /* Clean interrupt bits we don't care about */ |
---|
598 | | - ipd &= ~(REG_INT_BRF | REG_INT_BTF); |
---|
| 614 | + ipd &= ~(REG_INT_BRF | REG_INT_BTF | REG_INT_START); |
---|
599 | 615 | |
---|
600 | 616 | if (ipd & REG_INT_NAKRCV) { |
---|
601 | 617 | /* |
---|
.. | .. |
---|
642 | 658 | } |
---|
643 | 659 | |
---|
644 | 660 | /** |
---|
645 | | - * Get timing values of I2C specification |
---|
646 | | - * |
---|
| 661 | + * rk3x_i2c_get_spec - Get timing values of I2C specification |
---|
647 | 662 | * @speed: Desired SCL frequency |
---|
648 | 663 | * |
---|
649 | | - * Returns: Matched i2c spec values. |
---|
| 664 | + * Return: Matched i2c_spec_values. |
---|
650 | 665 | */ |
---|
651 | 666 | static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed) |
---|
652 | 667 | { |
---|
.. | .. |
---|
659 | 674 | } |
---|
660 | 675 | |
---|
661 | 676 | /** |
---|
662 | | - * Calculate divider values for desired SCL frequency |
---|
663 | | - * |
---|
| 677 | + * rk3x_i2c_v0_calc_timings - Calculate divider values for desired SCL frequency |
---|
664 | 678 | * @clk_rate: I2C input clock rate |
---|
665 | 679 | * @t: Known I2C timing information |
---|
666 | 680 | * @t_calc: Caculated rk3x private timings that would be written into regs |
---|
667 | 681 | * |
---|
668 | | - * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case |
---|
| 682 | + * Return: %0 on success, -%EINVAL if the goal SCL rate is too slow. In that case |
---|
669 | 683 | * a best-effort divider value is returned in divs. If the target rate is |
---|
670 | 684 | * too high, we silently use the highest possible rate. |
---|
671 | 685 | */ |
---|
.. | .. |
---|
820 | 834 | } |
---|
821 | 835 | |
---|
822 | 836 | /** |
---|
823 | | - * Calculate timing values for desired SCL frequency |
---|
824 | | - * |
---|
| 837 | + * rk3x_i2c_v1_calc_timings - Calculate timing values for desired SCL frequency |
---|
825 | 838 | * @clk_rate: I2C input clock rate |
---|
826 | 839 | * @t: Known I2C timing information |
---|
827 | 840 | * @t_calc: Caculated rk3x private timings that would be written into regs |
---|
828 | 841 | * |
---|
829 | | - * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case |
---|
| 842 | + * Return: %0 on success, -%EINVAL if the goal SCL rate is too slow. In that case |
---|
830 | 843 | * a best-effort divider value is returned in divs. If the target rate is |
---|
831 | 844 | * too high, we silently use the highest possible rate. |
---|
832 | 845 | * The following formulas are v1's method to calculate timings. |
---|
.. | .. |
---|
983 | 996 | { |
---|
984 | 997 | struct i2c_timings *t = &i2c->t; |
---|
985 | 998 | struct rk3x_i2c_calced_timings calc; |
---|
| 999 | + unsigned long period, time_hold = (WAIT_TIMEOUT / 2) * 1000000; |
---|
986 | 1000 | u64 t_low_ns, t_high_ns; |
---|
987 | 1001 | unsigned long flags; |
---|
988 | | - u32 val; |
---|
| 1002 | + u32 val, cnt; |
---|
989 | 1003 | int ret; |
---|
990 | 1004 | |
---|
991 | 1005 | ret = i2c->soc_data->calc_timings(clk_rate, t, &calc); |
---|
.. | .. |
---|
1000 | 1014 | i2c_writel(i2c, val, REG_CON); |
---|
1001 | 1015 | i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff), |
---|
1002 | 1016 | REG_CLKDIV); |
---|
| 1017 | + |
---|
| 1018 | + period = DIV_ROUND_UP(1000000000, clk_rate); |
---|
| 1019 | + cnt = DIV_ROUND_UP(time_hold, period); |
---|
| 1020 | + i2c_writel(i2c, cnt, REG_SCL_OE_DB); |
---|
1003 | 1021 | spin_unlock_irqrestore(&i2c->lock, flags); |
---|
1004 | 1022 | |
---|
1005 | 1023 | clk_disable(i2c->pclk); |
---|
.. | .. |
---|
1070 | 1088 | } |
---|
1071 | 1089 | |
---|
1072 | 1090 | /** |
---|
1073 | | - * Setup I2C registers for an I2C operation specified by msgs, num. |
---|
1074 | | - * |
---|
1075 | | - * Must be called with i2c->lock held. |
---|
1076 | | - * |
---|
| 1091 | + * rk3x_i2c_setup - Setup I2C registers for an I2C operation specified by msgs, num. |
---|
| 1092 | + * @i2c: target controller data |
---|
1077 | 1093 | * @msgs: I2C msgs to process |
---|
1078 | 1094 | * @num: Number of msgs |
---|
1079 | 1095 | * |
---|
1080 | | - * returns: Number of I2C msgs processed or negative in case of error |
---|
| 1096 | + * Must be called with i2c->lock held. |
---|
| 1097 | + * |
---|
| 1098 | + * Return: Number of I2C msgs processed or negative in case of error |
---|
1081 | 1099 | */ |
---|
1082 | 1100 | static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num) |
---|
1083 | 1101 | { |
---|
.. | .. |
---|
1165 | 1183 | return !i2c->busy; |
---|
1166 | 1184 | } |
---|
1167 | 1185 | |
---|
| 1186 | +/* |
---|
| 1187 | + * Reset i2c controller, reset all i2c registers. |
---|
| 1188 | + */ |
---|
| 1189 | +static void rk3x_i2c_reset_controller(struct rk3x_i2c *i2c) |
---|
| 1190 | +{ |
---|
| 1191 | + if (!IS_ERR_OR_NULL(i2c->reset)) { |
---|
| 1192 | + reset_control_assert(i2c->reset); |
---|
| 1193 | + udelay(10); |
---|
| 1194 | + reset_control_deassert(i2c->reset); |
---|
| 1195 | + } |
---|
| 1196 | + |
---|
| 1197 | + if (!IS_ERR_OR_NULL(i2c->reset_apb)) { |
---|
| 1198 | + reset_control_assert(i2c->reset_apb); |
---|
| 1199 | + udelay(10); |
---|
| 1200 | + reset_control_deassert(i2c->reset_apb); |
---|
| 1201 | + } |
---|
| 1202 | +} |
---|
| 1203 | + |
---|
1168 | 1204 | static int rk3x_i2c_xfer_common(struct i2c_adapter *adap, |
---|
1169 | 1205 | struct i2c_msg *msgs, int num, bool polling) |
---|
1170 | 1206 | { |
---|
1171 | 1207 | struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data; |
---|
1172 | 1208 | unsigned long timeout, flags; |
---|
1173 | | - u32 val; |
---|
| 1209 | + u32 val, ipd = 0; |
---|
1174 | 1210 | int ret = 0; |
---|
1175 | 1211 | int i; |
---|
1176 | 1212 | |
---|
.. | .. |
---|
1189 | 1225 | * rk3x_i2c_setup()). |
---|
1190 | 1226 | */ |
---|
1191 | 1227 | for (i = 0; i < num; i += ret) { |
---|
1192 | | - unsigned long xfer_time = 100; |
---|
| 1228 | + unsigned long xfer_time = WAIT_TIMEOUT; |
---|
1193 | 1229 | int len; |
---|
1194 | 1230 | |
---|
1195 | 1231 | ret = rk3x_i2c_setup(i2c, msgs + i, num - i); |
---|
.. | .. |
---|
1227 | 1263 | spin_lock_irqsave(&i2c->lock, flags); |
---|
1228 | 1264 | |
---|
1229 | 1265 | if (timeout == 0) { |
---|
| 1266 | + ipd = i2c_readl(i2c, REG_IPD); |
---|
1230 | 1267 | dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n", |
---|
1231 | | - i2c_readl(i2c, REG_IPD), i2c->state); |
---|
| 1268 | + ipd, i2c->state); |
---|
1232 | 1269 | |
---|
1233 | 1270 | /* Force a STOP condition without interrupt */ |
---|
1234 | 1271 | rk3x_i2c_disable_irq(i2c); |
---|
.. | .. |
---|
1255 | 1292 | clk_disable(i2c->clk); |
---|
1256 | 1293 | |
---|
1257 | 1294 | spin_unlock_irqrestore(&i2c->lock, flags); |
---|
| 1295 | + |
---|
| 1296 | + if ((ret == -ETIMEDOUT) && (ipd & REG_INT_SLV_HDSCL)) { |
---|
| 1297 | + rk3x_i2c_reset_controller(i2c); |
---|
| 1298 | + dev_err(i2c->dev, "SCL hold by slave, check your device.\n"); |
---|
| 1299 | + rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk)); |
---|
| 1300 | + } |
---|
1258 | 1301 | |
---|
1259 | 1302 | return ret < 0 ? ret : num; |
---|
1260 | 1303 | } |
---|
.. | .. |
---|
1489 | 1532 | device_property_read_u32(i2c->dev, "i2c,clk-rate", (u32 *)&clk_rate); |
---|
1490 | 1533 | |
---|
1491 | 1534 | rk3x_i2c_adapt_div(i2c, clk_rate); |
---|
1492 | | - |
---|
| 1535 | + if (rk3x_i2c_get_version(i2c) >= RK_I2C_VERSION5) |
---|
| 1536 | + i2c->autostop_supported = true; |
---|
1493 | 1537 | enable_irq(i2c->irq); |
---|
1494 | 1538 | } |
---|
1495 | 1539 | |
---|
.. | .. |
---|
1601 | 1645 | |
---|
1602 | 1646 | platform_set_drvdata(pdev, i2c); |
---|
1603 | 1647 | |
---|
| 1648 | + i2c->reset = devm_reset_control_get(&pdev->dev, "i2c"); |
---|
1604 | 1649 | if (!has_acpi_companion(&pdev->dev)) { |
---|
1605 | 1650 | if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) { |
---|
1606 | 1651 | /* Only one clock to use for bus clock and peripheral clock */ |
---|
.. | .. |
---|
1609 | 1654 | } else { |
---|
1610 | 1655 | i2c->clk = devm_clk_get(&pdev->dev, "i2c"); |
---|
1611 | 1656 | i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); |
---|
| 1657 | + i2c->reset_apb = devm_reset_control_get(&pdev->dev, "apb"); |
---|
1612 | 1658 | } |
---|
1613 | 1659 | |
---|
1614 | 1660 | if (IS_ERR(i2c->clk)) |
---|
.. | .. |
---|
1648 | 1694 | device_property_read_u32(&pdev->dev, "i2c,clk-rate", (u32 *)&clk_rate); |
---|
1649 | 1695 | |
---|
1650 | 1696 | rk3x_i2c_adapt_div(i2c, clk_rate); |
---|
1651 | | - } |
---|
1652 | 1697 | |
---|
1653 | | - if (rk3x_i2c_get_version(i2c) >= RK_I2C_VERSION5) |
---|
1654 | | - i2c->autostop_supported = true; |
---|
| 1698 | + if (rk3x_i2c_get_version(i2c) >= RK_I2C_VERSION5) |
---|
| 1699 | + i2c->autostop_supported = true; |
---|
| 1700 | + } |
---|
1655 | 1701 | |
---|
1656 | 1702 | ret = i2c_add_numbered_adapter(&i2c->adap); |
---|
1657 | 1703 | if (ret < 0) |
---|