From patchwork Wed Feb 19 12:32:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13982072 Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 5ABC71DF974; Wed, 19 Feb 2025 12:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739968352; cv=none; b=JWbTX9lcHxNyA/8JQ/7NU0ZlVOaj2uFLsR1HOcuMOFLo+H1EmJTHnj9u7vH+eUr4/lxEtJlrR2oEXX3G3Qn9pUNR89zj3hNUu1bcrFsUGvoV3UIlyZdz9oHC5xspdc6+io6Kk+YDxaaHSaGNwgwK0s36edci33fHy+zdQ9sIgyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739968352; c=relaxed/simple; bh=H48Lkeq71Idm7qh8MQ4iHSeWtKwyxrxWrUPyhfQpTto=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=plRNjXfHtGy4o9Gp937UL/hBG52+HCRIm0ObxLndoTy2x6+eys4SRpv5Bn4/06ZkigNowOksIQkgvwoYT39oIrgzOfUKEUfcXa3gYuS2sml7gJ2teqwF7/UeyuweTp/wL1aTu55TvuuHuXOjjDhcrPzc1N6tY/JR9Q0bDupDRdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=e/OA+Qfz; arc=none smtp.client-ip=209.85.208.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e/OA+Qfz" Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-30a2dfcfd83so32471981fa.1; Wed, 19 Feb 2025 04:32:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739968348; x=1740573148; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=0DYvjW5f0VauVHlYQoEoi+1LNEnpUSDiH5KvYpk6CeA=; b=e/OA+QfzQEH5ZJiXdsVhdvFFAoz4cIkk5u9WZ4dDvanaUPM+BbJBVVxX/OTSZ3HpcL keiT+Q0TCN0gCuXp1k8Uh9rXoud4szV0JzhU7UjujeupWLzC1KW9jcATjoDMMbHtcgrk E0rKytfACS7dgvi+io27akEpp5a9W3Jd67Vl/UXfLLY417Grf+xXVj0iPEx6k/QoEu+I 1sZrfJRprMSS1k69OZjDBiJ1WIh3rDinQDsT0ZjcjsvqCsqpZh8eOq8NPjdlLpxEz4cp ytYD970KO1UHfWceBWKzop9In7SKEJ+y4AEjYQ+y6Ze2lzr8oaigsnt/BYJ1lMmi4gSO aekw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739968348; x=1740573148; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0DYvjW5f0VauVHlYQoEoi+1LNEnpUSDiH5KvYpk6CeA=; b=BIH1KPJnROumdh+z4Or57q5Ik4wIie7x/aVl9NVWrkbu72Or3daHIwO0MNPUGCVmkk CXL0Bhv5sB2olf0xBFpKeUT9is1Eewcj8+FK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/Fg7ZHn fchKRTXeLhys9mh1uOuLbg== X-Google-Smtp-Source: AGHT+IGpj+sXTk9ydInkrFSzD/8u3Ea569jfA3qPAyfTIDaLYJDMfGIteoPF8DQijF1k+5z6DuXOPg== X-Received: by 2002:a2e:9905:0:b0:308:f01f:181f with SMTP id 38308e7fff4ca-30927aff022mr48385981fa.31.1739968348109; Wed, 19 Feb 2025 04:32:28 -0800 (PST) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30a344c8900sm9350131fa.22.2025.02.19.04.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 04:32:27 -0800 (PST) Date: Wed, 19 Feb 2025 14:32:23 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matti Vaittinen , Lad Prabhakar , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Hugo Villeneuve , Nuno Sa , David Lechner , Javier Carrasco , Andy Shevchenko , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH v3 9/9] iio: adc: ti-ads7924: Respect device tree config Message-ID: <3279aa9348e7149bfbd433daaa201f2eb5873e1f.1739967040.git.mazziesaccount@gmail.com> References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The ti-ads7924 driver ignores the device-tree ADC channel specification and always exposes all 4 channels to users whether they are present in the device-tree or not. Additionally, the "reg" values in the channel nodes are ignored, although an error is printed if they are out of range. Register only the channels described in the device-tree, and use the reg property as a channel ID. Signed-off-by: Matti Vaittinen --- Revision history: v2 => v3: New patch Please note that this is potentially breaking existing users if they have wrong values in the device-tree. I believe the device-tree should ideally be respected, and if it says device X has only one channel, then we should believe it and not register 4. Well, we don't live in the ideal world, so even though I believe this is TheRightThingToDo - it may cause havoc because correct device-tree has not been required from the day 1. So, please review and test and apply at your own risk :) As a side note, this might warrant a fixes tag but the adc-helper -stuff is hardly worth to be backported... (And I've already exceeded my time budget with this series - hence I'll leave crafting backportable fix to TI people ;) ) This has only been compile tested! All testing is highly appreciated. --- drivers/iio/adc/ti-ads7924.c | 80 +++++++++++++++++------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/drivers/iio/adc/ti-ads7924.c b/drivers/iio/adc/ti-ads7924.c index b1f745f75dbe..a5b8f7c81b8a 100644 --- a/drivers/iio/adc/ti-ads7924.c +++ b/drivers/iio/adc/ti-ads7924.c @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -119,15 +120,12 @@ #define ADS7924_TOTAL_CONVTIME_US (ADS7924_PWRUPTIME_US + ADS7924_ACQTIME_US + \ ADS7924_CONVTIME_US) -#define ADS7924_V_CHAN(_chan, _addr) { \ - .type = IIO_VOLTAGE, \ - .indexed = 1, \ - .channel = _chan, \ - .address = _addr, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ - .datasheet_name = "AIN"#_chan, \ -} +static const struct iio_chan_spec ads7924_chan_template = { + .type = IIO_VOLTAGE, + .indexed = 1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), +}; struct ads7924_data { struct device *dev; @@ -182,13 +180,6 @@ static const struct regmap_config ads7924_regmap_config = { .writeable_reg = ads7924_is_writeable_reg, }; -static const struct iio_chan_spec ads7924_channels[] = { - ADS7924_V_CHAN(0, ADS7924_DATA0_U_REG), - ADS7924_V_CHAN(1, ADS7924_DATA1_U_REG), - ADS7924_V_CHAN(2, ADS7924_DATA2_U_REG), - ADS7924_V_CHAN(3, ADS7924_DATA3_U_REG), -}; - static int ads7924_get_adc_result(struct ads7924_data *data, struct iio_chan_spec const *chan, int *val) { @@ -251,32 +242,38 @@ static const struct iio_info ads7924_info = { .read_raw = ads7924_read_raw, }; -static int ads7924_get_channels_config(struct device *dev) +static const struct iio_adc_props ads7924_chan_props = { + .required = IIO_ADC_CHAN_PROP_TYPE_REG, +}; + +static int ads7924_get_channels_config(struct iio_dev *indio_dev, + struct device *dev) { - struct fwnode_handle *node; - int num_channels = 0; + struct iio_chan_spec *chan_array; + int num_channels = 0, i; - device_for_each_child_node(dev, node) { - u32 pval; - unsigned int channel; + num_channels = devm_iio_adc_device_alloc_chaninfo(dev, + &ads7924_chan_template, &chan_array, + &ads7924_chan_props); - if (fwnode_property_read_u32(node, "reg", &pval)) { - dev_err(dev, "invalid reg on %pfw\n", node); - continue; - } + if (num_channels < 0) + return num_channels; - channel = pval; - if (channel >= ADS7924_CHANNELS) { - dev_err(dev, "invalid channel index %d on %pfw\n", - channel, node); - continue; - } + if (!num_channels) + return -EINVAL; + + for (i = 0; i < num_channels; i++) { + static const char * const datasheet_names[] = { + "AIN0", "AIN1", "AIN2", "AIN3" + }; + int ch_id = chan_array[i].channel; - num_channels++; + chan_array[i].address = ADS7924_DATA0_U_REG + ch_id; + chan_array[i].datasheet_name = datasheet_names[ch_id]; } - if (!num_channels) - return -EINVAL; + indio_dev->channels = chan_array; + indio_dev->num_channels = num_channels; return 0; } @@ -370,18 +367,15 @@ static int ads7924_probe(struct i2c_client *client) mutex_init(&data->lock); - indio_dev->name = "ads7924"; - indio_dev->modes = INDIO_DIRECT_MODE; - - indio_dev->channels = ads7924_channels; - indio_dev->num_channels = ARRAY_SIZE(ads7924_channels); - indio_dev->info = &ads7924_info; - - ret = ads7924_get_channels_config(dev); + ret = ads7924_get_channels_config(indio_dev, dev); if (ret < 0) return dev_err_probe(dev, ret, "failed to get channels configuration\n"); + indio_dev->name = "ads7924"; + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->info = &ads7924_info; + data->regmap = devm_regmap_init_i2c(client, &ads7924_regmap_config); if (IS_ERR(data->regmap)) return dev_err_probe(dev, PTR_ERR(data->regmap),