Blame view

drivers/rtc/rtc-fm3130.c 16.7 KB
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * rtc-fm3130.c - RTC driver for Ramtron FM3130 I2C chip.
   *
   *  Copyright (C) 2008 Sergey Lapin
   *  Based on ds1307 driver by James Chapman and David Brownell
   *
   * 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.
   */
  
  #include <linux/module.h>
  #include <linux/i2c.h>
  #include <linux/rtc.h>
  #include <linux/bcd.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
16
  #include <linux/slab.h>
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  
  #define FM3130_RTC_CONTROL	(0x0)
  #define FM3130_CAL_CONTROL	(0x1)
  #define FM3130_RTC_SECONDS	(0x2)
  #define FM3130_RTC_MINUTES	(0x3)
  #define FM3130_RTC_HOURS	(0x4)
  #define FM3130_RTC_DAY		(0x5)
  #define FM3130_RTC_DATE		(0x6)
  #define FM3130_RTC_MONTHS	(0x7)
  #define FM3130_RTC_YEARS	(0x8)
  
  #define FM3130_ALARM_SECONDS	(0x9)
  #define FM3130_ALARM_MINUTES	(0xa)
  #define FM3130_ALARM_HOURS	(0xb)
  #define FM3130_ALARM_DATE	(0xc)
  #define FM3130_ALARM_MONTHS	(0xd)
  #define FM3130_ALARM_WP_CONTROL	(0xe)
  
  #define FM3130_CAL_CONTROL_BIT_nOSCEN (1 << 7) /* Osciallator enabled */
  #define FM3130_RTC_CONTROL_BIT_LB (1 << 7) /* Low battery */
  #define FM3130_RTC_CONTROL_BIT_AF (1 << 6) /* Alarm flag */
  #define FM3130_RTC_CONTROL_BIT_CF (1 << 5) /* Century overflow */
  #define FM3130_RTC_CONTROL_BIT_POR (1 << 4) /* Power on reset */
  #define FM3130_RTC_CONTROL_BIT_AEN (1 << 3) /* Alarm enable */
  #define FM3130_RTC_CONTROL_BIT_CAL (1 << 2) /* Calibration mode */
  #define FM3130_RTC_CONTROL_BIT_WRITE (1 << 1) /* W=1 -> write mode W=0 normal */
  #define FM3130_RTC_CONTROL_BIT_READ (1 << 0) /* R=1 -> read mode R=0 normal */
  
  #define FM3130_CLOCK_REGS 7
  #define FM3130_ALARM_REGS 5
  
  struct fm3130 {
  	u8			reg_addr_time;
  	u8 			reg_addr_alarm;
  	u8			regs[15];
  	struct i2c_msg		msg[4];
  	struct i2c_client	*client;
  	struct rtc_device	*rtc;
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
55
  	int			alarm_valid;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
56
  	int			data_valid;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
57
58
  };
  static const struct i2c_device_id fm3130_id[] = {
876550aa3   Alessandro Zummo   rtc-fm3130: fix c...
59
  	{ "fm3130", 0 },
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, fm3130_id);
  
  #define FM3130_MODE_NORMAL		0
  #define FM3130_MODE_WRITE		1
  #define FM3130_MODE_READ		2
  
  static void fm3130_rtc_mode(struct device *dev, int mode)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  
  	fm3130->regs[FM3130_RTC_CONTROL] =
  		i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL);
  	switch (mode) {
  	case FM3130_MODE_NORMAL:
  		fm3130->regs[FM3130_RTC_CONTROL] &=
  			~(FM3130_RTC_CONTROL_BIT_WRITE |
  			FM3130_RTC_CONTROL_BIT_READ);
  		break;
  	case FM3130_MODE_WRITE:
  		fm3130->regs[FM3130_RTC_CONTROL] |= FM3130_RTC_CONTROL_BIT_WRITE;
  		break;
  	case FM3130_MODE_READ:
  		fm3130->regs[FM3130_RTC_CONTROL] |= FM3130_RTC_CONTROL_BIT_READ;
  		break;
  	default:
  		dev_dbg(dev, "invalid mode %d
  ", mode);
  		break;
  	}
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
91

c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
92
93
94
95
96
97
98
99
100
101
102
103
  	i2c_smbus_write_byte_data(fm3130->client,
  		 FM3130_RTC_CONTROL, fm3130->regs[FM3130_RTC_CONTROL]);
  }
  
  static int fm3130_get_time(struct device *dev, struct rtc_time *t)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  	int		tmp;
  
  	if (!fm3130->data_valid) {
  		/* We have invalid data in RTC, probably due
  		to battery faults or other problems. Return EIO
fd0961ff6   Uwe Kleine-König   fix typos concern...
104
  		for now, it will allow us to set data later instead
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  		of error during probing which disables device */
  		return -EIO;
  	}
  	fm3130_rtc_mode(dev, FM3130_MODE_READ);
  
  	/* read the RTC date and time registers all at once */
  	tmp = i2c_transfer(to_i2c_adapter(fm3130->client->dev.parent),
  			fm3130->msg, 2);
  	if (tmp != 2) {
  		dev_err(dev, "%s error %d
  ", "read", tmp);
  		return -EIO;
  	}
  
  	fm3130_rtc_mode(dev, FM3130_MODE_NORMAL);
  
  	dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x"
  			"%02x %02x %02x %02x %02x %02x %02x
  ",
  			"read",
  			fm3130->regs[0], fm3130->regs[1],
  			fm3130->regs[2], fm3130->regs[3],
  			fm3130->regs[4], fm3130->regs[5],
  			fm3130->regs[6], fm3130->regs[7],
  			fm3130->regs[8], fm3130->regs[9],
  			fm3130->regs[0xa], fm3130->regs[0xb],
  			fm3130->regs[0xc], fm3130->regs[0xd],
  			fm3130->regs[0xe]);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
