Skip to content

Commit e48585d

Browse files
hkallweitalexandrebelloni
authored andcommitted
rtc: ds1307: factor out century bit handling
The driver has lots of places with chip-specific code what doesn't necessarily facilitate maintenance. Let's describe chip-specific differences in century bit handling in struct chip_desc to improve this. Signed-off-by: Heiner Kallweit <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
1 parent 078f3f6 commit e48585d

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

drivers/rtc/rtc-ds1307.c

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ struct chip_desc {
136136
unsigned alarm:1;
137137
u16 nvram_offset;
138138
u16 nvram_size;
139+
u8 century_reg;
140+
u8 century_enable_bit;
141+
u8 century_bit;
139142
u16 trickle_charger_reg;
140143
u8 trickle_charger_setup;
141144
u8 (*do_trickle_setup)(struct ds1307 *, uint32_t,
@@ -151,24 +154,33 @@ static struct chip_desc chips[last_ds_type] = {
151154
},
152155
[ds_1337] = {
153156
.alarm = 1,
157+
.century_reg = DS1307_REG_MONTH,
158+
.century_bit = DS1337_BIT_CENTURY,
154159
},
155160
[ds_1338] = {
156161
.nvram_offset = 8,
157162
.nvram_size = 56,
158163
},
159164
[ds_1339] = {
160165
.alarm = 1,
166+
.century_reg = DS1307_REG_MONTH,
167+
.century_bit = DS1337_BIT_CENTURY,
161168
.trickle_charger_reg = 0x10,
162169
.do_trickle_setup = &do_trickle_setup_ds1339,
163170
},
164171
[ds_1340] = {
172+
.century_reg = DS1307_REG_HOUR,
173+
.century_enable_bit = DS1340_BIT_CENTURY_EN,
174+
.century_bit = DS1340_BIT_CENTURY,
165175
.trickle_charger_reg = 0x08,
166176
},
167177
[ds_1388] = {
168178
.trickle_charger_reg = 0x0a,
169179
},
170180
[ds_3231] = {
171181
.alarm = 1,
182+
.century_reg = DS1307_REG_MONTH,
183+
.century_bit = DS1337_BIT_CENTURY,
172184
},
173185
[rx_8130] = {
174186
.alarm = 1,
@@ -328,6 +340,7 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
328340
{
329341
struct ds1307 *ds1307 = dev_get_drvdata(dev);
330342
int tmp, ret;
343+
const struct chip_desc *chip = &chips[ds1307->type];
331344

332345
/* read the RTC date and time registers all at once */
333346
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)
355368
t->tm_mon = bcd2bin(tmp) - 1;
356369
t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
357370

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;
374374

375375
dev_dbg(dev, "%s secs=%d, mins=%d, "
376376
"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)
385385
static int ds1307_set_time(struct device *dev, struct rtc_time *t)
386386
{
387387
struct ds1307 *ds1307 = dev_get_drvdata(dev);
388+
const struct chip_desc *chip = &chips[ds1307->type];
388389
int result;
389390
int tmp;
390391
u8 *buf = ds1307->regs;
@@ -395,24 +396,14 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
395396
t->tm_hour, t->tm_mday,
396397
t->tm_mon, t->tm_year, t->tm_wday);
397398

398-
#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
399399
if (t->tm_year < 100)
400400
return -EINVAL;
401401

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;
414405
#else
415-
if (t->tm_year < 100 || t->tm_year > 199)
406+
if (t->tm_year > 199)
416407
return -EINVAL;
417408
#endif
418409

@@ -427,29 +418,19 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
427418
tmp = t->tm_year - 100;
428419
buf[DS1307_REG_YEAR] = bin2bcd(tmp);
429420

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) {
443427
/*
444428
* these bits were cleared when preparing the date/time
445429
* values and need to be set again before writing the
446430
* buffer out to the device.
447431
*/
448432
buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
449433
buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
450-
break;
451-
default:
452-
break;
453434
}
454435

455436
dev_dbg(dev, "%s: %7ph\n", "write", buf);

0 commit comments

Comments
 (0)