blob: 34d272ada23d5cb4771eeed1b3afc6471117369e [file] [log] [blame]
Mark Brown006948b2008-11-06 10:56:21 +00001/*
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
Joe Perches27c766a2012-02-15 15:06:19 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Mark Brown006948b2008-11-06 10:56:21 +000013#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
Mark Brown006948b2008-11-06 10:56:21 +000017#include <linux/platform_device.h>
18#include <linux/watchdog.h>
19#include <linux/uaccess.h>
20#include <linux/mfd/wm8350/core.h>
21
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010022static bool nowayout = WATCHDOG_NOWAYOUT;
23module_param(nowayout, bool, 0);
Mark Brown006948b2008-11-06 10:56:21 +000024MODULE_PARM_DESC(nowayout,
25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27
Mark Brown006948b2008-11-06 10:56:21 +000028static DEFINE_MUTEX(wdt_mutex);
29
30static struct {
Axel Lin2e51d902012-01-23 15:26:59 +080031 unsigned int time; /* Seconds */
32 u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */
Mark Brown006948b2008-11-06 10:56:21 +000033} wm8350_wdt_cfgs[] = {
34 { 1, 0x02 },
35 { 2, 0x04 },
36 { 4, 0x05 },
37};
38
Axel Lin2e51d902012-01-23 15:26:59 +080039static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev,
40 unsigned int timeout)
Mark Brown006948b2008-11-06 10:56:21 +000041{
Axel Lin2e51d902012-01-23 15:26:59 +080042 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
43 int ret, i;
Mark Brown006948b2008-11-06 10:56:21 +000044 u16 reg;
45
Axel Lin2e51d902012-01-23 15:26:59 +080046 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
Mark Brown006948b2008-11-06 10:56:21 +000052 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;
Axel Lin2e51d902012-01-23 15:26:59 +080057 reg |= wm8350_wdt_cfgs[i].val;
Mark Brown006948b2008-11-06 10:56:21 +000058 ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
59
60 wm8350_reg_lock(wm8350);
61 mutex_unlock(&wdt_mutex);
62
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +010063 wdt_dev->timeout = timeout;
Mark Brown006948b2008-11-06 10:56:21 +000064 return ret;
65}
66
Axel Lin2e51d902012-01-23 15:26:59 +080067static int wm8350_wdt_start(struct watchdog_device *wdt_dev)
Mark Brown006948b2008-11-06 10:56:21 +000068{
Axel Lin2e51d902012-01-23 15:26:59 +080069 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
Mark Brown006948b2008-11-06 10:56:21 +000070 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
Axel Lin2e51d902012-01-23 15:26:59 +080087static int wm8350_wdt_stop(struct watchdog_device *wdt_dev)
Mark Brown006948b2008-11-06 10:56:21 +000088{
Axel Lin2e51d902012-01-23 15:26:59 +080089 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
Mark Brown006948b2008-11-06 10:56:21 +000090 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
Axel Lin2e51d902012-01-23 15:26:59 +0800106static int wm8350_wdt_ping(struct watchdog_device *wdt_dev)
Mark Brown006948b2008-11-06 10:56:21 +0000107{
Axel Lin2e51d902012-01-23 15:26:59 +0800108 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
Mark Brown006948b2008-11-06 10:56:21 +0000109 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
Axel Lin2e51d902012-01-23 15:26:59 +0800122static const struct watchdog_info wm8350_wdt_info = {
Mark Brown006948b2008-11-06 10:56:21 +0000123 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
124 .identity = "WM8350 Watchdog",
125};
126
Axel Lin2e51d902012-01-23 15:26:59 +0800127static const struct watchdog_ops wm8350_wdt_ops = {
Mark Brown006948b2008-11-06 10:56:21 +0000128 .owner = THIS_MODULE,
Axel Lin2e51d902012-01-23 15:26:59 +0800129 .start = wm8350_wdt_start,
130 .stop = wm8350_wdt_stop,
131 .ping = wm8350_wdt_ping,
132 .set_timeout = wm8350_wdt_set_timeout,
Mark Brown006948b2008-11-06 10:56:21 +0000133};
134
Axel Lin2e51d902012-01-23 15:26:59 +0800135static 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,
Mark Brown006948b2008-11-06 10:56:21 +0000141};
142
Bill Pemberton2d991a12012-11-19 13:21:41 -0500143static int wm8350_wdt_probe(struct platform_device *pdev)
Mark Brown006948b2008-11-06 10:56:21 +0000144{
145 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
146
147 if (!wm8350) {
Julia Lawallcfca31c2010-05-27 14:32:24 +0200148 pr_err("No driver data supplied\n");
Mark Brown006948b2008-11-06 10:56:21 +0000149 return -ENODEV;
150 }
151
Axel Lin2e51d902012-01-23 15:26:59 +0800152 watchdog_set_nowayout(&wm8350_wdt, nowayout);
153 watchdog_set_drvdata(&wm8350_wdt, wm8350);
154
Mark Brown006948b2008-11-06 10:56:21 +0000155 /* Default to 4s timeout */
Axel Lin2e51d902012-01-23 15:26:59 +0800156 wm8350_wdt_set_timeout(&wm8350_wdt, 4);
Mark Brown006948b2008-11-06 10:56:21 +0000157
Axel Lin2e51d902012-01-23 15:26:59 +0800158 return watchdog_register_device(&wm8350_wdt);
Mark Brown006948b2008-11-06 10:56:21 +0000159}
160
Bill Pemberton4b12b892012-11-19 13:26:24 -0500161static int wm8350_wdt_remove(struct platform_device *pdev)
Mark Brown006948b2008-11-06 10:56:21 +0000162{
Axel Lin2e51d902012-01-23 15:26:59 +0800163 watchdog_unregister_device(&wm8350_wdt);
Mark Brown006948b2008-11-06 10:56:21 +0000164 return 0;
165}
166
167static struct platform_driver wm8350_wdt_driver = {
168 .probe = wm8350_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500169 .remove = wm8350_wdt_remove,
Mark Brown006948b2008-11-06 10:56:21 +0000170 .driver = {
171 .name = "wm8350-wdt",
172 },
173};
174
Mark Brown216f3ad2011-11-23 15:22:36 +0000175module_platform_driver(wm8350_wdt_driver);
Mark Brown006948b2008-11-06 10:56:21 +0000176
177MODULE_AUTHOR("Mark Brown");
178MODULE_DESCRIPTION("WM8350 Watchdog");
179MODULE_LICENSE("GPL");
180MODULE_ALIAS("platform:wm8350-wdt");