From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: MIME-Version: 1.0 References: <20180907151744.32634-1-jgebben@sweptlaser.com> <20180908150957.GD2598@piout.net> In-Reply-To: <20180908150957.GD2598@piout.net> From: Jeremy Gebben Date: Mon, 10 Sep 2018 10:39:21 -0600 Message-ID: Subject: Re: [PATCH] rtc: abx80x: add basic watchdog support To: Alexandre Belloni Cc: Alessandro Zummo , linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: multipart/alternative; boundary="0000000000007fdd8a05758700a4" List-ID: --0000000000007fdd8a05758700a4 Content-Type: text/plain; charset="UTF-8" On Sat, Sep 8, 2018 at 9:10 AM Alexandre Belloni < alexandre.belloni@bootlin.com> wrote: > Hi, > > Please Cc the watchdog maintainers. Else, I have very few comments. > Sure, I'll add them on the next version. > On 07/09/2018 09:17:43-0600, Jeremy Gebben wrote: > > The abx804 and abx805 chips have support for a simple watchdog > > function that can trigger an external reset. > > > > Signed-off-by: Jeremy Gebben > > --- > > drivers/rtc/Kconfig | 11 +++ > > drivers/rtc/rtc-abx80x.c | 160 ++++++++++++++++++++++++++++++++++++--- > > 2 files changed, 159 insertions(+), 12 deletions(-) > > > > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig > > index 7d7be60a2413..4771e3a89721 100644 > > --- a/drivers/rtc/Kconfig > > +++ b/drivers/rtc/Kconfig > > @@ -195,6 +195,17 @@ config RTC_DRV_ABX80X > > This driver can also be built as a module. If so, the module > > will be called rtc-abx80x. > > > > +config RTC_DRV_ABX80X_WDT > > + bool "Abracon ABx80x Watchdog" > > + default y > > + depends on RTC_DRV_ABX80X > > + help > > + If you say yes here you get watchdog support for Abracon ABX804 > and ABX805 > > + clock chips. This watchdog supports timeouts up to 31 seconds > with 1 second > > + granularity. > > + > > + This watchdog device is part of the rtc-abx80x module. > > + > > I'm not sure it is worth having a separate config option for that. You > may as well just always include the watchdog support code. However, > doesn't it need to depend on CONFIG_WATCHDOG? > Yes, you're right about CONFIG_WATCHDOG, I thought I had that already but I guess it got dropped. > > config RTC_DRV_AC100 > > tristate "X-Powers AC100" > > depends on MFD_AC100 > > diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c > > index 2cefa67a1132..fa558939e944 100644 > > --- a/drivers/rtc/rtc-abx80x.c > > +++ b/drivers/rtc/rtc-abx80x.c > > @@ -17,6 +17,7 @@ > > #include > > #include > > #include > > +#include > > > > #define ABX8XX_REG_HTH 0x00 > > #define ABX8XX_REG_SC 0x01 > > @@ -37,6 +38,7 @@ > > > > #define ABX8XX_REG_STATUS 0x0f > > #define ABX8XX_STATUS_AF BIT(2) > > +#define ABX8XX_STATUS_WDT BIT(6) > > > > #define ABX8XX_REG_CTRL1 0x10 > > #define ABX8XX_CTRL_WRITE BIT(0) > > @@ -61,6 +63,14 @@ > > #define ABX8XX_OSS_OF BIT(1) > > #define ABX8XX_OSS_OMODE BIT(4) > > > > +#define ABX8XX_REG_WDT 0x1b > > +#define ABX8XX_WDT_WDS BIT(7) > > +#define ABX8XX_WDT_BMB_MASK 0x7c > > +#define ABX8XX_WDT_BMB_SHIFT 2 > > +#define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> > ABX8XX_WDT_BMB_SHIFT) > > +#define ABX8XX_WDT_WRB_MASK 0x03 > > +#define ABX8XX_WDT_WRB_1HZ 0x02 > > + > > #define ABX8XX_REG_CFG_KEY 0x1f > > #define ABX8XX_CFG_KEY_OSC 0xa1 > > #define ABX8XX_CFG_KEY_MISC 0x9d > > @@ -80,20 +90,27 @@ enum abx80x_chip {AB0801, AB0803, AB0804, AB0805, > > struct abx80x_cap { > > u16 pn; > > bool has_tc; > > + bool has_wdog; > > }; > > > > static struct abx80x_cap abx80x_caps[] = { > > [AB0801] = {.pn = 0x0801}, > > [AB0803] = {.pn = 0x0803}, > > - [AB0804] = {.pn = 0x0804, .has_tc = true}, > > - [AB0805] = {.pn = 0x0805, .has_tc = true}, > > + [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true}, > > + [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true}, > > [AB1801] = {.pn = 0x1801}, > > [AB1803] = {.pn = 0x1803}, > > - [AB1804] = {.pn = 0x1804, .has_tc = true}, > > - [AB1805] = {.pn = 0x1805, .has_tc = true}, > > + [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true}, > > + [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true}, > > [ABX80X] = {.pn = 0} > > }; > > > > +struct abx80x_priv { > > + struct rtc_device *rtc; > > + struct i2c_client *client; > > + struct watchdog_device wdog; > > +}; > > + > > To make the watchdog part easier to review, you could separate the > introduction of abx80x_priv in a preliminary patch and then introduce > the watchdog code. > Ok. > static int abx80x_is_rc_mode(struct i2c_client *client) > > { > > int flags = 0; > > @@ -218,7 +235,8 @@ static int abx80x_rtc_set_time(struct device *dev, > struct rtc_time *tm) > > static irqreturn_t abx80x_handle_irq(int irq, void *dev_id) > > { > > struct i2c_client *client = dev_id; > > - struct rtc_device *rtc = i2c_get_clientdata(client); > > + struct abx80x_priv *priv = i2c_get_clientdata(client); > > + struct rtc_device *rtc = priv->rtc; > > int status; > > > > status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); > > @@ -228,6 +246,13 @@ static irqreturn_t abx80x_handle_irq(int irq, void > *dev_id) > > if (status & ABX8XX_STATUS_AF) > > rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); > > > > + /* > > + * It is unclear if we'll get an interrupt before the external > > + * reset kicks in. > > + */ > > + if (status & ABX8XX_STATUS_WDT) > > + dev_err(&client->dev, "watchdog timeout interrupt!\n"); > > + > > dev_alert maybe? > Ok. Thanks, Jeremy > > > i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); > > > > return IRQ_HANDLED; > > @@ -529,11 +554,110 @@ static void rtc_calib_remove_sysfs_group(void > *_dev) > > sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); > > } > > > > +#ifdef CONFIG_RTC_DRV_ABX80X_WDT > > + > > +static inline u8 timeout_bits(unsigned int timeout) > > +{ > > + return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) | > > + ABX8XX_WDT_WRB_1HZ; > > +} > > + > > +static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog, > > + unsigned int timeout) > > +{ > > + struct abx80x_priv *priv = watchdog_get_drvdata(wdog); > > + u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout); > > + > > + /* > > + * Writing any timeout to the WDT register resets the watchdog > timer. > > + * Writing 0 disables it. > > + */ > > + return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, > val); > > +} > > + > > +static int abx80x_wdog_set_timeout(struct watchdog_device *wdog, > > + unsigned int new_timeout) > > +{ > > + int err = __abx80x_wdog_set_timeout(wdog, new_timeout); > > + > > + if (err) > > + return err; > > + > > + wdog->timeout = new_timeout; > > + return 0; > > +} > > + > > +static int abx80x_wdog_ping(struct watchdog_device *wdog) > > +{ > > + return __abx80x_wdog_set_timeout(wdog, wdog->timeout); > > +} > > + > > +static int abx80x_wdog_start(struct watchdog_device *wdog) > > +{ > > + int err = __abx80x_wdog_set_timeout(wdog, wdog->timeout); > > + > > + if (err) > > + return err; > > + > > + set_bit(WDOG_HW_RUNNING, &wdog->status); > > + return 0; > > +} > > + > > +static int abx80x_wdog_stop(struct watchdog_device *wdog) > > +{ > > + int err = __abx80x_wdog_set_timeout(wdog, 0); > > + > > + if (err) > > + return err; > > + > > + clear_bit(WDOG_HW_RUNNING, &wdog->status); > > + return 0; > > +} > > + > > +static const struct watchdog_info abx80x_wdog_info = { > > + .identity = "abx80x watchdog", > > + .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | > WDIOF_MAGICCLOSE, > > +}; > > + > > +static const struct watchdog_ops abx80x_wdog_ops = { > > + .owner = THIS_MODULE, > > + .start = abx80x_wdog_start, > > + .stop = abx80x_wdog_stop, > > + .ping = abx80x_wdog_ping, > > + .set_timeout = abx80x_wdog_set_timeout, > > +}; > > + > > +static int abx80x_setup_watchdog(struct abx80x_priv *priv) > > +{ > > + int err; > > + > > + priv->wdog.ops = &abx80x_wdog_ops; > > + priv->wdog.info = &abx80x_wdog_info; > > + priv->wdog.min_timeout = 1; > > + priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME; > > + > > + err = devm_watchdog_register_device(&priv->client->dev, > > + &priv->wdog); > > + if (err) > > + return err; > > + > > + watchdog_set_drvdata(&priv->wdog, priv); > > + watchdog_init_timeout(&priv->wdog, ABX8XX_WDT_MAX_TIME, > > + &priv->client->dev); > > + return 0; > > +} > > +#else > > +static int abx80x_setup_watchdog(struct abx80x_priv *priv) > > +{ > > + return 0; > > +} > > +#endif > > + > > static int abx80x_probe(struct i2c_client *client, > > const struct i2c_device_id *id) > > { > > struct device_node *np = client->dev.of_node; > > - struct rtc_device *rtc; > > + struct abx80x_priv *priv; > > int i, data, err, trickle_cfg = -EINVAL; > > char buf[7]; > > unsigned int part = id->driver_data; > > @@ -610,13 +734,25 @@ static int abx80x_probe(struct i2c_client *client, > > if (err) > > return err; > > > > - rtc = devm_rtc_allocate_device(&client->dev); > > - if (IS_ERR(rtc)) > > - return PTR_ERR(rtc); > > + priv = devm_kzalloc(&client->dev, sizeof(struct abx80x_priv), > > + GFP_KERNEL); > > + if (priv == NULL) > > + return -ENOMEM; > > + > > + priv->rtc = devm_rtc_allocate_device(&client->dev); > > + if (IS_ERR(priv->rtc)) > > + return PTR_ERR(priv->rtc); > > > > - rtc->ops = &abx80x_rtc_ops; > > + priv->rtc->ops = &abx80x_rtc_ops; > > + priv->client = client; > > > > - i2c_set_clientdata(client, rtc); > > + i2c_set_clientdata(client, priv); > > + > > + if (abx80x_caps[part].has_wdog) { > > + err = abx80x_setup_watchdog(priv); > > + if (err) > > + return err; > > + } > > > > if (client->irq > 0) { > > dev_info(&client->dev, "IRQ %d supplied\n", client->irq); > > @@ -649,7 +785,7 @@ static int abx80x_probe(struct i2c_client *client, > > return err; > > } > > > > - err = rtc_register_device(rtc); > > + err = rtc_register_device(priv->rtc); > > > > return err; > > } > > -- > > 2.17.1 > > > > -- > Alexandre Belloni, Bootlin > Embedded Linux and Kernel engineering > https://bootlin.com > --0000000000007fdd8a05758700a4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Sat= , Sep 8, 2018 at 9:10 AM Alexandre Belloni <alexandre.belloni@bootlin.com> wrote:
=
Hi,

