blob: 1f0eb79e69f90e279c33be8efc771a357c7ede91 [file] [log] [blame]
Kim B. Heino8fc2c762008-07-23 21:30:34 -07001/*
2 * Driver for ST M41T94 SPI RTC
3 *
4 * Copyright (C) 2008 Kim B. Heino
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/rtc.h>
15#include <linux/spi/spi.h>
16#include <linux/bcd.h>
17
18#define M41T94_REG_SECONDS 0x01
19#define M41T94_REG_MINUTES 0x02
20#define M41T94_REG_HOURS 0x03
21#define M41T94_REG_WDAY 0x04
22#define M41T94_REG_DAY 0x05
23#define M41T94_REG_MONTH 0x06
24#define M41T94_REG_YEAR 0x07
25#define M41T94_REG_HT 0x0c
26
27#define M41T94_BIT_HALT 0x40
28#define M41T94_BIT_STOP 0x80
29#define M41T94_BIT_CB 0x40
30#define M41T94_BIT_CEB 0x80
31
32static int m41t94_set_time(struct device *dev, struct rtc_time *tm)
33{
34 struct spi_device *spi = to_spi_device(dev);
35 u8 buf[8]; /* write cmd + 7 registers */
36
37 dev_dbg(dev, "%s secs=%d, mins=%d, "
38 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
39 "write", tm->tm_sec, tm->tm_min,
40 tm->tm_hour, tm->tm_mday,
41 tm->tm_mon, tm->tm_year, tm->tm_wday);
42
43 buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */
Adrian Bunkfe20ba72008-10-18 20:28:41 -070044 buf[M41T94_REG_SECONDS] = bin2bcd(tm->tm_sec);
45 buf[M41T94_REG_MINUTES] = bin2bcd(tm->tm_min);
46 buf[M41T94_REG_HOURS] = bin2bcd(tm->tm_hour);
47 buf[M41T94_REG_WDAY] = bin2bcd(tm->tm_wday + 1);
48 buf[M41T94_REG_DAY] = bin2bcd(tm->tm_mday);
49 buf[M41T94_REG_MONTH] = bin2bcd(tm->tm_mon + 1);
Kim B. Heino8fc2c762008-07-23 21:30:34 -070050
51 buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB;
52 if (tm->tm_year >= 100)
53 buf[M41T94_REG_HOURS] |= M41T94_BIT_CB;
Adrian Bunkfe20ba72008-10-18 20:28:41 -070054 buf[M41T94_REG_YEAR] = bin2bcd(tm->tm_year % 100);
Kim B. Heino8fc2c762008-07-23 21:30:34 -070055
56 return spi_write(spi, buf, 8);
57}
58
59static int m41t94_read_time(struct device *dev, struct rtc_time *tm)
60{
61 struct spi_device *spi = to_spi_device(dev);
62 u8 buf[2];
63 int ret, hour;
64
65 /* clear halt update bit */
66 ret = spi_w8r8(spi, M41T94_REG_HT);
67 if (ret < 0)
68 return ret;
69 if (ret & M41T94_BIT_HALT) {
70 buf[0] = 0x80 | M41T94_REG_HT;
71 buf[1] = ret & ~M41T94_BIT_HALT;
72 spi_write(spi, buf, 2);
73 }
74
75 /* clear stop bit */
76 ret = spi_w8r8(spi, M41T94_REG_SECONDS);
77 if (ret < 0)
78 return ret;
79 if (ret & M41T94_BIT_STOP) {
80 buf[0] = 0x80 | M41T94_REG_SECONDS;
81 buf[1] = ret & ~M41T94_BIT_STOP;
82 spi_write(spi, buf, 2);
83 }
84
Adrian Bunkfe20ba72008-10-18 20:28:41 -070085 tm->tm_sec = bcd2bin(spi_w8r8(spi, M41T94_REG_SECONDS));
86 tm->tm_min = bcd2bin(spi_w8r8(spi, M41T94_REG_MINUTES));
Kim B. Heino8fc2c762008-07-23 21:30:34 -070087 hour = spi_w8r8(spi, M41T94_REG_HOURS);
Adrian Bunkfe20ba72008-10-18 20:28:41 -070088 tm->tm_hour = bcd2bin(hour & 0x3f);
89 tm->tm_wday = bcd2bin(spi_w8r8(spi, M41T94_REG_WDAY)) - 1;
90 tm->tm_mday = bcd2bin(spi_w8r8(spi, M41T94_REG_DAY));
91 tm->tm_mon = bcd2bin(spi_w8r8(spi, M41T94_REG_MONTH)) - 1;
92 tm->tm_year = bcd2bin(spi_w8r8(spi, M41T94_REG_YEAR));
Kim B. Heino8fc2c762008-07-23 21:30:34 -070093 if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB))
94 tm->tm_year += 100;
95
96 dev_dbg(dev, "%s secs=%d, mins=%d, "
97 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
98 "read", tm->tm_sec, tm->tm_min,
99 tm->tm_hour, tm->tm_mday,
100 tm->tm_mon, tm->tm_year, tm->tm_wday);
101
102 /* initial clock setting can be undefined */
103 return rtc_valid_tm(tm);
104}
105
106static const struct rtc_class_ops m41t94_rtc_ops = {
107 .read_time = m41t94_read_time,
108 .set_time = m41t94_set_time,
109};
110
111static struct spi_driver m41t94_driver;
112
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800113static int m41t94_probe(struct spi_device *spi)
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700114{
115 struct rtc_device *rtc;
116 int res;
117
118 spi->bits_per_word = 8;
119 spi_setup(spi);
120
121 res = spi_w8r8(spi, M41T94_REG_SECONDS);
122 if (res < 0) {
123 dev_err(&spi->dev, "not found.\n");
124 return res;
125 }
126
Jingoo Hanfb320d02013-04-29 16:19:41 -0700127 rtc = devm_rtc_device_register(&spi->dev, m41t94_driver.driver.name,
128 &m41t94_rtc_ops, THIS_MODULE);
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700129 if (IS_ERR(rtc))
130 return PTR_ERR(rtc);
131
Jingoo Hanee624742013-04-29 16:20:18 -0700132 spi_set_drvdata(spi, rtc);
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700133
134 return 0;
135}
136
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700137static struct spi_driver m41t94_driver = {
138 .driver = {
139 .name = "rtc-m41t94",
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700140 },
141 .probe = m41t94_probe,
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700142};
143
Axel Lin109e9412012-03-23 15:02:30 -0700144module_spi_driver(m41t94_driver);
Kim B. Heino8fc2c762008-07-23 21:30:34 -0700145
146MODULE_AUTHOR("Kim B. Heino <Kim.Heino@bluegiga.com>");
147MODULE_DESCRIPTION("Driver for ST M41T94 SPI RTC");
148MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700149MODULE_ALIAS("spi:rtc-m41t94");