wm8350_wdt.c 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Watchdog driver for the wm8350
 *
 * Copyright (C) 2007, 2008 Wolfson Microelectronics <linux@wolfsonmicro.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation
 */

11 12
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

13 14 15 16 17 18 19 20 21
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/watchdog.h>
#include <linux/uaccess.h>
#include <linux/mfd/wm8350/core.h>

W
Wim Van Sebroeck 已提交
22 23
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
24 25 26 27 28 29 30
MODULE_PARM_DESC(nowayout,
		 "Watchdog cannot be stopped once started (default="
		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

static DEFINE_MUTEX(wdt_mutex);

static struct {
31 32
	unsigned int time;  /* Seconds */
	u16 val;	    /* To be set in WM8350_SYSTEM_CONTROL_2 */
33 34 35 36 37 38
} wm8350_wdt_cfgs[] = {
	{ 1, 0x02 },
	{ 2, 0x04 },
	{ 4, 0x05 },
};

39 40
static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev,
				  unsigned int timeout)
41
{
42 43
	struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
	int ret, i;
44 45
	u16 reg;

46 47 48 49 50 51
	for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
		if (wm8350_wdt_cfgs[i].time == timeout)
			break;
	if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
		return -EINVAL;

52 53 54 55 56
	mutex_lock(&wdt_mutex);
	wm8350_reg_unlock(wm8350);

	reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
	reg &= ~WM8350_WDOG_TO_MASK;
57
	reg |= wm8350_wdt_cfgs[i].val;
58 59 60 61 62 63 64 65
	ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);

	wm8350_reg_lock(wm8350);
	mutex_unlock(&wdt_mutex);

	return ret;
}

66
static int wm8350_wdt_start(struct watchdog_device *wdt_dev)
67
{
68
	struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	int ret;
	u16 reg;

	mutex_lock(&wdt_mutex);
	wm8350_reg_unlock(wm8350);

	reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
	reg &= ~WM8350_WDOG_MODE_MASK;
	reg |= 0x20;
	ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);

	wm8350_reg_lock(wm8350);
	mutex_unlock(&wdt_mutex);

	return ret;
}

86
static int wm8350_wdt_stop(struct watchdog_device *wdt_dev)
87
{
88
	struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	int ret;
	u16 reg;

	mutex_lock(&wdt_mutex);
	wm8350_reg_unlock(wm8350);

	reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
	reg &= ~WM8350_WDOG_MODE_MASK;
	ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);

	wm8350_reg_lock(wm8350);
	mutex_unlock(&wdt_mutex);

	return ret;
}

105
static int wm8350_wdt_ping(struct watchdog_device *wdt_dev)
106
{
107
	struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
108 109 110 111 112 113 114 115 116 117 118 119 120
	int ret;
	u16 reg;

	mutex_lock(&wdt_mutex);

	reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
	ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);

	mutex_unlock(&wdt_mutex);

	return ret;
}

121
static const struct watchdog_info wm8350_wdt_info = {
122 123 124 125
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
	.identity = "WM8350 Watchdog",
};

126
static const struct watchdog_ops wm8350_wdt_ops = {
127
	.owner = THIS_MODULE,
128 129 130 131
	.start = wm8350_wdt_start,
	.stop = wm8350_wdt_stop,
	.ping = wm8350_wdt_ping,
	.set_timeout = wm8350_wdt_set_timeout,
132 133
};

134 135 136 137 138 139
static struct watchdog_device wm8350_wdt = {
	.info = &wm8350_wdt_info,
	.ops = &wm8350_wdt_ops,
	.timeout = 4,
	.min_timeout = 1,
	.max_timeout = 4,
140 141
};

142
static int __devinit wm8350_wdt_probe(struct platform_device *pdev)
143 144 145 146
{
	struct wm8350 *wm8350 = platform_get_drvdata(pdev);

	if (!wm8350) {
147
		pr_err("No driver data supplied\n");
148 149 150
		return -ENODEV;
	}

151 152
	watchdog_set_nowayout(&wm8350_wdt, nowayout);
	watchdog_set_drvdata(&wm8350_wdt, wm8350);
153

154 155
	/* Default to 4s timeout */
	wm8350_wdt_set_timeout(&wm8350_wdt, 4);
156

157
	return watchdog_register_device(&wm8350_wdt);
158 159
}

160
static int __devexit wm8350_wdt_remove(struct platform_device *pdev)
161
{
162
	watchdog_unregister_device(&wm8350_wdt);
163 164 165 166 167
	return 0;
}

static struct platform_driver wm8350_wdt_driver = {
	.probe = wm8350_wdt_probe,
168
	.remove = __devexit_p(wm8350_wdt_remove),
169 170 171 172 173
	.driver = {
		.name = "wm8350-wdt",
	},
};

174
module_platform_driver(wm8350_wdt_driver);
175 176 177 178 179

MODULE_AUTHOR("Mark Brown");
MODULE_DESCRIPTION("WM8350 Watchdog");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:wm8350-wdt");