* [PATCH v3 0/2] rtc: abx80x: add basic watchdog support @ 2018-09-11 17:28 Jeremy Gebben 2018-09-11 17:28 ` [PATCH 1/2] rtc: abx80x: use a 'priv' struct for client data Jeremy Gebben ` (2 more replies) 0 siblings, 3 replies; 8+ messages in thread From: Jeremy Gebben @ 2018-09-11 17:28 UTC (permalink / raw) To: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, Guenter Roeck, linux-rtc Cc: linux-watchdog, linux-kernel This patch series adds watchdog support for abracon RTC chips which include basic watchdog functionality. Thank you for reviewing. Changes in v3 (all in patch 2): * fix CONFIG_WATCHDOG dependency again (hopefully correctly this time) * don't start hardware when set_timeout is called * remove unneeded WDOG_HW_RUNNING bit changes * fix abx80x_setup_watchdog() * fix formatting of multiline splits Changes in v2: * split out priv structure into a separate patch * remove new Kconfig option * fix CONFIG_WATCHDOG dependency * fix WDT interrupt message Jeremy Gebben (2): rtc: abx80x: use a 'priv' struct for client data rtc: abx80x: add basic watchdog support drivers/rtc/Kconfig | 1 + drivers/rtc/rtc-abx80x.c | 143 +++++++++++++++++++++++++++++++++++---- 2 files changed, 132 insertions(+), 12 deletions(-) -- 2.17.1 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/2] rtc: abx80x: use a 'priv' struct for client data 2018-09-11 17:28 [PATCH v3 0/2] rtc: abx80x: add basic watchdog support Jeremy Gebben @ 2018-09-11 17:28 ` Jeremy Gebben 2018-09-11 17:28 ` [PATCH 2/2] rtc: abx80x: add basic watchdog support Jeremy Gebben 2018-09-12 11:18 ` [PATCH v3 0/2] " Alexandre Belloni 2 siblings, 0 replies; 8+ messages in thread From: Jeremy Gebben @ 2018-09-11 17:28 UTC (permalink / raw) To: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, Guenter Roeck, linux-rtc Cc: linux-watchdog, linux-kernel This will allow additional data to be tracked, for future improvements. Signed-off-by: Jeremy Gebben <jgebben@sweptlaser.com> --- drivers/rtc/rtc-abx80x.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c index 2cefa67a1132..9d49054a0a4a 100644 --- a/drivers/rtc/rtc-abx80x.c +++ b/drivers/rtc/rtc-abx80x.c @@ -94,6 +94,11 @@ static struct abx80x_cap abx80x_caps[] = { [ABX80X] = {.pn = 0} }; +struct abx80x_priv { + struct rtc_device *rtc; + struct i2c_client *client; +}; + static int abx80x_is_rc_mode(struct i2c_client *client) { int flags = 0; @@ -218,7 +223,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); @@ -533,7 +539,7 @@ 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 +616,18 @@ 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(*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 (client->irq > 0) { dev_info(&client->dev, "IRQ %d supplied\n", client->irq); @@ -649,7 +660,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 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/2] rtc: abx80x: add basic watchdog support 2018-09-11 17:28 [PATCH v3 0/2] rtc: abx80x: add basic watchdog support Jeremy Gebben 2018-09-11 17:28 ` [PATCH 1/2] rtc: abx80x: use a 'priv' struct for client data Jeremy Gebben @ 2018-09-11 17:28 ` Jeremy Gebben 2018-09-11 21:49 ` Guenter Roeck 2018-09-12 11:18 ` [PATCH v3 0/2] " Alexandre Belloni 2 siblings, 1 reply; 8+ messages in thread From: Jeremy Gebben @ 2018-09-11 17:28 UTC (permalink / raw) To: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, Guenter Roeck, linux-rtc Cc: linux-watchdog, linux-kernel 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> --- drivers/rtc/Kconfig | 1 + drivers/rtc/rtc-abx80x.c | 116 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 4 deletions(-) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 7d7be60a2413..bcb0e9eda781 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -187,6 +187,7 @@ config RTC_DRV_ABB5ZES3 config RTC_DRV_ABX80X tristate "Abracon ABx80x" + select WATCHDOG_CORE if WATCHDOG help If you say yes here you get support for Abracon AB080X and AB180X families of ultra-low-power battery- and capacitor-backed real-time diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c index 9d49054a0a4a..d8e94edcb0ba 100644 --- a/drivers/rtc/rtc-abx80x.c +++ b/drivers/rtc/rtc-abx80x.c @@ -17,6 +17,7 @@ #include <linux/i2c.h> #include <linux/module.h> #include <linux/rtc.h> +#include <linux/watchdog.h> #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,23 +90,25 @@ 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; }; static int abx80x_is_rc_mode(struct i2c_client *client) @@ -234,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_alert(&client->dev, "watchdog timeout interrupt.\n"); + i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); return IRQ_HANDLED; @@ -535,6 +554,89 @@ static void rtc_calib_remove_sysfs_group(void *_dev) sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); } +#ifdef CONFIG_WATCHDOG + +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 = 0; + + if (watchdog_hw_running(wdog)) + err = __abx80x_wdog_set_timeout(wdog, new_timeout); + + if (err == 0) + wdog->timeout = new_timeout; + + return err; +} + +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) +{ + return __abx80x_wdog_set_timeout(wdog, wdog->timeout); +} + +static int abx80x_wdog_stop(struct watchdog_device *wdog) +{ + return __abx80x_wdog_set_timeout(wdog, 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) +{ + priv->wdog.parent = &priv->client->dev; + 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; + priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; + + watchdog_set_drvdata(&priv->wdog, priv); + + return devm_watchdog_register_device(&priv->client->dev, &priv->wdog); +} +#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) { @@ -629,6 +731,12 @@ static int abx80x_probe(struct i2c_client *client, 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); err = devm_request_threaded_irq(&client->dev, client->irq, NULL, -- 2.17.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] rtc: abx80x: add basic watchdog support 2018-09-11 17:28 ` [PATCH 2/2] rtc: abx80x: add basic watchdog support Jeremy Gebben @ 2018-09-11 21:49 ` Guenter Roeck 0 siblings, 0 replies; 8+ messages in thread From: Guenter Roeck @ 2018-09-11 21:49 UTC (permalink / raw) To: Jeremy Gebben Cc: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, linux-rtc, linux-watchdog, linux-kernel On Tue, Sep 11, 2018 at 11:28:26AM -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> Reviewed-by: Guenter Roeck <linux@roeck-us.net> > --- > drivers/rtc/Kconfig | 1 + > drivers/rtc/rtc-abx80x.c | 116 +++++++++++++++++++++++++++++++++++++-- > 2 files changed, 113 insertions(+), 4 deletions(-) > > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig > index 7d7be60a2413..bcb0e9eda781 100644 > --- a/drivers/rtc/Kconfig > +++ b/drivers/rtc/Kconfig > @@ -187,6 +187,7 @@ config RTC_DRV_ABB5ZES3 > > config RTC_DRV_ABX80X > tristate "Abracon ABx80x" > + select WATCHDOG_CORE if WATCHDOG > help > If you say yes here you get support for Abracon AB080X and AB180X > families of ultra-low-power battery- and capacitor-backed real-time > diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c > index 9d49054a0a4a..d8e94edcb0ba 100644 > --- a/drivers/rtc/rtc-abx80x.c > +++ b/drivers/rtc/rtc-abx80x.c > @@ -17,6 +17,7 @@ > #include <linux/i2c.h> > #include <linux/module.h> > #include <linux/rtc.h> > +#include <linux/watchdog.h> > > #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,23 +90,25 @@ 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; > }; > > static int abx80x_is_rc_mode(struct i2c_client *client) > @@ -234,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_alert(&client->dev, "watchdog timeout interrupt.\n"); > + > i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); > > return IRQ_HANDLED; > @@ -535,6 +554,89 @@ static void rtc_calib_remove_sysfs_group(void *_dev) > sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); > } > > +#ifdef CONFIG_WATCHDOG > + > +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 = 0; > + > + if (watchdog_hw_running(wdog)) > + err = __abx80x_wdog_set_timeout(wdog, new_timeout); > + > + if (err == 0) > + wdog->timeout = new_timeout; > + > + return err; > +} > + > +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) > +{ > + return __abx80x_wdog_set_timeout(wdog, wdog->timeout); > +} > + > +static int abx80x_wdog_stop(struct watchdog_device *wdog) > +{ > + return __abx80x_wdog_set_timeout(wdog, 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) > +{ > + priv->wdog.parent = &priv->client->dev; > + 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; > + priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; > + > + watchdog_set_drvdata(&priv->wdog, priv); > + > + return devm_watchdog_register_device(&priv->client->dev, &priv->wdog); > +} > +#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) > { > @@ -629,6 +731,12 @@ static int abx80x_probe(struct i2c_client *client, > > 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); > err = devm_request_threaded_irq(&client->dev, client->irq, NULL, > -- > 2.17.1 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] rtc: abx80x: add basic watchdog support 2018-09-11 17:28 [PATCH v3 0/2] rtc: abx80x: add basic watchdog support Jeremy Gebben 2018-09-11 17:28 ` [PATCH 1/2] rtc: abx80x: use a 'priv' struct for client data Jeremy Gebben 2018-09-11 17:28 ` [PATCH 2/2] rtc: abx80x: add basic watchdog support Jeremy Gebben @ 2018-09-12 11:18 ` Alexandre Belloni 2 siblings, 0 replies; 8+ messages in thread From: Alexandre Belloni @ 2018-09-12 11:18 UTC (permalink / raw) To: Jeremy Gebben Cc: Alessandro Zummo, Wim Van Sebroeck, Guenter Roeck, linux-rtc, linux-watchdog, linux-kernel On 11/09/2018 11:28:24-0600, Jeremy Gebben wrote: > This patch series adds watchdog support for abracon RTC chips which > include basic watchdog functionality. > > Thank you for reviewing. > > Changes in v3 (all in patch 2): > * fix CONFIG_WATCHDOG dependency again (hopefully correctly this time) > * don't start hardware when set_timeout is called > * remove unneeded WDOG_HW_RUNNING bit changes > * fix abx80x_setup_watchdog() > * fix formatting of multiline splits > > Changes in v2: > * split out priv structure into a separate patch > * remove new Kconfig option > * fix CONFIG_WATCHDOG dependency > * fix WDT interrupt message > > > Jeremy Gebben (2): > rtc: abx80x: use a 'priv' struct for client data > rtc: abx80x: add basic watchdog support > > drivers/rtc/Kconfig | 1 + > drivers/rtc/rtc-abx80x.c | 143 +++++++++++++++++++++++++++++++++++---- > 2 files changed, 132 insertions(+), 12 deletions(-) > Applied, thanks. -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 0/2] rtc: abx80x: add basic watchdog support @ 2018-09-10 21:36 Jeremy Gebben 2018-09-10 21:36 ` [PATCH 2/2] " Jeremy Gebben 0 siblings, 1 reply; 8+ messages in thread From: Jeremy Gebben @ 2018-09-10 21:36 UTC (permalink / raw) To: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, Guenter Roeck, linux-rtc Cc: linux-watchdog, linux-kernel This patch series adds watchod support for abracon RTC chips which include basic watchdog functionality. Changes in v2: * split out priv structure into a separate patch * remove new Kconfig option * fix CONFIG_WATCHDOG dependency * fix WDT interrupt message Jeremy Gebben (2): rtc: abx80x: use a 'priv' struct for client data rtc: abx80x: add basic watchdog support drivers/rtc/rtc-abx80x.c | 159 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 147 insertions(+), 12 deletions(-) -- 2.17.1 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/2] rtc: abx80x: add basic watchdog support 2018-09-10 21:36 [PATCH v2 " Jeremy Gebben @ 2018-09-10 21:36 ` Jeremy Gebben 2018-09-10 23:20 ` Guenter Roeck 0 siblings, 1 reply; 8+ messages in thread From: Jeremy Gebben @ 2018-09-10 21:36 UTC (permalink / raw) To: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, Guenter Roeck, linux-rtc Cc: linux-watchdog, linux-kernel 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> --- drivers/rtc/rtc-abx80x.c | 132 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 4 deletions(-) diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c index 9d49054a0a4a..e1ec4cce093a 100644 --- a/drivers/rtc/rtc-abx80x.c +++ b/drivers/rtc/rtc-abx80x.c @@ -17,6 +17,7 @@ #include <linux/i2c.h> #include <linux/module.h> #include <linux/rtc.h> +#include <linux/watchdog.h> #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,23 +90,25 @@ 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; }; static int abx80x_is_rc_mode(struct i2c_client *client) @@ -234,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_alert(&client->dev, "watchdog timeout interrupt.\n"); + i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); return IRQ_HANDLED; @@ -535,6 +554,105 @@ static void rtc_calib_remove_sysfs_group(void *_dev) sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); } +#ifdef CONFIG_WATCHDOG + +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) { @@ -629,6 +747,12 @@ static int abx80x_probe(struct i2c_client *client, 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); err = devm_request_threaded_irq(&client->dev, client->irq, NULL, -- 2.17.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] rtc: abx80x: add basic watchdog support 2018-09-10 21:36 ` [PATCH 2/2] " Jeremy Gebben @ 2018-09-10 23:20 ` Guenter Roeck 2018-09-11 7:39 ` Alexandre Belloni 0 siblings, 1 reply; 8+ messages in thread From: Guenter Roeck @ 2018-09-10 23:20 UTC (permalink / raw) To: Jeremy Gebben Cc: Alessandro Zummo, Alexandre Belloni, Wim Van Sebroeck, linux-rtc, linux-watchdog, linux-kernel On Mon, Sep 10, 2018 at 03:36:54PM -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> I am missing the "select WATCHDOG_CORE" that would normally go with watchdog drivers. If you don't want a separate configuration option for the watchdog, you'll still need something like select WATCHDOG_CORE if WATCHDOG in the Kconfig entry for this driver. > --- > drivers/rtc/rtc-abx80x.c | 132 +++++++++++++++++++++++++++++++++++++-- > 1 file changed, 128 insertions(+), 4 deletions(-) > > diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c > index 9d49054a0a4a..e1ec4cce093a 100644 > --- a/drivers/rtc/rtc-abx80x.c > +++ b/drivers/rtc/rtc-abx80x.c > @@ -17,6 +17,7 @@ > #include <linux/i2c.h> > #include <linux/module.h> > #include <linux/rtc.h> > +#include <linux/watchdog.h> > > #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,23 +90,25 @@ 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; > }; > > static int abx80x_is_rc_mode(struct i2c_client *client) > @@ -234,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_alert(&client->dev, "watchdog timeout interrupt.\n"); > + > i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); > > return IRQ_HANDLED; > @@ -535,6 +554,105 @@ static void rtc_calib_remove_sysfs_group(void *_dev) > sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); > } > > +#ifdef CONFIG_WATCHDOG > + > +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) I'll leave multi-line alignment issues to the driver maintainer. > +{ > + int err = __abx80x_wdog_set_timeout(wdog, new_timeout); > + This will unconditionally enable the watchdog, even if it is currently disabled. > + 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); This is only necessary if the watchdog is running at probe, or if it can not be stopped. > + 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); This suggests that the watchdog can be stopped, thus it is unnecessary to set or clear the bit. > + 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; priv->wdog.parent should be set. > + > + err = devm_watchdog_register_device(&priv->client->dev, > + &priv->wdog); > + if (err) > + return err; > + > + watchdog_set_drvdata(&priv->wdog, priv); This is too late and racy; it must be called prior to registration. > + watchdog_init_timeout(&priv->wdog, ABX8XX_WDT_MAX_TIME, > + &priv->client->dev); This is quite pointless. Just set priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; above (prior to registration). If the idea is to be able to pick a timeout value from devicetree, you would still set the default value above and make the call watchdog_init_timeout(&priv->wdog, 0, &priv->client->dev); but again prior to registration. > + 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) > { > @@ -629,6 +747,12 @@ static int abx80x_probe(struct i2c_client *client, > > 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); > err = devm_request_threaded_irq(&client->dev, client->irq, NULL, > -- > 2.17.1 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] rtc: abx80x: add basic watchdog support 2018-09-10 23:20 ` Guenter Roeck @ 2018-09-11 7:39 ` Alexandre Belloni 0 siblings, 0 replies; 8+ messages in thread From: Alexandre Belloni @ 2018-09-11 7:39 UTC (permalink / raw) To: Guenter Roeck Cc: Jeremy Gebben, Alessandro Zummo, Wim Van Sebroeck, linux-rtc, linux-watchdog, linux-kernel On 10/09/2018 16:20:19-0700, Guenter Roeck wrote: > > +static int abx80x_wdog_set_timeout(struct watchdog_device *wdog, > > + unsigned int new_timeout) > > I'll leave multi-line alignment issues to the driver maintainer. > Indeed, I'd like those to be aligned on the parenthesis. Thank you for your review. -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2018-09-12 16:22 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-09-11 17:28 [PATCH v3 0/2] rtc: abx80x: add basic watchdog support Jeremy Gebben 2018-09-11 17:28 ` [PATCH 1/2] rtc: abx80x: use a 'priv' struct for client data Jeremy Gebben 2018-09-11 17:28 ` [PATCH 2/2] rtc: abx80x: add basic watchdog support Jeremy Gebben 2018-09-11 21:49 ` Guenter Roeck 2018-09-12 11:18 ` [PATCH v3 0/2] " Alexandre Belloni -- strict thread matches above, loose matches on Subject: below -- 2018-09-10 21:36 [PATCH v2 " Jeremy Gebben 2018-09-10 21:36 ` [PATCH 2/2] " Jeremy Gebben 2018-09-10 23:20 ` Guenter Roeck 2018-09-11 7:39 ` Alexandre Belloni
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).