Please Cc the watchdog maintainers. Else, I have very few comments.

Sure, I'll add them on the next version.= =C2=A0

=C2=A0
On 07/09/2018 09:17:43-0600, Jeremy Gebben wrote:
> The abx804 and abx805 chips have support for a simple watchdog
> function that can trigger an external reset.
>
> Signed-off-by: Jeremy Gebben <jgebben@sweptlaser.com>
> ---
>=C2=A0 drivers/rtc/Kconfig=C2=A0 =C2=A0 =C2=A0 |=C2=A0 11 +++
>=C2=A0 drivers/rtc/rtc-abx80x.c | 160 +++++++++++++++++++++++++++++++++= +++---
>=C2=A0 2 files changed, 159 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index 7d7be60a2413..4771e3a89721 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -195,6 +195,17 @@ config RTC_DRV_ABX80X
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0This driver can also be built as a mo= dule. If so, the module
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0will be called rtc-abx80x.
>=C2=A0
> +config RTC_DRV_ABX80X_WDT
> +=C2=A0 =C2=A0 =C2=A0bool "Abracon ABx80x Watchdog"
> +=C2=A0 =C2=A0 =C2=A0default y
> +=C2=A0 =C2=A0 =C2=A0depends on RTC_DRV_ABX80X
> +=C2=A0 =C2=A0 =C2=A0help
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0If you say yes here you get watchdog suppo= rt for Abracon ABX804 and ABX805
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0clock chips. This watchdog supports timeou= ts up to 31 seconds with 1 second
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0granularity.
> +
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 This watchdog device is part of th= e rtc-abx80x module.
> +

