From patchwork Wed Oct 19 12:52:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Mitja_=C5=A0pes?= X-Patchwork-Id: 5596 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp365790wrs; Wed, 19 Oct 2022 07:43:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7LkoY+ize7f1e/q6IpAI4l80Qli0R5iH9JKctoNROU5TZZbk1sfdu3MDMhpNtk1NQpgSSt X-Received: by 2002:a05:6402:1356:b0:459:3c9e:81d4 with SMTP id y22-20020a056402135600b004593c9e81d4mr7990424edw.148.1666190595954; Wed, 19 Oct 2022 07:43:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666190595; cv=none; d=google.com; s=arc-20160816; b=PNaTMZRcwGt05MfIVRIf78VsnNKVaktFa/cO9JpENnK17MCzJa7kh5TDIrydrNck1f 3obGY+aJWfocjSzXevHDChZSBlYOSUuJ9lY7QBxryDV3zc4Mz6exME0SHlI5G/yz704o GP04wUxHrJjTjsgxHCsfszC1cRWdLTHDqTOcezCWHm/A/Kb/CoKnuhyG+35sKHgkkoiG 1FB9E1cDdAjyTNlnVozaUMuXLRFm9EwBsSu5+1M9nn4tO7sLR03pyzmsRf0MGrHwnvyQ QnHYV2hFbbGUzuaxLlkcR2wGarDRa0dhPQSVX+KjPjgYG6Qd7uJV3wEKfu3NfIN0V175 a1+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=t4Yaw2ZLuxRrXeXgMKdBXuSmh/kxDmx/nG04x9JTVpY=; b=pvQKJUc7oNVzE55owHOdcdxuBb0dver/kBry+Qnc5Fxfno3EGZIoJPVcXtAX8SwsMM 5drjI5/cwWrlcZ6cpygQUThFvkMiAjn86O9dLSTJ+hC8PPInFjI4kX1+VBZQGpDDNCqF XkXi0frdbma+fPObMyw1QK9Xgj3wdsVYcZHUIwiOxuXBIvnS+xSPaYH+2DXJoaJxFYzv VYkQ1nsZoUc+0o1Z0HwfsuQ7b+HLrLtHJ3tTccqiyUbQpVJHlJB9BqKYBvImSEDF113z ObEwWt2Am9HYyiOYBjtwk8vJqD0ad2VIeA31OnxbJ/Xtn8rI63UReawut7ooGT1OSQjP Qa6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=jRHmYMff; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e11-20020a056402104b00b004587cd5a5bdsi12906914edu.81.2022.10.19.07.42.47; Wed, 19 Oct 2022 07:43:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=jRHmYMff; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233597AbiJSOWe (ORCPT + 99 others); Wed, 19 Oct 2022 10:22:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233690AbiJSOWD (ORCPT ); Wed, 19 Oct 2022 10:22:03 -0400 Received: from dhl.lxnav.com (dhl.lxnav.com [IPv6:2a01:4f8:c010:2f07::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B39A1BB95A; Wed, 19 Oct 2022 07:05:39 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 2582B4068C; Wed, 19 Oct 2022 14:55:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lxnav.com; s=dhl; t=1666184103; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding; bh=t4Yaw2ZLuxRrXeXgMKdBXuSmh/kxDmx/nG04x9JTVpY=; b=jRHmYMffIWRnfbrYiOqE4kr4qmjAgByvauE5ssWejiTP7a6P6B6ll2wjJEIpCZ5RVfYRjJ IVQB5jk1ReSBD+vK9HtY4vqB1hv39FaWRG4UtSefk8bJpa92P5XmOsOv5uPAZp530/zcGm 4ZKyox1WtgeC9E0uSmhghMI2X2IvI5DmNJ5nae9r7pTNEqOUQ6UOML6VmFepGOY9V5LdVD WIrR9Ik3ejI3FE9ZdcN8/6Wnrha+8Mp79bT2K7Aycm6j3KOsKqCTLTwwOW/L8XxegRmvm1 /G8gUc8C97Nrr1jetQ9GDdVvmNRBNPuYER1DVcqJ3C39QDAGasqQwNWIDVn7rQ== From: Mitja Spes To: linux-iio@vger.kernel.org, Jonathan Cameron Cc: Mitja Spes , Lars-Peter Clausen , Rob Herring , Andy Shevchenko , Tomasz Duszynski , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] iio: pressure: ms5611: fixed value compensation bug Date: Wed, 19 Oct 2022 14:52:50 +0200 Message-Id: <20221019125254.952588-1-mitja@lxnav.com> MIME-Version: 1.0 X-Last-TLS-Session-Version: TLSv1.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747123630263947363?= X-GMAIL-MSGID: =?utf-8?q?1747127470329008341?= When using multiple instances of this driver the compensation PROM was overwritten by the last initialized sensor. Now each sensor has own PROM storage. Signed-off-by: Mitja Spes --- drivers/iio/pressure/ms5611.h | 12 +++---- drivers/iio/pressure/ms5611_core.c | 51 ++++++++++++++++-------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/drivers/iio/pressure/ms5611.h b/drivers/iio/pressure/ms5611.h index cbc9349c342a..550b75b7186f 100644 --- a/drivers/iio/pressure/ms5611.h +++ b/drivers/iio/pressure/ms5611.h @@ -25,13 +25,6 @@ enum { MS5607, }; -struct ms5611_chip_info { - u16 prom[MS5611_PROM_WORDS_NB]; - - int (*temp_and_pressure_compensate)(struct ms5611_chip_info *chip_info, - s32 *temp, s32 *pressure); -}; - /* * OverSampling Rate descriptor. * Warning: cmd MUST be kept aligned on a word boundary (see @@ -50,12 +43,15 @@ struct ms5611_state { const struct ms5611_osr *pressure_osr; const struct ms5611_osr *temp_osr; + u16 prom[MS5611_PROM_WORDS_NB]; + int (*reset)(struct ms5611_state *st); int (*read_prom_word)(struct ms5611_state *st, int index, u16 *word); int (*read_adc_temp_and_pressure)(struct ms5611_state *st, s32 *temp, s32 *pressure); - struct ms5611_chip_info *chip_info; + int (*compensate_temp_and_pressure)(struct ms5611_state *st, s32 *temp, + s32 *pressure); struct regulator *vdd; }; diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c index 717521de66c4..c564a1d6cafe 100644 --- a/drivers/iio/pressure/ms5611_core.c +++ b/drivers/iio/pressure/ms5611_core.c @@ -85,7 +85,7 @@ static int ms5611_read_prom(struct iio_dev *indio_dev) struct ms5611_state *st = iio_priv(indio_dev); for (i = 0; i < MS5611_PROM_WORDS_NB; i++) { - ret = st->read_prom_word(st, i, &st->chip_info->prom[i]); + ret = st->read_prom_word(st, i, &st->prom[i]); if (ret < 0) { dev_err(&indio_dev->dev, "failed to read prom at %d\n", i); @@ -93,7 +93,7 @@ static int ms5611_read_prom(struct iio_dev *indio_dev) } } - if (!ms5611_prom_is_valid(st->chip_info->prom, MS5611_PROM_WORDS_NB)) { + if (!ms5611_prom_is_valid(st->prom, MS5611_PROM_WORDS_NB)) { dev_err(&indio_dev->dev, "PROM integrity check failed\n"); return -ENODEV; } @@ -114,21 +114,20 @@ static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev, return ret; } - return st->chip_info->temp_and_pressure_compensate(st->chip_info, - temp, pressure); + return st->compensate_temp_and_pressure(st, temp, pressure); } -static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info, +static int ms5611_temp_and_pressure_compensate(struct ms5611_state *st, s32 *temp, s32 *pressure) { s32 t = *temp, p = *pressure; s64 off, sens, dt; - dt = t - (chip_info->prom[5] << 8); - off = ((s64)chip_info->prom[2] << 16) + ((chip_info->prom[4] * dt) >> 7); - sens = ((s64)chip_info->prom[1] << 15) + ((chip_info->prom[3] * dt) >> 8); + dt = t - (st->prom[5] << 8); + off = ((s64)st->prom[2] << 16) + ((st->prom[4] * dt) >> 7); + sens = ((s64)st->prom[1] << 15) + ((st->prom[3] * dt) >> 8); - t = 2000 + ((chip_info->prom[6] * dt) >> 23); + t = 2000 + ((st->prom[6] * dt) >> 23); if (t < 2000) { s64 off2, sens2, t2; @@ -154,17 +153,17 @@ static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_inf return 0; } -static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info, +static int ms5607_temp_and_pressure_compensate(struct ms5611_state *st, s32 *temp, s32 *pressure) { s32 t = *temp, p = *pressure; s64 off, sens, dt; - dt = t - (chip_info->prom[5] << 8); - off = ((s64)chip_info->prom[2] << 17) + ((chip_info->prom[4] * dt) >> 6); - sens = ((s64)chip_info->prom[1] << 16) + ((chip_info->prom[3] * dt) >> 7); + dt = t - (st->prom[5] << 8); + off = ((s64)st->prom[2] << 17) + ((st->prom[4] * dt) >> 6); + sens = ((s64)st->prom[1] << 16) + ((st->prom[3] * dt) >> 7); - t = 2000 + ((chip_info->prom[6] * dt) >> 23); + t = 2000 + ((st->prom[6] * dt) >> 23); if (t < 2000) { s64 off2, sens2, t2, tmp; @@ -342,15 +341,6 @@ static int ms5611_write_raw(struct iio_dev *indio_dev, static const unsigned long ms5611_scan_masks[] = {0x3, 0}; -static struct ms5611_chip_info chip_info_tbl[] = { - [MS5611] = { - .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate, - }, - [MS5607] = { - .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate, - } -}; - static const struct iio_chan_spec ms5611_channels[] = { { .type = IIO_PRESSURE, @@ -433,7 +423,20 @@ int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, struct ms5611_state *st = iio_priv(indio_dev); mutex_init(&st->lock); - st->chip_info = &chip_info_tbl[type]; + + switch (type) { + case MS5611: + st->compensate_temp_and_pressure = + ms5611_temp_and_pressure_compensate; + break; + case MS5607: + st->compensate_temp_and_pressure = + ms5607_temp_and_pressure_compensate; + break; + default: + return -EINVAL; + } + st->temp_osr = &ms5611_avail_temp_osr[ARRAY_SIZE(ms5611_avail_temp_osr) - 1]; st->pressure_osr =