blob: 26a5b2984094da437cb768140b7d1330e7a134d6 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Ashish Jangam312b00e12012-10-12 15:00:03 +05302/*
3 * System monitoring driver for DA9055 PMICs.
4 *
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 *
7 * Author: David Dajun Chen <dchen@diasemi.com>
8 *
Ashish Jangam312b00e12012-10-12 15:00:03 +05309 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/watchdog.h>
17#include <linux/delay.h>
18
19#include <linux/mfd/da9055/core.h>
20#include <linux/mfd/da9055/reg.h>
21
22static bool nowayout = WATCHDOG_NOWAYOUT;
23module_param(nowayout, bool, 0);
24MODULE_PARM_DESC(nowayout,
25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27
28#define DA9055_DEF_TIMEOUT 4
29#define DA9055_TWDMIN 256
30
31struct da9055_wdt_data {
32 struct watchdog_device wdt;
33 struct da9055 *da9055;
Ashish Jangam312b00e12012-10-12 15:00:03 +053034};
35
36static const struct {
37 u8 reg_val;
38 int user_time; /* In seconds */
39} da9055_wdt_maps[] = {
40 { 0, 0 },
41 { 1, 2 },
42 { 2, 4 },
43 { 3, 8 },
44 { 4, 16 },
45 { 5, 32 },
46 { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
47 { 6, 65 },
48 { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
49 { 7, 131 },
50};
51
52static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
53 unsigned int timeout)
54{
55 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
56 struct da9055 *da9055 = driver_data->da9055;
57 int ret, i;
58
59 for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
60 if (da9055_wdt_maps[i].user_time == timeout)
61 break;
62
63 if (i == ARRAY_SIZE(da9055_wdt_maps))
64 ret = -EINVAL;
65 else
66 ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
67 DA9055_TWDSCALE_MASK,
68 da9055_wdt_maps[i].reg_val <<
69 DA9055_TWDSCALE_SHIFT);
Axel Lin98e4a292012-12-22 11:07:01 +080070 if (ret < 0) {
Ashish Jangam312b00e12012-10-12 15:00:03 +053071 dev_err(da9055->dev,
72 "Failed to update timescale bit, %d\n", ret);
Axel Lin98e4a292012-12-22 11:07:01 +080073 return ret;
74 }
Ashish Jangam312b00e12012-10-12 15:00:03 +053075
76 wdt_dev->timeout = timeout;
77
Axel Lin98e4a292012-12-22 11:07:01 +080078 return 0;
Ashish Jangam312b00e12012-10-12 15:00:03 +053079}
80
81static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
82{
83 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
84 struct da9055 *da9055 = driver_data->da9055;
Ashish Jangam312b00e12012-10-12 15:00:03 +053085
86 /*
87 * We have a minimum time for watchdog window called TWDMIN. A write
88 * to the watchdog before this elapsed time will cause an error.
89 */
90 mdelay(DA9055_TWDMIN);
91
92 /* Reset the watchdog timer */
Axel Lin98e4a292012-12-22 11:07:01 +080093 return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
94 DA9055_WATCHDOG_MASK, 1);
Ashish Jangam312b00e12012-10-12 15:00:03 +053095}
96
Ashish Jangam312b00e12012-10-12 15:00:03 +053097static int da9055_wdt_start(struct watchdog_device *wdt_dev)
98{
99 return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
100}
101
102static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
103{
104 return da9055_wdt_set_timeout(wdt_dev, 0);
105}
106
Bhumika Goyal6c368932016-12-26 22:35:11 +0530107static const struct watchdog_info da9055_wdt_info = {
Ashish Jangam312b00e12012-10-12 15:00:03 +0530108 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
109 .identity = "DA9055 Watchdog",
110};
111
112static const struct watchdog_ops da9055_wdt_ops = {
113 .owner = THIS_MODULE,
114 .start = da9055_wdt_start,
115 .stop = da9055_wdt_stop,
116 .ping = da9055_wdt_ping,
117 .set_timeout = da9055_wdt_set_timeout,
Ashish Jangam312b00e12012-10-12 15:00:03 +0530118};
119
120static int da9055_wdt_probe(struct platform_device *pdev)
121{
122 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
123 struct da9055_wdt_data *driver_data;
124 struct watchdog_device *da9055_wdt;
125 int ret;
126
127 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
128 GFP_KERNEL);
Jingoo Han8deea832014-02-11 15:46:43 +0900129 if (!driver_data)
Ashish Jangam312b00e12012-10-12 15:00:03 +0530130 return -ENOMEM;
Ashish Jangam312b00e12012-10-12 15:00:03 +0530131
132 driver_data->da9055 = da9055;
133
134 da9055_wdt = &driver_data->wdt;
135
136 da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
137 da9055_wdt->info = &da9055_wdt_info;
138 da9055_wdt->ops = &da9055_wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530139 da9055_wdt->parent = &pdev->dev;
Ashish Jangam312b00e12012-10-12 15:00:03 +0530140 watchdog_set_nowayout(da9055_wdt, nowayout);
141 watchdog_set_drvdata(da9055_wdt, driver_data);
142
Ashish Jangam312b00e12012-10-12 15:00:03 +0530143 ret = da9055_wdt_stop(da9055_wdt);
144 if (ret < 0) {
145 dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret);
Guenter Roeck89f944c02017-01-10 15:21:47 -0800146 return ret;
Ashish Jangam312b00e12012-10-12 15:00:03 +0530147 }
148
Guenter Roeck89f944c02017-01-10 15:21:47 -0800149 ret = devm_watchdog_register_device(&pdev->dev, &driver_data->wdt);
Ashish Jangam312b00e12012-10-12 15:00:03 +0530150 if (ret != 0)
151 dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
152 ret);
153
Ashish Jangam312b00e12012-10-12 15:00:03 +0530154 return ret;
155}
156
Ashish Jangam312b00e12012-10-12 15:00:03 +0530157static struct platform_driver da9055_wdt_driver = {
158 .probe = da9055_wdt_probe,
Ashish Jangam312b00e12012-10-12 15:00:03 +0530159 .driver = {
160 .name = "da9055-watchdog",
161 },
162};
163
164module_platform_driver(da9055_wdt_driver);
165
166MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
167MODULE_DESCRIPTION("DA9055 watchdog");
168MODULE_LICENSE("GPL");
169MODULE_ALIAS("platform:da9055-watchdog");