Blame view
drivers/rtc/rtc-ab8500.c
10.9 KB
af873fcec treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
0af62f4d1 rtc: AB8500 RTC d... |
2 3 4 |
/* * Copyright (C) ST-Ericsson SA 2010 * |
0af62f4d1 rtc: AB8500 RTC d... |
5 6 7 8 9 10 11 12 13 14 15 16 |
* Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com> * * RTC clock driver for the RTC part of the AB8500 Power management chip. * Based on RTC clock driver for the AB3100 Analog Baseband Chip by * 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> |
47c169750 mfd: Align ab8500... |
17 |
#include <linux/mfd/abx500.h> |
ee66e653c mfd: Unify abx500... |
18 |
#include <linux/mfd/abx500/ab8500.h> |
0af62f4d1 rtc: AB8500 RTC d... |
19 |
#include <linux/delay.h> |
ad49fcbe9 drivers/rtc/rtc-a... |
20 |
#include <linux/of.h> |
93a6f9168 rtc: ab8500: remo... |
21 |
#include <linux/pm_wakeirq.h> |
0af62f4d1 rtc: AB8500 RTC d... |
22 |
|
47c169750 mfd: Align ab8500... |
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#define AB8500_RTC_SOFF_STAT_REG 0x00 #define AB8500_RTC_CC_CONF_REG 0x01 #define AB8500_RTC_READ_REQ_REG 0x02 #define AB8500_RTC_WATCH_TSECMID_REG 0x03 #define AB8500_RTC_WATCH_TSECHI_REG 0x04 #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05 #define AB8500_RTC_WATCH_TMIN_MID_REG 0x06 #define AB8500_RTC_WATCH_TMIN_HI_REG 0x07 #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08 #define AB8500_RTC_ALRM_MIN_MID_REG 0x09 #define AB8500_RTC_ALRM_MIN_HI_REG 0x0A #define AB8500_RTC_STAT_REG 0x0B #define AB8500_RTC_BKUP_CHG_REG 0x0C #define AB8500_RTC_FORCE_BKUP_REG 0x0D #define AB8500_RTC_CALIB_REG 0x0E #define AB8500_RTC_SWITCH_STAT_REG 0x0F |
0af62f4d1 rtc: AB8500 RTC d... |
39 40 41 42 43 44 45 46 47 48 |
/* RtcReadRequest bits */ #define RTC_READ_REQUEST 0x01 #define RTC_WRITE_REQUEST 0x02 /* RtcCtrl bits */ #define RTC_ALARM_ENA 0x04 #define RTC_STATUS_DATA 0x01 #define COUNTS_PER_SEC (0xF000 / 60) |
0af62f4d1 rtc: AB8500 RTC d... |
49 |
|
47c169750 mfd: Align ab8500... |
50 |
static const u8 ab8500_rtc_time_regs[] = { |
0af62f4d1 rtc: AB8500 RTC d... |
51 52 53 54 |
AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG, AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG, AB8500_RTC_WATCH_TSECMID_REG }; |
47c169750 mfd: Align ab8500... |
55 |
static const u8 ab8500_rtc_alarm_regs[] = { |
0af62f4d1 rtc: AB8500 RTC d... |
56 57 58 |
AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG, AB8500_RTC_ALRM_MIN_LOW_REG }; |
0af62f4d1 rtc: AB8500 RTC d... |
59 60 |
static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm) { |
0af62f4d1 rtc: AB8500 RTC d... |
61 62 63 64 |
unsigned long timeout = jiffies + HZ; int retval, i; unsigned long mins, secs; unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; |
47c169750 mfd: Align ab8500... |
65 |
u8 value; |
0af62f4d1 rtc: AB8500 RTC d... |
66 67 |
/* Request a data read */ |
47c169750 mfd: Align ab8500... |
68 69 |
retval = abx500_set_register_interruptible(dev, AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST); |
0af62f4d1 rtc: AB8500 RTC d... |
70 71 |
if (retval < 0) return retval; |
064407f1c drivers/rtc/rtc-a... |
72 73 74 75 76 77 78 79 80 81 82 |
/* Wait for some cycles after enabling the rtc read in ab8500 */ while (time_before(jiffies, timeout)) { retval = abx500_get_register_interruptible(dev, AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value); if (retval < 0) return retval; if (!(value & RTC_READ_REQUEST)) break; usleep_range(1000, 5000); |
0af62f4d1 rtc: AB8500 RTC d... |
83 84 85 86 |
} /* Read the Watchtime registers */ for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { |
47c169750 mfd: Align ab8500... |
87 88 |
retval = abx500_get_register_interruptible(dev, AB8500_RTC, ab8500_rtc_time_regs[i], &value); |
0af62f4d1 rtc: AB8500 RTC d... |
89 90 |
if (retval < 0) return retval; |
47c169750 mfd: Align ab8500... |
91 |
buf[i] = value; |
0af62f4d1 rtc: AB8500 RTC d... |
92 93 94 95 96 97 98 |
} mins = (buf[0] << 16) | (buf[1] << 8) | buf[2]; secs = (buf[3] << 8) | buf[4]; secs = secs / COUNTS_PER_SEC; secs = secs + (mins * 60); |
a5965a310 rtc: ab8500: swit... |
99 |
rtc_time64_to_tm(secs, tm); |
ab62670e5 rtc: stop validat... |
100 |
return 0; |
0af62f4d1 rtc: AB8500 RTC d... |
101 102 103 104 |
} static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm) { |
0af62f4d1 rtc: AB8500 RTC d... |
105 106 107 |
int retval, i; unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; unsigned long no_secs, no_mins, secs = 0; |
a5965a310 rtc: ab8500: swit... |
108 |
secs = rtc_tm_to_time64(tm); |
0af62f4d1 rtc: AB8500 RTC d... |
109 |
|
0af62f4d1 rtc: AB8500 RTC d... |
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
no_mins = secs / 60; no_secs = secs % 60; /* Make the seconds count as per the RTC resolution */ no_secs = no_secs * COUNTS_PER_SEC; buf[4] = no_secs & 0xFF; buf[3] = (no_secs >> 8) & 0xFF; buf[2] = no_mins & 0xFF; buf[1] = (no_mins >> 8) & 0xFF; buf[0] = (no_mins >> 16) & 0xFF; for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { |
47c169750 mfd: Align ab8500... |
124 125 |
retval = abx500_set_register_interruptible(dev, AB8500_RTC, ab8500_rtc_time_regs[i], buf[i]); |
0af62f4d1 rtc: AB8500 RTC d... |
126 127 128 129 130 |
if (retval < 0) return retval; } /* Request a data write */ |
47c169750 mfd: Align ab8500... |
131 132 |
return abx500_set_register_interruptible(dev, AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST); |
0af62f4d1 rtc: AB8500 RTC d... |
133 134 135 136 |
} static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) { |
0af62f4d1 rtc: AB8500 RTC d... |
137 |
int retval, i; |
47c169750 mfd: Align ab8500... |
138 |
u8 rtc_ctrl, value; |
0af62f4d1 rtc: AB8500 RTC d... |
139 140 141 142 |
unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; unsigned long secs, mins; /* Check if the alarm is enabled or not */ |
47c169750 mfd: Align ab8500... |
143 144 145 146 |
retval = abx500_get_register_interruptible(dev, AB8500_RTC, AB8500_RTC_STAT_REG, &rtc_ctrl); if (retval < 0) return retval; |
0af62f4d1 rtc: AB8500 RTC d... |
147 148 149 150 151 152 153 154 155 |
if (rtc_ctrl & RTC_ALARM_ENA) alarm->enabled = 1; else alarm->enabled = 0; alarm->pending = 0; for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { |
47c169750 mfd: Align ab8500... |
156 157 |
retval = abx500_get_register_interruptible(dev, AB8500_RTC, ab8500_rtc_alarm_regs[i], &value); |
0af62f4d1 rtc: AB8500 RTC d... |
158 159 |
if (retval < 0) return retval; |
47c169750 mfd: Align ab8500... |
160 |
buf[i] = value; |
0af62f4d1 rtc: AB8500 RTC d... |
161 162 163 164 |
} mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]); secs = mins * 60; |
a5965a310 rtc: ab8500: swit... |
165 |
rtc_time64_to_tm(secs, &alarm->time); |
0af62f4d1 rtc: AB8500 RTC d... |
166 |
|
9a90a5bcc rtc: ab8500: remo... |
167 |
return 0; |
0af62f4d1 rtc: AB8500 RTC d... |
168 169 170 171 |
} static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled) { |
47c169750 mfd: Align ab8500... |
172 173 174 |
return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC, AB8500_RTC_STAT_REG, RTC_ALARM_ENA, enabled ? RTC_ALARM_ENA : 0); |
0af62f4d1 rtc: AB8500 RTC d... |
175 176 177 178 |
} static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) { |
0af62f4d1 rtc: AB8500 RTC d... |
179 180 |
int retval, i; unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; |
dc43d4a26 drivers/rtc/rtc-a... |
181 182 |
unsigned long mins, secs = 0, cursec = 0; struct rtc_time curtm; |
0af62f4d1 rtc: AB8500 RTC d... |
183 |
|
0af62f4d1 rtc: AB8500 RTC d... |
184 |
/* Get the number of seconds since 1970 */ |
a5965a310 rtc: ab8500: swit... |
185 |
secs = rtc_tm_to_time64(&alarm->time); |
0af62f4d1 rtc: AB8500 RTC d... |
186 187 |
/* |
dc43d4a26 drivers/rtc/rtc-a... |
188 189 190 191 192 |
* Check whether alarm is set less than 1min. * Since our RTC doesn't support alarm resolution less than 1min, * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON */ ab8500_rtc_read_time(dev, &curtm); /* Read current time */ |
a5965a310 rtc: ab8500: swit... |
193 |
cursec = rtc_tm_to_time64(&curtm); |
dc43d4a26 drivers/rtc/rtc-a... |
194 195 196 197 198 |
if ((secs - cursec) < 59) { dev_dbg(dev, "Alarm less than 1 minute not supported\r "); return -EINVAL; } |
0af62f4d1 rtc: AB8500 RTC d... |
199 200 201 202 203 204 205 206 |
mins = secs / 60; buf[2] = mins & 0xFF; buf[1] = (mins >> 8) & 0xFF; buf[0] = (mins >> 16) & 0xFF; /* Set the alarm time */ for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { |
47c169750 mfd: Align ab8500... |
207 208 |
retval = abx500_set_register_interruptible(dev, AB8500_RTC, ab8500_rtc_alarm_regs[i], buf[i]); |
0af62f4d1 rtc: AB8500 RTC d... |
209 210 211 212 213 214 |
if (retval < 0) return retval; } return ab8500_rtc_irq_enable(dev, alarm->enabled); } |
dda367ac0 rtc/ab8500: add c... |
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
static int ab8500_rtc_set_calibration(struct device *dev, int calibration) { int retval; u8 rtccal = 0; /* * Check that the calibration value (which is in units of 0.5 * parts-per-million) is in the AB8500's range for RtcCalibration * register. -128 (0x80) is not permitted because the AB8500 uses * a sign-bit rather than two's complement, so 0x80 is just another * representation of zero. */ if ((calibration < -127) || (calibration > 127)) { dev_err(dev, "RtcCalibration value outside permitted range "); return -EINVAL; } /* * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) * so need to convert to this sort of representation before writing * into RtcCalibration register... */ if (calibration >= 0) rtccal = 0x7F & calibration; else rtccal = ~(calibration - 1) | 0x80; retval = abx500_set_register_interruptible(dev, AB8500_RTC, AB8500_RTC_CALIB_REG, rtccal); return retval; } static int ab8500_rtc_get_calibration(struct device *dev, int *calibration) { int retval; u8 rtccal = 0; retval = abx500_get_register_interruptible(dev, AB8500_RTC, AB8500_RTC_CALIB_REG, &rtccal); if (retval >= 0) { /* * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) * so need to convert value from RtcCalibration register into * a two's complement signed value... */ if (rtccal & 0x80) *calibration = 0 - (rtccal & 0x7F); else *calibration = 0x7F & rtccal; } return retval; } static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int retval; int calibration = 0; if (sscanf(buf, " %i ", &calibration) != 1) { dev_err(dev, "Failed to store RTC calibration attribute "); return -EINVAL; } retval = ab8500_rtc_set_calibration(dev, calibration); return retval ? retval : count; } static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev, struct device_attribute *attr, char *buf) { int retval = 0; int calibration = 0; retval = ab8500_rtc_get_calibration(dev, &calibration); if (retval < 0) { dev_err(dev, "Failed to read RTC calibration attribute "); sprintf(buf, "0 "); return retval; } return sprintf(buf, "%d ", calibration); } static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR, ab8500_sysfs_show_rtc_calibration, ab8500_sysfs_store_rtc_calibration); |
b56295dd3 rtc: ab8500: use ... |
311 312 313 314 |
static struct attribute *ab8500_rtc_attrs[] = { &dev_attr_rtc_calibration.attr, NULL }; |
dda367ac0 rtc/ab8500: add c... |
315 |
|
b56295dd3 rtc: ab8500: use ... |
316 317 318 |
static const struct attribute_group ab8500_rtc_sysfs_files = { .attrs = ab8500_rtc_attrs, }; |
dda367ac0 rtc/ab8500: add c... |
319 |
|
0af62f4d1 rtc: AB8500 RTC d... |
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
static irqreturn_t rtc_alarm_handler(int irq, void *data) { struct rtc_device *rtc = data; unsigned long events = RTC_IRQF | RTC_AF; dev_dbg(&rtc->dev, "%s ", __func__); rtc_update_irq(rtc, 1, events); return IRQ_HANDLED; } static const struct rtc_class_ops ab8500_rtc_ops = { .read_time = ab8500_rtc_read_time, .set_time = ab8500_rtc_set_time, .read_alarm = ab8500_rtc_read_alarm, .set_alarm = ab8500_rtc_set_alarm, .alarm_irq_enable = ab8500_rtc_irq_enable, }; |
9a72f4104 rtc: ab8500: Cons... |
339 |
static const struct platform_device_id ab85xx_rtc_ids[] = { |
25d053cf1 drivers/rtc/rtc-a... |
340 |
{ "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, }, |
8a67e9315 rtc: ab8500: Add ... |
341 |
{ /* sentinel */ } |
25d053cf1 drivers/rtc/rtc-a... |
342 |
}; |
63074cc3d rtc: Fix module a... |
343 |
MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids); |
25d053cf1 drivers/rtc/rtc-a... |
344 |
|
5a167f454 Drivers: rtc: rem... |
345 |
static int ab8500_rtc_probe(struct platform_device *pdev) |
0af62f4d1 rtc: AB8500 RTC d... |
346 |
{ |
25d053cf1 drivers/rtc/rtc-a... |
347 |
const struct platform_device_id *platid = platform_get_device_id(pdev); |
0af62f4d1 rtc: AB8500 RTC d... |
348 349 |
int err; struct rtc_device *rtc; |
47c169750 mfd: Align ab8500... |
350 |
u8 rtc_ctrl; |
0af62f4d1 rtc: AB8500 RTC d... |
351 352 353 354 355 356 357 |
int irq; irq = platform_get_irq_byname(pdev, "ALARM"); if (irq < 0) return irq; /* For RTC supply test */ |
47c169750 mfd: Align ab8500... |
358 359 |
err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC, AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA); |
0af62f4d1 rtc: AB8500 RTC d... |
360 361 362 363 |
if (err < 0) return err; /* Wait for reset by the PorRtc */ |
012e52e15 drivers/rtc/rtc-a... |
364 |
usleep_range(1000, 5000); |
0af62f4d1 rtc: AB8500 RTC d... |
365 |
|
47c169750 mfd: Align ab8500... |
366 367 368 369 |
err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC, AB8500_RTC_STAT_REG, &rtc_ctrl); if (err < 0) return err; |
0af62f4d1 rtc: AB8500 RTC d... |
370 371 372 373 374 375 376 |
/* Check if the RTC Supply fails */ if (!(rtc_ctrl & RTC_STATUS_DATA)) { dev_err(&pdev->dev, "RTC supply failure "); return -ENODEV; } |
b62581e62 rtc/ab8500: set c... |
377 |
device_init_wakeup(&pdev->dev, true); |
b56295dd3 rtc: ab8500: use ... |
378 379 380 381 382 |
rtc = devm_rtc_allocate_device(&pdev->dev); if (IS_ERR(rtc)) return PTR_ERR(rtc); rtc->ops = (struct rtc_class_ops *)platid->driver_data; |
0af62f4d1 rtc: AB8500 RTC d... |
383 |
|
fa11f7e77 rtc: rtc-ab8500: ... |
384 |
err = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
93a6f9168 rtc: ab8500: remo... |
385 |
rtc_alarm_handler, IRQF_ONESHOT, |
fa11f7e77 rtc: rtc-ab8500: ... |
386 387 |
"ab8500-rtc", rtc); if (err < 0) |
0af62f4d1 rtc: AB8500 RTC d... |
388 |
return err; |
0af62f4d1 rtc: AB8500 RTC d... |
389 |
|
93a6f9168 rtc: ab8500: remo... |
390 |
dev_pm_set_wake_irq(&pdev->dev, irq); |
0af62f4d1 rtc: AB8500 RTC d... |
391 |
platform_set_drvdata(pdev, rtc); |
c594d6787 rtc/ab8500: set u... |
392 |
rtc->uie_unsupported = 1; |
38ab97aeb rtc: ab8500: let ... |
393 394 395 |
rtc->range_max = (1ULL << 24) * 60 - 1; // 24-bit minutes + 59 secs rtc->start_secs = RTC_TIMESTAMP_BEGIN_2000; rtc->set_start_time = true; |
b56295dd3 rtc: ab8500: use ... |
396 397 398 399 400 |
err = rtc_add_group(rtc, &ab8500_rtc_sysfs_files); if (err) return err; return rtc_register_device(rtc); |
0af62f4d1 rtc: AB8500 RTC d... |
401 |
} |
5a167f454 Drivers: rtc: rem... |
402 |
static int ab8500_rtc_remove(struct platform_device *pdev) |
0af62f4d1 rtc: AB8500 RTC d... |
403 |
{ |
93a6f9168 rtc: ab8500: remo... |
404 405 |
dev_pm_clear_wake_irq(&pdev->dev); device_init_wakeup(&pdev->dev, false); |
dda367ac0 rtc/ab8500: add c... |
406 |
|
0af62f4d1 rtc: AB8500 RTC d... |
407 408 409 410 411 412 |
return 0; } static struct platform_driver ab8500_rtc_driver = { .driver = { .name = "ab8500-rtc", |
0af62f4d1 rtc: AB8500 RTC d... |
413 414 |
}, .probe = ab8500_rtc_probe, |
5a167f454 Drivers: rtc: rem... |
415 |
.remove = ab8500_rtc_remove, |
25d053cf1 drivers/rtc/rtc-a... |
416 |
.id_table = ab85xx_rtc_ids, |
0af62f4d1 rtc: AB8500 RTC d... |
417 |
}; |
0c4eae665 rtc: convert driv... |
418 |
module_platform_driver(ab8500_rtc_driver); |
0af62f4d1 rtc: AB8500 RTC d... |
419 |
|
0af62f4d1 rtc: AB8500 RTC d... |
420 421 422 |
MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>"); MODULE_DESCRIPTION("AB8500 RTC Driver"); MODULE_LICENSE("GPL v2"); |