11
11
* published by the Free Software Foundation.
12
12
*/
13
13
14
- #include <linux/module.h>
14
+ #include <linux/bcd.h>
15
+ #include <linux/i2c.h>
15
16
#include <linux/init.h>
17
+ #include <linux/module.h>
18
+ #include <linux/rtc/ds1307.h>
19
+ #include <linux/rtc.h>
16
20
#include <linux/slab.h>
17
- #include <linux/i2c.h>
18
21
#include <linux/string.h>
19
- #include <linux/rtc.h>
20
- #include <linux/bcd.h>
21
- #include <linux/rtc/ds1307.h>
22
22
23
23
/*
24
24
* We can't determine type by probing, but if we expect pre-Linux code
25
25
* to have set the chip up as a clock (turning on the oscillator and
26
26
* setting the date and time), Linux can ignore the non-clock features.
27
27
* That's a natural job for a factory or repair bench.
28
28
*/
29
+
29
30
enum ds_type {
30
31
ds_1307 ,
31
32
ds_1337 ,
@@ -41,7 +42,6 @@ enum ds_type {
41
42
/* rs5c372 too? different address... */
42
43
};
43
44
44
-
45
45
/* RTC registers don't differ much, except for the century flag */
46
46
#define DS1307_REG_SECS 0x00 /* 00-59 */
47
47
# define DS1307_BIT_CH 0x80
@@ -114,7 +114,6 @@ struct ds1307 {
114
114
#define HAS_ALARM 1 /* bit 1 == irq claimed */
115
115
struct i2c_client * client ;
116
116
struct rtc_device * rtc ;
117
- struct work_struct work ;
118
117
s32 (* read_block_data )(const struct i2c_client * client , u8 command ,
119
118
u8 length , u8 * values );
120
119
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,
311
310
/*----------------------------------------------------------------------*/
312
311
313
312
/*
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
- *
320
313
* The ds1337 and ds1339 both have two alarms, but we only use the first
321
314
* one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
322
315
* signal; ds1339 chips have only one alarm signal.
323
316
*/
324
- static void ds1307_work ( struct work_struct * work )
317
+ static irqreturn_t ds1307_irq ( int irq , void * dev_id )
325
318
{
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 ;
329
322
int stat , control ;
330
323
331
- ds1307 = container_of (work , struct ds1307 , work );
332
- client = ds1307 -> client ;
333
- lock = & ds1307 -> rtc -> ops_lock ;
334
-
335
324
mutex_lock (lock );
336
325
stat = i2c_smbus_read_byte_data (client , DS1337_REG_STATUS );
337
326
if (stat < 0 )
@@ -352,18 +341,8 @@ static void ds1307_work(struct work_struct *work)
352
341
}
353
342
354
343
out :
355
- if (test_bit (HAS_ALARM , & ds1307 -> flags ))
356
- enable_irq (client -> irq );
357
344
mutex_unlock (lock );
358
- }
359
345
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 );
367
346
return IRQ_HANDLED ;
368
347
}
369
348
@@ -634,13 +613,14 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
634
613
MCP794XX_BIT_ALMX_C1 | \
635
614
MCP794XX_BIT_ALMX_C2)
636
615
637
- static void mcp794xx_work ( struct work_struct * work )
616
+ static irqreturn_t mcp794xx_irq ( int irq , void * dev_id )
638
617
{
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 ;
641
621
int reg , ret ;
642
622
643
- mutex_lock (& ds1307 -> rtc -> ops_lock );
623
+ mutex_lock (lock );
644
624
645
625
/* Check and clear alarm 0 interrupt flag. */
646
626
reg = i2c_smbus_read_byte_data (client , MCP794XX_REG_ALARM0_CTRL );
@@ -665,9 +645,9 @@ static void mcp794xx_work(struct work_struct *work)
665
645
rtc_update_irq (ds1307 -> rtc , 1 , RTC_AF | RTC_IRQF );
666
646
667
647
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 ;
671
651
}
672
652
673
653
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)
734
714
regs [3 ] = bin2bcd (t -> time .tm_sec );
735
715
regs [4 ] = bin2bcd (t -> time .tm_min );
736
716
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 ) ;
738
718
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 ) ;
740
720
741
721
/* Clear the alarm 0 interrupt flag. */
742
722
regs [6 ] &= ~MCP794XX_BIT_ALMX_IF ;
743
723
/* Set alarm match: second, minute, hour, day, date, month. */
744
724
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 ;
750
727
751
728
ret = ds1307 -> write_block_data (client , MCP794XX_REG_CONTROL , 10 , regs );
752
729
if (ret < 0 )
753
730
return ret ;
754
731
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 ]);
756
736
}
757
737
758
738
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,
798
778
client = kobj_to_i2c_client (kobj );
799
779
ds1307 = i2c_get_clientdata (client );
800
780
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
-
808
781
result = ds1307 -> read_block_data (client , ds1307 -> nvram_offset + off ,
809
782
count , buf );
810
783
if (result < 0 )
@@ -824,13 +797,6 @@ ds1307_nvram_write(struct file *filp, struct kobject *kobj,
824
797
client = kobj_to_i2c_client (kobj );
825
798
ds1307 = i2c_get_clientdata (client );
826
799
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
-
834
800
result = ds1307 -> write_block_data (client , ds1307 -> nvram_offset + off ,
835
801
count , buf );
836
802
if (result < 0 ) {
@@ -894,8 +860,11 @@ static int ds1307_probe(struct i2c_client *client,
894
860
struct chip_desc * chip = & chips [id -> driver_data ];
895
861
struct i2c_adapter * adapter = to_i2c_adapter (client -> dev .parent );
896
862
bool want_irq = false;
863
+ bool ds1307_can_wakeup_device = false;
897
864
unsigned char * buf ;
898
865
struct ds1307_platform_data * pdata = dev_get_platdata (& client -> dev );
866
+ irq_handler_t irq_handler = ds1307_irq ;
867
+
899
868
static const int bbsqi_bitpos [] = {
900
869
[ds_1337 ] = 0 ,
901
870
[ds_1339 ] = DS1339_BIT_BBSQI ,
@@ -939,6 +908,24 @@ static int ds1307_probe(struct i2c_client *client,
939
908
ds1307 -> write_block_data = ds1307_write_block_data ;
940
909
}
941
910
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
+
942
929
switch (ds1307 -> type ) {
943
930
case ds_1337 :
944
931
case ds_1339 :
@@ -957,13 +944,12 @@ static int ds1307_probe(struct i2c_client *client,
957
944
ds1307 -> regs [0 ] &= ~DS1337_BIT_nEOSC ;
958
945
959
946
/*
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.
961
949
* For some variants, be sure alarms can trigger when we're
962
950
* running on Vbackup (BBSQI/BBSQW)
963
951
*/
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 )) {
967
953
ds1307 -> regs [0 ] |= DS1337_BIT_INTCN
968
954
| bbsqi_bitpos [ds1307 -> type ];
969
955
ds1307 -> regs [0 ] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE );
@@ -1053,7 +1039,7 @@ static int ds1307_probe(struct i2c_client *client,
1053
1039
case mcp794xx :
1054
1040
rtc_ops = & mcp794xx_rtc_ops ;
1055
1041
if (ds1307 -> client -> irq > 0 && chip -> alarm ) {
1056
- INIT_WORK ( & ds1307 -> work , mcp794xx_work ) ;
1042
+ irq_handler = mcp794xx_irq ;
1057
1043
want_irq = true;
1058
1044
}
1059
1045
break ;
@@ -1168,24 +1154,36 @@ static int ds1307_probe(struct i2c_client *client,
1168
1154
bin2bcd (tmp ));
1169
1155
}
1170
1156
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
+ }
1172
1161
ds1307 -> rtc = devm_rtc_device_register (& client -> dev , client -> name ,
1173
1162
rtc_ops , THIS_MODULE );
1174
1163
if (IS_ERR (ds1307 -> rtc )) {
1175
1164
return PTR_ERR (ds1307 -> rtc );
1176
1165
}
1177
1166
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
+
1178
1175
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 );
1181
1180
if (err ) {
1182
1181
client -> irq = 0 ;
1182
+ device_set_wakeup_capable (& client -> dev , false);
1183
+ clear_bit (HAS_ALARM , & ds1307 -> flags );
1183
1184
dev_err (& client -> dev , "unable to request IRQ!\n" );
1184
- } else {
1185
-
1186
- set_bit (HAS_ALARM , & ds1307 -> flags );
1185
+ } else
1187
1186
dev_dbg (& client -> dev , "got IRQ %d\n" , client -> irq );
1188
- }
1189
1187
}
1190
1188
1191
1189
if (chip -> nvram_size ) {
@@ -1231,11 +1229,6 @@ static int ds1307_remove(struct i2c_client *client)
1231
1229
{
1232
1230
struct ds1307 * ds1307 = i2c_get_clientdata (client );
1233
1231
1234
- if (test_and_clear_bit (HAS_ALARM , & ds1307 -> flags )) {
1235
- free_irq (client -> irq , client );
1236
- cancel_work_sync (& ds1307 -> work );
1237
- }
1238
-
1239
1232
if (test_and_clear_bit (HAS_NVRAM , & ds1307 -> flags ))
1240
1233
sysfs_remove_bin_file (& client -> dev .kobj , ds1307 -> nvram );
1241
1234
0 commit comments