Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * A driver for the I2C members of the Abracon AB x8xx RTC family,
0004  * and compatible: AB 1805 and AB 0805
0005  *
0006  * Copyright 2014-2015 Macq S.A.
0007  *
0008  * Author: Philippe De Muyter <phdm@macqel.be>
0009  * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
0010  *
0011  */
0012 
0013 #include <linux/bcd.h>
0014 #include <linux/i2c.h>
0015 #include <linux/module.h>
0016 #include <linux/of_device.h>
0017 #include <linux/rtc.h>
0018 #include <linux/watchdog.h>
0019 
0020 #define ABX8XX_REG_HTH      0x00
0021 #define ABX8XX_REG_SC       0x01
0022 #define ABX8XX_REG_MN       0x02
0023 #define ABX8XX_REG_HR       0x03
0024 #define ABX8XX_REG_DA       0x04
0025 #define ABX8XX_REG_MO       0x05
0026 #define ABX8XX_REG_YR       0x06
0027 #define ABX8XX_REG_WD       0x07
0028 
0029 #define ABX8XX_REG_AHTH     0x08
0030 #define ABX8XX_REG_ASC      0x09
0031 #define ABX8XX_REG_AMN      0x0a
0032 #define ABX8XX_REG_AHR      0x0b
0033 #define ABX8XX_REG_ADA      0x0c
0034 #define ABX8XX_REG_AMO      0x0d
0035 #define ABX8XX_REG_AWD      0x0e
0036 
0037 #define ABX8XX_REG_STATUS   0x0f
0038 #define ABX8XX_STATUS_AF    BIT(2)
0039 #define ABX8XX_STATUS_BLF   BIT(4)
0040 #define ABX8XX_STATUS_WDT   BIT(6)
0041 
0042 #define ABX8XX_REG_CTRL1    0x10
0043 #define ABX8XX_CTRL_WRITE   BIT(0)
0044 #define ABX8XX_CTRL_ARST    BIT(2)
0045 #define ABX8XX_CTRL_12_24   BIT(6)
0046 
0047 #define ABX8XX_REG_CTRL2    0x11
0048 #define ABX8XX_CTRL2_RSVD   BIT(5)
0049 
0050 #define ABX8XX_REG_IRQ      0x12
0051 #define ABX8XX_IRQ_AIE      BIT(2)
0052 #define ABX8XX_IRQ_IM_1_4   (0x3 << 5)
0053 
0054 #define ABX8XX_REG_CD_TIMER_CTL 0x18
0055 
0056 #define ABX8XX_REG_OSC      0x1c
0057 #define ABX8XX_OSC_FOS      BIT(3)
0058 #define ABX8XX_OSC_BOS      BIT(4)
0059 #define ABX8XX_OSC_ACAL_512 BIT(5)
0060 #define ABX8XX_OSC_ACAL_1024    BIT(6)
0061 
0062 #define ABX8XX_OSC_OSEL     BIT(7)
0063 
0064 #define ABX8XX_REG_OSS      0x1d
0065 #define ABX8XX_OSS_OF       BIT(1)
0066 #define ABX8XX_OSS_OMODE    BIT(4)
0067 
0068 #define ABX8XX_REG_WDT      0x1b
0069 #define ABX8XX_WDT_WDS      BIT(7)
0070 #define ABX8XX_WDT_BMB_MASK 0x7c
0071 #define ABX8XX_WDT_BMB_SHIFT    2
0072 #define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT)
0073 #define ABX8XX_WDT_WRB_MASK 0x03
0074 #define ABX8XX_WDT_WRB_1HZ  0x02
0075 
0076 #define ABX8XX_REG_CFG_KEY  0x1f
0077 #define ABX8XX_CFG_KEY_OSC  0xa1
0078 #define ABX8XX_CFG_KEY_MISC 0x9d
0079 
0080 #define ABX8XX_REG_ID0      0x28
0081 
0082 #define ABX8XX_REG_OUT_CTRL 0x30
0083 #define ABX8XX_OUT_CTRL_EXDS    BIT(4)
0084 
0085 #define ABX8XX_REG_TRICKLE  0x20
0086 #define ABX8XX_TRICKLE_CHARGE_ENABLE    0xa0
0087 #define ABX8XX_TRICKLE_STANDARD_DIODE   0x8
0088 #define ABX8XX_TRICKLE_SCHOTTKY_DIODE   0x4
0089 
0090 static u8 trickle_resistors[] = {0, 3, 6, 11};
0091 
0092 enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
0093     AB1801, AB1803, AB1804, AB1805, RV1805, ABX80X};
0094 
0095 struct abx80x_cap {
0096     u16 pn;
0097     bool has_tc;
0098     bool has_wdog;
0099 };
0100 
0101 static struct abx80x_cap abx80x_caps[] = {
0102     [AB0801] = {.pn = 0x0801},
0103     [AB0803] = {.pn = 0x0803},
0104     [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true},
0105     [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true},
0106     [AB1801] = {.pn = 0x1801},
0107     [AB1803] = {.pn = 0x1803},
0108     [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true},
0109     [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
0110     [RV1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
0111     [ABX80X] = {.pn = 0}
0112 };
0113 
0114 struct abx80x_priv {
0115     struct rtc_device *rtc;
0116     struct i2c_client *client;
0117     struct watchdog_device wdog;
0118 };
0119 
0120 static int abx80x_write_config_key(struct i2c_client *client, u8 key)
0121 {
0122     if (i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, key) < 0) {
0123         dev_err(&client->dev, "Unable to write configuration key\n");
0124         return -EIO;
0125     }
0126 
0127     return 0;
0128 }
0129 
0130 static int abx80x_is_rc_mode(struct i2c_client *client)
0131 {
0132     int flags = 0;
0133 
0134     flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
0135     if (flags < 0) {
0136         dev_err(&client->dev,
0137             "Failed to read autocalibration attribute\n");
0138         return flags;
0139     }
0140 
0141     return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
0142 }
0143 
0144 static int abx80x_enable_trickle_charger(struct i2c_client *client,
0145                      u8 trickle_cfg)
0146 {
0147     int err;
0148 
0149     /*
0150      * Write the configuration key register to enable access to the Trickle
0151      * register
0152      */
0153     if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
0154         return -EIO;
0155 
0156     err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
0157                     ABX8XX_TRICKLE_CHARGE_ENABLE |
0158                     trickle_cfg);
0159     if (err < 0) {
0160         dev_err(&client->dev, "Unable to write trickle register\n");
0161         return -EIO;
0162     }
0163 
0164     return 0;
0165 }
0166 
0167 static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
0168 {
0169     struct i2c_client *client = to_i2c_client(dev);
0170     unsigned char buf[8];
0171     int err, flags, rc_mode = 0;
0172 
0173     /* Read the Oscillator Failure only in XT mode */
0174     rc_mode = abx80x_is_rc_mode(client);
0175     if (rc_mode < 0)
0176         return rc_mode;
0177 
0178     if (!rc_mode) {
0179         flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
0180         if (flags < 0)
0181             return flags;
0182 
0183         if (flags & ABX8XX_OSS_OF) {
0184             dev_err(dev, "Oscillator failure, data is invalid.\n");
0185             return -EINVAL;
0186         }
0187     }
0188 
0189     err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
0190                         sizeof(buf), buf);
0191     if (err < 0) {
0192         dev_err(&client->dev, "Unable to read date\n");
0193         return -EIO;
0194     }
0195 
0196     tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
0197     tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
0198     tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
0199     tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
0200     tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
0201     tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
0202     tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
0203 
0204     return 0;
0205 }
0206 
0207 static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
0208 {
0209     struct i2c_client *client = to_i2c_client(dev);
0210     unsigned char buf[8];
0211     int err, flags;
0212 
0213     if (tm->tm_year < 100)
0214         return -EINVAL;
0215 
0216     buf[ABX8XX_REG_HTH] = 0;
0217     buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
0218     buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
0219     buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
0220     buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
0221     buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
0222     buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
0223     buf[ABX8XX_REG_WD] = tm->tm_wday;
0224 
0225     err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
0226                          sizeof(buf), buf);
0227     if (err < 0) {
0228         dev_err(&client->dev, "Unable to write to date registers\n");
0229         return -EIO;
0230     }
0231 
0232     /* Clear the OF bit of Oscillator Status Register */
0233     flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
0234     if (flags < 0)
0235         return flags;
0236 
0237     err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
0238                     flags & ~ABX8XX_OSS_OF);
0239     if (err < 0) {
0240         dev_err(&client->dev, "Unable to write oscillator status register\n");
0241         return err;
0242     }
0243 
0244     return 0;
0245 }
0246 
0247 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
0248 {
0249     struct i2c_client *client = dev_id;
0250     struct abx80x_priv *priv = i2c_get_clientdata(client);
0251     struct rtc_device *rtc = priv->rtc;
0252     int status;
0253 
0254     status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
0255     if (status < 0)
0256         return IRQ_NONE;
0257 
0258     if (status & ABX8XX_STATUS_AF)
0259         rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
0260 
0261     /*
0262      * It is unclear if we'll get an interrupt before the external
0263      * reset kicks in.
0264      */
0265     if (status & ABX8XX_STATUS_WDT)
0266         dev_alert(&client->dev, "watchdog timeout interrupt.\n");
0267 
0268     i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
0269 
0270     return IRQ_HANDLED;
0271 }
0272 
0273 static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
0274 {
0275     struct i2c_client *client = to_i2c_client(dev);
0276     unsigned char buf[7];
0277 
0278     int irq_mask, err;
0279 
0280     if (client->irq <= 0)
0281         return -EINVAL;
0282 
0283     err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
0284                         sizeof(buf), buf);
0285     if (err)
0286         return err;
0287 
0288     irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
0289     if (irq_mask < 0)
0290         return irq_mask;
0291 
0292     t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
0293     t->time.tm_min = bcd2bin(buf[1] & 0x7F);
0294     t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
0295     t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
0296     t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
0297     t->time.tm_wday = buf[5] & 0x7;
0298 
0299     t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
0300     t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
0301 
0302     return err;
0303 }
0304 
0305 static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
0306 {
0307     struct i2c_client *client = to_i2c_client(dev);
0308     u8 alarm[6];
0309     int err;
0310 
0311     if (client->irq <= 0)
0312         return -EINVAL;
0313 
0314     alarm[0] = 0x0;
0315     alarm[1] = bin2bcd(t->time.tm_sec);
0316     alarm[2] = bin2bcd(t->time.tm_min);
0317     alarm[3] = bin2bcd(t->time.tm_hour);
0318     alarm[4] = bin2bcd(t->time.tm_mday);
0319     alarm[5] = bin2bcd(t->time.tm_mon + 1);
0320 
0321     err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
0322                          sizeof(alarm), alarm);
0323     if (err < 0) {
0324         dev_err(&client->dev, "Unable to write alarm registers\n");
0325         return -EIO;
0326     }
0327 
0328     if (t->enabled) {
0329         err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
0330                         (ABX8XX_IRQ_IM_1_4 |
0331                          ABX8XX_IRQ_AIE));
0332         if (err)
0333             return err;
0334     }
0335 
0336     return 0;
0337 }
0338 
0339 static int abx80x_rtc_set_autocalibration(struct device *dev,
0340                       int autocalibration)
0341 {
0342     struct i2c_client *client = to_i2c_client(dev);
0343     int retval, flags = 0;
0344 
0345     if ((autocalibration != 0) && (autocalibration != 1024) &&
0346         (autocalibration != 512)) {
0347         dev_err(dev, "autocalibration value outside permitted range\n");
0348         return -EINVAL;
0349     }
0350 
0351     flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
0352     if (flags < 0)
0353         return flags;
0354 
0355     if (autocalibration == 0) {
0356         flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
0357     } else if (autocalibration == 1024) {
0358         /* 1024 autocalibration is 0x10 */
0359         flags |= ABX8XX_OSC_ACAL_1024;
0360         flags &= ~(ABX8XX_OSC_ACAL_512);
0361     } else {
0362         /* 512 autocalibration is 0x11 */
0363         flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
0364     }
0365 
0366     /* Unlock write access to Oscillator Control Register */
0367     if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
0368         return -EIO;
0369 
0370     retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
0371 
0372     return retval;
0373 }
0374 
0375 static int abx80x_rtc_get_autocalibration(struct device *dev)
0376 {
0377     struct i2c_client *client = to_i2c_client(dev);
0378     int flags = 0, autocalibration;
0379 
0380     flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
0381     if (flags < 0)
0382         return flags;
0383 
0384     if (flags & ABX8XX_OSC_ACAL_512)
0385         autocalibration = 512;
0386     else if (flags & ABX8XX_OSC_ACAL_1024)
0387         autocalibration = 1024;
0388     else
0389         autocalibration = 0;
0390 
0391     return autocalibration;
0392 }
0393 
0394 static ssize_t autocalibration_store(struct device *dev,
0395                      struct device_attribute *attr,
0396                      const char *buf, size_t count)
0397 {
0398     int retval;
0399     unsigned long autocalibration = 0;
0400 
0401     retval = kstrtoul(buf, 10, &autocalibration);
0402     if (retval < 0) {
0403         dev_err(dev, "Failed to store RTC autocalibration attribute\n");
0404         return -EINVAL;
0405     }
0406 
0407     retval = abx80x_rtc_set_autocalibration(dev->parent, autocalibration);
0408 
0409     return retval ? retval : count;
0410 }
0411 
0412 static ssize_t autocalibration_show(struct device *dev,
0413                     struct device_attribute *attr, char *buf)
0414 {
0415     int autocalibration = 0;
0416 
0417     autocalibration = abx80x_rtc_get_autocalibration(dev->parent);
0418     if (autocalibration < 0) {
0419         dev_err(dev, "Failed to read RTC autocalibration\n");
0420         sprintf(buf, "0\n");
0421         return autocalibration;
0422     }
0423 
0424     return sprintf(buf, "%d\n", autocalibration);
0425 }
0426 
0427 static DEVICE_ATTR_RW(autocalibration);
0428 
0429 static ssize_t oscillator_store(struct device *dev,
0430                 struct device_attribute *attr,
0431                 const char *buf, size_t count)
0432 {
0433     struct i2c_client *client = to_i2c_client(dev->parent);
0434     int retval, flags, rc_mode = 0;
0435 
0436     if (strncmp(buf, "rc", 2) == 0) {
0437         rc_mode = 1;
0438     } else if (strncmp(buf, "xtal", 4) == 0) {
0439         rc_mode = 0;
0440     } else {
0441         dev_err(dev, "Oscillator selection value outside permitted ones\n");
0442         return -EINVAL;
0443     }
0444 
0445     flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
0446     if (flags < 0)
0447         return flags;
0448 
0449     if (rc_mode == 0)
0450         flags &= ~(ABX8XX_OSC_OSEL);
0451     else
0452         flags |= (ABX8XX_OSC_OSEL);
0453 
0454     /* Unlock write access on Oscillator Control register */
0455     if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
0456         return -EIO;
0457 
0458     retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
0459     if (retval < 0) {
0460         dev_err(dev, "Failed to write Oscillator Control register\n");
0461         return retval;
0462     }
0463 
0464     return retval ? retval : count;
0465 }
0466 
0467 static ssize_t oscillator_show(struct device *dev,
0468                    struct device_attribute *attr, char *buf)
0469 {
0470     int rc_mode = 0;
0471     struct i2c_client *client = to_i2c_client(dev->parent);
0472 
0473     rc_mode = abx80x_is_rc_mode(client);
0474 
0475     if (rc_mode < 0) {
0476         dev_err(dev, "Failed to read RTC oscillator selection\n");
0477         sprintf(buf, "\n");
0478         return rc_mode;
0479     }
0480 
0481     if (rc_mode)
0482         return sprintf(buf, "rc\n");
0483     else
0484         return sprintf(buf, "xtal\n");
0485 }
0486 
0487 static DEVICE_ATTR_RW(oscillator);
0488 
0489 static struct attribute *rtc_calib_attrs[] = {
0490     &dev_attr_autocalibration.attr,
0491     &dev_attr_oscillator.attr,
0492     NULL,
0493 };
0494 
0495 static const struct attribute_group rtc_calib_attr_group = {
0496     .attrs      = rtc_calib_attrs,
0497 };
0498 
0499 static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
0500 {
0501     struct i2c_client *client = to_i2c_client(dev);
0502     int err;
0503 
0504     if (enabled)
0505         err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
0506                         (ABX8XX_IRQ_IM_1_4 |
0507                          ABX8XX_IRQ_AIE));
0508     else
0509         err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
0510                         ABX8XX_IRQ_IM_1_4);
0511     return err;
0512 }
0513 
0514 static int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
0515 {
0516     struct i2c_client *client = to_i2c_client(dev);
0517     int status, tmp;
0518 
0519     switch (cmd) {
0520     case RTC_VL_READ:
0521         status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
0522         if (status < 0)
0523             return status;
0524 
0525         tmp = status & ABX8XX_STATUS_BLF ? RTC_VL_BACKUP_LOW : 0;
0526 
0527         return put_user(tmp, (unsigned int __user *)arg);
0528 
0529     case RTC_VL_CLR:
0530         status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
0531         if (status < 0)
0532             return status;
0533 
0534         status &= ~ABX8XX_STATUS_BLF;
0535 
0536         tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
0537         if (tmp < 0)
0538             return tmp;
0539 
0540         return 0;
0541 
0542     default:
0543         return -ENOIOCTLCMD;
0544     }
0545 }
0546 
0547 static const struct rtc_class_ops abx80x_rtc_ops = {
0548     .read_time  = abx80x_rtc_read_time,
0549     .set_time   = abx80x_rtc_set_time,
0550     .read_alarm = abx80x_read_alarm,
0551     .set_alarm  = abx80x_set_alarm,
0552     .alarm_irq_enable = abx80x_alarm_irq_enable,
0553     .ioctl      = abx80x_ioctl,
0554 };
0555 
0556 static int abx80x_dt_trickle_cfg(struct i2c_client *client)
0557 {
0558     struct device_node *np = client->dev.of_node;
0559     const char *diode;
0560     int trickle_cfg = 0;
0561     int i, ret;
0562     u32 tmp;
0563 
0564     ret = of_property_read_string(np, "abracon,tc-diode", &diode);
0565     if (ret)
0566         return ret;
0567 
0568     if (!strcmp(diode, "standard")) {
0569         trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
0570     } else if (!strcmp(diode, "schottky")) {
0571         trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
0572     } else {
0573         dev_dbg(&client->dev, "Invalid tc-diode value: %s\n", diode);
0574         return -EINVAL;
0575     }
0576 
0577     ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
0578     if (ret)
0579         return ret;
0580 
0581     for (i = 0; i < sizeof(trickle_resistors); i++)
0582         if (trickle_resistors[i] == tmp)
0583             break;
0584 
0585     if (i == sizeof(trickle_resistors)) {
0586         dev_dbg(&client->dev, "Invalid tc-resistor value: %u\n", tmp);
0587         return -EINVAL;
0588     }
0589 
0590     return (trickle_cfg | i);
0591 }
0592 
0593 #ifdef CONFIG_WATCHDOG
0594 
0595 static inline u8 timeout_bits(unsigned int timeout)
0596 {
0597     return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) |
0598          ABX8XX_WDT_WRB_1HZ;
0599 }
0600 
0601 static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog,
0602                      unsigned int timeout)
0603 {
0604     struct abx80x_priv *priv = watchdog_get_drvdata(wdog);
0605     u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout);
0606 
0607     /*
0608      * Writing any timeout to the WDT register resets the watchdog timer.
0609      * Writing 0 disables it.
0610      */
0611     return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val);
0612 }
0613 
0614 static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
0615                    unsigned int new_timeout)
0616 {
0617     int err = 0;
0618 
0619     if (watchdog_hw_running(wdog))
0620         err = __abx80x_wdog_set_timeout(wdog, new_timeout);
0621 
0622     if (err == 0)
0623         wdog->timeout = new_timeout;
0624 
0625     return err;
0626 }
0627 
0628 static int abx80x_wdog_ping(struct watchdog_device *wdog)
0629 {
0630     return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
0631 }
0632 
0633 static int abx80x_wdog_start(struct watchdog_device *wdog)
0634 {
0635     return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
0636 }
0637 
0638 static int abx80x_wdog_stop(struct watchdog_device *wdog)
0639 {
0640     return __abx80x_wdog_set_timeout(wdog, 0);
0641 }
0642 
0643 static const struct watchdog_info abx80x_wdog_info = {
0644     .identity = "abx80x watchdog",
0645     .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
0646 };
0647 
0648 static const struct watchdog_ops abx80x_wdog_ops = {
0649     .owner = THIS_MODULE,
0650     .start = abx80x_wdog_start,
0651     .stop = abx80x_wdog_stop,
0652     .ping = abx80x_wdog_ping,
0653     .set_timeout = abx80x_wdog_set_timeout,
0654 };
0655 
0656 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
0657 {
0658     priv->wdog.parent = &priv->client->dev;
0659     priv->wdog.ops = &abx80x_wdog_ops;
0660     priv->wdog.info = &abx80x_wdog_info;
0661     priv->wdog.min_timeout = 1;
0662     priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME;
0663     priv->wdog.timeout = ABX8XX_WDT_MAX_TIME;
0664 
0665     watchdog_set_drvdata(&priv->wdog, priv);
0666 
0667     return devm_watchdog_register_device(&priv->client->dev, &priv->wdog);
0668 }
0669 #else
0670 static int abx80x_setup_watchdog(struct abx80x_priv *priv)
0671 {
0672     return 0;
0673 }
0674 #endif
0675 
0676 static int abx80x_probe(struct i2c_client *client,
0677             const struct i2c_device_id *id)
0678 {
0679     struct device_node *np = client->dev.of_node;
0680     struct abx80x_priv *priv;
0681     int i, data, err, trickle_cfg = -EINVAL;
0682     char buf[7];
0683     unsigned int part = id->driver_data;
0684     unsigned int partnumber;
0685     unsigned int majrev, minrev;
0686     unsigned int lot;
0687     unsigned int wafer;
0688     unsigned int uid;
0689 
0690     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0691         return -ENODEV;
0692 
0693     err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
0694                         sizeof(buf), buf);
0695     if (err < 0) {
0696         dev_err(&client->dev, "Unable to read partnumber\n");
0697         return -EIO;
0698     }
0699 
0700     partnumber = (buf[0] << 8) | buf[1];
0701     majrev = buf[2] >> 3;
0702     minrev = buf[2] & 0x7;
0703     lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
0704     uid = ((buf[4] & 0x7f) << 8) | buf[5];
0705     wafer = (buf[6] & 0x7c) >> 2;
0706     dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
0707          partnumber, majrev, minrev, lot, wafer, uid);
0708 
0709     data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
0710     if (data < 0) {
0711         dev_err(&client->dev, "Unable to read control register\n");
0712         return -EIO;
0713     }
0714 
0715     err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
0716                     ((data & ~(ABX8XX_CTRL_12_24 |
0717                            ABX8XX_CTRL_ARST)) |
0718                      ABX8XX_CTRL_WRITE));
0719     if (err < 0) {
0720         dev_err(&client->dev, "Unable to write control register\n");
0721         return -EIO;
0722     }
0723 
0724     /* Configure RV1805 specifics */
0725     if (part == RV1805) {
0726         /*
0727          * Avoid accidentally entering test mode. This can happen
0728          * on the RV1805 in case the reserved bit 5 in control2
0729          * register is set. RV-1805-C3 datasheet indicates that
0730          * the bit should be cleared in section 11h - Control2.
0731          */
0732         data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL2);
0733         if (data < 0) {
0734             dev_err(&client->dev,
0735                 "Unable to read control2 register\n");
0736             return -EIO;
0737         }
0738 
0739         err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL2,
0740                         data & ~ABX8XX_CTRL2_RSVD);
0741         if (err < 0) {
0742             dev_err(&client->dev,
0743                 "Unable to write control2 register\n");
0744             return -EIO;
0745         }
0746 
0747         /*
0748          * Avoid extra power leakage. The RV1805 uses smaller
0749          * 10pin package and the EXTI input is not present.
0750          * Disable it to avoid leakage.
0751          */
0752         data = i2c_smbus_read_byte_data(client, ABX8XX_REG_OUT_CTRL);
0753         if (data < 0) {
0754             dev_err(&client->dev,
0755                 "Unable to read output control register\n");
0756             return -EIO;
0757         }
0758 
0759         /*
0760          * Write the configuration key register to enable access to
0761          * the config2 register
0762          */
0763         if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
0764             return -EIO;
0765 
0766         err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OUT_CTRL,
0767                         data | ABX8XX_OUT_CTRL_EXDS);
0768         if (err < 0) {
0769             dev_err(&client->dev,
0770                 "Unable to write output control register\n");
0771             return -EIO;
0772         }
0773     }
0774 
0775     /* part autodetection */
0776     if (part == ABX80X) {
0777         for (i = 0; abx80x_caps[i].pn; i++)
0778             if (partnumber == abx80x_caps[i].pn)
0779                 break;
0780         if (abx80x_caps[i].pn == 0) {
0781             dev_err(&client->dev, "Unknown part: %04x\n",
0782                 partnumber);
0783             return -EINVAL;
0784         }
0785         part = i;
0786     }
0787 
0788     if (partnumber != abx80x_caps[part].pn) {
0789         dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
0790             partnumber, abx80x_caps[part].pn);
0791         return -EINVAL;
0792     }
0793 
0794     if (np && abx80x_caps[part].has_tc)
0795         trickle_cfg = abx80x_dt_trickle_cfg(client);
0796 
0797     if (trickle_cfg > 0) {
0798         dev_info(&client->dev, "Enabling trickle charger: %02x\n",
0799              trickle_cfg);
0800         abx80x_enable_trickle_charger(client, trickle_cfg);
0801     }
0802 
0803     err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
0804                     BIT(2));
0805     if (err)
0806         return err;
0807 
0808     priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
0809     if (priv == NULL)
0810         return -ENOMEM;
0811 
0812     priv->rtc = devm_rtc_allocate_device(&client->dev);
0813     if (IS_ERR(priv->rtc))
0814         return PTR_ERR(priv->rtc);
0815 
0816     priv->rtc->ops = &abx80x_rtc_ops;
0817     priv->client = client;
0818 
0819     i2c_set_clientdata(client, priv);
0820 
0821     if (abx80x_caps[part].has_wdog) {
0822         err = abx80x_setup_watchdog(priv);
0823         if (err)
0824             return err;
0825     }
0826 
0827     if (client->irq > 0) {
0828         dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
0829         err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
0830                         abx80x_handle_irq,
0831                         IRQF_SHARED | IRQF_ONESHOT,
0832                         "abx8xx",
0833                         client);
0834         if (err) {
0835             dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
0836             client->irq = 0;
0837         }
0838     }
0839 
0840     err = rtc_add_group(priv->rtc, &rtc_calib_attr_group);
0841     if (err) {
0842         dev_err(&client->dev, "Failed to create sysfs group: %d\n",
0843             err);
0844         return err;
0845     }
0846 
0847     return devm_rtc_register_device(priv->rtc);
0848 }
0849 
0850 static const struct i2c_device_id abx80x_id[] = {
0851     { "abx80x", ABX80X },
0852     { "ab0801", AB0801 },
0853     { "ab0803", AB0803 },
0854     { "ab0804", AB0804 },
0855     { "ab0805", AB0805 },
0856     { "ab1801", AB1801 },
0857     { "ab1803", AB1803 },
0858     { "ab1804", AB1804 },
0859     { "ab1805", AB1805 },
0860     { "rv1805", RV1805 },
0861     { }
0862 };
0863 MODULE_DEVICE_TABLE(i2c, abx80x_id);
0864 
0865 #ifdef CONFIG_OF
0866 static const struct of_device_id abx80x_of_match[] = {
0867     {
0868         .compatible = "abracon,abx80x",
0869         .data = (void *)ABX80X
0870     },
0871     {
0872         .compatible = "abracon,ab0801",
0873         .data = (void *)AB0801
0874     },
0875     {
0876         .compatible = "abracon,ab0803",
0877         .data = (void *)AB0803
0878     },
0879     {
0880         .compatible = "abracon,ab0804",
0881         .data = (void *)AB0804
0882     },
0883     {
0884         .compatible = "abracon,ab0805",
0885         .data = (void *)AB0805
0886     },
0887     {
0888         .compatible = "abracon,ab1801",
0889         .data = (void *)AB1801
0890     },
0891     {
0892         .compatible = "abracon,ab1803",
0893         .data = (void *)AB1803
0894     },
0895     {
0896         .compatible = "abracon,ab1804",
0897         .data = (void *)AB1804
0898     },
0899     {
0900         .compatible = "abracon,ab1805",
0901         .data = (void *)AB1805
0902     },
0903     {
0904         .compatible = "microcrystal,rv1805",
0905         .data = (void *)RV1805
0906     },
0907     { }
0908 };
0909 MODULE_DEVICE_TABLE(of, abx80x_of_match);
0910 #endif
0911 
0912 static struct i2c_driver abx80x_driver = {
0913     .driver     = {
0914         .name   = "rtc-abx80x",
0915         .of_match_table = of_match_ptr(abx80x_of_match),
0916     },
0917     .probe      = abx80x_probe,
0918     .id_table   = abx80x_id,
0919 };
0920 
0921 module_i2c_driver(abx80x_driver);
0922 
0923 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
0924 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
0925 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
0926 MODULE_LICENSE("GPL v2");