12 #include <linux/module.h>
13 #include <linux/kernel.h>
16 #include <linux/rtc.h>
22 #define AB8500_RTC_SOFF_STAT_REG 0x00
23 #define AB8500_RTC_CC_CONF_REG 0x01
24 #define AB8500_RTC_READ_REQ_REG 0x02
25 #define AB8500_RTC_WATCH_TSECMID_REG 0x03
26 #define AB8500_RTC_WATCH_TSECHI_REG 0x04
27 #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05
28 #define AB8500_RTC_WATCH_TMIN_MID_REG 0x06
29 #define AB8500_RTC_WATCH_TMIN_HI_REG 0x07
30 #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08
31 #define AB8500_RTC_ALRM_MIN_MID_REG 0x09
32 #define AB8500_RTC_ALRM_MIN_HI_REG 0x0A
33 #define AB8500_RTC_STAT_REG 0x0B
34 #define AB8500_RTC_BKUP_CHG_REG 0x0C
35 #define AB8500_RTC_FORCE_BKUP_REG 0x0D
36 #define AB8500_RTC_CALIB_REG 0x0E
37 #define AB8500_RTC_SWITCH_STAT_REG 0x0F
40 #define RTC_READ_REQUEST 0x01
41 #define RTC_WRITE_REQUEST 0x02
44 #define RTC_ALARM_ENA 0x04
45 #define RTC_STATUS_DATA 0x01
47 #define COUNTS_PER_SEC (0xF000 / 60)
48 #define AB8500_RTC_EPOCH 2000
50 static const u8 ab8500_rtc_time_regs[] = {
56 static const u8 ab8500_rtc_alarm_regs[] = {
62 static unsigned long get_elapsed_seconds(
int year)
82 unsigned long mins, secs;
106 for (i = 0; i <
ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
114 mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
116 secs = (buf[3] << 8) | buf[4];
118 secs = secs + (mins * 60);
127 static int ab8500_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
130 unsigned char buf[
ARRAY_SIZE(ab8500_rtc_time_regs)];
131 unsigned long no_secs, no_mins, secs = 0;
134 dev_dbg(dev,
"year should be equal to or greater than %d\n",
154 buf[4] = no_secs & 0xFF;
155 buf[3] = (no_secs >> 8) & 0xFF;
157 buf[2] = no_mins & 0xFF;
158 buf[1] = (no_mins >> 8) & 0xFF;
159 buf[0] = (no_mins >> 16) & 0xFF;
161 for (i = 0; i <
ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
163 ab8500_rtc_time_regs[i], buf[i]);
177 unsigned char buf[
ARRAY_SIZE(ab8500_rtc_alarm_regs)];
178 unsigned long secs, mins;
193 for (i = 0; i <
ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
195 ab8500_rtc_alarm_regs[i], &value);
201 mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
212 static int ab8500_rtc_irq_enable(
struct device *dev,
unsigned int enabled)
216 enabled ? RTC_ALARM_ENA : 0);
219 static int ab8500_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *alarm)
222 unsigned char buf[
ARRAY_SIZE(ab8500_rtc_alarm_regs)];
223 unsigned long mins, secs = 0, cursec = 0;
227 dev_dbg(dev,
"year should be equal to or greater than %d\n",
240 ab8500_rtc_read_time(dev, &curtm);
242 if ((secs - cursec) < 59) {
243 dev_dbg(dev,
"Alarm less than 1 minute not supported\r\n");
255 buf[2] = mins & 0xFF;
256 buf[1] = (mins >> 8) & 0xFF;
257 buf[0] = (mins >> 16) & 0xFF;
260 for (i = 0; i <
ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
262 ab8500_rtc_alarm_regs[i], buf[i]);
267 return ab8500_rtc_irq_enable(dev, alarm->
enabled);
271 static int ab8500_rtc_set_calibration(
struct device *dev,
int calibration)
283 if ((calibration < -127) || (calibration > 127)) {
284 dev_err(dev,
"RtcCalibration value outside permitted range\n");
293 if (calibration >= 0)
294 rtccal = 0x7F & calibration;
296 rtccal = ~(calibration - 1) | 0x80;
304 static int ab8500_rtc_get_calibration(
struct device *dev,
int *calibration)
318 *calibration = 0 - (rtccal & 0x7F);
320 *calibration = 0x7F & rtccal;
326 static ssize_t ab8500_sysfs_store_rtc_calibration(
struct device *dev,
328 const char *buf,
size_t count)
333 if (
sscanf(buf,
" %i ", &calibration) != 1) {
334 dev_err(dev,
"Failed to store RTC calibration attribute\n");
338 retval = ab8500_rtc_set_calibration(dev, calibration);
340 return retval ? retval :
count;
343 static ssize_t ab8500_sysfs_show_rtc_calibration(
struct device *dev,
349 retval = ab8500_rtc_get_calibration(dev, &calibration);
351 dev_err(dev,
"Failed to read RTC calibration attribute\n");
356 return sprintf(buf,
"%d\n", calibration);
360 ab8500_sysfs_show_rtc_calibration,
361 ab8500_sysfs_store_rtc_calibration);
363 static int ab8500_sysfs_rtc_register(
struct device *dev)
368 static void ab8500_sysfs_rtc_unregister(
struct device *dev)
385 .read_time = ab8500_rtc_read_time,
386 .set_time = ab8500_rtc_set_time,
387 .read_alarm = ab8500_rtc_read_alarm,
388 .set_alarm = ab8500_rtc_set_alarm,
389 .alarm_irq_enable = ab8500_rtc_irq_enable,
440 platform_set_drvdata(pdev, rtc);
442 err = ab8500_sysfs_rtc_register(&pdev->
dev);
444 dev_err(&pdev->
dev,
"sysfs RTC failed to register\n");
453 struct rtc_device *rtc = platform_get_drvdata(pdev);
456 ab8500_sysfs_rtc_unregister(&pdev->
dev);
460 platform_set_drvdata(pdev,
NULL);
467 .name =
"ab8500-rtc",
470 .probe = ab8500_rtc_probe,