2 * SPI Driver for Microchip MCP795 RTC
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: %ptR\n", tim);
241 static int mcp795_read_time(struct device *dev, struct rtc_time *tim)
246 ret = mcp795_rtcc_read(dev, MCP795_REG_SECONDS, data, sizeof(data));
251 tim->tm_sec = bcd2bin(data[0] & 0x7F);
252 tim->tm_min = bcd2bin(data[1] & 0x7F);
253 tim->tm_hour = bcd2bin(data[2] & 0x3F);
254 tim->tm_wday = bcd2bin(data[3] & 0x07) - 1;
255 tim->tm_mday = bcd2bin(data[4] & 0x3F);
256 tim->tm_mon = bcd2bin(data[5] & 0x1F) - 1;
257 tim->tm_year = bcd2bin(data[6]) + 100; /* Assume we are in 20xx */
259 dev_dbg(dev, "Read from mcp795: %ptR\n", tim);
264 static int mcp795_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
266 struct rtc_time now_tm;
272 /* Read current time from RTC hardware */
273 ret = mcp795_read_time(dev, &now_tm);
276 /* Get the number of seconds since 1970 */
277 now = rtc_tm_to_time64(&now_tm);
278 later = rtc_tm_to_time64(&alm->time);
281 /* make sure alarm fires within the next one year */
283 (SEC_PER_DAY * (365 + is_leap_year(alm->time.tm_year))))
286 ret = mcp795_update_alarm(dev, false);
289 /* Read registers, so we can leave configuration bits untouched */
290 ret = mcp795_rtcc_read(dev, MCP795_REG_ALM0_SECONDS, tmp, sizeof(tmp));
294 alm->time.tm_year = -1;
295 alm->time.tm_isdst = -1;
296 alm->time.tm_yday = -1;
298 tmp[0] = (tmp[0] & 0x80) | bin2bcd(alm->time.tm_sec);
299 tmp[1] = (tmp[1] & 0x80) | bin2bcd(alm->time.tm_min);
300 tmp[2] = (tmp[2] & 0xE0) | bin2bcd(alm->time.tm_hour);
301 tmp[3] = (tmp[3] & 0x80) | bin2bcd(alm->time.tm_wday + 1);
302 /* set alarm match: seconds, minutes, hour, day, date and month */
303 tmp[3] |= (MCP795_ALM0C2_BIT | MCP795_ALM0C1_BIT | MCP795_ALM0C0_BIT);
304 tmp[4] = (tmp[4] & 0xC0) | bin2bcd(alm->time.tm_mday);
305 tmp[5] = (tmp[5] & 0xE0) | bin2bcd(alm->time.tm_mon + 1);
307 ret = mcp795_rtcc_write(dev, MCP795_REG_ALM0_SECONDS, tmp, sizeof(tmp));
311 /* enable alarm if requested */
313 ret = mcp795_update_alarm(dev, true);
316 dev_dbg(dev, "Alarm IRQ armed\n");
318 dev_dbg(dev, "Set alarm: %ptRdr(%d) %ptRt\n",
319 &alm->time, alm->time.tm_wday, &alm->time);
323 static int mcp795_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
328 ret = mcp795_rtcc_read(
329 dev, MCP795_REG_ALM0_SECONDS, data, sizeof(data));
333 alm->time.tm_sec = bcd2bin(data[0] & 0x7F);
334 alm->time.tm_min = bcd2bin(data[1] & 0x7F);
335 alm->time.tm_hour = bcd2bin(data[2] & 0x1F);
336 alm->time.tm_wday = bcd2bin(data[3] & 0x07) - 1;
337 alm->time.tm_mday = bcd2bin(data[4] & 0x3F);
338 alm->time.tm_mon = bcd2bin(data[5] & 0x1F) - 1;
339 alm->time.tm_year = -1;
340 alm->time.tm_isdst = -1;
341 alm->time.tm_yday = -1;
343 dev_dbg(dev, "Read alarm: %ptRdr(%d) %ptRt\n",
344 &alm->time, alm->time.tm_wday, &alm->time);
348 static int mcp795_alarm_irq_enable(struct device *dev, unsigned int enabled)
350 return mcp795_update_alarm(dev, !!enabled);
353 static irqreturn_t mcp795_irq(int irq, void *data)
355 struct spi_device *spi = data;
356 struct rtc_device *rtc = spi_get_drvdata(spi);
357 struct mutex *lock = &rtc->ops_lock;
363 * There is no need to clear ALM0IF (Alarm 0 Interrupt Flag) bit,
364 * because it is done every time when alarm is enabled.
366 ret = mcp795_update_alarm(&spi->dev, false);
369 "Failed to disable alarm in IRQ (ret=%d)\n", ret);
370 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
377 static const struct rtc_class_ops mcp795_rtc_ops = {
378 .read_time = mcp795_read_time,
379 .set_time = mcp795_set_time,
380 .read_alarm = mcp795_read_alarm,
381 .set_alarm = mcp795_set_alarm,
382 .alarm_irq_enable = mcp795_alarm_irq_enable
385 static int mcp795_probe(struct spi_device *spi)
387 struct rtc_device *rtc;
390 spi->mode = SPI_MODE_0;
391 spi->bits_per_word = 8;
392 ret = spi_setup(spi);
394 dev_err(&spi->dev, "Unable to setup SPI\n");
398 /* Start the oscillator but don't set the value of EXTOSC bit */
399 mcp795_start_oscillator(&spi->dev, NULL);
400 /* Clear the 12 hour mode flag*/
401 mcp795_rtcc_set_bits(&spi->dev, 0x03, MCP795_24_BIT, 0);
403 rtc = devm_rtc_device_register(&spi->dev, "rtc-mcp795",
404 &mcp795_rtc_ops, THIS_MODULE);
408 spi_set_drvdata(spi, rtc);
411 dev_dbg(&spi->dev, "Alarm support enabled\n");
413 /* Clear any pending alarm (ALM0IF bit) before requesting
416 mcp795_rtcc_set_bits(&spi->dev, MCP795_REG_ALM0_DAY,
417 MCP795_ALM0IF_BIT, 0);
418 ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
419 mcp795_irq, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
420 dev_name(&rtc->dev), spi);
422 dev_err(&spi->dev, "Failed to request IRQ: %d: %d\n",
425 device_init_wakeup(&spi->dev, true);
431 static const struct of_device_id mcp795_of_match[] = {
432 { .compatible = "maxim,mcp795" },
435 MODULE_DEVICE_TABLE(of, mcp795_of_match);
438 static struct spi_driver mcp795_driver = {
440 .name = "rtc-mcp795",
441 .of_match_table = of_match_ptr(mcp795_of_match),
443 .probe = mcp795_probe,
446 module_spi_driver(mcp795_driver);
448 MODULE_DESCRIPTION("MCP795 RTC SPI Driver");
450 MODULE_LICENSE("GPL");
451 MODULE_ALIAS("spi:mcp795");