From patchwork Sat Jan 9 23:11:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Belloni X-Patchwork-Id: 12008809 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A51AC433DB for ; Sat, 9 Jan 2021 23:19:41 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 005FF2396F for ; Sat, 9 Jan 2021 23:19:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 005FF2396F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bootlin.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=QoynrT32PR18SoEf9POStO91nB/83JowK6LsCHG3J5A=; b=boXmHajAF7iUj8TRsTu3OF1SN wYWs2IVRO2rub+O+LIIbD86jcjqSSIh63ChRw6ShU7aEI8nKcCI17nBlmYvBJPLJ7g7g1Cg+mv0rj 14iHZqPESieslom1CPWmHlbDOAeq7nm2js/GZZztgl3sCB3o9g5jrxhCilEuWb1+Yp0n74bHew46b Q7A512lbooXS+pCNrnxLW6CbLkV9wJbHu+HbB0NFzNrpHKJF1Kk78Rj/uRFgg5gE0fmXsjLIqb8sm xY4PnK11At+Iz4cHtY7dMnBNYT0a9X2b2UCWXv8QUukJJK9dVTg26kvqlKkBB0xk07BkOJUU2QGl6 TfzrTxlmQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kyNSw-0008Sn-Ol; Sat, 09 Jan 2021 23:16:16 +0000 Received: from relay2-d.mail.gandi.net ([217.70.183.194]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kyNPg-0006fT-Vl for linux-arm-kernel@lists.infradead.org; Sat, 09 Jan 2021 23:12:54 +0000 X-Originating-IP: 86.202.109.140 Received: from localhost (lfbn-lyo-1-13-140.w86-202.abo.wanadoo.fr [86.202.109.140]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id F151B40008; Sat, 9 Jan 2021 23:12:46 +0000 (UTC) From: Alexandre Belloni To: Jonathan Cameron Subject: [PATCH v2 5/6] iio:common:ms_sensors:ms_sensors_i2c: add support for alternative PROM layout Date: Sun, 10 Jan 2021 00:11:47 +0100 Message-Id: <20210109231148.1168104-6-alexandre.belloni@bootlin.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210109231148.1168104-1-alexandre.belloni@bootlin.com> References: <20210109231148.1168104-1-alexandre.belloni@bootlin.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210109_181253_358612_F21FF8C2 X-CRM114-Status: GOOD ( 15.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, Alexandre Belloni , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Meerwald-Stadler , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Currently, only the 112bit PROM with 7 words is supported. However the ms58xx family also have devices with a 128bit PROM on 8 words. See AN520: C-CODE EXAMPLE FOR MS56XX, MS57XX (EXCEPT ANALOG SENSOR), AND MS58XX SERIES PRESSURE SENSORS and the various device datasheets. The difference is that the CRC is the 4 LSBs of word7 instead of being the 4 MSBs of word0. Signed-off-by: Alexandre Belloni --- Changes in v2: - Fix return value documentation for ms_sensors_tp_crc_valid_112 and ms_sensors_tp_crc_valid_128 .../iio/common/ms_sensors/ms_sensors_i2c.c | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c index 872f90459e2e..16ea697e945c 100644 --- a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c +++ b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c @@ -488,21 +488,18 @@ int ms_sensors_ht_read_humidity(struct ms_ht_dev *dev_data, EXPORT_SYMBOL(ms_sensors_ht_read_humidity); /** - * ms_sensors_tp_crc_valid() - CRC check function for + * ms_sensors_tp_crc4() - Calculate PROM CRC for * Temperature and pressure devices. * This function is only used when reading PROM coefficients * * @prom: pointer to PROM coefficients array * - * Return: True if CRC is ok. + * Return: CRC. */ -static bool ms_sensors_tp_crc_valid(u16 *prom) +static u8 ms_sensors_tp_crc4(u16 *prom) { unsigned int cnt, n_bit; - u16 n_rem = 0x0000, crc_read = prom[0], crc = (*prom & 0xF000) >> 12; - - prom[MS_SENSORS_TP_PROM_WORDS_NB - 1] = 0; - prom[0] &= 0x0FFF; /* Clear the CRC computation part */ + u16 n_rem = 0x0000; for (cnt = 0; cnt < MS_SENSORS_TP_PROM_WORDS_NB * 2; cnt++) { if (cnt % 2 == 1) @@ -517,10 +514,55 @@ static bool ms_sensors_tp_crc_valid(u16 *prom) n_rem <<= 1; } } - n_rem >>= 12; - prom[0] = crc_read; - return n_rem == crc; + return n_rem >> 12; +} + +/** + * ms_sensors_tp_crc_valid_112() - CRC check function for + * Temperature and pressure devices for 112bit PROM. + * This function is only used when reading PROM coefficients + * + * @prom: pointer to PROM coefficients array + * + * Return: True if CRC is ok. + */ +static bool ms_sensors_tp_crc_valid_112(u16 *prom) +{ + u16 w0 = prom[0], crc_read = (w0 & 0xF000) >> 12; + u8 crc; + + prom[0] &= 0x0FFF; /* Clear the CRC computation part */ + prom[MS_SENSORS_TP_PROM_WORDS_NB - 1] = 0; + + crc = ms_sensors_tp_crc4(prom); + + prom[0] = w0; + + return crc == crc_read; +} + +/** + * ms_sensors_tp_crc_valid_128() - CRC check function for + * Temperature and pressure devices for 128bit PROM. + * This function is only used when reading PROM coefficients + * + * @prom: pointer to PROM coefficients array + * + * Return: True if CRC is ok. + */ +static bool ms_sensors_tp_crc_valid_128(u16 *prom) +{ + u16 w7 = prom[7], crc_read = w7 & 0x000F; + u8 crc; + + prom[7] &= 0xFF00; /* Clear the CRC and LSB part */ + + crc = ms_sensors_tp_crc4(prom); + + prom[7] = w7; + + return crc == crc_read; } /** @@ -535,6 +577,7 @@ static bool ms_sensors_tp_crc_valid(u16 *prom) int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data) { int i, ret; + bool valid; for (i = 0; i < dev_data->hw->prom_len; i++) { ret = ms_sensors_read_prom_word( @@ -546,7 +589,12 @@ int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data) return ret; } - if (!ms_sensors_tp_crc_valid(dev_data->prom)) { + if (dev_data->hw->prom_len == 8) + valid = ms_sensors_tp_crc_valid_128(dev_data->prom); + else + valid = ms_sensors_tp_crc_valid_112(dev_data->prom); + + if (!valid) { dev_err(&dev_data->client->dev, "Calibration coefficients crc check error\n"); return -ENODEV;