Merge drm/drm-next into drm-intel-next
[linux-block.git] / drivers / watchdog / twl4030_wdt.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
80e45b1e
TK
2/*
3 * Copyright (C) Nokia Corporation
4 *
5 * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
80e45b1e
TK
6 */
7
8#include <linux/module.h>
9#include <linux/types.h>
5a0e3ad6 10#include <linux/slab.h>
80e45b1e 11#include <linux/kernel.h>
099d387e 12#include <linux/mod_devicetable.h>
80e45b1e
TK
13#include <linux/watchdog.h>
14#include <linux/platform_device.h>
a2054256 15#include <linux/mfd/twl.h>
80e45b1e
TK
16
17#define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
18
86a1e189
WVS
19static bool nowayout = WATCHDOG_NOWAYOUT;
20module_param(nowayout, bool, 0);
80e45b1e
TK
21MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
22 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
23
24static int twl4030_wdt_write(unsigned char val)
25{
2bc3f62f 26 return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val,
80e45b1e
TK
27 TWL4030_WATCHDOG_CFG_REG_OFFS);
28}
29
b2c4e4b2 30static int twl4030_wdt_start(struct watchdog_device *wdt)
80e45b1e 31{
b2c4e4b2 32 return twl4030_wdt_write(wdt->timeout + 1);
80e45b1e
TK
33}
34
b2c4e4b2 35static int twl4030_wdt_stop(struct watchdog_device *wdt)
80e45b1e
TK
36{
37 return twl4030_wdt_write(0);
38}
39
b2c4e4b2
JN
40static int twl4030_wdt_set_timeout(struct watchdog_device *wdt,
41 unsigned int timeout)
80e45b1e 42{
b2c4e4b2 43 wdt->timeout = timeout;
80e45b1e
TK
44 return 0;
45}
46
b2c4e4b2 47static const struct watchdog_info twl4030_wdt_info = {
fb1cbeae 48 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
b2c4e4b2
JN
49 .identity = "TWL4030 Watchdog",
50};
80e45b1e 51
b2c4e4b2 52static const struct watchdog_ops twl4030_wdt_ops = {
80e45b1e 53 .owner = THIS_MODULE,
b2c4e4b2
JN
54 .start = twl4030_wdt_start,
55 .stop = twl4030_wdt_stop,
56 .set_timeout = twl4030_wdt_set_timeout,
80e45b1e
TK
57};
58
2d991a16 59static int twl4030_wdt_probe(struct platform_device *pdev)
80e45b1e 60{
b42488bc 61 struct device *dev = &pdev->dev;
b2c4e4b2 62 struct watchdog_device *wdt;
80e45b1e 63
b42488bc 64 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
80e45b1e
TK
65 if (!wdt)
66 return -ENOMEM;
67
b2c4e4b2
JN
68 wdt->info = &twl4030_wdt_info;
69 wdt->ops = &twl4030_wdt_ops;
70 wdt->status = 0;
71 wdt->timeout = 30;
72 wdt->min_timeout = 1;
73 wdt->max_timeout = 30;
b42488bc 74 wdt->parent = dev;
80e45b1e 75
b2c4e4b2 76 watchdog_set_nowayout(wdt, nowayout);
80e45b1e
TK
77 platform_set_drvdata(pdev, wdt);
78
b2c4e4b2 79 twl4030_wdt_stop(wdt);
80e45b1e 80
b42488bc 81 return devm_watchdog_register_device(dev, wdt);
80e45b1e
TK
82}
83
80e45b1e
TK
84static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
85{
b2c4e4b2
JN
86 struct watchdog_device *wdt = platform_get_drvdata(pdev);
87 if (watchdog_active(wdt))
88 return twl4030_wdt_stop(wdt);
80e45b1e
TK
89
90 return 0;
91}
92
93static int twl4030_wdt_resume(struct platform_device *pdev)
94{
b2c4e4b2
JN
95 struct watchdog_device *wdt = platform_get_drvdata(pdev);
96 if (watchdog_active(wdt))
97 return twl4030_wdt_start(wdt);
80e45b1e
TK
98
99 return 0;
100}
80e45b1e 101
8899b8d9
AK
102static const struct of_device_id twl_wdt_of_match[] = {
103 { .compatible = "ti,twl4030-wdt", },
104 { },
105};
106MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
107
80e45b1e
TK
108static struct platform_driver twl4030_wdt_driver = {
109 .probe = twl4030_wdt_probe,
d36eda79
PC
110 .suspend = pm_ptr(twl4030_wdt_suspend),
111 .resume = pm_ptr(twl4030_wdt_resume),
80e45b1e 112 .driver = {
8899b8d9
AK
113 .name = "twl4030_wdt",
114 .of_match_table = twl_wdt_of_match,
80e45b1e
TK
115 },
116};
117
b8ec6118 118module_platform_driver(twl4030_wdt_driver);
80e45b1e
TK
119
120MODULE_AUTHOR("Nokia Corporation");
121MODULE_LICENSE("GPL");
80e45b1e
TK
122MODULE_ALIAS("platform:twl4030_wdt");
123