diff options
author | Josef Gajdusek <atx@atx.name> | 2014-06-06 17:36:02 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-06-06 19:08:08 -0400 |
commit | 1fcbe42c1b763456b56d492c31e06e27547b0481 (patch) | |
tree | 62173a94b0df43a44fb2355dca8c1b5b518e76d4 /drivers/rtc | |
parent | 10b06b87b77e9fcd7d1282f8cacf9736fb5143b3 (diff) |
drivers/rtc: add support for Microchip MCP795
Add driver for SPI RTC Microchip MCP795. Only supports saving/loading
time from the chip (i. e. no alarms/power events/ID).
Signed-off-by: Josef Gajdusek <atx@atx.name>
Cc: Jingoo Han <jg1.han@samsung.com>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/rtc')
-rw-r--r-- | drivers/rtc/Kconfig | 8 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-mcp795.c | 199 |
3 files changed, 208 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index a27958de11ff..486d34b225d5 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -661,6 +661,14 @@ config RTC_DRV_RX4581 | |||
661 | This driver can also be built as a module. If so the module | 661 | This driver can also be built as a module. If so the module |
662 | will be called rtc-rx4581. | 662 | will be called rtc-rx4581. |
663 | 663 | ||
664 | config RTC_DRV_MCP795 | ||
665 | tristate "Microchip MCP795" | ||
666 | help | ||
667 | If you say yes here you will get support for the Microchip MCP795. | ||
668 | |||
669 | This driver can also be built as a module. If so the module | ||
670 | will be called rtc-mcp795. | ||
671 | |||
664 | endif # SPI_MASTER | 672 | endif # SPI_MASTER |
665 | 673 | ||
666 | comment "Platform RTC drivers" | 674 | comment "Platform RTC drivers" |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 660ab296b956..9d7775534a3d 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -81,6 +81,7 @@ obj-$(CONFIG_RTC_DRV_MAX8997) += rtc-max8997.o | |||
81 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o | 81 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o |
82 | obj-$(CONFIG_RTC_DRV_MAX77686) += rtc-max77686.o | 82 | obj-$(CONFIG_RTC_DRV_MAX77686) += rtc-max77686.o |
83 | obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o | 83 | obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o |
84 | obj-$(CONFIG_RTC_DRV_MCP795) += rtc-mcp795.o | ||
84 | obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o | 85 | obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o |
85 | obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o | 86 | obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o |
86 | obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o | 87 | obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o |
diff --git a/drivers/rtc/rtc-mcp795.c b/drivers/rtc/rtc-mcp795.c new file mode 100644 index 000000000000..34295bf00416 --- /dev/null +++ b/drivers/rtc/rtc-mcp795.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | * SPI Driver for Microchip MCP795 RTC | ||
3 | * | ||
4 | * Copyright (C) Josef Gajdusek <atx@atx.name> | ||
5 | * | ||
6 | * based on other Linux RTC drivers | ||
7 | * | ||
8 | * Device datasheet: | ||
9 | * http://ww1.microchip.com/downloads/en/DeviceDoc/22280A.pdf | ||
10 | * | ||
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. | ||
14 | * | ||
15 | * */ | ||
16 | |||
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> | ||
23 | |||
24 | /* MCP795 Instructions, see datasheet table 3-1 */ | ||
25 | #define MCP795_EEREAD 0x03 | ||
26 | #define MCP795_EEWRITE 0x02 | ||
27 | #define MCP795_EEWRDI 0x04 | ||
28 | #define MCP795_EEWREN 0x06 | ||
29 | #define MCP795_SRREAD 0x05 | ||
30 | #define MCP795_SRWRITE 0x01 | ||
31 | #define MCP795_READ 0x13 | ||
32 | #define MCP795_WRITE 0x12 | ||
33 | #define MCP795_UNLOCK 0x14 | ||
34 | #define MCP795_IDWRITE 0x32 | ||
35 | #define MCP795_IDREAD 0x33 | ||
36 | #define MCP795_CLRWDT 0x44 | ||
37 | #define MCP795_CLRRAM 0x54 | ||
38 | |||
39 | #define MCP795_ST_BIT 0x80 | ||
40 | #define MCP795_24_BIT 0x40 | ||
41 | |||
42 | static int mcp795_rtcc_read(struct device *dev, u8 addr, u8 *buf, u8 count) | ||
43 | { | ||
44 | struct spi_device *spi = to_spi_device(dev); | ||
45 | int ret; | ||
46 | u8 tx[2]; | ||
47 | |||
48 | tx[0] = MCP795_READ; | ||
49 | tx[1] = addr; | ||
50 | ret = spi_write_then_read(spi, tx, sizeof(tx), buf, count); | ||
51 | |||
52 | if (ret) | ||
53 | dev_err(dev, "Failed reading %d bytes from address %x.\n", | ||
54 | count, addr); | ||
55 | |||
56 | return ret; | ||
57 | } | ||
58 | |||
59 | static int mcp795_rtcc_write(struct device *dev, u8 addr, u8 *data, u8 count) | ||
60 | { | ||
61 | struct spi_device *spi = to_spi_device(dev); | ||
62 | int ret; | ||
63 | u8 tx[2 + count]; | ||
64 | |||
65 | tx[0] = MCP795_WRITE; | ||
66 | tx[1] = addr; | ||
67 | memcpy(&tx[2], data, count); | ||
68 | |||
69 | ret = spi_write(spi, tx, 2 + count); | ||
70 | |||
71 | if (ret) | ||
72 | dev_err(dev, "Failed to write %d bytes to address %x.\n", | ||
73 | count, addr); | ||
74 | |||
75 | return ret; | ||
76 | } | ||
77 | |||
78 | static int mcp795_rtcc_set_bits(struct device *dev, u8 addr, u8 mask, u8 state) | ||
79 | { | ||
80 | int ret; | ||
81 | u8 tmp; | ||
82 | |||
83 | ret = mcp795_rtcc_read(dev, addr, &tmp, 1); | ||
84 | if (ret) | ||
85 | return ret; | ||
86 | |||
87 | if ((tmp & mask) != state) { | ||
88 | tmp = (tmp & ~mask) | state; | ||
89 | ret = mcp795_rtcc_write(dev, addr, &tmp, 1); | ||
90 | } | ||
91 | |||
92 | return ret; | ||
93 | } | ||
94 | |||
95 | static int mcp795_set_time(struct device *dev, struct rtc_time *tim) | ||
96 | { | ||
97 | int ret; | ||
98 | u8 data[7]; | ||
99 | |||
100 | /* Read first, so we can leave config bits untouched */ | ||
101 | ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data)); | ||
102 | |||
103 | if (ret) | ||
104 | return ret; | ||
105 | |||
106 | data[0] = (data[0] & 0x80) | ((tim->tm_sec / 10) << 4) | (tim->tm_sec % 10); | ||
107 | data[1] = (data[1] & 0x80) | ((tim->tm_min / 10) << 4) | (tim->tm_min % 10); | ||
108 | data[2] = ((tim->tm_hour / 10) << 4) | (tim->tm_hour % 10); | ||
109 | data[4] = ((tim->tm_mday / 10) << 4) | ((tim->tm_mday) % 10); | ||
110 | data[5] = (data[5] & 0x10) | (tim->tm_mon / 10) | (tim->tm_mon % 10); | ||
111 | |||
112 | if (tim->tm_year > 100) | ||
113 | tim->tm_year -= 100; | ||
114 | |||
115 | data[6] = ((tim->tm_year / 10) << 4) | (tim->tm_year % 10); | ||
116 | |||
117 | ret = mcp795_rtcc_write(dev, 0x01, data, sizeof(data)); | ||
118 | |||
119 | if (ret) | ||
120 | return ret; | ||
121 | |||
122 | dev_dbg(dev, "Set mcp795: %04d-%02d-%02d %02d:%02d:%02d\n", | ||
123 | tim->tm_year + 1900, tim->tm_mon, tim->tm_mday, | ||
124 | tim->tm_hour, tim->tm_min, tim->tm_sec); | ||
125 | |||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | static int mcp795_read_time(struct device *dev, struct rtc_time *tim) | ||
130 | { | ||
131 | int ret; | ||
132 | u8 data[7]; | ||
133 | |||
134 | ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data)); | ||
135 | |||
136 | if (ret) | ||
137 | return ret; | ||
138 | |||
139 | tim->tm_sec = ((data[0] & 0x70) >> 4) * 10 + (data[0] & 0x0f); | ||
140 | tim->tm_min = ((data[1] & 0x70) >> 4) * 10 + (data[1] & 0x0f); | ||
141 | tim->tm_hour = ((data[2] & 0x30) >> 4) * 10 + (data[2] & 0x0f); | ||
142 | tim->tm_mday = ((data[4] & 0x30) >> 4) * 10 + (data[4] & 0x0f); | ||
143 | tim->tm_mon = ((data[5] & 0x10) >> 4) * 10 + (data[5] & 0x0f); | ||
144 | tim->tm_year = ((data[6] & 0xf0) >> 4) * 10 + (data[6] & 0x0f) + 100; /* Assume we are in 20xx */ | ||
145 | |||
146 | dev_dbg(dev, "Read from mcp795: %04d-%02d-%02d %02d:%02d:%02d\n", | ||
147 | tim->tm_year + 1900, tim->tm_mon, tim->tm_mday, | ||
148 | tim->tm_hour, tim->tm_min, tim->tm_sec); | ||
149 | |||
150 | return rtc_valid_tm(tim); | ||
151 | } | ||
152 | |||
153 | static struct rtc_class_ops mcp795_rtc_ops = { | ||
154 | .read_time = mcp795_read_time, | ||
155 | .set_time = mcp795_set_time | ||
156 | }; | ||
157 | |||
158 | static int mcp795_probe(struct spi_device *spi) | ||
159 | { | ||
160 | struct rtc_device *rtc; | ||
161 | int ret; | ||
162 | |||
163 | spi->mode = SPI_MODE_0; | ||
164 | spi->bits_per_word = 8; | ||
165 | ret = spi_setup(spi); | ||
166 | if (ret) { | ||
167 | dev_err(&spi->dev, "Unable to setup SPI\n"); | ||
168 | return ret; | ||
169 | } | ||
170 | |||
171 | /* Start the oscillator */ | ||
172 | mcp795_rtcc_set_bits(&spi->dev, 0x01, MCP795_ST_BIT, MCP795_ST_BIT); | ||
173 | /* Clear the 12 hour mode flag*/ | ||
174 | mcp795_rtcc_set_bits(&spi->dev, 0x03, MCP795_24_BIT, 0); | ||
175 | |||
176 | rtc = devm_rtc_device_register(&spi->dev, "rtc-mcp795", | ||
177 | &mcp795_rtc_ops, THIS_MODULE); | ||
178 | if (IS_ERR(rtc)) | ||
179 | return PTR_ERR(rtc); | ||
180 | |||
181 | spi_set_drvdata(spi, rtc); | ||
182 | |||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | static struct spi_driver mcp795_driver = { | ||
187 | .driver = { | ||
188 | .name = "rtc-mcp795", | ||
189 | .owner = THIS_MODULE, | ||
190 | }, | ||
191 | .probe = mcp795_probe, | ||
192 | }; | ||
193 | |||
194 | module_spi_driver(mcp795_driver); | ||
195 | |||
196 | MODULE_DESCRIPTION("MCP795 RTC SPI Driver"); | ||
197 | MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>"); | ||
198 | MODULE_LICENSE("GPL"); | ||
199 | MODULE_ALIAS("spi:mcp795"); | ||