| .. | .. |
|---|
| 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> |
|---|
| .. | .. |
|---|
| 148 | 148 | static int snvs_rtc_read_time(struct device *dev, struct rtc_time *tm) |
|---|
| 149 | 149 | { |
|---|
| 150 | 150 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 151 | | - unsigned long time = rtc_read_lp_counter(data); |
|---|
| 151 | + unsigned long time; |
|---|
| 152 | + int ret; |
|---|
| 152 | 153 | |
|---|
| 153 | | - rtc_time_to_tm(time, tm); |
|---|
| 154 | + if (data->clk) { |
|---|
| 155 | + ret = clk_enable(data->clk); |
|---|
| 156 | + if (ret) |
|---|
| 157 | + return ret; |
|---|
| 158 | + } |
|---|
| 159 | + |
|---|
| 160 | + time = rtc_read_lp_counter(data); |
|---|
| 161 | + rtc_time64_to_tm(time, tm); |
|---|
| 162 | + |
|---|
| 163 | + if (data->clk) |
|---|
| 164 | + clk_disable(data->clk); |
|---|
| 154 | 165 | |
|---|
| 155 | 166 | return 0; |
|---|
| 156 | 167 | } |
|---|
| .. | .. |
|---|
| 158 | 169 | static int snvs_rtc_set_time(struct device *dev, struct rtc_time *tm) |
|---|
| 159 | 170 | { |
|---|
| 160 | 171 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 161 | | - unsigned long time; |
|---|
| 172 | + unsigned long time = rtc_tm_to_time64(tm); |
|---|
| 162 | 173 | int ret; |
|---|
| 163 | 174 | |
|---|
| 164 | | - rtc_tm_to_time(tm, &time); |
|---|
| 175 | + if (data->clk) { |
|---|
| 176 | + ret = clk_enable(data->clk); |
|---|
| 177 | + if (ret) |
|---|
| 178 | + return ret; |
|---|
| 179 | + } |
|---|
| 165 | 180 | |
|---|
| 166 | 181 | /* Disable RTC first */ |
|---|
| 167 | 182 | ret = snvs_rtc_enable(data, false); |
|---|
| .. | .. |
|---|
| 175 | 190 | /* Enable RTC again */ |
|---|
| 176 | 191 | ret = snvs_rtc_enable(data, true); |
|---|
| 177 | 192 | |
|---|
| 193 | + if (data->clk) |
|---|
| 194 | + clk_disable(data->clk); |
|---|
| 195 | + |
|---|
| 178 | 196 | return ret; |
|---|
| 179 | 197 | } |
|---|
| 180 | 198 | |
|---|
| .. | .. |
|---|
| 182 | 200 | { |
|---|
| 183 | 201 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 184 | 202 | u32 lptar, lpsr; |
|---|
| 203 | + int ret; |
|---|
| 204 | + |
|---|
| 205 | + if (data->clk) { |
|---|
| 206 | + ret = clk_enable(data->clk); |
|---|
| 207 | + if (ret) |
|---|
| 208 | + return ret; |
|---|
| 209 | + } |
|---|
| 185 | 210 | |
|---|
| 186 | 211 | regmap_read(data->regmap, data->offset + SNVS_LPTAR, &lptar); |
|---|
| 187 | | - rtc_time_to_tm(lptar, &alrm->time); |
|---|
| 212 | + rtc_time64_to_tm(lptar, &alrm->time); |
|---|
| 188 | 213 | |
|---|
| 189 | 214 | regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr); |
|---|
| 190 | 215 | alrm->pending = (lpsr & SNVS_LPSR_LPTA) ? 1 : 0; |
|---|
| 216 | + |
|---|
| 217 | + if (data->clk) |
|---|
| 218 | + clk_disable(data->clk); |
|---|
| 191 | 219 | |
|---|
| 192 | 220 | return 0; |
|---|
| 193 | 221 | } |
|---|
| .. | .. |
|---|
| 195 | 223 | static int snvs_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
|---|
| 196 | 224 | { |
|---|
| 197 | 225 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 226 | + int ret; |
|---|
| 227 | + |
|---|
| 228 | + if (data->clk) { |
|---|
| 229 | + ret = clk_enable(data->clk); |
|---|
| 230 | + if (ret) |
|---|
| 231 | + return ret; |
|---|
| 232 | + } |
|---|
| 198 | 233 | |
|---|
| 199 | 234 | regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, |
|---|
| 200 | 235 | (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN), |
|---|
| 201 | 236 | enable ? (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN) : 0); |
|---|
| 202 | 237 | |
|---|
| 203 | | - return rtc_write_sync_lp(data); |
|---|
| 238 | + ret = rtc_write_sync_lp(data); |
|---|
| 239 | + |
|---|
| 240 | + if (data->clk) |
|---|
| 241 | + clk_disable(data->clk); |
|---|
| 242 | + |
|---|
| 243 | + return ret; |
|---|
| 204 | 244 | } |
|---|
| 205 | 245 | |
|---|
| 206 | 246 | static int snvs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
|---|
| 207 | 247 | { |
|---|
| 208 | 248 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 209 | | - struct rtc_time *alrm_tm = &alrm->time; |
|---|
| 210 | | - unsigned long time; |
|---|
| 249 | + unsigned long time = rtc_tm_to_time64(&alrm->time); |
|---|
| 211 | 250 | int ret; |
|---|
| 212 | 251 | |
|---|
| 213 | | - rtc_tm_to_time(alrm_tm, &time); |
|---|
| 252 | + if (data->clk) { |
|---|
| 253 | + ret = clk_enable(data->clk); |
|---|
| 254 | + if (ret) |
|---|
| 255 | + return ret; |
|---|
| 256 | + } |
|---|
| 214 | 257 | |
|---|
| 215 | 258 | regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_LPTA_EN, 0); |
|---|
| 216 | 259 | ret = rtc_write_sync_lp(data); |
|---|
| .. | .. |
|---|
| 220 | 263 | |
|---|
| 221 | 264 | /* Clear alarm interrupt status bit */ |
|---|
| 222 | 265 | regmap_write(data->regmap, data->offset + SNVS_LPSR, SNVS_LPSR_LPTA); |
|---|
| 266 | + |
|---|
| 267 | + if (data->clk) |
|---|
| 268 | + clk_disable(data->clk); |
|---|
| 223 | 269 | |
|---|
| 224 | 270 | return snvs_rtc_alarm_irq_enable(dev, alrm->enabled); |
|---|
| 225 | 271 | } |
|---|
| .. | .. |
|---|
| 239 | 285 | u32 lpsr; |
|---|
| 240 | 286 | u32 events = 0; |
|---|
| 241 | 287 | |
|---|
| 288 | + if (data->clk) |
|---|
| 289 | + clk_enable(data->clk); |
|---|
| 290 | + |
|---|
| 242 | 291 | regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr); |
|---|
| 243 | 292 | |
|---|
| 244 | 293 | if (lpsr & SNVS_LPSR_LPTA) { |
|---|
| .. | .. |
|---|
| 253 | 302 | /* clear interrupt status */ |
|---|
| 254 | 303 | regmap_write(data->regmap, data->offset + SNVS_LPSR, lpsr); |
|---|
| 255 | 304 | |
|---|
| 305 | + if (data->clk) |
|---|
| 306 | + clk_disable(data->clk); |
|---|
| 307 | + |
|---|
| 256 | 308 | return events ? IRQ_HANDLED : IRQ_NONE; |
|---|
| 257 | 309 | } |
|---|
| 258 | 310 | |
|---|
| .. | .. |
|---|
| 262 | 314 | .reg_stride = 4, |
|---|
| 263 | 315 | }; |
|---|
| 264 | 316 | |
|---|
| 317 | +static void snvs_rtc_action(void *data) |
|---|
| 318 | +{ |
|---|
| 319 | + if (data) |
|---|
| 320 | + clk_disable_unprepare(data); |
|---|
| 321 | +} |
|---|
| 322 | + |
|---|
| 265 | 323 | static int snvs_rtc_probe(struct platform_device *pdev) |
|---|
| 266 | 324 | { |
|---|
| 267 | 325 | struct snvs_rtc_data *data; |
|---|
| 268 | | - struct resource *res; |
|---|
| 269 | 326 | int ret; |
|---|
| 270 | 327 | void __iomem *mmio; |
|---|
| 271 | 328 | |
|---|
| .. | .. |
|---|
| 281 | 338 | |
|---|
| 282 | 339 | if (IS_ERR(data->regmap)) { |
|---|
| 283 | 340 | 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 | 341 | |
|---|
| 286 | | - mmio = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 342 | + mmio = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 287 | 343 | if (IS_ERR(mmio)) |
|---|
| 288 | 344 | return PTR_ERR(mmio); |
|---|
| 289 | 345 | |
|---|
| .. | .. |
|---|
| 314 | 370 | } |
|---|
| 315 | 371 | } |
|---|
| 316 | 372 | |
|---|
| 373 | + ret = devm_add_action_or_reset(&pdev->dev, snvs_rtc_action, data->clk); |
|---|
| 374 | + if (ret) |
|---|
| 375 | + return ret; |
|---|
| 376 | + |
|---|
| 317 | 377 | platform_set_drvdata(pdev, data); |
|---|
| 318 | 378 | |
|---|
| 319 | 379 | /* Initialize glitch detect */ |
|---|
| .. | .. |
|---|
| 326 | 386 | ret = snvs_rtc_enable(data, true); |
|---|
| 327 | 387 | if (ret) { |
|---|
| 328 | 388 | dev_err(&pdev->dev, "failed to enable rtc %d\n", ret); |
|---|
| 329 | | - goto error_rtc_device_register; |
|---|
| 389 | + return ret; |
|---|
| 330 | 390 | } |
|---|
| 331 | 391 | |
|---|
| 332 | 392 | device_init_wakeup(&pdev->dev, true); |
|---|
| 393 | + ret = dev_pm_set_wake_irq(&pdev->dev, data->irq); |
|---|
| 394 | + if (ret) |
|---|
| 395 | + dev_err(&pdev->dev, "failed to enable irq wake\n"); |
|---|
| 333 | 396 | |
|---|
| 334 | 397 | ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler, |
|---|
| 335 | 398 | IRQF_SHARED, "rtc alarm", &pdev->dev); |
|---|
| 336 | 399 | if (ret) { |
|---|
| 337 | 400 | dev_err(&pdev->dev, "failed to request irq %d: %d\n", |
|---|
| 338 | 401 | data->irq, ret); |
|---|
| 339 | | - goto error_rtc_device_register; |
|---|
| 402 | + return ret; |
|---|
| 340 | 403 | } |
|---|
| 341 | 404 | |
|---|
| 342 | 405 | 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 | | - } |
|---|
| 406 | + data->rtc->range_max = U32_MAX; |
|---|
| 348 | 407 | |
|---|
| 349 | | - return 0; |
|---|
| 350 | | - |
|---|
| 351 | | -error_rtc_device_register: |
|---|
| 352 | | - if (data->clk) |
|---|
| 353 | | - clk_disable_unprepare(data->clk); |
|---|
| 354 | | - |
|---|
| 355 | | - return ret; |
|---|
| 408 | + return rtc_register_device(data->rtc); |
|---|
| 356 | 409 | } |
|---|
| 357 | 410 | |
|---|
| 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) |
|---|
| 411 | +static int __maybe_unused snvs_rtc_suspend_noirq(struct device *dev) |
|---|
| 370 | 412 | { |
|---|
| 371 | 413 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 372 | 414 | |
|---|
| 373 | 415 | if (data->clk) |
|---|
| 374 | | - clk_disable_unprepare(data->clk); |
|---|
| 416 | + clk_disable(data->clk); |
|---|
| 375 | 417 | |
|---|
| 376 | 418 | return 0; |
|---|
| 377 | 419 | } |
|---|
| 378 | 420 | |
|---|
| 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) |
|---|
| 421 | +static int __maybe_unused snvs_rtc_resume_noirq(struct device *dev) |
|---|
| 390 | 422 | { |
|---|
| 391 | 423 | struct snvs_rtc_data *data = dev_get_drvdata(dev); |
|---|
| 392 | 424 | |
|---|
| 393 | 425 | if (data->clk) |
|---|
| 394 | | - return clk_prepare_enable(data->clk); |
|---|
| 426 | + return clk_enable(data->clk); |
|---|
| 395 | 427 | |
|---|
| 396 | 428 | return 0; |
|---|
| 397 | 429 | } |
|---|
| 398 | 430 | |
|---|
| 399 | 431 | 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, |
|---|
| 432 | + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(snvs_rtc_suspend_noirq, snvs_rtc_resume_noirq) |
|---|
| 404 | 433 | }; |
|---|
| 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 | 434 | |
|---|
| 414 | 435 | static const struct of_device_id snvs_dt_ids[] = { |
|---|
| 415 | 436 | { .compatible = "fsl,sec-v4.0-mon-rtc-lp", }, |
|---|
| .. | .. |
|---|
| 420 | 441 | static struct platform_driver snvs_rtc_driver = { |
|---|
| 421 | 442 | .driver = { |
|---|
| 422 | 443 | .name = "snvs_rtc", |
|---|
| 423 | | - .pm = SNVS_RTC_PM_OPS, |
|---|
| 444 | + .pm = &snvs_rtc_pm_ops, |
|---|
| 424 | 445 | .of_match_table = snvs_dt_ids, |
|---|
| 425 | 446 | }, |
|---|
| 426 | 447 | .probe = snvs_rtc_probe, |
|---|