Krzysztof Kozlowski | 734c5cd | 2018-08-07 18:19:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // RTC driver for Maxim MAX8998 |
| 4 | // |
| 5 | // Copyright (C) 2010 Samsung Electronics Co.Ltd |
| 6 | // Author: Minkyu Kang <mk7.kang@samsung.com> |
| 7 | // Author: Joonyoung Shim <jy0922.shim@samsung.com> |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/bcd.h> |
Tomasz Figa | 443c6ae | 2013-06-24 14:39:52 +0200 | [diff] [blame] | 13 | #include <linux/irqdomain.h> |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 14 | #include <linux/rtc.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/mfd/max8998.h> |
| 17 | #include <linux/mfd/max8998-private.h> |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 18 | #include <linux/delay.h> |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 19 | |
| 20 | #define MAX8998_RTC_SEC 0x00 |
| 21 | #define MAX8998_RTC_MIN 0x01 |
| 22 | #define MAX8998_RTC_HOUR 0x02 |
| 23 | #define MAX8998_RTC_WEEKDAY 0x03 |
| 24 | #define MAX8998_RTC_DATE 0x04 |
| 25 | #define MAX8998_RTC_MONTH 0x05 |
| 26 | #define MAX8998_RTC_YEAR1 0x06 |
| 27 | #define MAX8998_RTC_YEAR2 0x07 |
| 28 | #define MAX8998_ALARM0_SEC 0x08 |
| 29 | #define MAX8998_ALARM0_MIN 0x09 |
| 30 | #define MAX8998_ALARM0_HOUR 0x0a |
| 31 | #define MAX8998_ALARM0_WEEKDAY 0x0b |
| 32 | #define MAX8998_ALARM0_DATE 0x0c |
| 33 | #define MAX8998_ALARM0_MONTH 0x0d |
| 34 | #define MAX8998_ALARM0_YEAR1 0x0e |
| 35 | #define MAX8998_ALARM0_YEAR2 0x0f |
| 36 | #define MAX8998_ALARM1_SEC 0x10 |
| 37 | #define MAX8998_ALARM1_MIN 0x11 |
| 38 | #define MAX8998_ALARM1_HOUR 0x12 |
| 39 | #define MAX8998_ALARM1_WEEKDAY 0x13 |
| 40 | #define MAX8998_ALARM1_DATE 0x14 |
| 41 | #define MAX8998_ALARM1_MONTH 0x15 |
| 42 | #define MAX8998_ALARM1_YEAR1 0x16 |
| 43 | #define MAX8998_ALARM1_YEAR2 0x17 |
| 44 | #define MAX8998_ALARM0_CONF 0x18 |
| 45 | #define MAX8998_ALARM1_CONF 0x19 |
| 46 | #define MAX8998_RTC_STATUS 0x1a |
| 47 | #define MAX8998_WTSR_SMPL_CNTL 0x1b |
| 48 | #define MAX8998_TEST 0x1f |
| 49 | |
| 50 | #define HOUR_12 (1 << 7) |
| 51 | #define HOUR_PM (1 << 5) |
| 52 | #define ALARM0_STATUS (1 << 1) |
| 53 | #define ALARM1_STATUS (1 << 2) |
| 54 | |
| 55 | enum { |
| 56 | RTC_SEC = 0, |
| 57 | RTC_MIN, |
| 58 | RTC_HOUR, |
| 59 | RTC_WEEKDAY, |
| 60 | RTC_DATE, |
| 61 | RTC_MONTH, |
| 62 | RTC_YEAR1, |
| 63 | RTC_YEAR2, |
| 64 | }; |
| 65 | |
| 66 | struct max8998_rtc_info { |
| 67 | struct device *dev; |
| 68 | struct max8998_dev *max8998; |
| 69 | struct i2c_client *rtc; |
| 70 | struct rtc_device *rtc_dev; |
| 71 | int irq; |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 72 | bool lp3974_bug_workaround; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static void max8998_data_to_tm(u8 *data, struct rtc_time *tm) |
| 76 | { |
| 77 | tm->tm_sec = bcd2bin(data[RTC_SEC]); |
| 78 | tm->tm_min = bcd2bin(data[RTC_MIN]); |
| 79 | if (data[RTC_HOUR] & HOUR_12) { |
| 80 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); |
| 81 | if (data[RTC_HOUR] & HOUR_PM) |
| 82 | tm->tm_hour += 12; |
| 83 | } else |
| 84 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); |
| 85 | |
| 86 | tm->tm_wday = data[RTC_WEEKDAY] & 0x07; |
| 87 | tm->tm_mday = bcd2bin(data[RTC_DATE]); |
| 88 | tm->tm_mon = bcd2bin(data[RTC_MONTH]); |
| 89 | tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; |
| 90 | tm->tm_year -= 1900; |
| 91 | } |
| 92 | |
| 93 | static void max8998_tm_to_data(struct rtc_time *tm, u8 *data) |
| 94 | { |
| 95 | data[RTC_SEC] = bin2bcd(tm->tm_sec); |
| 96 | data[RTC_MIN] = bin2bcd(tm->tm_min); |
| 97 | data[RTC_HOUR] = bin2bcd(tm->tm_hour); |
| 98 | data[RTC_WEEKDAY] = tm->tm_wday; |
| 99 | data[RTC_DATE] = bin2bcd(tm->tm_mday); |
| 100 | data[RTC_MONTH] = bin2bcd(tm->tm_mon); |
| 101 | data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); |
| 102 | data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); |
| 103 | } |
| 104 | |
| 105 | static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 106 | { |
| 107 | struct max8998_rtc_info *info = dev_get_drvdata(dev); |
| 108 | u8 data[8]; |
| 109 | int ret; |
| 110 | |
| 111 | ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | |
| 115 | max8998_data_to_tm(data, tm); |
| 116 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 117 | return 0; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 121 | { |
| 122 | struct max8998_rtc_info *info = dev_get_drvdata(dev); |
| 123 | u8 data[8]; |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 124 | int ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 125 | |
| 126 | max8998_tm_to_data(tm, data); |
| 127 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 128 | ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data); |
| 129 | |
| 130 | if (info->lp3974_bug_workaround) |
| 131 | msleep(2000); |
| 132 | |
| 133 | return ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 137 | { |
| 138 | struct max8998_rtc_info *info = dev_get_drvdata(dev); |
| 139 | u8 data[8]; |
| 140 | u8 val; |
| 141 | int ret; |
| 142 | |
| 143 | ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data); |
| 144 | if (ret < 0) |
| 145 | return ret; |
| 146 | |
| 147 | max8998_data_to_tm(data, &alrm->time); |
| 148 | |
| 149 | ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val); |
| 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | |
| 153 | alrm->enabled = !!val; |
| 154 | |
| 155 | ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val); |
| 156 | if (ret < 0) |
| 157 | return ret; |
| 158 | |
| 159 | if (val & ALARM0_STATUS) |
| 160 | alrm->pending = 1; |
| 161 | else |
| 162 | alrm->pending = 0; |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info) |
| 168 | { |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 169 | int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0); |
| 170 | |
| 171 | if (info->lp3974_bug_workaround) |
| 172 | msleep(2000); |
| 173 | |
| 174 | return ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int max8998_rtc_start_alarm(struct max8998_rtc_info *info) |
| 178 | { |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 179 | int ret; |
| 180 | u8 alarm0_conf = 0x77; |
| 181 | |
| 182 | /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */ |
| 183 | if (info->lp3974_bug_workaround) |
| 184 | alarm0_conf = 0x57; |
| 185 | |
| 186 | ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf); |
| 187 | |
| 188 | if (info->lp3974_bug_workaround) |
| 189 | msleep(2000); |
| 190 | |
| 191 | return ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 195 | { |
| 196 | struct max8998_rtc_info *info = dev_get_drvdata(dev); |
| 197 | u8 data[8]; |
| 198 | int ret; |
| 199 | |
| 200 | max8998_tm_to_data(&alrm->time, data); |
| 201 | |
| 202 | ret = max8998_rtc_stop_alarm(info); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
| 206 | ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data); |
| 207 | if (ret < 0) |
| 208 | return ret; |
| 209 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 210 | if (info->lp3974_bug_workaround) |
| 211 | msleep(2000); |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 212 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 213 | if (alrm->enabled) |
| 214 | ret = max8998_rtc_start_alarm(info); |
| 215 | |
| 216 | return ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static int max8998_rtc_alarm_irq_enable(struct device *dev, |
| 220 | unsigned int enabled) |
| 221 | { |
| 222 | struct max8998_rtc_info *info = dev_get_drvdata(dev); |
| 223 | |
| 224 | if (enabled) |
| 225 | return max8998_rtc_start_alarm(info); |
| 226 | else |
| 227 | return max8998_rtc_stop_alarm(info); |
| 228 | } |
| 229 | |
| 230 | static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data) |
| 231 | { |
| 232 | struct max8998_rtc_info *info = data; |
| 233 | |
| 234 | rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); |
| 235 | |
| 236 | return IRQ_HANDLED; |
| 237 | } |
| 238 | |
| 239 | static const struct rtc_class_ops max8998_rtc_ops = { |
| 240 | .read_time = max8998_rtc_read_time, |
| 241 | .set_time = max8998_rtc_set_time, |
| 242 | .read_alarm = max8998_rtc_read_alarm, |
| 243 | .set_alarm = max8998_rtc_set_alarm, |
| 244 | .alarm_irq_enable = max8998_rtc_alarm_irq_enable, |
| 245 | }; |
| 246 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 247 | static int max8998_rtc_probe(struct platform_device *pdev) |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 248 | { |
| 249 | struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent); |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 250 | struct max8998_platform_data *pdata = max8998->pdata; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 251 | struct max8998_rtc_info *info; |
| 252 | int ret; |
| 253 | |
Jingoo Han | 8925e31 | 2013-04-29 16:20:43 -0700 | [diff] [blame] | 254 | info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info), |
| 255 | GFP_KERNEL); |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 256 | if (!info) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | info->dev = &pdev->dev; |
| 260 | info->max8998 = max8998; |
| 261 | info->rtc = max8998->rtc; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 262 | |
John Stultz | 03cf7c4 | 2011-05-06 17:27:07 -0700 | [diff] [blame] | 263 | platform_set_drvdata(pdev, info); |
| 264 | |
Jingoo Han | 8925e31 | 2013-04-29 16:20:43 -0700 | [diff] [blame] | 265 | info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc", |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 266 | &max8998_rtc_ops, THIS_MODULE); |
| 267 | |
| 268 | if (IS_ERR(info->rtc_dev)) { |
| 269 | ret = PTR_ERR(info->rtc_dev); |
| 270 | dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); |
Jingoo Han | 4802f22 | 2013-07-03 15:06:28 -0700 | [diff] [blame] | 271 | return ret; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Tomasz Figa | 443c6ae | 2013-06-24 14:39:52 +0200 | [diff] [blame] | 274 | if (!max8998->irq_domain) |
| 275 | goto no_irq; |
| 276 | |
| 277 | info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0); |
| 278 | if (!info->irq) { |
| 279 | dev_warn(&pdev->dev, "Failed to map alarm IRQ\n"); |
| 280 | goto no_irq; |
| 281 | } |
| 282 | |
Jingoo Han | 8925e31 | 2013-04-29 16:20:43 -0700 | [diff] [blame] | 283 | ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, |
| 284 | max8998_rtc_alarm_irq, 0, "rtc-alarm0", info); |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 285 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 286 | if (ret < 0) |
| 287 | dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", |
| 288 | info->irq, ret); |
| 289 | |
Tomasz Figa | 443c6ae | 2013-06-24 14:39:52 +0200 | [diff] [blame] | 290 | no_irq: |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 291 | dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name); |
Tomasz Figa | 43c523b | 2013-05-24 15:55:35 -0700 | [diff] [blame] | 292 | if (pdata && pdata->rtc_delay) { |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 293 | info->lp3974_bug_workaround = true; |
| 294 | dev_warn(&pdev->dev, "LP3974 with RTC REGERR option." |
| 295 | " RTC updates will be extremely slow.\n"); |
| 296 | } |
| 297 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 298 | return 0; |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 299 | } |
| 300 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 301 | static const struct platform_device_id max8998_rtc_id[] = { |
| 302 | { "max8998-rtc", TYPE_MAX8998 }, |
| 303 | { "lp3974-rtc", TYPE_LP3974 }, |
| 304 | { } |
| 305 | }; |
Axel Lin | 8ad0f5b | 2011-04-18 20:16:57 +0800 | [diff] [blame] | 306 | MODULE_DEVICE_TABLE(platform, max8998_rtc_id); |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 307 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 308 | static struct platform_driver max8998_rtc_driver = { |
| 309 | .driver = { |
| 310 | .name = "max8998-rtc", |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 311 | }, |
| 312 | .probe = max8998_rtc_probe, |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 313 | .id_table = max8998_rtc_id, |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 314 | }; |
| 315 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 316 | module_platform_driver(max8998_rtc_driver); |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 317 | |
| 318 | MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>"); |
| 319 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); |
| 320 | MODULE_DESCRIPTION("Maxim MAX8998 RTC driver"); |
| 321 | MODULE_LICENSE("GPL"); |