Skip to content

Commit 29f88f3

Browse files
author
vitalogy
committed
rtc-ds1307.c: add DT property 'wakeup-source' to indicate the rtc can wakeup the device without an IRQ
1 parent 9780b0c commit 29f88f3

File tree

1 file changed

+70
-77
lines changed

1 file changed

+70
-77
lines changed

drivers/rtc/rtc-ds1307.c

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
* published by the Free Software Foundation.
1212
*/
1313

14-
#include <linux/module.h>
14+
#include <linux/bcd.h>
15+
#include <linux/i2c.h>
1516
#include <linux/init.h>
17+
#include <linux/module.h>
18+
#include <linux/rtc/ds1307.h>
19+
#include <linux/rtc.h>
1620
#include <linux/slab.h>
17-
#include <linux/i2c.h>
1821
#include <linux/string.h>
19-
#include <linux/rtc.h>
20-
#include <linux/bcd.h>
21-
#include <linux/rtc/ds1307.h>
2222

2323
/*
2424
* We can't determine type by probing, but if we expect pre-Linux code
2525
* to have set the chip up as a clock (turning on the oscillator and
2626
* setting the date and time), Linux can ignore the non-clock features.
2727
* That's a natural job for a factory or repair bench.
2828
*/
29+
2930
enum ds_type {
3031
ds_1307,
3132
ds_1337,
@@ -41,7 +42,6 @@ enum ds_type {
4142
/* rs5c372 too? different address... */
4243
};
4344

44-
4545
/* RTC registers don't differ much, except for the century flag */
4646
#define DS1307_REG_SECS 0x00 /* 00-59 */
4747
# define DS1307_BIT_CH 0x80
@@ -114,7 +114,6 @@ struct ds1307 {
114114
#define HAS_ALARM 1 /* bit 1 == irq claimed */
115115
struct i2c_client *client;
116116
struct rtc_device *rtc;
117-
struct work_struct work;
118117
s32 (*read_block_data)(const struct i2c_client *client, u8 command,
119118
u8 length, u8 *values);
120119
s32 (*write_block_data)(const struct i2c_client *client, u8 command,
@@ -311,27 +310,17 @@ static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
311310
/*----------------------------------------------------------------------*/
312311

313312
/*
314-
* The IRQ logic includes a "real" handler running in IRQ context just
315-
* long enough to schedule this workqueue entry. We need a task context
316-
* to talk to the RTC, since I2C I/O calls require that; and disable the
317-
* IRQ until we clear its status on the chip, so that this handler can
318-
* work with any type of triggering (not just falling edge).
319-
*
320313
* The ds1337 and ds1339 both have two alarms, but we only use the first
321314
* one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
322315
* signal; ds1339 chips have only one alarm signal.
323316
*/
324-
static void ds1307_work(struct work_struct *work)
317+
static irqreturn_t ds1307_irq(int irq, void *dev_id)
325318
{
326-
struct ds1307 *ds1307;
327-
struct i2c_client *client;
328-
struct mutex *lock;
319+
struct i2c_client *client = dev_id;
320+
struct ds1307 *ds1307 = i2c_get_clientdata(client);
321+
struct mutex *lock = &ds1307->rtc->ops_lock;
329322
int stat, control;
330323

331-
ds1307 = container_of(work, struct ds1307, work);
332-
client = ds1307->client;
333-
lock = &ds1307->rtc->ops_lock;
334-
335324
mutex_lock(lock);
336325
stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
337326
if (stat < 0)
@@ -352,18 +341,8 @@ static void ds1307_work(struct work_struct *work)
352341
}
353342

354343
out:
355-
if (test_bit(HAS_ALARM, &ds1307->flags))
356-
enable_irq(client->irq);
357344
mutex_unlock(lock);
358-
}
359345

360-
static irqreturn_t ds1307_irq(int irq, void *dev_id)
361-
{
362-
struct i2c_client *client = dev_id;
363-
struct ds1307 *ds1307 = i2c_get_clientdata(client);
364-
365-
disable_irq_nosync(irq);
366-
schedule_work(&ds1307->work);
367346
return IRQ_HANDLED;
368347
}
369348

@@ -634,13 +613,14 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
634613
MCP794XX_BIT_ALMX_C1 | \
635614
MCP794XX_BIT_ALMX_C2)
636615

637-
static void mcp794xx_work(struct work_struct *work)
616+
static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
638617
{
639-
struct ds1307 *ds1307 = container_of(work, struct ds1307, work);
640-
struct i2c_client *client = ds1307->client;
618+
struct i2c_client *client = dev_id;
619+
struct ds1307 *ds1307 = i2c_get_clientdata(client);
620+
struct mutex *lock = &ds1307->rtc->ops_lock;
641621
int reg, ret;
642622

643-
mutex_lock(&ds1307->rtc->ops_lock);
623+
mutex_lock(lock);
644624

645625
/* Check and clear alarm 0 interrupt flag. */
646626
reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
@@ -665,9 +645,9 @@ static void mcp794xx_work(struct work_struct *work)
665645
rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
666646

667647
out:
668-
if (test_bit(HAS_ALARM, &ds1307->flags))
669-
enable_irq(client->irq);
670-
mutex_unlock(&ds1307->rtc->ops_lock);
648+
mutex_unlock(lock);
649+
650+
return IRQ_HANDLED;
671651
}
672652

673653
static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
@@ -734,25 +714,25 @@ static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
734714
regs[3] = bin2bcd(t->time.tm_sec);
735715
regs[4] = bin2bcd(t->time.tm_min);
736716
regs[5] = bin2bcd(t->time.tm_hour);
737-
regs[6] = bin2bcd(t->time.tm_wday) + 1;
717+
regs[6] = bin2bcd(t->time.tm_wday + 1);
738718
regs[7] = bin2bcd(t->time.tm_mday);
739-
regs[8] = bin2bcd(t->time.tm_mon) + 1;
719+
regs[8] = bin2bcd(t->time.tm_mon + 1);
740720

741721
/* Clear the alarm 0 interrupt flag. */
742722
regs[6] &= ~MCP794XX_BIT_ALMX_IF;
743723
/* Set alarm match: second, minute, hour, day, date, month. */
744724
regs[6] |= MCP794XX_MSK_ALMX_MATCH;
745-
746-
if (t->enabled)
747-
regs[0] |= MCP794XX_BIT_ALM0_EN;
748-
else
749-
regs[0] &= ~MCP794XX_BIT_ALM0_EN;
725+
/* Disable interrupt. We will not enable until completely programmed */
726+
regs[0] &= ~MCP794XX_BIT_ALM0_EN;
750727

751728
ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
752729
if (ret < 0)
753730
return ret;
754731

755-
return 0;
732+
if (!t->enabled)
733+
return 0;
734+
regs[0] |= MCP794XX_BIT_ALM0_EN;
735+
return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
756736
}
757737