133
134
  	t->tm_sec = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f);
  	t->tm_min = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
135
  	tmp = fm3130->regs[FM3130_RTC_HOURS] & 0x3f;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
136
137
138
  	t->tm_hour = bcd2bin(tmp);
  	t->tm_wday = bcd2bin(fm3130->regs[FM3130_RTC_DAY] & 0x07) - 1;
  	t->tm_mday = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
139
  	tmp = fm3130->regs[FM3130_RTC_MONTHS] & 0x1f;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
140
  	t->tm_mon = bcd2bin(tmp) - 1;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
141
142
  
  	/* assume 20YY not 19YY, and ignore CF bit */
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
143
  	t->tm_year = bcd2bin(fm3130->regs[FM3130_RTC_YEARS]) + 100;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  
  	dev_dbg(dev, "%s secs=%d, mins=%d, "
  		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d
  ",
  		"read", t->tm_sec, t->tm_min,
  		t->tm_hour, t->tm_mday,
  		t->tm_mon, t->tm_year, t->tm_wday);
  
  	/* initial clock setting can be undefined */
  	return rtc_valid_tm(t);
  }
  
  
  static int fm3130_set_time(struct device *dev, struct rtc_time *t)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  	int		tmp, i;
  	u8		*buf = fm3130->regs;
  
  	dev_dbg(dev, "%s secs=%d, mins=%d, "
  		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d
  ",
  		"write", t->tm_sec, t->tm_min,
  		t->tm_hour, t->tm_mday,
  		t->tm_mon, t->tm_year, t->tm_wday);
  
  	/* first register addr */
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
171
172
173
174
175
176
  	buf[FM3130_RTC_SECONDS] = bin2bcd(t->tm_sec);
  	buf[FM3130_RTC_MINUTES] = bin2bcd(t->tm_min);
  	buf[FM3130_RTC_HOURS] = bin2bcd(t->tm_hour);
  	buf[FM3130_RTC_DAY] = bin2bcd(t->tm_wday + 1);
  	buf[FM3130_RTC_DATE] = bin2bcd(t->tm_mday);
  	buf[FM3130_RTC_MONTHS] = bin2bcd(t->tm_mon + 1);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
177
178
179
  
  	/* assume 20YY not 19YY */
  	tmp = t->tm_year - 100;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
