2022-12-16 17:46:16

by Sean Anderson

[permalink] [raw]
Subject: [PATCH v3] 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]>
---

Changes in v3:
- Use devm_rtc_nvmem_register
- Remove ifdefs

Changes in v2:
- Fix building on non-arm platforms

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

diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
index 9b0138d07232..3f15f75489ed 100644
--- a/drivers/rtc/rtc-abx80x.c
+++ b/drivers/rtc/rtc-abx80x.c
@@ -11,6 +11,7 @@
*/

#include <linux/bcd.h>
+#include <linux/bitfield.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of_device.h>
@@ -87,6 +88,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 +684,69 @@ static int abx80x_setup_watchdog(struct abx80x_priv *priv)
}
#endif

+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 = {
+ .type = NVMEM_TYPE_BATTERY_BACKED,
+ .reg_read = abx80x_nvmem_read,
+ .reg_write = abx80x_nvmem_write,
+ .size = ABX8XX_RAM_SIZE,
+ .priv = priv,
+ };
+
+ return devm_rtc_nvmem_register(priv->rtc, &config);
+}
+
static int abx80x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -824,6 +898,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-16 21:47:37

by kernel test robot

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

Hi Sean,

I love your patch! Perhaps something to improve:

[auto build test WARNING on v6.1]
[also build test WARNING on linus/master]
[cannot apply to abelloni/rtc-next next-20221216]
[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/20221217-011601
patch link: https://lore.kernel.org/r/20221216170643.2953920-1-sean.anderson%40seco.com
patch subject: [PATCH v3] rtc: abx80x: Add nvmem support
config: x86_64-allyesconfig
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/853a2a957b8626c0dce3f71d3055d6a83b8f5c18
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/20221217-011601
git checkout 853a2a957b8626c0dce3f71d3055d6a83b8f5c18
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=x86_64 olddefconfig
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/

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

All warnings (new ones prefixed by >>):

drivers/rtc/rtc-abx80x.c: In function 'abx80x_setup_nvmem':
>> drivers/rtc/rtc-abx80x.c:738:24: warning: unused variable 'dev' [-Wunused-variable]
738 | struct device *dev = &priv->client->dev;
| ^~~


vim +/dev +738 drivers/rtc/rtc-abx80x.c

735
736 static int abx80x_setup_nvmem(struct abx80x_priv *priv)
737 {
> 738 struct device *dev = &priv->client->dev;
739 struct nvmem_config config = {
740 .type = NVMEM_TYPE_BATTERY_BACKED,
741 .reg_read = abx80x_nvmem_read,
742 .reg_write = abx80x_nvmem_write,
743 .size = ABX8XX_RAM_SIZE,
744 .priv = priv,
745 };
746
747 return devm_rtc_nvmem_register(priv->rtc, &config);
748 }
749

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


Attachments:
(No filename) (2.25 kB)
config (297.69 kB)
Download all attachments

2022-12-16 21:47:45

by kernel test robot

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

Hi Sean,

I love your patch! Perhaps something to improve:

[auto build test WARNING on v6.1]
[also build test WARNING on linus/master]
[cannot apply to abelloni/rtc-next next-20221216]
[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/20221217-011601
patch link: https://lore.kernel.org/r/20221216170643.2953920-1-sean.anderson%40seco.com
patch subject: [PATCH v3] 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/853a2a957b8626c0dce3f71d3055d6a83b8f5c18
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/20221217-011601
git checkout 853a2a957b8626c0dce3f71d3055d6a83b8f5c18
# 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 olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/rtc/

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

All warnings (new ones prefixed by >>):

>> drivers/rtc/rtc-abx80x.c:738:17: warning: unused variable 'dev' [-Wunused-variable]
struct device *dev = &priv->client->dev;
^
1 warning generated.


vim +/dev +738 drivers/rtc/rtc-abx80x.c

735
736 static int abx80x_setup_nvmem(struct abx80x_priv *priv)
737 {
> 738 struct device *dev = &priv->client->dev;
739 struct nvmem_config config = {
740 .type = NVMEM_TYPE_BATTERY_BACKED,
741 .reg_read = abx80x_nvmem_read,
742 .reg_write = abx80x_nvmem_write,
743 .size = ABX8XX_RAM_SIZE,
744 .priv = priv,
745 };
746
747 return devm_rtc_nvmem_register(priv->rtc, &config);
748 }
749

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


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