From patchwork Sat Aug 8 12:10:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crt Mori X-Patchwork-Id: 11706303 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 98B3A722 for ; Sat, 8 Aug 2020 12:13:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8247A20866 for ; Sat, 8 Aug 2020 12:13:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=melexis.com header.i=@melexis.com header.b="LyblSclQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726291AbgHHMNc (ORCPT ); Sat, 8 Aug 2020 08:13:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726359AbgHHMLG (ORCPT ); Sat, 8 Aug 2020 08:11:06 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF85C061A27 for ; Sat, 8 Aug 2020 05:11:01 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a26so4771442ejc.2 for ; Sat, 08 Aug 2020 05:11:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AIzoyOqsmvq2UL6ht5EBrZhZITvwdcSbBz+FO/uaKCQ=; b=LyblSclQpF5crcdFVVNLh5GR6t/10gHDiCpaCcye5I3bANIIXnNhx/kjSO0QghXDNI RyrUoJ8DjDoQ5IzPKKIJB1nKQZnFFF2BUBsq9fRVXQsbBUfOOT2W5Bv0k+ryGtun9ffZ UuAchgJNtd1ZpQ531U1tArWf+URhm+u75sG5ME+qzmc0GNvfmd+Uty8mFheRqkuji7eP 75A6QdPvTI7+m5ykdZpLrh0Jhx+Mt8zhuwjKx/Zt0xoa7ox2uGg+v3yary1dJpqY/xgD ZAX/jmYHZaiTqw/iyQ/lzkwPUY58PrHmy+rF3U3ZtvwQ4+XQNcI34zakwcQIWxSzBg6k 1uEg== 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=AIzoyOqsmvq2UL6ht5EBrZhZITvwdcSbBz+FO/uaKCQ=; b=lkyxfnNbtB68+pj262SjecDWaU0J4BnZKL4ID0++97OgoWeAqkft3f5vfXVYzk1Coz C5gxzx/NPB9cEE51M8aP0V7pkvvumEnxplTIIRbqEYWiXX2mNL+nNG9sWJAGFGbhkMuO Hwz5aBY0WG968/3cbBMEGZHzrBEh3BtsEnQvMzV28hPp5fw5pcP935FeUNvCMzEUzHR3 y4gtJzLB/R8/oogDIfv1uwp34Qjz5kbYi2mtInPhJBc3jmi1gJzOYT1EhFV3BfvcVO5l HYUCrwjXDd/Dj+7Lfnmn2QlbA4xNbSXZndGOXl4KYdbrAdU0y/A/8Bkk2KJykcgHV4KD PHOA== X-Gm-Message-State: AOAM530fhL2qgYy9qU1VpD6eIapop9MRguAxBxSNltFKX2FcliwlfIv7 MNTtVCJ8ozSVG9kuGmFu99xo1X/CNgCUnA== X-Google-Smtp-Source: ABdhPJwlFvwjxLUDV8XeezUFPyAzMz7NFUXWNsDc9b6F2mY+eT4xy28+Bn61wngvBNrtD8502sM11A== X-Received: by 2002:a17:906:7104:: with SMTP id x4mr14336172ejj.417.1596888658132; Sat, 08 Aug 2020 05:10:58 -0700 (PDT) Received: from localhost.localdomain (ptr-4xajgywt9p1tstfsdfc.18120a2.ip6.access.telenet.be. [2a02:1810:a421:dd00:b0e6:1cd9:30c1:35f8]) by smtp.gmail.com with ESMTPSA id b2sm8130833ejg.70.2020.08.08.05.10.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 05:10:57 -0700 (PDT) From: Crt Mori To: Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Crt Mori Subject: [PATCH v4 1/4] iio:temperature:mlx90632: Reduce number of equal calulcations Date: Sat, 8 Aug 2020 14:10:23 +0200 Message-Id: <20200808121026.1300375-2-cmo@melexis.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200808121026.1300375-1-cmo@melexis.com> References: <20200808121026.1300375-1-cmo@melexis.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org TAdut4 was calculated each iteration although it did not change. In light of near future additions of the Extended range DSP calculations, this function refactoring will help reduce unrelated changes in that series as well as reduce the number of new functions needed. Signed-off-by: Crt Mori Reviewed-by: Andy Shevchenko --- drivers/iio/temperature/mlx90632.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/iio/temperature/mlx90632.c b/drivers/iio/temperature/mlx90632.c index 51b812bcff2e..c3de10ba5b1e 100644 --- a/drivers/iio/temperature/mlx90632.c +++ b/drivers/iio/temperature/mlx90632.c @@ -374,11 +374,11 @@ static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw, } static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object, - s64 TAdut, s32 Fa, s32 Fb, + s64 TAdut, s64 TAdut4, s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb, u16 emissivity) { - s64 calcedKsTO, calcedKsTA, ir_Alpha, TAdut4, Alpha_corr; + s64 calcedKsTO, calcedKsTA, ir_Alpha, Alpha_corr; s64 Ha_customer, Hb_customer; Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL; @@ -393,30 +393,35 @@ static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object, Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL); Alpha_corr = div64_s64(Alpha_corr, 1000LL); ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr); - TAdut4 = (div64_s64(TAdut, 10000LL) + 27315) * - (div64_s64(TAdut, 10000LL) + 27315) * - (div64_s64(TAdut, 10000LL) + 27315) * - (div64_s64(TAdut, 10000LL) + 27315); return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4)) - 27315 - Hb_customer) * 10; } +static s64 mlx90632_calc_ta4(s64 TAdut, s64 scale) +{ + return (div64_s64(TAdut, scale) + 27315) * + (div64_s64(TAdut, scale) + 27315) * + (div64_s64(TAdut, scale) + 27315) * + (div64_s64(TAdut, scale) + 27315); +} + static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb, s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb, u16 tmp_emi) { - s64 kTA, kTA0, TAdut; + s64 kTA, kTA0, TAdut, TAdut4; s64 temp = 25000; s8 i; kTA = (Ea * 1000LL) >> 16LL; kTA0 = (Eb * 1000LL) >> 8LL; TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL; + TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); /* Iterations of calculation as described in datasheet */ for (i = 0; i < 5; ++i) { - temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, + temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TAdut4, Fa, Fb, Ga, Ha, Hb, tmp_emi); } From patchwork Sat Aug 8 12:10:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crt Mori X-Patchwork-Id: 11706299 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 E6DF8722 for ; Sat, 8 Aug 2020 12:13:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5D8520866 for ; Sat, 8 Aug 2020 12:13:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=melexis.com header.i=@melexis.com header.b="qSTJGsFj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726401AbgHHMMx (ORCPT ); Sat, 8 Aug 2020 08:12:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726350AbgHHMLG (ORCPT ); Sat, 8 Aug 2020 08:11:06 -0400 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE78FC061A2A for ; Sat, 8 Aug 2020 05:11:02 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id m22so4744678eje.10 for ; Sat, 08 Aug 2020 05:11:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JUzxxhRQc9SmVWOtPb2Ta23avgrxjGDXDwpA5n9imCM=; b=qSTJGsFjvPtgxPINxJ910s0N5z3OFb4iAJ8U7T02s0dzmBdu6PIMh39AvnEQp9W/lv mhbjg1qFFJMgf772ZYBkqGu3x8Y5cgzD5aHGmX9KXahKv4ebUzNF6QaOBx0fOvJb3UiX jeeQ02kvBESvd2INqXArO57g2w0W8QiiL+XyBhwgTnQscP0JjQHbxizDXCzwlE0kSxN3 ERQl45Mxy9uqZNsdSVMNOwnhY07csCztc8inT+CISMCcHiiJaFVYcCbMkjRFVgyX7YsK pcmoqjjU1Z1iwG3VXUpKsm/S5epYjJMeiyS1b9oIsDu1tCZ5NS6CjCcjG6EY/3LzM/Oj EMIQ== 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=JUzxxhRQc9SmVWOtPb2Ta23avgrxjGDXDwpA5n9imCM=; b=IHSG52oBOoOvS70k+VdPA3hKwCxhm1Cd5dyYFkqAeyWHUwI/yPjnKJ8sv1YRqSwnBV /iLSlyUa1omx/cmMKHIG/Wstu3hubmADcGM3lGo1jY7OVkxJXmqRsPo71H/l/bPMwBmC k/QMYwZRP+vKPHZTnT0WgU7BlQEW/up+wGxHFe4ofyi2Tzd4JEWAD01/2ck0Jk8Fo4G7 MLxVC+JKoc2Vhq2S0BWjTtumoSoauqEtkjZgLmK1WHZQgeh3/aaeRgwopiBhgCsJNsNZ TB1UziO5WwysW5yImyGwsJhCUrHHex3BDGIUzepPJaFF3Pbbu3doSjv39B3Fn0HFdrWU LrUQ== X-Gm-Message-State: AOAM533YKT2CpcwojSzEYoQFC65Ukoza/aBBSL6tK9+kJBOfvVAzVoi2 wLy9RRK/bq/G/P82hroIp+u67g== X-Google-Smtp-Source: ABdhPJwGuq5y8Z2w9pW9iDBF0sIEhf4Rl/GtP35Krx/UwvgmRbAE2EFZFOas9PSh1nNxP7nxPicQEA== X-Received: by 2002:a17:906:1105:: with SMTP id h5mr13584531eja.307.1596888661428; Sat, 08 Aug 2020 05:11:01 -0700 (PDT) Received: from localhost.localdomain (ptr-4xajgywt9p1tstfsdfc.18120a2.ip6.access.telenet.be. [2a02:1810:a421:dd00:b0e6:1cd9:30c1:35f8]) by smtp.gmail.com with ESMTPSA id b2sm8130833ejg.70.2020.08.08.05.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 05:11:00 -0700 (PDT) From: Crt Mori To: Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Crt Mori Subject: [PATCH v4 2/4] iio:temperature:mlx90632: Adding extended calibration option Date: Sat, 8 Aug 2020 14:10:24 +0200 Message-Id: <20200808121026.1300375-3-cmo@melexis.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200808121026.1300375-1-cmo@melexis.com> References: <20200808121026.1300375-1-cmo@melexis.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org For some time the market wants medical grade accuracy in medical range, while still retaining the declared accuracy outside of the medical range within the same sensor. That is why we created extended calibration which is automatically switched to when object temperature is too high. This patch also introduces the object_ambient_temperature variable which is needed for more accurate calculation of the object infra-red footprint as sensor's ambient temperature might be totally different than what the ambient temperature is at object and that is why we can have some more errors which can be eliminated. Currently this temperature is fixed at 25, but the interface to adjust it by user (with external sensor or just IR measurement of the other object which acts as ambient), will be introduced in another commit. Signed-off-by: Crt Mori --- drivers/iio/temperature/mlx90632.c | 212 ++++++++++++++++++++++++++++- 1 file changed, 210 insertions(+), 2 deletions(-) diff --git a/drivers/iio/temperature/mlx90632.c b/drivers/iio/temperature/mlx90632.c index c3de10ba5b1e..049c1217a166 100644 --- a/drivers/iio/temperature/mlx90632.c +++ b/drivers/iio/temperature/mlx90632.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,8 @@ /* Control register address - volatile */ #define MLX90632_REG_CONTROL 0x3001 /* Control Register address */ #define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */ +#define MLX90632_CFG_MTYP_MASK GENMASK(8, 4) /* Meas select Mask */ + /* PowerModes statuses */ #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1) #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */ @@ -65,6 +68,18 @@ #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */ #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous*/ +/* Measurement types */ +#define MLX90632_MTYP_MEDICAL 0 +#define MLX90632_MTYP_EXTENDED 17 + +/* Measurement type select*/ +#define MLX90632_MTYP_STATUS(ctrl_val) (ctrl_val << 4) +#define MLX90632_MTYP_STATUS_MEDICAL MLX90632_MTYP_STATUS(MLX90632_MTYP_MEDICAL) +#define MLX90632_MTYP_STATUS_EXTENDED MLX90632_MTYP_STATUS(MLX90632_MTYP_EXTENDED) + +/* I2C command register - volatile */ +#define MLX90632_REG_I2C_CMD 0x3005 /* I2C command Register address */ + /* Device status register - volatile */ #define MLX90632_REG_STATUS 0x3fff /* Device status register */ #define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */ @@ -81,6 +96,8 @@ /* Magic constants */ #define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */ #define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */ +#define MLX90632_ID_EXTENDED 0x0505 /* EEPROM DSPv5 Extended range device id */ +#define MLX90632_ID_MASK GENMASK(14, 0) /* DSP version and device ID in EE_VERSION */ #define MLX90632_DSP_VERSION 5 /* DSP version */ #define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */ #define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */ @@ -88,16 +105,20 @@ #define MLX90632_REF_3 12LL /**< ResCtrlRef value of Channel 3 */ #define MLX90632_MAX_MEAS_NUM 31 /**< Maximum measurements in list */ #define MLX90632_SLEEP_DELAY_MS 3000 /**< Autosleep delay */ +#define MLX90632_EXTENDED_LIMIT 27000 /* Extended mode raw value limit */ struct mlx90632_data { struct i2c_client *client; struct mutex lock; /* Multiple reads for single measurement */ struct regmap *regmap; u16 emissivity; + u8 mtyp; /* measurement type - to enable extended range calculations */ + u32 object_ambient_temperature; }; static const struct regmap_range mlx90632_volatile_reg_range[] = { regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), + regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), regmap_reg_range(MLX90632_RAM_1(0), MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), @@ -113,6 +134,7 @@ static const struct regmap_range mlx90632_read_reg_range[] = { regmap_reg_range(MLX90632_EE_CTRL, MLX90632_EE_I2C_ADDR), regmap_reg_range(MLX90632_EE_Ha, MLX90632_EE_Hb), regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), + regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), regmap_reg_range(MLX90632_RAM_1(0), MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), @@ -199,6 +221,26 @@ static int mlx90632_perform_measurement(struct mlx90632_data *data) return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2; } +static int mlx90632_set_meas_type(struct regmap *regmap, u8 type) +{ + int ret; + + if ((type != MLX90632_MTYP_MEDICAL) && (type != MLX90632_MTYP_EXTENDED)) + return -EINVAL; + + ret = regmap_write(regmap, MLX90632_REG_I2C_CMD, MLX90632_RESET_CMD); + if (ret < 0) + return ret; + + ret = regmap_write_bits(regmap, MLX90632_REG_CONTROL, + (MLX90632_CFG_MTYP_MASK | MLX90632_CFG_PWR_MASK), + (MLX90632_MTYP_STATUS(type) | MLX90632_PWR_STATUS_HALT)); + if (ret < 0) + return ret; + + return mlx90632_pwr_continuous(regmap); +} + static int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new, uint8_t *channel_old) { @@ -300,6 +342,106 @@ static int mlx90632_read_all_channel(struct mlx90632_data *data, return ret; } +static int mlx90632_read_ambient_raw_extended(struct regmap *regmap, + s16 *ambient_new_raw, s16 *ambient_old_raw) +{ + unsigned int read_tmp; + int ret; + + ret = regmap_read(regmap, MLX90632_RAM_3(17), &read_tmp); + if (ret < 0) + return ret; + *ambient_new_raw = (s16)read_tmp; + + ret = regmap_read(regmap, MLX90632_RAM_3(18), &read_tmp); + if (ret < 0) + return ret; + *ambient_old_raw = (s16)read_tmp; + + return 0; +} + +static int mlx90632_read_object_raw_extended(struct regmap *regmap, s16 *object_new_raw) +{ + unsigned int read_tmp; + s32 read; + int ret; + + ret = regmap_read(regmap, MLX90632_RAM_1(17), &read_tmp); + if (ret < 0) + return ret; + read = (s16)read_tmp; + + ret = regmap_read(regmap, MLX90632_RAM_2(17), &read_tmp); + if (ret < 0) + return ret; + read = read - (s16)read_tmp; + + ret = regmap_read(regmap, MLX90632_RAM_1(18), &read_tmp); + if (ret < 0) + return ret; + read = read - (s16)read_tmp; + + ret = regmap_read(regmap, MLX90632_RAM_2(18), &read_tmp); + if (ret < 0) + return ret; + read = (read + (s16)read_tmp) / 2; + + ret = regmap_read(regmap, MLX90632_RAM_1(19), &read_tmp); + if (ret < 0) + return ret; + read = read + (s16)read_tmp; + + ret = regmap_read(regmap, MLX90632_RAM_2(19), &read_tmp); + if (ret < 0) + return ret; + read = read + (s16)read_tmp; + + if (read > S16_MAX || read < S16_MIN) + return -ERANGE; + + *object_new_raw = read; + + return 0; +} + +static int mlx90632_read_all_channel_extended(struct mlx90632_data *data, s16 *object_new_raw, + s16 *ambient_new_raw, s16 *ambient_old_raw) +{ + int tries = 4; + int ret; + + mutex_lock(&data->lock); + ret = mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_EXTENDED); + if (ret < 0) + goto read_unlock; + + while (tries-- > 0) { + ret = mlx90632_perform_measurement(data); + if (ret < 0) + goto read_unlock; + + if (ret == 19) + break; + } + if (tries < 0) { + ret = -ETIMEDOUT; + goto read_unlock; + } + + ret = mlx90632_read_object_raw_extended(data->regmap, object_new_raw); + if (ret < 0) + goto read_unlock; + + ret = mlx90632_read_ambient_raw_extended(data->regmap, ambient_new_raw, ambient_old_raw); + +read_unlock: + (void) mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_MEDICAL); + + mutex_unlock(&data->lock); + return ret; +} + static int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb, s32 *reg_value) { @@ -354,9 +496,23 @@ static s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw, return div64_s64((tmp << 19ULL), 1000LL); } +static s64 mlx90632_preprocess_temp_obj_extended(s16 object_new_raw, s16 ambient_new_raw, + s16 ambient_old_raw, s16 Ka) +{ + s64 VR_IR, kKa, tmp; + + kKa = ((s64)Ka * 1000LL) >> 10ULL; + VR_IR = (s64)ambient_old_raw * 1000000LL + + kKa * div64_s64((s64)ambient_new_raw * 1000LL, + MLX90632_REF_3); + tmp = div64_s64( + div64_s64((s64) object_new_raw * 1000000000000LL, MLX90632_REF_12), + VR_IR); + return div64_s64(tmp << 19ULL, 1000LL); +} + static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw, - s32 P_T, s32 P_R, s32 P_G, s32 P_O, - s16 Gb) + s32 P_T, s32 P_R, s32 P_G, s32 P_O, s16 Gb) { s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum; @@ -428,6 +584,31 @@ static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb, return temp; } +static s32 mlx90632_calc_temp_object_extended(s64 object, s64 ambient, s64 reflected, + s32 Ea, s32 Eb, s32 Fa, s32 Fb, s32 Ga, + s16 Ha, s16 Hb, u16 tmp_emi) +{ + s64 kTA, kTA0, TAdut, TAdut4, Tr4, TaTr4; + s64 temp = 25000; + s8 i; + + kTA = (Ea * 1000LL) >> 16LL; + kTA0 = (Eb * 1000LL) >> 8LL; + TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL; + Tr4 = mlx90632_calc_ta4(reflected, 10); + TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); + TaTr4 = Tr4 - div64_s64(Tr4 - TAdut4, tmp_emi) * 1000; + + /* Iterations of calculation as described in datasheet */ + for (i = 0; i < 5; ++i) { + temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TaTr4, + Fa / 2, Fb, Ga, Ha, Hb, + tmp_emi); + } + + return temp; +} + static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val) { s32 ret; @@ -475,6 +656,26 @@ static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val) if (ret < 0) return ret; + if (object_new_raw > MLX90632_EXTENDED_LIMIT && + data->mtyp == MLX90632_MTYP_EXTENDED) { + ret = mlx90632_read_all_channel_extended(data, &object_new_raw, + &ambient_new_raw, &ambient_old_raw); + if (ret < 0) + return ret; + + /* Use extended mode calculations */ + ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, + ambient_old_raw, Gb); + object = mlx90632_preprocess_temp_obj_extended(object_new_raw, + ambient_new_raw, + ambient_old_raw, Ka); + *val = mlx90632_calc_temp_object_extended(object, ambient, + data->object_ambient_temperature, + Ea, Eb, Fa, Fb, Ga, + Ha, Hb, data->emissivity); + return 0; + } + ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw, Gb); object = mlx90632_preprocess_temp_obj(object_new_raw, @@ -648,6 +849,7 @@ static int mlx90632_probe(struct i2c_client *client, i2c_set_clientdata(client, indio_dev); mlx90632->client = client; mlx90632->regmap = regmap; + mlx90632->mtyp = MLX90632_MTYP_MEDICAL; mutex_init(&mlx90632->lock); indio_dev->name = id->name; @@ -667,12 +869,17 @@ static int mlx90632_probe(struct i2c_client *client, dev_err(&client->dev, "read of version failed: %d\n", ret); return ret; } + read = read & MLX90632_ID_MASK; if (read == MLX90632_ID_MEDICAL) { dev_dbg(&client->dev, "Detected Medical EEPROM calibration %x\n", read); } else if (read == MLX90632_ID_CONSUMER) { dev_dbg(&client->dev, "Detected Consumer EEPROM calibration %x\n", read); + } else if (read == MLX90632_ID_EXTENDED) { + dev_dbg(&client->dev, + "Detected Extended range EEPROM calibration %x\n", read); + mlx90632->mtyp = MLX90632_MTYP_EXTENDED; } else if ((read & MLX90632_DSP_MASK) == MLX90632_DSP_VERSION) { dev_dbg(&client->dev, "Detected Unknown EEPROM calibration %x\n", read); @@ -684,6 +891,7 @@ static int mlx90632_probe(struct i2c_client *client, } mlx90632->emissivity = 1000; + mlx90632->object_ambient_temperature = 25000; /* 25 degrees milliCelsius */ pm_runtime_disable(&client->dev); ret = pm_runtime_set_active(&client->dev); From patchwork Sat Aug 8 12:10:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crt Mori X-Patchwork-Id: 11706297 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 7E8F3138A for ; Sat, 8 Aug 2020 12:12:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6637120759 for ; Sat, 8 Aug 2020 12:12:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=melexis.com header.i=@melexis.com header.b="nmjhCjBR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726384AbgHHMMN (ORCPT ); Sat, 8 Aug 2020 08:12:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726240AbgHHMLP (ORCPT ); Sat, 8 Aug 2020 08:11:15 -0400 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A754AC061A2E for ; Sat, 8 Aug 2020 05:11:04 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id df16so3106352edb.9 for ; Sat, 08 Aug 2020 05:11:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iwT5dLwqwIktBk8BAuaK5rEPEXEhVNZFccTAy3Fbof4=; b=nmjhCjBRc9mxMyseEIB8/TrT6uRxSgmqvU7lsjzBSn9l+mqTkV5yzykZ0W1vi5hLq4 Lo51w9QKljps5lw8NaHixOkRYaLTcCh7CUboskR2scMCZqOzkVX+1fyrkFE1lkRjLCbc LUKsrlrQi+EB/Z7+U2hJQAoWDkw+vLVNnmizolCmnobSU8kbDNAlXivMvNW5vxGKa1gM KWGXzHkNwVyUqCb06LXqqQrdBlJX+wW6UPcLZQKNkcqUHyG2zMCz1avYZ2YZ73DAIrlV 7YE63aY+8HCA6Z5tezExRob9FEUi1DdoKXuqHSaARb7Ahx3jZejeeRVIuJX4BdqY78UM CWdw== 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=iwT5dLwqwIktBk8BAuaK5rEPEXEhVNZFccTAy3Fbof4=; b=IqJRcFcrHN508w3w4TY6nFU5MhbsyyGFpMm+ks/dtPH4UMpvD+no6yp+VTDuWemlug NyDjuY0CCIKN4mIt81k7EA+nUm6bYpsIKFbrT3zlAjZcD7xc4Oeuq3jvjYvvs36r1y0c BY8X2qu9AC9cndWmqcEbh/66CEUb0stf01HPB57jcIqzwcOHWJswXyQ6cJu0h3ClyLXZ jo+S9uJvbba1KjhP2y3usG5WL0Gi9d6bEecGKQUv0zpXdWgUGMuq5Cwm0puqtxyWMVKB ULC3lUNlphc1BWQ5lzNMrcr7Lo5VrFIwoNvNufNFp2WJ2a8i3jM6ubOyBpxCyGyRilOn iQsQ== X-Gm-Message-State: AOAM53025tLXsKEalw3rpJIpnsfbWYA4dmIIY1rRkNuABeiFKkuy2zOt JEHa28EyTYheFtAN4SIFYY0+3A== X-Google-Smtp-Source: ABdhPJxwIPtcD5lqZgbxRX29wdIA7hTdDh3T8+rpEb7WJPXKRxX2wPPSEkQhGDxzi07w08J4v0phHw== X-Received: by 2002:aa7:dc4f:: with SMTP id g15mr13266140edu.335.1596888663357; Sat, 08 Aug 2020 05:11:03 -0700 (PDT) Received: from localhost.localdomain (ptr-4xajgywt9p1tstfsdfc.18120a2.ip6.access.telenet.be. [2a02:1810:a421:dd00:b0e6:1cd9:30c1:35f8]) by smtp.gmail.com with ESMTPSA id b2sm8130833ejg.70.2020.08.08.05.11.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 05:11:02 -0700 (PDT) From: Crt Mori To: Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Crt Mori Subject: [PATCH v4 3/4] iio:temperature:mlx90632: Add kerneldoc to the internal struct Date: Sat, 8 Aug 2020 14:10:25 +0200 Message-Id: <20200808121026.1300375-4-cmo@melexis.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200808121026.1300375-1-cmo@melexis.com> References: <20200808121026.1300375-1-cmo@melexis.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Document internal/private struct for mlx90632 device. Signed-off-by: Crt Mori --- drivers/iio/temperature/mlx90632.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/iio/temperature/mlx90632.c b/drivers/iio/temperature/mlx90632.c index 049c1217a166..4e0131705c11 100644 --- a/drivers/iio/temperature/mlx90632.c +++ b/drivers/iio/temperature/mlx90632.c @@ -107,12 +107,23 @@ #define MLX90632_SLEEP_DELAY_MS 3000 /**< Autosleep delay */ #define MLX90632_EXTENDED_LIMIT 27000 /* Extended mode raw value limit */ +/** + * struct mlx90632_data - private data for the MLX90632 device + * @client: I2C client of the device + * @lock: Internal mutex for multiple reads for single measurement + * @regmap: Regmap of the device + * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1. + * @mtyp: Measurement type physical sensor configuration for extended range + * calculations + * @object_ambient_temperature: Ambient temperature at object (might differ of + * the ambient temperature of sensor. + */ struct mlx90632_data { struct i2c_client *client; - struct mutex lock; /* Multiple reads for single measurement */ + struct mutex lock; struct regmap *regmap; u16 emissivity; - u8 mtyp; /* measurement type - to enable extended range calculations */ + u8 mtyp; u32 object_ambient_temperature; }; From patchwork Sat Aug 8 12:10:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crt Mori X-Patchwork-Id: 11706301 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 9FA9E138A for ; Sat, 8 Aug 2020 12:13:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 876EC20759 for ; Sat, 8 Aug 2020 12:13:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=melexis.com header.i=@melexis.com header.b="awV+k81W" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726400AbgHHMMh (ORCPT ); Sat, 8 Aug 2020 08:12:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726312AbgHHMLP (ORCPT ); Sat, 8 Aug 2020 08:11:15 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FEDEC061A30 for ; Sat, 8 Aug 2020 05:11:06 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id o18so4755164eje.7 for ; Sat, 08 Aug 2020 05:11:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/XWY4gvQRYHXEyWmE4WsXwZ1nN+0si4ZlbIfngpHZ5M=; b=awV+k81WnzaxYEO9gsxcGspjzXsyKu03ExAJEbK9V/ZL3FfU2Nj1gdRph/ixyi65Rk jfIun1AY/HiBnDJVnRqedB1bV3fPirB79TaRcxNKY3r2qYkKLb7wKha8Z60S1Tk/lK+5 kStUyHD0H6iu1uI8qwM2RnoBj8EKttgGc8k8SIU4oz2n2gCYjNyd6+eEa9fYVy3NB7Yf j52xN1L+DnnztgzcDWAhlIJyBpP/kgiHA3oxcInwJ7MO7/jcMwU0xq0RCimqrThsLQYj qyxavCKanOWsEAKpByVVRmc+FtDiy65xRcKihp1//lvK25CauS8V3W/vWiQL+++aLMMU hMog== 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=/XWY4gvQRYHXEyWmE4WsXwZ1nN+0si4ZlbIfngpHZ5M=; b=tAlC1rEfVhfPoiw3LXi2jNVO2SiNFT+NivO0LdlKcFoWpCvTN7UWAg+jID8kfPuz8T aahl3rF4i450ttL1YingxsZcXkUa0aj6Uc2CyF3ujLabLdEFojbtilUjQYqa2DcmyR+8 3g+H/aPn/1VMWUK3yx13ZffG2eIEUu2Lz2q90denAgkJu4KZitQFc6Xb2gIiDsx6ejqz D6d748p63Yv/wunfZt7+H3qaJMbbmxcZq5kNGibyDHNowCTD/Bu+OuEw1d+hQuXf9vKL fl7t+A81XBDYo1i8+d6FeEhcZBkV723fOggIoiCxipdn5ELf+KphtiIY2tshYr/DEFsv JBDQ== X-Gm-Message-State: AOAM5326S/Nn6DGvMx5MBMhSTLrHpHQEC2SsiNeZoH1zX4ByQIDfUZki 5g5qBqaptK80yQ+7ZijhkG5fxQ== X-Google-Smtp-Source: ABdhPJxQzvJnqokhx97YNGfRwinM7IAeYCnLYiHfVMfvK3qytGjU16LoWecSxOgwsXIzu1cScWZ/LA== X-Received: by 2002:a17:906:f905:: with SMTP id lc5mr13207897ejb.340.1596888665305; Sat, 08 Aug 2020 05:11:05 -0700 (PDT) Received: from localhost.localdomain (ptr-4xajgywt9p1tstfsdfc.18120a2.ip6.access.telenet.be. [2a02:1810:a421:dd00:b0e6:1cd9:30c1:35f8]) by smtp.gmail.com with ESMTPSA id b2sm8130833ejg.70.2020.08.08.05.11.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 05:11:04 -0700 (PDT) From: Crt Mori To: Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Crt Mori Subject: [PATCH v4 4/4] iio:temperature:mlx90632: Convert polling while loops to do-while Date: Sat, 8 Aug 2020 14:10:26 +0200 Message-Id: <20200808121026.1300375-5-cmo@melexis.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200808121026.1300375-1-cmo@melexis.com> References: <20200808121026.1300375-1-cmo@melexis.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Reduce number of lines and improve readability to convert polling while loops to do-while. The iopoll.h interface was not used, because we require more than 20ms timeout, because time for sensor to perform a measurement is around 10ms and it needs to perform measurements for each channel (which currently is 3). Signed-off-by: Crt Mori --- drivers/iio/temperature/mlx90632.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/iio/temperature/mlx90632.c b/drivers/iio/temperature/mlx90632.c index 4e0131705c11..24ffcce9ad74 100644 --- a/drivers/iio/temperature/mlx90632.c +++ b/drivers/iio/temperature/mlx90632.c @@ -214,15 +214,13 @@ static int mlx90632_perform_measurement(struct mlx90632_data *data) if (ret < 0) return ret; - while (tries-- > 0) { + do { ret = regmap_read(data->regmap, MLX90632_REG_STATUS, ®_status); if (ret < 0) return ret; - if (reg_status & MLX90632_STAT_DATA_RDY) - break; usleep_range(10000, 11000); - } + } while (!(reg_status & MLX90632_STAT_DATA_RDY) && tries--); if (tries < 0) { dev_err(&data->client->dev, "data not ready"); @@ -419,7 +417,7 @@ static int mlx90632_read_object_raw_extended(struct regmap *regmap, s16 *object_ static int mlx90632_read_all_channel_extended(struct mlx90632_data *data, s16 *object_new_raw, s16 *ambient_new_raw, s16 *ambient_old_raw) { - int tries = 4; + int tries = 5; int ret; mutex_lock(&data->lock); @@ -427,14 +425,13 @@ static int mlx90632_read_all_channel_extended(struct mlx90632_data *data, s16 *o if (ret < 0) goto read_unlock; - while (tries-- > 0) { + do { ret = mlx90632_perform_measurement(data); if (ret < 0) goto read_unlock; - if (ret == 19) - break; - } + } while ((ret != 19) && tries--); + if (tries < 0) { ret = -ETIMEDOUT; goto read_unlock;