180
  	buf[FM3130_RTC_YEARS] = bin2bcd(tmp);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  
  	dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x"
  		"%02x %02x %02x %02x %02x %02x %02x %02x
  ",
  		"write", buf[0], buf[1], buf[2], buf[3],
  		buf[4], buf[5], buf[6], buf[7],
  		buf[8], buf[9], buf[0xa], buf[0xb],
  		buf[0xc], buf[0xd], buf[0xe]);
  
  	fm3130_rtc_mode(dev, FM3130_MODE_WRITE);
  
  	/* Writing time registers, we don't support multibyte transfers */
  	for (i = 0; i < FM3130_CLOCK_REGS; i++) {
  		i2c_smbus_write_byte_data(fm3130->client,
  					FM3130_RTC_SECONDS + i,
  					fm3130->regs[FM3130_RTC_SECONDS + i]);
  	}
  
  	fm3130_rtc_mode(dev, FM3130_MODE_NORMAL);
  
  	/* We assume here that data are valid once written */
  	if (!fm3130->data_valid)
  		fm3130->data_valid = 1;
  	return 0;
  }
  
  static int fm3130_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  	int tmp;
  	struct rtc_time *tm = &alrm->time;
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
212
213
214
215
216
217
218
219
220
221
  
  	if (!fm3130->alarm_valid) {
  		/*
  		 * We have invalid alarm in RTC, probably due to battery faults
  		 * or other problems. Return EIO for now, it will allow us to
  		 * set alarm value later instead of error during probing which
  		 * disables device
  		 */
  		return -EIO;
  	}
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  	/* read the RTC alarm registers all at once */
  	tmp = i2c_transfer(to_i2c_adapter(fm3130->client->dev.parent),
  			&fm3130->msg[2], 2);
  	if (tmp != 2) {
  		dev_err(dev, "%s error %d
  ", "read", tmp);
  		return -EIO;
  	}
  	dev_dbg(dev, "alarm read %02x %02x %02x %02x %02x
  ",
  			fm3130->regs[FM3130_ALARM_SECONDS],
  			fm3130->regs[FM3130_ALARM_MINUTES],
  			fm3130->regs[FM3130_ALARM_HOURS],
  			fm3130->regs[FM3130_ALARM_DATE],
  			fm3130->regs[FM3130_ALARM_MONTHS]);
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
237
238
239
240
241
  	tm->tm_sec	= bcd2bin(fm3130->regs[FM3130_ALARM_SECONDS] & 0x7F);
  	tm->tm_min	= bcd2bin(fm3130->regs[FM3130_ALARM_MINUTES] & 0x7F);
  	tm->tm_hour	= bcd2bin(fm3130->regs[FM3130_ALARM_HOURS] & 0x3F);
  	tm->tm_mday	= bcd2bin(fm3130->regs[FM3130_ALARM_DATE] & 0x3F);
  	tm->tm_mon	= bcd2bin(fm3130->regs[FM3130_ALARM_MONTHS] & 0x1F);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
242

c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
243
244
  	if (tm->tm_mon > 0)
  		tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
245

c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
246
247
248
249
250
251
  	dev_dbg(dev, "%s secs=%d, mins=%d, "
  		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d
  ",
  		"read alarm", tm->tm_sec, tm->tm_min,
  		tm->tm_hour, tm->tm_mday,
  		tm->tm_mon, tm->tm_year, tm->tm_wday);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
252
253
254
255
256
257
258
259
260
  	/* check if alarm enabled */
  	fm3130->regs[FM3130_RTC_CONTROL] =
  		i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL);
  
  	if ((fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_AEN) &&
  		(~fm3130->regs[FM3130_RTC_CONTROL] &
  			FM3130_RTC_CONTROL_BIT_CAL)) {
  		alrm->enabled = 1;
  	}
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  	return 0;
  }
  
  static int fm3130_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  	struct rtc_time *tm = &alrm->time;
  	int i;
  
  	dev_dbg(dev, "%s secs=%d, mins=%d, "
  		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d
  ",
  		"write alarm", tm->tm_sec, tm->tm_min,
  		tm->tm_hour, tm->tm_mday,
  		tm->tm_mon, tm->tm_year, tm->tm_wday);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
276
277
  	fm3130->regs[FM3130_ALARM_SECONDS] =
  		(tm->tm_sec != -1) ? bin2bcd(tm->tm_sec) : 0x80;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
278

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
279
280
  	fm3130->regs[FM3130_ALARM_MINUTES] =
  		(tm->tm_min != -1) ? bin2bcd(tm->tm_min) : 0x80;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
