Blame view

drivers/rtc/rtc-v3020.c 6.07 KB
362600fe6   Raphael Assenat   [PATCH] Add v3020...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /* drivers/rtc/rtc-v3020.c
   *
   * Copyright (C) 2006 8D Technologies inc.
   * Copyright (C) 2004 Compulab Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * Driver for the V3020 RTC
   *
   * Changelog:
   *
   *  10-May-2006: Raphael Assenat <raph@8d.com>
   *				- Converted to platform driver
   *				- Use the generic rtc class
   *
   *  ??-???-2004: Someone at Compulab
   *  			- Initial driver creation.
   *
   */
  #include <linux/platform_device.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/rtc.h>
  #include <linux/types.h>
  #include <linux/bcd.h>
  #include <linux/rtc-v3020.h>
f3d79b20d   Mike Rapoport   RTC v3020 fixes
29
  #include <linux/delay.h>
362600fe6   Raphael Assenat   [PATCH] Add v3020...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
  #include <asm/io.h>
  
  #undef DEBUG
  
  struct v3020 {
  	void __iomem *ioaddress;
  	int leftshift;
  	struct rtc_device *rtc;
  };
  
  static void v3020_set_reg(struct v3020 *chip, unsigned char address,
  			unsigned char data)
  {
  	int i;
  	unsigned char tmp;
  
  	tmp = address;
  	for (i = 0; i < 4; i++) {
  		writel((tmp & 1) << chip->leftshift, chip->ioaddress);
  		tmp >>= 1;
f3d79b20d   Mike Rapoport   RTC v3020 fixes
51
  		udelay(1);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
52
53
54
55
56
57
58
  	}
  
  	/* Commands dont have data */
  	if (!V3020_IS_COMMAND(address)) {
  		for (i = 0; i < 8; i++) {
  			writel((data & 1) << chip->leftshift, chip->ioaddress);
  			data >>= 1;
f3d79b20d   Mike Rapoport   RTC v3020 fixes
59
  			udelay(1);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
60
61
62
63
64
65
66
67
68
69
70
71
  		}
  	}
  }
  
  static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
  {
  	unsigned int data=0;
  	int i;
  
  	for (i = 0; i < 4; i++) {
  		writel((address & 1) << chip->leftshift, chip->ioaddress);
  		address >>= 1;
f3d79b20d   Mike Rapoport   RTC v3020 fixes
72
  		udelay(1);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
73
74
75
76
77
78
  	}
  
  	for (i = 0; i < 8; i++) {
  		data >>= 1;
  		if (readl(chip->ioaddress) & (1 << chip->leftshift))
  			data |= 0x80;
f3d79b20d   Mike Rapoport   RTC v3020 fixes
79
  		udelay(1);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  	}
  
  	return data;
  }
  
  static int v3020_read_time(struct device *dev, struct rtc_time *dt)
  {
  	struct v3020 *chip = dev_get_drvdata(dev);
  	int tmp;
  
  	/* Copy the current time to ram... */
  	v3020_set_reg(chip, V3020_CMD_CLOCK2RAM, 0);
  
  	/* ...and then read constant values. */
  	tmp = v3020_get_reg(chip, V3020_SECONDS);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
95
  	dt->tm_sec	= bcd2bin(tmp);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
96
  	tmp = v3020_get_reg(chip, V3020_MINUTES);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
97
  	dt->tm_min	= bcd2bin(tmp);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
98
  	tmp = v3020_get_reg(chip, V3020_HOURS);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
99
  	dt->tm_hour	= bcd2bin(tmp);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
100
  	tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
101
  	dt->tm_mday	= bcd2bin(tmp);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
102
  	tmp = v3020_get_reg(chip, V3020_MONTH);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
103
  	dt->tm_mon    = bcd2bin(tmp) - 1;
362600fe6   Raphael Assenat   [PATCH] Add v3020...
104
  	tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
105
  	dt->tm_wday	= bcd2bin(tmp);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
106
  	tmp = v3020_get_reg(chip, V3020_YEAR);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
107
  	dt->tm_year = bcd2bin(tmp)+100;
362600fe6   Raphael Assenat   [PATCH] Add v3020...
108
109
  
  #ifdef DEBUG
2a4e2b878   Harvey Harrison   rtc: replace rema...
110
111
112
  	printk("
  %s : Read RTC values
  ",__func__);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  	printk("tm_hour: %i
  ",dt->tm_hour);
  	printk("tm_min : %i
  ",dt->tm_min);
  	printk("tm_sec : %i
  ",dt->tm_sec);
  	printk("tm_year: %i
  ",dt->tm_year);
  	printk("tm_mon : %i
  ",dt->tm_mon);
  	printk("tm_mday: %i
  ",dt->tm_mday);
  	printk("tm_wday: %i
  ",dt->tm_wday);
  #endif
  
  	return 0;
  }
  
  
  static int v3020_set_time(struct device *dev, struct rtc_time *dt)
  {
  	struct v3020 *chip = dev_get_drvdata(dev);
  
  #ifdef DEBUG
2a4e2b878   Harvey Harrison   rtc: replace rema...
138
139
140
  	printk("
  %s : Setting RTC values
  ",__func__);
362600fe6   Raphael Assenat   [PATCH] Add v3020...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  	printk("tm_sec : %i
  ",dt->tm_sec);
  	printk("tm_min : %i
  ",dt->tm_min);
  	printk("tm_hour: %i
  ",dt->tm_hour);
  	printk("tm_mday: %i
  ",dt->tm_mday);
  	printk("tm_wday: %i
  ",dt->tm_wday);
  	printk("tm_year: %i
  ",dt->tm_year);
  #endif
  
  	/* Write all the values to ram... */
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
156
157
158
159
160
161
162
  	v3020_set_reg(chip, V3020_SECONDS, 	bin2bcd(dt->tm_sec));
  	v3020_set_reg(chip, V3020_MINUTES, 	bin2bcd(dt->tm_min));
  	v3020_set_reg(chip, V3020_HOURS, 	bin2bcd(dt->tm_hour));
  	v3020_set_reg(chip, V3020_MONTH_DAY,	bin2bcd(dt->tm_mday));
  	v3020_set_reg(chip, V3020_MONTH,     bin2bcd(dt->tm_mon + 1));
  	v3020_set_reg(chip, V3020_WEEK_DAY, 	bin2bcd(dt->tm_wday));
  	v3020_set_reg(chip, V3020_YEAR, 	bin2bcd(dt->tm_year % 100));
362600fe6   Raphael Assenat   [PATCH] Add v3020...
163
164
165
166
167
168
169
170
171
172
  
  	/* ...and set the clock. */
  	v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);
  
  	/* Compulab used this delay here. I dont know why,
  	 * the datasheet does not specify a delay. */
  	/*mdelay(5);*/
  
  	return 0;
  }
ff8371ac9   David Brownell   [PATCH] constify ...
173
  static const struct rtc_class_ops v3020_rtc_ops = {
362600fe6   Raphael Assenat   [PATCH] Add v3020...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  	.read_time	= v3020_read_time,
  	.set_time	= v3020_set_time,
  };
  
  static int rtc_probe(struct platform_device *pdev)
  {
  	struct v3020_platform_data *pdata = pdev->dev.platform_data;
  	struct v3020 *chip;
  	struct rtc_device *rtc;
  	int retval = -EBUSY;
  	int i;
  	int temp;
  
  	if (pdev->num_resources != 1)
  		return -EBUSY;
  
  	if (pdev->resource[0].flags != IORESOURCE_MEM)
  		return -EBUSY;
362600fe6   Raphael Assenat   [PATCH] Add v3020...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  	chip = kzalloc(sizeof *chip, GFP_KERNEL);
  	if (!chip)
  		return -ENOMEM;
  
  	chip->leftshift = pdata->leftshift;
  	chip->ioaddress = ioremap(pdev->resource[0].start, 1);
  	if (chip->ioaddress == NULL)
  		goto err_chip;
  
  	/* Make sure the v3020 expects a communication cycle
  	 * by reading 8 times */
  	for (i = 0; i < 8; i++)
  		temp = readl(chip->ioaddress);
  
  	/* Test chip by doing a write/read sequence
  	 * to the chip ram */
  	v3020_set_reg(chip, V3020_SECONDS, 0x33);
  	if(v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
  		retval = -ENODEV;
  		goto err_io;
  	}
  
  	/* Make sure frequency measurment mode, test modes, and lock
  	 * are all disabled */
  	v3020_set_reg(chip, V3020_STATUS_0, 0x0);
6a15f46c1   Jeff Garzik   [PATCH] rtc: fix ...
217
  	dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
362600fe6   Raphael Assenat   [PATCH] Add v3020...
218
219
  		"data connected to D%d
  ",
6a15f46c1   Jeff Garzik   [PATCH] rtc: fix ...
220
  		(unsigned long long)pdev->resource[0].start,
362600fe6   Raphael Assenat   [PATCH] Add v3020...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  		chip->leftshift);
  
  	platform_set_drvdata(pdev, chip);
  
  	rtc = rtc_device_register("v3020",
  				&pdev->dev, &v3020_rtc_ops, THIS_MODULE);
  	if (IS_ERR(rtc)) {
  		retval = PTR_ERR(rtc);
  		goto err_io;
  	}
  	chip->rtc = rtc;
  
  	return 0;
  
  err_io:
  	iounmap(chip->ioaddress);
  err_chip:
  	kfree(chip);
  
  	return retval;
  }
  
  static int rtc_remove(struct platform_device *dev)
  {
  	struct v3020 *chip = platform_get_drvdata(dev);
  	struct rtc_device *rtc = chip->rtc;
  
  	if (rtc)
  		rtc_device_unregister(rtc);
  
  	iounmap(chip->ioaddress);
  	kfree(chip);
  
  	return 0;
  }
  
  static struct platform_driver rtc_device_driver = {
  	.probe	= rtc_probe,
  	.remove = rtc_remove,
  	.driver = {
  		.name	= "v3020",
  		.owner	= THIS_MODULE,
  	},
  };
  
  static __init int v3020_init(void)
  {
  	return platform_driver_register(&rtc_device_driver);
  }
  
  static __exit void v3020_exit(void)
  {
  	platform_driver_unregister(&rtc_device_driver);
  }
  
  module_init(v3020_init);
  module_exit(v3020_exit);
  
  MODULE_DESCRIPTION("V3020 RTC");
  MODULE_AUTHOR("Raphael Assenat");
  MODULE_LICENSE("GPL");
ad28a07bc   Kay Sievers   rtc: fix platform...
282
  MODULE_ALIAS("platform:v3020");