This source file includes following definitions.
- ltc2952_poweroff_timer_wde
- ltc2952_poweroff_start_wde
- ltc2952_poweroff_timer_trigger
- ltc2952_poweroff_handler
- ltc2952_poweroff_kill
- ltc2952_poweroff_default
- ltc2952_poweroff_init
- ltc2952_poweroff_notify_panic
- ltc2952_poweroff_probe
- ltc2952_poweroff_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/interrupt.h>
49 #include <linux/device.h>
50 #include <linux/platform_device.h>
51 #include <linux/ktime.h>
52 #include <linux/slab.h>
53 #include <linux/kmod.h>
54 #include <linux/module.h>
55 #include <linux/mod_devicetable.h>
56 #include <linux/gpio/consumer.h>
57 #include <linux/reboot.h>
58
59 struct ltc2952_poweroff {
60 struct hrtimer timer_trigger;
61 struct hrtimer timer_wde;
62
63 ktime_t trigger_delay;
64 ktime_t wde_interval;
65
66 struct device *dev;
67
68 struct gpio_desc *gpio_trigger;
69 struct gpio_desc *gpio_watchdog;
70 struct gpio_desc *gpio_kill;
71
72 bool kernel_panic;
73 struct notifier_block panic_notifier;
74 };
75
76 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
77
78
79
80
81
82 static struct ltc2952_poweroff *ltc2952_data;
83
84
85
86
87
88
89
90
91
92
93 static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
94 {
95 ktime_t now;
96 int state;
97 unsigned long overruns;
98 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
99
100 if (data->kernel_panic)
101 return HRTIMER_NORESTART;
102
103 state = gpiod_get_value(data->gpio_watchdog);
104 gpiod_set_value(data->gpio_watchdog, !state);
105
106 now = hrtimer_cb_get_time(timer);
107 overruns = hrtimer_forward(timer, now, data->wde_interval);
108
109 return HRTIMER_RESTART;
110 }
111
112 static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
113 {
114 hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
115 }
116
117 static enum hrtimer_restart
118 ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
119 {
120 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
121
122 ltc2952_poweroff_start_wde(data);
123 dev_info(data->dev, "executing shutdown\n");
124 orderly_poweroff(true);
125
126 return HRTIMER_NORESTART;
127 }
128
129
130
131
132
133
134
135
136
137
138 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
139 {
140 struct ltc2952_poweroff *data = dev_id;
141
142 if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
143
144 return IRQ_HANDLED;
145 }
146
147 if (gpiod_get_value(data->gpio_trigger)) {
148 hrtimer_start(&data->timer_trigger, data->trigger_delay,
149 HRTIMER_MODE_REL);
150 } else {
151 hrtimer_cancel(&data->timer_trigger);
152 }
153 return IRQ_HANDLED;
154 }
155
156 static void ltc2952_poweroff_kill(void)
157 {
158 gpiod_set_value(ltc2952_data->gpio_kill, 1);
159 }
160
161 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
162 {
163 data->wde_interval = 300L * 1E6L;
164 data->trigger_delay = ktime_set(2, 500L*1E6L);
165
166 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
167 data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
168
169 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
170 data->timer_wde.function = ltc2952_poweroff_timer_wde;
171 }
172
173 static int ltc2952_poweroff_init(struct platform_device *pdev)
174 {
175 int ret;
176 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
177
178 ltc2952_poweroff_default(data);
179
180 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
181 GPIOD_OUT_LOW);
182 if (IS_ERR(data->gpio_watchdog)) {
183 ret = PTR_ERR(data->gpio_watchdog);
184 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
185 return ret;
186 }
187
188 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
189 if (IS_ERR(data->gpio_kill)) {
190 ret = PTR_ERR(data->gpio_kill);
191 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
192 return ret;
193 }
194
195 data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
196 GPIOD_IN);
197 if (IS_ERR(data->gpio_trigger)) {
198
199
200
201
202
203 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
204 data->gpio_trigger = NULL;
205 }
206
207 if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
208 ltc2952_poweroff_handler,
209 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
210 "ltc2952-poweroff",
211 data)) {
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 if (data->gpio_trigger) {
228 dev_warn(&pdev->dev,
229 "unable to configure the trigger interrupt\n");
230 devm_gpiod_put(&pdev->dev, data->gpio_trigger);
231 data->gpio_trigger = NULL;
232 }
233 dev_info(&pdev->dev,
234 "power down trigger input will not be used\n");
235 ltc2952_poweroff_start_wde(data);
236 }
237
238 return 0;
239 }
240
241 static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
242 unsigned long code, void *unused)
243 {
244 struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
245
246 data->kernel_panic = true;
247 return NOTIFY_DONE;
248 }
249
250 static int ltc2952_poweroff_probe(struct platform_device *pdev)
251 {
252 int ret;
253 struct ltc2952_poweroff *data;
254
255 if (pm_power_off) {
256 dev_err(&pdev->dev, "pm_power_off already registered");
257 return -EBUSY;
258 }
259
260 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
261 if (!data)
262 return -ENOMEM;
263
264 data->dev = &pdev->dev;
265 platform_set_drvdata(pdev, data);
266
267 ret = ltc2952_poweroff_init(pdev);
268 if (ret)
269 return ret;
270
271
272 ltc2952_data = data;
273 pm_power_off = ltc2952_poweroff_kill;
274
275 data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
276 atomic_notifier_chain_register(&panic_notifier_list,
277 &data->panic_notifier);
278 dev_info(&pdev->dev, "probe successful\n");
279
280 return 0;
281 }
282
283 static int ltc2952_poweroff_remove(struct platform_device *pdev)
284 {
285 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
286
287 pm_power_off = NULL;
288 hrtimer_cancel(&data->timer_trigger);
289 hrtimer_cancel(&data->timer_wde);
290 atomic_notifier_chain_unregister(&panic_notifier_list,
291 &data->panic_notifier);
292 return 0;
293 }
294
295 static const struct of_device_id of_ltc2952_poweroff_match[] = {
296 { .compatible = "lltc,ltc2952"},
297 {},
298 };
299 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
300
301 static struct platform_driver ltc2952_poweroff_driver = {
302 .probe = ltc2952_poweroff_probe,
303 .remove = ltc2952_poweroff_remove,
304 .driver = {
305 .name = "ltc2952-poweroff",
306 .of_match_table = of_ltc2952_poweroff_match,
307 },
308 };
309
310 module_platform_driver(ltc2952_poweroff_driver);
311
312 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
313 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
314 MODULE_LICENSE("GPL v2");