I'm not sure it is worth having a separate config option for that. You<= br> may as well just always include the watchdog support code. However,
doesn't it need to depend on CONFIG_WATCHDOG?

=
Yes, you're right about CONFIG_WATCHDOG, I thought I had tha= t already but I guess it got dropped.

=C2=A0
>=C2=A0 config RTC_DRV_AC100
>=C2=A0 =C2=A0 =C2=A0 =C2=A0tristate "X-Powers AC100"
>=C2=A0 =C2=A0 =C2=A0 =C2=A0depends on MFD_AC100
> diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
> index 2cefa67a1132..fa558939e944 100644
> --- a/drivers/rtc/rtc-abx80x.c
> +++ b/drivers/rtc/rtc-abx80x.c
> @@ -17,6 +17,7 @@
>=C2=A0 #include <linux/i2c.h>
>=C2=A0 #include <linux/module.h>
>=C2=A0 #include <linux/rtc.h>
> +#include <linux/watchdog.h>
>=C2=A0
>=C2=A0 #define ABX8XX_REG_HTH=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A00x00
>=C2=A0 #define ABX8XX_REG_SC=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 0x01
> @@ -37,6 +38,7 @@
>=C2=A0
>=C2=A0 #define ABX8XX_REG_STATUS=C2=A0 =C2=A0 0x0f
>=C2=A0 #define ABX8XX_STATUS_AF=C2=A0 =C2=A0 =C2=A0BIT(2)
> +#define ABX8XX_STATUS_WDT=C2=A0 =C2=A0 BIT(6)
>=C2=A0
>=C2=A0 #define ABX8XX_REG_CTRL1=C2=A0 =C2=A0 =C2=A00x10
>=C2=A0 #define ABX8XX_CTRL_WRITE=C2=A0 =C2=A0 BIT(0)
> @@ -61,6 +63,14 @@
>=C2=A0 #define ABX8XX_OSS_OF=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 BIT(1)
>=C2=A0 #define ABX8XX_OSS_OMODE=C2=A0 =C2=A0 =C2=A0BIT(4)
>=C2=A0
> +#define ABX8XX_REG_WDT=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A00x1b
> +#define ABX8XX_WDT_WDS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0BIT(7)
> +#define ABX8XX_WDT_BMB_MASK=C2=A0 0x7c
> +#define ABX8XX_WDT_BMB_SHIFT 2
> +#define ABX8XX_WDT_MAX_TIME=C2=A0 (ABX8XX_WDT_BMB_MASK >> ABX8X= X_WDT_BMB_SHIFT)
> +#define ABX8XX_WDT_WRB_MASK=C2=A0 0x03
> +#define ABX8XX_WDT_WRB_1HZ=C2=A0 =C2=A00x02
> +
>=C2=A0 #define ABX8XX_REG_CFG_KEY=C2=A0 =C2=A00x1f
>=C2=A0 #define ABX8XX_CFG_KEY_OSC=C2=A0 =C2=A00xa1
>=C2=A0 #define ABX8XX_CFG_KEY_MISC=C2=A0 0x9d
> @@ -80,20 +90,27 @@ enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,<= br> >=C2=A0 struct abx80x_cap {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0u16 pn;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0bool has_tc;
> +=C2=A0 =C2=A0 =C2=A0bool has_wdog;
>=C2=A0 };
>=C2=A0
>=C2=A0 static struct abx80x_cap abx80x_caps[] =3D {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[AB0801] =3D {.pn =3D 0x0801},
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[AB0803] =3D {.pn =3D 0x0803},
> -=C2=A0 =C2=A0 =C2=A0[AB0804] =3D {.pn =3D 0x0804, .has_tc =3D true},<= br> > -=C2=A0 =C2=A0 =C2=A0[AB0805] =3D {.pn =3D 0x0805, .has_tc =3D true},<= br> > +=C2=A0 =C2=A0 =C2=A0[AB0804] =3D {.pn =3D 0x0804, .has_tc =3D true, .= has_wdog =3D true},
> +=C2=A0 =C2=A0 =C2=A0[AB0805] =3D {.pn =3D 0x0805, .has_tc =3D true, .= has_wdog =3D true},
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[AB1801] =3D {.pn =3D 0x1801},
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[AB1803] =3D {.pn =3D 0x1803},
> -=C2=A0 =C2=A0 =C2=A0[AB1804] =3D {.pn =3D 0x1804, .has_tc =3D true},<= br> > -=C2=A0 =C2=A0 =C2=A0[AB1805] =3D {.pn =3D 0x1805, .has_tc =3D true},<= br> > +=C2=A0 =C2=A0 =C2=A0[AB1804] =3D {.pn =3D 0x1804, .has_tc =3D true, .= has_wdog =3D true},
> +=C2=A0 =C2=A0 =C2=A0[AB1805] =3D {.pn =3D 0x1805, .has_tc =3D true, .= has_wdog =3D true},
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[ABX80X] =3D {.pn =3D 0}
>=C2=A0 };
>=C2=A0
> +struct abx80x_priv {
> +=C2=A0 =C2=A0 =C2=A0struct rtc_device *rtc;
> +=C2=A0 =C2=A0 =C2=A0struct i2c_client *client;
> +=C2=A0 =C2=A0 =C2=A0struct watchdog_device wdog;
> +};
> +

