From nobody Mon Nov 25 20:31:38 2024 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A98431D4615 for ; Fri, 25 Oct 2024 09:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729850398; cv=none; b=MaTw4D3sq2/ilsJyN75v7/eRVANeJmNpOqbxPJIx8dH9iaZ/gxUx0BIoeYXsKYb7kbzP7WTlWVVRF4ttouEQifLtUbMgGBmGvy7qGgz0MvuDM4umg1mbyJ0UGOgwC7GswmXkUuP7sSEQ6+G6GuF0QcR21Kv3O/ok8U/q4Ab7plg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729850398; c=relaxed/simple; bh=bnh6k+2nsTlVPGOPQPnS6MUhhFKm2XQJVM/36CFpswI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TsWE07RfphWebPCxv2zU4GfWK3+TdmqQVjaCsreqbJk1rTDwN6QE7LLZgRIILY/HZL/h9SWyqWTumRn46Tf8O//rAfesiHInsqcWqjep8KGPrT1UtEDux9wQCo/wycpYV83LkMimwT9P3R5aDG9felDQBnwD5slHSV0ROglWgvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=ZzTVltuU; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="ZzTVltuU" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a9a2cdc6f0cso250846866b.2 for ; Fri, 25 Oct 2024 02:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729850394; x=1730455194; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PGoGtNY9UtsrMSYGprOqDMvN9BiGWMz+YiNB4/Bovi0=; b=ZzTVltuU80JUGGq/SDxc/Y4FLEnkYnYQJSjWDIxE8A7To4N8pEAGePUFg7aQXOVTtY RQk6o7yt7LBiIrw5d5B81kK+e9/NT9Aidnm3fyrkLro67bPN4Gnn/ti02Os1tq4yjYdO 1dv+BPKEg+HJrKTyqnzHNoKemw/lPfDlM1CnQm6ldyHzf3+k6+RceDfGmNXFl8oqfZzw Rnbbwavfi9SGXRLnRA60sgpenT8ssy1WOQav6totKjkn6T3r2NnHyQGXNO2JUme9QIsJ MfGHEhHWCDdf1VSHNt+S1NDJL8/4kg6jtrWfYnDsaklupfBC5ERmQD0QfqVXAPQPjp2N jppw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729850394; x=1730455194; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PGoGtNY9UtsrMSYGprOqDMvN9BiGWMz+YiNB4/Bovi0=; b=brol2mMG6REJqRMC1SjJYwWip7i5YC2nqgU8bxVKvzd/QkUk8nRNV5P0wqHnBGRG9M z7nb7zwKdk0Sc/fBTPQUU0yd/UrNk/xzN65kHtu3+7c5P3aHIf+iqjx2c9nKdJywq5hQ 4WnLvcBG7diI9QRo/ADJjF2KE1uz05xObKT3i94i20sVYNNJBTHRUmz0WCRjXhCydAfg EhruucPY0Vi/BIWvo+luwhYsf/YxayarNXj3Dszw2ZHYQ24opDWuDgIOPDbbxKZq25nU 0skFYT5DNbhvYpjX5jPDqLrAeHlgZTx2MLen2y2coJpVTQcqZDcnFesfhLjJPqTxpzZa 1ksg== X-Forwarded-Encrypted: i=1; AJvYcCWYDH+Tq586xGvwaihONmB/qaXrjiwYBF3hj/bSrG+UDt2h05pOU+I/q5jLcPURrOTmS7g7rDnB6Dvt9/g=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2sWwzwk2BZWwmcgWeEEZtph0LtkUg+QZ9qxpQI3KQsywnfl42 90D0UkyWxlfPM8hpM8/+Ji9ugCamQGTmN5w+Yy+i1ONgvcj3R6vZ1NgTow/9BlA= X-Google-Smtp-Source: AGHT+IFMtmJpPN1a5j/r7T2Jvn7suJkfOXqhDLSHqOMT3yOEa2QZQmBW3GF5Q/ArO2jjFvM4A8g9kg== X-Received: by 2002:a17:907:31c5:b0:a9a:a5c:e23b with SMTP id a640c23a62f3a-a9abf96aa4cmr870774166b.58.1729850393935; Fri, 25 Oct 2024 02:59:53 -0700 (PDT) Received: from localhost.localdomain ([188.27.128.50]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9b1f02951esm51737266b.71.2024.10.25.02.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 02:59:53 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, dlechner@baylibre.com, conor.dooley@microchip.com, Alexandru Ardelean Subject: [PATCH v2 3/5] iio: adc: ad7606: rework scale-available to be static Date: Fri, 25 Oct 2024 12:59:37 +0300 Message-ID: <20241025095939.271811-4-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241025095939.271811-1-aardelean@baylibre.com> References: <20241025095939.271811-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The main driver for this change is the AD7607 part, which has a scale of "1.220703" for the =C2=B110V range. The AD7607 has a resolution of 14-bits. So, just adding the scale-available list for that part would require some quirks to handle just that scale value. But to do it more neatly, the best approach is to rework the scale available lists to have the same format as it is returned to userspace. That way, we can also get rid of the allocation for the 'scale_avail_show' array. Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 106 ++++++++++++++++++--------------------- drivers/iio/adc/ad7606.h | 6 +-- 2 files changed, 50 insertions(+), 62 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index effb98b4dc77..94756bb87b95 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -33,42 +33,44 @@ =20 /* * Scales are computed as 5000/32768 and 10000/32768 respectively, - * so that when applied to the raw values they provide mV values + * so that when applied to the raw values they provide mV values. + * The scale arrays are kept as IIO_VAL_INT_PLUS_MICRO, so index + * X is the integer part and X + 1 is the fractional part. */ -static const unsigned int ad7606_16bit_hw_scale_avail[2] =3D { - 152588, 305176 +static const unsigned int ad7606_16bit_hw_scale_avail[2][2] =3D { + { 0, 152588 }, { 0, 305176 } }; =20 -static const unsigned int ad7606_18bit_hw_scale_avail[2] =3D { - 38147, 76294 +static const unsigned int ad7606_18bit_hw_scale_avail[2][2] =3D { + { 0, 38147 }, { 0, 76294 } }; =20 -static const unsigned int ad7606c_16bit_single_ended_unipolar_scale_avail[= 3] =3D { - 76294, 152588, 190735, +static const unsigned int ad7606c_16bit_single_ended_unipolar_scale_avail[= 3][2] =3D { + { 0, 76294 }, { 0, 152588 }, { 0, 190735 } }; =20 -static const unsigned int ad7606c_16bit_single_ended_bipolar_scale_avail[5= ] =3D { - 76294, 152588, 190735, 305176, 381470 +static const unsigned int ad7606c_16bit_single_ended_bipolar_scale_avail[5= ][2] =3D { + { 0, 76294 }, { 0, 152588 }, { 0, 190735 }, { 0, 305176 }, { 0, 381470 } }; =20 -static const unsigned int ad7606c_16bit_differential_bipolar_scale_avail[4= ] =3D { - 152588, 305176, 381470, 610352 +static const unsigned int ad7606c_16bit_differential_bipolar_scale_avail[4= ][2] =3D { + { 0, 152588 }, { 0, 305176 }, { 0, 381470 }, { 0, 610352 } }; =20 -static const unsigned int ad7606c_18bit_single_ended_unipolar_scale_avail[= 3] =3D { - 19073, 38147, 47684 +static const unsigned int ad7606c_18bit_single_ended_unipolar_scale_avail[= 3][2] =3D { + { 0, 19073 }, { 0, 38147 }, { 0, 47684 } }; =20 -static const unsigned int ad7606c_18bit_single_ended_bipolar_scale_avail[5= ] =3D { - 19073, 38147, 47684, 76294, 95367 +static const unsigned int ad7606c_18bit_single_ended_bipolar_scale_avail[5= ][2] =3D { + { 0, 19073 }, { 0, 38147 }, { 0, 47684 }, { 0, 76294 }, { 0, 95367 } }; =20 -static const unsigned int ad7606c_18bit_differential_bipolar_scale_avail[4= ] =3D { - 38147, 76294, 95367, 152588 +static const unsigned int ad7606c_18bit_differential_bipolar_scale_avail[4= ][2] =3D { + { 0, 38147 }, { 0, 76294 }, { 0, 95367 }, { 0, 152588 } }; =20 -static const unsigned int ad7606_16bit_sw_scale_avail[3] =3D { - 76293, 152588, 305176 +static const unsigned int ad7606_16bit_sw_scale_avail[3][2] =3D { + { 0, 76293 }, { 0, 152588 }, { 0, 305176 } }; =20 static const unsigned int ad7606_oversampling_avail[7] =3D { @@ -649,8 +651,8 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, if (st->sw_mode_en) ch =3D chan->address; cs =3D &st->chan_scales[ch]; - *val =3D 0; - *val2 =3D cs->scale_avail[cs->range]; + *val =3D cs->scale_avail[cs->range][0]; + *val2 =3D cs->scale_avail[cs->range][1]; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: *val =3D st->oversampling; @@ -667,21 +669,6 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, return -EINVAL; } =20 -static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals, - unsigned int n, bool micros) -{ - size_t len =3D 0; - int i; - - for (i =3D 0; i < n; i++) { - len +=3D scnprintf(buf + len, PAGE_SIZE - len, - micros ? "0.%06u " : "%u ", vals[i]); - } - buf[len - 1] =3D '\n'; - - return len; -} - static ssize_t in_voltage_scale_available_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -689,8 +676,16 @@ static ssize_t in_voltage_scale_available_show(struct = device *dev, struct iio_dev *indio_dev =3D dev_to_iio_dev(dev); struct ad7606_state *st =3D iio_priv(indio_dev); struct ad7606_chan_scale *cs =3D &st->chan_scales[0]; + const unsigned int (*vals)[2] =3D cs->scale_avail; + unsigned int i; + size_t len =3D 0; =20 - return ad7606_show_avail(buf, cs->scale_avail, cs->num_scales, true); + for (i =3D 0; i < cs->num_scales; i++) + len +=3D scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ", + vals[i][0], vals[i][1]); + buf[len - 1] =3D '\n'; + + return len; } =20 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0); @@ -728,6 +723,7 @@ static int ad7606_write_raw(struct iio_dev *indio_dev, long mask) { struct ad7606_state *st =3D iio_priv(indio_dev); + unsigned int scale_avail_uv[AD760X_MAX_SCALES]; struct ad7606_chan_scale *cs; int i, ret, ch =3D 0; =20 @@ -738,7 +734,12 @@ static int ad7606_write_raw(struct iio_dev *indio_dev, if (st->sw_mode_en) ch =3D chan->address; cs =3D &st->chan_scales[ch]; - i =3D find_closest(val2, cs->scale_avail, cs->num_scales); + for (i =3D 0; i < cs->num_scales; i++) { + scale_avail_uv[i] =3D cs->scale_avail[i][0] * MICRO + + cs->scale_avail[i][1]; + } + val =3D (val * MICRO) + val2; + i =3D find_closest(val, scale_avail_uv, cs->num_scales); ret =3D st->write_scale(indio_dev, ch, i + cs->reg_offset); if (ret < 0) return ret; @@ -771,9 +772,15 @@ static ssize_t ad7606_oversampling_ratio_avail(struct = device *dev, { struct iio_dev *indio_dev =3D dev_to_iio_dev(dev); struct ad7606_state *st =3D iio_priv(indio_dev); + const unsigned int *vals =3D st->oversampling_avail; + unsigned int i; + size_t len =3D 0; =20 - return ad7606_show_avail(buf, st->oversampling_avail, - st->num_os_ratios, false); + for (i =3D 0; i < st->num_os_ratios; i++) + len +=3D scnprintf(buf + len, PAGE_SIZE - len, "%u ", vals[i]); + buf[len - 1] =3D '\n'; + + return len; } =20 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444, @@ -927,8 +934,8 @@ static int ad7606_read_avail(struct iio_dev *indio_dev, ch =3D chan->address; =20 cs =3D &st->chan_scales[ch]; - *vals =3D cs->scale_avail_show; - *length =3D cs->num_scales * 2; + *vals =3D (int *)cs->scale_avail; + *length =3D cs->num_scales; *type =3D IIO_VAL_INT_PLUS_MICRO; =20 return IIO_AVAIL_LIST; @@ -1051,24 +1058,9 @@ static int ad7606_chan_scales_setup(struct iio_dev *= indio_dev) indio_dev->channels =3D chans; =20 for (ch =3D 0; ch < num_channels; ch++) { - struct ad7606_chan_scale *cs; - int i; - ret =3D st->chip_info->scale_setup_cb(st, &chans[ch + 1], ch); if (ret) return ret; - - cs =3D &st->chan_scales[ch]; - - if (cs->num_scales * 2 > AD760X_MAX_SCALE_SHOW) - return dev_err_probe(st->dev, -ERANGE, - "Driver error: scale range too big"); - - /* Generate a scale_avail list for showing to userspace */ - for (i =3D 0; i < cs->num_scales; i++) { - cs->scale_avail_show[i * 2] =3D 0; - cs->scale_avail_show[i * 2 + 1] =3D cs->scale_avail[i]; - } } =20 return 0; diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index 2c629a15cc33..32c6f776c5df 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -103,8 +103,6 @@ struct ad7606_chip_info { /** * struct ad7606_chan_scale - channel scale configuration * @scale_avail pointer to the array which stores the available scales - * @scale_avail_show a duplicate of 'scale_avail' which is readily formatt= ed - * such that it can be read via the 'read_avail' hook * @num_scales number of elements stored in the scale_avail array * @range voltage range selection, selects which scale to apply * @reg_offset offset for the register value, to be applied when @@ -112,9 +110,7 @@ struct ad7606_chip_info { */ struct ad7606_chan_scale { #define AD760X_MAX_SCALES 16 -#define AD760X_MAX_SCALE_SHOW (AD760X_MAX_SCALES * 2) - const unsigned int *scale_avail; - int scale_avail_show[AD760X_MAX_SCALE_SHOW]; + const unsigned int (*scale_avail)[2]; unsigned int num_scales; unsigned int range; unsigned int reg_offset; --=20 2.46.1