drm/exynos: Remove redundant check in exynos_drm_fimd.c file
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / watchdog / da9052_wdt.c
1 /*
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>
24
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
31 struct da9052_wdt_data {
32         struct watchdog_device wdt;
33         struct da9052 *da9052;
34         struct kref kref;
35         unsigned long jpast;
36 };
37
38 static const struct {
39         u8 reg_val;
40         int time;  /* Seconds */
41 } da9052_wdt_maps[] = {
42         { 1, 2 },
43         { 2, 4 },
44         { 3, 8 },
45         { 4, 16 },
46         { 5, 32 },
47         { 5, 33 },  /* Actual time  32.768s so included both 32s and 33s */
48         { 6, 65 },
49         { 6, 66 },  /* Actual time 65.536s so include both, 65s and 66s */
50         { 7, 131 },
51 };
52
53
54 static void da9052_wdt_release_resources(struct kref *r)
55 {
56         struct da9052_wdt_data *driver_data =
57                 container_of(r, struct da9052_wdt_data, kref);
58
59         kfree(driver_data);
60 }
61
62 static int da9052_wdt_set_timeout(struct watchdog_device *wdt_dev,
63                                   unsigned int timeout)
64 {
65         struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
66         struct da9052 *da9052 = driver_data->da9052;
67         int ret, i;
68
69         /*
70          * Disable the Watchdog timer before setting
71          * new time out.
72          */
73         ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
74                                 DA9052_CONTROLD_TWDSCALE, 0);
75         if (ret < 0) {
76                 dev_err(da9052->dev, "Failed to disable watchdog bit, %d\n",
77                         ret);
78                 return ret;
79         }
80         if (timeout) {
81                 /*
82                  * To change the timeout, da9052 needs to
83                  * be disabled for at least 150 us.
84                  */
85                 udelay(150);
86
87                 /* Set the desired timeout */
88                 for (i = 0; i < ARRAY_SIZE(da9052_wdt_maps); i++)
89                         if (da9052_wdt_maps[i].time == timeout)
90                                 break;
91
92                 if (i == ARRAY_SIZE(da9052_wdt_maps))
93                         ret = -EINVAL;
94                 else
95                         ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
96                                                 DA9052_CONTROLD_TWDSCALE,
97                                                 da9052_wdt_maps[i].reg_val);
98                 if (ret < 0) {
99                         dev_err(da9052->dev,
100                                 "Failed to update timescale bit, %d\n", ret);
101                         return ret;
102                 }
103
104                 wdt_dev->timeout = timeout;
105                 driver_data->jpast = jiffies;
106         }
107
108         return 0;
109 }
110
111 static void da9052_wdt_ref(struct watchdog_device *wdt_dev)
112 {
113         struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
114
115         kref_get(&driver_data->kref);
116 }
117
118 static void da9052_wdt_unref(struct watchdog_device *wdt_dev)
119 {
120         struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
121
122         kref_put(&driver_data->kref, da9052_wdt_release_resources);
123 }
124
125 static int da9052_wdt_start(struct watchdog_device *wdt_dev)
126 {
127         return da9052_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
128 }
129
130 static int da9052_wdt_stop(struct watchdog_device *wdt_dev)
131 {
132         return da9052_wdt_set_timeout(wdt_dev, 0);
133 }
134
135 static int da9052_wdt_ping(struct watchdog_device *wdt_dev)
136 {
137         struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
138         struct da9052 *da9052 = driver_data->da9052;
139         unsigned long msec, jnow = jiffies;
140         int ret;
141
142         /*
143          * We have a minimum time for watchdog window called TWDMIN. A write
144          * to the watchdog before this elapsed time should cause an error.
145          */
146         msec = (jnow - driver_data->jpast) * 1000/HZ;
147         if (msec < DA9052_TWDMIN)
148                 mdelay(msec);
149
150         /* Reset the watchdog timer */
151         ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
152                                 DA9052_CONTROLD_WATCHDOG, 1 << 7);
153         if (ret < 0)
154                 goto err_strobe;
155
156         /*
157          * FIXME: Reset the watchdog core, in general PMIC
158          * is supposed to do this
159          */
160         ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
161                                 DA9052_CONTROLD_WATCHDOG, 0 << 7);
162 err_strobe:
163         return ret;
164 }
165
166 static struct watchdog_info da9052_wdt_info = {
167         .options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
168         .identity       = "DA9052 Watchdog",
169 };
170
171 static const struct watchdog_ops da9052_wdt_ops = {
172         .owner = THIS_MODULE,
173         .start = da9052_wdt_start,
174         .stop = da9052_wdt_stop,
175         .ping = da9052_wdt_ping,
176         .set_timeout = da9052_wdt_set_timeout,
177         .ref = da9052_wdt_ref,
178         .unref = da9052_wdt_unref,
179 };
180
181
182 static int __devinit da9052_wdt_probe(struct platform_device *pdev)
183 {
184         struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
185         struct da9052_wdt_data *driver_data;
186         struct watchdog_device *da9052_wdt;
187         int ret;
188
189         driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
190                                    GFP_KERNEL);
191         if (!driver_data) {
192                 dev_err(da9052->dev, "Unable to alloacate watchdog device\n");
193                 ret = -ENOMEM;
194                 goto err;
195         }
196         driver_data->da9052 = da9052;
197
198         da9052_wdt = &driver_data->wdt;
199
200         da9052_wdt->timeout = DA9052_DEF_TIMEOUT;
201         da9052_wdt->info = &da9052_wdt_info;
202         da9052_wdt->ops = &da9052_wdt_ops;
203         watchdog_set_drvdata(da9052_wdt, driver_data);
204
205         kref_init(&driver_data->kref);
206
207         ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
208                                 DA9052_CONTROLD_TWDSCALE, 0);
209         if (ret < 0) {
210                 dev_err(&pdev->dev, "Failed to disable watchdog bits, %d\n",
211                         ret);
212                 goto err;
213         }
214
215         ret = watchdog_register_device(&driver_data->wdt);
216         if (ret != 0) {
217                 dev_err(da9052->dev, "watchdog_register_device() failed: %d\n",
218                         ret);
219                 goto err;
220         }
221
222         dev_set_drvdata(&pdev->dev, driver_data);
223 err:
224         return ret;
225 }
226
227 static int __devexit da9052_wdt_remove(struct platform_device *pdev)
228 {
229         struct da9052_wdt_data *driver_data = dev_get_drvdata(&pdev->dev);
230
231         watchdog_unregister_device(&driver_data->wdt);
232         kref_put(&driver_data->kref, da9052_wdt_release_resources);
233
234         return 0;
235 }
236
237 static struct platform_driver da9052_wdt_driver = {
238         .probe = da9052_wdt_probe,
239         .remove = __devexit_p(da9052_wdt_remove),
240         .driver = {
241                 .name   = "da9052-watchdog",
242         },
243 };
244
245 module_platform_driver(da9052_wdt_driver);
246
247 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
248 MODULE_DESCRIPTION("DA9052 SM Device Driver");
249 MODULE_LICENSE("GPL");
250 MODULE_ALIAS("platform:da9052-watchdog");