@@ -136,6 +136,9 @@ struct chip_desc {
136
136
unsigned alarm :1 ;
137
137
u16 nvram_offset ;
138
138
u16 nvram_size ;
139
+ u8 century_reg ;
140
+ u8 century_enable_bit ;
141
+ u8 century_bit ;
139
142
u16 trickle_charger_reg ;
140
143
u8 trickle_charger_setup ;
141
144
u8 (* do_trickle_setup )(struct ds1307 * , uint32_t ,
@@ -151,24 +154,33 @@ static struct chip_desc chips[last_ds_type] = {
151
154
},
152
155
[ds_1337 ] = {
153
156
.alarm = 1 ,
157
+ .century_reg = DS1307_REG_MONTH ,
158
+ .century_bit = DS1337_BIT_CENTURY ,
154
159
},
155
160
[ds_1338 ] = {
156
161
.nvram_offset = 8 ,
157
162
.nvram_size = 56 ,
158
163
},
159
164
[ds_1339 ] = {
160
165
.alarm = 1 ,
166
+ .century_reg = DS1307_REG_MONTH ,
167
+ .century_bit = DS1337_BIT_CENTURY ,
161
168
.trickle_charger_reg = 0x10 ,
162
169
.do_trickle_setup = & do_trickle_setup_ds1339 ,
163
170
},
164
171
[ds_1340 ] = {
172
+ .century_reg = DS1307_REG_HOUR ,
173
+ .century_enable_bit = DS1340_BIT_CENTURY_EN ,
174
+ .century_bit = DS1340_BIT_CENTURY ,
165
175
.trickle_charger_reg = 0x08 ,
166
176
},
167
177
[ds_1388 ] = {
168
178
.trickle_charger_reg = 0x0a ,
169
179
},
170
180
[ds_3231 ] = {
171
181
.alarm = 1 ,
182
+ .century_reg = DS1307_REG_MONTH ,
183
+ .century_bit = DS1337_BIT_CENTURY ,
172
184
},
173
185
[rx_8130 ] = {
174
186
.alarm = 1 ,
@@ -328,6 +340,7 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
328
340
{
329
341
struct ds1307 * ds1307 = dev_get_drvdata (dev );
330
342
int tmp , ret ;
343
+ const struct chip_desc * chip = & chips [ds1307 -> type ];
331
344
332
345
/* read the RTC date and time registers all at once */
333
346
ret = regmap_bulk_read (ds1307 -> regmap , ds1307 -> offset , ds1307 -> regs , 7 );
@@ -355,22 +368,9 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
355
368
t -> tm_mon = bcd2bin (tmp ) - 1 ;
356
369
t -> tm_year = bcd2bin (ds1307 -> regs [DS1307_REG_YEAR ]) + 100 ;
357
370
358
- #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
359
- switch (ds1307 -> type ) {
360
- case ds_1337 :
361
- case ds_1339 :
362
- case ds_3231 :
363
- if (ds1307 -> regs [DS1307_REG_MONTH ] & DS1337_BIT_CENTURY )
364
- t -> tm_year += 100 ;
365
- break ;
366
- case ds_1340 :
367
- if (ds1307 -> regs [DS1307_REG_HOUR ] & DS1340_BIT_CENTURY )
368
- t -> tm_year += 100 ;
369
- break ;
370
- default :
371
- break ;
372
- }
373
- #endif
371
+ if (ds1307 -> regs [chip -> century_reg ] & chip -> century_bit &&
372
+ IS_ENABLED (CONFIG_RTC_DRV_DS1307_CENTURY ))
373
+ t -> tm_year += 100 ;
374
374
375
375
dev_dbg (dev , "%s secs=%d, mins=%d, "
376
376
"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n" ,
@@ -385,6 +385,7 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
385
385
static int ds1307_set_time (struct device * dev , struct rtc_time * t )
386
386
{
387
387
struct ds1307 * ds1307 = dev_get_drvdata (dev );
388
+ const struct chip_desc * chip = & chips [ds1307 -> type ];
388
389
int result ;
389
390
int tmp ;
390
391
u8 * buf = ds1307 -> regs ;
@@ -395,24 +396,14 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
395
396
t -> tm_hour , t -> tm_mday ,
396
397
t -> tm_mon , t -> tm_year , t -> tm_wday );
397
398
398
- #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
399
399
if (t -> tm_year < 100 )
400
400
return - EINVAL ;
401
401
402
- switch (ds1307 -> type ) {
403
- case ds_1337 :
404
- case ds_1339 :
405
- case ds_3231 :
406
- case ds_1340 :
407
- if (t -> tm_year > 299 )
408
- return - EINVAL ;
409
- default :
410
- if (t -> tm_year > 199 )
411
- return - EINVAL ;
412
- break ;
413
- }
402
+ #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
403
+ if (t -> tm_year > (chip -> century_bit ? 299 : 199 ))
404
+ return - EINVAL ;
414
405
#else
415
- if (t -> tm_year < 100 || t -> tm_year > 199 )
406
+ if (t -> tm_year > 199 )
416
407
return - EINVAL ;
417
408
#endif
418
409
@@ -427,29 +418,19 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
427
418
tmp = t -> tm_year - 100 ;
428
419
buf [DS1307_REG_YEAR ] = bin2bcd (tmp );
429
420
430
- switch (ds1307 -> type ) {
431
- case ds_1337 :
432
- case ds_1339 :
433
- case ds_3231 :
434
- if (t -> tm_year > 199 )
435
- buf [DS1307_REG_MONTH ] |= DS1337_BIT_CENTURY ;
436
- break ;
437
- case ds_1340 :
438
- buf [DS1307_REG_HOUR ] |= DS1340_BIT_CENTURY_EN ;
439
- if (t -> tm_year > 199 )
440
- buf [DS1307_REG_HOUR ] |= DS1340_BIT_CENTURY ;
441
- break ;
442
- case mcp794xx :
421
+ if (chip -> century_enable_bit )
422
+ buf [chip -> century_reg ] |= chip -> century_enable_bit ;
423
+ if (t -> tm_year > 199 && chip -> century_bit )
424
+ buf [chip -> century_reg ] |= chip -> century_bit ;
425
+
426
+ if (ds1307 -> type == mcp794xx ) {
443
427
/*
444
428
* these bits were cleared when preparing the date/time
445
429
* values and need to be set again before writing the
446
430
* buffer out to the device.
447
431
*/
448
432
buf [DS1307_REG_SECS ] |= MCP794XX_BIT_ST ;
449
433
buf [DS1307_REG_WDAY ] |= MCP794XX_BIT_VBATEN ;
450
- break ;
451
- default :
452
- break ;
453
434
}
454
435
455
436
dev_dbg (dev , "%s: %7ph\n" , "write" , buf );
0 commit comments