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: 11499815 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C47B115AB for ; Mon, 20 Apr 2020 20:40:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AD122206E9 for ; Mon, 20 Apr 2020 20:40:29 +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 S1726006AbgDTUk3 (ORCPT ); Mon, 20 Apr 2020 16:40:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725897AbgDTUk2 (ORCPT ); Mon, 20 Apr 2020 16:40:28 -0400 Received: from mail-lf1-x143.google.com (mail-lf1-x143.google.com [IPv6:2a00:1450:4864:20::143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D84FC061A0C for ; Mon, 20 Apr 2020 13:40:27 -0700 (PDT) Received: by mail-lf1-x143.google.com with SMTP id k28so9194612lfe.10 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=ri15rAV9hna6RxH+K+b8UNmDe5AVj7eiFLqjTLdhD8P6eLMU2Vmb8/czlAYCbl2+yT BitepZM8fsLI0uKw1HWMWVQ3toUtBJtidPUb+XNcTFJCgv4AH07Ng6VHzJHzROl6lVY1 P0IScFuxKeoAFeWCQfk6+6grujpYB6xfT5RDVqQsjsoxr9oxTD5Li077LIYNAwZdABi9 V8RoqJbeuwY2j+KQtjst0808zyOF6CdfKhMKSuBsiEAWI2Wtkb7umLHcd/6fe+o1XWtk kU80CzLkpK+suDTCxN1LoK3rn2xldFrp/mrYZelEkvYNb8d95c2/RNGsE2xa2COsrYS5 aNKA== X-Gm-Message-State: AGi0PubvZI64Zz78IcMjiehHZdH4eAEBy9GG1U+tjH0+UXg9ktu1qTva cnZbQDlf4M78X7iRzt0D32GeHQ== 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-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@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; From patchwork Mon Apr 20 20:40:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 11499819 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0D3B8913 for ; Mon, 20 Apr 2020 20:40:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E81C520857 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="H16wjBCU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726089AbgDTUka (ORCPT ); Mon, 20 Apr 2020 16:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726055AbgDTUka (ORCPT ); Mon, 20 Apr 2020 16:40:30 -0400 Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C66D3C061A0E for ; Mon, 20 Apr 2020 13:40:29 -0700 (PDT) Received: by mail-lj1-x241.google.com with SMTP id n6so8211019ljg.12 for ; Mon, 20 Apr 2020 13:40:29 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=N8hZ33Ib2VrL63s5ftZNUenxJncMIHHQLe/XqYyzmzI=; b=H16wjBCUBkg/a/dNB/Ys7k9h2VUrW1iJOi+qUwh4Kfd3PbppfsCulmr/vXKHHemGBK S+Ri5Q8FD+VuJWHhUQ1bD7tYFFYZF4aEsde3S7/bnXle2Hhud2vlSl5+MZ6+1DvbrzKs qdH1taEtBw8Svr5lZYw98mPk5pcGe4ArKL/xxibfXwGq5BHW4KyarWGZBNiYjZz9HN7d qboTZ10gCS+ZXn7hcpphgyMYAv3+0UREY4tFAhmyZ1t8uwrDPiFgibvYaXKn+4aFdRdW SLMGPR/uL+oseyYHQspFiE5jAzrMpGxkvvOUUqjg6V7Pqv1RC1T+0uGLOMCHtsrylcUC YnDw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=N8hZ33Ib2VrL63s5ftZNUenxJncMIHHQLe/XqYyzmzI=; b=FD8iOIdUk1xBvkdcxl634oCZiiZghY0S5Pv+0OwXa+W82WjVJkPzN50mPnOOeYM43X a0+0dA8XNFm5eGLKbQMCV9dkcYMRa8tJWOkCKfJz96FO70iLEVS5Ewq9VqDBYgyH4iAU 0Xk0pt8jWfO/yhzute91TYZawQ/57xNtEsrX4JFA9IccjEMQuwCoF3SVYexiUqrlNHhS 828s6HI78ceBkD0gfxTv24RLKhXH9LTHP7a/dRyszgc083TVvqUoc0fzFtcubz1Rdb2n oQonYt2hei/y07yitpWARYOKEmHC3C907ekRpXABGANfkR2cpaEzYCKrrxNMNryOPyO1 pGmA== X-Gm-Message-State: AGi0PubYKkNvix8LrBdbp+77Wsyavten+whxslw5qrMP5SP3lCMV5oy+ qNgxMPghKMjSbtNLyOsFe0rzEw== X-Google-Smtp-Source: APiQypJG5gX8CWPFe3FcHCUFncJiQGKBMWb1Ts8VqTLrLqpnreTvQ6C57m4MdtCC0uNv5XzwgVggcA== X-Received: by 2002:a2e:740c:: with SMTP id p12mr10932112ljc.11.1587415228061; Mon, 20 Apr 2020 13:40:28 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 13:40:27 -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 2/3 v4] iio: magnetometer: ak8974: Break out measurement Date: Mon, 20 Apr 2020 22:40:21 +0200 Message-Id: <20200420204022.18235-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200420204022.18235-1-linus.walleij@linaro.org> References: <20200420204022.18235-1-linus.walleij@linaro.org> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This breaks out the measurement code to its own function so we can handle this without swirling it up with the big switch() statement inside ak8974_read_raw(). Keep a local s16 helper variable for the signed value coming out of the measurement before assigning it to the integer *val. The local variable makes the code easier to read and the compiler will optimize it if possible. Cc: Nick Reitemeyer Cc: Stephan Gerhold Reviewed-by: Michał Mirosław Signed-off-by: Linus Walleij --- ChangeLog v3->v4: - Assign the casted s16 to the int *val. - Collect Michał's review tag. ChangeLog v2->v3: - Return directly from the raw read function, we need no goto:s as we got rid of the lock. - Change the measurement function to return an int * after measurement and just pass *val to the function saving a local variable. - Insert a comment explaining the explicit cast to (s16). - Rename function as ak8974_measure_channel() so the name states exactly what is going on. - Break out as a separate patch. --- drivers/iio/magnetometer/ak8974.c | 66 +++++++++++++++++++------------ 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c index 7d585c3b7e1e..fb6d84b79a7a 100644 --- a/drivers/iio/magnetometer/ak8974.c +++ b/drivers/iio/magnetometer/ak8974.c @@ -554,47 +554,61 @@ static int ak8974_detect(struct ak8974 *ak8974) return 0; } +static int ak8974_measure_channel(struct ak8974 *ak8974, unsigned long address, + int *val) +{ + __le16 hw_values[3]; + int ret; + + pm_runtime_get_sync(&ak8974->i2c->dev); + mutex_lock(&ak8974->lock); + + /* + * We read all axes and discard all but one, for optimized + * reading, use the triggered buffer. + */ + ret = ak8974_trigmeas(ak8974); + if (ret) + goto out_unlock; + ret = ak8974_getresult(ak8974, hw_values); + if (ret) + goto out_unlock; + /* + * This explicit cast to (s16) is necessary as the measurement + * is done in 2's complement with positive and negative values. + * The follwing assignment to *val will then convert the signed + * s16 value to a signed int value. + */ + *val = (s16)le16_to_cpu(hw_values[address]); +out_unlock: + mutex_unlock(&ak8974->lock); + pm_runtime_mark_last_busy(&ak8974->i2c->dev); + pm_runtime_put_autosuspend(&ak8974->i2c->dev); + + return ret; +} + static int ak8974_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct ak8974 *ak8974 = iio_priv(indio_dev); - __le16 hw_values[3]; - int ret = -EINVAL; - - pm_runtime_get_sync(&ak8974->i2c->dev); - mutex_lock(&ak8974->lock); + int ret; switch (mask) { case IIO_CHAN_INFO_RAW: if (chan->address > 2) { dev_err(&ak8974->i2c->dev, "faulty channel address\n"); - ret = -EIO; - goto out_unlock; + return -EIO; } - ret = ak8974_trigmeas(ak8974); + ret = ak8974_measure_channel(ak8974, chan->address, val); if (ret) - goto out_unlock; - ret = ak8974_getresult(ak8974, hw_values); - if (ret) - goto out_unlock; - - /* - * We read all axes and discard all but one, for optimized - * reading, use the triggered buffer. - */ - *val = (s16)le16_to_cpu(hw_values[chan->address]); - - ret = IIO_VAL_INT; + return ret; + return IIO_VAL_INT; } - out_unlock: - mutex_unlock(&ak8974->lock); - pm_runtime_mark_last_busy(&ak8974->i2c->dev); - pm_runtime_put_autosuspend(&ak8974->i2c->dev); - - return ret; + return -EINVAL; } static void ak8974_fill_buffer(struct iio_dev *indio_dev) From patchwork Mon Apr 20 20:40:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 11499825 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1382F15AB for ; Mon, 20 Apr 2020 20:40:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EC80020857 for ; Mon, 20 Apr 2020 20:40:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="lL8jv7BB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726284AbgDTUkc (ORCPT ); Mon, 20 Apr 2020 16:40:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726100AbgDTUkb (ORCPT ); Mon, 20 Apr 2020 16:40:31 -0400 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 914A6C061A0E for ; Mon, 20 Apr 2020 13:40:31 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id z26so11556062ljz.11 for ; Mon, 20 Apr 2020 13:40:31 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=1+vHbHPDXWO2KyltN0oHVXvvCaxZJYvjk+daKL1jRQg=; b=lL8jv7BBJcgAWWxjqfmxHRuLc+LEc6KifjaVt0NvzrW9XZ67F7gG6qO2AjNiXbdFpu i5R3j01rDHt3z/mceb/zxyE+RTNmpew/NZi2IjxAOHV0JbfC36r2aMFXWEEJWkciZqGV 6evOJSpnQQTkJu3hq1hVYu2dqI67VFEkXMafccqLz8uOrTiJZFpek6kT9N3seaV3BiEB 1+WbQVJabXkGx2E3Fr+O0M3r/Joy40EdyD69kvmS673ENldpiVXJtc8TS1E6+Bo9qP87 cWbxyLGtrirnQNvBTp5v5A0OgvzMOR+yiLyDwIG+KBy07PAeDdmc5RNCrl2sR3QynQpc fzMA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=1+vHbHPDXWO2KyltN0oHVXvvCaxZJYvjk+daKL1jRQg=; b=Slt7mjhiyMDOoVZQ6ahkrES5c/2upXM+CJUCYl9vJdixmNcUI58iMocPDkzdQ2F2II PjWiwx2R+6TCMcGBwui+e0c/fM55uXrMRdlXRp23/TwuBtIqJagmEBjPWkZyNQOtouuZ iXQZPIJQ9yYIC0zpgOG7h7oB1p3znlN4oRP7eCOcolcvzHZyJB62wsWjlTGQZX441Tt3 Hm0OjrEJIWlPn/mkJssIKkT3hVyjjXEcqVB38fP1sc2nhm7gasEbEdSC+53UscyrYn/o H6zR88Yfjcatj/MkLhW1mB3OpA172RcaT3SQFxtGnH6AA3W5rdkdPSDFxB5fwFxcw6P7 4bPw== X-Gm-Message-State: AGi0PuYqCS5/VCLlJhisAzFcQvlZ/w2wBAA5CWKCxWd00Q1793N1fYvf 1dlgnTDIbj/xBB4obv/GvhcMvg== X-Google-Smtp-Source: APiQypI9l0uGHPWRg+ZpH8uWLlpt6agKj6OmqGdfFjnStlaksoKJ229ep147HVuklcXe+j8SZfp1xw== X-Received: by 2002:a2e:b0ca:: with SMTP id g10mr11485118ljl.169.1587415229894; Mon, 20 Apr 2020 13:40:29 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 13:40:29 -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 3/3 v4] iio: magnetometer: ak8974: Provide scaling Date: Mon, 20 Apr 2020 22:40:22 +0200 Message-Id: <20200420204022.18235-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200420204022.18235-1-linus.walleij@linaro.org> References: <20200420204022.18235-1-linus.walleij@linaro.org> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The manual for the HSCDTD008A gives us a scaling for the three axis as +/- 2.4mT (24 Gauss) per axis. The manual for the AMI305 and AMI306 gives us a scaling for the three axis as +/- 12 Gauss per axis. Tests with the HSCDTD008A sensor, cat the raw values: $ cat in_magn_*_raw raw 45 189 -19 The scaling factor in in_magn_*_scale is 0.001464843, which gives: 0.065 Gauss 0.277 Gauss -0.027 Gauss The earths magnetic field is in the range of 0.25 to 0.65 Gauss on the surface according to Wikipedia, so these seem like reasonable values. Again we are guessing that the AK8974 has a 12 bit ADC, based on the similarity with AMI305 and AMI306. Cc: Nick Reitemeyer Cc: Stephan Gerhold Cc: Michał Mirosław Signed-off-by: Linus Walleij --- ChangeLog v3->v4: - Do not multiply the range with 2, instead divide by two in divisor and dividend. (Thus it becomes 2^11 and 2^14 respectively.) - Use the IIO_VAL_FRACTIONAL_LOG2 trick and pass 2^11 or 2^14 as range. ChangeLog v2->v3: - Scale the 2.4mT/24Gauss to 15 bits for the HSCDTD008A. - Scale the 12 Gauss to 12 bits for the AMI305/AMI306 - Use 12 bits for the other variants. - Return directly in the raw read function. ChangeLog v1->v2: - Split out the measurement refactoring. --- drivers/iio/magnetometer/ak8974.c | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c index fb6d84b79a7a..2d500be9da40 100644 --- a/drivers/iio/magnetometer/ak8974.c +++ b/drivers/iio/magnetometer/ak8974.c @@ -606,6 +606,48 @@ static int ak8974_read_raw(struct iio_dev *indio_dev, if (ret) return ret; return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + switch (ak8974->variant) { + case AK8974_WHOAMI_VALUE_AMI306: + case AK8974_WHOAMI_VALUE_AMI305: + /* + * The datasheet for AMI305 and AMI306, page 6 + * specifies the range of the sensor to be + * +/- 12 Gauss. + */ + *val = 12; + /* + * 12 bits are used, +/- 2^11 + * [ -2048 .. 2047 ] (manual page 20) + * [ 0xf800 .. 0x07ff ] + */ + *val2 = 11; + return IIO_VAL_FRACTIONAL_LOG2; + case AK8974_WHOAMI_VALUE_HSCDTD008A: + /* + * The datasheet for HSCDTF008A, page 3 specifies the + * range of the sensor as +/- 2.4 mT per axis, which + * corresponds to +/- 2400 uT = +/- 24 Gauss. + */ + *val = 24; + /* + * 15 bits are used (set up in CTRL4), +/- 2^14 + * [ -16384 .. 16383 ] (manual page 24) + * [ 0xc000 .. 0x3fff ] + */ + *val2 = 14; + return IIO_VAL_FRACTIONAL_LOG2; + default: + /* GUESSING +/- 12 Gauss */ + *val = 12; + /* GUESSING 12 bits ADC +/- 2^11 */ + *val2 = 11; + return IIO_VAL_FRACTIONAL_LOG2; + } + break; + default: + /* Unknown request */ + break; } return -EINVAL; @@ -670,7 +712,8 @@ static const struct iio_chan_spec_ext_info ak8974_ext_info[] = { .type = IIO_MAGN, \ .modified = 1, \ .channel2 = IIO_MOD_##axis, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ .ext_info = ak8974_ext_info, \ .address = index, \ .scan_index = index, \