.. | .. |
---|
7 | 7 | #include <linux/kernel.h> |
---|
8 | 8 | #include <linux/module.h> |
---|
9 | 9 | #include <linux/of.h> |
---|
10 | | -#include <linux/of_device.h> |
---|
11 | 10 | #include <linux/platform_device.h> |
---|
| 11 | +#include <linux/pm_wakeirq.h> |
---|
12 | 12 | #include <linux/rtc.h> |
---|
13 | 13 | #include <linux/clk.h> |
---|
14 | 14 | #include <linux/mfd/syscon.h> |
---|
.. | .. |
---|
31 | 31 | |
---|
32 | 32 | #define SNVS_LPPGDR_INIT 0x41736166 |
---|
33 | 33 | #define CNTR_TO_SECS_SH 15 |
---|
| 34 | + |
---|
| 35 | +/* The maximum RTC clock cycles that are allowed to pass between two |
---|
| 36 | + * consecutive clock counter register reads. If the values are corrupted a |
---|
| 37 | + * bigger difference is expected. The RTC frequency is 32kHz. With 320 cycles |
---|
| 38 | + * we end at 10ms which should be enough for most cases. If it once takes |
---|
| 39 | + * longer than expected we do a retry. |
---|
| 40 | + */ |
---|
| 41 | +#define MAX_RTC_READ_DIFF_CYCLES 320 |
---|
34 | 42 | |
---|
35 | 43 | struct snvs_rtc_data { |
---|
36 | 44 | struct rtc_device *rtc; |
---|
.. | .. |
---|
56 | 64 | static u32 rtc_read_lp_counter(struct snvs_rtc_data *data) |
---|
57 | 65 | { |
---|
58 | 66 | u64 read1, read2; |
---|
| 67 | + s64 diff; |
---|
59 | 68 | unsigned int timeout = 100; |
---|
60 | 69 | |
---|
61 | 70 | /* As expected, the registers might update between the read of the LSB |
---|
.. | .. |
---|
66 | 75 | do { |
---|
67 | 76 | read2 = read1; |
---|
68 | 77 | read1 = rtc_read_lpsrt(data); |
---|
69 | | - } while (read1 != read2 && --timeout); |
---|
| 78 | + diff = read1 - read2; |
---|
| 79 | + } while (((diff < 0) || (diff > MAX_RTC_READ_DIFF_CYCLES)) && --timeout); |
---|
70 | 80 | if (!timeout) |
---|
71 | 81 | dev_err(&data->rtc->dev, "Timeout trying to get valid LPSRT Counter read\n"); |
---|
72 | 82 | |
---|
.. | .. |
---|
78 | 88 | static int rtc_read_lp_counter_lsb(struct snvs_rtc_data *data, u32 *lsb) |
---|
79 | 89 | { |
---|
80 | 90 | u32 count1, count2; |
---|
| 91 | + s32 diff; |
---|
81 | 92 | unsigned int timeout = 100; |
---|
82 | 93 | |
---|
83 | 94 | regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count1); |
---|
84 | 95 | do { |
---|
85 | 96 | count2 = count1; |
---|
86 | 97 | regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count1); |
---|
87 | | - } while (count1 != count2 && --timeout); |
---|
| 98 | + diff = count1 - count2; |
---|
| 99 | + } while (((diff < 0) || (diff > MAX_RTC_READ_DIFF_CYCLES)) && --timeout); |
---|
88 | 100 | if (!timeout) { |
---|
89 | 101 | dev_err(&data->rtc->dev, "Timeout trying to get valid LPSRT Counter read\n"); |
---|
90 | 102 | return -ETIMEDOUT; |
---|
.. | .. |
---|
148 | 160 | static int snvs_rtc_read_time(struct device *dev, struct rtc_time *tm) |
---|
149 | 161 | { |
---|
150 | 162 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
151 | | - unsigned long time = rtc_read_lp_counter(data); |
---|
| 163 | + unsigned long time; |
---|
| 164 | + int ret; |
---|
152 | 165 | |
---|
153 | | - rtc_time_to_tm(time, tm); |
---|
| 166 | + if (data->clk) { |
---|
| 167 | + ret = clk_enable(data->clk); |
---|
| 168 | + if (ret) |
---|
| 169 | + return ret; |
---|
| 170 | + } |
---|
| 171 | + |
---|
| 172 | + time = rtc_read_lp_counter(data); |
---|
| 173 | + rtc_time64_to_tm(time, tm); |
---|
| 174 | + |
---|
| 175 | + if (data->clk) |
---|
| 176 | + clk_disable(data->clk); |
---|
154 | 177 | |
---|
155 | 178 | return 0; |
---|
156 | 179 | } |
---|
.. | .. |
---|
158 | 181 | static int snvs_rtc_set_time(struct device *dev, struct rtc_time *tm) |
---|
159 | 182 | { |
---|
160 | 183 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
161 | | - unsigned long time; |
---|
| 184 | + unsigned long time = rtc_tm_to_time64(tm); |
---|
162 | 185 | int ret; |
---|
163 | 186 | |
---|
164 | | - rtc_tm_to_time(tm, &time); |
---|
| 187 | + if (data->clk) { |
---|
| 188 | + ret = clk_enable(data->clk); |
---|
| 189 | + if (ret) |
---|
| 190 | + return ret; |
---|
| 191 | + } |
---|
165 | 192 | |
---|
166 | 193 | /* Disable RTC first */ |
---|
167 | 194 | ret = snvs_rtc_enable(data, false); |
---|
.. | .. |
---|
175 | 202 | /* Enable RTC again */ |
---|
176 | 203 | ret = snvs_rtc_enable(data, true); |
---|
177 | 204 | |
---|
| 205 | + if (data->clk) |
---|
| 206 | + clk_disable(data->clk); |
---|
| 207 | + |
---|
178 | 208 | return ret; |
---|
179 | 209 | } |
---|
180 | 210 | |
---|
.. | .. |
---|
182 | 212 | { |
---|
183 | 213 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
184 | 214 | u32 lptar, lpsr; |
---|
| 215 | + int ret; |
---|
| 216 | + |
---|
| 217 | + if (data->clk) { |
---|
| 218 | + ret = clk_enable(data->clk); |
---|
| 219 | + if (ret) |
---|
| 220 | + return ret; |
---|
| 221 | + } |
---|
185 | 222 | |
---|
186 | 223 | regmap_read(data->regmap, data->offset + SNVS_LPTAR, &lptar); |
---|
187 | | - rtc_time_to_tm(lptar, &alrm->time); |
---|
| 224 | + rtc_time64_to_tm(lptar, &alrm->time); |
---|
188 | 225 | |
---|
189 | 226 | regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr); |
---|
190 | 227 | alrm->pending = (lpsr & SNVS_LPSR_LPTA) ? 1 : 0; |
---|
| 228 | + |
---|
| 229 | + if (data->clk) |
---|
| 230 | + clk_disable(data->clk); |
---|
191 | 231 | |
---|
192 | 232 | return 0; |
---|
193 | 233 | } |
---|
.. | .. |
---|
195 | 235 | static int snvs_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
---|
196 | 236 | { |
---|
197 | 237 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
| 238 | + int ret; |
---|
| 239 | + |
---|
| 240 | + if (data->clk) { |
---|
| 241 | + ret = clk_enable(data->clk); |
---|
| 242 | + if (ret) |
---|
| 243 | + return ret; |
---|
| 244 | + } |
---|
198 | 245 | |
---|
199 | 246 | regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, |
---|
200 | 247 | (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN), |
---|
201 | 248 | enable ? (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN) : 0); |
---|
202 | 249 | |
---|
203 | | - return rtc_write_sync_lp(data); |
---|
| 250 | + ret = rtc_write_sync_lp(data); |
---|
| 251 | + |
---|
| 252 | + if (data->clk) |
---|
| 253 | + clk_disable(data->clk); |
---|
| 254 | + |
---|
| 255 | + return ret; |
---|
204 | 256 | } |
---|
205 | 257 | |
---|
206 | 258 | static int snvs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
---|
207 | 259 | { |
---|
208 | 260 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
209 | | - struct rtc_time *alrm_tm = &alrm->time; |
---|
210 | | - unsigned long time; |
---|
| 261 | + unsigned long time = rtc_tm_to_time64(&alrm->time); |
---|
211 | 262 | int ret; |
---|
212 | 263 | |
---|
213 | | - rtc_tm_to_time(alrm_tm, &time); |
---|
| 264 | + if (data->clk) { |
---|
| 265 | + ret = clk_enable(data->clk); |
---|
| 266 | + if (ret) |
---|
| 267 | + return ret; |
---|
| 268 | + } |
---|
214 | 269 | |
---|
215 | 270 | regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_LPTA_EN, 0); |
---|
216 | 271 | ret = rtc_write_sync_lp(data); |
---|
.. | .. |
---|
220 | 275 | |
---|
221 | 276 | /* Clear alarm interrupt status bit */ |
---|
222 | 277 | regmap_write(data->regmap, data->offset + SNVS_LPSR, SNVS_LPSR_LPTA); |
---|
| 278 | + |
---|
| 279 | + if (data->clk) |
---|
| 280 | + clk_disable(data->clk); |
---|
223 | 281 | |
---|
224 | 282 | return snvs_rtc_alarm_irq_enable(dev, alrm->enabled); |
---|
225 | 283 | } |
---|
.. | .. |
---|
239 | 297 | u32 lpsr; |
---|
240 | 298 | u32 events = 0; |
---|
241 | 299 | |
---|
| 300 | + if (data->clk) |
---|
| 301 | + clk_enable(data->clk); |
---|
| 302 | + |
---|
242 | 303 | regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr); |
---|
243 | 304 | |
---|
244 | 305 | if (lpsr & SNVS_LPSR_LPTA) { |
---|
.. | .. |
---|
253 | 314 | /* clear interrupt status */ |
---|
254 | 315 | regmap_write(data->regmap, data->offset + SNVS_LPSR, lpsr); |
---|
255 | 316 | |
---|
| 317 | + if (data->clk) |
---|
| 318 | + clk_disable(data->clk); |
---|
| 319 | + |
---|
256 | 320 | return events ? IRQ_HANDLED : IRQ_NONE; |
---|
257 | 321 | } |
---|
258 | 322 | |
---|
.. | .. |
---|
262 | 326 | .reg_stride = 4, |
---|
263 | 327 | }; |
---|
264 | 328 | |
---|
| 329 | +static void snvs_rtc_action(void *data) |
---|
| 330 | +{ |
---|
| 331 | + if (data) |
---|
| 332 | + clk_disable_unprepare(data); |
---|
| 333 | +} |
---|
| 334 | + |
---|
265 | 335 | static int snvs_rtc_probe(struct platform_device *pdev) |
---|
266 | 336 | { |
---|
267 | 337 | struct snvs_rtc_data *data; |
---|
268 | | - struct resource *res; |
---|
269 | 338 | int ret; |
---|
270 | 339 | void __iomem *mmio; |
---|
271 | 340 | |
---|
.. | .. |
---|
281 | 350 | |
---|
282 | 351 | if (IS_ERR(data->regmap)) { |
---|
283 | 352 | dev_warn(&pdev->dev, "snvs rtc: you use old dts file, please update it\n"); |
---|
284 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
---|
285 | 353 | |
---|
286 | | - mmio = devm_ioremap_resource(&pdev->dev, res); |
---|
| 354 | + mmio = devm_platform_ioremap_resource(pdev, 0); |
---|
287 | 355 | if (IS_ERR(mmio)) |
---|
288 | 356 | return PTR_ERR(mmio); |
---|
289 | 357 | |
---|
.. | .. |
---|
314 | 382 | } |
---|
315 | 383 | } |
---|
316 | 384 | |
---|
| 385 | + ret = devm_add_action_or_reset(&pdev->dev, snvs_rtc_action, data->clk); |
---|
| 386 | + if (ret) |
---|
| 387 | + return ret; |
---|
| 388 | + |
---|
317 | 389 | platform_set_drvdata(pdev, data); |
---|
318 | 390 | |
---|
319 | 391 | /* Initialize glitch detect */ |
---|
.. | .. |
---|
326 | 398 | ret = snvs_rtc_enable(data, true); |
---|
327 | 399 | if (ret) { |
---|
328 | 400 | dev_err(&pdev->dev, "failed to enable rtc %d\n", ret); |
---|
329 | | - goto error_rtc_device_register; |
---|
| 401 | + return ret; |
---|
330 | 402 | } |
---|
331 | 403 | |
---|
332 | 404 | device_init_wakeup(&pdev->dev, true); |
---|
| 405 | + ret = dev_pm_set_wake_irq(&pdev->dev, data->irq); |
---|
| 406 | + if (ret) |
---|
| 407 | + dev_err(&pdev->dev, "failed to enable irq wake\n"); |
---|
333 | 408 | |
---|
334 | 409 | ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler, |
---|
335 | 410 | IRQF_SHARED, "rtc alarm", &pdev->dev); |
---|
336 | 411 | if (ret) { |
---|
337 | 412 | dev_err(&pdev->dev, "failed to request irq %d: %d\n", |
---|
338 | 413 | data->irq, ret); |
---|
339 | | - goto error_rtc_device_register; |
---|
| 414 | + return ret; |
---|
340 | 415 | } |
---|
341 | 416 | |
---|
342 | 417 | data->rtc->ops = &snvs_rtc_ops; |
---|
343 | | - ret = rtc_register_device(data->rtc); |
---|
344 | | - if (ret) { |
---|
345 | | - dev_err(&pdev->dev, "failed to register rtc: %d\n", ret); |
---|
346 | | - goto error_rtc_device_register; |
---|
347 | | - } |
---|
| 418 | + data->rtc->range_max = U32_MAX; |
---|
348 | 419 | |
---|
349 | | - return 0; |
---|
350 | | - |
---|
351 | | -error_rtc_device_register: |
---|
352 | | - if (data->clk) |
---|
353 | | - clk_disable_unprepare(data->clk); |
---|
354 | | - |
---|
355 | | - return ret; |
---|
| 420 | + return rtc_register_device(data->rtc); |
---|
356 | 421 | } |
---|
357 | 422 | |
---|
358 | | -#ifdef CONFIG_PM_SLEEP |
---|
359 | | -static int snvs_rtc_suspend(struct device *dev) |
---|
360 | | -{ |
---|
361 | | - struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
362 | | - |
---|
363 | | - if (device_may_wakeup(dev)) |
---|
364 | | - return enable_irq_wake(data->irq); |
---|
365 | | - |
---|
366 | | - return 0; |
---|
367 | | -} |
---|
368 | | - |
---|
369 | | -static int snvs_rtc_suspend_noirq(struct device *dev) |
---|
| 423 | +static int __maybe_unused snvs_rtc_suspend_noirq(struct device *dev) |
---|
370 | 424 | { |
---|
371 | 425 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
372 | 426 | |
---|
373 | 427 | if (data->clk) |
---|
374 | | - clk_disable_unprepare(data->clk); |
---|
| 428 | + clk_disable(data->clk); |
---|
375 | 429 | |
---|
376 | 430 | return 0; |
---|
377 | 431 | } |
---|
378 | 432 | |
---|
379 | | -static int snvs_rtc_resume(struct device *dev) |
---|
380 | | -{ |
---|
381 | | - struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
382 | | - |
---|
383 | | - if (device_may_wakeup(dev)) |
---|
384 | | - return disable_irq_wake(data->irq); |
---|
385 | | - |
---|
386 | | - return 0; |
---|
387 | | -} |
---|
388 | | - |
---|
389 | | -static int snvs_rtc_resume_noirq(struct device *dev) |
---|
| 433 | +static int __maybe_unused snvs_rtc_resume_noirq(struct device *dev) |
---|
390 | 434 | { |
---|
391 | 435 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
---|
392 | 436 | |
---|
393 | 437 | if (data->clk) |
---|
394 | | - return clk_prepare_enable(data->clk); |
---|
| 438 | + return clk_enable(data->clk); |
---|
395 | 439 | |
---|
396 | 440 | return 0; |
---|
397 | 441 | } |
---|
398 | 442 | |
---|
399 | 443 | static const struct dev_pm_ops snvs_rtc_pm_ops = { |
---|
400 | | - .suspend = snvs_rtc_suspend, |
---|
401 | | - .suspend_noirq = snvs_rtc_suspend_noirq, |
---|
402 | | - .resume = snvs_rtc_resume, |
---|
403 | | - .resume_noirq = snvs_rtc_resume_noirq, |
---|
| 444 | + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(snvs_rtc_suspend_noirq, snvs_rtc_resume_noirq) |
---|
404 | 445 | }; |
---|
405 | | - |
---|
406 | | -#define SNVS_RTC_PM_OPS (&snvs_rtc_pm_ops) |
---|
407 | | - |
---|
408 | | -#else |
---|
409 | | - |
---|
410 | | -#define SNVS_RTC_PM_OPS NULL |
---|
411 | | - |
---|
412 | | -#endif |
---|
413 | 446 | |
---|
414 | 447 | static const struct of_device_id snvs_dt_ids[] = { |
---|
415 | 448 | { .compatible = "fsl,sec-v4.0-mon-rtc-lp", }, |
---|
.. | .. |
---|
420 | 453 | static struct platform_driver snvs_rtc_driver = { |
---|
421 | 454 | .driver = { |
---|
422 | 455 | .name = "snvs_rtc", |
---|
423 | | - .pm = SNVS_RTC_PM_OPS, |
---|
| 456 | + .pm = &snvs_rtc_pm_ops, |
---|
424 | 457 | .of_match_table = snvs_dt_ids, |
---|
425 | 458 | }, |
---|
426 | 459 | .probe = snvs_rtc_probe, |
---|