From patchwork Mon Apr 20 20:40:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 207931 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=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, 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 2222DC55181 for ; Mon, 20 Apr 2020 20:40:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 021AF20782 for ; Mon, 20 Apr 2020 20:40:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="YvDlyCEz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725897AbgDTUk3 (ORCPT ); Mon, 20 Apr 2020 16:40:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725774AbgDTUk2 (ORCPT ); Mon, 20 Apr 2020 16:40:28 -0400 Received: from mail-lf1-x142.google.com (mail-lf1-x142.google.com [IPv6:2a00:1450:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43A19C061A0E for ; Mon, 20 Apr 2020 13:40:27 -0700 (PDT) Received: by mail-lf1-x142.google.com with SMTP id j14so9185687lfg.9 for ; Mon, 20 Apr 2020 13:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Dug/S9RxcVtJcdC7Ie/848Ex7Mkp2l4OqdGz3YQ9qH0=; b=YvDlyCEzKawDYecvvgnqs+hhm4igZHsu1HexOQADd4dtUiPk2XQVs7xoiDdA4Lby3r wmmC1bifHQR387A/BkUUO4AEBvdf2u9+xrCt4nHrhpwUcO8NTo66jW3rtHYfQ1xUOUwM sMTlwS5nfSsKZb7eKnd5+elHOMjfir16KuADks97WZhlpmm3KyNfF3pSGmfOPBbuWzqE oR6TTUE48GlFf4QBKvHOxdvW+IyXuuOZPER91r3sFFL4AgyafqASqagurh/oaUAzZbDJ e9SQhT2l3Myuml4Y2a4vhKFbG4gJADJwVTX4LlPLGCNpkJAq4QjWdvUE8LA0CVs6W3Xw p+FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Dug/S9RxcVtJcdC7Ie/848Ex7Mkp2l4OqdGz3YQ9qH0=; b=sm+EQNmlLucnlDjlZOJHY55PQ3x6K4YvgXuXSOzboiBSI4fnmwQ0/6kJzO8Bse/A/9 xnIAFj24i5IllmCc6md25k/GfT/xA11/Ipcru9lp8XjX+WgsaV71zIviPCBK2uZpr8tO 9iSV6WHjYGmrViiC+VhXEuX9rKHAl0Zb3tLzicipDGPGVVJLlfp1HwTM6KH8xsAfy/CB sL7af3Qfcth0fMz4Ml+/f4Z6rgDhoN7xwDRTl/v3z0iDVv0GCfy9CpLB7BbOim2BRm2i CyWfJip+EQtVG9//PAycnlvU44RZwsDghvgc+Bq+iDgnEa2obmmoDzDaZVoOPs99SSFa /GkQ== X-Gm-Message-State: AGi0PuYcquGog98N9pI9FIkN3ohf6FY5NBg8eqpSOCRVzbbccb9576wk KCoCmvevzhmSo3wswse9YGdB3w== X-Google-Smtp-Source: APiQypKAfSimA+/MIssEjbUzkMgrhs/yQHmvoV0pLp03d2obBPlE8h/TNmnUjsf4uAKA3mhFP3G2XA== X-Received: by 2002:ac2:5395:: with SMTP id g21mr11614880lfh.61.1587415225588; Mon, 20 Apr 2020 13:40:25 -0700 (PDT) Received: from localhost.bredbandsbolaget (c-f3d7225c.014-348-6c756e10.bbcust.telenor.se. [92.34.215.243]) by smtp.gmail.com with ESMTPSA id y22sm336637lfg.92.2020.04.20.13.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 13:40:24 -0700 (PDT) From: Linus Walleij To: Jonathan Cameron , linux-iio@vger.kernel.org Cc: Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-input@vger.kernel.org, Linus Walleij , Nick Reitemeyer , Stephan Gerhold , =?utf-8?b?TWljaGHFgiBNaXJvc8WCYXc=?= Subject: [PATCH 1/3 v4] iio: magnetometer: ak8974: Correct realbits Date: Mon, 20 Apr 2020 22:40:20 +0200 Message-Id: <20200420204022.18235-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.21.1 MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The HSCDTD008A has 15 bits of actual ADC valie, and the AMI305 and AMI306 has 12 bits. Correct this by providing an extra parameter to the channel macro and define a separate set of channels for each variant of the chip. The resolution is the actual resolution of the internal ADC of the chip. The values are stored in a S16 in 2's complement so all 16 bits are used for storing (no shifting needed). The AMI305, AMI306 and HSCDTD008A valid bits are picked from respective datasheet. My best educated guess is that AK8974 is also 12 bits. The AK8973 is an 8 bit and earlier version, and the sibling drivers AMI305 and AMI306 are 12 bits, so it makes sense to assume that the AK8974 is also 12 bits. Cc: Nick Reitemeyer Cc: Stephan Gerhold Cc: Michał Mirosław Signed-off-by: Linus Walleij --- ChangeLog v3->v4: - Create two sets of channels for 12 or 16 bit mode. ChangeLog v1->v3: - New patch in the v3 patch set. --- drivers/iio/magnetometer/ak8974.c | 45 +++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c index ade4ed8f67d2..7d585c3b7e1e 100644 --- a/drivers/iio/magnetometer/ak8974.c +++ b/drivers/iio/magnetometer/ak8974.c @@ -651,7 +651,7 @@ static const struct iio_chan_spec_ext_info ak8974_ext_info[] = { { }, }; -#define AK8974_AXIS_CHANNEL(axis, index) \ +#define AK8974_AXIS_CHANNEL(axis, index, bits) \ { \ .type = IIO_MAGN, \ .modified = 1, \ @@ -662,16 +662,32 @@ static const struct iio_chan_spec_ext_info ak8974_ext_info[] = { .scan_index = index, \ .scan_type = { \ .sign = 's', \ - .realbits = 16, \ + .realbits = bits, \ .storagebits = 16, \ .endianness = IIO_LE \ }, \ } -static const struct iio_chan_spec ak8974_channels[] = { - AK8974_AXIS_CHANNEL(X, 0), - AK8974_AXIS_CHANNEL(Y, 1), - AK8974_AXIS_CHANNEL(Z, 2), +/* + * We have no datasheet for the AK8974 but we guess that its + * ADC is 12 bits. The AMI305 and AMI306 certainly has 12bit + * ADC. + */ +static const struct iio_chan_spec ak8974_12_bits_channels[] = { + AK8974_AXIS_CHANNEL(X, 0, 12), + AK8974_AXIS_CHANNEL(Y, 1, 12), + AK8974_AXIS_CHANNEL(Z, 2, 12), + IIO_CHAN_SOFT_TIMESTAMP(3), +}; + +/* + * The HSCDTD008A has 15 bits resolution the way we set it up + * in CTRL4. + */ +static const struct iio_chan_spec ak8974_15_bits_channels[] = { + AK8974_AXIS_CHANNEL(X, 0, 15), + AK8974_AXIS_CHANNEL(Y, 1, 15), + AK8974_AXIS_CHANNEL(Z, 2, 15), IIO_CHAN_SOFT_TIMESTAMP(3), }; @@ -815,8 +831,21 @@ static int ak8974_probe(struct i2c_client *i2c, pm_runtime_put(&i2c->dev); indio_dev->dev.parent = &i2c->dev; - indio_dev->channels = ak8974_channels; - indio_dev->num_channels = ARRAY_SIZE(ak8974_channels); + switch (ak8974->variant) { + case AK8974_WHOAMI_VALUE_AMI306: + case AK8974_WHOAMI_VALUE_AMI305: + indio_dev->channels = ak8974_12_bits_channels; + indio_dev->num_channels = ARRAY_SIZE(ak8974_12_bits_channels); + break; + case AK8974_WHOAMI_VALUE_HSCDTD008A: + indio_dev->channels = ak8974_15_bits_channels; + indio_dev->num_channels = ARRAY_SIZE(ak8974_15_bits_channels); + break; + default: + indio_dev->channels = ak8974_12_bits_channels; + indio_dev->num_channels = ARRAY_SIZE(ak8974_12_bits_channels); + break; + } indio_dev->info = &ak8974_info; indio_dev->available_scan_masks = ak8974_scan_masks; indio_dev->modes = INDIO_DIRECT_MODE;