758738
static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
@@ -798,13 +778,6 @@ ds1307_nvram_read(struct file *filp, struct kobject *kobj,
798778
client = kobj_to_i2c_client(kobj);
799779
ds1307 = i2c_get_clientdata(client);
800780

801-
if (unlikely(off >= ds1307->nvram->size))
802-
return 0;
803-
if ((off + count) > ds1307->nvram->size)
804-
count = ds1307->nvram->size - off;
805-
if (unlikely(!count))
806-
return count;
807-
808781
result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
809782
count, buf);
810783
if (result < 0)
@@ -824,13 +797,6 @@ ds1307_nvram_write(struct file *filp, struct kobject *kobj,
824797
client = kobj_to_i2c_client(kobj);
825798
ds1307 = i2c_get_clientdata(client);
826799

827-
if (unlikely(off >= ds1307->nvram->size))
828-
return -EFBIG;
829-
if ((off + count) > ds1307->nvram->size)
830-
count = ds1307->nvram->size - off;
831-
if (unlikely(!count))
832-
return count;
833-
834800
result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
835801
count, buf);
836802
if (result < 0) {
@@ -894,8 +860,11 @@ static int ds1307_probe(struct i2c_client *client,
894860
struct chip_desc *chip = &chips[id->driver_data];
895861
struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
896862
bool want_irq = false;
863+
bool ds1307_can_wakeup_device = false;
897864
unsigned char *buf;
898865
struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
866+
irq_handler_t irq_handler = ds1307_irq;
867+
899868
static const int bbsqi_bitpos[] = {
900869
[ds_1337] = 0,
901870
[ds_1339] = DS1339_BIT_BBSQI,
@@ -939,6 +908,24 @@ static int ds1307_probe(struct i2c_client *client,
939908
ds1307->write_block_data = ds1307_write_block_data;
940909
}
941910

911+
#ifdef CONFIG_OF
912+
/*
913+
* One would expect the device to be marked as a wakeup source only
914+
* when an IRQ pin of the RTC is routed to an interrupt line of the
915+
* CPU. In practice, such an IRQ pin can be connected to a PMIC and
916+
* this allows the device to be powered up when RTC alarm rings. This
917+
* is for instance the case on the Witty Pi extension board. On this
918+
* device with no IRQ driectly connected to the SoC, the RTC chip
919+
* can be forced as a wakeup source by stating that explicitly in
920+
* the device's .dts file using the "wakeup-source" boolean property.
921+
* If "wakeup-source is set, don't request an IRQ.
922+
* This will guarantee 'wakealarm' sysfs entry is available on the device.
923+
*/
924+
if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
925+
ds1307_can_wakeup_device = true;
926+
}
927+
#endif
928+
942929
switch (ds1307->type) {
943930
case ds_1337:
944931
case ds_1339:
@@ -957,13 +944,12 @@ static int ds1307_probe(struct i2c_client *client,
957944
ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
958945

959946
/*
960-
* Using IRQ? Disable the square wave and both alarms.
947+
* Using IRQ or set wakeup-source in .dts file?
948+
* Disable the square wave and both alarms.
961949
* For some variants, be sure alarms can trigger when we're
962950
* running on Vbackup (BBSQI/BBSQW)
963951
*/
964-
if (ds1307->client->irq > 0 && chip->alarm) {
965-
INIT_WORK(&ds1307->work, ds1307_work);
966-
952+
if (chip->alarm & (ds1307->client->irq > 0 || ds1307_can_wakeup_device)) {
967953
ds1307->regs[0] |= DS1337_BIT_INTCN
968954
| bbsqi_bitpos[ds1307->type];
969955
ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
@@ -1053,7 +1039,7 @@ static int ds1307_probe(struct i2c_client *client,
10531039
case mcp794xx:
10541040
rtc_ops = &mcp794xx_rtc_ops;
10551041
if (ds1307->client->irq > 0 && chip->alarm) {
1056-
INIT_WORK(&ds1307->work, mcp794xx_work);
1042+
irq_handler = mcp794xx_irq;
10571043
want_irq = true;
10581044
}
10591045
break;
@@ -1168,24 +1154,36 @@ static int ds1307_probe(struct i2c_client *client,
11681154
bin2bcd(tmp));
11691155
}
11701156

1171-
device_set_wakeup_capable(&client->dev, want_irq);
1157+
if (want_irq) {
1158+
device_set_wakeup_capable(&client->dev, true);
1159+
set_bit(HAS_ALARM, &ds1307->flags);
1160+
}
11721161
ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
11731162
rtc_ops, THIS_MODULE);
11741163
if (IS_ERR(ds1307->rtc)) {
11751164
return PTR_ERR(ds1307->rtc);
11761165
}
11771166

1167+
if (ds1307_can_wakeup_device) {
1168+
/* Disable request for an IRQ */
1169+
want_irq = false;
1170+
dev_info(&client->dev, "'wakeup-source' is set, request for an IRQ is disabled!\n");
1171+
/* We cannot support UIE mode if we do not have an IRQ line */
1172+
ds1307->rtc->uie_unsupported = 1;
1173+
}
1174+
11781175
if (want_irq) {
1179-
err = request_irq(client->irq, ds1307_irq, IRQF_SHARED,
1180-
ds1307->rtc->name, client);
1176+
err = devm_request_threaded_irq(&client->dev,
1177+
client->irq, NULL, irq_handler,
1178+
IRQF_SHARED | IRQF_ONESHOT,
1179+
ds1307->rtc->name, client);
11811180
if (err) {
11821181
client->irq = 0;
1182+
device_set_wakeup_capable(&client->dev, false);
1183+
clear_bit(HAS_ALARM, &ds1307->flags);
11831184
dev_err(&client->dev, "unable to request IRQ!\n");
1184-
} else {
1185-
1186-
set_bit(HAS_ALARM, &ds1307->flags);
1185+
} else
11871186
dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
1188-
}
11891187
}
11901188

11911189
if (chip->nvram_size) {
@@ -1231,11 +1229,6 @@ static int ds1307_remove(struct i2c_client *client)
12311229
{
12321230
struct ds1307 *ds1307 = i2c_get_clientdata(client);
12331231

1234-
if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
1235-
free_irq(client->irq, client);
1236-
cancel_work_sync(&ds1307->work);
1237-
}
1238-
12391232
if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
12401233
sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
12411234

0 commit comments

Comments
 (0)