Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2788871rdb; Mon, 4 Dec 2023 07:35:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IFmeBwSqdVgj4NnHAOMrxKP6gJxYX7qBxZJkD7ccUnRE9tbPQn6l4sW1VPZPIIEUXqlhQ29 X-Received: by 2002:a17:903:1c5:b0:1cf:b12a:a9eb with SMTP id e5-20020a17090301c500b001cfb12aa9ebmr5614025plh.19.1701704123574; Mon, 04 Dec 2023 07:35:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701704123; cv=none; d=google.com; s=arc-20160816; b=wh09aSSoo0fy6xHPeYYMZDLhCe7lvHy+CTXFozBTkxlInNs1WS0LESAxOjCG6V2RSv HE6D/QJMtABZqFhMRPyeIpi0XCBCryeoE8eAf/K+POVSXfl+9PGAn68b1/zXtoa/Ta2g QZlevTxG0mvZO8Mp4AdKro+kKdkha21jzWHOWY/WSQ63gwT1wt5uPXkjwXUPaPw+WdIg inmHEfJHZX46m+EwkaN072x89MvQ9lK0KI8uAGQvEQfSoEp1xKoiAz3wa4Mr++GPRoMZ y+ntnFLMHmiIgzRQa3S5CKmyuN5odGUdMhOTqHmAWP22050MpKM9GuOVuNEeBJcpto1Z utLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WDUehL7n0JLsz+Gx1lP++nFrAN6gSvo/7fbfTtyTKX4=; fh=q394J62gLmoYnXlXua5o1ht4bA4Nt58YAG8jhKuCs38=; b=LNz1klUCAG9MfikrjRUsp6OLdE7zPYZzvVn/KB4JT7Rx8BweF7Rx4jg944T3f7biw/ mf/lWtaM4y8vo4YMCiUCZVGAwFlL4Oe+6JPRxCFI4xR43RRipTYnTpVT5W+0Yksm+/HW zV7EXOSS3Z9oaV1C3NrjLLhZslrSis2RjQR6KvZzYGyPCEgS3PC3Vcy/LjST7ST5GLsX udrvuAjfjPI/ubDVjmx3bKUUHMYlE8LmckA1L/h52x+yqJJV4vNTUA7W9r89BNtcaYhz r6vz0MMuK46qKX4VaMdAsRwiREkixrYQpTaHkpUnf/my7HDq4Ng5CmA+sr3J2PiI+w7N ROtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@melexis.com header.s=google header.b=S7dVFzbl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=melexis.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id h7-20020a170902f7c700b001cfa37f9a6esi2446335plw.531.2023.12.04.07.35.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 07:35:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@melexis.com header.s=google header.b=S7dVFzbl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=melexis.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 77341804C219; Mon, 4 Dec 2023 07:35:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234119AbjLDPfE (ORCPT + 99 others); Mon, 4 Dec 2023 10:35:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231328AbjLDPfC (ORCPT ); Mon, 4 Dec 2023 10:35:02 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE3A7B6 for ; Mon, 4 Dec 2023 07:35:07 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3b8903f7192so2368368b6e.0 for ; Mon, 04 Dec 2023 07:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; t=1701704107; x=1702308907; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=WDUehL7n0JLsz+Gx1lP++nFrAN6gSvo/7fbfTtyTKX4=; b=S7dVFzblGdjfaR/oaonbPAS2wykpAXH3rFrumzI63knevYyuGotOER1f42OLCojdEY UrjPVERD0zjRVKkkuWhghxuShKitJ12waJMK8r+Qaw2EQKtKiDzYlHVLuoCNthm8G05t JSSnavEzmq8/2PreYIwQyNzNwf6OTBYOq348XC51XRLlKUDJx3YC9FmIXhvCquO1bN5j 4403LreycFSg7C0yE7bVkU+BR+Cy3bV34KHW7x3na9cFCG0/8VmgN9rq+IjIzMX2+nCQ Ya/hbWXh20mlrzwaw2Cl/R/7yOL4E/nqXoPnZxCkBbFSi7Q+1ST1cnpjFAJHJocbK06J qRpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701704107; x=1702308907; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WDUehL7n0JLsz+Gx1lP++nFrAN6gSvo/7fbfTtyTKX4=; b=CNm77L0Qyf42NsXeekqu68U+PFZiWYvBjuh+YQljgPtfZXBrLCMCS9fRmBHET/t53l MLvToBAM9gfAh1UHBSS4ewjTZ5fL/FUvuaR9KDu8+GOd6PpasZruE94D4GRyVMfjoGrt 711ZGkzpcNoqn010DAm8rFYCvYepfangtZsvgXHEE+dCoDhGZmVBrYXa+e6CBTaSzHDE GvBbBvendWIDAZMfSnFiO3fZwYzAKs1Nsx+B1SlU/JXV3bS/M8DkZPUXFrROjT2jJhEt n7joONuwvTiFxpXGkdIg4uhroRtc5V28XgIOVP3Ts6zxXMCIOM7+Hv0jeMWnnulBpTNM j3AA== X-Gm-Message-State: AOJu0Yy50wm9mdt2z0jX/fTlYFNshBTiR0CaTZPEwtycnUc49ouLFZdb WB5N9KWK5N2cXvIyglr+8ShyRUOLbGbF51wUxA0tqyF1PYImD0dy X-Received: by 2002:a05:6871:82a:b0:1fb:75a:c41d with SMTP id q42-20020a056871082a00b001fb075ac41dmr4326491oap.70.1701704106873; Mon, 04 Dec 2023 07:35:06 -0800 (PST) MIME-Version: 1.0 References: <20231204142224.51f2ccdf@jic23-huawei> In-Reply-To: <20231204142224.51f2ccdf@jic23-huawei> From: Crt Mori Date: Mon, 4 Dec 2023 16:34:30 +0100 Message-ID: Subject: Re: [PATCH v2 1/2] iio: temperature: mlx90635 MLX90635 IR Temperature sensor To: Jonathan Cameron Cc: Lars-Peter Clausen , Andrew Hepp , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 04 Dec 2023 07:35:20 -0800 (PST) On Mon, 4 Dec 2023 at 15:22, Jonathan Cameron wrote: > ... > > switches to Continuous power mode where measurements constantly change > > without triggering. > > > > Signed-off-by: Crt Mori > > Hi Crt, > > I don't understand some of the regcache_cache_only() manipulation in here. > If I understand the aim correctly it is to allow us to write settings whilst > powered down (in sleep_step) that will then by synced to the device when it enters > continuous mode? > > If so, I'd expect to only see manipulation of whether the caching is or or > not at places where we transition state. You currently have them in various > other place. In some cases I scan see it's to allow a temporary change of > state, but it's not obvious. So perhaps a comment ever time you manually > tweak whether writes hit the device or just stick in the regacache. > That comment can explain why each of them is needed. > > A few other comments inline, > > Thanks, > > Jonathan > While in Sleep Step mode, the EEPROM is powered down, but the cache buffers those values. Still when you try to write or read a volatile register (which should not be prevented by cache enabled as per my opinion, but code says differently) in that mode, it returns -EBUSY (as we discovered by code), so this kind of manipulation is needed to enable write and read operations from volatile registers. And you need to trigger the measurement (burst mode) in that state, but since you cannot read EEPROM, yet still need its values to calculate the final temperature, the cache is used for this case. There is nothing to re-cache when we get back as all registers I read/write to are marked as volatile, so they would not be cached anyway. Thanks for the review - I still have some questions below (and explanation, but not sure where to put those). > > diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile ... > > + * @lock: Internal mutex for multiple reads for single measurement > > Multiple reads shouldn't be a problem, unless someone else can do something > destructive in between. Perhaps a little more detail on why multiple reads matter? > You trigger device to perform measurement in Sleep Step mode, so to ensure both object and ambient temperature reads are from the same triggered measurement, the mutex needs to be held. If for example in between you would retrigger the measurement, then you would operate on "invalid" data (shouldn't differ much, but I wanted to prevent that as it might be 0). > > + * @regmap: Regmap of the device > > + * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1. > > + * @regulator: Regulator of the device > > + * @powerstatus: Current POWER status of the device > > + * @interaction_ts: Timestamp of the last temperature read that is used > > + * for power management in jiffies > > + */ ... > > + mutex_lock(&data->lock); > > + if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) { > > + regcache_cache_only(data->regmap, false); > > + ret = mlx90635_perform_measurement_burst(data); > > Why is a burst needed here? Perhaps a comment? > Burst is from 90632 terminology (and our chip register map), but maybe more general would be "trigger_measurement"? > > +static int mlx90635_get_refresh_rate(struct mlx90635_data *data, > > + unsigned int *refresh_rate) > > +{ > > + unsigned int reg; > > + int ret; > > + > > + if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) > > + regcache_cache_only(data->regmap, false); > > Definitely needs a comment on why this is needed in this case. > Here and below (where we turn it back to true?), but then I assume in all other instances as well? Maybe a more general comment in the sleep_step mode function? > > + > > + ret = regmap_read(data->regmap, MLX90635_REG_CTRL1, ®); > > + if (ret < 0) > > + return ret; > > + > > + if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) > > + regcache_cache_only(data->regmap, true); > > + > > + *refresh_rate = FIELD_GET(MLX90635_CTRL1_REFRESH_RATE_MASK, reg); > > + > > + return 0; > > +} > > + > > +static const struct { > > + int val; > > + int val2; > > +} mlx90635_freqs[] = { > > + {0, 200000}, > Prefer spaces after { and before } ok. > > + {0, 500000}, > > + {0, 900000}, > > + {1, 700000}, > > + {3, 0}, > > + {4, 800000}, > > + {6, 900000}, > > + {8, 900000} > > +}; ... > > + if (i == ARRAY_SIZE(mlx90635_freqs)) > > + return -EINVAL; > > + > > + if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) > > + regcache_cache_only(data->regmap, false); > > So here you want the rate to get through even though we otherwise have the > device powered down? Is that because some registers are safe for writes > and not others? If so you may need some locking to stop a race where you > turn on writes here and someone else writes. > Yes, exactly the case. Read/Write into registers (REG_) is possible in all modes, but read of EEPROM is not (to save power the EEPROM is turned off). I do not see how write race would get us into trouble here since it is only 1, and as long as chip powerstatus is not changed we should end up in correct state. I can wrap a mutex around though. > > + > > + ret = regmap_write_bits(data->regmap, MLX90635_REG_CTRL1, > > + MLX90635_CTRL1_REFRESH_RATE_MASK, i); > > + > > + if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) > > + regcache_cache_only(data->regmap, true); > > + return ret; > > + default: > > + return -EINVAL; > > + } > > +} >