From nobody Mon Feb 9 04:12:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82F32C83F3E for ; Sun, 3 Sep 2023 09:01:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236187AbjICJBF (ORCPT ); Sun, 3 Sep 2023 05:01:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233079AbjICJBE (ORCPT ); Sun, 3 Sep 2023 05:01:04 -0400 Received: from relmlie5.idc.renesas.com (relmlor1.renesas.com [210.160.252.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6218D126; Sun, 3 Sep 2023 02:01:00 -0700 (PDT) X-IronPort-AV: E=Sophos;i="6.02,224,1688396400"; d="scan'208";a="174837520" Received: from unknown (HELO relmlir6.idc.renesas.com) ([10.200.68.152]) by relmlie5.idc.renesas.com with ESMTP; 03 Sep 2023 18:01:00 +0900 Received: from localhost.localdomain (unknown [10.226.92.30]) by relmlir6.idc.renesas.com (Postfix) with ESMTP id 8674D4189AAA; Sun, 3 Sep 2023 18:00:57 +0900 (JST) From: Biju Das To: Jonathan Cameron Cc: Biju Das , Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Biju Das , Andy Shevchenko Subject: [PATCH v3 1/2] iio: accel: adxl345: Convert enum->pointer for data in match data table Date: Sun, 3 Sep 2023 10:00:50 +0100 Message-Id: <20230903090051.39274-2-biju.das.jz@bp.renesas.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230903090051.39274-1-biju.das.jz@bp.renesas.com> References: <20230903090051.39274-1-biju.das.jz@bp.renesas.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Convert enum->pointer for data in match data table, so that device_get_match_data() can do match against OF/ACPI/I2C tables, once i2c bus type match support added to it. Add struct adxl345_chip_info and replace enum->adxl345_chip_info in the match table and simplify adxl345_probe(). Signed-off-by: Biju Das --- v2->v3: * Replaced struct adxl3x5_chip_info->struct adxl345_chip_info. v1->v2: * Replaced EINVAL->ENODEV for invalid chip type. * Kept leading commas for adxl345_*_info and adxl375_*_info. * Restored switch statement in adxl345_core_probe()=20 --- drivers/iio/accel/adxl345.h | 5 +++++ drivers/iio/accel/adxl345_core.c | 24 ++++++------------------ drivers/iio/accel/adxl345_i2c.c | 20 +++++++++++++++----- drivers/iio/accel/adxl345_spi.c | 20 +++++++++++++++----- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/drivers/iio/accel/adxl345.h b/drivers/iio/accel/adxl345.h index d7e67cb08538..826e0575ae79 100644 --- a/drivers/iio/accel/adxl345.h +++ b/drivers/iio/accel/adxl345.h @@ -13,6 +13,11 @@ enum adxl345_device_type { ADXL375 =3D 2, }; =20 +struct adxl345_chip_info { + const char *name; + unsigned int type; +}; + int adxl345_core_probe(struct device *dev, struct regmap *regmap); =20 #endif /* _ADXL345_H_ */ diff --git a/drivers/iio/accel/adxl345_core.c b/drivers/iio/accel/adxl345_c= ore.c index 1919e0089c11..28f77b5de47d 100644 --- a/drivers/iio/accel/adxl345_core.c +++ b/drivers/iio/accel/adxl345_core.c @@ -61,9 +61,9 @@ static const int adxl345_uscale =3D 38300; static const int adxl375_uscale =3D 480000; =20 struct adxl345_data { + const struct adxl345_chip_info *info; struct regmap *regmap; u8 data_range; - enum adxl345_device_type type; }; =20 #define ADXL345_CHANNEL(index, axis) { \ @@ -110,7 +110,7 @@ static int adxl345_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: *val =3D 0; - switch (data->type) { + switch (data->info->type) { case ADXL345: *val2 =3D adxl345_uscale; break; @@ -222,25 +222,11 @@ static void adxl345_powerdown(void *regmap) =20 int adxl345_core_probe(struct device *dev, struct regmap *regmap) { - enum adxl345_device_type type; struct adxl345_data *data; struct iio_dev *indio_dev; - const char *name; u32 regval; int ret; =20 - type =3D (uintptr_t)device_get_match_data(dev); - switch (type) { - case ADXL345: - name =3D "adxl345"; - break; - case ADXL375: - name =3D "adxl375"; - break; - default: - return -EINVAL; - } - ret =3D regmap_read(regmap, ADXL345_REG_DEVID, ®val); if (ret < 0) return dev_err_probe(dev, ret, "Error reading device ID\n"); @@ -255,16 +241,18 @@ int adxl345_core_probe(struct device *dev, struct reg= map *regmap) =20 data =3D iio_priv(indio_dev); data->regmap =3D regmap; - data->type =3D type; /* Enable full-resolution mode */ data->data_range =3D ADXL345_DATA_FORMAT_FULL_RES; + data->info =3D device_get_match_data(dev); + if (!data->info) + return -ENODEV; =20 ret =3D regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT, data->data_range); if (ret < 0) return dev_err_probe(dev, ret, "Failed to set data range\n"); =20 - indio_dev->name =3D name; + indio_dev->name =3D data->info->name; indio_dev->info =3D &adxl345_info; indio_dev->modes =3D INDIO_DIRECT_MODE; indio_dev->channels =3D adxl345_channels; diff --git a/drivers/iio/accel/adxl345_i2c.c b/drivers/iio/accel/adxl345_i2= c.c index e47d12f19602..8cb6254297f7 100644 --- a/drivers/iio/accel/adxl345_i2c.c +++ b/drivers/iio/accel/adxl345_i2c.c @@ -30,22 +30,32 @@ static int adxl345_i2c_probe(struct i2c_client *client) return adxl345_core_probe(&client->dev, regmap); } =20 +static const struct adxl345_chip_info adxl345_i2c_info =3D { + .name =3D "adxl345", + .type =3D ADXL345, +}; + +static const struct adxl345_chip_info adxl375_i2c_info =3D { + .name =3D "adxl375", + .type =3D ADXL375, +}; + static const struct i2c_device_id adxl345_i2c_id[] =3D { - { "adxl345", ADXL345 }, - { "adxl375", ADXL375 }, + { "adxl345", (kernel_ulong_t)&adxl345_i2c_info }, + { "adxl375", (kernel_ulong_t)&adxl375_i2c_info }, { } }; MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id); =20 static const struct of_device_id adxl345_of_match[] =3D { - { .compatible =3D "adi,adxl345", .data =3D (const void *)ADXL345 }, - { .compatible =3D "adi,adxl375", .data =3D (const void *)ADXL375 }, + { .compatible =3D "adi,adxl345", .data =3D &adxl345_i2c_info }, + { .compatible =3D "adi,adxl375", .data =3D &adxl375_i2c_info }, { } }; MODULE_DEVICE_TABLE(of, adxl345_of_match); =20 static const struct acpi_device_id adxl345_acpi_match[] =3D { - { "ADS0345", ADXL345 }, + { "ADS0345", (kernel_ulong_t)&adxl345_i2c_info }, { } }; MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match); diff --git a/drivers/iio/accel/adxl345_spi.c b/drivers/iio/accel/adxl345_sp= i.c index aaade5808657..ede9b9496158 100644 --- a/drivers/iio/accel/adxl345_spi.c +++ b/drivers/iio/accel/adxl345_spi.c @@ -36,22 +36,32 @@ static int adxl345_spi_probe(struct spi_device *spi) return adxl345_core_probe(&spi->dev, regmap); } =20 +static const struct adxl345_chip_info adxl345_spi_info =3D { + .name =3D "adxl345", + .type =3D ADXL345, +}; + +static const struct adxl345_chip_info adxl375_spi_info =3D { + .name =3D "adxl375", + .type =3D ADXL375, +}; + static const struct spi_device_id adxl345_spi_id[] =3D { - { "adxl345", ADXL345 }, - { "adxl375", ADXL375 }, + { "adxl345", (kernel_ulong_t)&adxl345_spi_info }, + { "adxl375", (kernel_ulong_t)&adxl375_spi_info }, { } }; MODULE_DEVICE_TABLE(spi, adxl345_spi_id); =20 static const struct of_device_id adxl345_of_match[] =3D { - { .compatible =3D "adi,adxl345", .data =3D (const void *)ADXL345 }, - { .compatible =3D "adi,adxl375", .data =3D (const void *)ADXL375 }, + { .compatible =3D "adi,adxl345", .data =3D &adxl345_spi_info }, + { .compatible =3D "adi,adxl375", .data =3D &adxl375_spi_info }, { } }; MODULE_DEVICE_TABLE(of, adxl345_of_match); =20 static const struct acpi_device_id adxl345_acpi_match[] =3D { - { "ADS0345", ADXL345 }, + { "ADS0345", (kernel_ulong_t)&adxl345_spi_info }, { } }; MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match); --=20 2.25.1 From nobody Mon Feb 9 04:12:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 941FCC71153 for ; Sun, 3 Sep 2023 09:01:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236207AbjICJBL (ORCPT ); Sun, 3 Sep 2023 05:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236192AbjICJBK (ORCPT ); Sun, 3 Sep 2023 05:01:10 -0400 Received: from relmlie6.idc.renesas.com (relmlor2.renesas.com [210.160.252.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 522CE127; Sun, 3 Sep 2023 02:01:03 -0700 (PDT) X-IronPort-AV: E=Sophos;i="6.02,224,1688396400"; d="scan'208";a="178544903" Received: from unknown (HELO relmlir6.idc.renesas.com) ([10.200.68.152]) by relmlie6.idc.renesas.com with ESMTP; 03 Sep 2023 18:01:03 +0900 Received: from localhost.localdomain (unknown [10.226.92.30]) by relmlir6.idc.renesas.com (Postfix) with ESMTP id 9D4CD4189CC3; Sun, 3 Sep 2023 18:01:00 +0900 (JST) From: Biju Das To: Jonathan Cameron Cc: Biju Das , Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Biju Das , Andy Shevchenko Subject: [PATCH v3 2/2] iio: accel: adxl345: Simplify adxl345_read_raw() Date: Sun, 3 Sep 2023 10:00:51 +0100 Message-Id: <20230903090051.39274-3-biju.das.jz@bp.renesas.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230903090051.39274-1-biju.das.jz@bp.renesas.com> References: <20230903090051.39274-1-biju.das.jz@bp.renesas.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Simplify adxl345_read_raw() by adding uscale variable to struct adxl345_chip_info. Also convert variables adxl3{4,7}5_uscale to macros and use it in OF/ACPI/ID match table. Drop enum adxl345_device_type as there is no user. Signed-off-by: Biju Das --- v3: * New patch. --- drivers/iio/accel/adxl345.h | 20 +++++++++++++++----- drivers/iio/accel/adxl345_core.c | 25 +------------------------ drivers/iio/accel/adxl345_i2c.c | 4 ++-- drivers/iio/accel/adxl345_spi.c | 4 ++-- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/drivers/iio/accel/adxl345.h b/drivers/iio/accel/adxl345.h index 826e0575ae79..284bd387ce69 100644 --- a/drivers/iio/accel/adxl345.h +++ b/drivers/iio/accel/adxl345.h @@ -8,14 +8,24 @@ #ifndef _ADXL345_H_ #define _ADXL345_H_ =20 -enum adxl345_device_type { - ADXL345 =3D 1, - ADXL375 =3D 2, -}; +/* + * In full-resolution mode, scale factor is maintained at ~4 mg/LSB + * in all g ranges. + * + * At +/- 16g with 13-bit resolution, scale is computed as: + * (16 + 16) * 9.81 / (2^13 - 1) =3D 0.0383 + */ +#define ADXL345_USCALE 38300 + +/* + * The Datasheet lists a resolution of Resolution is ~49 mg per LSB. That's + * ~480mm/s**2 per LSB. + */ +#define ADXL375_USCALE 480000 =20 struct adxl345_chip_info { const char *name; - unsigned int type; + int uscale; }; =20 int adxl345_core_probe(struct device *dev, struct regmap *regmap); diff --git a/drivers/iio/accel/adxl345_core.c b/drivers/iio/accel/adxl345_c= ore.c index 28f77b5de47d..8bd30a23ed3b 100644 --- a/drivers/iio/accel/adxl345_core.c +++ b/drivers/iio/accel/adxl345_core.c @@ -45,21 +45,6 @@ =20 #define ADXL345_DEVID 0xE5 =20 -/* - * In full-resolution mode, scale factor is maintained at ~4 mg/LSB - * in all g ranges. - * - * At +/- 16g with 13-bit resolution, scale is computed as: - * (16 + 16) * 9.81 / (2^13 - 1) =3D 0.0383 - */ -static const int adxl345_uscale =3D 38300; - -/* - * The Datasheet lists a resolution of Resolution is ~49 mg per LSB. That's - * ~480mm/s**2 per LSB. - */ -static const int adxl375_uscale =3D 480000; - struct adxl345_data { const struct adxl345_chip_info *info; struct regmap *regmap; @@ -110,15 +95,7 @@ static int adxl345_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: *val =3D 0; - switch (data->info->type) { - case ADXL345: - *val2 =3D adxl345_uscale; - break; - case ADXL375: - *val2 =3D adxl375_uscale; - break; - } - + *val2 =3D data->info->uscale; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_CALIBBIAS: ret =3D regmap_read(data->regmap, diff --git a/drivers/iio/accel/adxl345_i2c.c b/drivers/iio/accel/adxl345_i2= c.c index 8cb6254297f7..a3084b0a8f78 100644 --- a/drivers/iio/accel/adxl345_i2c.c +++ b/drivers/iio/accel/adxl345_i2c.c @@ -32,12 +32,12 @@ static int adxl345_i2c_probe(struct i2c_client *client) =20 static const struct adxl345_chip_info adxl345_i2c_info =3D { .name =3D "adxl345", - .type =3D ADXL345, + .uscale =3D ADXL345_USCALE, }; =20 static const struct adxl345_chip_info adxl375_i2c_info =3D { .name =3D "adxl375", - .type =3D ADXL375, + .uscale =3D ADXL375_USCALE, }; =20 static const struct i2c_device_id adxl345_i2c_id[] =3D { diff --git a/drivers/iio/accel/adxl345_spi.c b/drivers/iio/accel/adxl345_sp= i.c index ede9b9496158..93ca349f1780 100644 --- a/drivers/iio/accel/adxl345_spi.c +++ b/drivers/iio/accel/adxl345_spi.c @@ -38,12 +38,12 @@ static int adxl345_spi_probe(struct spi_device *spi) =20 static const struct adxl345_chip_info adxl345_spi_info =3D { .name =3D "adxl345", - .type =3D ADXL345, + .uscale =3D ADXL345_USCALE, }; =20 static const struct adxl345_chip_info adxl375_spi_info =3D { .name =3D "adxl375", - .type =3D ADXL375, + .uscale =3D ADXL375_USCALE, }; =20 static const struct spi_device_id adxl345_spi_id[] =3D { --=20 2.25.1