Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3292004ybl; Mon, 27 Jan 2020 00:55:23 -0800 (PST) X-Google-Smtp-Source: APXvYqzGd1Y7YRCzjehmLCm9adB80+ehg8h33A6URc72cFO7ryqULeNifZQJmJ2BbtZ59mCUYcho X-Received: by 2002:aca:815:: with SMTP id 21mr7076544oii.52.1580115323244; Mon, 27 Jan 2020 00:55:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580115323; cv=none; d=google.com; s=arc-20160816; b=CD7oCFJUEgqZFMoLCczxZjXtKos8Zs8Pu/j1a8EIhQzNVwPgbUTHyQ1T01fFpVDxDX rdWjXGIZKvjkTTJDKdkmA9ZlqYQUgnhSAANcuQGMnS8t/WDsSDae4uQbfsY1xNlTR02P XKz4ddMMNKaaFZUhvInOFQHOZVloSOUa5IrC9YcLnQQcgsBr6Ic1BHuyPQt4eGLffgF8 xQJ/Tzae5XPp0Dh6ZGafSakGKE9NpuJ4USHEcFGeUd30+R67RWS4KsNMCEaNFQLEQFSS U8BSAYq61tZ07CxDwr9o+EPW0fyNtsgP/vPEuLqBzQgA2Cp6SGe5h1KjYBvSW/hVbaOG aAmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:ironport-sdr; bh=x0l9CG0TtM2ipF1zTcnY1LrFf9aG/rR8QKtEn7S3m/k=; b=HQ+RHzcU/lkqVeCzGtf/AVBAxWdISU3J/A00+e+a3uh7jC6DoPOMF0jjGk4EVApuNl DM2rYLovHiEmhJrPULqHbbpgfsLI0GgSyaKONJsTiPuF5pU42fMd6TTkeTdiBO+EkKt4 7/5hbTuYJilSM20aE/u9GarUYagaTWHbBhkO3PjeIQE4brQ/hPC6JoyfxgJBVLNAVA7R 9odrlWQ2X17JNUR4qPTK8kABMEXmbKks5LM+beOsNHuWRZaJZub5HcDuj1iyrTdm1ZMo zLnAdK/nl2ISEV8S6dRLvk2yyg5HVa1uLIMwEiE0lQEuW8w7fCVz3N/t6ODOLmEk15QO lLiQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=microchip.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n7si6752715otk.277.2020.01.27.00.55.11; Mon, 27 Jan 2020 00:55:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729316AbgA0IvU (ORCPT + 99 others); Mon, 27 Jan 2020 03:51:20 -0500 Received: from esa4.microchip.iphmx.com ([68.232.154.123]:62224 "EHLO esa4.microchip.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726191AbgA0IvT (ORCPT ); Mon, 27 Jan 2020 03:51:19 -0500 Received-SPF: Pass (esa4.microchip.iphmx.com: domain of Ludovic.Desroches@microchip.com designates 198.175.253.82 as permitted sender) identity=mailfrom; client-ip=198.175.253.82; receiver=esa4.microchip.iphmx.com; envelope-from="Ludovic.Desroches@microchip.com"; x-sender="Ludovic.Desroches@microchip.com"; x-conformance=spf_only; x-record-type="v=spf1"; x-record-text="v=spf1 mx a:ushub1.microchip.com a:smtpout.microchip.com -exists:%{i}.spf.microchip.iphmx.com include:servers.mcsv.net include:mktomail.com include:spf.protection.outlook.com ~all" Received-SPF: None (esa4.microchip.iphmx.com: no sender authenticity information available from domain of postmaster@email.microchip.com) identity=helo; client-ip=198.175.253.82; receiver=esa4.microchip.iphmx.com; envelope-from="Ludovic.Desroches@microchip.com"; x-sender="postmaster@email.microchip.com"; x-conformance=spf_only Authentication-Results: esa4.microchip.iphmx.com; dkim=none (message not signed) header.i=none; spf=Pass smtp.mailfrom=Ludovic.Desroches@microchip.com; spf=None smtp.helo=postmaster@email.microchip.com; dmarc=pass (p=none dis=none) d=microchip.com IronPort-SDR: AxAwd0T3ZOmV0XLI2LYNOrI/YJoJkdhfnjSn5h5cXH3thlUN4Ju0axioMoSydwZrDv6iNwH1Y1 UTnNmsXFX98ErPpA6hBTBPvCO70Rgu0z03tDTMuejhrkdicshIX7J9EBM6eB0cCQdynEQdnlJw y2vCPb12x4FERSVS7YSztdAjU4wE56wK1aT89TfpXsYXzPRKhDg/wIXDdTUudyGnhpt6phFFmA G9XTTHWYBrKAMsNt0zHR37PMj+vv7g1ppC0SS2VQ8sYDlFNNkIw6fjoa5cwMIMlrALNJoZy4cm TdE= X-IronPort-AV: E=Sophos;i="5.70,369,1574146800"; d="scan'208";a="62246076" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 27 Jan 2020 01:51:16 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.87.72) by chn-vm-ex02.mchp-main.com (10.10.87.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Mon, 27 Jan 2020 01:51:14 -0700 Received: from localhost (10.10.85.251) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Mon, 27 Jan 2020 01:51:14 -0700 Date: Mon, 27 Jan 2020 09:50:49 +0100 From: Ludovic Desroches To: Codrin Ciubotariu CC: , , , , , , , , , , Subject: Re: [PATCH v3 2/6] i2c: at91: implement i2c bus recovery Message-ID: <20200127085049.itsxvqm5dlypzc4x@M43218.corp.atmel.com> Mail-Followup-To: Codrin Ciubotariu , linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kamel.bouhara@bootlin.com, wsa@the-dreams.de, Nicolas.Ferre@microchip.com, alexandre.belloni@bootlin.com, robh@kernel.org, peda@axentia.se, linux@armlinux.org.uk References: <20200115115422.17097-1-codrin.ciubotariu@microchip.com> <20200115115422.17097-3-codrin.ciubotariu@microchip.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20200115115422.17097-3-codrin.ciubotariu@microchip.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 15, 2020 at 01:54:18PM +0200, Codrin Ciubotariu wrote: > From: Kamel Bouhara > > Implement i2c bus recovery when slaves devices might hold SDA low. > In this case re-assign SCL/SDA to gpios and issue 9 dummy clock pulses > until the slave release SDA. > > Signed-off-by: Kamel Bouhara > Signed-off-by: Codrin Ciubotariu Acked-by: Ludovic Desroches > --- > > Changes in v3: > - removed unnecessary condition from info print; > - removed unneded declarations; > > Changes in v2: > - called i2c_recover_bus() after an error occurs, if SDA is down; > - release gpios if recovery information is incomplete; > > drivers/i2c/busses/i2c-at91-master.c | 78 ++++++++++++++++++++++++++++ > drivers/i2c/busses/i2c-at91.h | 4 ++ > 2 files changed, 82 insertions(+) > > diff --git a/drivers/i2c/busses/i2c-at91-master.c b/drivers/i2c/busses/i2c-at91-master.c > index 7a862e00b475..0aba51a7df32 100644 > --- a/drivers/i2c/busses/i2c-at91-master.c > +++ b/drivers/i2c/busses/i2c-at91-master.c > @@ -18,11 +18,13 @@ > #include > #include > #include > +#include > #include > #include > #include > #include > #include > +#include > #include > #include > #include > @@ -478,6 +480,7 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev) > unsigned long time_left; > bool has_unre_flag = dev->pdata->has_unre_flag; > bool has_alt_cmd = dev->pdata->has_alt_cmd; > + struct i2c_bus_recovery_info *rinfo = &dev->rinfo; > > /* > * WARNING: the TXCOMP bit in the Status Register is NOT a clear on > @@ -637,6 +640,13 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev) > at91_twi_write(dev, AT91_TWI_CR, > AT91_TWI_THRCLR | AT91_TWI_LOCKCLR); > } > + > + if (rinfo->get_sda && !(rinfo->get_sda(&dev->adapter))) { > + dev_dbg(dev->dev, > + "SDA is down; clear bus using gpio\n"); > + i2c_recover_bus(&dev->adapter); > + } > + > return ret; > } > > @@ -806,6 +816,70 @@ static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr) > return ret; > } > > +static void at91_prepare_twi_recovery(struct i2c_adapter *adap) > +{ > + struct at91_twi_dev *dev = i2c_get_adapdata(adap); > + > + pinctrl_select_state(dev->pinctrl, dev->pinctrl_pins_gpio); > +} > + > +static void at91_unprepare_twi_recovery(struct i2c_adapter *adap) > +{ > + struct at91_twi_dev *dev = i2c_get_adapdata(adap); > + > + pinctrl_select_state(dev->pinctrl, dev->pinctrl_pins_default); > +} > + > +static int at91_init_twi_recovery_info(struct platform_device *pdev, > + struct at91_twi_dev *dev) > +{ > + struct i2c_bus_recovery_info *rinfo = &dev->rinfo; > + > + dev->pinctrl = devm_pinctrl_get(&pdev->dev); > + if (!dev->pinctrl || IS_ERR(dev->pinctrl)) { > + dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n"); > + return PTR_ERR(dev->pinctrl); > + } > + > + dev->pinctrl_pins_default = pinctrl_lookup_state(dev->pinctrl, > + PINCTRL_STATE_DEFAULT); > + dev->pinctrl_pins_gpio = pinctrl_lookup_state(dev->pinctrl, > + "gpio"); > + rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN); > + if (PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER) > + return -EPROBE_DEFER; > + > + rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", > + GPIOD_OUT_HIGH_OPEN_DRAIN); > + if (PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER) > + return -EPROBE_DEFER; > + > + if (IS_ERR(rinfo->sda_gpiod) || > + IS_ERR(rinfo->scl_gpiod) || > + IS_ERR(dev->pinctrl_pins_default) || > + IS_ERR(dev->pinctrl_pins_gpio)) { > + dev_info(&pdev->dev, "recovery information incomplete\n"); > + if (!IS_ERR(rinfo->sda_gpiod)) { > + gpiod_put(rinfo->sda_gpiod); > + rinfo->sda_gpiod = NULL; > + } > + if (!IS_ERR(rinfo->scl_gpiod)) { > + gpiod_put(rinfo->scl_gpiod); > + rinfo->scl_gpiod = NULL; > + } > + return -EINVAL; > + } > + > + dev_info(&pdev->dev, "using scl, sda for recovery\n"); > + > + rinfo->prepare_recovery = at91_prepare_twi_recovery; > + rinfo->unprepare_recovery = at91_unprepare_twi_recovery; > + rinfo->recover_bus = i2c_generic_scl_recovery; > + dev->adapter.bus_recovery_info = rinfo; > + > + return 0; > +} > + > int at91_twi_probe_master(struct platform_device *pdev, > u32 phy_addr, struct at91_twi_dev *dev) > { > @@ -838,6 +912,10 @@ int at91_twi_probe_master(struct platform_device *pdev, > "i2c-analog-filter"); > at91_calc_twi_clock(dev); > > + rc = at91_init_twi_recovery_info(pdev, dev); > + if (rc == -EPROBE_DEFER) > + return rc; > + > dev->adapter.algo = &at91_twi_algorithm; > dev->adapter.quirks = &at91_twi_quirks; > > diff --git a/drivers/i2c/busses/i2c-at91.h b/drivers/i2c/busses/i2c-at91.h > index 977a67bc0f88..f57a6cab96b4 100644 > --- a/drivers/i2c/busses/i2c-at91.h > +++ b/drivers/i2c/busses/i2c-at91.h > @@ -151,6 +151,10 @@ struct at91_twi_dev { > u32 fifo_size; > struct at91_twi_dma dma; > bool slave_detected; > + struct i2c_bus_recovery_info rinfo; > + struct pinctrl *pinctrl; > + struct pinctrl_state *pinctrl_pins_default; > + struct pinctrl_state *pinctrl_pins_gpio; > #ifdef CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL > unsigned smr; > struct i2c_client *slave; > -- > 2.20.1 >