2022-12-01 23:11:25

by Sean Anderson

[permalink] [raw]
Subject: [PATCH] rtc: abx80x: Add nvmem support

This adds support for the 256-byte internal RAM. There are two windows
which can be used to access this RAM: 64 bytes at 0x40 (the "standard"
address space) and 128 bytes at 0x80 (the "alternate" address space). We
use the standard address space because it is also accessible over SPI
(if such a port is ever done). We are limited to 32-byte reads for SMBus
compatibility, so there's no advantage to using the alternate address
space.

There are some reserved bits in the EXTRAM register, and the datasheet
doesn't say what to do with them. I've opted to skip a read/modify/write
and just write the whole thing. If this driver is ever converted to
regmap, this would be a good place to use regmap_update_bits.

Signed-off-by: Sean Anderson <[email protected]>
---

drivers/rtc/rtc-abx80x.c | 86 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)

diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
index 9b0138d07232..6e4fadbe7a80 100644
--- a/drivers/rtc/rtc-abx80x.c
+++ b/drivers/rtc/rtc-abx80x.c
@@ -87,6 +87,16 @@
#define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
#define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4

+#define ABX8XX_REG_EXTRAM 0x3f
+#define ABX8XX_EXTRAM_XADS GENMASK(1, 0)
+
+#define ABX8XX_SRAM_BASE 0x40
+#define ABX8XX_SRAM_WIN_SIZE 0x40
+#define ABX8XX_RAM_SIZE 256
+
+#define NVMEM_ADDR_LOWER GENMASK(5, 0)
+#define NVMEM_ADDR_UPPER GENMASK(7, 6)
+
static u8 trickle_resistors[] = {0, 3, 6, 11};

enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
@@ -673,6 +683,78 @@ static int abx80x_setup_watchdog(struct abx80x_priv *priv)
}
#endif

+#ifdef CONFIG_NVMEM
+static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
+ void *val, size_t bytes, bool write)
+{
+ int ret;
+
+ while (bytes) {
+ u8 extram, reg, len, lower, upper;
+
+ lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
+ upper = FIELD_GET(NVMEM_ADDR_UPPER, offset);
+ extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
+ reg = ABX8XX_SRAM_BASE + lower;
+ len = min(lower + bytes, (size_t)ABX8XX_SRAM_WIN_SIZE) - lower;
+ len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
+
+ ret = i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_EXTRAM,
+ extram);
+ if (ret)
+ return ret;
+
+ if (write)
+ ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
+ len, val);
+ else
+ ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
+ len, val);
+ if (ret)
+ return ret;
+
+ offset += len;
+ val += len;
+ bytes -= len;
+ }
+
+ return 0;
+}
+
+static int abx80x_nvmem_read(void *priv, unsigned int offset, void *val,
+ size_t bytes)
+{
+ return abx80x_nvmem_xfer(priv, offset, val, bytes, false);
+}
+
+static int abx80x_nvmem_write(void *priv, unsigned int offset, void *val,
+ size_t bytes)
+{
+ return abx80x_nvmem_xfer(priv, offset, val, bytes, true);
+}
+
+static int abx80x_setup_nvmem(struct abx80x_priv *priv)
+{
+ struct device *dev = &priv->client->dev;
+ struct nvmem_config config = {
+ .dev = dev,
+ .type = NVMEM_TYPE_BATTERY_BACKED,
+ .reg_read = abx80x_nvmem_read,
+ .reg_write = abx80x_nvmem_write,
+ .size = ABX8XX_RAM_SIZE,
+ .priv = priv,
+ };
+
+ return PTR_ERR_OR_ZERO(devm_nvmem_register(&priv->client->dev,
+ &config));
+}
+#else
+static int abx80x_setup_nvmem(struct abx80x_priv *priv)
+{
+ return 0;
+}
+#endif
+
static int abx80x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -824,6 +906,10 @@ static int abx80x_probe(struct i2c_client *client,
return err;
}

+ err = abx80x_setup_nvmem(priv);
+ if (err)
+ return err;
+
if (client->irq > 0) {
dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
--
2.35.1.1320.gc452695387.dirty


2022-12-03 08:55:54

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] rtc: abx80x: Add nvmem support

Hi Sean,

I love your patch! Yet something to improve:

[auto build test ERROR on v6.1-rc7]
[also build test ERROR on linus/master]
[cannot apply to abelloni/rtc-next next-20221202]
[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/Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
patch link: https://lore.kernel.org/r/20221201230208.713118-1-sean.anderson%40seco.com
patch subject: [PATCH] rtc: abx80x: Add nvmem support
config: x86_64-randconfig-a014
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
git checkout 6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> drivers/rtc/rtc-abx80x.c:695:11: error: implicit declaration of function 'FIELD_GET' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
^
>> drivers/rtc/rtc-abx80x.c:697:12: error: implicit declaration of function 'FIELD_PREP' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
^
drivers/rtc/rtc-abx80x.c:697:12: note: did you mean 'FIELD_GET'?
drivers/rtc/rtc-abx80x.c:695:11: note: 'FIELD_GET' declared here
lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
^
2 errors generated.


vim +/FIELD_GET +695 drivers/rtc/rtc-abx80x.c

685
686 #ifdef CONFIG_NVMEM
687 static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
688 void *val, size_t bytes, bool write)
689 {
690 int ret;
691
692 while (bytes) {
693 u8 extram, reg, len, lower, upper;
694
> 695 lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
696 upper = FIELD_GET(NVMEM_ADDR_UPPER, offset);
> 697 extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
698 reg = ABX8XX_SRAM_BASE + lower;
699 len = min(lower + bytes, (size_t)ABX8XX_SRAM_WIN_SIZE) - lower;
700 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
701
702 ret = i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_EXTRAM,
703 extram);
704 if (ret)
705 return ret;
706
707 if (write)
708 ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
709 len, val);
710 else
711 ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
712 len, val);
713 if (ret)
714 return ret;
715
716 offset += len;
717 val += len;
718 bytes -= len;
719 }
720
721 return 0;
722 }
723

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (3.65 kB)
config (169.54 kB)
Download all attachments

