Merge tag 'trace-3.19-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[cascardo/linux.git] / drivers / rtc / rtc-pcf8563.c
index 78f76d9..96fb32e 100644 (file)
 
 #define PCF8563_REG_CLKO       0x0D /* clock out */
 #define PCF8563_REG_TMRC       0x0E /* timer control */
+#define PCF8563_TMRC_ENABLE    BIT(7)
+#define PCF8563_TMRC_4096      0
+#define PCF8563_TMRC_64                1
+#define PCF8563_TMRC_1         2
+#define PCF8563_TMRC_1_60      3
+#define PCF8563_TMRC_MASK      3
+
 #define PCF8563_REG_TMR                0x0F /* timer */
 
 #define PCF8563_SC_LV          0x80 /* low voltage */
@@ -361,6 +368,14 @@ static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
        struct i2c_client *client = to_i2c_client(dev);
        unsigned char buf[4];
        int err;
+       unsigned long alarm_time;
+
+       /* The alarm has no seconds, round up to nearest minute */
+       if (tm->time.tm_sec) {
+               rtc_tm_to_time(&tm->time, &alarm_time);
+               alarm_time += 60-tm->time.tm_sec;
+               rtc_time_to_tm(alarm_time, &tm->time);
+       }
 
        dev_dbg(dev, "%s, min=%d hour=%d wday=%d mday=%d "
                "enabled=%d pending=%d\n", __func__,
@@ -381,6 +396,7 @@ static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
 
 static int pcf8563_irq_enable(struct device *dev, unsigned int enabled)
 {
+       dev_dbg(dev, "%s: en=%d\n", __func__, enabled);
        return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled);
 }
 
@@ -398,6 +414,8 @@ static int pcf8563_probe(struct i2c_client *client,
 {
        struct pcf8563 *pcf8563;
        int err;
+       unsigned char buf;
+       unsigned char alm_pending;
 
        dev_dbg(&client->dev, "%s\n", __func__);
 
@@ -415,6 +433,22 @@ static int pcf8563_probe(struct i2c_client *client,
        pcf8563->client = client;
        device_set_wakeup_capable(&client->dev, 1);
 
+       /* Set timer to lowest frequency to save power (ref Haoyu datasheet) */
+       buf = PCF8563_TMRC_1_60;
+       err = pcf8563_write_block_data(client, PCF8563_REG_TMRC, 1, &buf);
+       if (err < 0) {
+               dev_err(&client->dev, "%s: write error\n", __func__);
+               return err;
+       }
+
+       err = pcf8563_get_alarm_mode(client, NULL, &alm_pending);
+       if (err < 0) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
+               return err;
+       }
+       if (alm_pending)
+               pcf8563_set_alarm_mode(client, 0);
+
        pcf8563->rtc = devm_rtc_device_register(&client->dev,
                                pcf8563_driver.driver.name,
                                &pcf8563_rtc_ops, THIS_MODULE);
@@ -435,6 +469,9 @@ static int pcf8563_probe(struct i2c_client *client,
 
        }
 
+       /* the pcf8563 alarm only supports a minute accuracy */
+       pcf8563->rtc->uie_unsupported = 1;
+
        return 0;
 }