Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Intersil ISL1208 rtc class driver
0004  *
0005  * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
0006  */
0007 
0008 #include <linux/bcd.h>
0009 #include <linux/i2c.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/of_irq.h>
0013 #include <linux/rtc.h>
0014 
0015 /* Register map */
0016 /* rtc section */
0017 #define ISL1208_REG_SC  0x00
0018 #define ISL1208_REG_MN  0x01
0019 #define ISL1208_REG_HR  0x02
0020 #define ISL1208_REG_HR_MIL     (1<<7)   /* 24h/12h mode */
0021 #define ISL1208_REG_HR_PM      (1<<5)   /* PM/AM bit in 12h mode */
0022 #define ISL1208_REG_DT  0x03
0023 #define ISL1208_REG_MO  0x04
0024 #define ISL1208_REG_YR  0x05
0025 #define ISL1208_REG_DW  0x06
0026 #define ISL1208_RTC_SECTION_LEN 7
0027 
0028 /* control/status section */
0029 #define ISL1208_REG_SR  0x07
0030 #define ISL1208_REG_SR_ARST    (1<<7)   /* auto reset */
0031 #define ISL1208_REG_SR_XTOSCB  (1<<6)   /* crystal oscillator */
0032 #define ISL1208_REG_SR_WRTC    (1<<4)   /* write rtc */
0033 #define ISL1208_REG_SR_EVT     (1<<3)   /* event */
0034 #define ISL1208_REG_SR_ALM     (1<<2)   /* alarm */
0035 #define ISL1208_REG_SR_BAT     (1<<1)   /* battery */
0036 #define ISL1208_REG_SR_RTCF    (1<<0)   /* rtc fail */
0037 #define ISL1208_REG_INT 0x08
0038 #define ISL1208_REG_INT_ALME   (1<<6)   /* alarm enable */
0039 #define ISL1208_REG_INT_IM     (1<<7)   /* interrupt/alarm mode */
0040 #define ISL1219_REG_EV  0x09
0041 #define ISL1219_REG_EV_EVEN    (1<<4)   /* event detection enable */
0042 #define ISL1219_REG_EV_EVIENB  (1<<7)   /* event in pull-up disable */
0043 #define ISL1208_REG_ATR 0x0a
0044 #define ISL1208_REG_DTR 0x0b
0045 
0046 /* alarm section */
0047 #define ISL1208_REG_SCA 0x0c
0048 #define ISL1208_REG_MNA 0x0d
0049 #define ISL1208_REG_HRA 0x0e
0050 #define ISL1208_REG_DTA 0x0f
0051 #define ISL1208_REG_MOA 0x10
0052 #define ISL1208_REG_DWA 0x11
0053 #define ISL1208_ALARM_SECTION_LEN 6
0054 
0055 /* user section */
0056 #define ISL1208_REG_USR1 0x12
0057 #define ISL1208_REG_USR2 0x13
0058 #define ISL1208_USR_SECTION_LEN 2
0059 
0060 /* event section */
0061 #define ISL1219_REG_SCT 0x14
0062 #define ISL1219_REG_MNT 0x15
0063 #define ISL1219_REG_HRT 0x16
0064 #define ISL1219_REG_DTT 0x17
0065 #define ISL1219_REG_MOT 0x18
0066 #define ISL1219_REG_YRT 0x19
0067 #define ISL1219_EVT_SECTION_LEN 6
0068 
0069 static struct i2c_driver isl1208_driver;
0070 
0071 /* ISL1208 various variants */
0072 enum isl1208_id {
0073     TYPE_ISL1208 = 0,
0074     TYPE_ISL1209,
0075     TYPE_ISL1218,
0076     TYPE_ISL1219,
0077     ISL_LAST_ID
0078 };
0079 
0080 /* Chip capabilities table */
0081 static const struct isl1208_config {
0082     const char  name[8];
0083     unsigned int    nvmem_length;
0084     unsigned    has_tamper:1;
0085     unsigned    has_timestamp:1;
0086 } isl1208_configs[] = {
0087     [TYPE_ISL1208] = { "isl1208", 2, false, false },
0088     [TYPE_ISL1209] = { "isl1209", 2, true,  false },
0089     [TYPE_ISL1218] = { "isl1218", 8, false, false },
0090     [TYPE_ISL1219] = { "isl1219", 2, true,  true },
0091 };
0092 
0093 static const struct i2c_device_id isl1208_id[] = {
0094     { "isl1208", TYPE_ISL1208 },
0095     { "isl1209", TYPE_ISL1209 },
0096     { "isl1218", TYPE_ISL1218 },
0097     { "isl1219", TYPE_ISL1219 },
0098     { }
0099 };
0100 MODULE_DEVICE_TABLE(i2c, isl1208_id);
0101 
0102 static const __maybe_unused struct of_device_id isl1208_of_match[] = {
0103     { .compatible = "isil,isl1208", .data = &isl1208_configs[TYPE_ISL1208] },
0104     { .compatible = "isil,isl1209", .data = &isl1208_configs[TYPE_ISL1209] },
0105     { .compatible = "isil,isl1218", .data = &isl1208_configs[TYPE_ISL1218] },
0106     { .compatible = "isil,isl1219", .data = &isl1208_configs[TYPE_ISL1219] },
0107     { }
0108 };
0109 MODULE_DEVICE_TABLE(of, isl1208_of_match);
0110 
0111 /* Device state */
0112 struct isl1208_state {
0113     struct nvmem_config nvmem_config;
0114     struct rtc_device *rtc;
0115     const struct isl1208_config *config;
0116 };
0117 
0118 /* block read */
0119 static int
0120 isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
0121               unsigned len)
0122 {
0123     int ret;
0124 
0125     WARN_ON(reg > ISL1219_REG_YRT);
0126     WARN_ON(reg + len > ISL1219_REG_YRT + 1);
0127 
0128     ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
0129     return (ret < 0) ? ret : 0;
0130 }
0131 
0132 /* block write */
0133 static int
0134 isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
0135              unsigned len)
0136 {
0137     int ret;
0138 
0139     WARN_ON(reg > ISL1219_REG_YRT);
0140     WARN_ON(reg + len > ISL1219_REG_YRT + 1);
0141 
0142     ret = i2c_smbus_write_i2c_block_data(client, reg, len, buf);
0143     return (ret < 0) ? ret : 0;
0144 }
0145 
0146 /* simple check to see whether we have a isl1208 */
0147 static int
0148 isl1208_i2c_validate_client(struct i2c_client *client)
0149 {
0150     u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
0151     u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
0152         0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
0153     };
0154     int i;
0155     int ret;
0156 
0157     ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
0158     if (ret < 0)
0159         return ret;
0160 
0161     for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
0162         if (regs[i] & zero_mask[i]) /* check if bits are cleared */
0163             return -ENODEV;
0164     }
0165 
0166     return 0;
0167 }
0168 
0169 static int
0170 isl1208_i2c_get_sr(struct i2c_client *client)
0171 {
0172     return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
0173 }
0174 
0175 static int
0176 isl1208_i2c_get_atr(struct i2c_client *client)
0177 {
0178     int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
0179     if (atr < 0)
0180         return atr;
0181 
0182     /* The 6bit value in the ATR register controls the load
0183      * capacitance C_load * in steps of 0.25pF
0184      *
0185      * bit (1<<5) of the ATR register is inverted
0186      *
0187      * C_load(ATR=0x20) =  4.50pF
0188      * C_load(ATR=0x00) = 12.50pF
0189      * C_load(ATR=0x1f) = 20.25pF
0190      *
0191      */
0192 
0193     atr &= 0x3f;        /* mask out lsb */
0194     atr ^= 1 << 5;      /* invert 6th bit */
0195     atr += 2 * 9;       /* add offset of 4.5pF; unit[atr] = 0.25pF */
0196 
0197     return atr;
0198 }
0199 
0200 /* returns adjustment value + 100 */
0201 static int
0202 isl1208_i2c_get_dtr(struct i2c_client *client)
0203 {
0204     int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
0205     if (dtr < 0)
0206         return -EIO;
0207 
0208     /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
0209     dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
0210 
0211     return dtr + 100;
0212 }
0213 
0214 static int
0215 isl1208_i2c_get_usr(struct i2c_client *client)
0216 {
0217     u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
0218     int ret;
0219 
0220     ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
0221                     ISL1208_USR_SECTION_LEN);
0222     if (ret < 0)
0223         return ret;
0224 
0225     return (buf[1] << 8) | buf[0];
0226 }
0227 
0228 static int
0229 isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
0230 {
0231     u8 buf[ISL1208_USR_SECTION_LEN];
0232 
0233     buf[0] = usr & 0xff;
0234     buf[1] = (usr >> 8) & 0xff;
0235 
0236     return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
0237                     ISL1208_USR_SECTION_LEN);
0238 }
0239 
0240 static int
0241 isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
0242 {
0243     int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
0244 
0245     if (icr < 0) {
0246         dev_err(&client->dev, "%s: reading INT failed\n", __func__);
0247         return icr;
0248     }
0249 
0250     if (enable)
0251         icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
0252     else
0253         icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
0254 
0255     icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
0256     if (icr < 0) {
0257         dev_err(&client->dev, "%s: writing INT failed\n", __func__);
0258         return icr;
0259     }
0260 
0261     return 0;
0262 }
0263 
0264 static int
0265 isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
0266 {
0267     struct i2c_client *const client = to_i2c_client(dev);
0268     int sr, dtr, atr, usr;
0269 
0270     sr = isl1208_i2c_get_sr(client);
0271     if (sr < 0) {
0272         dev_err(&client->dev, "%s: reading SR failed\n", __func__);
0273         return sr;
0274     }
0275 
0276     seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
0277            (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
0278            (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
0279            (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
0280            (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
0281            (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
0282            (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
0283 
0284     seq_printf(seq, "batt_status\t: %s\n",
0285            (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
0286 
0287     dtr = isl1208_i2c_get_dtr(client);
0288     if (dtr >= 0)
0289         seq_printf(seq, "digital_trim\t: %d ppm\n", dtr - 100);
0290 
0291     atr = isl1208_i2c_get_atr(client);
0292     if (atr >= 0)
0293         seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
0294                atr >> 2, (atr & 0x3) * 25);
0295 
0296     usr = isl1208_i2c_get_usr(client);
0297     if (usr >= 0)
0298         seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
0299 
0300     return 0;
0301 }
0302 
0303 static int
0304 isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
0305 {
0306     int sr;
0307     u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
0308 
0309     sr = isl1208_i2c_get_sr(client);
0310     if (sr < 0) {
0311         dev_err(&client->dev, "%s: reading SR failed\n", __func__);
0312         return -EIO;
0313     }
0314 
0315     sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
0316     if (sr < 0) {
0317         dev_err(&client->dev, "%s: reading RTC section failed\n",
0318             __func__);
0319         return sr;
0320     }
0321 
0322     tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
0323     tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
0324 
0325     /* HR field has a more complex interpretation */
0326     {
0327         const u8 _hr = regs[ISL1208_REG_HR];
0328         if (_hr & ISL1208_REG_HR_MIL)   /* 24h format */
0329             tm->tm_hour = bcd2bin(_hr & 0x3f);
0330         else {
0331             /* 12h format */
0332             tm->tm_hour = bcd2bin(_hr & 0x1f);
0333             if (_hr & ISL1208_REG_HR_PM)    /* PM flag set */
0334                 tm->tm_hour += 12;
0335         }
0336     }
0337 
0338     tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
0339     tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
0340     tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
0341     tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
0342 
0343     return 0;
0344 }
0345 
0346 static int
0347 isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
0348 {
0349     struct rtc_time *const tm = &alarm->time;
0350     u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
0351     int icr, yr, sr = isl1208_i2c_get_sr(client);
0352 
0353     if (sr < 0) {
0354         dev_err(&client->dev, "%s: reading SR failed\n", __func__);
0355         return sr;
0356     }
0357 
0358     sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
0359                    ISL1208_ALARM_SECTION_LEN);
0360     if (sr < 0) {
0361         dev_err(&client->dev, "%s: reading alarm section failed\n",
0362             __func__);
0363         return sr;
0364     }
0365 
0366     /* MSB of each alarm register is an enable bit */
0367     tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
0368     tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
0369     tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
0370     tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
0371     tm->tm_mon =
0372         bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
0373     tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
0374 
0375     /* The alarm doesn't store the year so get it from the rtc section */
0376     yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
0377     if (yr < 0) {
0378         dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
0379         return yr;
0380     }
0381     tm->tm_year = bcd2bin(yr) + 100;
0382 
0383     icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
0384     if (icr < 0) {
0385         dev_err(&client->dev, "%s: reading INT failed\n", __func__);
0386         return icr;
0387     }
0388     alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
0389 
0390     return 0;
0391 }
0392 
0393 static int
0394 isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
0395 {
0396     struct rtc_time *alarm_tm = &alarm->time;
0397     u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
0398     const int offs = ISL1208_REG_SCA;
0399     struct rtc_time rtc_tm;
0400     int err, enable;
0401 
0402     err = isl1208_i2c_read_time(client, &rtc_tm);
0403     if (err)
0404         return err;
0405 
0406     /* If the alarm time is before the current time disable the alarm */
0407     if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
0408         enable = 0x00;
0409     else
0410         enable = 0x80;
0411 
0412     /* Program the alarm and enable it for each setting */
0413     regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
0414     regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
0415     regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
0416         ISL1208_REG_HR_MIL | enable;
0417 
0418     regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
0419     regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
0420     regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
0421 
0422     /* write ALARM registers */
0423     err = isl1208_i2c_set_regs(client, offs, regs,
0424                   ISL1208_ALARM_SECTION_LEN);
0425     if (err < 0) {
0426         dev_err(&client->dev, "%s: writing ALARM section failed\n",
0427             __func__);
0428         return err;
0429     }
0430 
0431     err = isl1208_rtc_toggle_alarm(client, enable);
0432     if (err)
0433         return err;
0434 
0435     return 0;
0436 }
0437 
0438 static int
0439 isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
0440 {
0441     return isl1208_i2c_read_time(to_i2c_client(dev), tm);
0442 }
0443 
0444 static int
0445 isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
0446 {
0447     int sr;
0448     u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
0449 
0450     /* The clock has an 8 bit wide bcd-coded register (they never learn)
0451      * for the year. tm_year is an offset from 1900 and we are interested
0452      * in the 2000-2099 range, so any value less than 100 is invalid.
0453      */
0454     if (tm->tm_year < 100)
0455         return -EINVAL;
0456 
0457     regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
0458     regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
0459     regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
0460 
0461     regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
0462     regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
0463     regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
0464 
0465     regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
0466 
0467     sr = isl1208_i2c_get_sr(client);
0468     if (sr < 0) {
0469         dev_err(&client->dev, "%s: reading SR failed\n", __func__);
0470         return sr;
0471     }
0472 
0473     /* set WRTC */
0474     sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
0475                        sr | ISL1208_REG_SR_WRTC);
0476     if (sr < 0) {
0477         dev_err(&client->dev, "%s: writing SR failed\n", __func__);
0478         return sr;
0479     }
0480 
0481     /* write RTC registers */
0482     sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
0483     if (sr < 0) {
0484         dev_err(&client->dev, "%s: writing RTC section failed\n",
0485             __func__);
0486         return sr;
0487     }
0488 
0489     /* clear WRTC again */
0490     sr = isl1208_i2c_get_sr(client);
0491     if (sr < 0) {
0492         dev_err(&client->dev, "%s: reading SR failed\n", __func__);
0493         return sr;
0494     }
0495     sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
0496                        sr & ~ISL1208_REG_SR_WRTC);
0497     if (sr < 0) {
0498         dev_err(&client->dev, "%s: writing SR failed\n", __func__);
0499         return sr;
0500     }
0501 
0502     return 0;
0503 }
0504 
0505 
0506 static int
0507 isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
0508 {
0509     return isl1208_i2c_set_time(to_i2c_client(dev), tm);
0510 }
0511 
0512 static int
0513 isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0514 {
0515     return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
0516 }
0517 
0518 static int
0519 isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0520 {
0521     return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
0522 }
0523 
0524 static ssize_t timestamp0_store(struct device *dev,
0525                 struct device_attribute *attr,
0526                 const char *buf, size_t count)
0527 {
0528     struct i2c_client *client = to_i2c_client(dev->parent);
0529     int sr;
0530 
0531     sr = isl1208_i2c_get_sr(client);
0532     if (sr < 0) {
0533         dev_err(dev, "%s: reading SR failed\n", __func__);
0534         return sr;
0535     }
0536 
0537     sr &= ~ISL1208_REG_SR_EVT;
0538 
0539     sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
0540     if (sr < 0)
0541         dev_err(dev, "%s: writing SR failed\n",
0542             __func__);
0543 
0544     return count;
0545 };
0546 
0547 static ssize_t timestamp0_show(struct device *dev,
0548                    struct device_attribute *attr, char *buf)
0549 {
0550     struct i2c_client *client = to_i2c_client(dev->parent);
0551     u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
0552     struct rtc_time tm;
0553     int sr;
0554 
0555     sr = isl1208_i2c_get_sr(client);
0556     if (sr < 0) {
0557         dev_err(dev, "%s: reading SR failed\n", __func__);
0558         return sr;
0559     }
0560 
0561     if (!(sr & ISL1208_REG_SR_EVT))
0562         return 0;
0563 
0564     sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
0565                    ISL1219_EVT_SECTION_LEN);
0566     if (sr < 0) {
0567         dev_err(dev, "%s: reading event section failed\n",
0568             __func__);
0569         return 0;
0570     }
0571 
0572     /* MSB of each alarm register is an enable bit */
0573     tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
0574     tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
0575     tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
0576     tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
0577     tm.tm_mon =
0578         bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
0579     tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
0580 
0581     sr = rtc_valid_tm(&tm);
0582     if (sr)
0583         return sr;
0584 
0585     return sprintf(buf, "%llu\n",
0586                 (unsigned long long)rtc_tm_to_time64(&tm));
0587 };
0588 
0589 static DEVICE_ATTR_RW(timestamp0);
0590 
0591 static irqreturn_t
0592 isl1208_rtc_interrupt(int irq, void *data)
0593 {
0594     unsigned long timeout = jiffies + msecs_to_jiffies(1000);
0595     struct i2c_client *client = data;
0596     struct isl1208_state *isl1208 = i2c_get_clientdata(client);
0597     int handled = 0, sr, err;
0598 
0599     /*
0600      * I2C reads get NAK'ed if we read straight away after an interrupt?
0601      * Using a mdelay/msleep didn't seem to help either, so we work around
0602      * this by continually trying to read the register for a short time.
0603      */
0604     while (1) {
0605         sr = isl1208_i2c_get_sr(client);
0606         if (sr >= 0)
0607             break;
0608 
0609         if (time_after(jiffies, timeout)) {
0610             dev_err(&client->dev, "%s: reading SR failed\n",
0611                 __func__);
0612             return sr;
0613         }
0614     }
0615 
0616     if (sr & ISL1208_REG_SR_ALM) {
0617         dev_dbg(&client->dev, "alarm!\n");
0618 
0619         rtc_update_irq(isl1208->rtc, 1, RTC_IRQF | RTC_AF);
0620 
0621         /* Clear the alarm */
0622         sr &= ~ISL1208_REG_SR_ALM;
0623         sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
0624         if (sr < 0)
0625             dev_err(&client->dev, "%s: writing SR failed\n",
0626                 __func__);
0627         else
0628             handled = 1;
0629 
0630         /* Disable the alarm */
0631         err = isl1208_rtc_toggle_alarm(client, 0);
0632         if (err)
0633             return err;
0634     }
0635 
0636     if (isl1208->config->has_tamper && (sr & ISL1208_REG_SR_EVT)) {
0637         dev_warn(&client->dev, "event detected");
0638         handled = 1;
0639         if (isl1208->config->has_timestamp)
0640             sysfs_notify(&isl1208->rtc->dev.kobj, NULL,
0641                      dev_attr_timestamp0.attr.name);
0642     }
0643 
0644     return handled ? IRQ_HANDLED : IRQ_NONE;
0645 }
0646 
0647 static const struct rtc_class_ops isl1208_rtc_ops = {
0648     .proc = isl1208_rtc_proc,
0649     .read_time = isl1208_rtc_read_time,
0650     .set_time = isl1208_rtc_set_time,
0651     .read_alarm = isl1208_rtc_read_alarm,
0652     .set_alarm = isl1208_rtc_set_alarm,
0653 };
0654 
0655 /* sysfs interface */
0656 
0657 static ssize_t
0658 isl1208_sysfs_show_atrim(struct device *dev,
0659              struct device_attribute *attr, char *buf)
0660 {
0661     int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
0662     if (atr < 0)
0663         return atr;
0664 
0665     return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
0666 }
0667 
0668 static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
0669 
0670 static ssize_t
0671 isl1208_sysfs_show_dtrim(struct device *dev,
0672              struct device_attribute *attr, char *buf)
0673 {
0674     int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
0675     if (dtr < 0)
0676         return dtr;
0677 
0678     return sprintf(buf, "%d ppm\n", dtr - 100);
0679 }
0680 
0681 static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
0682 
0683 static ssize_t
0684 isl1208_sysfs_show_usr(struct device *dev,
0685                struct device_attribute *attr, char *buf)
0686 {
0687     int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
0688     if (usr < 0)
0689         return usr;
0690 
0691     return sprintf(buf, "0x%.4x\n", usr);
0692 }
0693 
0694 static ssize_t
0695 isl1208_sysfs_store_usr(struct device *dev,
0696             struct device_attribute *attr,
0697             const char *buf, size_t count)
0698 {
0699     int usr = -1;
0700 
0701     if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
0702         if (sscanf(buf, "%x", &usr) != 1)
0703             return -EINVAL;
0704     } else {
0705         if (sscanf(buf, "%d", &usr) != 1)
0706             return -EINVAL;
0707     }
0708 
0709     if (usr < 0 || usr > 0xffff)
0710         return -EINVAL;
0711 
0712     if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
0713         return -EIO;
0714 
0715     return count;
0716 }
0717 
0718 static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
0719            isl1208_sysfs_store_usr);
0720 
0721 static struct attribute *isl1208_rtc_attrs[] = {
0722     &dev_attr_atrim.attr,
0723     &dev_attr_dtrim.attr,
0724     &dev_attr_usr.attr,
0725     NULL
0726 };
0727 
0728 static const struct attribute_group isl1208_rtc_sysfs_files = {
0729     .attrs  = isl1208_rtc_attrs,
0730 };
0731 
0732 static struct attribute *isl1219_rtc_attrs[] = {
0733     &dev_attr_timestamp0.attr,
0734     NULL
0735 };
0736 
0737 static const struct attribute_group isl1219_rtc_sysfs_files = {
0738     .attrs  = isl1219_rtc_attrs,
0739 };
0740 
0741 static int isl1208_nvmem_read(void *priv, unsigned int off, void *buf,
0742                   size_t count)
0743 {
0744     struct isl1208_state *isl1208 = priv;
0745     struct i2c_client *client = to_i2c_client(isl1208->rtc->dev.parent);
0746     int ret;
0747 
0748     /* nvmem sanitizes offset/count for us, but count==0 is possible */
0749     if (!count)
0750         return count;
0751     ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1 + off, buf,
0752                     count);
0753     return ret == 0 ? count : ret;
0754 }
0755 
0756 static int isl1208_nvmem_write(void *priv, unsigned int off, void *buf,
0757                    size_t count)
0758 {
0759     struct isl1208_state *isl1208 = priv;
0760     struct i2c_client *client = to_i2c_client(isl1208->rtc->dev.parent);
0761     int ret;
0762 
0763     /* nvmem sanitizes off/count for us, but count==0 is possible */
0764     if (!count)
0765         return count;
0766     ret = isl1208_i2c_set_regs(client, ISL1208_REG_USR1 + off, buf,
0767                    count);
0768 
0769     return ret == 0 ? count : ret;
0770 }
0771 
0772 static const struct nvmem_config isl1208_nvmem_config = {
0773     .name = "isl1208_nvram",
0774     .word_size = 1,
0775     .stride = 1,
0776     /* .size from chip specific config */
0777     .reg_read = isl1208_nvmem_read,
0778     .reg_write = isl1208_nvmem_write,
0779 };
0780 
0781 static int isl1208_setup_irq(struct i2c_client *client, int irq)
0782 {
0783     int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
0784                     isl1208_rtc_interrupt,
0785                     IRQF_SHARED | IRQF_ONESHOT,
0786                     isl1208_driver.driver.name,
0787                     client);
0788     if (!rc) {
0789         device_init_wakeup(&client->dev, 1);
0790         enable_irq_wake(irq);
0791     } else {
0792         dev_err(&client->dev,
0793             "Unable to request irq %d, no alarm support\n",
0794             irq);
0795     }
0796     return rc;
0797 }
0798 
0799 static int
0800 isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
0801 {
0802     int rc = 0;
0803     struct isl1208_state *isl1208;
0804     int evdet_irq = -1;
0805 
0806     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0807         return -ENODEV;
0808 
0809     if (isl1208_i2c_validate_client(client) < 0)
0810         return -ENODEV;
0811 
0812     /* Allocate driver state, point i2c client data to it */
0813     isl1208 = devm_kzalloc(&client->dev, sizeof(*isl1208), GFP_KERNEL);
0814     if (!isl1208)
0815         return -ENOMEM;
0816     i2c_set_clientdata(client, isl1208);
0817 
0818     /* Determine which chip we have */
0819     if (client->dev.of_node) {
0820         isl1208->config = of_device_get_match_data(&client->dev);
0821         if (!isl1208->config)
0822             return -ENODEV;
0823     } else {
0824         if (id->driver_data >= ISL_LAST_ID)
0825             return -ENODEV;
0826         isl1208->config = &isl1208_configs[id->driver_data];
0827     }
0828 
0829     isl1208->rtc = devm_rtc_allocate_device(&client->dev);
0830     if (IS_ERR(isl1208->rtc))
0831         return PTR_ERR(isl1208->rtc);
0832 
0833     isl1208->rtc->ops = &isl1208_rtc_ops;
0834 
0835     /* Setup nvmem configuration in driver state struct */
0836     isl1208->nvmem_config = isl1208_nvmem_config;
0837     isl1208->nvmem_config.size = isl1208->config->nvmem_length;
0838     isl1208->nvmem_config.priv = isl1208;
0839 
0840     rc = isl1208_i2c_get_sr(client);
0841     if (rc < 0) {
0842         dev_err(&client->dev, "reading status failed\n");
0843         return rc;
0844     }
0845 
0846     if (rc & ISL1208_REG_SR_RTCF)
0847         dev_warn(&client->dev, "rtc power failure detected, "
0848              "please set clock.\n");
0849 
0850     if (isl1208->config->has_tamper) {
0851         struct device_node *np = client->dev.of_node;
0852         u32 evienb;
0853 
0854         rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
0855         if (rc < 0) {
0856             dev_err(&client->dev, "failed to read EV reg\n");
0857             return rc;
0858         }
0859         rc |= ISL1219_REG_EV_EVEN;
0860         if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
0861             if (evienb)
0862                 rc |= ISL1219_REG_EV_EVIENB;
0863             else
0864                 rc &= ~ISL1219_REG_EV_EVIENB;
0865         }
0866         rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
0867         if (rc < 0) {
0868             dev_err(&client->dev, "could not enable tamper detection\n");
0869             return rc;
0870         }
0871         evdet_irq = of_irq_get_byname(np, "evdet");
0872     }
0873     if (isl1208->config->has_timestamp) {
0874         rc = rtc_add_group(isl1208->rtc, &isl1219_rtc_sysfs_files);
0875         if (rc)
0876             return rc;
0877     }
0878 
0879     rc = rtc_add_group(isl1208->rtc, &isl1208_rtc_sysfs_files);
0880     if (rc)
0881         return rc;
0882 
0883     if (client->irq > 0) {
0884         rc = isl1208_setup_irq(client, client->irq);
0885         if (rc)
0886             return rc;
0887 
0888     } else {
0889         clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, isl1208->rtc->features);
0890     }
0891 
0892     if (evdet_irq > 0 && evdet_irq != client->irq)
0893         rc = isl1208_setup_irq(client, evdet_irq);
0894     if (rc)
0895         return rc;
0896 
0897     rc = devm_rtc_nvmem_register(isl1208->rtc, &isl1208->nvmem_config);
0898     if (rc)
0899         return rc;
0900 
0901     return devm_rtc_register_device(isl1208->rtc);
0902 }
0903 
0904 static struct i2c_driver isl1208_driver = {
0905     .driver = {
0906         .name = "rtc-isl1208",
0907         .of_match_table = of_match_ptr(isl1208_of_match),
0908     },
0909     .probe = isl1208_probe,
0910     .id_table = isl1208_id,
0911 };
0912 
0913 module_i2c_driver(isl1208_driver);
0914 
0915 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
0916 MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
0917 MODULE_LICENSE("GPL");