0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/slab.h>
0012 #include <linux/rtc.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/mfd/max8925.h>
0015
0016 enum {
0017 RTC_SEC = 0,
0018 RTC_MIN,
0019 RTC_HOUR,
0020 RTC_WEEKDAY,
0021 RTC_DATE,
0022 RTC_MONTH,
0023 RTC_YEAR1,
0024 RTC_YEAR2,
0025 };
0026
0027 #define MAX8925_RTC_SEC 0x00
0028 #define MAX8925_RTC_MIN 0x01
0029 #define MAX8925_RTC_HOUR 0x02
0030 #define MAX8925_RTC_WEEKDAY 0x03
0031 #define MAX8925_RTC_DATE 0x04
0032 #define MAX8925_RTC_MONTH 0x05
0033 #define MAX8925_RTC_YEAR1 0x06
0034 #define MAX8925_RTC_YEAR2 0x07
0035 #define MAX8925_ALARM0_SEC 0x08
0036 #define MAX8925_ALARM0_MIN 0x09
0037 #define MAX8925_ALARM0_HOUR 0x0a
0038 #define MAX8925_ALARM0_WEEKDAY 0x0b
0039 #define MAX8925_ALARM0_DATE 0x0c
0040 #define MAX8925_ALARM0_MON 0x0d
0041 #define MAX8925_ALARM0_YEAR1 0x0e
0042 #define MAX8925_ALARM0_YEAR2 0x0f
0043 #define MAX8925_ALARM1_SEC 0x10
0044 #define MAX8925_ALARM1_MIN 0x11
0045 #define MAX8925_ALARM1_HOUR 0x12
0046 #define MAX8925_ALARM1_WEEKDAY 0x13
0047 #define MAX8925_ALARM1_DATE 0x14
0048 #define MAX8925_ALARM1_MON 0x15
0049 #define MAX8925_ALARM1_YEAR1 0x16
0050 #define MAX8925_ALARM1_YEAR2 0x17
0051 #define MAX8925_RTC_CNTL 0x1b
0052 #define MAX8925_RTC_STATUS 0x20
0053
0054 #define TIME_NUM 8
0055 #define ALARM_1SEC (1 << 7)
0056 #define HOUR_12 (1 << 7)
0057 #define HOUR_AM_PM (1 << 5)
0058 #define ALARM0_IRQ (1 << 3)
0059 #define ALARM1_IRQ (1 << 2)
0060 #define ALARM0_STATUS (1 << 2)
0061 #define ALARM1_STATUS (1 << 1)
0062
0063
0064 struct max8925_rtc_info {
0065 struct rtc_device *rtc_dev;
0066 struct max8925_chip *chip;
0067 struct i2c_client *rtc;
0068 struct device *dev;
0069 int irq;
0070 };
0071
0072 static irqreturn_t rtc_update_handler(int irq, void *data)
0073 {
0074 struct max8925_rtc_info *info = (struct max8925_rtc_info *)data;
0075
0076
0077 max8925_set_bits(info->rtc, MAX8925_ALARM0_CNTL, 0x7f, 0);
0078 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
0079 return IRQ_HANDLED;
0080 }
0081
0082 static int tm_calc(struct rtc_time *tm, unsigned char *buf, int len)
0083 {
0084 if (len < TIME_NUM)
0085 return -EINVAL;
0086 tm->tm_year = (buf[RTC_YEAR2] >> 4) * 1000
0087 + (buf[RTC_YEAR2] & 0xf) * 100
0088 + (buf[RTC_YEAR1] >> 4) * 10
0089 + (buf[RTC_YEAR1] & 0xf);
0090 tm->tm_year -= 1900;
0091 tm->tm_mon = ((buf[RTC_MONTH] >> 4) & 0x01) * 10
0092 + (buf[RTC_MONTH] & 0x0f);
0093 tm->tm_mday = ((buf[RTC_DATE] >> 4) & 0x03) * 10
0094 + (buf[RTC_DATE] & 0x0f);
0095 tm->tm_wday = buf[RTC_WEEKDAY] & 0x07;
0096 if (buf[RTC_HOUR] & HOUR_12) {
0097 tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x1) * 10
0098 + (buf[RTC_HOUR] & 0x0f);
0099 if (buf[RTC_HOUR] & HOUR_AM_PM)
0100 tm->tm_hour += 12;
0101 } else
0102 tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x03) * 10
0103 + (buf[RTC_HOUR] & 0x0f);
0104 tm->tm_min = ((buf[RTC_MIN] >> 4) & 0x7) * 10
0105 + (buf[RTC_MIN] & 0x0f);
0106 tm->tm_sec = ((buf[RTC_SEC] >> 4) & 0x7) * 10
0107 + (buf[RTC_SEC] & 0x0f);
0108 return 0;
0109 }
0110
0111 static int data_calc(unsigned char *buf, struct rtc_time *tm, int len)
0112 {
0113 unsigned char high, low;
0114
0115 if (len < TIME_NUM)
0116 return -EINVAL;
0117
0118 high = (tm->tm_year + 1900) / 1000;
0119 low = (tm->tm_year + 1900) / 100;
0120 low = low - high * 10;
0121 buf[RTC_YEAR2] = (high << 4) + low;
0122 high = (tm->tm_year + 1900) / 10;
0123 low = tm->tm_year + 1900;
0124 low = low - high * 10;
0125 high = high - (high / 10) * 10;
0126 buf[RTC_YEAR1] = (high << 4) + low;
0127 high = tm->tm_mon / 10;
0128 low = tm->tm_mon;
0129 low = low - high * 10;
0130 buf[RTC_MONTH] = (high << 4) + low;
0131 high = tm->tm_mday / 10;
0132 low = tm->tm_mday;
0133 low = low - high * 10;
0134 buf[RTC_DATE] = (high << 4) + low;
0135 buf[RTC_WEEKDAY] = tm->tm_wday;
0136 high = tm->tm_hour / 10;
0137 low = tm->tm_hour;
0138 low = low - high * 10;
0139 buf[RTC_HOUR] = (high << 4) + low;
0140 high = tm->tm_min / 10;
0141 low = tm->tm_min;
0142 low = low - high * 10;
0143 buf[RTC_MIN] = (high << 4) + low;
0144 high = tm->tm_sec / 10;
0145 low = tm->tm_sec;
0146 low = low - high * 10;
0147 buf[RTC_SEC] = (high << 4) + low;
0148 return 0;
0149 }
0150
0151 static int max8925_rtc_read_time(struct device *dev, struct rtc_time *tm)
0152 {
0153 struct max8925_rtc_info *info = dev_get_drvdata(dev);
0154 unsigned char buf[TIME_NUM];
0155 int ret;
0156
0157 ret = max8925_bulk_read(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
0158 if (ret < 0)
0159 goto out;
0160 ret = tm_calc(tm, buf, TIME_NUM);
0161 out:
0162 return ret;
0163 }
0164
0165 static int max8925_rtc_set_time(struct device *dev, struct rtc_time *tm)
0166 {
0167 struct max8925_rtc_info *info = dev_get_drvdata(dev);
0168 unsigned char buf[TIME_NUM];
0169 int ret;
0170
0171 ret = data_calc(buf, tm, TIME_NUM);
0172 if (ret < 0)
0173 goto out;
0174 ret = max8925_bulk_write(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
0175 out:
0176 return ret;
0177 }
0178
0179 static int max8925_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0180 {
0181 struct max8925_rtc_info *info = dev_get_drvdata(dev);
0182 unsigned char buf[TIME_NUM];
0183 int ret;
0184
0185 ret = max8925_bulk_read(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
0186 if (ret < 0)
0187 goto out;
0188 ret = tm_calc(&alrm->time, buf, TIME_NUM);
0189 if (ret < 0)
0190 goto out;
0191 ret = max8925_reg_read(info->rtc, MAX8925_RTC_IRQ_MASK);
0192 if (ret < 0)
0193 goto out;
0194 if (ret & ALARM0_IRQ) {
0195 alrm->enabled = 0;
0196 } else {
0197 ret = max8925_reg_read(info->rtc, MAX8925_ALARM0_CNTL);
0198 if (ret < 0)
0199 goto out;
0200 if (!ret)
0201 alrm->enabled = 0;
0202 else
0203 alrm->enabled = 1;
0204 }
0205 ret = max8925_reg_read(info->rtc, MAX8925_RTC_STATUS);
0206 if (ret < 0)
0207 goto out;
0208 if (ret & ALARM0_STATUS)
0209 alrm->pending = 1;
0210 else
0211 alrm->pending = 0;
0212 return 0;
0213 out:
0214 return ret;
0215 }
0216
0217 static int max8925_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0218 {
0219 struct max8925_rtc_info *info = dev_get_drvdata(dev);
0220 unsigned char buf[TIME_NUM];
0221 int ret;
0222
0223 ret = data_calc(buf, &alrm->time, TIME_NUM);
0224 if (ret < 0)
0225 goto out;
0226 ret = max8925_bulk_write(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
0227 if (ret < 0)
0228 goto out;
0229 if (alrm->enabled)
0230
0231 ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x77);
0232 else
0233 ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x0);
0234 out:
0235 return ret;
0236 }
0237
0238 static const struct rtc_class_ops max8925_rtc_ops = {
0239 .read_time = max8925_rtc_read_time,
0240 .set_time = max8925_rtc_set_time,
0241 .read_alarm = max8925_rtc_read_alarm,
0242 .set_alarm = max8925_rtc_set_alarm,
0243 };
0244
0245 static int max8925_rtc_probe(struct platform_device *pdev)
0246 {
0247 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
0248 struct max8925_rtc_info *info;
0249 int ret;
0250
0251 info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_rtc_info),
0252 GFP_KERNEL);
0253 if (!info)
0254 return -ENOMEM;
0255 info->chip = chip;
0256 info->rtc = chip->rtc;
0257 info->dev = &pdev->dev;
0258 info->irq = platform_get_irq(pdev, 0);
0259
0260 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
0261 rtc_update_handler, IRQF_ONESHOT,
0262 "rtc-alarm0", info);
0263 if (ret < 0) {
0264 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0265 info->irq, ret);
0266 return ret;
0267 }
0268
0269 dev_set_drvdata(&pdev->dev, info);
0270
0271 platform_set_drvdata(pdev, info);
0272
0273 device_init_wakeup(&pdev->dev, 1);
0274
0275 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8925-rtc",
0276 &max8925_rtc_ops, THIS_MODULE);
0277 ret = PTR_ERR(info->rtc_dev);
0278 if (IS_ERR(info->rtc_dev)) {
0279 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
0280 return ret;
0281 }
0282
0283 return 0;
0284 }
0285
0286 #ifdef CONFIG_PM_SLEEP
0287 static int max8925_rtc_suspend(struct device *dev)
0288 {
0289 struct platform_device *pdev = to_platform_device(dev);
0290 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
0291
0292 if (device_may_wakeup(dev))
0293 chip->wakeup_flag |= 1 << MAX8925_IRQ_RTC_ALARM0;
0294 return 0;
0295 }
0296 static int max8925_rtc_resume(struct device *dev)
0297 {
0298 struct platform_device *pdev = to_platform_device(dev);
0299 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
0300
0301 if (device_may_wakeup(dev))
0302 chip->wakeup_flag &= ~(1 << MAX8925_IRQ_RTC_ALARM0);
0303 return 0;
0304 }
0305 #endif
0306
0307 static SIMPLE_DEV_PM_OPS(max8925_rtc_pm_ops, max8925_rtc_suspend, max8925_rtc_resume);
0308
0309 static struct platform_driver max8925_rtc_driver = {
0310 .driver = {
0311 .name = "max8925-rtc",
0312 .pm = &max8925_rtc_pm_ops,
0313 },
0314 .probe = max8925_rtc_probe,
0315 };
0316
0317 module_platform_driver(max8925_rtc_driver);
0318
0319 MODULE_DESCRIPTION("Maxim MAX8925 RTC driver");
0320 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
0321 MODULE_LICENSE("GPL");
0322