Blame view

drivers/watchdog/jz4740_wdt.c 5.09 KB
a912e80bd   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
f865c3522   Paul Cercueil   watchdog: add JZ4...
2
3
4
  /*
   *  Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
   *  JZ4740 Watchdog driver
f865c3522   Paul Cercueil   watchdog: add JZ4...
5
   */
df04cce3b   Paul Cercueil   watchdog: jz4740:...
6
  #include <linux/mfd/ingenic-tcu.h>
6d532143c   Paul Cercueil   watchdog: jz4740:...
7
  #include <linux/mfd/syscon.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
8
9
10
11
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
12
  #include <linux/watchdog.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
13
  #include <linux/platform_device.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
14
15
16
17
  #include <linux/io.h>
  #include <linux/device.h>
  #include <linux/clk.h>
  #include <linux/slab.h>
85f6df149   Axel Lin   watchdog: Convert...
18
  #include <linux/err.h>
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
19
  #include <linux/of.h>
6d532143c   Paul Cercueil   watchdog: jz4740:...
20
  #include <linux/regmap.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
21

f865c3522   Paul Cercueil   watchdog: add JZ4...
22
23
  #define DEFAULT_HEARTBEAT 5
  #define MAX_HEARTBEAT     2048
85f6df149   Axel Lin   watchdog: Convert...
24
25
26
27
28
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
  MODULE_PARM_DESC(nowayout,
  		 "Watchdog cannot be stopped once started (default="
  		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
f865c3522   Paul Cercueil   watchdog: add JZ4...
29

85f6df149   Axel Lin   watchdog: Convert...
30
31
32
33
34
35
  static unsigned int heartbeat = DEFAULT_HEARTBEAT;
  module_param(heartbeat, uint, 0);
  MODULE_PARM_DESC(heartbeat,
  		"Watchdog heartbeat period in seconds from 1 to "
  		__MODULE_STRING(MAX_HEARTBEAT) ", default "
  		__MODULE_STRING(DEFAULT_HEARTBEAT));
f865c3522   Paul Cercueil   watchdog: add JZ4...
36

85f6df149   Axel Lin   watchdog: Convert...
37
38
  struct jz4740_wdt_drvdata {
  	struct watchdog_device wdt;
6d532143c   Paul Cercueil   watchdog: jz4740:...
39
  	struct regmap *map;
1d9c30745   Paul Cercueil   watchdog: jz4740:...
40
41
  	struct clk *clk;
  	unsigned long clk_rate;
85f6df149   Axel Lin   watchdog: Convert...
42
  };
f865c3522   Paul Cercueil   watchdog: add JZ4...
43

85f6df149   Axel Lin   watchdog: Convert...
44
  static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
45
  {
85f6df149   Axel Lin   watchdog: Convert...
46
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
6d532143c   Paul Cercueil   watchdog: jz4740:...
47
  	regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
85f6df149   Axel Lin   watchdog: Convert...
48
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
49
  }
85f6df149   Axel Lin   watchdog: Convert...
50
51
  static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
  				    unsigned int new_timeout)
f865c3522   Paul Cercueil   watchdog: add JZ4...
52
  {
85f6df149   Axel Lin   watchdog: Convert...
53
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
1d9c30745   Paul Cercueil   watchdog: jz4740:...
54
  	u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout);
6d532143c   Paul Cercueil   watchdog: jz4740:...
55
  	unsigned int tcer;
f865c3522   Paul Cercueil   watchdog: add JZ4...
56

6d532143c   Paul Cercueil   watchdog: jz4740:...
57
58
  	regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
  	regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
f865c3522   Paul Cercueil   watchdog: add JZ4...
59

6d532143c   Paul Cercueil   watchdog: jz4740:...
60
61
  	regmap_write(drvdata->map, TCU_REG_WDT_TDR, timeout_value);
  	regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
f865c3522   Paul Cercueil   watchdog: add JZ4...
62

9b3461188   Paul Cercueil   watchdog: jz4740:...
63
  	if (tcer & TCU_WDT_TCER_TCEN)
6d532143c   Paul Cercueil   watchdog: jz4740:...
64
  		regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
f865c3522   Paul Cercueil   watchdog: add JZ4...
65

