20
20
#include <linux/delay.h>
21
21
#include <linux/err.h>
22
22
#include <linux/errno.h>
23
+ #include <linux/gpio/consumer.h>
23
24
#include <linux/i2c.h>
24
25
#include <linux/init.h>
25
26
#include <linux/interrupt.h>
28
29
#include <linux/module.h>
29
30
#include <linux/of.h>
30
31
#include <linux/of_device.h>
32
+ #include <linux/pinctrl/consumer.h>
31
33
#include <linux/platform_device.h>
32
34
#include <linux/platform_data/i2c-pxa.h>
33
35
#include <linux/slab.h>
@@ -260,6 +262,11 @@ struct pxa_i2c {
260
262
bool highmode_enter ;
261
263
u32 fm_mask ;
262
264
u32 hs_mask ;
265
+
266
+ struct i2c_bus_recovery_info recovery ;
267
+ struct pinctrl * pinctrl ;
268
+ struct pinctrl_state * pinctrl_default ;
269
+ struct pinctrl_state * pinctrl_recovery ;
263
270
};
264
271
265
272
#define _IBMR (i2c ) ((i2c)->reg_ibmr)
@@ -559,13 +566,8 @@ static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
559
566
#define i2c_pxa_set_slave (i2c , err ) do { } while (0)
560
567
#endif
561
568
562
- static void i2c_pxa_reset (struct pxa_i2c * i2c )
569
+ static void i2c_pxa_do_reset (struct pxa_i2c * i2c )
563
570
{
564
- pr_debug ("Resetting I2C Controller Unit\n" );
565
-
566
- /* abort any transfer currently under way */
567
- i2c_pxa_abort (i2c );
568
-
569
571
/* reset according to 9.8 */
570
572
writel (ICR_UR , _ICR (i2c ));
571
573
writel (I2C_ISR_INIT , _ISR (i2c ));
@@ -584,12 +586,25 @@ static void i2c_pxa_reset(struct pxa_i2c *i2c)
584
586
#endif
585
587
586
588
i2c_pxa_set_slave (i2c , 0 );
589
+ }
587
590
591
+ static void i2c_pxa_enable (struct pxa_i2c * i2c )
592
+ {
588
593
/* enable unit */
589
594
writel (readl (_ICR (i2c )) | ICR_IUE , _ICR (i2c ));
590
595
udelay (100 );
591
596
}
592
597
598
+ static void i2c_pxa_reset (struct pxa_i2c * i2c )
599
+ {
600
+ pr_debug ("Resetting I2C Controller Unit\n" );
601
+
602
+ /* abort any transfer currently under way */
603
+ i2c_pxa_abort (i2c );
604
+ i2c_pxa_do_reset (i2c );
605
+ i2c_pxa_enable (i2c );
606
+ }
607
+
593
608
594
609
#ifdef CONFIG_I2C_PXA_SLAVE
595
610
/*
@@ -1043,6 +1058,7 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
1043
1058
ret = i2c_pxa_wait_bus_not_busy (i2c );
1044
1059
if (ret ) {
1045
1060
dev_err (& i2c -> adap .dev , "i2c_pxa: timeout waiting for bus free\n" );
1061
+ i2c_recover_bus (& i2c -> adap );
1046
1062
goto out ;
1047
1063
}
1048
1064
@@ -1088,6 +1104,7 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
1088
1104
1089
1105
if (!timeout && i2c -> msg_num ) {
1090
1106
i2c_pxa_scream_blue_murder (i2c , "timeout with active message" );
1107
+ i2c_recover_bus (& i2c -> adap );
1091
1108
ret = I2C_RETRY ;
1092
1109
}
1093
1110
@@ -1277,6 +1294,129 @@ static int i2c_pxa_probe_pdata(struct platform_device *pdev,
1277
1294
return 0 ;
1278
1295
}
1279
1296
1297
+ static void i2c_pxa_prepare_recovery (struct i2c_adapter * adap )
1298
+ {
1299
+ struct pxa_i2c * i2c = adap -> algo_data ;
1300
+ u32 ibmr = readl (_IBMR (i2c ));
1301
+
1302
+ /*
1303
+ * Program the GPIOs to reflect the current I2C bus state while
1304
+ * we transition to recovery; this avoids glitching the bus.
1305
+ */
1306
+ gpiod_set_value (i2c -> recovery .scl_gpiod , ibmr & IBMR_SCLS );
1307
+ gpiod_set_value (i2c -> recovery .sda_gpiod , ibmr & IBMR_SDAS );
1308
+
1309
+ WARN_ON (pinctrl_select_state (i2c -> pinctrl , i2c -> pinctrl_recovery ));
1310
+ }
1311
+
1312
+ static void i2c_pxa_unprepare_recovery (struct i2c_adapter * adap )
1313
+ {
1314
+ struct pxa_i2c * i2c = adap -> algo_data ;
1315
+ u32 isr ;
1316
+
1317
+ /*
1318
+ * The bus should now be free. Clear up the I2C controller before
1319
+ * handing control of the bus back to avoid the bus changing state.
1320
+ */
1321
+ isr = readl (_ISR (i2c ));
1322
+ if (isr & (ISR_UB | ISR_IBB )) {
1323
+ dev_dbg (& i2c -> adap .dev ,
1324
+ "recovery: resetting controller, ISR=0x%08x\n" , isr );
1325
+ i2c_pxa_do_reset (i2c );
1326
+ }
1327
+
1328
+ WARN_ON (pinctrl_select_state (i2c -> pinctrl , i2c -> pinctrl_default ));
1329
+
1330
+ dev_dbg (& i2c -> adap .dev , "recovery: IBMR 0x%08x ISR 0x%08x\n" ,
1331
+ readl (_IBMR (i2c )), readl (_ISR (i2c )));
1332
+
1333
+ i2c_pxa_enable (i2c );
1334
+ }
1335
+
1336
+ static int i2c_pxa_init_recovery (struct pxa_i2c * i2c )
1337
+ {
1338
+ struct i2c_bus_recovery_info * bri = & i2c -> recovery ;
1339
+ struct device * dev = i2c -> adap .dev .parent ;
1340
+
1341
+ /*
1342
+ * When slave mode is enabled, we are not the only master on the bus.
1343
+ * Bus recovery can only be performed when we are the master, which
1344
+ * we can't be certain of. Therefore, when slave mode is enabled, do
1345
+ * not configure bus recovery.
1346
+ */
1347
+ if (IS_ENABLED (CONFIG_I2C_PXA_SLAVE ))
1348
+ return 0 ;
1349
+
1350
+ i2c -> pinctrl = devm_pinctrl_get (dev );
1351
+ if (IS_ERR (i2c -> pinctrl ))
1352
+ return PTR_ERR (i2c -> pinctrl );
1353
+
1354
+ if (!i2c -> pinctrl )
1355
+ return 0 ;
1356
+
1357
+ i2c -> pinctrl_default = pinctrl_lookup_state (i2c -> pinctrl ,
1358
+ PINCTRL_STATE_DEFAULT );
1359
+ i2c -> pinctrl_recovery = pinctrl_lookup_state (i2c -> pinctrl , "recovery" );
1360
+
1361
+ if (IS_ERR (i2c -> pinctrl_default ) || IS_ERR (i2c -> pinctrl_recovery )) {
1362
+ dev_info (dev , "missing pinmux recovery information: %ld %ld\n" ,
1363
+ PTR_ERR (i2c -> pinctrl_default ),
1364
+ PTR_ERR (i2c -> pinctrl_recovery ));
1365
+ return 0 ;
1366
+ }
1367
+
1368
+ /*
1369
+ * Claiming GPIOs can influence the pinmux state, and may glitch the
1370
+ * I2C bus. Do this carefully.
1371
+ */
1372
+ bri -> scl_gpiod = devm_gpiod_get (dev , "scl" , GPIOD_OUT_HIGH_OPEN_DRAIN );
1373
+ if (bri -> scl_gpiod == ERR_PTR (- EPROBE_DEFER ))
1374
+ return - EPROBE_DEFER ;
1375
+ if (IS_ERR (bri -> scl_gpiod )) {
1376
+ dev_info (dev , "missing scl gpio recovery information: %pe\n" ,
1377
+ bri -> scl_gpiod );
1378
+ return 0 ;
1379
+ }
1380
+
1381
+ /*
1382
+ * We have SCL. Pull SCL low and wait a bit so that SDA glitches
1383
+ * have no effect.
1384
+ */
1385
+ gpiod_direction_output (bri -> scl_gpiod , 0 );
1386
+ udelay (10 );
1387
+ bri -> sda_gpiod = devm_gpiod_get (dev , "sda" , GPIOD_OUT_HIGH_OPEN_DRAIN );
1388
+
1389
+ /* Wait a bit in case of a SDA glitch, and then release SCL. */
1390
+ udelay (10 );
1391
+ gpiod_direction_output (bri -> scl_gpiod , 1 );
1392
+
1393
+ if (bri -> sda_gpiod == ERR_PTR (- EPROBE_DEFER ))
1394
+ return - EPROBE_DEFER ;
1395
+
1396
+ if (IS_ERR (bri -> sda_gpiod )) {
1397
+ dev_info (dev , "missing sda gpio recovery information: %pe\n" ,
1398
+ bri -> sda_gpiod );
1399
+ return 0 ;
1400
+ }
1401
+
1402
+ bri -> prepare_recovery = i2c_pxa_prepare_recovery ;
1403
+ bri -> unprepare_recovery = i2c_pxa_unprepare_recovery ;
1404
+ bri -> recover_bus = i2c_generic_scl_recovery ;
1405
+
1406
+ i2c -> adap .bus_recovery_info = bri ;
1407
+
1408
+ /*
1409
+ * Claiming GPIOs can change the pinmux state, which confuses the
1410
+ * pinctrl since pinctrl's idea of the current setting is unaffected
1411
+ * by the pinmux change caused by claiming the GPIO. Work around that
1412
+ * by switching pinctrl to the GPIO state here. We do it this way to
1413
+ * avoid glitching the I2C bus.
1414
+ */
1415
+ pinctrl_select_state (i2c -> pinctrl , i2c -> pinctrl_recovery );
1416
+
1417
+ return pinctrl_select_state (i2c -> pinctrl , i2c -> pinctrl_default );
1418
+ }
1419
+
1280
1420
static int i2c_pxa_probe (struct platform_device * dev )
1281
1421
{
1282
1422
struct i2c_pxa_platform_data * plat = dev_get_platdata (& dev -> dev );
@@ -1289,6 +1429,16 @@ static int i2c_pxa_probe(struct platform_device *dev)
1289
1429
if (!i2c )
1290
1430
return - ENOMEM ;
1291
1431
1432
+ /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
1433
+ i2c -> adap .nr = dev -> id ;
1434
+ i2c -> adap .owner = THIS_MODULE ;
1435
+ i2c -> adap .retries = 5 ;
1436
+ i2c -> adap .algo_data = i2c ;
1437
+ i2c -> adap .dev .parent = & dev -> dev ;
1438
+ #ifdef CONFIG_OF
1439
+ i2c -> adap .dev .of_node = dev -> dev .of_node ;
1440
+ #endif
1441
+
1292
1442
res = platform_get_resource (dev , IORESOURCE_MEM , 0 );
1293
1443
i2c -> reg_base = devm_ioremap_resource (& dev -> dev , res );
1294
1444
if (IS_ERR (i2c -> reg_base ))
@@ -1298,18 +1448,16 @@ static int i2c_pxa_probe(struct platform_device *dev)
1298
1448
if (irq < 0 )
1299
1449
return irq ;
1300
1450
1301
- /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
1302
- i2c -> adap .nr = dev -> id ;
1451
+ ret = i2c_pxa_init_recovery (i2c );
1452
+ if (ret )
1453
+ return ret ;
1303
1454
1304
1455
ret = i2c_pxa_probe_dt (dev , i2c , & i2c_type );
1305
1456
if (ret > 0 )
1306
1457
ret = i2c_pxa_probe_pdata (dev , i2c , & i2c_type );
1307
1458
if (ret < 0 )
1308
1459
return ret ;
1309
1460
1310
- i2c -> adap .owner = THIS_MODULE ;
1311
- i2c -> adap .retries = 5 ;
1312
-
1313
1461
spin_lock_init (& i2c -> lock );
1314
1462
init_waitqueue_head (& i2c -> wait );
1315
1463
@@ -1375,12 +1523,6 @@ static int i2c_pxa_probe(struct platform_device *dev)
1375
1523
1376
1524
i2c_pxa_reset (i2c );
1377
1525
1378
- i2c -> adap .algo_data = i2c ;
1379
- i2c -> adap .dev .parent = & dev -> dev ;
1380
- #ifdef CONFIG_OF
1381
- i2c -> adap .dev .of_node = dev -> dev .of_node ;
1382
- #endif
1383
-
1384
1526
ret = i2c_add_numbered_adapter (& i2c -> adap );
1385
1527
if (ret < 0 )
1386
1528
goto ereqirq ;
0 commit comments