0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/rtc.h>
0010 #include <linux/i2c.h>
0011 #include <linux/bitrev.h>
0012 #include <linux/bcd.h>
0013 #include <linux/slab.h>
0014 #include <linux/delay.h>
0015
0016 #define S35390A_CMD_STATUS1 0
0017 #define S35390A_CMD_STATUS2 1
0018 #define S35390A_CMD_TIME1 2
0019 #define S35390A_CMD_TIME2 3
0020 #define S35390A_CMD_INT2_REG1 5
0021
0022 #define S35390A_BYTE_YEAR 0
0023 #define S35390A_BYTE_MONTH 1
0024 #define S35390A_BYTE_DAY 2
0025 #define S35390A_BYTE_WDAY 3
0026 #define S35390A_BYTE_HOURS 4
0027 #define S35390A_BYTE_MINS 5
0028 #define S35390A_BYTE_SECS 6
0029
0030 #define S35390A_ALRM_BYTE_WDAY 0
0031 #define S35390A_ALRM_BYTE_HOURS 1
0032 #define S35390A_ALRM_BYTE_MINS 2
0033
0034
0035 #define S35390A_FLAG_POC BIT(0)
0036 #define S35390A_FLAG_BLD BIT(1)
0037 #define S35390A_FLAG_INT2 BIT(2)
0038 #define S35390A_FLAG_24H BIT(6)
0039 #define S35390A_FLAG_RESET BIT(7)
0040
0041
0042 #define S35390A_FLAG_TEST BIT(0)
0043
0044
0045 #define S35390A_INT2_MODE_MASK 0x0E
0046 #define S35390A_INT2_MODE_NOINTR 0x00
0047 #define S35390A_INT2_MODE_ALARM BIT(1)
0048 #define S35390A_INT2_MODE_PMIN_EDG BIT(2)
0049 #define S35390A_INT2_MODE_FREQ BIT(3)
0050 #define S35390A_INT2_MODE_PMIN (BIT(3) | BIT(2))
0051
0052 static const struct i2c_device_id s35390a_id[] = {
0053 { "s35390a", 0 },
0054 { }
0055 };
0056 MODULE_DEVICE_TABLE(i2c, s35390a_id);
0057
0058 static const __maybe_unused struct of_device_id s35390a_of_match[] = {
0059 { .compatible = "s35390a" },
0060 { .compatible = "sii,s35390a" },
0061 { }
0062 };
0063 MODULE_DEVICE_TABLE(of, s35390a_of_match);
0064
0065 struct s35390a {
0066 struct i2c_client *client[8];
0067 struct rtc_device *rtc;
0068 int twentyfourhour;
0069 };
0070
0071 static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
0072 {
0073 struct i2c_client *client = s35390a->client[reg];
0074 struct i2c_msg msg[] = {
0075 {
0076 .addr = client->addr,
0077 .len = len,
0078 .buf = buf
0079 },
0080 };
0081
0082 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
0083 return -EIO;
0084
0085 return 0;
0086 }
0087
0088 static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
0089 {
0090 struct i2c_client *client = s35390a->client[reg];
0091 struct i2c_msg msg[] = {
0092 {
0093 .addr = client->addr,
0094 .flags = I2C_M_RD,
0095 .len = len,
0096 .buf = buf
0097 },
0098 };
0099
0100 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
0101 return -EIO;
0102
0103 return 0;
0104 }
0105
0106 static int s35390a_init(struct s35390a *s35390a)
0107 {
0108 u8 buf;
0109 int ret;
0110 unsigned initcount = 0;
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 initialize:
0121 buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
0122 ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
0123
0124 if (ret < 0)
0125 return ret;
0126
0127 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
0128 if (ret < 0)
0129 return ret;
0130
0131 if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
0132
0133 if (initcount < 5) {
0134 ++initcount;
0135 goto initialize;
0136 } else
0137 return -EIO;
0138 }
0139
0140 return 1;
0141 }
0142
0143
0144
0145
0146
0147
0148 static int s35390a_read_status(struct s35390a *s35390a, char *status1)
0149 {
0150 int ret;
0151
0152 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
0153 if (ret < 0)
0154 return ret;
0155
0156 if (*status1 & S35390A_FLAG_POC) {
0157
0158
0159
0160
0161 msleep(500);
0162 return 1;
0163 } else if (*status1 & S35390A_FLAG_BLD)
0164 return 1;
0165
0166
0167
0168 return 0;
0169 }
0170
0171 static int s35390a_disable_test_mode(struct s35390a *s35390a)
0172 {
0173 char buf[1];
0174
0175 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
0176 return -EIO;
0177
0178 if (!(buf[0] & S35390A_FLAG_TEST))
0179 return 0;
0180
0181 buf[0] &= ~S35390A_FLAG_TEST;
0182 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
0183 }
0184
0185 static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
0186 {
0187 if (s35390a->twentyfourhour)
0188 return bin2bcd(hour);
0189
0190 if (hour < 12)
0191 return bin2bcd(hour);
0192
0193 return 0x40 | bin2bcd(hour - 12);
0194 }
0195
0196 static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
0197 {
0198 unsigned hour;
0199
0200 if (s35390a->twentyfourhour)
0201 return bcd2bin(reg & 0x3f);
0202
0203 hour = bcd2bin(reg & 0x3f);
0204 if (reg & 0x40)
0205 hour += 12;
0206
0207 return hour;
0208 }
0209
0210 static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
0211 {
0212 struct i2c_client *client = to_i2c_client(dev);
0213 struct s35390a *s35390a = i2c_get_clientdata(client);
0214 int i, err;
0215 char buf[7], status;
0216
0217 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
0218 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
0219 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
0220 tm->tm_wday);
0221
0222 if (s35390a_read_status(s35390a, &status) == 1)
0223 s35390a_init(s35390a);
0224
0225 buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
0226 buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
0227 buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
0228 buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
0229 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
0230 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
0231 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
0232
0233
0234 for (i = 0; i < 7; ++i)
0235 buf[i] = bitrev8(buf[i]);
0236
0237 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
0238
0239 return err;
0240 }
0241
0242 static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
0243 {
0244 struct i2c_client *client = to_i2c_client(dev);
0245 struct s35390a *s35390a = i2c_get_clientdata(client);
0246 char buf[7], status;
0247 int i, err;
0248
0249 if (s35390a_read_status(s35390a, &status) == 1)
0250 return -EINVAL;
0251
0252 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
0253 if (err < 0)
0254 return err;
0255
0256
0257 for (i = 0; i < 7; ++i)
0258 buf[i] = bitrev8(buf[i]);
0259
0260 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
0261 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
0262 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
0263 tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
0264 tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
0265 tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
0266 tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
0267
0268 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
0269 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
0270 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
0271 tm->tm_wday);
0272
0273 return 0;
0274 }
0275
0276 static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
0277 {
0278 struct i2c_client *client = to_i2c_client(dev);
0279 struct s35390a *s35390a = i2c_get_clientdata(client);
0280 char buf[3], sts = 0;
0281 int err, i;
0282
0283 dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
0284 "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
0285 alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
0286 alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
0287
0288
0289 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
0290 if (err < 0)
0291 return err;
0292
0293
0294 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
0295 if (err < 0)
0296 return err;
0297
0298 if (alm->enabled)
0299 sts = S35390A_INT2_MODE_ALARM;
0300 else
0301 sts = S35390A_INT2_MODE_NOINTR;
0302
0303
0304 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
0305 if (err < 0)
0306 return err;
0307
0308 if (alm->time.tm_wday != -1)
0309 buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
0310 else
0311 buf[S35390A_ALRM_BYTE_WDAY] = 0;
0312
0313 buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
0314 alm->time.tm_hour) | 0x80;
0315 buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
0316
0317 if (alm->time.tm_hour >= 12)
0318 buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
0319
0320 for (i = 0; i < 3; ++i)
0321 buf[i] = bitrev8(buf[i]);
0322
0323 err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
0324 sizeof(buf));
0325
0326 return err;
0327 }
0328
0329 static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
0330 {
0331 struct i2c_client *client = to_i2c_client(dev);
0332 struct s35390a *s35390a = i2c_get_clientdata(client);
0333 char buf[3], sts;
0334 int i, err;
0335
0336 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
0337 if (err < 0)
0338 return err;
0339
0340 if ((sts & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
0341
0342
0343
0344
0345 alm->enabled = 0;
0346 return 0;
0347 } else {
0348 alm->enabled = 1;
0349 }
0350
0351 err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
0352 if (err < 0)
0353 return err;
0354
0355
0356 for (i = 0; i < 3; ++i)
0357 buf[i] = bitrev8(buf[i]);
0358
0359
0360
0361
0362
0363 if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
0364 alm->time.tm_wday =
0365 bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
0366
0367 if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
0368 alm->time.tm_hour =
0369 s35390a_reg2hr(s35390a,
0370 buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
0371
0372 if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
0373 alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
0374
0375
0376 alm->time.tm_sec = 0;
0377
0378 dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
0379 __func__, alm->time.tm_min, alm->time.tm_hour,
0380 alm->time.tm_wday);
0381
0382 return 0;
0383 }
0384
0385 static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
0386 unsigned long arg)
0387 {
0388 struct i2c_client *client = to_i2c_client(dev);
0389 struct s35390a *s35390a = i2c_get_clientdata(client);
0390 char sts;
0391 int err;
0392
0393 switch (cmd) {
0394 case RTC_VL_READ:
0395
0396 err = s35390a_read_status(s35390a, &sts);
0397 if (err < 0)
0398 return err;
0399 if (copy_to_user((void __user *)arg, &err, sizeof(int)))
0400 return -EFAULT;
0401 break;
0402 case RTC_VL_CLR:
0403
0404 err = s35390a_init(s35390a);
0405 if (err < 0)
0406 return err;
0407 break;
0408 default:
0409 return -ENOIOCTLCMD;
0410 }
0411
0412 return 0;
0413 }
0414
0415 static const struct rtc_class_ops s35390a_rtc_ops = {
0416 .read_time = s35390a_rtc_read_time,
0417 .set_time = s35390a_rtc_set_time,
0418 .set_alarm = s35390a_rtc_set_alarm,
0419 .read_alarm = s35390a_rtc_read_alarm,
0420 .ioctl = s35390a_rtc_ioctl,
0421 };
0422
0423 static int s35390a_probe(struct i2c_client *client)
0424 {
0425 int err, err_read;
0426 unsigned int i;
0427 struct s35390a *s35390a;
0428 char buf, status1;
0429 struct device *dev = &client->dev;
0430
0431 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0432 return -ENODEV;
0433
0434 s35390a = devm_kzalloc(dev, sizeof(struct s35390a), GFP_KERNEL);
0435 if (!s35390a)
0436 return -ENOMEM;
0437
0438 s35390a->client[0] = client;
0439 i2c_set_clientdata(client, s35390a);
0440
0441
0442 for (i = 1; i < 8; ++i) {
0443 s35390a->client[i] = devm_i2c_new_dummy_device(dev,
0444 client->adapter,
0445 client->addr + i);
0446 if (IS_ERR(s35390a->client[i])) {
0447 dev_err(dev, "Address %02x unavailable\n",
0448 client->addr + i);
0449 return PTR_ERR(s35390a->client[i]);
0450 }
0451 }
0452
0453 s35390a->rtc = devm_rtc_allocate_device(dev);
0454 if (IS_ERR(s35390a->rtc))
0455 return PTR_ERR(s35390a->rtc);
0456
0457 err_read = s35390a_read_status(s35390a, &status1);
0458 if (err_read < 0) {
0459 dev_err(dev, "error resetting chip\n");
0460 return err_read;
0461 }
0462
0463 if (status1 & S35390A_FLAG_24H)
0464 s35390a->twentyfourhour = 1;
0465 else
0466 s35390a->twentyfourhour = 0;
0467
0468 if (status1 & S35390A_FLAG_INT2) {
0469
0470 buf = 0;
0471 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
0472 if (err < 0) {
0473 dev_err(dev, "error disabling alarm");
0474 return err;
0475 }
0476 } else {
0477 err = s35390a_disable_test_mode(s35390a);
0478 if (err < 0) {
0479 dev_err(dev, "error disabling test mode\n");
0480 return err;
0481 }
0482 }
0483
0484 device_set_wakeup_capable(dev, 1);
0485
0486 s35390a->rtc->ops = &s35390a_rtc_ops;
0487 s35390a->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
0488 s35390a->rtc->range_max = RTC_TIMESTAMP_END_2099;
0489
0490 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, s35390a->rtc->features);
0491 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, s35390a->rtc->features );
0492
0493 if (status1 & S35390A_FLAG_INT2)
0494 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
0495
0496 return devm_rtc_register_device(s35390a->rtc);
0497 }
0498
0499 static struct i2c_driver s35390a_driver = {
0500 .driver = {
0501 .name = "rtc-s35390a",
0502 .of_match_table = of_match_ptr(s35390a_of_match),
0503 },
0504 .probe_new = s35390a_probe,
0505 .id_table = s35390a_id,
0506 };
0507
0508 module_i2c_driver(s35390a_driver);
0509
0510 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
0511 MODULE_DESCRIPTION("S35390A RTC driver");
0512 MODULE_LICENSE("GPL");