aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorMark Godfrey <mark.godfrey@stericsson.com>2012-01-10 18:10:42 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-01-10 19:30:52 -0500
commitdda367ac064d7473d397b1965019fb3be7cfb6b0 (patch)
tree332b14ebfc6340c6941be7ec64250093ee631f3d /drivers/rtc
parent012e52e15e7ebbc7b08165e8f4b10f71a3e6810b (diff)
rtc/ab8500: add calibration attribute to AB8500 RTC
The rtc_calibration attribute allows user-space to get and set the AB8500's RtcCalibration register. The AB8500 will then use the value in this register to compensate for RTC drift every 60 seconds. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Mark Godfrey <mark.godfrey@stericsson.com> Signed-off-by: Linus Walleij <linus.walleij@stericsson.com> Acked-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Cc: Alessandro Zummo <a.zummo@towertech.it> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/rtc-ab8500.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-ab8500.c b/drivers/rtc/rtc-ab8500.c
index 919b2e5cb7f..df7bfc304c5 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);