To make the watchdog part easier to review, you could separate the
introduction of abx80x_priv in a preliminary patch and then introduce
the watchdog code.

Ok.=C2=A0
=
>=C2=A0 static int abx80x_is_rc_mode(struct i2c_client *client)
>=C2=A0 {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0int flags =3D 0;
> @@ -218,7 +235,8 @@ static int abx80x_rtc_set_time(struct device *dev,= struct rtc_time *tm)
>=C2=A0 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
>=C2=A0 {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0struct i2c_client *client =3D dev_id;
> -=C2=A0 =C2=A0 =C2=A0struct rtc_device *rtc =3D i2c_get_clientdata(cli= ent);
> +=C2=A0 =C2=A0 =C2=A0struct abx80x_priv *priv =3D i2c_get_clientdata(c= lient);
> +=C2=A0 =C2=A0 =C2=A0struct rtc_device *rtc =3D priv->rtc;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0int status;
>=C2=A0
>=C2=A0 =C2=A0 =C2=A0 =C2=A0status =3D i2c_smbus_read_byte_data(client, = ABX8XX_REG_STATUS);
> @@ -228,6 +246,13 @@ static irqreturn_t abx80x_handle_irq(int irq, voi= d *dev_id)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (status & ABX8XX_STATUS_AF)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0rtc_update_irq(r= tc, 1, RTC_AF | RTC_IRQF);
>=C2=A0
> +=C2=A0 =C2=A0 =C2=A0/*
> +=C2=A0 =C2=A0 =C2=A0 * It is unclear if we'll get an interrupt be= fore the external
> +=C2=A0 =C2=A0 =C2=A0 * reset kicks in.
> +=C2=A0 =C2=A0 =C2=A0 */
> +=C2=A0 =C2=A0 =C2=A0if (status & ABX8XX_STATUS_WDT)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0dev_err(&client-&= gt;dev, "watchdog timeout interrupt!\n");
> +

dev_alert maybe?


Ok.

Thanks,

Jeremy
=C2= =A0

>=C2=A0 =C2=A0 =C2=A0 =C2=A0i2c_smbus_write_byte_data(client, ABX8XX_REG= _STATUS, 0);
>=C2=A0
>=C2=A0 =C2=A0 =C2=A0 =C2=A0return IRQ_HANDLED;
> @@ -529,11 +554,110 @@ static void rtc_calib_remove_sysfs_group(void *= _dev)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0sysfs_remove_group(&dev->kobj, &r= tc_calib_attr_group);
>=C2=A0 }
>=C2=A0
> +#ifdef CONFIG_RTC_DRV_ABX80X_WDT
> +
> +static inline u8 timeout_bits(unsigned int timeout)
> +{
> +=C2=A0 =C2=A0 =C2=A0return ((timeout << ABX8XX_WDT_BMB_SHIFT) &= amp; ABX8XX_WDT_BMB_MASK) |
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ABX8XX_WDT_WRB_1HZ;<= br> > +}
> +
> +static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog, > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned int timeout)
> +{
> +=C2=A0 =C2=A0 =C2=A0struct abx80x_priv *priv =3D watchdog_get_drvdata= (wdog);
> +=C2=A0 =C2=A0 =C2=A0u8 val =3D ABX8XX_WDT_WDS | timeout_bits(timeout)= ;
> +
> +=C2=A0 =C2=A0 =C2=A0/*
> +=C2=A0 =C2=A0 =C2=A0 * Writing any timeout to the WDT register resets= the watchdog timer.
> +=C2=A0 =C2=A0 =C2=A0 * Writing 0 disables it.
> +=C2=A0 =C2=A0 =C2=A0 */
> +=C2=A0 =C2=A0 =C2=A0return i2c_smbus_write_byte_data(priv->client,= ABX8XX_REG_WDT, val);
> +}
> +
> +static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned int new_timeout)
> +{
> +=C2=A0 =C2=A0 =C2=A0int err =3D __abx80x_wdog_set_timeout(wdog, new_t= imeout);
> +
> +=C2=A0 =C2=A0 =C2=A0if (err)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
> +
> +=C2=A0 =C2=A0 =C2=A0wdog->timeout =3D new_timeout;
> +=C2=A0 =C2=A0 =C2=A0return 0;
> +}
> +
> +static int abx80x_wdog_ping(struct watchdog_device *wdog)
> +{
> +=C2=A0 =C2=A0 =C2=A0return __abx80x_wdog_set_timeout(wdog, wdog->t= imeout);
> +}
> +
> +static int abx80x_wdog_start(struct watchdog_device *wdog)
> +{
> +=C2=A0 =C2=A0 =C2=A0int err =3D __abx80x_wdog_set_timeout(wdog, wdog-= >timeout);
> +
> +=C2=A0 =C2=A0 =C2=A0if (err)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
> +
> +=C2=A0 =C2=A0 =C2=A0set_bit(WDOG_HW_RUNNING, &wdog->status); > +=C2=A0 =C2=A0 =C2=A0return 0;
> +}
> +
> +static int abx80x_wdog_stop(struct watchdog_device *wdog)
> +{
> +=C2=A0 =C2=A0 =C2=A0int err =3D __abx80x_wdog_set_timeout(wdog, 0); > +
> +=C2=A0 =C2=A0 =C2=A0if (err)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
> +
> +=C2=A0 =C2=A0 =C2=A0clear_bit(WDOG_HW_RUNNING, &wdog->status);=
> +=C2=A0 =C2=A0 =C2=A0return 0;
> +}
> +
> +static const struct watchdog_info abx80x_wdog_info =3D {
> +=C2=A0 =C2=A0 =C2=A0.identity =3D "abx80x watchdog",
> +=C2=A0 =C2=A0 =C2=A0.options =3D WDIOF_KEEPALIVEPING | WDIOF_SETTIMEO= UT | WDIOF_MAGICCLOSE,
> +};
> +
> +static const struct watchdog_ops abx80x_wdog_ops =3D {
> +=C2=A0 =C2=A0 =C2=A0.owner =3D THIS_MODULE,
> +=C2=A0 =C2=A0 =C2=A0.start =3D abx80x_wdog_start,
> +=C2=A0 =C2=A0 =C2=A0.stop =3D abx80x_wdog_stop,
> +=C2=A0 =C2=A0 =C2=A0.ping =3D abx80x_wdog_ping,
> +=C2=A0 =C2=A0 =C2=A0.set_timeout =3D abx80x_wdog_set_timeout,
> +};
> +
> +static int abx80x_setup_watchdog(struct abx80x_priv *priv)
> +{
> +=C2=A0 =C2=A0 =C2=A0int err;
> +
> +=C2=A0 =C2=A0 =C2=A0priv->wdog.ops =3D &abx80x_wdog_ops;
> +=C2=A0 =C2=A0 =C2=A0priv->wdog.info =3D &abx80x_wdog_info;
> +=C2=A0 =C2=A0 =C2=A0priv->wdog.min_timeout =3D 1;
> +=C2=A0 =C2=A0 =C2=A0priv->wdog.max_timeout =3D ABX8XX_WDT_MAX_TIME= ;
> +
> +=C2=A0 =C2=A0 =C2=A0err =3D devm_watchdog_register_device(&priv-&= gt;client->dev,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0&priv->wdog);
> +=C2=A0 =C2=A0 =C2=A0if (err)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
> +
> +=C2=A0 =C2=A0 =C2=A0watchdog_set_drvdata(&priv->wdog, priv); > +=C2=A0 =C2=A0 =C2=A0watchdog_init_timeout(&priv->wdog, ABX8XX_= WDT_MAX_TIME,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0&priv->client->dev);
> +=C2=A0 =C2=A0 =C2=A0return 0;
> +}
> +#else
> +static int abx80x_setup_watchdog(struct abx80x_priv *priv)
> +{
> +=C2=A0 =C2=A0 =C2=A0return 0;
> +}
> +#endif
> +
>=C2=A0 static int abx80x_probe(struct i2c_client *client,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0const struct i2c_device_id *id)
>=C2=A0 {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0struct device_node *np =3D client->dev.of= _node;
> -=C2=A0 =C2=A0 =C2=A0struct rtc_device *rtc;
> +=C2=A0 =C2=A0 =C2=A0struct abx80x_priv *priv;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0int i, data, err, trickle_cfg =3D -EINVAL; >=C2=A0 =C2=A0 =C2=A0 =C2=A0char buf[7];
>=C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned int part =3D id->driver_data; > @@ -610,13 +734,25 @@ static int abx80x_probe(struct i2c_client *clien= t,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (err)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
>=C2=A0
> -=C2=A0 =C2=A0 =C2=A0rtc =3D devm_rtc_allocate_device(&client->= dev);
> -=C2=A0 =C2=A0 =C2=A0if (IS_ERR(rtc))
> -=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return PTR_ERR(rtc);<= br> > +=C2=A0 =C2=A0 =C2=A0priv =3D devm_kzalloc(&client->dev, sizeof= (struct abx80x_priv),
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0GFP_KERNEL);
> +=C2=A0 =C2=A0 =C2=A0if (priv =3D=3D NULL)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return -ENOMEM;
> +
> +=C2=A0 =C2=A0 =C2=A0priv->rtc =3D devm_rtc_allocate_device(&cl= ient->dev);
> +=C2=A0 =C2=A0 =C2=A0if (IS_ERR(priv->rtc))
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return PTR_ERR(priv-&= gt;rtc);
>=C2=A0
> -=C2=A0 =C2=A0 =C2=A0rtc->ops =3D &abx80x_rtc_ops;
> +=C2=A0 =C2=A0 =C2=A0priv->rtc->ops =3D &abx80x_rtc_ops;
> +=C2=A0 =C2=A0 =C2=A0priv->client =3D client;
>=C2=A0
> -=C2=A0 =C2=A0 =C2=A0i2c_set_clientdata(client, rtc);
> +=C2=A0 =C2=A0 =C2=A0i2c_set_clientdata(client, priv);
> +
> +=C2=A0 =C2=A0 =C2=A0if (abx80x_caps[part].has_wdog) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0err =3D abx80x_setup_= watchdog(priv);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (err)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0return err;
> +=C2=A0 =C2=A0 =C2=A0}
>=C2=A0
>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (client->irq > 0) {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0dev_info(&cl= ient->dev, "IRQ %d supplied\n", client->irq);
> @@ -649,7 +785,7 @@ static int abx80x_probe(struct i2c_client *client,=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0}
>=C2=A0
> -=C2=A0 =C2=A0 =C2=A0err =3D rtc_register_device(rtc);
> +=C2=A0 =C2=A0 =C2=A0err =3D rtc_register_device(priv->rtc);
>=C2=A0
>=C2=A0 =C2=A0 =C2=A0 =C2=A0return err;
>=C2=A0 }
> --
> 2.17.1
>

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https:= //bootlin.com
--0000000000007fdd8a05758700a4--