Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * rtc-isl12057 - Driver for Intersil ISL12057 I2C Real Time Clock |
| 3 | * |
| 4 | * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org> |
| 5 | * |
| 6 | * This work is largely based on Intersil ISL1208 driver developed by |
| 7 | * Hebert Valerio Riedel <hvr@gnu.org>. |
| 8 | * |
| 9 | * Detailed datasheet on which this development is based is available here: |
| 10 | * |
| 11 | * http://natisbad.org/NAS2/refs/ISL12057.pdf |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/rtc.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/bcd.h> |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_device.h> |
| 31 | #include <linux/regmap.h> |
| 32 | |
| 33 | #define DRV_NAME "rtc-isl12057" |
| 34 | |
| 35 | /* RTC section */ |
| 36 | #define ISL12057_REG_RTC_SC 0x00 /* Seconds */ |
| 37 | #define ISL12057_REG_RTC_MN 0x01 /* Minutes */ |
| 38 | #define ISL12057_REG_RTC_HR 0x02 /* Hours */ |
| 39 | #define ISL12057_REG_RTC_HR_PM BIT(5) /* AM/PM bit in 12h format */ |
| 40 | #define ISL12057_REG_RTC_HR_MIL BIT(6) /* 24h/12h format */ |
| 41 | #define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */ |
| 42 | #define ISL12057_REG_RTC_DT 0x04 /* Date */ |
| 43 | #define ISL12057_REG_RTC_MO 0x05 /* Month */ |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 44 | #define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */ |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 45 | #define ISL12057_REG_RTC_YR 0x06 /* Year */ |
| 46 | #define ISL12057_RTC_SEC_LEN 7 |
| 47 | |
| 48 | /* Alarm 1 section */ |
| 49 | #define ISL12057_REG_A1_SC 0x07 /* Alarm 1 Seconds */ |
| 50 | #define ISL12057_REG_A1_MN 0x08 /* Alarm 1 Minutes */ |
| 51 | #define ISL12057_REG_A1_HR 0x09 /* Alarm 1 Hours */ |
| 52 | #define ISL12057_REG_A1_HR_PM BIT(5) /* AM/PM bit in 12h format */ |
| 53 | #define ISL12057_REG_A1_HR_MIL BIT(6) /* 24h/12h format */ |
| 54 | #define ISL12057_REG_A1_DWDT 0x0A /* Alarm 1 Date / Day of the week */ |
| 55 | #define ISL12057_REG_A1_DWDT_B BIT(6) /* DW / DT selection bit */ |
| 56 | #define ISL12057_A1_SEC_LEN 4 |
| 57 | |
| 58 | /* Alarm 2 section */ |
| 59 | #define ISL12057_REG_A2_MN 0x0B /* Alarm 2 Minutes */ |
| 60 | #define ISL12057_REG_A2_HR 0x0C /* Alarm 2 Hours */ |
| 61 | #define ISL12057_REG_A2_DWDT 0x0D /* Alarm 2 Date / Day of the week */ |
| 62 | #define ISL12057_A2_SEC_LEN 3 |
| 63 | |
| 64 | /* Control/Status registers */ |
| 65 | #define ISL12057_REG_INT 0x0E |
| 66 | #define ISL12057_REG_INT_A1IE BIT(0) /* Alarm 1 interrupt enable bit */ |
| 67 | #define ISL12057_REG_INT_A2IE BIT(1) /* Alarm 2 interrupt enable bit */ |
| 68 | #define ISL12057_REG_INT_INTCN BIT(2) /* Interrupt control enable bit */ |
| 69 | #define ISL12057_REG_INT_RS1 BIT(3) /* Freq out control bit 1 */ |
| 70 | #define ISL12057_REG_INT_RS2 BIT(4) /* Freq out control bit 2 */ |
| 71 | #define ISL12057_REG_INT_EOSC BIT(7) /* Oscillator enable bit */ |
| 72 | |
| 73 | #define ISL12057_REG_SR 0x0F |
| 74 | #define ISL12057_REG_SR_A1F BIT(0) /* Alarm 1 interrupt bit */ |
| 75 | #define ISL12057_REG_SR_A2F BIT(1) /* Alarm 2 interrupt bit */ |
| 76 | #define ISL12057_REG_SR_OSF BIT(7) /* Oscillator failure bit */ |
| 77 | |
| 78 | /* Register memory map length */ |
| 79 | #define ISL12057_MEM_MAP_LEN 0x10 |
| 80 | |
| 81 | struct isl12057_rtc_data { |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 82 | struct rtc_device *rtc; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 83 | struct regmap *regmap; |
| 84 | struct mutex lock; |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 85 | int irq; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs) |
| 89 | { |
| 90 | tm->tm_sec = bcd2bin(regs[ISL12057_REG_RTC_SC]); |
| 91 | tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]); |
| 92 | |
| 93 | if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */ |
Arnaud Ebalard | 5945b28 | 2014-12-10 15:54:02 -0800 | [diff] [blame] | 94 | tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x1f); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 95 | if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM) |
| 96 | tm->tm_hour += 12; |
| 97 | } else { /* 24 hour mode */ |
| 98 | tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x3f); |
| 99 | } |
| 100 | |
| 101 | tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]); |
| 102 | tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */ |
Arnaud Ebalard | 5945b28 | 2014-12-10 15:54:02 -0800 | [diff] [blame] | 103 | tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO] & 0x1f) - 1; /* ditto */ |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 104 | tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100; |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 105 | |
| 106 | /* Check if years register has overflown from 99 to 00 */ |
| 107 | if (regs[ISL12057_REG_RTC_MO] & ISL12057_REG_RTC_MO_CEN) |
| 108 | tm->tm_year += 100; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm) |
| 112 | { |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 113 | u8 century_bit; |
| 114 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 115 | /* |
| 116 | * The clock has an 8 bit wide bcd-coded register for the year. |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 117 | * It also has a century bit encoded in MO flag which provides |
| 118 | * information about overflow of year register from 99 to 00. |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 119 | * tm_year is an offset from 1900 and we are interested in the |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 120 | * 2000-2199 range, so any value less than 100 or larger than |
| 121 | * 299 is invalid. |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 122 | */ |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 123 | if (tm->tm_year < 100 || tm->tm_year > 299) |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 126 | century_bit = (tm->tm_year > 199) ? ISL12057_REG_RTC_MO_CEN : 0; |
| 127 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 128 | regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec); |
| 129 | regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min); |
| 130 | regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */ |
| 131 | regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday); |
Arnaud Ebalard | b5f4184 | 2014-12-10 15:54:05 -0800 | [diff] [blame] | 132 | regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1) | century_bit; |
| 133 | regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year % 100); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 134 | regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Try and match register bits w/ fixed null values to see whether we |
| 141 | * are dealing with an ISL12057. Note: this function is called early |
| 142 | * during init and hence does need mutex protection. |
| 143 | */ |
| 144 | static int isl12057_i2c_validate_chip(struct regmap *regmap) |
| 145 | { |
| 146 | u8 regs[ISL12057_MEM_MAP_LEN]; |
| 147 | static const u8 mask[ISL12057_MEM_MAP_LEN] = { 0x80, 0x80, 0x80, 0xf8, |
| 148 | 0xc0, 0x60, 0x00, 0x00, |
| 149 | 0x00, 0x00, 0x00, 0x00, |
| 150 | 0x00, 0x00, 0x60, 0x7c }; |
| 151 | int ret, i; |
| 152 | |
| 153 | ret = regmap_bulk_read(regmap, 0, regs, ISL12057_MEM_MAP_LEN); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | for (i = 0; i < ISL12057_MEM_MAP_LEN; ++i) { |
| 158 | if (regs[i] & mask[i]) /* check if bits are cleared */ |
| 159 | return -ENODEV; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 165 | static int _isl12057_rtc_clear_alarm(struct device *dev) |
| 166 | { |
| 167 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 168 | int ret; |
| 169 | |
| 170 | ret = regmap_update_bits(data->regmap, ISL12057_REG_SR, |
| 171 | ISL12057_REG_SR_A1F, 0); |
| 172 | if (ret) |
| 173 | dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static int _isl12057_rtc_update_alarm(struct device *dev, int enable) |
| 179 | { |
| 180 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 181 | int ret; |
| 182 | |
| 183 | ret = regmap_update_bits(data->regmap, ISL12057_REG_INT, |
| 184 | ISL12057_REG_INT_A1IE, |
| 185 | enable ? ISL12057_REG_INT_A1IE : 0); |
| 186 | if (ret) |
| 187 | dev_err(dev, "%s: changing alarm interrupt flag failed (%d)\n", |
| 188 | __func__, ret); |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Note: as we only read from device and do not perform any update, there is |
| 195 | * no need for an equivalent function which would try and get driver's main |
| 196 | * lock. Here, it is safe for everyone if we just use regmap internal lock |
| 197 | * on the device when reading. |
| 198 | */ |
| 199 | static int _isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 200 | { |
| 201 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 202 | u8 regs[ISL12057_RTC_SEC_LEN]; |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 203 | unsigned int sr; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 204 | int ret; |
| 205 | |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 206 | ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr); |
| 207 | if (ret) { |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 208 | dev_err(dev, "%s: unable to read oscillator status flag (%d)\n", |
| 209 | __func__, ret); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 210 | goto out; |
| 211 | } else { |
| 212 | if (sr & ISL12057_REG_SR_OSF) { |
| 213 | ret = -ENODATA; |
| 214 | goto out; |
| 215 | } |
| 216 | } |
| 217 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 218 | ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs, |
| 219 | ISL12057_RTC_SEC_LEN); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 220 | if (ret) |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 221 | dev_err(dev, "%s: unable to read RTC time section (%d)\n", |
| 222 | __func__, ret); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 223 | |
| 224 | out: |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 225 | if (ret) |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 226 | return ret; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 227 | |
| 228 | isl12057_rtc_regs_to_tm(tm, regs); |
| 229 | |
| 230 | return rtc_valid_tm(tm); |
| 231 | } |
| 232 | |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 233 | static int isl12057_rtc_update_alarm(struct device *dev, int enable) |
| 234 | { |
| 235 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 236 | int ret; |
| 237 | |
| 238 | mutex_lock(&data->lock); |
| 239 | ret = _isl12057_rtc_update_alarm(dev, enable); |
| 240 | mutex_unlock(&data->lock); |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int isl12057_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 246 | { |
| 247 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 248 | struct rtc_time rtc_tm, *alarm_tm = &alarm->time; |
| 249 | unsigned long rtc_secs, alarm_secs; |
| 250 | u8 regs[ISL12057_A1_SEC_LEN]; |
| 251 | unsigned int ir; |
| 252 | int ret; |
| 253 | |
| 254 | mutex_lock(&data->lock); |
| 255 | ret = regmap_bulk_read(data->regmap, ISL12057_REG_A1_SC, regs, |
| 256 | ISL12057_A1_SEC_LEN); |
| 257 | if (ret) { |
| 258 | dev_err(dev, "%s: reading alarm section failed (%d)\n", |
| 259 | __func__, ret); |
| 260 | goto err_unlock; |
| 261 | } |
| 262 | |
| 263 | alarm_tm->tm_sec = bcd2bin(regs[0] & 0x7f); |
| 264 | alarm_tm->tm_min = bcd2bin(regs[1] & 0x7f); |
| 265 | alarm_tm->tm_hour = bcd2bin(regs[2] & 0x3f); |
| 266 | alarm_tm->tm_mday = bcd2bin(regs[3] & 0x3f); |
| 267 | alarm_tm->tm_wday = -1; |
| 268 | |
| 269 | /* |
| 270 | * The alarm section does not store year/month. We use the ones in rtc |
| 271 | * section as a basis and increment month and then year if needed to get |
| 272 | * alarm after current time. |
| 273 | */ |
| 274 | ret = _isl12057_rtc_read_time(dev, &rtc_tm); |
| 275 | if (ret) |
| 276 | goto err_unlock; |
| 277 | |
| 278 | alarm_tm->tm_year = rtc_tm.tm_year; |
| 279 | alarm_tm->tm_mon = rtc_tm.tm_mon; |
| 280 | |
| 281 | ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); |
| 282 | if (ret) |
| 283 | goto err_unlock; |
| 284 | |
| 285 | ret = rtc_tm_to_time(alarm_tm, &alarm_secs); |
| 286 | if (ret) |
| 287 | goto err_unlock; |
| 288 | |
| 289 | if (alarm_secs < rtc_secs) { |
| 290 | if (alarm_tm->tm_mon == 11) { |
| 291 | alarm_tm->tm_mon = 0; |
| 292 | alarm_tm->tm_year += 1; |
| 293 | } else { |
| 294 | alarm_tm->tm_mon += 1; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | ret = regmap_read(data->regmap, ISL12057_REG_INT, &ir); |
| 299 | if (ret) { |
| 300 | dev_err(dev, "%s: reading alarm interrupt flag failed (%d)\n", |
| 301 | __func__, ret); |
| 302 | goto err_unlock; |
| 303 | } |
| 304 | |
| 305 | alarm->enabled = !!(ir & ISL12057_REG_INT_A1IE); |
| 306 | |
| 307 | err_unlock: |
| 308 | mutex_unlock(&data->lock); |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | static int isl12057_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 314 | { |
| 315 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 316 | struct rtc_time *alarm_tm = &alarm->time; |
| 317 | unsigned long rtc_secs, alarm_secs; |
| 318 | u8 regs[ISL12057_A1_SEC_LEN]; |
| 319 | struct rtc_time rtc_tm; |
| 320 | int ret, enable = 1; |
| 321 | |
| 322 | mutex_lock(&data->lock); |
| 323 | ret = _isl12057_rtc_read_time(dev, &rtc_tm); |
| 324 | if (ret) |
| 325 | goto err_unlock; |
| 326 | |
| 327 | ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); |
| 328 | if (ret) |
| 329 | goto err_unlock; |
| 330 | |
| 331 | ret = rtc_tm_to_time(alarm_tm, &alarm_secs); |
| 332 | if (ret) |
| 333 | goto err_unlock; |
| 334 | |
| 335 | /* If alarm time is before current time, disable the alarm */ |
| 336 | if (!alarm->enabled || alarm_secs <= rtc_secs) { |
| 337 | enable = 0; |
| 338 | } else { |
| 339 | /* |
| 340 | * Chip only support alarms up to one month in the future. Let's |
| 341 | * return an error if we get something after that limit. |
| 342 | * Comparison is done by incrementing rtc_tm month field by one |
| 343 | * and checking alarm value is still below. |
| 344 | */ |
| 345 | if (rtc_tm.tm_mon == 11) { /* handle year wrapping */ |
| 346 | rtc_tm.tm_mon = 0; |
| 347 | rtc_tm.tm_year += 1; |
| 348 | } else { |
| 349 | rtc_tm.tm_mon += 1; |
| 350 | } |
| 351 | |
| 352 | ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); |
| 353 | if (ret) |
| 354 | goto err_unlock; |
| 355 | |
| 356 | if (alarm_secs > rtc_secs) { |
| 357 | dev_err(dev, "%s: max for alarm is one month (%d)\n", |
| 358 | __func__, ret); |
| 359 | ret = -EINVAL; |
| 360 | goto err_unlock; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* Disable the alarm before modifying it */ |
| 365 | ret = _isl12057_rtc_update_alarm(dev, 0); |
| 366 | if (ret < 0) { |
| 367 | dev_err(dev, "%s: unable to disable the alarm (%d)\n", |
| 368 | __func__, ret); |
| 369 | goto err_unlock; |
| 370 | } |
| 371 | |
| 372 | /* Program alarm registers */ |
| 373 | regs[0] = bin2bcd(alarm_tm->tm_sec) & 0x7f; |
| 374 | regs[1] = bin2bcd(alarm_tm->tm_min) & 0x7f; |
| 375 | regs[2] = bin2bcd(alarm_tm->tm_hour) & 0x3f; |
| 376 | regs[3] = bin2bcd(alarm_tm->tm_mday) & 0x3f; |
| 377 | |
| 378 | ret = regmap_bulk_write(data->regmap, ISL12057_REG_A1_SC, regs, |
| 379 | ISL12057_A1_SEC_LEN); |
| 380 | if (ret < 0) { |
| 381 | dev_err(dev, "%s: writing alarm section failed (%d)\n", |
| 382 | __func__, ret); |
| 383 | goto err_unlock; |
| 384 | } |
| 385 | |
| 386 | /* Enable or disable alarm */ |
| 387 | ret = _isl12057_rtc_update_alarm(dev, enable); |
| 388 | |
| 389 | err_unlock: |
| 390 | mutex_unlock(&data->lock); |
| 391 | |
| 392 | return ret; |
| 393 | } |
| 394 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 395 | static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 396 | { |
| 397 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 398 | u8 regs[ISL12057_RTC_SEC_LEN]; |
| 399 | int ret; |
| 400 | |
| 401 | ret = isl12057_rtc_tm_to_regs(regs, tm); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | |
| 405 | mutex_lock(&data->lock); |
| 406 | ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs, |
| 407 | ISL12057_RTC_SEC_LEN); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 408 | if (ret) { |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 409 | dev_err(dev, "%s: unable to write RTC time section (%d)\n", |
| 410 | __func__, ret); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 411 | goto out; |
| 412 | } |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 413 | |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 414 | /* |
| 415 | * Now that RTC time has been updated, let's clear oscillator |
| 416 | * failure flag, if needed. |
| 417 | */ |
| 418 | ret = regmap_update_bits(data->regmap, ISL12057_REG_SR, |
| 419 | ISL12057_REG_SR_OSF, 0); |
| 420 | if (ret < 0) |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 421 | dev_err(dev, "%s: unable to clear osc. failure bit (%d)\n", |
| 422 | __func__, ret); |
Arnaud Ebalard | 10df1e6 | 2014-12-10 15:54:08 -0800 | [diff] [blame] | 423 | |
| 424 | out: |
| 425 | mutex_unlock(&data->lock); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Check current RTC status and enable/disable what needs to be. Return 0 if |
| 432 | * everything went ok and a negative value upon error. Note: this function |
| 433 | * is called early during init and hence does need mutex protection. |
| 434 | */ |
| 435 | static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap) |
| 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | /* Enable oscillator if not already running */ |
| 440 | ret = regmap_update_bits(regmap, ISL12057_REG_INT, |
| 441 | ISL12057_REG_INT_EOSC, 0); |
| 442 | if (ret < 0) { |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 443 | dev_err(dev, "%s: unable to enable oscillator (%d)\n", |
| 444 | __func__, ret); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 448 | /* Clear alarm bit if needed */ |
| 449 | ret = regmap_update_bits(regmap, ISL12057_REG_SR, |
| 450 | ISL12057_REG_SR_A1F, 0); |
| 451 | if (ret < 0) { |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 452 | dev_err(dev, "%s: unable to clear alarm bit (%d)\n", |
| 453 | __func__, ret); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Arnaud Ebalard | 298ff01 | 2015-02-13 14:40:35 -0800 | [diff] [blame] | 460 | #ifdef CONFIG_OF |
| 461 | /* |
| 462 | * One would expect the device to be marked as a wakeup source only |
| 463 | * when an IRQ pin of the RTC is routed to an interrupt line of the |
| 464 | * CPU. In practice, such an IRQ pin can be connected to a PMIC and |
| 465 | * this allows the device to be powered up when RTC alarm rings. This |
| 466 | * is for instance the case on ReadyNAS 102, 104 and 2120. On those |
| 467 | * devices with no IRQ driectly connected to the SoC, the RTC chip |
| 468 | * can be forced as a wakeup source by stating that explicitly in |
| 469 | * the device's .dts file using the "isil,irq2-can-wakeup-machine" |
| 470 | * boolean property. This will guarantee 'wakealarm' sysfs entry is |
| 471 | * available on the device. |
| 472 | * |
| 473 | * The function below returns 1, i.e. the capability of the chip to |
| 474 | * wakeup the device, based on IRQ availability or if the boolean |
| 475 | * property has been set in the .dts file. Otherwise, it returns 0. |
| 476 | */ |
| 477 | |
| 478 | static bool isl12057_can_wakeup_machine(struct device *dev) |
| 479 | { |
| 480 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 481 | |
| 482 | return (data->irq || of_property_read_bool(dev->of_node, |
| 483 | "isil,irq2-can-wakeup-machine")); |
| 484 | } |
| 485 | #else |
| 486 | static bool isl12057_can_wakeup_machine(struct device *dev) |
| 487 | { |
| 488 | struct isl12057_rtc_data *data = dev_get_drvdata(dev); |
| 489 | |
| 490 | return !!data->irq; |
| 491 | } |
| 492 | #endif |
| 493 | |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 494 | static int isl12057_rtc_alarm_irq_enable(struct device *dev, |
| 495 | unsigned int enable) |
| 496 | { |
| 497 | struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 498 | int ret = -ENOTTY; |
| 499 | |
| 500 | if (rtc_data->irq) |
| 501 | ret = isl12057_rtc_update_alarm(dev, enable); |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | static irqreturn_t isl12057_rtc_interrupt(int irq, void *data) |
| 507 | { |
| 508 | struct i2c_client *client = data; |
| 509 | struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev); |
| 510 | struct rtc_device *rtc = rtc_data->rtc; |
| 511 | int ret, handled = IRQ_NONE; |
| 512 | unsigned int sr; |
| 513 | |
| 514 | ret = regmap_read(rtc_data->regmap, ISL12057_REG_SR, &sr); |
| 515 | if (!ret && (sr & ISL12057_REG_SR_A1F)) { |
| 516 | dev_dbg(&client->dev, "RTC alarm!\n"); |
| 517 | |
| 518 | rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); |
| 519 | |
| 520 | /* Acknowledge and disable the alarm */ |
| 521 | _isl12057_rtc_clear_alarm(&client->dev); |
| 522 | _isl12057_rtc_update_alarm(&client->dev, 0); |
| 523 | |
| 524 | handled = IRQ_HANDLED; |
| 525 | } |
| 526 | |
| 527 | return handled; |
| 528 | } |
| 529 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 530 | static const struct rtc_class_ops rtc_ops = { |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 531 | .read_time = _isl12057_rtc_read_time, |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 532 | .set_time = isl12057_rtc_set_time, |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 533 | .read_alarm = isl12057_rtc_read_alarm, |
| 534 | .set_alarm = isl12057_rtc_set_alarm, |
| 535 | .alarm_irq_enable = isl12057_rtc_alarm_irq_enable, |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 536 | }; |
| 537 | |
Krzysztof Kozlowski | 1ef2816 | 2015-02-13 14:40:51 -0800 | [diff] [blame] | 538 | static const struct regmap_config isl12057_rtc_regmap_config = { |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 539 | .reg_bits = 8, |
| 540 | .val_bits = 8, |
| 541 | }; |
| 542 | |
| 543 | static int isl12057_probe(struct i2c_client *client, |
| 544 | const struct i2c_device_id *id) |
| 545 | { |
| 546 | struct device *dev = &client->dev; |
| 547 | struct isl12057_rtc_data *data; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 548 | struct regmap *regmap; |
| 549 | int ret; |
| 550 | |
| 551 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | |
| 552 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 553 | I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 554 | return -ENODEV; |
| 555 | |
| 556 | regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config); |
| 557 | if (IS_ERR(regmap)) { |
| 558 | ret = PTR_ERR(regmap); |
Arnaud Ebalard | cf67d0b | 2014-12-10 15:54:11 -0800 | [diff] [blame] | 559 | dev_err(dev, "%s: regmap allocation failed (%d)\n", |
| 560 | __func__, ret); |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | ret = isl12057_i2c_validate_chip(regmap); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | ret = isl12057_check_rtc_status(dev, regmap); |
| 569 | if (ret) |
| 570 | return ret; |
| 571 | |
| 572 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 573 | if (!data) |
| 574 | return -ENOMEM; |
| 575 | |
| 576 | mutex_init(&data->lock); |
| 577 | data->regmap = regmap; |
| 578 | dev_set_drvdata(dev, data); |
| 579 | |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 580 | if (client->irq > 0) { |
| 581 | ret = devm_request_threaded_irq(dev, client->irq, NULL, |
| 582 | isl12057_rtc_interrupt, |
| 583 | IRQF_SHARED|IRQF_ONESHOT, |
| 584 | DRV_NAME, client); |
| 585 | if (!ret) |
| 586 | data->irq = client->irq; |
| 587 | else |
| 588 | dev_err(dev, "%s: irq %d unavailable (%d)\n", __func__, |
| 589 | client->irq, ret); |
| 590 | } |
| 591 | |
Arnaud Ebalard | 298ff01 | 2015-02-13 14:40:35 -0800 | [diff] [blame] | 592 | if (isl12057_can_wakeup_machine(dev)) |
| 593 | device_init_wakeup(dev, true); |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 594 | |
| 595 | data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops, |
| 596 | THIS_MODULE); |
| 597 | ret = PTR_ERR_OR_ZERO(data->rtc); |
| 598 | if (ret) { |
| 599 | dev_err(dev, "%s: unable to register RTC device (%d)\n", |
| 600 | __func__, ret); |
| 601 | goto err; |
| 602 | } |
| 603 | |
| 604 | /* We cannot support UIE mode if we do not have an IRQ line */ |
| 605 | if (!data->irq) |
| 606 | data->rtc->uie_unsupported = 1; |
| 607 | |
| 608 | err: |
| 609 | return ret; |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 612 | static int isl12057_remove(struct i2c_client *client) |
| 613 | { |
Arnaud Ebalard | 298ff01 | 2015-02-13 14:40:35 -0800 | [diff] [blame] | 614 | if (isl12057_can_wakeup_machine(&client->dev)) |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 615 | device_init_wakeup(&client->dev, false); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | #ifdef CONFIG_PM_SLEEP |
| 621 | static int isl12057_rtc_suspend(struct device *dev) |
| 622 | { |
| 623 | struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 624 | |
Arnaud Ebalard | 298ff01 | 2015-02-13 14:40:35 -0800 | [diff] [blame] | 625 | if (rtc_data->irq && device_may_wakeup(dev)) |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 626 | return enable_irq_wake(rtc_data->irq); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int isl12057_rtc_resume(struct device *dev) |
| 632 | { |
| 633 | struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 634 | |
Arnaud Ebalard | 298ff01 | 2015-02-13 14:40:35 -0800 | [diff] [blame] | 635 | if (rtc_data->irq && device_may_wakeup(dev)) |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 636 | return disable_irq_wake(rtc_data->irq); |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | #endif |
| 641 | |
| 642 | static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops, isl12057_rtc_suspend, |
| 643 | isl12057_rtc_resume); |
| 644 | |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 645 | #ifdef CONFIG_OF |
Jingoo Han | 8c5bfac | 2014-06-06 14:35:56 -0700 | [diff] [blame] | 646 | static const struct of_device_id isl12057_dt_match[] = { |
Arnaud Ebalard | 401b3c6 | 2015-02-16 15:58:35 -0800 | [diff] [blame] | 647 | { .compatible = "isl,isl12057" }, /* for backward compat., don't use */ |
| 648 | { .compatible = "isil,isl12057" }, |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 649 | { }, |
| 650 | }; |
| 651 | #endif |
| 652 | |
| 653 | static const struct i2c_device_id isl12057_id[] = { |
| 654 | { "isl12057", 0 }, |
| 655 | { } |
| 656 | }; |
| 657 | MODULE_DEVICE_TABLE(i2c, isl12057_id); |
| 658 | |
| 659 | static struct i2c_driver isl12057_driver = { |
| 660 | .driver = { |
| 661 | .name = DRV_NAME, |
| 662 | .owner = THIS_MODULE, |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 663 | .pm = &isl12057_rtc_pm_ops, |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 664 | .of_match_table = of_match_ptr(isl12057_dt_match), |
| 665 | }, |
| 666 | .probe = isl12057_probe, |
Arnaud Ebalard | fd71493 | 2015-02-13 14:40:32 -0800 | [diff] [blame] | 667 | .remove = isl12057_remove, |
Arnaud Ebalard | 70e1233 | 2013-12-19 23:27:28 +0100 | [diff] [blame] | 668 | .id_table = isl12057_id, |
| 669 | }; |
| 670 | module_i2c_driver(isl12057_driver); |
| 671 | |
| 672 | MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>"); |
| 673 | MODULE_DESCRIPTION("Intersil ISL12057 RTC driver"); |
| 674 | MODULE_LICENSE("GPL"); |