blob: 9bf3cc0f396106c730383ab561f4457876c6af85 [file] [log] [blame]
Timo Kokkonen80e45b12009-03-27 16:42:17 +02001/*
2 * Copyright (C) Nokia Corporation
3 *
4 * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/module.h>
22#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020024#include <linux/kernel.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020025#include <linux/watchdog.h>
26#include <linux/platform_device.h>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010027#include <linux/i2c/twl.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020028
29#define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
30
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010031static bool nowayout = WATCHDOG_NOWAYOUT;
32module_param(nowayout, bool, 0);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020033MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
34 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
35
36static int twl4030_wdt_write(unsigned char val)
37{
Peter Ujfalusi2bc3f622012-11-13 10:40:22 +010038 return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val,
Timo Kokkonen80e45b12009-03-27 16:42:17 +020039 TWL4030_WATCHDOG_CFG_REG_OFFS);
40}
41
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030042static int twl4030_wdt_start(struct watchdog_device *wdt)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020043{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030044 return twl4030_wdt_write(wdt->timeout + 1);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020045}
46
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030047static int twl4030_wdt_stop(struct watchdog_device *wdt)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020048{
49 return twl4030_wdt_write(0);
50}
51
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030052static int twl4030_wdt_set_timeout(struct watchdog_device *wdt,
53 unsigned int timeout)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020054{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030055 wdt->timeout = timeout;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020056 return 0;
57}
58
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030059static const struct watchdog_info twl4030_wdt_info = {
Tony Lindgrenfb1cbea2014-10-14 12:25:19 -070060 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030061 .identity = "TWL4030 Watchdog",
62};
Timo Kokkonen80e45b12009-03-27 16:42:17 +020063
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030064static const struct watchdog_ops twl4030_wdt_ops = {
Timo Kokkonen80e45b12009-03-27 16:42:17 +020065 .owner = THIS_MODULE,
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030066 .start = twl4030_wdt_start,
67 .stop = twl4030_wdt_stop,
68 .set_timeout = twl4030_wdt_set_timeout,
Timo Kokkonen80e45b12009-03-27 16:42:17 +020069};
70
Bill Pemberton2d991a12012-11-19 13:21:41 -050071static int twl4030_wdt_probe(struct platform_device *pdev)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020072{
73 int ret = 0;
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030074 struct watchdog_device *wdt;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020075
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030076 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020077 if (!wdt)
78 return -ENOMEM;
79
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030080 wdt->info = &twl4030_wdt_info;
81 wdt->ops = &twl4030_wdt_ops;
82 wdt->status = 0;
83 wdt->timeout = 30;
84 wdt->min_timeout = 1;
85 wdt->max_timeout = 30;
Pratyush Anand65518812015-08-20 14:05:01 +053086 wdt->parent = &pdev->dev;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020087
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030088 watchdog_set_nowayout(wdt, nowayout);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020089 platform_set_drvdata(pdev, wdt);
90
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030091 twl4030_wdt_stop(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020092
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030093 ret = watchdog_register_device(wdt);
Sachin Kamat6638f4e2013-05-04 01:41:18 +053094 if (ret)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020095 return ret;
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030096
Timo Kokkonen80e45b12009-03-27 16:42:17 +020097 return 0;
98}
99
Bill Pemberton4b12b892012-11-19 13:26:24 -0500100static int twl4030_wdt_remove(struct platform_device *pdev)
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200101{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +0300102 struct watchdog_device *wdt = platform_get_drvdata(pdev);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200103
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +0300104 watchdog_unregister_device(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200105
106 return 0;
107}
108
109#ifdef CONFIG_PM
110static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
111{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +0300112 struct watchdog_device *wdt = platform_get_drvdata(pdev);
113 if (watchdog_active(wdt))
114 return twl4030_wdt_stop(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200115
116 return 0;
117}
118
119static int twl4030_wdt_resume(struct platform_device *pdev)
120{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +0300121 struct watchdog_device *wdt = platform_get_drvdata(pdev);
122 if (watchdog_active(wdt))
123 return twl4030_wdt_start(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200124
125 return 0;
126}
127#else
128#define twl4030_wdt_suspend NULL
129#define twl4030_wdt_resume NULL
130#endif
131
Aaro Koskinen8899b8d2012-12-23 22:03:37 +0200132static const struct of_device_id twl_wdt_of_match[] = {
133 { .compatible = "ti,twl4030-wdt", },
134 { },
135};
136MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
137
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200138static struct platform_driver twl4030_wdt_driver = {
139 .probe = twl4030_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500140 .remove = twl4030_wdt_remove,
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200141 .suspend = twl4030_wdt_suspend,
142 .resume = twl4030_wdt_resume,
143 .driver = {
Aaro Koskinen8899b8d2012-12-23 22:03:37 +0200144 .name = "twl4030_wdt",
145 .of_match_table = twl_wdt_of_match,
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200146 },
147};
148
Axel Linb8ec6112011-11-29 13:56:27 +0800149module_platform_driver(twl4030_wdt_driver);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200150
151MODULE_AUTHOR("Nokia Corporation");
152MODULE_LICENSE("GPL");
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200153MODULE_ALIAS("platform:twl4030_wdt");
154