| .. | .. |
|---|
| 8 | 8 | #include <linux/slab.h> |
|---|
| 9 | 9 | #include <linux/interrupt.h> |
|---|
| 10 | 10 | #include <linux/platform_device.h> |
|---|
| 11 | +#include <linux/pm_wakeirq.h> |
|---|
| 11 | 12 | #include <linux/clk.h> |
|---|
| 12 | 13 | #include <linux/of.h> |
|---|
| 13 | 14 | #include <linux/of_device.h> |
|---|
| .. | .. |
|---|
| 183 | 184 | struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
|---|
| 184 | 185 | void __iomem *ioaddr = pdata->ioaddr; |
|---|
| 185 | 186 | u32 reg; |
|---|
| 187 | + unsigned long flags; |
|---|
| 186 | 188 | |
|---|
| 187 | | - spin_lock_irq(&pdata->rtc->irq_lock); |
|---|
| 189 | + spin_lock_irqsave(&pdata->rtc->irq_lock, flags); |
|---|
| 188 | 190 | reg = readw(ioaddr + RTC_RTCIENR); |
|---|
| 189 | 191 | |
|---|
| 190 | 192 | if (enabled) |
|---|
| .. | .. |
|---|
| 193 | 195 | reg &= ~bit; |
|---|
| 194 | 196 | |
|---|
| 195 | 197 | writew(reg, ioaddr + RTC_RTCIENR); |
|---|
| 196 | | - spin_unlock_irq(&pdata->rtc->irq_lock); |
|---|
| 198 | + spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags); |
|---|
| 197 | 199 | } |
|---|
| 198 | 200 | |
|---|
| 199 | 201 | /* This function is the RTC interrupt service routine. */ |
|---|
| .. | .. |
|---|
| 253 | 255 | /* |
|---|
| 254 | 256 | * This function sets the internal RTC time based on tm in Gregorian date. |
|---|
| 255 | 257 | */ |
|---|
| 256 | | -static int mxc_rtc_set_mmss(struct device *dev, time64_t time) |
|---|
| 258 | +static int mxc_rtc_set_time(struct device *dev, struct rtc_time *tm) |
|---|
| 257 | 259 | { |
|---|
| 258 | | - struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
|---|
| 259 | | - |
|---|
| 260 | | - /* |
|---|
| 261 | | - * TTC_DAYR register is 9-bit in MX1 SoC, save time and day of year only |
|---|
| 262 | | - */ |
|---|
| 263 | | - if (is_imx1_rtc(pdata)) { |
|---|
| 264 | | - struct rtc_time tm; |
|---|
| 265 | | - |
|---|
| 266 | | - rtc_time64_to_tm(time, &tm); |
|---|
| 267 | | - tm.tm_year = 70; |
|---|
| 268 | | - time = rtc_tm_to_time64(&tm); |
|---|
| 269 | | - } |
|---|
| 260 | + time64_t time = rtc_tm_to_time64(tm); |
|---|
| 270 | 261 | |
|---|
| 271 | 262 | /* Avoid roll-over from reading the different registers */ |
|---|
| 272 | 263 | do { |
|---|
| .. | .. |
|---|
| 310 | 301 | /* RTC layer */ |
|---|
| 311 | 302 | static const struct rtc_class_ops mxc_rtc_ops = { |
|---|
| 312 | 303 | .read_time = mxc_rtc_read_time, |
|---|
| 313 | | - .set_mmss64 = mxc_rtc_set_mmss, |
|---|
| 304 | + .set_time = mxc_rtc_set_time, |
|---|
| 314 | 305 | .read_alarm = mxc_rtc_read_alarm, |
|---|
| 315 | 306 | .set_alarm = mxc_rtc_set_alarm, |
|---|
| 316 | 307 | .alarm_irq_enable = mxc_rtc_alarm_irq_enable, |
|---|
| 317 | 308 | }; |
|---|
| 318 | 309 | |
|---|
| 310 | +static void mxc_rtc_action(void *p) |
|---|
| 311 | +{ |
|---|
| 312 | + struct rtc_plat_data *pdata = p; |
|---|
| 313 | + |
|---|
| 314 | + clk_disable_unprepare(pdata->clk_ref); |
|---|
| 315 | + clk_disable_unprepare(pdata->clk_ipg); |
|---|
| 316 | +} |
|---|
| 317 | + |
|---|
| 319 | 318 | static int mxc_rtc_probe(struct platform_device *pdev) |
|---|
| 320 | 319 | { |
|---|
| 321 | | - struct resource *res; |
|---|
| 322 | 320 | struct rtc_device *rtc; |
|---|
| 323 | 321 | struct rtc_plat_data *pdata = NULL; |
|---|
| 324 | 322 | u32 reg; |
|---|
| .. | .. |
|---|
| 336 | 334 | else |
|---|
| 337 | 335 | pdata->devtype = pdev->id_entry->driver_data; |
|---|
| 338 | 336 | |
|---|
| 339 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 340 | | - pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 337 | + pdata->ioaddr = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 341 | 338 | if (IS_ERR(pdata->ioaddr)) |
|---|
| 342 | 339 | return PTR_ERR(pdata->ioaddr); |
|---|
| 340 | + |
|---|
| 341 | + rtc = devm_rtc_allocate_device(&pdev->dev); |
|---|
| 342 | + if (IS_ERR(rtc)) |
|---|
| 343 | + return PTR_ERR(rtc); |
|---|
| 344 | + |
|---|
| 345 | + pdata->rtc = rtc; |
|---|
| 346 | + rtc->ops = &mxc_rtc_ops; |
|---|
| 347 | + if (is_imx1_rtc(pdata)) { |
|---|
| 348 | + struct rtc_time tm; |
|---|
| 349 | + |
|---|
| 350 | + /* 9bit days + hours minutes seconds */ |
|---|
| 351 | + rtc->range_max = (1 << 9) * 86400 - 1; |
|---|
| 352 | + |
|---|
| 353 | + /* |
|---|
| 354 | + * Set the start date as beginning of the current year. This can |
|---|
| 355 | + * be overridden using device tree. |
|---|
| 356 | + */ |
|---|
| 357 | + rtc_time64_to_tm(ktime_get_real_seconds(), &tm); |
|---|
| 358 | + rtc->start_secs = mktime64(tm.tm_year, 1, 1, 0, 0, 0); |
|---|
| 359 | + rtc->set_start_time = true; |
|---|
| 360 | + } else { |
|---|
| 361 | + /* 16bit days + hours minutes seconds */ |
|---|
| 362 | + rtc->range_max = (1 << 16) * 86400ULL - 1; |
|---|
| 363 | + } |
|---|
| 343 | 364 | |
|---|
| 344 | 365 | pdata->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
|---|
| 345 | 366 | if (IS_ERR(pdata->clk_ipg)) { |
|---|
| .. | .. |
|---|
| 353 | 374 | |
|---|
| 354 | 375 | pdata->clk_ref = devm_clk_get(&pdev->dev, "ref"); |
|---|
| 355 | 376 | if (IS_ERR(pdata->clk_ref)) { |
|---|
| 377 | + clk_disable_unprepare(pdata->clk_ipg); |
|---|
| 356 | 378 | dev_err(&pdev->dev, "unable to get ref clock!\n"); |
|---|
| 357 | | - ret = PTR_ERR(pdata->clk_ref); |
|---|
| 358 | | - goto exit_put_clk_ipg; |
|---|
| 379 | + return PTR_ERR(pdata->clk_ref); |
|---|
| 359 | 380 | } |
|---|
| 360 | 381 | |
|---|
| 361 | 382 | ret = clk_prepare_enable(pdata->clk_ref); |
|---|
| 383 | + if (ret) { |
|---|
| 384 | + clk_disable_unprepare(pdata->clk_ipg); |
|---|
| 385 | + return ret; |
|---|
| 386 | + } |
|---|
| 387 | + |
|---|
| 388 | + ret = devm_add_action_or_reset(&pdev->dev, mxc_rtc_action, pdata); |
|---|
| 362 | 389 | if (ret) |
|---|
| 363 | | - goto exit_put_clk_ipg; |
|---|
| 390 | + return ret; |
|---|
| 364 | 391 | |
|---|
| 365 | 392 | rate = clk_get_rate(pdata->clk_ref); |
|---|
| 366 | 393 | |
|---|
| .. | .. |
|---|
| 372 | 399 | reg = RTC_INPUT_CLK_38400HZ; |
|---|
| 373 | 400 | else { |
|---|
| 374 | 401 | dev_err(&pdev->dev, "rtc clock is not valid (%lu)\n", rate); |
|---|
| 375 | | - ret = -EINVAL; |
|---|
| 376 | | - goto exit_put_clk_ref; |
|---|
| 402 | + return -EINVAL; |
|---|
| 377 | 403 | } |
|---|
| 378 | 404 | |
|---|
| 379 | 405 | reg |= RTC_ENABLE_BIT; |
|---|
| 380 | 406 | writew(reg, (pdata->ioaddr + RTC_RTCCTL)); |
|---|
| 381 | 407 | if (((readw(pdata->ioaddr + RTC_RTCCTL)) & RTC_ENABLE_BIT) == 0) { |
|---|
| 382 | 408 | dev_err(&pdev->dev, "hardware module can't be enabled!\n"); |
|---|
| 383 | | - ret = -EIO; |
|---|
| 384 | | - goto exit_put_clk_ref; |
|---|
| 409 | + return -EIO; |
|---|
| 385 | 410 | } |
|---|
| 386 | 411 | |
|---|
| 387 | 412 | platform_set_drvdata(pdev, pdata); |
|---|
| .. | .. |
|---|
| 396 | 421 | pdata->irq = -1; |
|---|
| 397 | 422 | } |
|---|
| 398 | 423 | |
|---|
| 399 | | - if (pdata->irq >= 0) |
|---|
| 424 | + if (pdata->irq >= 0) { |
|---|
| 400 | 425 | device_init_wakeup(&pdev->dev, 1); |
|---|
| 401 | | - |
|---|
| 402 | | - rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops, |
|---|
| 403 | | - THIS_MODULE); |
|---|
| 404 | | - if (IS_ERR(rtc)) { |
|---|
| 405 | | - ret = PTR_ERR(rtc); |
|---|
| 406 | | - goto exit_put_clk_ref; |
|---|
| 426 | + ret = dev_pm_set_wake_irq(&pdev->dev, pdata->irq); |
|---|
| 427 | + if (ret) |
|---|
| 428 | + dev_err(&pdev->dev, "failed to enable irq wake\n"); |
|---|
| 407 | 429 | } |
|---|
| 408 | 430 | |
|---|
| 409 | | - pdata->rtc = rtc; |
|---|
| 410 | | - |
|---|
| 411 | | - return 0; |
|---|
| 412 | | - |
|---|
| 413 | | -exit_put_clk_ref: |
|---|
| 414 | | - clk_disable_unprepare(pdata->clk_ref); |
|---|
| 415 | | -exit_put_clk_ipg: |
|---|
| 416 | | - clk_disable_unprepare(pdata->clk_ipg); |
|---|
| 431 | + ret = rtc_register_device(rtc); |
|---|
| 417 | 432 | |
|---|
| 418 | 433 | return ret; |
|---|
| 419 | 434 | } |
|---|
| 420 | | - |
|---|
| 421 | | -static int mxc_rtc_remove(struct platform_device *pdev) |
|---|
| 422 | | -{ |
|---|
| 423 | | - struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
|---|
| 424 | | - |
|---|
| 425 | | - clk_disable_unprepare(pdata->clk_ref); |
|---|
| 426 | | - clk_disable_unprepare(pdata->clk_ipg); |
|---|
| 427 | | - |
|---|
| 428 | | - return 0; |
|---|
| 429 | | -} |
|---|
| 430 | | - |
|---|
| 431 | | -#ifdef CONFIG_PM_SLEEP |
|---|
| 432 | | -static int mxc_rtc_suspend(struct device *dev) |
|---|
| 433 | | -{ |
|---|
| 434 | | - struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
|---|
| 435 | | - |
|---|
| 436 | | - if (device_may_wakeup(dev)) |
|---|
| 437 | | - enable_irq_wake(pdata->irq); |
|---|
| 438 | | - |
|---|
| 439 | | - return 0; |
|---|
| 440 | | -} |
|---|
| 441 | | - |
|---|
| 442 | | -static int mxc_rtc_resume(struct device *dev) |
|---|
| 443 | | -{ |
|---|
| 444 | | - struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
|---|
| 445 | | - |
|---|
| 446 | | - if (device_may_wakeup(dev)) |
|---|
| 447 | | - disable_irq_wake(pdata->irq); |
|---|
| 448 | | - |
|---|
| 449 | | - return 0; |
|---|
| 450 | | -} |
|---|
| 451 | | -#endif |
|---|
| 452 | | - |
|---|
| 453 | | -static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume); |
|---|
| 454 | 435 | |
|---|
| 455 | 436 | static struct platform_driver mxc_rtc_driver = { |
|---|
| 456 | 437 | .driver = { |
|---|
| 457 | 438 | .name = "mxc_rtc", |
|---|
| 458 | 439 | .of_match_table = of_match_ptr(imx_rtc_dt_ids), |
|---|
| 459 | | - .pm = &mxc_rtc_pm_ops, |
|---|
| 460 | 440 | }, |
|---|
| 461 | 441 | .id_table = imx_rtc_devtype, |
|---|
| 462 | 442 | .probe = mxc_rtc_probe, |
|---|
| 463 | | - .remove = mxc_rtc_remove, |
|---|
| 464 | 443 | }; |
|---|
| 465 | 444 | |
|---|
| 466 | 445 | module_platform_driver(mxc_rtc_driver) |
|---|