blob: d6d5006efa717df162a4746341467d96a677f412 [file] [log] [blame]
Ashish Jangam664a0d72012-05-24 18:31:14 +05301/*
2 * System monitoring driver for DA9052 PMICs.
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: Anthony Olech <Anthony.Olech@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/delay.h>
17#include <linux/uaccess.h>
18#include <linux/platform_device.h>
19#include <linux/time.h>
20#include <linux/watchdog.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/jiffies.h>
Ashish Jangam664a0d72012-05-24 18:31:14 +053024
25#include <linux/mfd/da9052/reg.h>
26#include <linux/mfd/da9052/da9052.h>
27
28#define DA9052_DEF_TIMEOUT 4
29#define DA9052_TWDMIN 256
30
31struct da9052_wdt_data {
32 struct watchdog_device wdt;
33 struct da9052 *da9052;
Ashish Jangam664a0d72012-05-24 18:31:14 +053034 unsigned long jpast;
35};
36
37static const struct {
38 u8 reg_val;
39 int time; /* Seconds */
40} da9052_wdt_maps[] = {
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
52
Ashish Jangam664a0d72012-05-24 18:31:14 +053053static int da9052_wdt_set_timeout(struct watchdog_device *wdt_dev,
54 unsigned int timeout)
55{
56 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
57 struct da9052 *da9052 = driver_data->da9052;
58 int ret, i;
59
60 /*
61 * Disable the Watchdog timer before setting
62 * new time out.
63 */
64 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
65 DA9052_CONTROLD_TWDSCALE, 0);
66 if (ret < 0) {
67 dev_err(da9052->dev, "Failed to disable watchdog bit, %d\n",
68 ret);
69 return ret;
70 }
71 if (timeout) {
72 /*
73 * To change the timeout, da9052 needs to
74 * be disabled for at least 150 us.
75 */
76 udelay(150);
77
78 /* Set the desired timeout */
79 for (i = 0; i < ARRAY_SIZE(da9052_wdt_maps); i++)
80 if (da9052_wdt_maps[i].time == timeout)
81 break;
82
83 if (i == ARRAY_SIZE(da9052_wdt_maps))
84 ret = -EINVAL;
85 else
86 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
87 DA9052_CONTROLD_TWDSCALE,
88 da9052_wdt_maps[i].reg_val);
89 if (ret < 0) {
90 dev_err(da9052->dev,
91 "Failed to update timescale bit, %d\n", ret);
92 return ret;
93 }
94
95 wdt_dev->timeout = timeout;
96 driver_data->jpast = jiffies;
97 }
98
99 return 0;
100}
101
Ashish Jangam664a0d72012-05-24 18:31:14 +0530102static int da9052_wdt_start(struct watchdog_device *wdt_dev)
103{
104 return da9052_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
105}
106
107static int da9052_wdt_stop(struct watchdog_device *wdt_dev)
108{
109 return da9052_wdt_set_timeout(wdt_dev, 0);
110}
111
112static int da9052_wdt_ping(struct watchdog_device *wdt_dev)
113{
114 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
115 struct da9052 *da9052 = driver_data->da9052;
116 unsigned long msec, jnow = jiffies;
117 int ret;
118
119 /*
120 * We have a minimum time for watchdog window called TWDMIN. A write
121 * to the watchdog before this elapsed time should cause an error.
122 */
123 msec = (jnow - driver_data->jpast) * 1000/HZ;
124 if (msec < DA9052_TWDMIN)
125 mdelay(msec);
126
127 /* Reset the watchdog timer */
128 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
129 DA9052_CONTROLD_WATCHDOG, 1 << 7);
130 if (ret < 0)
Guenter Roeck189c0492017-01-10 15:21:47 -0800131 return ret;
Ashish Jangam664a0d72012-05-24 18:31:14 +0530132
133 /*
134 * FIXME: Reset the watchdog core, in general PMIC
135 * is supposed to do this
136 */
Guenter Roeck189c0492017-01-10 15:21:47 -0800137 return da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
138 DA9052_CONTROLD_WATCHDOG, 0 << 7);
Ashish Jangam664a0d72012-05-24 18:31:14 +0530139}
140
Bhumika Goyal6c368932016-12-26 22:35:11 +0530141static const struct watchdog_info da9052_wdt_info = {
Ashish Jangam664a0d72012-05-24 18:31:14 +0530142 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
143 .identity = "DA9052 Watchdog",
144};
145
146static const struct watchdog_ops da9052_wdt_ops = {
147 .owner = THIS_MODULE,
148 .start = da9052_wdt_start,
149 .stop = da9052_wdt_stop,
150 .ping = da9052_wdt_ping,
151 .set_timeout = da9052_wdt_set_timeout,
Ashish Jangam664a0d72012-05-24 18:31:14 +0530152};
153
154
Bill Pemberton2d991a12012-11-19 13:21:41 -0500155static int da9052_wdt_probe(struct platform_device *pdev)
Ashish Jangam664a0d72012-05-24 18:31:14 +0530156{
157 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
158 struct da9052_wdt_data *driver_data;
159 struct watchdog_device *da9052_wdt;
160 int ret;
161
162 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
163 GFP_KERNEL);
Guenter Roeck189c0492017-01-10 15:21:47 -0800164 if (!driver_data)
165 return -ENOMEM;
Ashish Jangam664a0d72012-05-24 18:31:14 +0530166 driver_data->da9052 = da9052;
167
168 da9052_wdt = &driver_data->wdt;
169
170 da9052_wdt->timeout = DA9052_DEF_TIMEOUT;
171 da9052_wdt->info = &da9052_wdt_info;
172 da9052_wdt->ops = &da9052_wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530173 da9052_wdt->parent = &pdev->dev;
Ashish Jangam664a0d72012-05-24 18:31:14 +0530174 watchdog_set_drvdata(da9052_wdt, driver_data);
175
Ashish Jangam664a0d72012-05-24 18:31:14 +0530176 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
177 DA9052_CONTROLD_TWDSCALE, 0);
178 if (ret < 0) {
179 dev_err(&pdev->dev, "Failed to disable watchdog bits, %d\n",
180 ret);
Guenter Roeck189c0492017-01-10 15:21:47 -0800181 return ret;
Ashish Jangam664a0d72012-05-24 18:31:14 +0530182 }
183
Guenter Roeck189c0492017-01-10 15:21:47 -0800184 ret = devm_watchdog_register_device(&pdev->dev, &driver_data->wdt);
Ashish Jangam664a0d72012-05-24 18:31:14 +0530185 if (ret != 0) {
186 dev_err(da9052->dev, "watchdog_register_device() failed: %d\n",
187 ret);
Guenter Roeck189c0492017-01-10 15:21:47 -0800188 return ret;
Ashish Jangam664a0d72012-05-24 18:31:14 +0530189 }
190
Ashish Jangam664a0d72012-05-24 18:31:14 +0530191 return ret;
192}
193
Ashish Jangam664a0d72012-05-24 18:31:14 +0530194static struct platform_driver da9052_wdt_driver = {
195 .probe = da9052_wdt_probe,
Ashish Jangam664a0d72012-05-24 18:31:14 +0530196 .driver = {
197 .name = "da9052-watchdog",
198 },
199};
200
201module_platform_driver(da9052_wdt_driver);
202
203MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
204MODULE_DESCRIPTION("DA9052 SM Device Driver");
205MODULE_LICENSE("GPL");
206MODULE_ALIAS("platform:da9052-watchdog");