Skip to contentSkip to conversation

ACPI: PMIC: Use sizeof() instead of hard coded value

PATCH Superseded Andy Shevchenko 3 participants 8 messages Aug 30, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ACPI: PMIC: Use sizeof() instead of hard coded value

It's better to use sizeof() of a given buffer than spreading a hard coded value.

---
 drivers/acpi/pmic/intel_pmic_chtdc_ti.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/pmic/intel_pmic_chtdc_ti.c +1 -1
index 418eec523025..6c2a6da430ed 100644
@@ -87,7 +87,7 @@ static int chtdc_ti_pmic_get_raw_temp(struct regmap *regmap, int reg)
 {
 	u8 buf[2];
 
-	if (regmap_bulk_read(regmap, reg, buf, 2))
+	if (regmap_bulk_read(regmap, reg, buf, sizeof(buf)))
 		return -EIO;
 
 	/* stored in big-endian */
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 ACPI: PMIC: Replace open coded be16_to_cpu()

It's easier to understand the nature of a data type when it's written explicitly. With that, replace open coded endianess conversion.

As a side effect it fixes the returned value of intel_crc_pmic_update_aux() since ACPI PMIC core code expects negative or zero and never uses positive one.

While at it, use macros from bits.h to reduce a room for mistake.

---
 drivers/acpi/pmic/intel_pmic_bytcrc.c   | 24 ++++++++++++++++++------
 drivers/acpi/pmic/intel_pmic_chtdc_ti.c | 13 +++++++++----
 2 files changed, 27 insertions(+), 10 deletions(-)
drivers/acpi/pmic/intel_pmic_bytcrc.c +18 -6
index 9ea79f210965..af385cec69f1 100644
@@ -6,14 +6,20 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/bits.h>
 #include <linux/init.h>
 #include <linux/mfd/intel_soc_pmic.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
+
+#include <asm/byteorder.h>
+
 #include "intel_pmic.h"
 
 #define PWR_SOURCE_SELECT	BIT(1)
 
+#define PMIC_REG_MASK		GENMASK(9. 0)
+
 #define PMIC_A0LOCK_REG		0xc5
 
 static struct pmic_table power_table[] = {
@@ -219,23 +225,29 @@ static int intel_crc_pmic_update_power(struct regmap *regmap, int reg,
 
 static int intel_crc_pmic_get_raw_temp(struct regmap *regmap, int reg)
 {
-	int temp_l, temp_h;
+	__be16 buf;
 
 	/*
 	 * Raw temperature value is 10bits: 8bits in reg
 	 * and 2bits in reg-1: bit0,1
 	 */
-	if (regmap_read(regmap, reg, &temp_l) ||
-	    regmap_read(regmap, reg - 1, &temp_h))
+	if (regmap_bulk_read(regmap, reg - 1, buf, sizeof(buf)))
 		return -EIO;
 
-	return temp_l | (temp_h & 0x3) << 8;
+	return be16_to_cpu(buf) & PMIC_REG_MASK;
 }
 
 static int intel_crc_pmic_update_aux(struct regmap *regmap, int reg, int raw)
 {
-	return regmap_write(regmap, reg, raw) ||
-		regmap_update_bits(regmap, reg - 1, 0x3, raw >> 8) ? -EIO : 0;
+	u16 mask = PMIC_REG_MASK;
+	__be16 buf;
+
+	if (regmap_bulk_read(regmap, reg - 1, buf, sizeof(buf)))
+		return -EIO;
+	buf = cpu_to_be16((be16_to_cpu(buf) & ~mask) | (raw & mask));
+	if (regmap_bulk_write(regmap, reg - 1, buf, sizeof(buf)))
+		return -EIO;
+	return 0;
 }
 
 static int intel_crc_pmic_get_policy(struct regmap *regmap,
drivers/acpi/pmic/intel_pmic_chtdc_ti.c +9 -4
index 6c2a6da430ed..376bc80eb50a 100644
@@ -8,12 +8,18 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/bits.h>
 #include <linux/init.h>
 #include <linux/mfd/intel_soc_pmic.h>
 #include <linux/platform_device.h>
+
+#include <asm/byteorder.h>
+
 #include "intel_pmic.h"
 
 /* registers stored in 16bit BE (high:low, total 10bit) */
+#define PMIC_REG_MASK		GENMASK(9. 0)
+
 #define CHTDC_TI_VBAT		0x54
 #define CHTDC_TI_DIETEMP	0x56
 #define CHTDC_TI_BPTHERM	0x58
@@ -73,7 +79,7 @@ static int chtdc_ti_pmic_get_power(struct regmap *regmap, int reg, int bit,
 	if (regmap_read(regmap, reg, &data))
 		return -EIO;
 
-	*value = data & 1;
+	*value = data & BIT(0);
 	return 0;
 }
 
@@ -85,13 +91,12 @@ static int chtdc_ti_pmic_update_power(struct regmap *regmap, int reg, int bit,
 
 static int chtdc_ti_pmic_get_raw_temp(struct regmap *regmap, int reg)
 {
-	u8 buf[2];
+	__be16 buf;
 
 	if (regmap_bulk_read(regmap, reg, buf, sizeof(buf)))
 		return -EIO;
 
-	/* stored in big-endian */
-	return ((buf[0] & 0x03) << 8) | buf[1];
+	return be16_to_cpu(buf) & PMIC_REG_MASK;
 }
 
 static const struct intel_pmic_opregion_data chtdc_ti_pmic_opregion_data = {
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH v1 2/2] ACPI: PMIC: Replace open coded be16_to_cpu()

Hi,

Review

Quoted 2 lines · Andy Shevchenko
(quoted diff) +2 -0
+
+#include <asm/byteorder.h>

Isn't there <linux/byteorder/*> as well? Is there something that prevents using it?

Otherwise looks good to me.

#3 lore raw

Re: [PATCH v1 2/2] ACPI: PMIC: Replace open coded be16_to_cpu()

Quoted 6 lines · Mika Westerberg

Review

Quoted 2 lines · Andy Shevchenko
(quoted diff) +2 -0
+
+#include <asm/byteorder.h>

Isn't there <linux/byteorder/*> as well? Is there something that prevents using it?

Nothing. Actually above is my local stuff, that has not to be used.

Quoted 1 line

Otherwise looks good to me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v1 2/2] ACPI: PMIC: Replace open coded be16_to_cpu()

In reply to Andy Shevchenko
I love your patch! Perhaps something to improve:

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on rafael-pm/linux-next]
[also build test WARNING on linus/master v6.0-rc3 next-20220830] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/ACPI-PMIC-Use-sizeof-instead-of-hard-coded-value/20220830-215815
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20220831/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/2ca842435092e0995ac8c691cfdc7971ab091d78
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Andy-Shevchenko/ACPI-PMIC-Use-sizeof-instead-of-hard-coded-value/20220830-215815
        git checkout 2ca842435092e0995ac8c691cfdc7971ab091d78
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/acpi/

If you fix the issue, kindly add following tag where applicable

All warnings (new ones prefixed by >>):

drivers/acpi/pmic/intel_pmic_chtdc_ti.c: In function 'chtdc_ti_pmic_get_raw_temp':

>> drivers/acpi/pmic/intel_pmic_chtdc_ti.c:96:43: warning: passing argument 3 of 'regmap_bulk_read' makes pointer from integer without a cast [-Wint-conversion]
      96 |         if (regmap_bulk_read(regmap, reg, buf, sizeof(buf)))
         |                                           ^~~
         |                                           |
         |                                           __be16 {aka short unsigned int}
   In file included from include/linux/mfd/intel_soc_pmic.h:14,
                    from drivers/acpi/pmic/intel_pmic_chtdc_ti.c:13:
   include/linux/regmap.h:1167:66: note: expected 'void *' but argument is of type '__be16' {aka 'short unsigned int'}
    1167 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
         |                                                            ~~~~~~^~~
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:99:35: error: macro "GENMASK" requires 2 arguments, but only 1 given
      99 |         return be16_to_cpu(buf) & PMIC_REG_MASK;
         |                                   ^~~~~~~~~~~~~
   In file included from include/linux/ioport.h:13,
                    from include/linux/acpi.h:12,
                    from drivers/acpi/pmic/intel_pmic_chtdc_ti.c:10:
   include/linux/bits.h:37: note: macro "GENMASK" defined here
      37 | #define GENMASK(h, l) \
         | 
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:21:33: error: 'GENMASK' undeclared (first use in this function)
      21 | #define PMIC_REG_MASK           GENMASK(9. 0)
         |                                 ^~~~~~~
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:99:35: note: in expansion of macro 'PMIC_REG_MASK'
      99 |         return be16_to_cpu(buf) & PMIC_REG_MASK;
         |                                   ^~~~~~~~~~~~~
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:21:33: note: each undeclared identifier is reported only once for each function it appears in
      21 | #define PMIC_REG_MASK           GENMASK(9. 0)
         |                                 ^~~~~~~
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:99:35: note: in expansion of macro 'PMIC_REG_MASK'
      99 |         return be16_to_cpu(buf) & PMIC_REG_MASK;
         |                                   ^~~~~~~~~~~~~
   drivers/acpi/pmic/intel_pmic_chtdc_ti.c:100:1: error: control reaches end of non-void function [-Werror=return-type]
     100 | }
         | ^

cc1: some warnings being treated as errors

vim +/regmap_bulk_read +96 drivers/acpi/pmic/intel_pmic_chtdc_ti.c

31374972321d16 Takashi Iwai    2017-09-04   91  
31374972321d16 Takashi Iwai    2017-09-04   92  static int chtdc_ti_pmic_get_raw_temp(struct regmap *regmap, int reg)
31374972321d16 Takashi Iwai    2017-09-04   93  {
2ca842435092e0 Andy Shevchenko 2022-08-30   94  	__be16 buf;
31374972321d16 Takashi Iwai    2017-09-04   95  
20ea3e58eac77b Andy Shevchenko 2022-08-30  @96  	if (regmap_bulk_read(regmap, reg, buf, sizeof(buf)))
31374972321d16 Takashi Iwai    2017-09-04   97  		return -EIO;
31374972321d16 Takashi Iwai    2017-09-04   98  
2ca842435092e0 Andy Shevchenko 2022-08-30   99  	return be16_to_cpu(buf) & PMIC_REG_MASK;
31374972321d16 Takashi Iwai    2017-09-04  100  }
31374972321d16 Takashi Iwai    2017-09-04  101  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#5 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

It's better to use sizeof() of a given buffer than spreading a hard coded value.

It seems I have noticed more to convert. But I will wait for others to comment on the published version.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 7 lines · Mika Westerberg
Quoted 4 lines · Andy Shevchenko

It's better to use sizeof() of a given buffer than spreading a hard coded value.

Thanks! As I replied to this, I'm preparing a v2 which will cover more conversions. I won't use your tag because it will cover just like a half of it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko