aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/ABI/testing/sysfs-class-rtc-rtc0-device-rtc_calibration12
-rw-r--r--drivers/rtc/rtc-ab8500.c112
2 files changed, 124 insertions, 0 deletions
diff --git a/Documentation/ABI/testing/sysfs-class-rtc-rtc0-device-rtc_calibration b/Documentation/ABI/testing/sysfs-class-rtc-rtc0-device-rtc_calibration
new file mode 100644
index 000000000000..4cf1e72222d9
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-rtc-rtc0-device-rtc_calibration
@@ -0,0 +1,12 @@
1What: Attribute for calibrating ST-Ericsson AB8500 Real Time Clock
2Date: Oct 2011
3KernelVersion: 3.0
4Contact: Mark Godfrey <mark.godfrey@stericsson.com>
5Description: The rtc_calibration attribute allows the userspace to
6 calibrate the AB8500.s 32KHz Real Time Clock.
7 Every 60 seconds the AB8500 will correct the RTC's value
8 by adding to it the value of this attribute.
9 The range of the attribute is -127 to +127 in units of
10 30.5 micro-seconds (half-parts-per-million of the 32KHz clock)
11Users: The /vendor/st-ericsson/base_utilities/core/rtc_calibration
12 daemon uses this interface.
diff --git a/drivers/rtc/rtc-ab8500.c b/drivers/rtc/rtc-ab8500.c
index 919b2e5cb7f0..df7bfc304c5e 100644
--- a/drivers/rtc/rtc-ab8500.c
+++ b/drivers/rtc/rtc-ab8500.c
@@ -258,6 +258,109 @@ static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
258 return ab8500_rtc_irq_enable(dev, alarm->enabled); 258 return ab8500_rtc_irq_enable(dev, alarm->enabled);
259} 259}
260 260
261
262static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
263{
264 int retval;
265 u8 rtccal = 0;
266
267 /*
268 * Check that the calibration value (which is in units of 0.5
269 * parts-per-million) is in the AB8500's range for RtcCalibration
270 * register. -128 (0x80) is not permitted because the AB8500 uses
271 * a sign-bit rather than two's complement, so 0x80 is just another
272 * representation of zero.
273 */
274 if ((calibration < -127) || (calibration > 127)) {
275 dev_err(dev, "RtcCalibration value outside permitted range\n");
276 return -EINVAL;
277 }
278
279 /*
280 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
281 * so need to convert to this sort of representation before writing
282 * into RtcCalibration register...
283 */
284 if (calibration >= 0)
285 rtccal = 0x7F & calibration;
286 else
287 rtccal = ~(calibration - 1) | 0x80;
288
289 retval = abx500_set_register_interruptible(dev, AB8500_RTC,
290 AB8500_RTC_CALIB_REG, rtccal);
291
292 return retval;
293}
294
295static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
296{
297 int retval;
298 u8 rtccal = 0;
299
300 retval = abx500_get_register_interruptible(dev, AB8500_RTC,
301 AB8500_RTC_CALIB_REG, &rtccal);
302 if (retval >= 0) {
303 /*
304 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
305 * so need to convert value from RtcCalibration register into
306 * a two's complement signed value...
307 */
308 if (rtccal & 0x80)
309 *calibration = 0 - (rtccal & 0x7F);
310 else
311 *calibration = 0x7F & rtccal;
312 }
313
314 return retval;
315}
316
317static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
320{
321 int retval;
322 int calibration = 0;
323
324 if (sscanf(buf, " %i ", &calibration) != 1) {
325 dev_err(dev, "Failed to store RTC calibration attribute\n");
326 return -EINVAL;
327 }
328
329 retval = ab8500_rtc_set_calibration(dev, calibration);
330
331 return retval ? retval : count;
332}
333
334static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 int retval = 0;
338 int calibration = 0;
339
340 retval = ab8500_rtc_get_calibration(dev, &calibration);
341 if (retval < 0) {
342 dev_err(dev, "Failed to read RTC calibration attribute\n");
343 sprintf(buf, "0\n");
344 return retval;
345 }
346
347 return sprintf(buf, "%d\n", calibration);
348}
349
350static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
351 ab8500_sysfs_show_rtc_calibration,
352 ab8500_sysfs_store_rtc_calibration);
353
354static int ab8500_sysfs_rtc_register(struct device *dev)
355{
356 return device_create_file(dev, &dev_attr_rtc_calibration);
357}
358
359static void ab8500_sysfs_rtc_unregister(struct device *dev)
360{
361 device_remove_file(dev, &dev_attr_rtc_calibration);
362}
363
261static irqreturn_t rtc_alarm_handler(int irq, void *data) 364static irqreturn_t rtc_alarm_handler(int irq, void *data)
262{ 365{
263 struct rtc_device *rtc = data; 366 struct rtc_device *rtc = data;
@@ -327,6 +430,13 @@ static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
327 430
328 platform_set_drvdata(pdev, rtc); 431 platform_set_drvdata(pdev, rtc);
329 432
433
434 err = ab8500_sysfs_rtc_register(&pdev->dev);
435 if (err) {
436 dev_err(&pdev->dev, "sysfs RTC failed to register\n");
437 return err;
438 }
439
330 return 0; 440 return 0;
331} 441}
332 442
@@ -335,6 +445,8 @@ static int __devexit ab8500_rtc_remove(struct platform_device *pdev)
335 struct rtc_device *rtc = platform_get_drvdata(pdev); 445 struct rtc_device *rtc = platform_get_drvdata(pdev);
336 int irq = platform_get_irq_byname(pdev, "ALARM"); 446 int irq = platform_get_irq_byname(pdev, "ALARM");
337 447
448 ab8500_sysfs_rtc_unregister(&pdev->dev);
449
338 free_irq(irq, rtc); 450 free_irq(irq, rtc);
339 rtc_device_unregister(rtc); 451 rtc_device_unregister(rtc);
340 platform_set_drvdata(pdev, NULL); 452 platform_set_drvdata(pdev, NULL);