Blame view

drivers/rtc/rtc-ab3100.c 6.42 KB
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
1
2
3
4
5
6
7
8
9
10
11
  /*
   * Copyright (C) 2007-2009 ST-Ericsson AB
   * License terms: GNU General Public License (GPL) version 2
   * RTC clock driver for the AB3100 Analog Baseband Chip
   * Author: Linus Walleij <linus.walleij@stericsson.com>
   */
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/platform_device.h>
  #include <linux/rtc.h>
812f9e9d4   Linus Walleij   mfd: Renamed ab31...
12
  #include <linux/mfd/abx500.h>
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
13
14
15
16
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
  
  /* Clock rate in Hz */
  #define AB3100_RTC_CLOCK_RATE	32768
  
  /*
   * The AB3100 RTC registers. These are the same for
   * AB3000 and AB3100.
   * Control register:
   * Bit 0: RTC Monitor cleared=0, active=1, if you set it
   *        to 1 it remains active until RTC power is lost.
   * Bit 1: 32 kHz Oscillator, 0 = on, 1 = bypass
   * Bit 2: Alarm on, 0 = off, 1 = on
   * Bit 3: 32 kHz buffer disabling, 0 = enabled, 1 = disabled
   */
  #define AB3100_RTC		0x53
  /* default setting, buffer disabled, alarm on */
  #define RTC_SETTING		0x30
  /* Alarm when AL0-AL3 == TI0-TI3  */
  #define AB3100_AL0		0x56
  #define AB3100_AL1		0x57
  #define AB3100_AL2		0x58
  #define AB3100_AL3		0x59
  /* This 48-bit register that counts up at 32768 Hz */
  #define AB3100_TI0		0x5a
  #define AB3100_TI1		0x5b
  #define AB3100_TI2		0x5c
  #define AB3100_TI3		0x5d
  #define AB3100_TI4		0x5e
  #define AB3100_TI5		0x5f
  
  /*
   * RTC clock functions and device struct declaration
   */
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
46
  static int ab3100_rtc_set_mmss(struct device *dev, time64_t secs)
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
47
  {
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
48
49
50
  	u8 regs[] = {AB3100_TI0, AB3100_TI1, AB3100_TI2,
  		     AB3100_TI3, AB3100_TI4, AB3100_TI5};
  	unsigned char buf[6];
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
51
  	u64 hw_counter = secs * AB3100_RTC_CLOCK_RATE * 2;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
52
53
  	int err = 0;
  	int i;
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
54
55
56
57
58
59
  	buf[0] = (hw_counter) & 0xFF;
  	buf[1] = (hw_counter >> 8) & 0xFF;
  	buf[2] = (hw_counter >> 16) & 0xFF;
  	buf[3] = (hw_counter >> 24) & 0xFF;
  	buf[4] = (hw_counter >> 32) & 0xFF;
  	buf[5] = (hw_counter >> 40) & 0xFF;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
60
61
  
  	for (i = 0; i < 6; i++) {
fa661258a   Mattias Wallin   mfd: AB3100 regis...
62
  		err = abx500_set_register_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
63
64
65
66
67
68
  							regs[i], buf[i]);
  		if (err)
  			return err;
  	}
  
  	/* Set the flag to mark that the clock is now set */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
69
  	return abx500_mask_and_set_register_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
70
  							  AB3100_RTC,
fa661258a   Mattias Wallin   mfd: AB3100 regis...
71
  							  0x01, 0x01);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
72
73
74
75
76
  
  }
  
  static int ab3100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
77
  	time64_t time;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
78
79
  	u8 rtcval;
  	int err;
fa661258a   Mattias Wallin   mfd: AB3100 regis...
80
  	err = abx500_get_register_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
81
82
83
84
85
86
87
88
  						AB3100_RTC, &rtcval);
  	if (err)
  		return err;
  
  	if (!(rtcval & 0x01)) {
  		dev_info(dev, "clock not set (lost power)");
  		return -EINVAL;
  	} else {
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
89
  		u64 hw_counter;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
90
91
92
  		u8 buf[6];
  
  		/* Read out time registers */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
93
  		err = abx500_get_register_page_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
94
95
96
97
  							     AB3100_TI0,
  							     buf, 6);
  		if (err != 0)
  			return err;
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
98
  		hw_counter = ((u64) buf[5] << 40) | ((u64) buf[4] << 32) |
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
99
100
  			((u64) buf[3] << 24) | ((u64) buf[2] << 16) |
  			((u64) buf[1] << 8) | (u64) buf[0];
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
101
  		time = hw_counter / (u64) (AB3100_RTC_CLOCK_RATE * 2);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
102
  	}
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
103
  	rtc_time64_to_tm(time, tm);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
104
105
106
107
108
109
  
  	return rtc_valid_tm(tm);
  }
  
  static int ab3100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  {
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
110
111
  	time64_t time;
  	u64 hw_counter;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
112
113
114
115
116
  	u8 buf[6];
  	u8 rtcval;
  	int err;
  
  	/* Figure out if alarm is enabled or not */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
117
  	err = abx500_get_register_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
118
119
120
121
122
123
124
125
126
127
  						AB3100_RTC, &rtcval);
  	if (err)
  		return err;
  	if (rtcval & 0x04)
  		alarm->enabled = 1;
  	else
  		alarm->enabled = 0;
  	/* No idea how this could be represented */
  	alarm->pending = 0;
  	/* Read out alarm registers, only 4 bytes */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
128
  	err = abx500_get_register_page_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
