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: 11493045 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 981B614DD for ; Thu, 16 Apr 2020 14:09:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7CBAD20656 for ; Thu, 16 Apr 2020 14:09:46 +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 S2393229AbgDPOJm (ORCPT ); Thu, 16 Apr 2020 10:09:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2393138AbgDPOJW (ORCPT ); Thu, 16 Apr 2020 10:09:22 -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 76560C061A0F for ; Thu, 16 Apr 2020 07:09:21 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id r7so7916798ljg.13 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=EYslI2x/KRuXzZNU1jfmhhui+GnZc7hQYvw9ccOplijFThRcPnt196Bo5gkwPvOKpX gjLiXEIvMTraE+1uEYy+oMLWIbqvo9kVb1oWIuUnTfb9DyveiHGIDRuPG/0bRw2b2p6S zjTFRWj1EvZVexRR2JPYKc+9K3dPrBLscm7QARPaebjTW+k/S4GH0TR6WGaXxkeb1Ihe MzgFfDaM1auoPzkQ11J5JiUg6vL3yAh+xWKmJKjsHurpbTMNUBScwUsA8I5f7TjfzXbd oNDyyp6zwYWAGEFUZnIkClDrR1SzZ9N9Rcl5HHE2xLsryv5FogGoqLk4XoVODN9UOQwH YKhw== X-Gm-Message-State: AGi0PuYdOOInWwnpMdZV5LB+YYBbSak2+FejrFIBc97T/l8XfuBYEBGX 6yzcMLumL5LCmjh3plVcC0uWyQ== 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-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@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: 11493041 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 A67F0112C for ; Thu, 16 Apr 2020 14:09:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8C4CE206D6 for ; Thu, 16 Apr 2020 14:09:37 +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 S2393515AbgDPOJf (ORCPT ); Thu, 16 Apr 2020 10:09:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2393226AbgDPOJY (ORCPT ); Thu, 16 Apr 2020 10:09:24 -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 CBCDEC061A10 for ; Thu, 16 Apr 2020 07:09:23 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id z26so7907273ljz.11 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=rcXKMz8F2sq6LVm4buSxh/54Wrn085DNffWyfhIn3S3kI8H2Hc100ffmLx2Hb3XInb StTFS7L3pWFZdWadptgovEfPTQLz9ct5eqJlH6ByZ6iFIo/dtj0rQMXUzs39try6LGTj MdCfOk23BcF7ho4Fr9BDUcTjjT7UWLAp9P4Q7A7PWGKgV8lMy6QAIbgeRe5xMfkAFxyi wZx6l9QdVO1k8gwKf6KyUOuTAWsQWWRBjr2r7NnV65MXQLw5VHmx1q6AjDOAY95EdYo4 4jg7R7ohgAQM4H8gnD+CDh1kVeS0zndjZzexZavP9/mADTMyeNq+81xUviRrbhWYFBwX 9y9Q== X-Gm-Message-State: AGi0PuYVZkyf1m5ntvV84wqXrIzRJfDlVcyppAxK/0FfRY3XJUuFmbCu pqMbk322HDBYMKYJVd9ZKW9oAA== 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-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@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, \