From patchwork Thu Apr 16 14:09:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 11493047 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 5680B14DD for ; Thu, 16 Apr 2020 14:10:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A8D92076D for ; Thu, 16 Apr 2020 14:10:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="GN52HuCO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729329AbgDPOKU (ORCPT ); Thu, 16 Apr 2020 10:10:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2393116AbgDPOJV (ORCPT ); Thu, 16 Apr 2020 10:09:21 -0400 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6191EC061A0C for ; Thu, 16 Apr 2020 07:09:21 -0700 (PDT) Received: by mail-lj1-x243.google.com with SMTP id v9so7910962ljk.12 for ; Thu, 16 Apr 2020 07:09:21 -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=EcwerKQQEP8j7nclGhrMgaoQqJ2z0i0teVUv4cSWNXE=; b=GN52HuCOuJ1SswJKMaB3uMoT9Eb7waHxo5fPIzcXanb+uCFUuFV9TkhM4PjXVBZGM4 X9GP/WnyyF12vzcnEFCYv7gJiciOItxiplPtQ813fPjhHClqYVqTvgWqTEjiVLTU1bms Og8XEEhsp2X6ua24ARy8T8XZfkqUsljn7IRGAqPB2qSXuTLw19sluh/ley6xJnCCtHMa 5n+QFWr4zT6kwQ63EfIZqBR2gzX30JHDRng2AYTzLQ4DWUxX1hK3yE+MV4XlG4P+ZGIY Etp9hrQ43oR1+uOnq0nhUh/Sh5WqYlC1+h1IeKX4VIbxG9eqaoREpo81upE/SDpDDH+l DiRQ== 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=EcwerKQQEP8j7nclGhrMgaoQqJ2z0i0teVUv4cSWNXE=; b=GIx1pwBGJoE5eWU45/KUGAFNS/r9fFfCnDzu5722rZQW0ePUdUDutBuVFAhnWdA2FK pL6iidkjkTyVRWtLkHd6YBgf05S1wbzxpQQybZ/H6jRAuH1xQgGPVgfq8fJvnREPIExK C5TV22xsNjy0vr8Z8LJsFl5l+WJCvEA9TLpN0otvCsRxPpeq71gAmkVFgh5cpJoGV8zw GASjSt21pW+ukiqchdSmOb3hy/yjIHC3tnaXNogC4Olw8Ehzxr5zELuV+DRNj2p/ZiU4 xZUmQ/WGgLgdiEzCB9tME4ze3SKknlwn3uF9kl5fpTuE26et7y3uG0xMC/Vt3oCsRDcl b5Rw== X-Gm-Message-State: AGi0PubO9xrcJUhoHQk4dRdEp0jAxWl0iQhdRAxoIEFoba7v2SVjBSPf zWOkBAozOwqWuZvvshpS/TcdCA== X-Google-Smtp-Source: APiQypJcSV0LCNGO0pS1gyWwCMCoYh6wY2j+JWPGs+yObCYqdjzVp901POV40kM+hMkZGLw4VxCyCQ== X-Received: by 2002:a05:651c:39b:: with SMTP id e27mr6962616ljp.45.1587046159829; Thu, 16 Apr 2020 07:09:19 -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 v12sm9744045ljd.85.2020.04.16.07.09.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 07:09:19 -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/2 v2] iio: magnetometer: ak8974: Break out measurement Date: Thu, 16 Apr 2020 16:09:16 +0200 Message-Id: <20200416140917.8087-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 This breaks out the measurement code to its own function so we can handle this without swirling it up with the bis switch() statement inside ak8974_read_raw(). Use an intermediary s16* variable since we read s16 but the external API required an int* so this way we get explicit casting. Cc: Nick Reitemeyer Cc: Stephan Gerhold Cc: Michał Mirosław Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - Break out as a separate patch. --- drivers/iio/magnetometer/ak8974.c | 51 +++++++++++++++++++------------ 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c index ade4ed8f67d2..5361647b9054 100644 --- a/drivers/iio/magnetometer/ak8974.c +++ b/drivers/iio/magnetometer/ak8974.c @@ -554,46 +554,57 @@ static int ak8974_detect(struct ak8974 *ak8974) return 0; } +static int ak8974_measure(struct ak8974 *ak8974, unsigned long address, s16 *val) +{ + __le16 hw_values[3]; + int ret; + + pm_runtime_get_sync(&ak8974->i2c->dev); + mutex_lock(&ak8974->lock); + + ret = ak8974_trigmeas(ak8974); + if (ret) + goto out_unlock; + ret = ak8974_getresult(ak8974, hw_values); + if (ret) + goto out_unlock; + *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); + s16 outval; 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; + goto out_err_read; } - ret = ak8974_trigmeas(ak8974); - 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 = ak8974_measure(ak8974, chan->address, &outval); + if (ret) + goto out_err_read; + *val = outval; ret = IIO_VAL_INT; + break; } - - out_unlock: - mutex_unlock(&ak8974->lock); - pm_runtime_mark_last_busy(&ak8974->i2c->dev); - pm_runtime_put_autosuspend(&ak8974->i2c->dev); - +out_err_read: return ret; } From patchwork Thu Apr 16 14:09:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 11493043 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 2598314DD for ; Thu, 16 Apr 2020 14:09:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0A7E2206D6 for ; Thu, 16 Apr 2020 14:09:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="wft9es95" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393526AbgDPOJl (ORCPT ); Thu, 16 Apr 2020 10:09:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2393229AbgDPOJY (ORCPT ); Thu, 16 Apr 2020 10:09:24 -0400 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCCAEC061A41 for ; Thu, 16 Apr 2020 07:09:23 -0700 (PDT) Received: by mail-lj1-x244.google.com with SMTP id m8so8013545lji.1 for ; Thu, 16 Apr 2020 07:09:23 -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=2cSkXHmvUNwPdGeVkj60CCBpcqm/zp0JNhGGEDa8e6c=; b=wft9es95d/UBAGc/gHlKWquJct6ZDPlxhqz8XVNlsJgUyYYFv5npGT+B8y/gb2HGy0 aArvOQ8QLLihF6z+o9Mr1VbiSkeCZCvx6YPqd4Fx+SlR21QJ1yIJ71LoUwwiJceqk7am Eh1Vg5VKyxYOU3DuyC2r12+snGkI1UB9FWjWaONhVSHoNV5VcxFDD03Q6vN/XdkXAp8n PNrbIkJCbO4h6tyu5T85Ga3rUXQI5+nV5gMZh8g4u++1ST/Va2e9SGpuXWxP0i4KXXBi 0KrveZGh2BzHhlx+MWOPz1dSOLDlippk34ejEelLvwpPmR3ZhlkoejoKr4wsTYzgWZWv W7nA== 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=2cSkXHmvUNwPdGeVkj60CCBpcqm/zp0JNhGGEDa8e6c=; b=l0tikINNEMNU9KNWp9DmbYM9Lx6/ue5jc8MdpWOay4uh0OnjqEd5Ztb/ArpP3XIxFd M0wHcFhD5xSjgCbPlIqioq9fYVfypQpp3s9tB6iAtwy2bC48N6/eEmTfQ/7+XNuA0lLU /O8pZ/ywTtzJXVla4ljqNhGz0QcSXfwjPMv7XmsHxJ3lhKULhC4CthKHsIVa20Q08Dw4 XQn/lE+52m249HclzKURrMH41PD3kmHj5SS5fRDDr3SxMDGGoaaXxJDJFB1XnpkkrxRx nMUn67W9YxJDwCYgh3osn2aatPN76edCSag/1g3mfbCDQbK1HLfsD1fxuYu8G67gnFJW yJcA== X-Gm-Message-State: AGi0Puah3U3IIIxSiFdvJOwwXcZNCmMNjj3E3T+85ghhKOAsuoJ2LAJm aem5pix7FnDXsHDx+pDskVV9qg== X-Google-Smtp-Source: APiQypIZv2NDJQmoyhSvQfPxfh+Eajh9UXVOuik6AHjDW0mBW1kGCsTWSVi6bzeVb0uVVVgXNP/Avw== X-Received: by 2002:a2e:3a0a:: with SMTP id h10mr6137225lja.54.1587046162057; Thu, 16 Apr 2020 07:09:22 -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 v12sm9744045ljd.85.2020.04.16.07.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 07:09:21 -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/2 v2] iio: magnetometer: ak8974: Provide scaling Date: Thu, 16 Apr 2020 16:09:17 +0200 Message-Id: <20200416140917.8087-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200416140917.8087-1-linus.walleij@linaro.org> References: <20200416140917.8087-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 per axis. When I implement this the biggest axis indicates 0.59 Gauss which is a reasonable measurement for the earths magnetic which is in the range of 0.25 to 0.65 Gauss on the surface according to Wikipedia. Cc: Nick Reitemeyer Cc: Stephan Gerhold Cc: Michał Mirosław Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - Split out the measurement refactoring. --- drivers/iio/magnetometer/ak8974.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c index 5361647b9054..effcdd93e650 100644 --- a/drivers/iio/magnetometer/ak8974.c +++ b/drivers/iio/magnetometer/ak8974.c @@ -603,6 +603,18 @@ static int ak8974_read_raw(struct iio_dev *indio_dev, *val = outval; ret = IIO_VAL_INT; break; + case IIO_CHAN_INFO_SCALE: + /* + * 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. So 0x7fff is 24 Gauss and + * 0xffff is -24 Gauss. To account for the one missing value if + * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX. + */ + *val = 24 * 2; + *val2 = U16_MAX; + ret = IIO_VAL_FRACTIONAL; + break; } out_err_read: return ret; @@ -667,7 +679,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, \