watchdog: intel-mid_wdt: Use 'dev' instead of dereferencing it repeatedly
[linux-2.6-block.git] / drivers / watchdog / intel-mid_wdt.c
CommitLineData
87a1ef80
DC
1/*
2 * intel-mid_wdt: generic Intel MID SCU watchdog driver
3 *
4 * Platforms supported so far:
5 * - Merrifield only
6 *
7 * Copyright (C) 2014 Intel Corporation. All rights reserved.
8 * Contact: David Cohen <david.a.cohen@linux.intel.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General
12 * Public License as published by the Free Software Foundation.
13 */
14
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/nmi.h>
18#include <linux/platform_device.h>
19#include <linux/watchdog.h>
20#include <linux/platform_data/intel-mid_wdt.h>
21
22#include <asm/intel_scu_ipc.h>
23#include <asm/intel-mid.h>
24
25#define IPC_WATCHDOG 0xf8
26
27#define MID_WDT_PRETIMEOUT 15
28#define MID_WDT_TIMEOUT_MIN (1 + MID_WDT_PRETIMEOUT)
29#define MID_WDT_TIMEOUT_MAX 170
30#define MID_WDT_DEFAULT_TIMEOUT 90
31
32/* SCU watchdog messages */
33enum {
34 SCU_WATCHDOG_START = 0,
35 SCU_WATCHDOG_STOP,
36 SCU_WATCHDOG_KEEPALIVE,
37};
38
39static inline int wdt_command(int sub, u32 *in, int inlen)
40{
41 return intel_scu_ipc_command(IPC_WATCHDOG, sub, in, inlen, NULL, 0);
42}
43
44static int wdt_start(struct watchdog_device *wd)
45{
bb790362 46 struct device *dev = watchdog_get_drvdata(wd);
87a1ef80
DC
47 int ret, in_size;
48 int timeout = wd->timeout;
49 struct ipc_wd_start {
50 u32 pretimeout;
51 u32 timeout;
52 } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
53
54 /*
55 * SCU expects the input size for watchdog IPC to
56 * be based on 4 bytes
57 */
58 in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
59
60 ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
bb790362 61 if (ret)
87a1ef80 62 dev_crit(dev, "error starting watchdog: %d\n", ret);
87a1ef80
DC
63
64 return ret;
65}
66
67static int wdt_ping(struct watchdog_device *wd)
68{
bb790362 69 struct device *dev = watchdog_get_drvdata(wd);
87a1ef80
DC
70 int ret;
71
72 ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
bb790362
AS
73 if (ret)
74 dev_crit(dev, "Error executing keepalive: %d\n", ret);
87a1ef80
DC
75
76 return ret;
77}
78
79static int wdt_stop(struct watchdog_device *wd)
80{
bb790362 81 struct device *dev = watchdog_get_drvdata(wd);
87a1ef80
DC
82 int ret;
83
84 ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
bb790362
AS
85 if (ret)
86 dev_crit(dev, "Error stopping watchdog: %d\n", ret);
87a1ef80
DC
87
88 return ret;
89}
90
91static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
92{
93 panic("Kernel Watchdog");
94
95 /* This code should not be reached */
96 return IRQ_HANDLED;
97}
98
99static const struct watchdog_info mid_wdt_info = {
100 .identity = "Intel MID SCU watchdog",
8cbb97ea 101 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
87a1ef80
DC
102};
103
104static const struct watchdog_ops mid_wdt_ops = {
105 .owner = THIS_MODULE,
106 .start = wdt_start,
107 .stop = wdt_stop,
108 .ping = wdt_ping,
109};
110
111static int mid_wdt_probe(struct platform_device *pdev)
112{
b7b6adf3 113 struct device *dev = &pdev->dev;
87a1ef80 114 struct watchdog_device *wdt_dev;
b7b6adf3 115 struct intel_mid_wdt_pdata *pdata = dev->platform_data;
87a1ef80
DC
116 int ret;
117
118 if (!pdata) {
b7b6adf3 119 dev_err(dev, "missing platform data\n");
87a1ef80
DC
120 return -EINVAL;
121 }
122
123 if (pdata->probe) {
124 ret = pdata->probe(pdev);
125 if (ret)
126 return ret;
127 }
128
b7b6adf3 129 wdt_dev = devm_kzalloc(dev, sizeof(*wdt_dev), GFP_KERNEL);
87a1ef80
DC
130 if (!wdt_dev)
131 return -ENOMEM;
132
133 wdt_dev->info = &mid_wdt_info;
134 wdt_dev->ops = &mid_wdt_ops;
135 wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
136 wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
137 wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
b7b6adf3 138 wdt_dev->parent = dev;
87a1ef80 139
b7b6adf3 140 watchdog_set_drvdata(wdt_dev, dev);
87a1ef80 141
b7b6adf3 142 ret = devm_request_irq(dev, pdata->irq, mid_wdt_irq,
87a1ef80
DC
143 IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
144 wdt_dev);
145 if (ret) {
b7b6adf3 146 dev_err(dev, "error requesting warning irq %d\n", pdata->irq);
87a1ef80
DC
147 return ret;
148 }
149
954351e8
AS
150 /*
151 * The firmware followed by U-Boot leaves the watchdog running
152 * with the default threshold which may vary. When we get here
153 * we should make a decision to prevent any side effects before
154 * user space daemon will take care of it. The best option,
155 * taking into consideration that there is no way to read values
156 * back from hardware, is to enforce watchdog being run with
157 * deterministic values.
158 */
159 ret = wdt_start(wdt_dev);
160 if (ret)
161 return ret;
162
163 /* Make sure the watchdog is serviced */
164 set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
31ecad65 165
b7b6adf3 166 ret = devm_watchdog_register_device(dev, wdt_dev);
87a1ef80 167 if (ret) {
b7b6adf3 168 dev_err(dev, "error registering watchdog device\n");
87a1ef80
DC
169 return ret;
170 }
171
b7b6adf3 172 dev_info(dev, "Intel MID watchdog device probed\n");
87a1ef80
DC
173
174 return 0;
175}
176
87a1ef80
DC
177static struct platform_driver mid_wdt_driver = {
178 .probe = mid_wdt_probe,
87a1ef80 179 .driver = {
87a1ef80
DC
180 .name = "intel_mid_wdt",
181 },
182};
183
184module_platform_driver(mid_wdt_driver);
185
186MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
187MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
188MODULE_LICENSE("GPL");