129
130
131
  						     AB3100_AL0, buf, 4);
  	if (err)
  		return err;
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
132
  	hw_counter = ((u64) buf[3] << 40) | ((u64) buf[2] << 32) |
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
133
  		((u64) buf[1] << 24) | ((u64) buf[0] << 16);
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
134
  	time = hw_counter / (u64) (AB3100_RTC_CLOCK_RATE * 2);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
135

5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
136
  	rtc_time64_to_tm(time, &alarm->time);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
137
138
139
140
141
142
  
  	return rtc_valid_tm(&alarm->time);
  }
  
  static int ab3100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  {
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
143
144
  	u8 regs[] = {AB3100_AL0, AB3100_AL1, AB3100_AL2, AB3100_AL3};
  	unsigned char buf[4];
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
145
146
  	time64_t secs;
  	u64 hw_counter;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
147
148
  	int err;
  	int i;
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
149
150
151
152
153
154
  	secs = rtc_tm_to_time64(&alarm->time);
  	hw_counter = secs * AB3100_RTC_CLOCK_RATE * 2;
  	buf[0] = (hw_counter >> 16) & 0xFF;
  	buf[1] = (hw_counter >> 24) & 0xFF;
  	buf[2] = (hw_counter >> 32) & 0xFF;
  	buf[3] = (hw_counter >> 40) & 0xFF;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
155
156
157
  
  	/* Set the alarm */
  	for (i = 0; i < 4; i++) {
fa661258a   Mattias Wallin   mfd: AB3100 regis...
158
  		err = abx500_set_register_interruptible(dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
159
160
161
162
163
  							regs[i], buf[i]);
  		if (err)
  			return err;
  	}
  	/* Then enable the alarm */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
164
165
  	return abx500_mask_and_set_register_interruptible(dev, 0,
  							  AB3100_RTC, (1 << 2),
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
166
167
168
169
170
  							  alarm->enabled << 2);
  }
  
  static int ab3100_rtc_irq_enable(struct device *dev, unsigned int enabled)
  {
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
171
172
173
174
175
176
177
178
  	/*
  	 * It's not possible to enable/disable the alarm IRQ for this RTC.
  	 * It does not actually trigger any IRQ: instead its only function is
  	 * to power up the system, if it wasn't on. This will manifest as
  	 * a "power up cause" in the AB3100 power driver (battery charging etc)
  	 * and need to be handled there instead.
  	 */
  	if (enabled)
fa661258a   Mattias Wallin   mfd: AB3100 regis...
179
180
  		return abx500_mask_and_set_register_interruptible(dev, 0,
  						    AB3100_RTC, (1 << 2),
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
181
182
  						    1 << 2);
  	else
fa661258a   Mattias Wallin   mfd: AB3100 regis...
183
184
  		return abx500_mask_and_set_register_interruptible(dev, 0,
  						    AB3100_RTC, (1 << 2),
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
185
186
187
188
189
  						    0);
  }
  
  static const struct rtc_class_ops ab3100_rtc_ops = {
  	.read_time	= ab3100_rtc_read_time,
5c7e11bc6   Xunlei Pang   drivers/rtc/ab310...
190
  	.set_mmss64	= ab3100_rtc_set_mmss,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
191
192
193
194
195
196
197
198
199
200
  	.read_alarm	= ab3100_rtc_read_alarm,
  	.set_alarm	= ab3100_rtc_set_alarm,
  	.alarm_irq_enable = ab3100_rtc_irq_enable,
  };
  
  static int __init ab3100_rtc_probe(struct platform_device *pdev)
  {
  	int err;
  	u8 regval;
  	struct rtc_device *rtc;
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
201
202
  
  	/* The first RTC register needs special treatment */
fa661258a   Mattias Wallin   mfd: AB3100 regis...
203
  	err = abx500_get_register_interruptible(&pdev->dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  						AB3100_RTC, &regval);
  	if (err) {
  		dev_err(&pdev->dev, "unable to read RTC register
  ");
  		return -ENODEV;
  	}
  
  	if ((regval & 0xFE) != RTC_SETTING) {
  		dev_warn(&pdev->dev, "not default value in RTC reg 0x%x
  ",
  			 regval);
  	}
  
  	if ((regval & 1) == 0) {
  		/*
  		 * Set bit to detect power loss.
  		 * This bit remains until RTC power is lost.
  		 */
  		regval = 1 | RTC_SETTING;
fa661258a   Mattias Wallin   mfd: AB3100 regis...
223
  		err = abx500_set_register_interruptible(&pdev->dev, 0,
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
224
225
226
  							AB3100_RTC, regval);
  		/* Ignore any error on this write */
  	}
ef886c4d7   Jingoo Han   rtc: rtc-ab3100: ...
227
228
  	rtc = devm_rtc_device_register(&pdev->dev, "ab3100-rtc",
  					&ab3100_rtc_ops, THIS_MODULE);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
229
230
231
232
  	if (IS_ERR(rtc)) {
  		err = PTR_ERR(rtc);
  		return err;
  	}
eba93fcc3   Axel Lin   drivers/rtc/rtc-a...
233
  	platform_set_drvdata(pdev, rtc);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
234
235
236
  
  	return 0;
  }
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
237
238
239
  static struct platform_driver ab3100_rtc_driver = {
  	.driver = {
  		.name = "ab3100-rtc",
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
240
  	},
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
241
  };
fb34a8120   Jingoo Han   rtc: rtc-ab3100: ...
242
  module_platform_driver_probe(ab3100_rtc_driver, ab3100_rtc_probe);
bd207cfb0   Linus Walleij   rtc: AB3100 RTC s...
243
244
245
246
  
  MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  MODULE_DESCRIPTION("AB3100 RTC Driver");
  MODULE_LICENSE("GPL");