Commit | Line | Data |
---|---|---|
006948ba MB |
1 | /* |
2 | * Watchdog driver for the wm8350 | |
3 | * | |
4 | * Copyright (C) 2007, 2008 Wolfson Microelectronics <linux@wolfsonmicro.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 | * as published by the Free Software Foundation | |
9 | */ | |
10 | ||
27c766aa JP |
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
12 | ||
006948ba MB |
13 | #include <linux/module.h> |
14 | #include <linux/moduleparam.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/kernel.h> | |
006948ba MB |
17 | #include <linux/platform_device.h> |
18 | #include <linux/watchdog.h> | |
19 | #include <linux/uaccess.h> | |
20 | #include <linux/mfd/wm8350/core.h> | |
21 | ||
86a1e189 WVS |
22 | static bool nowayout = WATCHDOG_NOWAYOUT; |
23 | module_param(nowayout, bool, 0); | |
006948ba MB |
24 | MODULE_PARM_DESC(nowayout, |
25 | "Watchdog cannot be stopped once started (default=" | |
26 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
27 | ||
006948ba MB |
28 | static DEFINE_MUTEX(wdt_mutex); |
29 | ||
30 | static struct { | |
2e51d90f AL |
31 | unsigned int time; /* Seconds */ |
32 | u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */ | |
006948ba MB |
33 | } wm8350_wdt_cfgs[] = { |
34 | { 1, 0x02 }, | |
35 | { 2, 0x04 }, | |
36 | { 4, 0x05 }, | |
37 | }; | |
38 | ||
2e51d90f AL |
39 | static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev, |
40 | unsigned int timeout) | |
006948ba | 41 | { |
2e51d90f AL |
42 | struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev); |
43 | int ret, i; | |
006948ba MB |
44 | u16 reg; |
45 | ||
2e51d90f AL |
46 | for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++) |
47 | if (wm8350_wdt_cfgs[i].time == timeout) | |
48 | break; | |
49 | if (i == ARRAY_SIZE(wm8350_wdt_cfgs)) | |
50 | return -EINVAL; | |
51 | ||
006948ba MB |
52 | mutex_lock(&wdt_mutex); |
53 | wm8350_reg_unlock(wm8350); | |
54 | ||
55 | reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2); | |
56 | reg &= ~WM8350_WDOG_TO_MASK; | |
2e51d90f | 57 | reg |= wm8350_wdt_cfgs[i].val; |
006948ba MB |
58 | ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg); |
59 | ||
60 | wm8350_reg_lock(wm8350); | |
61 | mutex_unlock(&wdt_mutex); | |
62 | ||
0197c1c4 | 63 | wdt_dev->timeout = timeout; |
006948ba MB |
64 | return ret; |
65 | } | |
66 | ||
2e51d90f | 67 | static int wm8350_wdt_start(struct watchdog_device *wdt_dev) |
006948ba | 68 | { |
2e51d90f | 69 | struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev); |
006948ba MB |
70 | int ret; |
71 | u16 reg; | |
72 | ||
73 | mutex_lock(&wdt_mutex); | |
74 | wm8350_reg_unlock(wm8350); | |
75 | ||
76 | reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2); | |
77 | reg &= ~WM8350_WDOG_MODE_MASK; | |
78 | reg |= 0x20; | |
79 | ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg); | |
80 | ||
81 | wm8350_reg_lock(wm8350); | |
82 | mutex_unlock(&wdt_mutex); | |
83 | ||
84 | return ret; | |
85 | } | |
86 | ||
2e51d90f | 87 | static int wm8350_wdt_stop(struct watchdog_device *wdt_dev) |
006948ba | 88 | { |
2e51d90f | 89 | struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev); |
006948ba MB |
90 | int ret; |
91 | u16 reg; | |
92 | ||
93 | mutex_lock(&wdt_mutex); | |
94 | wm8350_reg_unlock(wm8350); | |
95 | ||
96 | reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2); | |
97 | reg &= ~WM8350_WDOG_MODE_MASK; | |
98 | ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg); | |
99 | ||
100 | wm8350_reg_lock(wm8350); | |
101 | mutex_unlock(&wdt_mutex); | |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
2e51d90f | 106 | static int wm8350_wdt_ping(struct watchdog_device *wdt_dev) |
006948ba | 107 | { |
2e51d90f | 108 | struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev); |
006948ba MB |
109 | int ret; |
110 | u16 reg; | |
111 | ||
112 | mutex_lock(&wdt_mutex); | |
113 | ||
114 | reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2); | |
115 | ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg); | |
116 | ||
117 | mutex_unlock(&wdt_mutex); | |
118 | ||
119 | return ret; | |
120 | } | |
121 | ||
2e51d90f | 122 | static const struct watchdog_info wm8350_wdt_info = { |
006948ba MB |
123 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
124 | .identity = "WM8350 Watchdog", | |
125 | }; | |
126 | ||
2e51d90f | 127 | static const struct watchdog_ops wm8350_wdt_ops = { |
006948ba | 128 | .owner = THIS_MODULE, |
2e51d90f AL |
129 | .start = wm8350_wdt_start, |
130 | .stop = wm8350_wdt_stop, | |
131 | .ping = wm8350_wdt_ping, | |
132 | .set_timeout = wm8350_wdt_set_timeout, | |
006948ba MB |
133 | }; |
134 | ||
2e51d90f AL |
135 | static struct watchdog_device wm8350_wdt = { |
136 | .info = &wm8350_wdt_info, | |
137 | .ops = &wm8350_wdt_ops, | |
138 | .timeout = 4, | |
139 | .min_timeout = 1, | |
140 | .max_timeout = 4, | |
006948ba MB |
141 | }; |
142 | ||
2d991a16 | 143 | static int wm8350_wdt_probe(struct platform_device *pdev) |
006948ba MB |
144 | { |
145 | struct wm8350 *wm8350 = platform_get_drvdata(pdev); | |
146 | ||
147 | if (!wm8350) { | |
cfca31ce | 148 | pr_err("No driver data supplied\n"); |
006948ba MB |
149 | return -ENODEV; |
150 | } | |
151 | ||
2e51d90f AL |
152 | watchdog_set_nowayout(&wm8350_wdt, nowayout); |
153 | watchdog_set_drvdata(&wm8350_wdt, wm8350); | |
6551881c | 154 | wm8350_wdt.parent = &pdev->dev; |
006948ba | 155 | |
2e51d90f AL |
156 | /* Default to 4s timeout */ |
157 | wm8350_wdt_set_timeout(&wm8350_wdt, 4); | |
006948ba | 158 | |
2e51d90f | 159 | return watchdog_register_device(&wm8350_wdt); |
006948ba MB |
160 | } |
161 | ||
4b12b896 | 162 | static int wm8350_wdt_remove(struct platform_device *pdev) |
006948ba | 163 | { |
2e51d90f | 164 | watchdog_unregister_device(&wm8350_wdt); |
006948ba MB |
165 | return 0; |
166 | } | |
167 | ||
168 | static struct platform_driver wm8350_wdt_driver = { | |
169 | .probe = wm8350_wdt_probe, | |
82268714 | 170 | .remove = wm8350_wdt_remove, |
006948ba MB |
171 | .driver = { |
172 | .name = "wm8350-wdt", | |
173 | }, | |
174 | }; | |
175 | ||
216f3ad9 | 176 | module_platform_driver(wm8350_wdt_driver); |
006948ba MB |
177 | |
178 | MODULE_AUTHOR("Mark Brown"); | |
179 | MODULE_DESCRIPTION("WM8350 Watchdog"); | |
180 | MODULE_LICENSE("GPL"); | |
181 | MODULE_ALIAS("platform:wm8350-wdt"); |