1 /* drivers/rtc/rtc-v3020.c
3 * Copyright (C) 2006 8D Technologies inc.
4 * Copyright (C) 2004 Compulab Ltd.
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.
10 * Driver for the V3020 RTC
14 * 10-May-2006: Raphael Assenat <raph@8d.com>
15 * - Converted to platform driver
16 * - Use the generic rtc class
18 * ??-???-2004: Someone at Compulab
19 * - Initial driver creation.
22 #include <linux/platform_device.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/rtc.h>
26 #include <linux/types.h>
27 #include <linux/bcd.h>
28 #include <linux/rtc-v3020.h>
29 #include <linux/delay.h>
36 void __iomem
*ioaddress
;
38 struct rtc_device
*rtc
;
41 static void v3020_set_reg(struct v3020
*chip
, unsigned char address
,
48 for (i
= 0; i
< 4; i
++) {
49 writel((tmp
& 1) << chip
->leftshift
, chip
->ioaddress
);
54 /* Commands dont have data */
55 if (!V3020_IS_COMMAND(address
)) {
56 for (i
= 0; i
< 8; i
++) {
57 writel((data
& 1) << chip
->leftshift
, chip
->ioaddress
);
64 static unsigned char v3020_get_reg(struct v3020
*chip
, unsigned char address
)
69 for (i
= 0; i
< 4; i
++) {
70 writel((address
& 1) << chip
->leftshift
, chip
->ioaddress
);
75 for (i
= 0; i
< 8; i
++) {
77 if (readl(chip
->ioaddress
) & (1 << chip
->leftshift
))
85 static int v3020_read_time(struct device
*dev
, struct rtc_time
*dt
)
87 struct v3020
*chip
= dev_get_drvdata(dev
);
90 /* Copy the current time to ram... */
91 v3020_set_reg(chip
, V3020_CMD_CLOCK2RAM
, 0);
93 /* ...and then read constant values. */
94 tmp
= v3020_get_reg(chip
, V3020_SECONDS
);
95 dt
->tm_sec
= bcd2bin(tmp
);
96 tmp
= v3020_get_reg(chip
, V3020_MINUTES
);
97 dt
->tm_min
= bcd2bin(tmp
);
98 tmp
= v3020_get_reg(chip
, V3020_HOURS
);
99 dt
->tm_hour
= bcd2bin(tmp
);
100 tmp
= v3020_get_reg(chip
, V3020_MONTH_DAY
);
101 dt
->tm_mday
= bcd2bin(tmp
);
102 tmp
= v3020_get_reg(chip
, V3020_MONTH
);
103 dt
->tm_mon
= bcd2bin(tmp
) - 1;
104 tmp
= v3020_get_reg(chip
, V3020_WEEK_DAY
);
105 dt
->tm_wday
= bcd2bin(tmp
);
106 tmp
= v3020_get_reg(chip
, V3020_YEAR
);
107 dt
->tm_year
= bcd2bin(tmp
)+100;
110 printk("\n%s : Read RTC values\n",__func__
);
111 printk("tm_hour: %i\n",dt
->tm_hour
);
112 printk("tm_min : %i\n",dt
->tm_min
);
113 printk("tm_sec : %i\n",dt
->tm_sec
);
114 printk("tm_year: %i\n",dt
->tm_year
);
115 printk("tm_mon : %i\n",dt
->tm_mon
);
116 printk("tm_mday: %i\n",dt
->tm_mday
);
117 printk("tm_wday: %i\n",dt
->tm_wday
);
124 static int v3020_set_time(struct device
*dev
, struct rtc_time
*dt
)
126 struct v3020
*chip
= dev_get_drvdata(dev
);
129 printk("\n%s : Setting RTC values\n",__func__
);
130 printk("tm_sec : %i\n",dt
->tm_sec
);
131 printk("tm_min : %i\n",dt
->tm_min
);
132 printk("tm_hour: %i\n",dt
->tm_hour
);
133 printk("tm_mday: %i\n",dt
->tm_mday
);
134 printk("tm_wday: %i\n",dt
->tm_wday
);
135 printk("tm_year: %i\n",dt
->tm_year
);
138 /* Write all the values to ram... */
139 v3020_set_reg(chip
, V3020_SECONDS
, bin2bcd(dt
->tm_sec
));
140 v3020_set_reg(chip
, V3020_MINUTES
, bin2bcd(dt
->tm_min
));
141 v3020_set_reg(chip
, V3020_HOURS
, bin2bcd(dt
->tm_hour
));
142 v3020_set_reg(chip
, V3020_MONTH_DAY
, bin2bcd(dt
->tm_mday
));
143 v3020_set_reg(chip
, V3020_MONTH
, bin2bcd(dt
->tm_mon
+ 1));
144 v3020_set_reg(chip
, V3020_WEEK_DAY
, bin2bcd(dt
->tm_wday
));
145 v3020_set_reg(chip
, V3020_YEAR
, bin2bcd(dt
->tm_year
% 100));
147 /* ...and set the clock. */
148 v3020_set_reg(chip
, V3020_CMD_RAM2CLOCK
, 0);
150 /* Compulab used this delay here. I dont know why,
151 * the datasheet does not specify a delay. */
157 static const struct rtc_class_ops v3020_rtc_ops
= {
158 .read_time
= v3020_read_time
,
159 .set_time
= v3020_set_time
,
162 static int rtc_probe(struct platform_device
*pdev
)
164 struct v3020_platform_data
*pdata
= pdev
->dev
.platform_data
;
166 struct rtc_device
*rtc
;
171 if (pdev
->num_resources
!= 1)
174 if (pdev
->resource
[0].flags
!= IORESOURCE_MEM
)
177 chip
= kzalloc(sizeof *chip
, GFP_KERNEL
);
181 chip
->leftshift
= pdata
->leftshift
;
182 chip
->ioaddress
= ioremap(pdev
->resource
[0].start
, 1);
183 if (chip
->ioaddress
== NULL
)
186 /* Make sure the v3020 expects a communication cycle
187 * by reading 8 times */
188 for (i
= 0; i
< 8; i
++)
189 temp
= readl(chip
->ioaddress
);
191 /* Test chip by doing a write/read sequence
193 v3020_set_reg(chip
, V3020_SECONDS
, 0x33);
194 if(v3020_get_reg(chip
, V3020_SECONDS
) != 0x33) {
199 /* Make sure frequency measurment mode, test modes, and lock
200 * are all disabled */
201 v3020_set_reg(chip
, V3020_STATUS_0
, 0x0);
203 dev_info(&pdev
->dev
, "Chip available at physical address 0x%llx,"
204 "data connected to D%d\n",
205 (unsigned long long)pdev
->resource
[0].start
,
208 platform_set_drvdata(pdev
, chip
);
210 rtc
= rtc_device_register("v3020",
211 &pdev
->dev
, &v3020_rtc_ops
, THIS_MODULE
);
213 retval
= PTR_ERR(rtc
);
221 iounmap(chip
->ioaddress
);
228 static int rtc_remove(struct platform_device
*dev
)
230 struct v3020
*chip
= platform_get_drvdata(dev
);
231 struct rtc_device
*rtc
= chip
->rtc
;
234 rtc_device_unregister(rtc
);
236 iounmap(chip
->ioaddress
);
242 static struct platform_driver rtc_device_driver
= {
244 .remove
= rtc_remove
,
247 .owner
= THIS_MODULE
,
251 static __init
int v3020_init(void)
253 return platform_driver_register(&rtc_device_driver
);
256 static __exit
void v3020_exit(void)
258 platform_driver_unregister(&rtc_device_driver
);
261 module_init(v3020_init
);
262 module_exit(v3020_exit
);
264 MODULE_DESCRIPTION("V3020 RTC");
265 MODULE_AUTHOR("Raphael Assenat");
266 MODULE_LICENSE("GPL");
267 MODULE_ALIAS("platform:v3020");