0197c1c49   Wim Van Sebroeck   watchdog: fix set...
66
  	wdt_dev->timeout = new_timeout;
85f6df149   Axel Lin   watchdog: Convert...
67
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
68
  }
85f6df149   Axel Lin   watchdog: Convert...
69
  static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
70
  {
9b3461188   Paul Cercueil   watchdog: jz4740:...
71
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
6d532143c   Paul Cercueil   watchdog: jz4740:...
72
  	unsigned int tcer;
1d9c30745   Paul Cercueil   watchdog: jz4740:...
73
  	int ret;
9b3461188   Paul Cercueil   watchdog: jz4740:...
74

1d9c30745   Paul Cercueil   watchdog: jz4740:...
75
76
77
  	ret = clk_prepare_enable(drvdata->clk);
  	if (ret)
  		return ret;
6d532143c   Paul Cercueil   watchdog: jz4740:...
78
  	regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
9b3461188   Paul Cercueil   watchdog: jz4740:...
79

85f6df149   Axel Lin   watchdog: Convert...
80
  	jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
f865c3522   Paul Cercueil   watchdog: add JZ4...
81

9b3461188   Paul Cercueil   watchdog: jz4740:...
82
83
  	/* Start watchdog if it wasn't started already */
  	if (!(tcer & TCU_WDT_TCER_TCEN))
6d532143c   Paul Cercueil   watchdog: jz4740:...
84
  		regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
9b3461188   Paul Cercueil   watchdog: jz4740:...
85

85f6df149   Axel Lin   watchdog: Convert...
86
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
87
  }
85f6df149   Axel Lin   watchdog: Convert...
88
  static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
89
  {
85f6df149   Axel Lin   watchdog: Convert...
90
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
742e4b630   Wim Van Sebroeck   watchdog: jz4740_...
91

6d532143c   Paul Cercueil   watchdog: jz4740:...
92
  	regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
1d9c30745   Paul Cercueil   watchdog: jz4740:...
93
  	clk_disable_unprepare(drvdata->clk);
f865c3522   Paul Cercueil   watchdog: add JZ4...
94

85f6df149   Axel Lin   watchdog: Convert...
95
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
96
  }
b4918057f   Paul Cercueil   watchdog: JZ4740:...
97
98
99
100
101
102
103
  static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
  			      unsigned long action, void *data)
  {
  	wdt_dev->timeout = 0;
  	jz4740_wdt_start(wdt_dev);
  	return 0;
  }
85f6df149   Axel Lin   watchdog: Convert...
104
105
  static const struct watchdog_info jz4740_wdt_info = {
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
f865c3522   Paul Cercueil   watchdog: add JZ4...
106
107
  	.identity = "jz4740 Watchdog",
  };
85f6df149   Axel Lin   watchdog: Convert...
108
  static const struct watchdog_ops jz4740_wdt_ops = {
f865c3522   Paul Cercueil   watchdog: add JZ4...
109
  	.owner = THIS_MODULE,
85f6df149   Axel Lin   watchdog: Convert...
110
111
112
113
  	.start = jz4740_wdt_start,
  	.stop = jz4740_wdt_stop,
  	.ping = jz4740_wdt_ping,
  	.set_timeout = jz4740_wdt_set_timeout,
b4918057f   Paul Cercueil   watchdog: JZ4740:...
114
  	.restart = jz4740_wdt_restart,
f865c3522   Paul Cercueil   watchdog: add JZ4...
115
  };
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
116
117
118
  #ifdef CONFIG_OF
  static const struct of_device_id jz4740_wdt_of_matches[] = {
  	{ .compatible = "ingenic,jz4740-watchdog", },
71246c352   Mathieu Malaterre   watchdog: jz4740:...
119
  	{ .compatible = "ingenic,jz4780-watchdog", },
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
120
121
  	{ /* sentinel */ }
  };
35ffa961d   Stephen Boyd   watchdog: jz4740:...
122
  MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
123
  #endif