2022-12-03 09:53:31

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] rtc: abx80x: Add nvmem support

Hi Sean,

I love your patch! Yet something to improve:

[auto build test ERROR on v6.1-rc7]
[also build test ERROR on linus/master]
[cannot apply to abelloni/rtc-next next-20221202]
[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/Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
patch link: https://lore.kernel.org/r/20221201230208.713118-1-sean.anderson%40seco.com
patch subject: [PATCH] rtc: abx80x: Add nvmem support
config: ia64-randconfig-r035-20221201
compiler: ia64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
git checkout 6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=ia64 SHELL=/bin/bash drivers/rtc/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

drivers/rtc/rtc-abx80x.c: In function 'abx80x_nvmem_xfer':
>> drivers/rtc/rtc-abx80x.c:695:25: error: implicit declaration of function 'FIELD_GET'; did you mean 'FOLL_GET'? [-Werror=implicit-function-declaration]
695 | lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
| ^~~~~~~~~
| FOLL_GET
>> drivers/rtc/rtc-abx80x.c:697:26: error: implicit declaration of function 'FIELD_PREP' [-Werror=implicit-function-declaration]
697 | extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
| ^~~~~~~~~~
cc1: some warnings being treated as errors


vim +695 drivers/rtc/rtc-abx80x.c

685
686 #ifdef CONFIG_NVMEM
687 static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
688 void *val, size_t bytes, bool write)
689 {
690 int ret;
691
692 while (bytes) {
693 u8 extram, reg, len, lower, upper;
694
> 695 lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
696 upper = FIELD_GET(NVMEM_ADDR_UPPER, offset);
> 697 extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
698 reg = ABX8XX_SRAM_BASE + lower;
699 len = min(lower + bytes, (size_t)ABX8XX_SRAM_WIN_SIZE) - lower;
700 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
701
702 ret = i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_EXTRAM,
703 extram);
704 if (ret)
705 return ret;
706
707 if (write)
708 ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
709 len, val);
710 else
711 ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
712 len, val);
713 if (ret)
714 return ret;
715
716 offset += len;
717 val += len;
718 bytes -= len;
719 }
720
721 return 0;
722 }
723

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (3.52 kB)
config (168.61 kB)
Download all attachments

2022-12-05 15:21:23

by Sean Anderson

[permalink] [raw]
Subject: Re: [PATCH] rtc: abx80x: Add nvmem support

On 12/3/22 03:45, kernel test robot wrote:
> Hi Sean,
>
> I love your patch! Yet something to improve:
>
> [auto build test ERROR on v6.1-rc7]
> [also build test ERROR on linus/master]
> [cannot apply to abelloni/rtc-next next-20221202]
> [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/Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
> patch link: https://lore.kernel.org/r/20221201230208.713118-1-sean.anderson%40seco.com
> patch subject: [PATCH] rtc: abx80x: Add nvmem support
> config: x86_64-randconfig-a014
> compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
> reproduce (this is a W=1 build):
> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> chmod +x ~/bin/make.cross
> # https://github.com/intel-lab-lkp/linux/commit/6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
> git remote add linux-review https://github.com/intel-lab-lkp/linux
> git fetch --no-tags linux-review Sean-Anderson/rtc-abx80x-Add-nvmem-support/20221202-070301
> git checkout 6dbbdf7eef71cb4255c6751bc9762cf02a0f53b6
> # save the config file
> mkdir build_dir && cp config build_dir/.config
> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash
>
> If you fix the issue, kindly add following tag where applicable
> | Reported-by: kernel test robot <[email protected]>
>
> All errors (new ones prefixed by >>):
>
>>> drivers/rtc/rtc-abx80x.c:695:11: error: implicit declaration of function 'FIELD_GET' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
> lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
> ^
>>> drivers/rtc/rtc-abx80x.c:697:12: error: implicit declaration of function 'FIELD_PREP' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
> extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
> ^
> drivers/rtc/rtc-abx80x.c:697:12: note: did you mean 'FIELD_GET'?
> drivers/rtc/rtc-abx80x.c:695:11: note: 'FIELD_GET' declared here
> lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
> ^
> 2 errors generated.
>
>
> vim +/FIELD_GET +695 drivers/rtc/rtc-abx80x.c
>
> 685
> 686 #ifdef CONFIG_NVMEM
> 687 static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
> 688 void *val, size_t bytes, bool write)
> 689 {
> 690 int ret;
> 691
> 692 while (bytes) {
> 693 u8 extram, reg, len, lower, upper;
> 694
> > 695 lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
> 696 upper = FIELD_GET(NVMEM_ADDR_UPPER, offset);
> > 697 extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
> 698 reg = ABX8XX_SRAM_BASE + lower;
> 699 len = min(lower + bytes, (size_t)ABX8XX_SRAM_WIN_SIZE) - lower;
> 700 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
> 701
> 702 ret = i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_EXTRAM,
> 703 extram);
> 704 if (ret)
> 705 return ret;
> 706
> 707 if (write)
> 708 ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
> 709 len, val);
> 710 else
> 711 ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
> 712 len, val);
> 713 if (ret)
> 714 return ret;
> 715
> 716 offset += len;
> 717 val += len;
> 718 bytes -= len;
> 719 }
> 720
> 721 return 0;
> 722 }
> 723
>

Ah, the ARM64 bitfield inclusion strikes again...

--Sean