2020#include <linux/delay.h>
2121#include <linux/err.h>
2222#include <linux/errno.h>
23+ #include <linux/gpio/consumer.h>
2324#include <linux/i2c.h>
2425#include <linux/init.h>
2526#include <linux/interrupt.h>
2829#include <linux/module.h>
2930#include <linux/of.h>
3031#include <linux/of_device.h>
32+ #include <linux/pinctrl/consumer.h>
3133#include <linux/platform_device.h>
3234#include <linux/platform_data/i2c-pxa.h>
3335#include <linux/slab.h>
@@ -260,6 +262,11 @@ struct pxa_i2c {
260262bool highmode_enter ;
261263u32 fm_mask ;
262264u32 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 ;
263270};
264271
265272#define _IBMR (i2c )((i2c)->reg_ibmr)
@@ -559,13 +566,8 @@ static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
559566#define i2c_pxa_set_slave (i2c ,err )do { } while (0)
560567#endif
561568
562- static void i2c_pxa_reset (struct pxa_i2c * i2c )
569+ static void i2c_pxa_do_reset (struct pxa_i2c * i2c )
563570{
564- pr_debug ("Resetting I2C Controller Unit\n" );
565-
566- /* abort any transfer currently under way */
567- i2c_pxa_abort (i2c );
568-
569571/* reset according to 9.8 */
570572writel (ICR_UR ,_ICR (i2c ));
571573writel (I2C_ISR_INIT ,_ISR (i2c ));
@@ -584,12 +586,25 @@ static void i2c_pxa_reset(struct pxa_i2c *i2c)
584586#endif
585587
586588i2c_pxa_set_slave (i2c ,0 );
589+ }
587590
591+ static void i2c_pxa_enable (struct pxa_i2c * i2c )
592+ {
588593/* enable unit */
589594writel (readl (_ICR (i2c )) |ICR_IUE ,_ICR (i2c ));
590595udelay (100 );
591596}
592597
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+
593608
594609#ifdef CONFIG_I2C_PXA_SLAVE
595610/*
@@ -1043,6 +1058,7 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
10431058ret = i2c_pxa_wait_bus_not_busy (i2c );
10441059if (ret ) {
10451060dev_err (& i2c -> adap .dev ,"i2c_pxa: timeout waiting for bus free\n" );
1061+ i2c_recover_bus (& i2c -> adap );
10461062gotoout ;
10471063}
10481064
@@ -1088,6 +1104,7 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
10881104
10891105if (!timeout && i2c -> msg_num ) {
10901106i2c_pxa_scream_blue_murder (i2c ,"timeout with active message" );
1107+ i2c_recover_bus (& i2c -> adap );
10911108ret = I2C_RETRY ;
10921109}
10931110
@@ -1277,6 +1294,129 @@ static int i2c_pxa_probe_pdata(struct platform_device *pdev,
12771294return 0 ;
12781295}
12791296
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+
12801420static int i2c_pxa_probe (struct platform_device * dev )
12811421{
12821422struct i2c_pxa_platform_data * plat = dev_get_platdata (& dev -> dev );
@@ -1289,6 +1429,16 @@ static int i2c_pxa_probe(struct platform_device *dev)
12891429if (!i2c )
12901430return - ENOMEM ;
12911431
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+
12921442res = platform_get_resource (dev ,IORESOURCE_MEM ,0 );
12931443i2c -> reg_base = devm_ioremap_resource (& dev -> dev ,res );
12941444if (IS_ERR (i2c -> reg_base ))
@@ -1298,18 +1448,16 @@ static int i2c_pxa_probe(struct platform_device *dev)
12981448if (irq < 0 )
12991449return irq ;
13001450
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 ;
13031454
13041455ret = i2c_pxa_probe_dt (dev ,i2c ,& i2c_type );
13051456if (ret > 0 )
13061457ret = i2c_pxa_probe_pdata (dev ,i2c ,& i2c_type );
13071458if (ret < 0 )
13081459return ret ;
13091460
1310- i2c -> adap .owner = THIS_MODULE ;
1311- i2c -> adap .retries = 5 ;
1312-
13131461spin_lock_init (& i2c -> lock );
13141462init_waitqueue_head (& i2c -> wait );
13151463
@@ -1375,12 +1523,6 @@ static int i2c_pxa_probe(struct platform_device *dev)
13751523
13761524i2c_pxa_reset (i2c );
13771525
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-
13841526ret = i2c_add_numbered_adapter (& i2c -> adap );
13851527if (ret < 0 )
13861528gotoereqirq ;