Re: [PATCH V2] drivers: rtc: Add support for Qualcomm PMIC8xxx RTC

From: Wan ZongShun
Date: Mon May 09 2011 - 21:44:57 EST


2011/5/9 Anirudh Ghayal <aghayal@xxxxxxxxxxxxxx>:
> Hi Peter, Wan,
>
> I have updated the patch with your suggested changes.
> Could you please re-review this patch.

Acked-by: Wan ZongShun <mcuos.com@xxxxxxxxx>

thanks!

>
> Thank you,
> ~Anirudh
>
> On 5/5/2011 11:18 AM, Anirudh Ghayal wrote:
>>
>> This patch adds support for PMIC8xxx based RTC.
>> PMIC8xxx is Qualcomm's power management IC that
>> internally houses an RTC module. This driver
>> communicates with the PMIC module over SSBI bus.
>>
>> Cc: Wan ZongShun<mcuos.com@xxxxxxxxx>
>> Cc: Andrew Morton<akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: Lars-Peter Clausen<lars@xxxxxxxxxx>
>> Signed-off-by: Anirudh Ghayal<aghayal@xxxxxxxxxxxxxx>
>> ---
>> Âdrivers/rtc/Kconfig      Â|  10 +
>> Âdrivers/rtc/Makefile      |  Â1 +
>> Âdrivers/rtc/rtc-pm8xxx.c    | Â568
>> ++++++++++++++++++++++++++++++++++++++++
>> Âinclude/linux/mfd/pm8xxx/rtc.h | Â 25 ++
>> Â4 files changed, 604 insertions(+), 0 deletions(-)
>> Âcreate mode 100644 drivers/rtc/rtc-pm8xxx.c
>> Âcreate mode 100644 include/linux/mfd/pm8xxx/rtc.h
>>
>> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
>> index e187887..39b7032 100644
>> --- a/drivers/rtc/Kconfig
>> +++ b/drivers/rtc/Kconfig
>> @@ -985,6 +985,16 @@ config RTC_DRV_LPC32XX
>> Â Â Â Â ÂThis driver can also be buillt as a module. If so, the module
>> Â Â Â Â Âwill be called rtc-lpc32xx.
>>
>> +config RTC_DRV_PM8XXX
>> + Â Â Â tristate "Qualcomm PMIC8XXX RTC"
>> + Â Â Â depends on MFD_PM8XXX
>> + Â Â Â help
>> + Â Â Â Â If you say yes here you get support for the
>> + Â Â Â Â Qualcomm PMIC8XXX RTC.
>> +
>> + Â Â Â Â To compile this driver as a module, choose M here: the
>> + Â Â Â Â module will be called rtc-pm8xxx.
>> +
>> Âconfig RTC_DRV_TEGRA
>> Â Â Â Âtristate "NVIDIA Tegra Internal RTC driver"
>> Â Â Â Âdepends on RTC_CLASS&& ÂARCH_TEGRA
>> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
>> index ca91c3c..40a6e66 100644
>> --- a/drivers/rtc/Makefile
>> +++ b/drivers/rtc/Makefile
>> @@ -74,6 +74,7 @@ obj-$(CONFIG_RTC_DRV_PCF2123) += rtc-pcf2123.o
>> Âobj-$(CONFIG_RTC_DRV_PCF50633) Â Â Â Â+= rtc-pcf50633.o
>> Âobj-$(CONFIG_RTC_DRV_PL030) Â += rtc-pl030.o
>> Âobj-$(CONFIG_RTC_DRV_PL031) Â += rtc-pl031.o
>> +obj-$(CONFIG_RTC_DRV_PM8XXX) Â Â+= rtc-pm8xxx.o
>> Âobj-$(CONFIG_RTC_DRV_PS3) Â Â += rtc-ps3.o
>> Âobj-$(CONFIG_RTC_DRV_PXA) Â Â += rtc-pxa.o
>> Âobj-$(CONFIG_RTC_DRV_R9701) Â += rtc-r9701.o
>> diff --git a/drivers/rtc/rtc-pm8xxx.c b/drivers/rtc/rtc-pm8xxx.c
>> new file mode 100644
>> index 0000000..95e3911
>> --- /dev/null
>> +++ b/drivers/rtc/rtc-pm8xxx.c
>> @@ -0,0 +1,568 @@
>> +/* 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/pm.h>
>> +#include<linux/slab.h>
>> +#include<linux/spinlock.h>
>> +
>> +#include<linux/mfd/pm8xxx/core.h>
>> +#include<linux/mfd/pm8xxx/rtc.h>
>> +
>> +
>> +/* RTC Register offsets from RTC CTRL REG */
>> +#define PM8XXX_ALARM_CTRL_OFFSET 0x01
>> +#define PM8XXX_RTC_WRITE_OFFSET 0x02
>> +#define PM8XXX_RTC_READ_OFFSET 0x06
>> +#define PM8XXX_ALARM_RW_OFFSET 0x0A
>> +
>> +/* RTC_CTRL register bit fields */
>> +#define PM8xxx_RTC_ENABLE Â Â ÂBIT(7)
>> +#define PM8xxx_RTC_ALARM_ENABLE Â Â Â ÂBIT(1)
>> +#define PM8xxx_RTC_ALARM_CLEAR ÂBIT(0)
>> +
>> +#define NUM_8_BIT_RTC_REGS Â Â 0x4
>> +
>> +/**
>> + * struct pm8xxx_rtc - rtc driver internal structure
>> + * @rtc: rtc device for this driver
>> + * @rtc_alarm_irq: rtc alarm irq number
>> + */
>> +struct pm8xxx_rtc {
>> + Â Â Â struct rtc_device *rtc;
>> + Â Â Â int rtc_alarm_irq;
>> + Â Â Â int rtc_base;
>> + Â Â Â int rtc_read_base;
>> + Â Â Â int rtc_write_base;
>> + Â Â Â int alarm_rw_base;
>> + Â Â Â u8 Âctrl_reg;
>> + Â Â Â struct device *rtc_dev;
>> + Â Â Â spinlock_t ctrl_reg_lock;
>> +};
>> +
>> +/*
>> + * The RTC registers need to be read/written one byte at a time. This is
>> a
>> + * hardware limitation.
>> + */
>> +
>> +static int pm8xxx_read_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val,
>> + Â Â Â Â Â Â Â Â Â Â Â int base, int count)
>> +{
>> + Â Â Â int i, rc;
>> + Â Â Â struct device *parent = rtc_dd->rtc_dev->parent;
>> +
>> + Â Â Â for (i = 0; i< Âcount; i++) {
>> + Â Â Â Â Â Â Â rc = pm8xxx_readb(parent, base + i,&rtc_val[i]);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(rtc_dd->rtc_dev, "PM8xxx read failed\n");
>> + Â Â Â Â Â Â Â Â Â Â Â return rc;
>> + Â Â Â Â Â Â Â }
>> + Â Â Â }
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static int pm8xxx_write_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val,
>> + Â Â Â Â Â Â Â Â Â Â Â int base, int count)
>> +{
>> + Â Â Â int i, rc;
>> + Â Â Â struct device *parent = rtc_dd->rtc_dev->parent;
>> +
>> + Â Â Â for (i = 0; i< Âcount; i++) {
>> + Â Â Â Â Â Â Â rc = pm8xxx_writeb(parent, base + i, rtc_val[i]);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(rtc_dd->rtc_dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â Â Â Â Â 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 in step 1.
>> + */
>> +static int
>> +pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â unsigned long secs, irq_flags;
>> + Â Â Â u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> +
>> + Â Â Â 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;
>> +
>> + Â Â Â dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
>> +
>> + Â Â Â spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
>> + Â Â Â ctrl_reg = rtc_dd->ctrl_reg;
>> +
>> + Â Â Â if (ctrl_reg& ÂPM8xxx_RTC_ALARM_ENABLE) {
>> + Â Â Â Â Â Â Â alarm_enabled = 1;
>> + Â Â Â Â Â Â Â ctrl_reg&= ~PM8xxx_RTC_ALARM_ENABLE;
>> + Â Â Â Â Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg,
>> rtc_dd->rtc_base,
>> +
>> 1);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â Â Â Â Â }
>> + Â Â Â } else
>> + Â Â Â Â Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> +
>> + Â Â Â /* Write Byte[1], Byte[2], Byte[3], Byte[0] */
>> + Â Â Â /* Write 0 to Byte[0] */
>> + Â Â Â reg = 0;
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&reg, rtc_dd->rtc_write_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â /* Write Byte[1], Byte[2], Byte[3] */
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd, value + 1,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rtc_dd->rtc_write_base + 1, 3);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Write to RTC registers failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â /* Write Byte[0] */
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->rtc_write_base,
>> 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Write to RTC register failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â if (alarm_enabled) {
>> + Â Â Â Â Â Â Â ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE;
>> + Â Â Â Â Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg,
>> rtc_dd->rtc_base,
>> +
>> 1);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â Â Â Â Â }
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->ctrl_reg = ctrl_reg;
>> +
>> +rtc_rw_fail:
>> + Â Â Â if (alarm_enabled)
>> + Â Â Â Â Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> +
>> + Â Â Â return rc;
>> +}
>> +
>> +static int
>> +pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â u8 value[4], reg;
>> + Â Â Â unsigned long secs;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> +
>> + Â Â Â rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->rtc_read_base,
>> +
>> NUM_8_BIT_RTC_REGS);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "RTC time read failed\n");
>> + Â Â Â Â Â Â Â return rc;
>> + Â Â Â }
>> +
>> + Â Â Â /*
>> + Â Â Â Â* Read the LSB again and check if there has been a carry over.
>> + Â Â Â Â* If there is, redo the read operation.
>> + Â Â Â Â*/
>> + Â Â Â rc = pm8xxx_read_wrapper(rtc_dd,&reg, rtc_dd->rtc_read_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "PM8xxx read failed\n");
>> + Â Â Â Â Â Â Â return rc;
>> + Â Â Â }
>> +
>> + Â Â Â if (unlikely(reg< Âvalue[0])) {
>> + Â Â Â Â Â Â Â rc = pm8xxx_read_wrapper(rtc_dd, value,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rtc_dd->rtc_read_base,
>> NUM_8_BIT_RTC_REGS);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(dev, "RTC time read failed\n");
>> + Â Â Â Â Â Â Â Â Â Â Â 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) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Invalid time read from PM8xxx\n");
>> + Â Â Â Â Â Â Â return rc;
>> + Â Â Â }
>> +
>> + Â Â Â dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
>> + Â Â Â Â Â Â Â Â Â Â Â secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
>> + Â Â Â Â Â Â Â Â Â Â Â tm->tm_mday, tm->tm_mon, tm->tm_year);
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static int
>> +pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â u8 value[4], ctrl_reg;
>> + Â Â Â unsigned long secs, secs_rtc, irq_flags;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> + Â Â Â struct rtc_time rtc_tm;
>> +
>> + Â Â Â rtc_tm_to_time(&alarm->time,&secs);
>> +
>> + Â Â Â /*
>> + Â Â Â Â* Read the current RTC time and verify if the alarm time is in
>> the
>> + Â Â Â Â* past. If yes, return invalid.
>> + Â Â Â Â*/
>> + Â Â Â rc = pm8xxx_rtc_read_time(dev,&rtc_tm);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Unamble to read RTC time\n");
>> + Â Â Â Â Â Â Â return -EINVAL;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_tm_to_time(&rtc_tm,&secs_rtc);
>> + Â Â Â if (secs< Âsecs_rtc) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Trying to set alarm in the past\n");
>> + Â Â Â Â Â Â Â return -EINVAL;
>> + Â Â Â }
>> +
>> + Â Â Â value[0] = secs& Â0xFF;
>> + Â Â Â value[1] = (secs>> Â8)& Â0xFF;
>> + Â Â Â value[2] = (secs>> Â16)& Â0xFF;
>> + Â Â Â value[3] = (secs>> Â24)& Â0xFF;
>> +
>> + Â Â Â spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
>> +
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base,
>> +
>> NUM_8_BIT_RTC_REGS);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Write to RTC ALARM registers failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â ctrl_reg = rtc_dd->ctrl_reg;
>> + Â Â Â ctrl_reg = (alarm->enabled) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE)
>> :
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (ctrl_reg&
>> Â~PM8xxx_RTC_ALARM_ENABLE);
>> +
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->ctrl_reg = ctrl_reg;
>> +
>> + Â Â Â dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
>> + Â Â Â Â Â Â Â Â Â Â Â alarm->time.tm_hour, alarm->time.tm_min,
>> + Â Â Â Â Â Â Â Â Â Â Â alarm->time.tm_sec, alarm->time.tm_mday,
>> + Â Â Â Â Â Â Â Â Â Â Â alarm->time.tm_mon, alarm->time.tm_year);
>> +rtc_rw_fail:
>> + Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> + Â Â Â return rc;
>> +}
>> +
>> +static int
>> +pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â u8 value[4];
>> + Â Â Â unsigned long secs;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> +
>> + Â Â Â rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base,
>> + Â Â Â Â Â Â Â Â Â Â Â NUM_8_BIT_RTC_REGS);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "RTC alarm time read failed\n");
>> + Â Â Â Â Â Â Â 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) {
>> + Â Â Â Â Â Â Â dev_err(dev, "Invalid time read from PM8xxx\n");
>> + Â Â Â Â Â Â Â return rc;
>> + Â Â Â }
>> +
>> + Â Â Â dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
>> + Â Â Â Â Â Â Â 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
>> +pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â unsigned long irq_flags;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> + Â Â Â u8 ctrl_reg;
>> +
>> + Â Â Â spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
>> + Â Â Â ctrl_reg = rtc_dd->ctrl_reg;
>> + Â Â Â ctrl_reg = (enable) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE) :
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (ctrl_reg& Â~PM8xxx_RTC_ALARM_ENABLE);
>> +
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(dev, "PM8xxx write failed\n");
>> + Â Â Â Â Â Â Â goto rtc_rw_fail;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->ctrl_reg = ctrl_reg;
>> +
>> +rtc_rw_fail:
>> + Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> + Â Â Â return rc;
>> +}
>> +
>> +static struct rtc_class_ops pm8xxx_rtc_ops = {
>> +    .read_time   Â= pm8xxx_rtc_read_time,
>> +    .set_alarm   Â= pm8xxx_rtc_set_alarm,
>> +    .read_alarm   = pm8xxx_rtc_read_alarm,
>> + Â Â Â .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
>> +};
>> +
>> +static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
>> +{
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_id;
>> + Â Â Â u8 ctrl_reg;
>> + Â Â Â int rc;
>> + Â Â Â unsigned long irq_flags;
>> +
>> + Â Â Â rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
>> +
>> + Â Â Â spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
>> +
>> + Â Â Â /* Clear the alarm enable bit */
>> + Â Â Â ctrl_reg = rtc_dd->ctrl_reg;
>> + Â Â Â ctrl_reg&= ~PM8xxx_RTC_ALARM_ENABLE;
>> +
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> + Â Â Â Â Â Â Â dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
>> + Â Â Â Â Â Â Â goto rtc_alarm_handled;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->ctrl_reg = ctrl_reg;
>> + Â Â Â spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
>> +
>> + Â Â Â /* Clear RTC alarm register */
>> + Â Â Â rc = pm8xxx_read_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base +
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â PM8XXX_ALARM_CTRL_OFFSET,
>> 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
>> + Â Â Â Â Â Â Â goto rtc_alarm_handled;
>> + Â Â Â }
>> +
>> + Â Â Â ctrl_reg&= ~PM8xxx_RTC_ALARM_CLEAR;
>> + Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base +
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â PM8XXX_ALARM_CTRL_OFFSET,
>> 1);
>> + Â Â Â if (rc< Â0)
>> + Â Â Â Â Â Â Â dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
>> +
>> +rtc_alarm_handled:
>> + Â Â Â return IRQ_HANDLED;
>> +}
>> +
>> +static int __devinit pm8xxx_rtc_probe(struct platform_device *pdev)
>> +{
>> + Â Â Â int rc;
>> + Â Â Â u8 ctrl_reg;
>> + Â Â Â bool rtc_write_enable = false;
>> + Â Â Â struct pm8xxx_rtc *rtc_dd;
>> + Â Â Â struct resource *rtc_resource;
>> + Â Â Â const struct pm8xxx_rtc_platform_data *pdata = mfd_get_data(pdev);
>> +
>> + Â Â Â if (pdata != NULL)
>> + Â Â Â Â Â Â Â rtc_write_enable = pdata->rtc_write_enable;
>> +
>> + Â Â Â rtc_dd = kzalloc(sizeof(*rtc_dd), GFP_KERNEL);
>> + Â Â Â if (rtc_dd == NULL) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Unable to allocate memory!\n");
>> + Â Â Â Â Â Â Â return -ENOMEM;
>> + Â Â Â }
>> +
>> + Â Â Â /* Initialise spinlock to protect RTC cntrol register */
>> + Â Â Â spin_lock_init(&rtc_dd->ctrl_reg_lock);
>> +
>> + Â Â Â rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
>> + Â Â Â if (rtc_dd->rtc_alarm_irq< Â0) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Alarm IRQ resource absent!\n");
>> + Â Â Â Â Â Â Â rc = -ENXIO;
>> + Â Â Â Â Â Â Â goto fail_rtc_enable;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_resource = platform_get_resource_byname(pdev, IORESOURCE_IO,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "pmic_rtc_base");
>> + Â Â Â if (!(rtc_resource&& Ârtc_resource->start)) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "RTC IO resource absent!\n");
>> + Â Â Â Â Â Â Â rc = -ENXIO;
>> + Â Â Â Â Â Â Â goto fail_rtc_enable;
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->rtc_base = rtc_resource->start;
>> +
>> + Â Â Â /* Setup RTC register addresses */
>> + Â Â Â rtc_dd->rtc_write_base = rtc_dd->rtc_base +
>> PM8XXX_RTC_WRITE_OFFSET;
>> + Â Â Â rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET;
>> + Â Â Â rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET;
>> +
>> + Â Â Â rtc_dd->rtc_dev =&(pdev->dev);
>> +
>> + Â Â Â /* Check if the RTC is on, else turn it on */
>> + Â Â Â rc = pm8xxx_read_wrapper(rtc_dd,&ctrl_reg, rtc_dd->rtc_base, 1);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "PM8xxx read failed!\n");
>> + Â Â Â Â Â Â Â goto fail_rtc_enable;
>> + Â Â Â }
>> +
>> + Â Â Â if (!(ctrl_reg& ÂPM8xxx_RTC_ENABLE)) {
>> + Â Â Â Â Â Â Â ctrl_reg |= PM8xxx_RTC_ENABLE;
>> + Â Â Â Â Â Â Â rc = pm8xxx_write_wrapper(rtc_dd,&ctrl_reg,
>> rtc_dd->rtc_base,
>> +
>> 1);
>> + Â Â Â Â Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&pdev->dev, "PM8xxx write failed!\n");
>> + Â Â Â Â Â Â Â Â Â Â Â goto fail_rtc_enable;
>> + Â Â Â Â Â Â Â }
>> + Â Â Â }
>> +
>> + Â Â Â rtc_dd->ctrl_reg = ctrl_reg;
>> + Â Â Â if (rtc_write_enable == true)
>> + Â Â Â Â Â Â Â pm8xxx_rtc_ops.set_time = pm8xxx_rtc_set_time;
>> +
>> + Â Â Â platform_set_drvdata(pdev, rtc_dd);
>> +
>> + Â Â Â /* Register the RTC device */
>> + Â Â Â rtc_dd->rtc = rtc_device_register("pm8xxx_rtc",&pdev->dev,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &pm8xxx_rtc_ops, THIS_MODULE);
>> + Â Â Â if (IS_ERR(rtc_dd->rtc)) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __func__, PTR_ERR(rtc_dd->rtc));
>> + Â Â Â Â Â Â Â rc = PTR_ERR(rtc_dd->rtc);
>> + Â Â Â Â Â Â Â goto fail_rtc_enable;
>> + Â Â Â }
>> +
>> + Â Â Â /* Request the alarm IRQ */
>> + Â Â Â rc = request_any_context_irq(rtc_dd->rtc_alarm_irq,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âpm8xxx_alarm_trigger,
>> IRQF_TRIGGER_RISING,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"pm8xxx_rtc_alarm", rtc_dd);
>> + Â Â Â if (rc< Â0) {
>> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
>> + Â Â Â Â Â Â Â goto fail_req_irq;
>> + Â Â Â }
>> +
>> + Â Â Â device_init_wakeup(&pdev->dev, 1);
>> +
>> + Â Â Â dev_dbg(&pdev->dev, "Probe success !!\n");
>> +
>> + Â Â Â return 0;
>> +
>> +fail_req_irq:
>> + Â Â Â rtc_device_unregister(rtc_dd->rtc);
>> +fail_rtc_enable:
>> + Â Â Â platform_set_drvdata(pdev, NULL);
>> + Â Â Â kfree(rtc_dd);
>> + Â Â Â return rc;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int pm8xxx_rtc_resume(struct device *dev)
>> +{
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> +
>> + Â Â Â if (device_may_wakeup(dev))
>> + Â Â Â Â Â Â Â disable_irq_wake(rtc_dd->rtc_alarm_irq);
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static int pm8xxx_rtc_suspend(struct device *dev)
>> +{
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
>> +
>> + Â Â Â if (device_may_wakeup(dev))
>> + Â Â Â Â Â Â Â enable_irq_wake(rtc_dd->rtc_alarm_irq);
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static const struct dev_pm_ops pm8xxx_rtc_pm_ops = {
>> + Â Â Â .suspend = pm8xxx_rtc_suspend,
>> + Â Â Â .resume = pm8xxx_rtc_resume,
>> +};
>> +#endif
>> +static int __devexit pm8xxx_rtc_remove(struct platform_device *pdev)
>> +{
>> + Â Â Â struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev);
>> +
>> + Â Â Â device_init_wakeup(&pdev->dev, 0);
>> + Â Â Â free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
>> + Â Â Â rtc_device_unregister(rtc_dd->rtc);
>> + Â Â Â platform_set_drvdata(pdev, NULL);
>> + Â Â Â kfree(rtc_dd);
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static struct platform_driver pm8xxx_rtc_driver = {
>> +    .probe     Â= pm8xxx_rtc_probe,
>> +    .remove     = __devexit_p(pm8xxx_rtc_remove),
>> + Â Â Â .driver = {
>> +        .name  = PM8XXX_RTC_DEV_NAME,
>> + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
>> +#ifdef CONFIG_PM
>> +        .pm   =&pm8xxx_rtc_pm_ops,
>> +#endif
>> + Â Â Â },
>> +};
>> +
>> +static int __init pm8xxx_rtc_init(void)
>> +{
>> + Â Â Â return platform_driver_register(&pm8xxx_rtc_driver);
>> +}
>> +module_init(pm8xxx_rtc_init);
>> +
>> +static void __exit pm8xxx_rtc_exit(void)
>> +{
>> + Â Â Â platform_driver_unregister(&pm8xxx_rtc_driver);
>> +}
>> +module_exit(pm8xxx_rtc_exit);
>> +
>> +MODULE_ALIAS("platform:rtc-pm8xxx");
>> +MODULE_DESCRIPTION("PMIC8xxx RTC driver");
>> +MODULE_LICENSE("GPL v2");
>> +MODULE_AUTHOR("Anirudh Ghayal<aghayal@xxxxxxxxxxxxxx>");
>> diff --git a/include/linux/mfd/pm8xxx/rtc.h
>> b/include/linux/mfd/pm8xxx/rtc.h
>> new file mode 100644
>> index 0000000..14f1983
>> --- /dev/null
>> +++ b/include/linux/mfd/pm8xxx/rtc.h
>> @@ -0,0 +1,25 @@
>> +/* 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.
>> + */
>> +
>> +#ifndef __RTC_PM8XXX_H__
>> +#define __RTC_PM8XXX_H__
>> +
>> +#define PM8XXX_RTC_DEV_NAME Â Â "rtc-pm8xxx"
>> +/**
>> + * struct pm8xxx_rtc_pdata - RTC driver platform data
>> + * @rtc_write_enable: variable stating RTC write capability
>> + */
>> +struct pm8xxx_rtc_platform_data {
>> + Â Â Â bool rtc_write_enable;
>> +};
>> +
>> +#endif /* __RTC_PM8XXX_H__ */
>
>



--
Wan ZongShun.
www.mcuos.com
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/