| /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 and |
| * only version 2 as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| |
| #include <linux/module.h> |
| #include <linux/init.h> |
| #include <linux/rtc.h> |
| #include <linux/mfd/pmic8058.h> |
| #include <linux/pm.h> |
| #include <linux/slab.h> |
| #include <linux/rtc/rtc-pm8058.h> |
| #include <linux/pm_runtime.h> |
| |
| #define PM8058_RTC_CTRL 0x1E8 |
| #define PM8058_RTC_ENABLE BIT(7) |
| #define PM8058_RTC_ALARM_ENABLE BIT(1) |
| #define PM8058_RTC_ABORT_ENABLE BIT(0) |
| #define PM8058_RTC_ALARM_CTRL 0x1E9 |
| #define PM8058_RTC_ALARM_CLEAR BIT(0) |
| #define PM8058_RTC_TEST 0x1F6 |
| #define PM8058_RTC_READ_BASE 0x1EE |
| #define PM8058_RTC_WRITE_BASE 0x1EA |
| #define PM8058_RTC_ALARM_BASE 0x1F2 |
| |
| struct pm8058_rtc { |
| struct rtc_device *rtc0; |
| u8 rtc_ctrl_reg; |
| int rtc_irq; |
| int rtc_alarm_irq; |
| struct pm8058_chip *pm_chip; |
| }; |
| |
| static int |
| pm8058_rtc_read_bytes(struct pm8058_rtc *rtc_dd, u8 *rtc_val, int base) |
| { |
| int i, rc; |
| |
| /* |
| * Read the 32-bit RTC/Alarm Value. |
| * These values have to be read 8-bit at a time. |
| */ |
| for (i = 0; i < 4; i++) { |
| rc = pm8058_read(rtc_dd->pm_chip, base + i, &rtc_val[i], 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| return 0; |
| } |
| |
| static int |
| pm8058_rtc_write_bytes(struct pm8058_rtc *rtc_dd, u8 *rtc_val, int base) |
| { |
| int i, rc; |
| |
| /* |
| * Write the 32-bit Value. |
| * These values have to be written 8-bit at a time. |
| */ |
| for (i = 0; i < 4; i++) { |
| rc = pm8058_write(rtc_dd->pm_chip, base + i, &rtc_val[i], 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| return 0; |
| } |
| |
| /* |
| * Steps to write the RTC registers. |
| * 1. Disable alarm if enabled. |
| * 2. Write 0x00 to LSB. |
| * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0]. |
| * 4. Enable alarm if disabled earlier. |
| */ |
| #ifdef CONFIG_RTC_PM8058_WRITE_ENABLE |
| static int |
| pm8058_rtc0_set_time(struct device *dev, struct rtc_time *tm) |
| { |
| int rc; |
| unsigned long secs = 0; |
| u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg = 0, i; |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| ctrl_reg = rtc_dd->rtc_ctrl_reg; |
| |
| rtc_tm_to_time(tm, &secs); |
| |
| value[0] = secs & 0xFF; |
| value[1] = (secs >> 8) & 0xFF; |
| value[2] = (secs >> 16) & 0xFF; |
| value[3] = (secs >> 24) & 0xFF; |
| |
| pr_debug("%s: Seconds value to be written to RTC = %lu\n", __func__, |
| secs); |
| /* Disable alarm before updating RTC */ |
| if (ctrl_reg & PM8058_RTC_ALARM_ENABLE) { |
| alarm_enabled = 1; |
| ctrl_reg &= ~PM8058_RTC_ALARM_ENABLE; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, |
| &ctrl_reg, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| /* Write Byte[1], Byte[2], Byte[3], Byte[0] */ |
| reg = 0; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| |
| for (i = 1; i < 4; i++) { |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE + i, |
| &value[i], 1); |
| if (rc < 0) { |
| pr_err("%s:Write to RTC registers failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE, |
| &value[0], 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| |
| if (alarm_enabled) { |
| ctrl_reg |= PM8058_RTC_ALARM_ENABLE; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, |
| &ctrl_reg, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| rtc_dd->rtc_ctrl_reg = ctrl_reg; |
| |
| return 0; |
| } |
| #endif |
| |
| static int |
| pm8058_rtc0_read_time(struct device *dev, struct rtc_time *tm) |
| { |
| int rc; |
| u8 value[4], reg; |
| unsigned long secs = 0; |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| rc = pm8058_rtc_read_bytes(rtc_dd, value, PM8058_RTC_READ_BASE); |
| if (rc < 0) { |
| pr_err("%s: RTC time read failed\n", __func__); |
| return rc; |
| } |
| |
| /* |
| * Read the LSB again and check if there has been a carry over. |
| * If there is, redo the read operation. |
| */ |
| rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_READ_BASE, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| return rc; |
| } |
| |
| if (unlikely(reg < value[0])) { |
| rc = pm8058_rtc_read_bytes(rtc_dd, value, |
| PM8058_RTC_READ_BASE); |
| if (rc < 0) { |
| pr_err("%s: RTC time read failed\n", __func__); |
| return rc; |
| } |
| } |
| |
| secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24); |
| |
| rtc_time_to_tm(secs, tm); |
| |
| rc = rtc_valid_tm(tm); |
| if (rc < 0) { |
| pr_err("%s: Invalid time read from PMIC8058\n", __func__); |
| return rc; |
| } |
| |
| pr_debug("%s: secs = %lu, h::m:s == %d::%d::%d, d/m/y = %d/%d/%d\n", |
| __func__, secs, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| tm->tm_mday, tm->tm_mon, tm->tm_year); |
| |
| return 0; |
| } |
| |
| static int |
| pm8058_rtc0_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| { |
| int rc; |
| u8 value[4], reg; |
| struct rtc_time rtc_tm; |
| unsigned long secs_alarm, secs_rtc; |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| reg = rtc_dd->rtc_ctrl_reg; |
| |
| /* Check if the alarm is valid */ |
| rc = rtc_valid_tm(&alarm->time); |
| if (rc < 0) { |
| pr_err("%s: Alarm time invalid\n", __func__); |
| return -EINVAL; |
| } |
| |
| rtc_tm_to_time(&alarm->time, &secs_alarm); |
| |
| /* |
| * Read the current RTC time and verify if the alarm time is in the |
| * past. If yes, return invalid. |
| */ |
| rc = pm8058_rtc0_read_time(dev, &rtc_tm); |
| if (rc) { |
| pr_err("%s: Unable to read RTC time\n", __func__); |
| return -EINVAL; |
| } |
| rtc_tm_to_time(&rtc_tm, &secs_rtc); |
| |
| if (secs_alarm < secs_rtc) { |
| pr_err("%s: Trying to set alarm in the past\n", __func__); |
| return -EINVAL; |
| } |
| |
| value[0] = secs_alarm & 0xFF; |
| value[1] = (secs_alarm >> 8) & 0xFF; |
| value[2] = (secs_alarm >> 16) & 0xFF; |
| value[3] = (secs_alarm >> 24) & 0xFF; |
| |
| rc = pm8058_rtc_write_bytes(rtc_dd, value, PM8058_RTC_ALARM_BASE); |
| if (rc < 0) { |
| pr_err("%s: Alarm could not be set\n", __func__); |
| return rc; |
| } |
| |
| reg = (alarm->enabled) ? (reg | PM8058_RTC_ALARM_ENABLE) : |
| (reg & ~PM8058_RTC_ALARM_ENABLE); |
| |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| |
| rtc_dd->rtc_ctrl_reg = reg; |
| |
| pr_debug("%s: Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| __func__, alarm->time.tm_hour, alarm->time.tm_min, |
| alarm->time.tm_sec, alarm->time.tm_mday, |
| alarm->time.tm_mon, alarm->time.tm_year); |
| |
| return 0; |
| } |
| |
| static int |
| pm8058_rtc0_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| { |
| int rc; |
| u8 value[4], reg; |
| unsigned long secs = 0; |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| reg = rtc_dd->rtc_ctrl_reg; |
| |
| alarm->enabled = !!(reg & PM8058_RTC_ALARM_ENABLE); |
| |
| rc = pm8058_rtc_read_bytes(rtc_dd, value, |
| PM8058_RTC_ALARM_BASE); |
| if (rc < 0) { |
| pr_err("%s: RTC alarm time read failed\n", __func__); |
| return rc; |
| } |
| |
| secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24); |
| |
| rtc_time_to_tm(secs, &alarm->time); |
| |
| rc = rtc_valid_tm(&alarm->time); |
| if (rc < 0) { |
| pr_err("%s: Invalid time read from PMIC8058\n", __func__); |
| return rc; |
| } |
| |
| pr_debug("%s: Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| __func__, alarm->time.tm_hour, alarm->time.tm_min, |
| alarm->time.tm_sec, alarm->time.tm_mday, |
| alarm->time.tm_mon, alarm->time.tm_year); |
| |
| return 0; |
| } |
| |
| |
| static int |
| pm8058_rtc0_alarm_irq_enable(struct device *dev, unsigned int enable) |
| { |
| int rc; |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| u8 reg; |
| |
| reg = rtc_dd->rtc_ctrl_reg; |
| reg = (enable) ? (reg | PM8058_RTC_ALARM_ENABLE) : |
| (reg & ~PM8058_RTC_ALARM_ENABLE); |
| |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return rc; |
| } |
| |
| rtc_dd->rtc_ctrl_reg = reg; |
| |
| return rc; |
| } |
| |
| static struct rtc_class_ops pm8058_rtc0_ops = { |
| .read_time = pm8058_rtc0_read_time, |
| .set_alarm = pm8058_rtc0_set_alarm, |
| .read_alarm = pm8058_rtc0_read_alarm, |
| .alarm_irq_enable = pm8058_rtc0_alarm_irq_enable, |
| }; |
| |
| static irqreturn_t pm8058_alarm_trigger(int irq, void *dev_id) |
| { |
| u8 reg; |
| int rc; |
| unsigned long events = 0; |
| struct pm8058_rtc *rtc_dd = dev_id; |
| |
| events = RTC_IRQF | RTC_AF; |
| rtc_update_irq(rtc_dd->rtc0, 1, events); |
| |
| pr_debug("%s: Alarm Triggered !!\n", __func__); |
| |
| /* Clear the alarm enable bit */ |
| reg = rtc_dd->rtc_ctrl_reg; |
| |
| reg &= ~PM8058_RTC_ALARM_ENABLE; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, |
| ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| goto rtc_alarm_handled; |
| } |
| |
| rtc_dd->rtc_ctrl_reg = reg; |
| |
| /* Clear RTC alarm register */ |
| rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| goto rtc_alarm_handled; |
| } |
| |
| reg &= ~PM8058_RTC_ALARM_CLEAR; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| goto rtc_alarm_handled; |
| } |
| |
| rtc_alarm_handled: |
| return IRQ_HANDLED; |
| } |
| |
| static int __devinit pm8058_rtc_probe(struct platform_device *pdev) |
| { |
| int rc; |
| u8 reg, reg_alarm; |
| struct pm8058_rtc *rtc_dd; |
| struct pm8058_chip *pm_chip; |
| |
| pm_chip = dev_get_drvdata(pdev->dev.parent); |
| if (pm_chip == NULL) { |
| pr_err("%s: Invalid driver information\n", __func__); |
| return -ENXIO; |
| } |
| |
| rtc_dd = kzalloc(sizeof(*rtc_dd), GFP_KERNEL); |
| if (rtc_dd == NULL) { |
| pr_err("%s: Unable to allocate memory\n", __func__); |
| return -ENOMEM; |
| } |
| |
| /* Enable runtime PM ops, start in ACTIVE mode */ |
| rc = pm_runtime_set_active(&pdev->dev); |
| if (rc < 0) |
| dev_dbg(&pdev->dev, "unable to set runtime pm state\n"); |
| pm_runtime_enable(&pdev->dev); |
| |
| rtc_dd->rtc_irq = platform_get_irq(pdev, 0); |
| rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 1); |
| if (!rtc_dd->rtc_alarm_irq || !rtc_dd->rtc_irq) { |
| pr_err("%s: RTC Alarm IRQ absent\n", __func__); |
| rc = -ENXIO; |
| goto fail_rtc_enable; |
| } |
| |
| rtc_dd->pm_chip = pm_chip; |
| |
| rc = pm8058_read(pm_chip, PM8058_RTC_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| goto fail_rtc_enable; |
| } |
| |
| /* Enable RTC, ABORT enable and disable alarm */ |
| reg |= ((PM8058_RTC_ENABLE | PM8058_RTC_ABORT_ENABLE) & |
| ~PM8058_RTC_ALARM_ENABLE); |
| |
| rc = pm8058_write(pm_chip, PM8058_RTC_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| goto fail_rtc_enable; |
| } |
| |
| /* Clear RTC alarm control register */ |
| rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, |
| ®_alarm, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 read failed\n", __func__); |
| goto fail_rtc_enable; |
| } |
| |
| reg_alarm &= ~PM8058_RTC_ALARM_CLEAR; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, |
| ®_alarm, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| goto fail_rtc_enable; |
| } |
| |
| rtc_dd->rtc_ctrl_reg = reg; |
| |
| #ifdef CONFIG_RTC_PM8058_WRITE_ENABLE |
| pm8058_rtc0_ops.set_time = pm8058_rtc0_set_time; |
| #endif |
| |
| platform_set_drvdata(pdev, rtc_dd); |
| |
| /* Register the RTC device */ |
| rtc_dd->rtc0 = rtc_device_register("pm8058_rtc0", &pdev->dev, |
| &pm8058_rtc0_ops, THIS_MODULE); |
| if (IS_ERR(rtc_dd->rtc0)) { |
| pr_err("%s: RTC device registration failed (%ld)\n", |
| __func__, PTR_ERR(rtc_dd->rtc0)); |
| rc = PTR_ERR(rtc_dd->rtc0); |
| goto fail_rtc_enable; |
| } |
| |
| /* Request the alarm IRQ */ |
| rc = request_threaded_irq(rtc_dd->rtc_alarm_irq, NULL, |
| pm8058_alarm_trigger, IRQF_TRIGGER_RISING, |
| "pm8058_rtc_alarm", rtc_dd); |
| if (rc < 0) { |
| pr_err("%s: Request IRQ failed (%d)\n", __func__, rc); |
| goto fail_req_irq; |
| } |
| |
| device_init_wakeup(&pdev->dev, 1); |
| |
| pr_debug("%s: Probe success !!\n", __func__); |
| |
| return 0; |
| |
| fail_req_irq: |
| rtc_device_unregister(rtc_dd->rtc0); |
| fail_rtc_enable: |
| pm_runtime_set_suspended(&pdev->dev); |
| pm_runtime_disable(&pdev->dev); |
| kfree(rtc_dd); |
| return rc; |
| } |
| |
| #ifdef CONFIG_PM |
| static int pm8058_rtc_resume(struct device *dev) |
| { |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| if (device_may_wakeup(dev)) |
| disable_irq_wake(rtc_dd->rtc_alarm_irq); |
| |
| return 0; |
| } |
| |
| static int pm8058_rtc_suspend(struct device *dev) |
| { |
| struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev); |
| |
| if (device_may_wakeup(dev)) |
| enable_irq_wake(rtc_dd->rtc_alarm_irq); |
| |
| return 0; |
| } |
| |
| static struct dev_pm_ops pm8058_rtc_pm_ops = { |
| .suspend = pm8058_rtc_suspend, |
| .resume = pm8058_rtc_resume, |
| }; |
| #endif |
| |
| static int __devexit pm8058_rtc_remove(struct platform_device *pdev) |
| { |
| struct pm8058_rtc *rtc_dd = platform_get_drvdata(pdev); |
| |
| pm_runtime_set_suspended(&pdev->dev); |
| pm_runtime_disable(&pdev->dev); |
| |
| device_init_wakeup(&pdev->dev, 0); |
| free_irq(rtc_dd->rtc_alarm_irq, rtc_dd); |
| rtc_device_unregister(rtc_dd->rtc0); |
| kfree(rtc_dd); |
| |
| return 0; |
| } |
| |
| static void pm8058_rtc_shutdown(struct platform_device *pdev) |
| { |
| u8 reg; |
| int rc, i; |
| bool rtc_alarm_powerup = false; |
| struct pm8058_rtc *rtc_dd = platform_get_drvdata(pdev); |
| struct pm8058_rtc_platform_data *pdata = pdev->dev.platform_data; |
| |
| if (pdata != NULL) |
| rtc_alarm_powerup = pdata->rtc_alarm_powerup; |
| |
| if (!rtc_alarm_powerup) { |
| |
| dev_dbg(&pdev->dev, "Disabling alarm interrupts\n"); |
| |
| /* Disable RTC alarms */ |
| reg = rtc_dd->rtc_ctrl_reg; |
| reg &= ~PM8058_RTC_ALARM_ENABLE; |
| rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return; |
| } |
| |
| /* Clear Alarm register */ |
| reg = 0x0; |
| for (i = 0; i < 4; i++) { |
| rc = pm8058_write(rtc_dd->pm_chip, |
| PM8058_RTC_ALARM_BASE + i, ®, 1); |
| if (rc < 0) { |
| pr_err("%s: PM8058 write failed\n", __func__); |
| return; |
| } |
| } |
| |
| } |
| } |
| |
| static struct platform_driver pm8058_rtc_driver = { |
| .probe = pm8058_rtc_probe, |
| .remove = __devexit_p(pm8058_rtc_remove), |
| .shutdown = pm8058_rtc_shutdown, |
| .driver = { |
| .name = "pm8058-rtc", |
| .owner = THIS_MODULE, |
| #ifdef CONFIG_PM |
| .pm = &pm8058_rtc_pm_ops, |
| #endif |
| }, |
| }; |
| |
| static int __init pm8058_rtc_init(void) |
| { |
| return platform_driver_register(&pm8058_rtc_driver); |
| } |
| |
| static void __exit pm8058_rtc_exit(void) |
| { |
| platform_driver_unregister(&pm8058_rtc_driver); |
| } |
| |
| module_init(pm8058_rtc_init); |
| module_exit(pm8058_rtc_exit); |
| |
| MODULE_ALIAS("platform:pm8058-rtc"); |
| MODULE_DESCRIPTION("PMIC8058 RTC driver"); |
| MODULE_LICENSE("GPL v2"); |