2d991a164   Bill Pemberton   watchdog: remove ...
124
  static int jz4740_wdt_probe(struct platform_device *pdev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
125
  {
02189bb91   Guenter Roeck   watchdog: jz4740_...
126
  	struct device *dev = &pdev->dev;
85f6df149   Axel Lin   watchdog: Convert...
127
128
  	struct jz4740_wdt_drvdata *drvdata;
  	struct watchdog_device *jz4740_wdt;
1d9c30745   Paul Cercueil   watchdog: jz4740:...
129
130
  	long rate;
  	int ret;
85f6df149   Axel Lin   watchdog: Convert...
131

02189bb91   Guenter Roeck   watchdog: jz4740_...
132
  	drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
85f6df149   Axel Lin   watchdog: Convert...
133
  			       GFP_KERNEL);
e26e74b15   Colin Ian King   watchdog: remove ...
134
  	if (!drvdata)
85f6df149   Axel Lin   watchdog: Convert...
135
  		return -ENOMEM;
f865c3522   Paul Cercueil   watchdog: add JZ4...
136

1d9c30745   Paul Cercueil   watchdog: jz4740:...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  	drvdata->clk = devm_clk_get(&pdev->dev, "wdt");
  	if (IS_ERR(drvdata->clk)) {
  		dev_err(&pdev->dev, "cannot find WDT clock
  ");
  		return PTR_ERR(drvdata->clk);
  	}
  
  	/* Set smallest clock possible */
  	rate = clk_round_rate(drvdata->clk, 1);
  	if (rate < 0)
  		return rate;
  
  	ret = clk_set_rate(drvdata->clk, rate);
  	if (ret)
  		return ret;
f865c3522   Paul Cercueil   watchdog: add JZ4...
152

1d9c30745   Paul Cercueil   watchdog: jz4740:...
153
  	drvdata->clk_rate = rate;
85f6df149   Axel Lin   watchdog: Convert...
154
155
156
  	jz4740_wdt = &drvdata->wdt;
  	jz4740_wdt->info = &jz4740_wdt_info;
  	jz4740_wdt->ops = &jz4740_wdt_ops;
85f6df149   Axel Lin   watchdog: Convert...
157
  	jz4740_wdt->min_timeout = 1;
1d9c30745   Paul Cercueil   watchdog: jz4740:...
158
159
160
161
  	jz4740_wdt->max_timeout = 0xffff / rate;
  	jz4740_wdt->timeout = clamp(heartbeat,
  				    jz4740_wdt->min_timeout,
  				    jz4740_wdt->max_timeout);
02189bb91   Guenter Roeck   watchdog: jz4740_...
162
  	jz4740_wdt->parent = dev;
85f6df149   Axel Lin   watchdog: Convert...
163
164
  	watchdog_set_nowayout(jz4740_wdt, nowayout);
  	watchdog_set_drvdata(jz4740_wdt, drvdata);
6d532143c   Paul Cercueil   watchdog: jz4740:...
165
166
167
168
169
170
  	drvdata->map = device_node_to_regmap(dev->parent->of_node);
  	if (!drvdata->map) {
  		dev_err(dev, "regmap not found
  ");
  		return -EINVAL;
  	}
f865c3522   Paul Cercueil   watchdog: add JZ4...
171

9ee644c93   Wolfram Sang   watchdog: jz4740_...
172
  	return devm_watchdog_register_device(dev, &drvdata->wdt);
f865c3522   Paul Cercueil   watchdog: add JZ4...
173
  }
f865c3522   Paul Cercueil   watchdog: add JZ4...
174
175
  static struct platform_driver jz4740_wdt_driver = {
  	.probe = jz4740_wdt_probe,
f865c3522   Paul Cercueil   watchdog: add JZ4...
176
177
  	.driver = {
  		.name = "jz4740-wdt",
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
178
  		.of_match_table = of_match_ptr(jz4740_wdt_of_matches),
f865c3522   Paul Cercueil   watchdog: add JZ4...
179
180
  	},
  };
b8ec61189   Axel Lin   watchdog: convert...
181
  module_platform_driver(jz4740_wdt_driver);
f865c3522   Paul Cercueil   watchdog: add JZ4...
182
183
184
  
  MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
  MODULE_DESCRIPTION("jz4740 Watchdog Driver");
f865c3522   Paul Cercueil   watchdog: add JZ4...
185
  MODULE_LICENSE("GPL");
f865c3522   Paul Cercueil   watchdog: add JZ4...
186
  MODULE_ALIAS("platform:jz4740-wdt");