281

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
282
283
  	fm3130->regs[FM3130_ALARM_HOURS] =
  		(tm->tm_hour != -1) ? bin2bcd(tm->tm_hour) : 0x80;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
284

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
285
286
  	fm3130->regs[FM3130_ALARM_DATE] =
  		(tm->tm_mday != -1) ? bin2bcd(tm->tm_mday) : 0x80;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
287

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
288
289
  	fm3130->regs[FM3130_ALARM_MONTHS] =
  		(tm->tm_mon != -1) ? bin2bcd(tm->tm_mon + 1) : 0x80;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  
  	dev_dbg(dev, "alarm write %02x %02x %02x %02x %02x
  ",
  			fm3130->regs[FM3130_ALARM_SECONDS],
  			fm3130->regs[FM3130_ALARM_MINUTES],
  			fm3130->regs[FM3130_ALARM_HOURS],
  			fm3130->regs[FM3130_ALARM_DATE],
  			fm3130->regs[FM3130_ALARM_MONTHS]);
  	/* Writing time registers, we don't support multibyte transfers */
  	for (i = 0; i < FM3130_ALARM_REGS; i++) {
  		i2c_smbus_write_byte_data(fm3130->client,
  					FM3130_ALARM_SECONDS + i,
  					fm3130->regs[FM3130_ALARM_SECONDS + i]);
  	}
  	fm3130->regs[FM3130_RTC_CONTROL] =
  		i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
306
307
  
  	/* enable or disable alarm */
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
308
309
310
311
312
313
314
315
  	if (alrm->enabled) {
  		i2c_smbus_write_byte_data(fm3130->client, FM3130_RTC_CONTROL,
  			(fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_CAL)) |
  					FM3130_RTC_CONTROL_BIT_AEN);
  	} else {
  		i2c_smbus_write_byte_data(fm3130->client, FM3130_RTC_CONTROL,
  			fm3130->regs[FM3130_RTC_CONTROL] &
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
316
317
  				~(FM3130_RTC_CONTROL_BIT_CAL) &
  					~(FM3130_RTC_CONTROL_BIT_AEN));
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
318
  	}
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
319
320
321
322
  
  	/* We assume here that data is valid once written */
  	if (!fm3130->alarm_valid)
  		fm3130->alarm_valid = 1;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
