Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C |
| 3 | * |
Lei Xu | a2d6d2f | 2011-02-25 14:44:23 -0800 | [diff] [blame] | 4 | * Copyright (C) 2009-2011 Freescale Semiconductor. |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 5 | * Author: Jack Lan <jack.lan@freescale.com> |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | */ |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 12 | |
Joe Perches | a737e835 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/bcd.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/slab.h> |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 23 | #include <linux/regmap.h> |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 24 | |
| 25 | #define DS3232_REG_SECONDS 0x00 |
| 26 | #define DS3232_REG_MINUTES 0x01 |
| 27 | #define DS3232_REG_HOURS 0x02 |
| 28 | #define DS3232_REG_AMPM 0x02 |
| 29 | #define DS3232_REG_DAY 0x03 |
| 30 | #define DS3232_REG_DATE 0x04 |
| 31 | #define DS3232_REG_MONTH 0x05 |
| 32 | #define DS3232_REG_CENTURY 0x05 |
| 33 | #define DS3232_REG_YEAR 0x06 |
| 34 | #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */ |
| 35 | #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */ |
| 36 | #define DS3232_REG_CR 0x0E /* Control register */ |
| 37 | # define DS3232_REG_CR_nEOSC 0x80 |
| 38 | # define DS3232_REG_CR_INTCN 0x04 |
| 39 | # define DS3232_REG_CR_A2IE 0x02 |
| 40 | # define DS3232_REG_CR_A1IE 0x01 |
| 41 | |
| 42 | #define DS3232_REG_SR 0x0F /* control/status register */ |
| 43 | # define DS3232_REG_SR_OSF 0x80 |
| 44 | # define DS3232_REG_SR_BSY 0x04 |
| 45 | # define DS3232_REG_SR_A2F 0x02 |
| 46 | # define DS3232_REG_SR_A1F 0x01 |
| 47 | |
| 48 | struct ds3232 { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 49 | struct device *dev; |
| 50 | struct regmap *regmap; |
| 51 | int irq; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 52 | struct rtc_device *rtc; |
| 53 | struct work_struct work; |
| 54 | |
| 55 | /* The mutex protects alarm operations, and prevents a race |
| 56 | * between the enable_irq() in the workqueue and the free_irq() |
| 57 | * in the remove function. |
| 58 | */ |
| 59 | struct mutex mutex; |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 60 | bool suspended; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 61 | int exiting; |
| 62 | }; |
| 63 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 64 | static int ds3232_check_rtc_status(struct device *dev) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 65 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 66 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 67 | int ret = 0; |
| 68 | int control, stat; |
| 69 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 70 | ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat); |
| 71 | if (ret) |
| 72 | return ret; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 73 | |
| 74 | if (stat & DS3232_REG_SR_OSF) |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 75 | dev_warn(dev, |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 76 | "oscillator discontinuity flagged, " |
| 77 | "time unreliable\n"); |
| 78 | |
| 79 | stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F); |
| 80 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 81 | ret = regmap_write(ds3232->regmap, DS3232_REG_SR, stat); |
| 82 | if (ret) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 83 | return ret; |
| 84 | |
| 85 | /* If the alarm is pending, clear it before requesting |
| 86 | * the interrupt, so an interrupt event isn't reported |
| 87 | * before everything is initialized. |
| 88 | */ |
| 89 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 90 | ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control); |
| 91 | if (ret) |
| 92 | return ret; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 93 | |
| 94 | control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE); |
| 95 | control |= DS3232_REG_CR_INTCN; |
| 96 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 97 | return regmap_write(ds3232->regmap, DS3232_REG_CR, control); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int ds3232_read_time(struct device *dev, struct rtc_time *time) |
| 101 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 102 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 103 | int ret; |
| 104 | u8 buf[7]; |
| 105 | unsigned int year, month, day, hour, minute, second; |
| 106 | unsigned int week, twelve_hr, am_pm; |
| 107 | unsigned int century, add_century = 0; |
| 108 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 109 | ret = regmap_bulk_read(ds3232->regmap, DS3232_REG_SECONDS, buf, 7); |
| 110 | if (ret) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 111 | return ret; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 112 | |
| 113 | second = buf[0]; |
| 114 | minute = buf[1]; |
| 115 | hour = buf[2]; |
| 116 | week = buf[3]; |
| 117 | day = buf[4]; |
| 118 | month = buf[5]; |
| 119 | year = buf[6]; |
| 120 | |
| 121 | /* Extract additional information for AM/PM and century */ |
| 122 | |
| 123 | twelve_hr = hour & 0x40; |
| 124 | am_pm = hour & 0x20; |
| 125 | century = month & 0x80; |
| 126 | |
| 127 | /* Write to rtc_time structure */ |
| 128 | |
| 129 | time->tm_sec = bcd2bin(second); |
| 130 | time->tm_min = bcd2bin(minute); |
| 131 | if (twelve_hr) { |
| 132 | /* Convert to 24 hr */ |
| 133 | if (am_pm) |
| 134 | time->tm_hour = bcd2bin(hour & 0x1F) + 12; |
| 135 | else |
| 136 | time->tm_hour = bcd2bin(hour & 0x1F); |
| 137 | } else { |
| 138 | time->tm_hour = bcd2bin(hour); |
| 139 | } |
| 140 | |
Lei Xu | a2d6d2f | 2011-02-25 14:44:23 -0800 | [diff] [blame] | 141 | /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */ |
| 142 | time->tm_wday = bcd2bin(week) - 1; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 143 | time->tm_mday = bcd2bin(day); |
Lei Xu | a2d6d2f | 2011-02-25 14:44:23 -0800 | [diff] [blame] | 144 | /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */ |
| 145 | time->tm_mon = bcd2bin(month & 0x7F) - 1; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 146 | if (century) |
| 147 | add_century = 100; |
| 148 | |
| 149 | time->tm_year = bcd2bin(year) + add_century; |
| 150 | |
| 151 | return rtc_valid_tm(time); |
| 152 | } |
| 153 | |
| 154 | static int ds3232_set_time(struct device *dev, struct rtc_time *time) |
| 155 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 156 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 157 | u8 buf[7]; |
| 158 | |
| 159 | /* Extract time from rtc_time and load into ds3232*/ |
| 160 | |
| 161 | buf[0] = bin2bcd(time->tm_sec); |
| 162 | buf[1] = bin2bcd(time->tm_min); |
| 163 | buf[2] = bin2bcd(time->tm_hour); |
Lei Xu | a2d6d2f | 2011-02-25 14:44:23 -0800 | [diff] [blame] | 164 | /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */ |
| 165 | buf[3] = bin2bcd(time->tm_wday + 1); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 166 | buf[4] = bin2bcd(time->tm_mday); /* Date */ |
Lei Xu | a2d6d2f | 2011-02-25 14:44:23 -0800 | [diff] [blame] | 167 | /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */ |
| 168 | buf[5] = bin2bcd(time->tm_mon + 1); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 169 | if (time->tm_year >= 100) { |
| 170 | buf[5] |= 0x80; |
| 171 | buf[6] = bin2bcd(time->tm_year - 100); |
| 172 | } else { |
| 173 | buf[6] = bin2bcd(time->tm_year); |
| 174 | } |
| 175 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 176 | return regmap_bulk_write(ds3232->regmap, DS3232_REG_SECONDS, buf, 7); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 179 | /* |
| 180 | * DS3232 has two alarm, we only use alarm1 |
| 181 | * According to linux specification, only support one-shot alarm |
| 182 | * no periodic alarm mode |
| 183 | */ |
| 184 | static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 185 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 186 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 187 | int control, stat; |
| 188 | int ret; |
| 189 | u8 buf[4]; |
| 190 | |
| 191 | mutex_lock(&ds3232->mutex); |
| 192 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 193 | ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat); |
| 194 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 195 | goto out; |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 196 | ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control); |
| 197 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 198 | goto out; |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 199 | ret = regmap_bulk_read(ds3232->regmap, DS3232_REG_ALARM1, buf, 4); |
| 200 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 201 | goto out; |
| 202 | |
| 203 | alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F); |
| 204 | alarm->time.tm_min = bcd2bin(buf[1] & 0x7F); |
| 205 | alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F); |
| 206 | alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F); |
| 207 | |
| 208 | alarm->time.tm_mon = -1; |
| 209 | alarm->time.tm_year = -1; |
| 210 | alarm->time.tm_wday = -1; |
| 211 | alarm->time.tm_yday = -1; |
| 212 | alarm->time.tm_isdst = -1; |
| 213 | |
| 214 | alarm->enabled = !!(control & DS3232_REG_CR_A1IE); |
| 215 | alarm->pending = !!(stat & DS3232_REG_SR_A1F); |
| 216 | |
| 217 | ret = 0; |
| 218 | out: |
| 219 | mutex_unlock(&ds3232->mutex); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * linux rtc-module does not support wday alarm |
| 225 | * and only 24h time mode supported indeed |
| 226 | */ |
| 227 | static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 228 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 229 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 230 | int control, stat; |
| 231 | int ret; |
| 232 | u8 buf[4]; |
| 233 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 234 | if (ds3232->irq <= 0) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 235 | return -EINVAL; |
| 236 | |
| 237 | mutex_lock(&ds3232->mutex); |
| 238 | |
| 239 | buf[0] = bin2bcd(alarm->time.tm_sec); |
| 240 | buf[1] = bin2bcd(alarm->time.tm_min); |
| 241 | buf[2] = bin2bcd(alarm->time.tm_hour); |
| 242 | buf[3] = bin2bcd(alarm->time.tm_mday); |
| 243 | |
| 244 | /* clear alarm interrupt enable bit */ |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 245 | ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control); |
| 246 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 247 | goto out; |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 248 | control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE); |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 249 | ret = regmap_write(ds3232->regmap, DS3232_REG_CR, control); |
| 250 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 251 | goto out; |
| 252 | |
| 253 | /* clear any pending alarm flag */ |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 254 | ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat); |
| 255 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 256 | goto out; |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 257 | stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F); |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 258 | ret = regmap_write(ds3232->regmap, DS3232_REG_SR, stat); |
| 259 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 260 | goto out; |
| 261 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 262 | ret = regmap_bulk_write(ds3232->regmap, DS3232_REG_ALARM1, buf, 4); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 263 | |
| 264 | if (alarm->enabled) { |
| 265 | control |= DS3232_REG_CR_A1IE; |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 266 | ret = regmap_write(ds3232->regmap, DS3232_REG_CR, control); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 267 | } |
| 268 | out: |
| 269 | mutex_unlock(&ds3232->mutex); |
| 270 | return ret; |
| 271 | } |
| 272 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 273 | static void ds3232_update_alarm(struct device *dev) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 274 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 275 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 276 | int control; |
| 277 | int ret; |
| 278 | u8 buf[4]; |
| 279 | |
| 280 | mutex_lock(&ds3232->mutex); |
| 281 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 282 | ret = regmap_bulk_read(ds3232->regmap, DS3232_REG_ALARM1, buf, 4); |
| 283 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 284 | goto unlock; |
| 285 | |
| 286 | buf[0] = bcd2bin(buf[0]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ? |
| 287 | 0x80 : buf[0]; |
| 288 | buf[1] = bcd2bin(buf[1]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ? |
| 289 | 0x80 : buf[1]; |
| 290 | buf[2] = bcd2bin(buf[2]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ? |
| 291 | 0x80 : buf[2]; |
| 292 | buf[3] = bcd2bin(buf[3]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ? |
| 293 | 0x80 : buf[3]; |
| 294 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 295 | ret = regmap_bulk_write(ds3232->regmap, DS3232_REG_ALARM1, buf, 4); |
| 296 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 297 | goto unlock; |
| 298 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 299 | ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control); |
| 300 | if (ret) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 301 | goto unlock; |
| 302 | |
| 303 | if (ds3232->rtc->irq_data & (RTC_AF | RTC_UF)) |
| 304 | /* enable alarm1 interrupt */ |
| 305 | control |= DS3232_REG_CR_A1IE; |
| 306 | else |
| 307 | /* disable alarm1 interrupt */ |
| 308 | control &= ~(DS3232_REG_CR_A1IE); |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 309 | regmap_write(ds3232->regmap, DS3232_REG_CR, control); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 310 | |
| 311 | unlock: |
| 312 | mutex_unlock(&ds3232->mutex); |
| 313 | } |
| 314 | |
| 315 | static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 316 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 317 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 318 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 319 | if (ds3232->irq <= 0) |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
| 322 | if (enabled) |
| 323 | ds3232->rtc->irq_data |= RTC_AF; |
| 324 | else |
| 325 | ds3232->rtc->irq_data &= ~RTC_AF; |
| 326 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 327 | ds3232_update_alarm(dev); |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 331 | static irqreturn_t ds3232_irq(int irq, void *dev_id) |
| 332 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 333 | struct device *dev = dev_id; |
| 334 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 335 | |
| 336 | disable_irq_nosync(irq); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * If rtc as a wakeup source, can't schedule the work |
| 340 | * at system resume flow, because at this time the i2c bus |
| 341 | * has not been resumed. |
| 342 | */ |
| 343 | if (!ds3232->suspended) |
| 344 | schedule_work(&ds3232->work); |
| 345 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 346 | return IRQ_HANDLED; |
| 347 | } |
| 348 | |
| 349 | static void ds3232_work(struct work_struct *work) |
| 350 | { |
| 351 | struct ds3232 *ds3232 = container_of(work, struct ds3232, work); |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 352 | int ret; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 353 | int stat, control; |
| 354 | |
| 355 | mutex_lock(&ds3232->mutex); |
| 356 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 357 | ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat); |
| 358 | if (ret) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 359 | goto unlock; |
| 360 | |
| 361 | if (stat & DS3232_REG_SR_A1F) { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 362 | ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control); |
| 363 | if (ret) { |
Joe Perches | a737e835 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 364 | pr_warn("Read Control Register error - Disable IRQ%d\n", |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 365 | ds3232->irq); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 366 | } else { |
| 367 | /* disable alarm1 interrupt */ |
| 368 | control &= ~(DS3232_REG_CR_A1IE); |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 369 | regmap_write(ds3232->regmap, DS3232_REG_CR, control); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 370 | |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 371 | /* clear the alarm pend flag */ |
| 372 | stat &= ~DS3232_REG_SR_A1F; |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 373 | regmap_write(ds3232->regmap, DS3232_REG_SR, stat); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 374 | |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 375 | rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF); |
| 376 | |
| 377 | if (!ds3232->exiting) |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 378 | enable_irq(ds3232->irq); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 379 | } |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 382 | unlock: |
| 383 | mutex_unlock(&ds3232->mutex); |
| 384 | } |
| 385 | |
| 386 | static const struct rtc_class_ops ds3232_rtc_ops = { |
| 387 | .read_time = ds3232_read_time, |
| 388 | .set_time = ds3232_set_time, |
Lan Chunhe-B25806 | f46418c | 2010-10-27 15:33:12 -0700 | [diff] [blame] | 389 | .read_alarm = ds3232_read_alarm, |
| 390 | .set_alarm = ds3232_set_alarm, |
| 391 | .alarm_irq_enable = ds3232_alarm_irq_enable, |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 394 | static int ds3232_probe(struct device *dev, struct regmap *regmap, int irq, |
| 395 | const char *name) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 396 | { |
| 397 | struct ds3232 *ds3232; |
| 398 | int ret; |
| 399 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 400 | ds3232 = devm_kzalloc(dev, sizeof(*ds3232), GFP_KERNEL); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 401 | if (!ds3232) |
| 402 | return -ENOMEM; |
| 403 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 404 | ds3232->regmap = regmap; |
| 405 | ds3232->irq = irq; |
| 406 | ds3232->dev = dev; |
| 407 | dev_set_drvdata(dev, ds3232); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 408 | |
| 409 | INIT_WORK(&ds3232->work, ds3232_work); |
| 410 | mutex_init(&ds3232->mutex); |
| 411 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 412 | ret = ds3232_check_rtc_status(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 413 | if (ret) |
Sachin Kamat | 6671461 | 2013-04-29 16:20:31 -0700 | [diff] [blame] | 414 | return ret; |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 415 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 416 | if (ds3232->irq > 0) { |
| 417 | ret = devm_request_irq(dev, ds3232->irq, ds3232_irq, |
| 418 | IRQF_SHARED, name, dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 419 | if (ret) { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 420 | ds3232->irq = 0; |
| 421 | dev_err(dev, "unable to request IRQ\n"); |
| 422 | } else |
| 423 | device_init_wakeup(dev, 1); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 424 | } |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 425 | ds3232->rtc = devm_rtc_device_register(dev, name, &ds3232_rtc_ops, |
| 426 | THIS_MODULE); |
| 427 | |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 428 | return PTR_ERR_OR_ZERO(ds3232->rtc); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 431 | static int ds3232_remove(struct device *dev) |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 432 | { |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 433 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 434 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 435 | if (ds3232->irq > 0) { |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 436 | mutex_lock(&ds3232->mutex); |
| 437 | ds3232->exiting = 1; |
| 438 | mutex_unlock(&ds3232->mutex); |
| 439 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 440 | devm_free_irq(dev, ds3232->irq, dev); |
Tejun Heo | 9db8995 | 2010-12-24 16:00:17 +0100 | [diff] [blame] | 441 | cancel_work_sync(&ds3232->work); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 447 | #ifdef CONFIG_PM_SLEEP |
| 448 | static int ds3232_suspend(struct device *dev) |
| 449 | { |
| 450 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 451 | |
| 452 | if (device_can_wakeup(dev)) { |
| 453 | ds3232->suspended = true; |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 454 | if (irq_set_irq_wake(ds3232->irq, 1)) { |
Wang Dongsheng | dc2280e | 2015-08-12 17:14:13 +0800 | [diff] [blame] | 455 | dev_warn_once(dev, "Cannot set wakeup source\n"); |
| 456 | ds3232->suspended = false; |
| 457 | } |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int ds3232_resume(struct device *dev) |
| 464 | { |
| 465 | struct ds3232 *ds3232 = dev_get_drvdata(dev); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 466 | |
| 467 | if (ds3232->suspended) { |
| 468 | ds3232->suspended = false; |
| 469 | |
| 470 | /* Clear the hardware alarm pend flag */ |
| 471 | schedule_work(&ds3232->work); |
| 472 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 473 | irq_set_irq_wake(ds3232->irq, 0); |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | #endif |
| 479 | |
| 480 | static const struct dev_pm_ops ds3232_pm_ops = { |
| 481 | SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend, ds3232_resume) |
| 482 | }; |
| 483 | |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 484 | static int ds3232_i2c_probe(struct i2c_client *client, |
| 485 | const struct i2c_device_id *id) |
| 486 | { |
| 487 | struct regmap *regmap; |
| 488 | static const struct regmap_config config = { |
| 489 | .reg_bits = 8, |
| 490 | .val_bits = 8, |
| 491 | }; |
| 492 | |
| 493 | regmap = devm_regmap_init_i2c(client, &config); |
| 494 | if (IS_ERR(regmap)) { |
| 495 | dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", |
| 496 | __func__, PTR_ERR(regmap)); |
| 497 | return PTR_ERR(regmap); |
| 498 | } |
| 499 | |
| 500 | return ds3232_probe(&client->dev, regmap, client->irq, client->name); |
| 501 | } |
| 502 | |
| 503 | static int ds3232_i2c_remove(struct i2c_client *client) |
| 504 | { |
| 505 | return ds3232_remove(&client->dev); |
| 506 | } |
| 507 | |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 508 | static const struct i2c_device_id ds3232_id[] = { |
| 509 | { "ds3232", 0 }, |
| 510 | { } |
| 511 | }; |
| 512 | MODULE_DEVICE_TABLE(i2c, ds3232_id); |
| 513 | |
| 514 | static struct i2c_driver ds3232_driver = { |
| 515 | .driver = { |
| 516 | .name = "rtc-ds3232", |
Wang Dongsheng | c93a3ae2 | 2014-04-03 14:50:08 -0700 | [diff] [blame] | 517 | .pm = &ds3232_pm_ops, |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 518 | }, |
Akinobu Mita | 370927c | 2016-03-07 00:27:47 +0900 | [diff] [blame^] | 519 | .probe = ds3232_i2c_probe, |
| 520 | .remove = ds3232_i2c_remove, |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 521 | .id_table = ds3232_id, |
| 522 | }; |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 523 | module_i2c_driver(ds3232_driver); |
Roy Zang | c03675f | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 524 | |
| 525 | MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>"); |
| 526 | MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver"); |
| 527 | MODULE_LICENSE("GPL"); |