Blame view

drivers/rtc/rtc-max6902.c 4.16 KB
f30c22695   Uwe Zeisberger   fix file specific...
1
  /* drivers/rtc/rtc-max6902.c
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
2
3
4
5
6
7
8
9
10
11
   *
   * 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 MAX6902 spi RTC
   *
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
12
   */
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
13
  #include <linux/module.h>
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
14
15
16
17
18
19
  #include <linux/kernel.h>
  #include <linux/platform_device.h>
  #include <linux/init.h>
  #include <linux/rtc.h>
  #include <linux/spi/spi.h>
  #include <linux/bcd.h>
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
20
21
22
23
24
25
26
27
28
29
  
  #define MAX6902_REG_SECONDS		0x01
  #define MAX6902_REG_MINUTES		0x03
  #define MAX6902_REG_HOURS		0x05
  #define MAX6902_REG_DATE		0x07
  #define MAX6902_REG_MONTH		0x09
  #define MAX6902_REG_DAY			0x0B
  #define MAX6902_REG_YEAR		0x0D
  #define MAX6902_REG_CONTROL		0x0F
  #define MAX6902_REG_CENTURY		0x13
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
30
  static int max6902_set_reg(struct device *dev, unsigned char address,
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
31
32
33
34
35
36
37
38
  				unsigned char data)
  {
  	struct spi_device *spi = to_spi_device(dev);
  	unsigned char buf[2];
  
  	/* MSB must be '0' to write */
  	buf[0] = address & 0x7f;
  	buf[1] = data;
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
39
  	return spi_write_then_read(spi, buf, 2, NULL, 0);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
40
41
42
43
44
45
  }
  
  static int max6902_get_reg(struct device *dev, unsigned char address,
  				unsigned char *data)
  {
  	struct spi_device *spi = to_spi_device(dev);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
46
47
  
  	/* Set MSB to indicate read */
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
48
  	*data = address | 0x80;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
49

a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
50
  	return spi_write_then_read(spi, data, 1, data, 1);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
51
  }
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
52
  static int max6902_read_time(struct device *dev, struct rtc_time *dt)
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
53
  {
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
54
  	int err, century;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
55
  	struct spi_device *spi = to_spi_device(dev);
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
56
  	unsigned char buf[8];
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
57

a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
58
  	buf[0] = 0xbf;	/* Burst read */
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
59

a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
60
61
62
  	err = spi_write_then_read(spi, buf, 1, buf, 8);
  	if (err != 0)
  		return err;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
63
64
65
  
  	/* The chip sends data in this order:
  	 * Seconds, Minutes, Hours, Date, Month, Day, Year */
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
66
67
68
69
70
71
72
73
74
75
76
77
  	dt->tm_sec	= bcd2bin(buf[0]);
  	dt->tm_min	= bcd2bin(buf[1]);
  	dt->tm_hour	= bcd2bin(buf[2]);
  	dt->tm_mday	= bcd2bin(buf[3]);
  	dt->tm_mon	= bcd2bin(buf[4]) - 1;
  	dt->tm_wday	= bcd2bin(buf[5]);
  	dt->tm_year	= bcd2bin(buf[6]);
  
  	/* Read century */
  	err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
  	if (err != 0)
  		return err;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
78

a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
79
  	century = bcd2bin(buf[0]) * 100;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
80
81
82
  
  	dt->tm_year += century;
  	dt->tm_year -= 1900;
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
83
  	return rtc_valid_tm(dt);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
84
  }
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
85
  static int max6902_set_time(struct device *dev, struct rtc_time *dt)
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
86
  {
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
87
  	dt->tm_year = dt->tm_year + 1900;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
88
89
90
  
  	/* Remove write protection */
  	max6902_set_reg(dev, 0xF, 0);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
91
92
93
  	max6902_set_reg(dev, 0x01, bin2bcd(dt->tm_sec));
  	max6902_set_reg(dev, 0x03, bin2bcd(dt->tm_min));
  	max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour));
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
94

fe20ba70a   Adrian Bunk   drivers/rtc/: use...
95
  	max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday));
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
96
  	max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon + 1));
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
97
  	max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday));
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
98
99
  	max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year % 100));
  	max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year / 100));
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
100
101
102
103
104
105
106
107
108
109
  
  	/* Compulab used a delay here. However, the datasheet
  	 * does not mention a delay being required anywhere... */
  	/* delay(2000); */
  
  	/* Write protect */
  	max6902_set_reg(dev, 0xF, 0x80);
  
  	return 0;
  }
ff8371ac9   David Brownell   [PATCH] constify ...
110
  static const struct rtc_class_ops max6902_rtc_ops = {
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
111
112
113
114
115
116
117
118
  	.read_time	= max6902_read_time,
  	.set_time	= max6902_set_time,
  };
  
  static int __devinit max6902_probe(struct spi_device *spi)
  {
  	struct rtc_device *rtc;
  	unsigned char tmp;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
119
  	int res;
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
120
121
122
  	spi->mode = SPI_MODE_3;
  	spi->bits_per_word = 8;
  	spi_setup(spi);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
123
  	res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
124
  	if (res != 0)
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
125
  		return res;
a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
126
127
128
129
130
  
  	rtc = rtc_device_register("max6902",
  				&spi->dev, &max6902_rtc_ops, THIS_MODULE);
  	if (IS_ERR(rtc))
  		return PTR_ERR(rtc);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
131

5f003feba   Axel Lin   rtc: rtc-max6902 ...
132
  	dev_set_drvdata(&spi->dev, rtc);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
133
134
135
136
137
  	return 0;
  }
  
  static int __devexit max6902_remove(struct spi_device *spi)
  {
5f003feba   Axel Lin   rtc: rtc-max6902 ...
138
  	struct rtc_device *rtc = dev_get_drvdata(&spi->dev);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
139

a5771c6c4   Alessandro Zummo   rtc: rtc-max6902 ...
140
  	rtc_device_unregister(rtc);
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
141
142
143
144
145
  	return 0;
  }
  
  static struct spi_driver max6902_driver = {
  	.driver = {
5c076fce2   David Brownell   rtc-max6902 minor...
146
  		.name	= "rtc-max6902",
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
147
148
  		.owner	= THIS_MODULE,
  	},
5c076fce2   David Brownell   rtc-max6902 minor...
149
  	.probe	= max6902_probe,
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
150
151
152
153
154
  	.remove = __devexit_p(max6902_remove),
  };
  
  static __init int max6902_init(void)
  {
8e12ecc2b   Raphael Assenat   [PATCH] Add max69...
155
156
157
158
159
160
161
162
163
164
165
166
167
  	return spi_register_driver(&max6902_driver);
  }
  module_init(max6902_init);
  
  static __exit void max6902_exit(void)
  {
  	spi_unregister_driver(&max6902_driver);
  }
  module_exit(max6902_exit);
  
  MODULE_DESCRIPTION ("max6902 spi RTC driver");
  MODULE_AUTHOR ("Raphael Assenat");
  MODULE_LICENSE ("GPL");
e0626e384   Anton Vorontsov   spi: prefix modal...
168
  MODULE_ALIAS("spi:rtc-max6902");