323
324
  	return 0;
  }
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  static int fm3130_alarm_irq_enable(struct device *dev, unsigned int enabled)
  {
  	struct fm3130 *fm3130 = dev_get_drvdata(dev);
  	int ret = 0;
  
  	fm3130->regs[FM3130_RTC_CONTROL] =
  		i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL);
  
  	dev_dbg(dev, "alarm_irq_enable: enable=%d, FM3130_RTC_CONTROL=%02x
  ",
  		enabled, fm3130->regs[FM3130_RTC_CONTROL]);
  
  	switch (enabled) {
  	case 0:		/* alarm off */
  		ret = i2c_smbus_write_byte_data(fm3130->client,
  			FM3130_RTC_CONTROL, fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_CAL) &
  					~(FM3130_RTC_CONTROL_BIT_AEN));
  		break;
  	case 1:		/* alarm on */
  		ret = i2c_smbus_write_byte_data(fm3130->client,
  			FM3130_RTC_CONTROL, (fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_CAL)) |
  					FM3130_RTC_CONTROL_BIT_AEN);
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
  
  	return ret;
  }
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
357
358
359
360
361
  static const struct rtc_class_ops fm3130_rtc_ops = {
  	.read_time	= fm3130_get_time,
  	.set_time	= fm3130_set_time,
  	.read_alarm	= fm3130_read_alarm,
  	.set_alarm	= fm3130_set_alarm,
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
362
  	.alarm_irq_enable = fm3130_alarm_irq_enable,
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  };
  
  static struct i2c_driver fm3130_driver;
  
  static int __devinit fm3130_probe(struct i2c_client *client,
  				  const struct i2c_device_id *id)
  {
  	struct fm3130		*fm3130;
  	int			err = -ENODEV;
  	int			tmp;
  	struct i2c_adapter	*adapter = to_i2c_adapter(client->dev.parent);
  
  	if (!i2c_check_functionality(adapter,
  			I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  		return -EIO;
  
  	fm3130 = kzalloc(sizeof(struct fm3130), GFP_KERNEL);
  
  	if (!fm3130)
  		return -ENOMEM;
  
  	fm3130->client = client;
  	i2c_set_clientdata(client, fm3130);
  	fm3130->reg_addr_time = FM3130_RTC_SECONDS;
  	fm3130->reg_addr_alarm = FM3130_ALARM_SECONDS;
  
  	/* Messages to read time */
  	fm3130->msg[0].addr = client->addr;
  	fm3130->msg[0].flags = 0;
  	fm3130->msg[0].len = 1;
  	fm3130->msg[0].buf = &fm3130->reg_addr_time;
  
  	fm3130->msg[1].addr = client->addr;
  	fm3130->msg[1].flags = I2C_M_RD;
  	fm3130->msg[1].len = FM3130_CLOCK_REGS;
  	fm3130->msg[1].buf = &fm3130->regs[FM3130_RTC_SECONDS];
  
  	/* Messages to read alarm */
  	fm3130->msg[2].addr = client->addr;
  	fm3130->msg[2].flags = 0;
  	fm3130->msg[2].len = 1;
  	fm3130->msg[2].buf = &fm3130->reg_addr_alarm;
  
  	fm3130->msg[3].addr = client->addr;
  	fm3130->msg[3].flags = I2C_M_RD;
  	fm3130->msg[3].len = FM3130_ALARM_REGS;
  	fm3130->msg[3].buf = &fm3130->regs[FM3130_ALARM_SECONDS];
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
410
  	fm3130->alarm_valid = 0;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  	fm3130->data_valid = 0;
  
  	tmp = i2c_transfer(adapter, fm3130->msg, 4);
  	if (tmp != 4) {
  		pr_debug("read error %d
  ", tmp);
  		err = -EIO;
  		goto exit_free;
  	}
  
  	fm3130->regs[FM3130_RTC_CONTROL] =
  		i2c_smbus_read_byte_data(client, FM3130_RTC_CONTROL);
  	fm3130->regs[FM3130_CAL_CONTROL] =
  		i2c_smbus_read_byte_data(client, FM3130_CAL_CONTROL);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
425
  	/* Disabling calibration mode */
f4b516282   Sergey Matyukevich   rtc-fm3130: add m...
426
  	if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_CAL) {
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
427
428
429
430
431
  		i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL,
  			fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_CAL));
  		dev_warn(&client->dev, "Disabling calibration mode!
  ");
f4b516282   Sergey Matyukevich   rtc-fm3130: add m...
432
  	}
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
433
434
435
  
  	/* Disabling read and write modes */
  	if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_WRITE ||
f4b516282   Sergey Matyukevich   rtc-fm3130: add m...
436
  	    fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_READ) {
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
437
438
439
440
441
442
  		i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL,
  			fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_READ |
  					FM3130_RTC_CONTROL_BIT_WRITE));
  		dev_warn(&client->dev, "Disabling READ or WRITE mode!
  ");
f4b516282   Sergey Matyukevich   rtc-fm3130: add m...
443
  	}
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
444
445
446
447
448
449
  
  	/* oscillator off?  turn it on, so clock can tick. */
  	if (fm3130->regs[FM3130_CAL_CONTROL] & FM3130_CAL_CONTROL_BIT_nOSCEN)
  		i2c_smbus_write_byte_data(client, FM3130_CAL_CONTROL,
  			fm3130->regs[FM3130_CAL_CONTROL] &
  				~(FM3130_CAL_CONTROL_BIT_nOSCEN));
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
450
451
452
453
454
  	/* low battery?  clear flag, and warn */
  	if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_LB) {
  		i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL,
  			fm3130->regs[FM3130_RTC_CONTROL] &
  				~(FM3130_RTC_CONTROL_BIT_LB));
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
455
456
  		dev_warn(&client->dev, "Low battery!
  ");
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
457
  	}
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
458

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
459
  	/* check if Power On Reset bit is set */
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
460
461
462
463
  	if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_POR) {
  		i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL,
  			fm3130->regs[FM3130_RTC_CONTROL] &
  				~FM3130_RTC_CONTROL_BIT_POR);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
