Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Watchdog driver for Ricoh RN5T618 PMIC
4 *
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
6 */
7
8#include <linux/device.h>
9#include <linux/mfd/rn5t618.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/watchdog.h>
13
14#define DRIVER_NAME "rn5t618-wdt"
15
16static bool nowayout = WATCHDOG_NOWAYOUT;
17static unsigned int timeout;
18
19module_param(timeout, uint, 0);
20MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds");
21
22module_param(nowayout, bool, 0);
23MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
24 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
25
26struct rn5t618_wdt {
27 struct watchdog_device wdt_dev;
28 struct rn5t618 *rn5t618;
29};
30
31/*
32 * This array encodes the values of WDOGTIM field for the supported
33 * watchdog expiration times. If the watchdog is not accessed before
34 * the timer expiration, the PMU generates an interrupt and if the CPU
35 * doesn't clear it within one second the system is restarted.
36 */
37static const struct {
38 u8 reg_val;
39 unsigned int time;
40} rn5t618_wdt_map[] = {
41 { 0, 1 },
42 { 1, 8 },
43 { 2, 32 },
44 { 3, 128 },
45};
46
47static int rn5t618_wdt_set_timeout(struct watchdog_device *wdt_dev,
48 unsigned int t)
49{
50 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
51 int ret, i;
52
53 for (i = 0; i < ARRAY_SIZE(rn5t618_wdt_map); i++) {
54 if (rn5t618_wdt_map[i].time + 1 >= t)
55 break;
56 }
57
58 if (i == ARRAY_SIZE(rn5t618_wdt_map))
59 return -EINVAL;
60
61 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
62 RN5T618_WATCHDOG_WDOGTIM_M,
63 rn5t618_wdt_map[i].reg_val);
64 if (!ret)
65 wdt_dev->timeout = rn5t618_wdt_map[i].time;
66
67 return ret;
68}
69
70static int rn5t618_wdt_start(struct watchdog_device *wdt_dev)
71{
72 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
73 int ret;
74
75 ret = rn5t618_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
76 if (ret)
77 return ret;
78
79 /* enable repower-on */
80 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_REPCNT,
81 RN5T618_REPCNT_REPWRON,
82 RN5T618_REPCNT_REPWRON);
83 if (ret)
84 return ret;
85
86 /* enable watchdog */
87 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
88 RN5T618_WATCHDOG_WDOGEN,
89 RN5T618_WATCHDOG_WDOGEN);
90 if (ret)
91 return ret;
92
93 /* enable watchdog interrupt */
94 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIREN,
95 RN5T618_PWRIRQ_IR_WDOG,
96 RN5T618_PWRIRQ_IR_WDOG);
97}
98
99static int rn5t618_wdt_stop(struct watchdog_device *wdt_dev)
100{
101 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
102
103 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
104 RN5T618_WATCHDOG_WDOGEN, 0);
105}
106
107static int rn5t618_wdt_ping(struct watchdog_device *wdt_dev)
108{
109 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
110 unsigned int val;
111 int ret;
112
113 /* The counter is restarted after a R/W access to watchdog register */
114 ret = regmap_read(wdt->rn5t618->regmap, RN5T618_WATCHDOG, &val);
115 if (ret)
116 return ret;
117
118 ret = regmap_write(wdt->rn5t618->regmap, RN5T618_WATCHDOG, val);
119 if (ret)
120 return ret;
121
122 /* Clear pending watchdog interrupt */
123 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIRQ,
124 RN5T618_PWRIRQ_IR_WDOG, 0);
125}
126
127static const struct watchdog_info rn5t618_wdt_info = {
128 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
129 WDIOF_KEEPALIVEPING,
130 .identity = DRIVER_NAME,
131};
132
133static const struct watchdog_ops rn5t618_wdt_ops = {
134 .owner = THIS_MODULE,
135 .start = rn5t618_wdt_start,
136 .stop = rn5t618_wdt_stop,
137 .ping = rn5t618_wdt_ping,
138 .set_timeout = rn5t618_wdt_set_timeout,
139};
140
141static int rn5t618_wdt_probe(struct platform_device *pdev)
142{
143 struct device *dev = &pdev->dev;
144 struct rn5t618 *rn5t618 = dev_get_drvdata(dev->parent);
145 struct rn5t618_wdt *wdt;
146 int min_timeout, max_timeout;
147 int ret;
148 unsigned int val;
149
150 wdt = devm_kzalloc(dev, sizeof(struct rn5t618_wdt), GFP_KERNEL);
151 if (!wdt)
152 return -ENOMEM;
153
154 min_timeout = rn5t618_wdt_map[0].time;
155 max_timeout = rn5t618_wdt_map[ARRAY_SIZE(rn5t618_wdt_map) - 1].time;
156
157 wdt->rn5t618 = rn5t618;
158 wdt->wdt_dev.info = &rn5t618_wdt_info;
159 wdt->wdt_dev.ops = &rn5t618_wdt_ops;
160 wdt->wdt_dev.min_timeout = min_timeout;
161 wdt->wdt_dev.max_timeout = max_timeout;
162 wdt->wdt_dev.timeout = max_timeout;
163 wdt->wdt_dev.parent = dev;
164
165 /* Read out previous power-off factor */
166 ret = regmap_read(wdt->rn5t618->regmap, RN5T618_POFFHIS, &val);
167 if (ret)
168 return ret;
169
170 if (val & RN5T618_POFFHIS_VINDET)
171 wdt->wdt_dev.bootstatus = WDIOF_POWERUNDER;
172 else if (val & RN5T618_POFFHIS_WDG)
173 wdt->wdt_dev.bootstatus = WDIOF_CARDRESET;
174
175 watchdog_set_drvdata(&wdt->wdt_dev, wdt);
176 watchdog_init_timeout(&wdt->wdt_dev, timeout, dev);
177 watchdog_set_nowayout(&wdt->wdt_dev, nowayout);
178
179 platform_set_drvdata(pdev, wdt);
180
181 return watchdog_register_device(&wdt->wdt_dev);
182}
183
184static int rn5t618_wdt_remove(struct platform_device *pdev)
185{
186 struct rn5t618_wdt *wdt = platform_get_drvdata(pdev);
187
188 watchdog_unregister_device(&wdt->wdt_dev);
189
190 return 0;
191}
192
193static struct platform_driver rn5t618_wdt_driver = {
194 .probe = rn5t618_wdt_probe,
195 .remove = rn5t618_wdt_remove,
196 .driver = {
197 .name = DRIVER_NAME,
198 },
199};
200
201module_platform_driver(rn5t618_wdt_driver);
202
203MODULE_ALIAS("platform:rn5t618-wdt");
204MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
205MODULE_DESCRIPTION("RN5T618 watchdog driver");
206MODULE_LICENSE("GPL v2");
1/*
2 * Watchdog driver for Ricoh RN5T618 PMIC
3 *
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * You should have received a copy of the GNU General Public License
11 * along with this program. If not, see <http://www.gnu.org/licenses/>.
12 */
13
14#include <linux/device.h>
15#include <linux/mfd/rn5t618.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/watchdog.h>
19
20#define DRIVER_NAME "rn5t618-wdt"
21
22static bool nowayout = WATCHDOG_NOWAYOUT;
23static unsigned int timeout;
24
25module_param(timeout, uint, 0);
26MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds");
27
28module_param(nowayout, bool, 0);
29MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
30 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
31
32struct rn5t618_wdt {
33 struct watchdog_device wdt_dev;
34 struct rn5t618 *rn5t618;
35};
36
37/*
38 * This array encodes the values of WDOGTIM field for the supported
39 * watchdog expiration times. If the watchdog is not accessed before
40 * the timer expiration, the PMU generates an interrupt and if the CPU
41 * doesn't clear it within one second the system is restarted.
42 */
43static const struct {
44 u8 reg_val;
45 unsigned int time;
46} rn5t618_wdt_map[] = {
47 { 0, 1 },
48 { 1, 8 },
49 { 2, 32 },
50 { 3, 128 },
51};
52
53static int rn5t618_wdt_set_timeout(struct watchdog_device *wdt_dev,
54 unsigned int t)
55{
56 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
57 int ret, i;
58
59 for (i = 0; i < ARRAY_SIZE(rn5t618_wdt_map); i++) {
60 if (rn5t618_wdt_map[i].time + 1 >= t)
61 break;
62 }
63
64 if (i == ARRAY_SIZE(rn5t618_wdt_map))
65 return -EINVAL;
66
67 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
68 RN5T618_WATCHDOG_WDOGTIM_M,
69 rn5t618_wdt_map[i].reg_val);
70 if (!ret)
71 wdt_dev->timeout = rn5t618_wdt_map[i].time;
72
73 return ret;
74}
75
76static int rn5t618_wdt_start(struct watchdog_device *wdt_dev)
77{
78 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
79 int ret;
80
81 ret = rn5t618_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
82 if (ret)
83 return ret;
84
85 /* enable repower-on */
86 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_REPCNT,
87 RN5T618_REPCNT_REPWRON,
88 RN5T618_REPCNT_REPWRON);
89 if (ret)
90 return ret;
91
92 /* enable watchdog */
93 ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
94 RN5T618_WATCHDOG_WDOGEN,
95 RN5T618_WATCHDOG_WDOGEN);
96 if (ret)
97 return ret;
98
99 /* enable watchdog interrupt */
100 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIREN,
101 RN5T618_PWRIRQ_IR_WDOG,
102 RN5T618_PWRIRQ_IR_WDOG);
103}
104
105static int rn5t618_wdt_stop(struct watchdog_device *wdt_dev)
106{
107 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
108
109 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
110 RN5T618_WATCHDOG_WDOGEN, 0);
111}
112
113static int rn5t618_wdt_ping(struct watchdog_device *wdt_dev)
114{
115 struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
116 unsigned int val;
117 int ret;
118
119 /* The counter is restarted after a R/W access to watchdog register */
120 ret = regmap_read(wdt->rn5t618->regmap, RN5T618_WATCHDOG, &val);
121 if (ret)
122 return ret;
123
124 ret = regmap_write(wdt->rn5t618->regmap, RN5T618_WATCHDOG, val);
125 if (ret)
126 return ret;
127
128 /* Clear pending watchdog interrupt */
129 return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIRQ,
130 RN5T618_PWRIRQ_IR_WDOG, 0);
131}
132
133static struct watchdog_info rn5t618_wdt_info = {
134 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
135 WDIOF_KEEPALIVEPING,
136 .identity = DRIVER_NAME,
137};
138
139static struct watchdog_ops rn5t618_wdt_ops = {
140 .owner = THIS_MODULE,
141 .start = rn5t618_wdt_start,
142 .stop = rn5t618_wdt_stop,
143 .ping = rn5t618_wdt_ping,
144 .set_timeout = rn5t618_wdt_set_timeout,
145};
146
147static int rn5t618_wdt_probe(struct platform_device *pdev)
148{
149 struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
150 struct rn5t618_wdt *wdt;
151 int min_timeout, max_timeout;
152
153 wdt = devm_kzalloc(&pdev->dev, sizeof(struct rn5t618_wdt), GFP_KERNEL);
154 if (!wdt)
155 return -ENOMEM;
156
157 min_timeout = rn5t618_wdt_map[0].time;
158 max_timeout = rn5t618_wdt_map[ARRAY_SIZE(rn5t618_wdt_map) - 1].time;
159
160 wdt->rn5t618 = rn5t618;
161 wdt->wdt_dev.info = &rn5t618_wdt_info;
162 wdt->wdt_dev.ops = &rn5t618_wdt_ops;
163 wdt->wdt_dev.min_timeout = min_timeout;
164 wdt->wdt_dev.max_timeout = max_timeout;
165 wdt->wdt_dev.timeout = max_timeout;
166 wdt->wdt_dev.parent = &pdev->dev;
167
168 watchdog_set_drvdata(&wdt->wdt_dev, wdt);
169 watchdog_init_timeout(&wdt->wdt_dev, timeout, &pdev->dev);
170 watchdog_set_nowayout(&wdt->wdt_dev, nowayout);
171
172 platform_set_drvdata(pdev, wdt);
173
174 return watchdog_register_device(&wdt->wdt_dev);
175}
176
177static int rn5t618_wdt_remove(struct platform_device *pdev)
178{
179 struct rn5t618_wdt *wdt = platform_get_drvdata(pdev);
180
181 watchdog_unregister_device(&wdt->wdt_dev);
182
183 return 0;
184}
185
186static struct platform_driver rn5t618_wdt_driver = {
187 .probe = rn5t618_wdt_probe,
188 .remove = rn5t618_wdt_remove,
189 .driver = {
190 .name = DRIVER_NAME,
191 },
192};
193
194module_platform_driver(rn5t618_wdt_driver);
195
196MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
197MODULE_DESCRIPTION("RN5T618 watchdog driver");
198MODULE_LICENSE("GPL v2");