blob: 8377c43f3f20ff0c7537642b05ac580badffdd71 [file] [log] [blame]
Ashish Jangam312b00e12012-10-12 15:00:03 +05301/*
2 * System monitoring driver for DA9055 PMICs.
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/watchdog.h>
21#include <linux/delay.h>
22
23#include <linux/mfd/da9055/core.h>
24#include <linux/mfd/da9055/reg.h>
25
26static bool nowayout = WATCHDOG_NOWAYOUT;
27module_param(nowayout, bool, 0);
28MODULE_PARM_DESC(nowayout,
29 "Watchdog cannot be stopped once started (default="
30 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
31
32#define DA9055_DEF_TIMEOUT 4
33#define DA9055_TWDMIN 256
34
35struct da9055_wdt_data {
36 struct watchdog_device wdt;
37 struct da9055 *da9055;
Ashish Jangam312b00e12012-10-12 15:00:03 +053038};
39
40static const struct {
41 u8 reg_val;
42 int user_time; /* In seconds */
43} da9055_wdt_maps[] = {
44 { 0, 0 },
45 { 1, 2 },
46 { 2, 4 },
47 { 3, 8 },
48 { 4, 16 },
49 { 5, 32 },
50 { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
51 { 6, 65 },
52 { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
53 { 7, 131 },
54};
55
56static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
57 unsigned int timeout)
58{
59 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
60 struct da9055 *da9055 = driver_data->da9055;
61 int ret, i;
62
63 for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
64 if (da9055_wdt_maps[i].user_time == timeout)
65 break;
66
67 if (i == ARRAY_SIZE(da9055_wdt_maps))
68 ret = -EINVAL;
69 else
70 ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
71 DA9055_TWDSCALE_MASK,
72 da9055_wdt_maps[i].reg_val <<
73 DA9055_TWDSCALE_SHIFT);
Axel Lin98e4a292012-12-22 11:07:01 +080074 if (ret < 0) {
Ashish Jangam312b00e12012-10-12 15:00:03 +053075 dev_err(da9055->dev,
76 "Failed to update timescale bit, %d\n", ret);
Axel Lin98e4a292012-12-22 11:07:01 +080077 return ret;
78 }
Ashish Jangam312b00e12012-10-12 15:00:03 +053079
80 wdt_dev->timeout = timeout;
81
Axel Lin98e4a292012-12-22 11:07:01 +080082 return 0;
Ashish Jangam312b00e12012-10-12 15:00:03 +053083}
84
85static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
86{
87 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
88 struct da9055 *da9055 = driver_data->da9055;
Ashish Jangam312b00e12012-10-12 15:00:03 +053089
90 /*
91 * We have a minimum time for watchdog window called TWDMIN. A write
92 * to the watchdog before this elapsed time will cause an error.
93 */
94 mdelay(DA9055_TWDMIN);
95
96 /* Reset the watchdog timer */
Axel Lin98e4a292012-12-22 11:07:01 +080097 return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
98 DA9055_WATCHDOG_MASK, 1);
Ashish Jangam312b00e12012-10-12 15:00:03 +053099}
100
Ashish Jangam312b00e12012-10-12 15:00:03 +0530101static int da9055_wdt_start(struct watchdog_device *wdt_dev)
102{
103 return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
104}
105
106static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
107{
108 return da9055_wdt_set_timeout(wdt_dev, 0);
109}
110
111static struct watchdog_info da9055_wdt_info = {
112 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
113 .identity = "DA9055 Watchdog",
114};
115
116static const struct watchdog_ops da9055_wdt_ops = {
117 .owner = THIS_MODULE,
118 .start = da9055_wdt_start,
119 .stop = da9055_wdt_stop,
120 .ping = da9055_wdt_ping,
121 .set_timeout = da9055_wdt_set_timeout,
Ashish Jangam312b00e12012-10-12 15:00:03 +0530122};
123
124static int da9055_wdt_probe(struct platform_device *pdev)
125{
126 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
127 struct da9055_wdt_data *driver_data;
128 struct watchdog_device *da9055_wdt;
129 int ret;
130
131 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
132 GFP_KERNEL);
Jingoo Han8deea832014-02-11 15:46:43 +0900133 if (!driver_data)
Ashish Jangam312b00e12012-10-12 15:00:03 +0530134 return -ENOMEM;
Ashish Jangam312b00e12012-10-12 15:00:03 +0530135
136 driver_data->da9055 = da9055;
137
138 da9055_wdt = &driver_data->wdt;
139
140 da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
141 da9055_wdt->info = &da9055_wdt_info;
142 da9055_wdt->ops = &da9055_wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530143 da9055_wdt->parent = &pdev->dev;
Ashish Jangam312b00e12012-10-12 15:00:03 +0530144 watchdog_set_nowayout(da9055_wdt, nowayout);
145 watchdog_set_drvdata(da9055_wdt, driver_data);
146
Ashish Jangam312b00e12012-10-12 15:00:03 +0530147 ret = da9055_wdt_stop(da9055_wdt);
148 if (ret < 0) {
149 dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret);
150 goto err;
151 }
152
Jingoo Han76692c92013-05-23 19:44:23 +0900153 platform_set_drvdata(pdev, driver_data);
Ashish Jangam312b00e12012-10-12 15:00:03 +0530154
155 ret = watchdog_register_device(&driver_data->wdt);
156 if (ret != 0)
157 dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
158 ret);
159
160err:
161 return ret;
162}
163
164static int da9055_wdt_remove(struct platform_device *pdev)
165{
Jingoo Han76692c92013-05-23 19:44:23 +0900166 struct da9055_wdt_data *driver_data = platform_get_drvdata(pdev);
Ashish Jangam312b00e12012-10-12 15:00:03 +0530167
168 watchdog_unregister_device(&driver_data->wdt);
Ashish Jangam312b00e12012-10-12 15:00:03 +0530169
170 return 0;
171}
172
173static struct platform_driver da9055_wdt_driver = {
174 .probe = da9055_wdt_probe,
175 .remove = da9055_wdt_remove,
176 .driver = {
177 .name = "da9055-watchdog",
178 },
179};
180
181module_platform_driver(da9055_wdt_driver);
182
183MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
184MODULE_DESCRIPTION("DA9055 watchdog");
185MODULE_LICENSE("GPL");
186MODULE_ALIAS("platform:da9055-watchdog");