Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-20 Thread Alexandre Belloni
On 20/02/2018 at 11:43:47 -0800, David Daney wrote:
> On 02/20/2018 03:03 AM, Alexandre Belloni wrote:
> [...]
> 
> 
> > > diff --git a/drivers/rtc/rtc-isl12026.c b/drivers/rtc/rtc-isl12026.c
> > > new file mode 100644
> > > index ..29e5bdf96c67
> > > --- /dev/null
> > > +++ b/drivers/rtc/rtc-isl12026.c
> > > @@ -0,0 +1,529 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +/*
> > > + * An I2C driver for the Intersil ISL 12026
> > > + *
> > > + * Copyright (c) 2018 Cavium, Inc.
> > > + */
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +
> > > +/* register offsets */
> > > +#define ISL12026_REG_PWR 0x14
> > > +# define ISL12026_REG_PWR_BSWBIT(6)
> > > +# define ISL12026_REG_PWR_SBIB   BIT(7)
> > > +#define ISL12026_REG_SC  0x30
> > > +#define ISL12026_REG_HR  0x32
> > > +# define ISL12026_REG_HR_MIL BIT(7)  /* military or 24 hour time */
> > > +#define ISL12026_REG_SR  0x3f
> > > +# define ISL12026_REG_SR_RTCFBIT(0)
> > > +# define ISL12026_REG_SR_WEL BIT(1)
> > > +# define ISL12026_REG_SR_RWELBIT(2)
> > > +# define ISL12026_REG_SR_MBZ BIT(3)
> > > +# define ISL12026_REG_SR_OSCFBIT(4)
> > > +
> > > +/* The EEPROM array responds at i2c address 0x57 */
> > > +#define ISL12026_EEPROM_ADDR 0x57
> > > +
> > > +#define ISL12026_PAGESIZE 16
> > > +#define ISL12026_NVMEM_WRITE_TIME 20
> > > +
> > > +struct isl12026 {
> > > + struct rtc_device *rtc;
> > > + struct i2c_client *nvm_client;
> > > + struct nvmem_config nvm_cfg;
> > > + /*
> > > +  * RTC write operations require that multiple messages be
> > > +  * transmitted, we hold the lock for all accesses to the
> > > +  * device so that these sequences cannot be disrupted.  Also,
> > > +  * the write cycle to the nvmem takes many ms during which the
> > > +  * device does not respond to commands, so holding the lock
> > > +  * also prevents access during these times.
> > > +  */
> > > + struct mutex lock;
> > > +};
> > > +
> > > +static int isl12026_read_reg(struct i2c_client *client, int reg)
> > > +{
> > > + struct isl12026 *priv = i2c_get_clientdata(client);
> > > + u8 addr[] = {0, reg};
> > > + u8 val;
> > > + int ret;
> > > +
> > > + struct i2c_msg msgs[] = {
> > > + {
> > > + .addr   = client->addr,
> > > + .flags  = 0,
> > > + .len= sizeof(addr),
> > > + .buf= addr
> > > + }, {
> > > + .addr   = client->addr,
> > > + .flags  = I2C_M_RD,
> > > + .len= 1,
> > > + .buf= &val
> > > + }
> > > + };
> > 
> > I'm pretty sure you can use regmap instead of open coding all the i2c
> > transfers, did you try?
> 
> I couldn't figure out how to make it do the device-atomic stores to SR.RWEL
> and SR.WEL that must precede certain register store operations. Also,
> dealing with locking across multiple i2c target addresses seems
> problematical with the regmap helpers.
> 
> The open coding doesn't clutter things up too much, and allows us to follow
> the isl12026 protocol without having to jump through too many hoops.
> 
> > 
> > > +
> > > + mutex_lock(&priv->lock);
> > > +
> > 
> > Also, regmap will remove the need for that lock.
> 
> Since
> 
> 
> > 
> > > + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
> > > + if (ret != ARRAY_SIZE(msgs)) {
> > > + dev_err(&client->dev, "read reg error, ret=%d\n", ret);
> > > + ret = ret < 0 ? ret : -EIO;
> > > + } else {
> > > + ret = val;
> > > + }
> > > +
> > > + mutex_unlock(&priv->lock);
> > > +
> > > + return ret;
> > > +}
> > > +
> > > +static int isl12026_write_reg(struct i2c_client *client, int reg, u8 val)
> > > +{
> > > + struct isl12026 *priv = i2c_get_clientdata(client);
> > > + int ret;
> > > + u8 op[3];
> > > + struct i2c_msg msg = {
> > > + .addr   = client->addr,
> > > + .flags  = 0,
> > > + .len= 1,
> > > + .buf= op
> > > + };
> > > +
> > > + mutex_lock(&priv->lock);
> > > +
> > > + /* Set SR.WEL */
> > > + op[0] = 0;
> > > + op[1] = ISL12026_REG_SR;
> > > + op[2] = ISL12026_REG_SR_WEL;
> > > + msg.len = 3;
> > > + ret = i2c_transfer(client->adapter, &msg, 1);
> > > + if (ret != 1) {
> > > + dev_err(&client->dev, "write error SR.WEL, ret=%d\n", ret);
> > > + ret = ret < 0 ? ret : -EIO;
> > > + goto out;
> > > + }
> > 
> > If you don't clear SR.WEL, I don't think you need to set it each time
> > you write to the RTC. I would just set SR.WEL at probe time and let it
> > there. That removes two i2c writes for each write operation.
> 
> I don't like the idea of leaving the thing partially armed when write
> operations should be rare.
> 

Ok, then, can you at least factorize the write enabling/disabling in two
functions. That would make the code smaller.

> > > + 

Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-20 Thread David Daney

On 02/20/2018 03:03 AM, Alexandre Belloni wrote:
[...]



diff --git a/drivers/rtc/rtc-isl12026.c b/drivers/rtc/rtc-isl12026.c
new file mode 100644
index ..29e5bdf96c67
--- /dev/null
+++ b/drivers/rtc/rtc-isl12026.c
@@ -0,0 +1,529 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * An I2C driver for the Intersil ISL 12026
+ *
+ * Copyright (c) 2018 Cavium, Inc.
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* register offsets */
+#define ISL12026_REG_PWR   0x14
+# define ISL12026_REG_PWR_BSW  BIT(6)
+# define ISL12026_REG_PWR_SBIB BIT(7)
+#define ISL12026_REG_SC0x30
+#define ISL12026_REG_HR0x32
+# define ISL12026_REG_HR_MIL   BIT(7)  /* military or 24 hour time */
+#define ISL12026_REG_SR0x3f
+# define ISL12026_REG_SR_RTCF  BIT(0)
+# define ISL12026_REG_SR_WEL   BIT(1)
+# define ISL12026_REG_SR_RWEL  BIT(2)
+# define ISL12026_REG_SR_MBZ   BIT(3)
+# define ISL12026_REG_SR_OSCF  BIT(4)
+
+/* The EEPROM array responds at i2c address 0x57 */
+#define ISL12026_EEPROM_ADDR   0x57
+
+#define ISL12026_PAGESIZE 16
+#define ISL12026_NVMEM_WRITE_TIME 20
+
+struct isl12026 {
+   struct rtc_device *rtc;
+   struct i2c_client *nvm_client;
+   struct nvmem_config nvm_cfg;
+   /*
+* RTC write operations require that multiple messages be
+* transmitted, we hold the lock for all accesses to the
+* device so that these sequences cannot be disrupted.  Also,
+* the write cycle to the nvmem takes many ms during which the
+* device does not respond to commands, so holding the lock
+* also prevents access during these times.
+*/
+   struct mutex lock;
+};
+
+static int isl12026_read_reg(struct i2c_client *client, int reg)
+{
+   struct isl12026 *priv = i2c_get_clientdata(client);
+   u8 addr[] = {0, reg};
+   u8 val;
+   int ret;
+
+   struct i2c_msg msgs[] = {
+   {
+   .addr   = client->addr,
+   .flags  = 0,
+   .len= sizeof(addr),
+   .buf= addr
+   }, {
+   .addr   = client->addr,
+   .flags  = I2C_M_RD,
+   .len= 1,
+   .buf= &val
+   }
+   };


I'm pretty sure you can use regmap instead of open coding all the i2c
transfers, did you try?


I couldn't figure out how to make it do the device-atomic stores to 
SR.RWEL and SR.WEL that must precede certain register store operations. 
Also, dealing with locking across multiple i2c target addresses seems 
problematical with the regmap helpers.


The open coding doesn't clutter things up too much, and allows us to 
follow the isl12026 protocol without having to jump through too many hoops.





+
+   mutex_lock(&priv->lock);
+


Also, regmap will remove the need for that lock.


Since





+   ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+   if (ret != ARRAY_SIZE(msgs)) {
+   dev_err(&client->dev, "read reg error, ret=%d\n", ret);
+   ret = ret < 0 ? ret : -EIO;
+   } else {
+   ret = val;
+   }
+
+   mutex_unlock(&priv->lock);
+
+   return ret;
+}
+
+static int isl12026_write_reg(struct i2c_client *client, int reg, u8 val)
+{
+   struct isl12026 *priv = i2c_get_clientdata(client);
+   int ret;
+   u8 op[3];
+   struct i2c_msg msg = {
+   .addr   = client->addr,
+   .flags  = 0,
+   .len= 1,
+   .buf= op
+   };
+
+   mutex_lock(&priv->lock);
+
+   /* Set SR.WEL */
+   op[0] = 0;
+   op[1] = ISL12026_REG_SR;
+   op[2] = ISL12026_REG_SR_WEL;
+   msg.len = 3;
+   ret = i2c_transfer(client->adapter, &msg, 1);
+   if (ret != 1) {
+   dev_err(&client->dev, "write error SR.WEL, ret=%d\n", ret);
+   ret = ret < 0 ? ret : -EIO;
+   goto out;
+   }


If you don't clear SR.WEL, I don't think you need to set it each time
you write to the RTC. I would just set SR.WEL at probe time and let it
there. That removes two i2c writes for each write operation.


I don't like the idea of leaving the thing partially armed when write 
operations should be rare.



[...]

+static int isl12026_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+   struct i2c_client *client = to_i2c_client(dev);
+   struct isl12026 *priv = i2c_get_clientdata(client);
+   u8 ccr[8];
+   u8 addr[2];
+   u8 sr;
+   int ret;
+   struct i2c_msg msgs[] = {
+   {
+   .addr   = client->addr,
+   .flags  = 0,
+   .len= sizeof(addr),
+   .buf= addr
+   }, {
+   .addr   = client->addr,
+   .fl

Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-20 Thread Alexandre Belloni
Hi,

On 16/02/2018 at 11:44:15 -0800, David Daney wrote:
> The ISL12026 is a combination RTC and EEPROM device with I2C
> interface.  The standard RTC driver interface is provided.  The EEPROM
> is accessed via the NVMEM interface via the "eeprom0" directory in the
> sysfs entry for the device.
> 
> Reviewed-by: Andy Shevchenko 
> Signed-off-by: David Daney 
> ---
> Changes from v3:
> 
> o Add Reviewed-by
> 
> o s/dev_err/dev_warn/ in one place
> 
> o Remove redundant ','
> 
> Changes from v2:
> 
> o More code cleanups suggested by reviewers.
> 
> Changes from v1:
> 
> o Fixed device tree bindings document example.
> 
> o Use RTC_NVMEM facility for eeprom support.
> 
> o Small code cleanups suggested by reviewers.
> 
>  .../devicetree/bindings/rtc/isil,isl12026.txt  |  28 ++
>  drivers/rtc/Kconfig|   9 +
>  drivers/rtc/Makefile   |   1 +
>  drivers/rtc/rtc-isl12026.c | 529 
> +
>  4 files changed, 567 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/rtc/isil,isl12026.txt
>  create mode 100644 drivers/rtc/rtc-isl12026.c
> 
> diff --git a/Documentation/devicetree/bindings/rtc/isil,isl12026.txt 
> b/Documentation/devicetree/bindings/rtc/isil,isl12026.txt
> new file mode 100644
> index ..2e0be45193bb
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/rtc/isil,isl12026.txt
> @@ -0,0 +1,28 @@
> +ISL12026 I2C RTC/EEPROM
> +
> +ISL12026 is an I2C RTC/EEPROM combination device.  The RTC and control
> +registers respond at bus address 0x6f, and the EEPROM array responds
> +at bus address 0x57.  The canonical "reg" value will be for the RTC portion.
> +
> +Required properties supported by the device:
> +
> + - "compatible": must be "isil,isl12026"
> + - "reg": I2C bus address of the device (always 0x6f)
> +
> +Optional properties:
> +
> + - "isil,pwr-bsw": If present PWR.BSW bit must be set to the specified
> +   value for proper operation.
> +
> + - "isil,pwr-sbib": If present PWR.SBIB bit must be set to the specified
> +value for proper operation.
> +
> +
> +Example:
> +
> + rtc@6f {
> + compatible = "isil,isl12026";
> + reg = <0x6f>;
> + isil,pwr-bsw = <0>;
> + isil,pwr-sbib = <1>;
> + }
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index 8ab5f0a5d323..85171e9e3ada 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -407,6 +407,15 @@ config RTC_DRV_ISL12022
> This driver can also be built as a module. If so, the module
> will be called rtc-isl12022.
>  
> +config RTC_DRV_ISL12026
> + tristate "Intersil ISL12026"
> + help
> +   If you say yes here you get support for the
> +   Intersil ISL12026 RTC chip.
> +
> +   This driver can also be built as a module. If so, the module
> +   will be called rtc-isl12026.
> +
>  config RTC_DRV_X1205
>   tristate "Xicor/Intersil X1205"
>   help
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index 4fbf87e45a7c..f481661a6eae 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -76,6 +76,7 @@ obj-$(CONFIG_RTC_DRV_HID_SENSOR_TIME) += 
> rtc-hid-sensor-time.o
>  obj-$(CONFIG_RTC_DRV_HYM8563)+= rtc-hym8563.o
>  obj-$(CONFIG_RTC_DRV_IMXDI)  += rtc-imxdi.o
>  obj-$(CONFIG_RTC_DRV_ISL12022)   += rtc-isl12022.o
> +obj-$(CONFIG_RTC_DRV_ISL12026)   += rtc-isl12026.o
>  obj-$(CONFIG_RTC_DRV_ISL1208)+= rtc-isl1208.o
>  obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
>  obj-$(CONFIG_RTC_DRV_LP8788) += rtc-lp8788.o
> diff --git a/drivers/rtc/rtc-isl12026.c b/drivers/rtc/rtc-isl12026.c
> new file mode 100644
> index ..29e5bdf96c67
> --- /dev/null
> +++ b/drivers/rtc/rtc-isl12026.c
> @@ -0,0 +1,529 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * An I2C driver for the Intersil ISL 12026
> + *
> + * Copyright (c) 2018 Cavium, Inc.
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +/* register offsets */
> +#define ISL12026_REG_PWR 0x14
> +# define ISL12026_REG_PWR_BSWBIT(6)
> +# define ISL12026_REG_PWR_SBIB   BIT(7)
> +#define ISL12026_REG_SC  0x30
> +#define ISL12026_REG_HR  0x32
> +# define ISL12026_REG_HR_MIL BIT(7)  /* military or 24 hour time */
> +#define ISL12026_REG_SR  0x3f
> +# define ISL12026_REG_SR_RTCFBIT(0)
> +# define ISL12026_REG_SR_WEL BIT(1)
> +# define ISL12026_REG_SR_RWELBIT(2)
> +# define ISL12026_REG_SR_MBZ BIT(3)
> +# define ISL12026_REG_SR_OSCFBIT(4)
> +
> +/* The EEPROM array responds at i2c address 0x57 */
> +#define ISL12026_EEPROM_ADDR 0x57
> +
> +#define ISL12026_PAGESIZE 16
> +#define ISL12026_NVMEM_WRITE_TIME 20
> +
> +struct isl12026 {
> + struct rtc_device *rtc;
> + struct i2c_client *nvm_client;
> + struct nv

Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-19 Thread Rob Herring
On Fri, Feb 16, 2018 at 11:44:15AM -0800, David Daney wrote:
> The ISL12026 is a combination RTC and EEPROM device with I2C
> interface.  The standard RTC driver interface is provided.  The EEPROM
> is accessed via the NVMEM interface via the "eeprom0" directory in the
> sysfs entry for the device.
> 
> Reviewed-by: Andy Shevchenko 
> Signed-off-by: David Daney 
> ---
> Changes from v3:
> 
> o Add Reviewed-by
> 
> o s/dev_err/dev_warn/ in one place
> 
> o Remove redundant ','
> 
> Changes from v2:
> 
> o More code cleanups suggested by reviewers.
> 
> Changes from v1:
> 
> o Fixed device tree bindings document example.
> 
> o Use RTC_NVMEM facility for eeprom support.
> 
> o Small code cleanups suggested by reviewers.
> 
>  .../devicetree/bindings/rtc/isil,isl12026.txt  |  28 ++

Reviewed-by: Rob Herring 

>  drivers/rtc/Kconfig|   9 +
>  drivers/rtc/Makefile   |   1 +
>  drivers/rtc/rtc-isl12026.c | 529 
> +
>  4 files changed, 567 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/rtc/isil,isl12026.txt
>  create mode 100644 drivers/rtc/rtc-isl12026.c


Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-18 Thread Philippe Ombredanne
Hi David,

On Fri, Feb 16, 2018 at 8:44 PM, David Daney  wrote:
> The ISL12026 is a combination RTC and EEPROM device with I2C
> interface.  The standard RTC driver interface is provided.  The EEPROM
> is accessed via the NVMEM interface via the "eeprom0" directory in the
> sysfs entry for the device.
>
> Reviewed-by: Andy Shevchenko 
> Signed-off-by: David Daney 



> --- /dev/null
> +++ b/drivers/rtc/rtc-isl12026.c
> @@ -0,0 +1,529 @@
> +// SPDX-License-Identifier: GPL-2.0



> +MODULE_LICENSE("GPL");

Your MODULE_LICENSE does not match your SPDX tag.
Per module.h, GPL would mean GPL-2.0+ not GPL-2.0
It would be best if you can sync the two.

-- 
Cordially
Philippe Ombredanne


Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-16 Thread Andy Shevchenko
On Fri, Feb 16, 2018 at 11:19 PM, David Daney  wrote:
> On 02/16/2018 12:13 PM, Andy Shevchenko wrote:
>> On Fri, Feb 16, 2018 at 9:44 PM, David Daney 
>> wrote:

>>> +config RTC_DRV_ISL12026
>>> +   tristate "Intersil ISL12026"

>> depends on OF

> It doesn't depend on CONFIG_OF, it builds just fine without it.

OK.
But then it's useless.

depends on OF || COMPILE_TEST

>>> +static struct i2c_driver isl12026_driver = {
>>> +   .driver = {
>>> +   .name   = "rtc-isl12026",
>>> +   .of_match_table = of_match_ptr(isl12026_dt_match),

>> /of_match_ptr//

> of_match_ptr() needed if we build without CONFIG_OF

OK.
Have you tried to enable warnings?
You must get `defined but not used` one.

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-16 Thread David Daney

On 02/16/2018 12:13 PM, Andy Shevchenko wrote:

On Fri, Feb 16, 2018 at 9:44 PM, David Daney  wrote:

The ISL12026 is a combination RTC and EEPROM device with I2C
interface.  The standard RTC driver interface is provided.  The EEPROM
is accessed via the NVMEM interface via the "eeprom0" directory in the
sysfs entry for the device.



+config RTC_DRV_ISL12026
+   tristate "Intersil ISL12026"


depends on OF


It doesn't depend on CONFIG_OF, it builds just fine without it.




+static struct i2c_driver isl12026_driver = {
+   .driver = {
+   .name   = "rtc-isl12026",
+   .of_match_table = of_match_ptr(isl12026_dt_match),


/of_match_ptr//



of_match_ptr() needed if we build without CONFIG_OF




+   },
+   .probe_new  = isl12026_probe_new,
+   .remove = isl12026_remove,
+};






Re: [PATCH v4] rtc: isl12026: Add driver.

2018-02-16 Thread Andy Shevchenko
On Fri, Feb 16, 2018 at 9:44 PM, David Daney  wrote:
> The ISL12026 is a combination RTC and EEPROM device with I2C
> interface.  The standard RTC driver interface is provided.  The EEPROM
> is accessed via the NVMEM interface via the "eeprom0" directory in the
> sysfs entry for the device.

> +config RTC_DRV_ISL12026
> +   tristate "Intersil ISL12026"

depends on OF

> +static struct i2c_driver isl12026_driver = {
> +   .driver = {
> +   .name   = "rtc-isl12026",
> +   .of_match_table = of_match_ptr(isl12026_dt_match),

/of_match_ptr//

> +   },
> +   .probe_new  = isl12026_probe_new,
> +   .remove = isl12026_remove,
> +};

-- 
With Best Regards,
Andy Shevchenko