2 * SPI Driver for Microchip MCP795 RTC
4 * Copyright (C) Josef Gajdusek <atx@atx.name>
6 * based on other Linux RTC drivers
9 * http://ww1.microchip.com/downloads/en/DeviceDoc/22280A.pdf
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/printk.h>
21 #include <linux/spi/spi.h>
22 #include <linux/rtc.h>
24 #include <linux/bcd.h>
25 #include <linux/delay.h>
27 /* MCP795 Instructions, see datasheet table 3-1 */
28 #define MCP795_EEREAD 0x03
29 #define MCP795_EEWRITE 0x02
30 #define MCP795_EEWRDI 0x04
31 #define MCP795_EEWREN 0x06
32 #define MCP795_SRREAD 0x05
33 #define MCP795_SRWRITE 0x01
34 #define MCP795_READ 0x13
35 #define MCP795_WRITE 0x12
36 #define MCP795_UNLOCK 0x14
37 #define MCP795_IDWRITE 0x32
38 #define MCP795_IDREAD 0x33
39 #define MCP795_CLRWDT 0x44
40 #define MCP795_CLRRAM 0x54
42 /* MCP795 RTCC registers, see datasheet table 4-1 */
43 #define MCP795_REG_SECONDS 0x01
44 #define MCP795_REG_DAY 0x04
45 #define MCP795_REG_MONTH 0x06
46 #define MCP795_REG_CONTROL 0x08
47 #define MCP795_REG_ALM0_SECONDS 0x0C
48 #define MCP795_REG_ALM0_DAY 0x0F
50 #define MCP795_ST_BIT BIT(7)
51 #define MCP795_24_BIT BIT(6)
52 #define MCP795_LP_BIT BIT(5)
53 #define MCP795_EXTOSC_BIT BIT(3)
54 #define MCP795_OSCON_BIT BIT(5)
55 #define MCP795_ALM0_BIT BIT(4)
56 #define MCP795_ALM1_BIT BIT(5)
57 #define MCP795_ALM0IF_BIT BIT(3)
58 #define MCP795_ALM0C0_BIT BIT(4)
59 #define MCP795_ALM0C1_BIT BIT(5)
60 #define MCP795_ALM0C2_BIT BIT(6)
62 #define SEC_PER_DAY (24 * 60 * 60)
64 static int mcp795_rtcc_read(struct device
*dev
, u8 addr
, u8
*buf
, u8 count
)
66 struct spi_device
*spi
= to_spi_device(dev
);
72 ret
= spi_write_then_read(spi
, tx
, sizeof(tx
), buf
, count
);
75 dev_err(dev
, "Failed reading %d bytes from address %x.\n",
81 static int mcp795_rtcc_write(struct device
*dev
, u8 addr
, u8
*data
, u8 count
)
83 struct spi_device
*spi
= to_spi_device(dev
);
89 memcpy(&tx
[2], data
, count
);
91 ret
= spi_write(spi
, tx
, 2 + count
);
94 dev_err(dev
, "Failed to write %d bytes to address %x.\n",
100 static int mcp795_rtcc_set_bits(struct device
*dev
, u8 addr
, u8 mask
, u8 state
)
105 ret
= mcp795_rtcc_read(dev
, addr
, &tmp
, 1);
109 if ((tmp
& mask
) != state
) {
110 tmp
= (tmp
& ~mask
) | state
;
111 ret
= mcp795_rtcc_write(dev
, addr
, &tmp
, 1);
117 static int mcp795_stop_oscillator(struct device
*dev
, bool *extosc
)
123 ret
= mcp795_rtcc_set_bits(dev
, MCP795_REG_SECONDS
, MCP795_ST_BIT
, 0);
126 ret
= mcp795_rtcc_read(dev
, MCP795_REG_CONTROL
, &data
, 1);
129 *extosc
= !!(data
& MCP795_EXTOSC_BIT
);
130 ret
= mcp795_rtcc_set_bits(
131 dev
, MCP795_REG_CONTROL
, MCP795_EXTOSC_BIT
, 0);
134 /* wait for the OSCON bit to clear */
136 usleep_range(700, 800);
137 ret
= mcp795_rtcc_read(dev
, MCP795_REG_DAY
, &data
, 1);
140 if (!(data
& MCP795_OSCON_BIT
))
145 return !retries
? -EIO
: ret
;
148 static int mcp795_start_oscillator(struct device
*dev
, bool *extosc
)
151 u8 data
= *extosc
? MCP795_EXTOSC_BIT
: 0;
154 ret
= mcp795_rtcc_set_bits(
155 dev
, MCP795_REG_CONTROL
, MCP795_EXTOSC_BIT
, data
);
159 return mcp795_rtcc_set_bits(
160 dev
, MCP795_REG_SECONDS
, MCP795_ST_BIT
, MCP795_ST_BIT
);
163 /* Enable or disable Alarm 0 in RTC */
164 static int mcp795_update_alarm(struct device
*dev
, bool enable
)
168 dev_dbg(dev
, "%s alarm\n", enable
? "Enable" : "Disable");
171 /* clear ALM0IF (Alarm 0 Interrupt Flag) bit */
172 ret
= mcp795_rtcc_set_bits(dev
, MCP795_REG_ALM0_DAY
,
173 MCP795_ALM0IF_BIT
, 0);
177 ret
= mcp795_rtcc_set_bits(dev
, MCP795_REG_CONTROL
,
178 MCP795_ALM0_BIT
, MCP795_ALM0_BIT
);
180 /* disable alarm 0 and alarm 1 */
181 ret
= mcp795_rtcc_set_bits(dev
, MCP795_REG_CONTROL
,
182 MCP795_ALM0_BIT
| MCP795_ALM1_BIT
, 0);
187 static int mcp795_set_time(struct device
*dev
, struct rtc_time
*tim
)
193 /* Stop RTC and store current value of EXTOSC bit */
194 ret
= mcp795_stop_oscillator(dev
, &extosc
);
198 /* Read first, so we can leave config bits untouched */
199 ret
= mcp795_rtcc_read(dev
, MCP795_REG_SECONDS
, data
, sizeof(data
));
204 data
[0] = (data
[0] & 0x80) | bin2bcd(tim
->tm_sec
);
205 data
[1] = (data
[1] & 0x80) | bin2bcd(tim
->tm_min
);
206 data
[2] = bin2bcd(tim
->tm_hour
);
207 data
[3] = (data
[3] & 0xF8) | bin2bcd(tim
->tm_wday
+ 1);
208 data
[4] = bin2bcd(tim
->tm_mday
);
209 data
[5] = (data
[5] & MCP795_LP_BIT
) | bin2bcd(tim
->tm_mon
+ 1);
211 if (tim
->tm_year
> 100)
214 data
[6] = bin2bcd(tim
->tm_year
);
216 /* Always write the date and month using a separate Write command.
217 * This is a workaround for a know silicon issue that some combinations
218 * of date and month values may result in the date being reset to 1.
220 ret
= mcp795_rtcc_write(dev
, MCP795_REG_SECONDS
, data
, 5);
224 ret
= mcp795_rtcc_write(dev
, MCP795_REG_MONTH
, &data
[5], 2);
228 /* Start back RTC and restore previous value of EXTOSC bit.
229 * There is no need to clear EXTOSC bit when the previous value was 0
230 * because it was already cleared when stopping the RTC oscillator.
232 ret
= mcp795_start_oscillator(dev
, extosc
? &extosc
: NULL
);
236 dev_dbg(dev
, "Set mcp795: %04d-%02d-%02d(%d) %02d:%02d:%02d\n",
237 tim
->tm_year
+ 1900, tim
->tm_mon
, tim
->tm_mday
,
238 tim
->tm_wday
, tim
->tm_hour
, tim
->tm_min
, tim
->tm_sec
);
243 static int mcp795_read_time(struct device
*dev
, struct rtc_time
*tim
)
248 ret
= mcp795_rtcc_read(dev
, MCP795_REG_SECONDS
, data
, sizeof(data
));
253 tim
->tm_sec
= bcd2bin(data
[0] & 0x7F);
254 tim
->tm_min
= bcd2bin(data
[1] & 0x7F);
255 tim
->tm_hour
= bcd2bin(data
[2] & 0x3F);
256 tim
->tm_wday
= bcd2bin(data
[3] & 0x07) - 1;
257 tim
->tm_mday
= bcd2bin(data
[4] & 0x3F);
258 tim
->tm_mon
= bcd2bin(data
[5] & 0x1F) - 1;
259 tim
->tm_year
= bcd2bin(data
[6]) + 100; /* Assume we are in 20xx */
261 dev_dbg(dev
, "Read from mcp795: %04d-%02d-%02d(%d) %02d:%02d:%02d\n",
262 tim
->tm_year
+ 1900, tim
->tm_mon
, tim
->tm_mday
,
263 tim
->tm_wday
, tim
->tm_hour
, tim
->tm_min
, tim
->tm_sec
);
268 static int mcp795_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
270 struct rtc_time now_tm
;
276 /* Read current time from RTC hardware */
277 ret
= mcp795_read_time(dev
, &now_tm
);
280 /* Get the number of seconds since 1970 */
281 now
= rtc_tm_to_time64(&now_tm
);
282 later
= rtc_tm_to_time64(&alm
->time
);
285 /* make sure alarm fires within the next one year */
287 (SEC_PER_DAY
* (365 + is_leap_year(alm
->time
.tm_year
))))
290 ret
= mcp795_update_alarm(dev
, false);
293 /* Read registers, so we can leave configuration bits untouched */
294 ret
= mcp795_rtcc_read(dev
, MCP795_REG_ALM0_SECONDS
, tmp
, sizeof(tmp
));
298 alm
->time
.tm_year
= -1;
299 alm
->time
.tm_isdst
= -1;
300 alm
->time
.tm_yday
= -1;
302 tmp
[0] = (tmp
[0] & 0x80) | bin2bcd(alm
->time
.tm_sec
);
303 tmp
[1] = (tmp
[1] & 0x80) | bin2bcd(alm
->time
.tm_min
);
304 tmp
[2] = (tmp
[2] & 0xE0) | bin2bcd(alm
->time
.tm_hour
);
305 tmp
[3] = (tmp
[3] & 0x80) | bin2bcd(alm
->time
.tm_wday
+ 1);
306 /* set alarm match: seconds, minutes, hour, day, date and month */
307 tmp
[3] |= (MCP795_ALM0C2_BIT
| MCP795_ALM0C1_BIT
| MCP795_ALM0C0_BIT
);
308 tmp
[4] = (tmp
[4] & 0xC0) | bin2bcd(alm
->time
.tm_mday
);
309 tmp
[5] = (tmp
[5] & 0xE0) | bin2bcd(alm
->time
.tm_mon
+ 1);
311 ret
= mcp795_rtcc_write(dev
, MCP795_REG_ALM0_SECONDS
, tmp
, sizeof(tmp
));
315 /* enable alarm if requested */
317 ret
= mcp795_update_alarm(dev
, true);
320 dev_dbg(dev
, "Alarm IRQ armed\n");
322 dev_dbg(dev
, "Set alarm: %02d-%02d(%d) %02d:%02d:%02d\n",
323 alm
->time
.tm_mon
, alm
->time
.tm_mday
, alm
->time
.tm_wday
,
324 alm
->time
.tm_hour
, alm
->time
.tm_min
, alm
->time
.tm_sec
);
328 static int mcp795_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
333 ret
= mcp795_rtcc_read(
334 dev
, MCP795_REG_ALM0_SECONDS
, data
, sizeof(data
));
338 alm
->time
.tm_sec
= bcd2bin(data
[0] & 0x7F);
339 alm
->time
.tm_min
= bcd2bin(data
[1] & 0x7F);
340 alm
->time
.tm_hour
= bcd2bin(data
[2] & 0x1F);
341 alm
->time
.tm_wday
= bcd2bin(data
[3] & 0x07) - 1;
342 alm
->time
.tm_mday
= bcd2bin(data
[4] & 0x3F);
343 alm
->time
.tm_mon
= bcd2bin(data
[5] & 0x1F) - 1;
344 alm
->time
.tm_year
= -1;
345 alm
->time
.tm_isdst
= -1;
346 alm
->time
.tm_yday
= -1;
348 dev_dbg(dev
, "Read alarm: %02d-%02d(%d) %02d:%02d:%02d\n",
349 alm
->time
.tm_mon
, alm
->time
.tm_mday
, alm
->time
.tm_wday
,
350 alm
->time
.tm_hour
, alm
->time
.tm_min
, alm
->time
.tm_sec
);
354 static int mcp795_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
356 return mcp795_update_alarm(dev
, !!enabled
);
359 static irqreturn_t
mcp795_irq(int irq
, void *data
)
361 struct spi_device
*spi
= data
;
362 struct rtc_device
*rtc
= spi_get_drvdata(spi
);
363 struct mutex
*lock
= &rtc
->ops_lock
;
369 * There is no need to clear ALM0IF (Alarm 0 Interrupt Flag) bit,
370 * because it is done every time when alarm is enabled.
372 ret
= mcp795_update_alarm(&spi
->dev
, false);
375 "Failed to disable alarm in IRQ (ret=%d)\n", ret
);
376 rtc_update_irq(rtc
, 1, RTC_AF
| RTC_IRQF
);
383 static const struct rtc_class_ops mcp795_rtc_ops
= {
384 .read_time
= mcp795_read_time
,
385 .set_time
= mcp795_set_time
,
386 .read_alarm
= mcp795_read_alarm
,
387 .set_alarm
= mcp795_set_alarm
,
388 .alarm_irq_enable
= mcp795_alarm_irq_enable
391 static int mcp795_probe(struct spi_device
*spi
)
393 struct rtc_device
*rtc
;
396 spi
->mode
= SPI_MODE_0
;
397 spi
->bits_per_word
= 8;
398 ret
= spi_setup(spi
);
400 dev_err(&spi
->dev
, "Unable to setup SPI\n");
404 /* Start the oscillator but don't set the value of EXTOSC bit */
405 mcp795_start_oscillator(&spi
->dev
, NULL
);
406 /* Clear the 12 hour mode flag*/
407 mcp795_rtcc_set_bits(&spi
->dev
, 0x03, MCP795_24_BIT
, 0);
409 rtc
= devm_rtc_device_register(&spi
->dev
, "rtc-mcp795",
410 &mcp795_rtc_ops
, THIS_MODULE
);
414 spi_set_drvdata(spi
, rtc
);
417 dev_dbg(&spi
->dev
, "Alarm support enabled\n");
419 /* Clear any pending alarm (ALM0IF bit) before requesting
422 mcp795_rtcc_set_bits(&spi
->dev
, MCP795_REG_ALM0_DAY
,
423 MCP795_ALM0IF_BIT
, 0);
424 ret
= devm_request_threaded_irq(&spi
->dev
, spi
->irq
, NULL
,
425 mcp795_irq
, IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
426 dev_name(&rtc
->dev
), spi
);
428 dev_err(&spi
->dev
, "Failed to request IRQ: %d: %d\n",
431 device_init_wakeup(&spi
->dev
, true);
437 static const struct of_device_id mcp795_of_match
[] = {
438 { .compatible
= "maxim,mcp795" },
441 MODULE_DEVICE_TABLE(of
, mcp795_of_match
);
444 static struct spi_driver mcp795_driver
= {
446 .name
= "rtc-mcp795",
447 .of_match_table
= of_match_ptr(mcp795_of_match
),
449 .probe
= mcp795_probe
,
452 module_spi_driver(mcp795_driver
);
454 MODULE_DESCRIPTION("MCP795 RTC SPI Driver");
455 MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
456 MODULE_LICENSE("GPL");
457 MODULE_ALIAS("spi:mcp795");