464
465
  		dev_dbg(&client->dev, "POR bit is set
  ");
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
466
467
468
  	}
  	/* ACS is controlled by alarm */
  	i2c_smbus_write_byte_data(client, FM3130_ALARM_WP_CONTROL, 0x80);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
469
470
471
472
  	/* alarm registers sanity check */
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f);
  	if (tmp > 59)
  		goto bad_alarm;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
473
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f);
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
474
475
476
477
478
479
  	if (tmp > 59)
  		goto bad_alarm;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_HOURS] & 0x3f);
  	if (tmp > 23)
  		goto bad_alarm;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
480

fe20ba70a   Adrian Bunk   drivers/rtc/: use...
481
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
482
  	if (tmp == 0 || tmp > 31)
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
483
  		goto bad_alarm;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
484

fe20ba70a   Adrian Bunk   drivers/rtc/: use...
485
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f);
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
486
  	if (tmp == 0 || tmp > 12)
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
487
  		goto bad_alarm;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
488

f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  	fm3130->alarm_valid = 1;
  
  bad_alarm:
  
  	/* clock registers sanity chek */
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f);
  	if (tmp > 59)
  		goto bad_clock;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f);
  	if (tmp > 59)
  		goto bad_clock;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_HOURS] & 0x3f);
  	if (tmp > 23)
  		goto bad_clock;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_DAY] & 0x7);
  	if (tmp == 0 || tmp > 7)
  		goto bad_clock;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f);
  	if (tmp == 0 || tmp > 31)
  		goto bad_clock;
  
  	tmp = bcd2bin(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f);
  	if (tmp == 0 || tmp > 12)
  		goto bad_clock;
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
517
518
  
  	fm3130->data_valid = 1;
f3f99cf39   Sergey Matyukevich   rtc: fixes and ne...
519
520
521
  bad_clock:
  
  	if (!fm3130->data_valid || !fm3130->alarm_valid)
c6d8f400c   Sergey Lapin   rtc: Ramtron FM31...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  		dev_dbg(&client->dev,
  				"%s: %02x %02x %02x %02x %02x %02x %02x %02x"
  				"%02x %02x %02x %02x %02x %02x %02x
  ",
  			"bogus registers",
  			fm3130->regs[0], fm3130->regs[1],
  			fm3130->regs[2], fm3130->regs[3],
  			fm3130->regs[4], fm3130->regs[5],
  			fm3130->regs[6], fm3130->regs[7],
  			fm3130->regs[8], fm3130->regs[9],
  			fm3130->regs[0xa], fm3130->regs[0xb],
  			fm3130->regs[0xc], fm3130->regs[0xd],
  			fm3130->regs[0xe]);
  
  	/* We won't bail out here because we just got invalid data.
  	   Time setting from u-boot doesn't work anyway */
  	fm3130->rtc = rtc_device_register(client->name, &client->dev,
  				&fm3130_rtc_ops, THIS_MODULE);
  	if (IS_ERR(fm3130->rtc)) {
  		err = PTR_ERR(fm3130->rtc);
  		dev_err(&client->dev,
  			"unable to register the class device
  ");
  		goto exit_free;
  	}
  	return 0;
  exit_free:
  	kfree(fm3130);
  	return err;
  }
  
  static int __devexit fm3130_remove(struct i2c_client *client)
  {
  	struct fm3130 *fm3130 = i2c_get_clientdata(client);
  
  	rtc_device_unregister(fm3130->rtc);
  	kfree(fm3130);
  	return 0;
  }
  
  static struct i2c_driver fm3130_driver = {
  	.driver = {
  		.name	= "rtc-fm3130",
  		.owner	= THIS_MODULE,
  	},
  	.probe		= fm3130_probe,
  	.remove		= __devexit_p(fm3130_remove),
  	.id_table	= fm3130_id,
  };
  
  static int __init fm3130_init(void)
  {
  	return i2c_add_driver(&fm3130_driver);
  }
  module_init(fm3130_init);
  
  static void __exit fm3130_exit(void)
  {
  	i2c_del_driver(&fm3130_driver);
  }
  module_exit(fm3130_exit);
  
  MODULE_DESCRIPTION("RTC driver for FM3130");
  MODULE_AUTHOR("Sergey Lapin <slapin@ossfans.org>");
  MODULE_LICENSE("GPL");