rtc-isl1208.c 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Intersil ISL1208 rtc class driver
 *
 * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/bcd.h>
#include <linux/rtc.h>
17
#include "rtc-core.h"
18
#include <linux/of_irq.h>
19 20 21 22 23 24

/* Register map */
/* rtc section */
#define ISL1208_REG_SC  0x00
#define ISL1208_REG_MN  0x01
#define ISL1208_REG_HR  0x02
25 26
#define ISL1208_REG_HR_MIL     (1<<7)	/* 24h/12h mode */
#define ISL1208_REG_HR_PM      (1<<5)	/* PM/AM bit in 12h mode */
27 28 29 30 31 32 33 34
#define ISL1208_REG_DT  0x03
#define ISL1208_REG_MO  0x04
#define ISL1208_REG_YR  0x05
#define ISL1208_REG_DW  0x06
#define ISL1208_RTC_SECTION_LEN 7

/* control/status section */
#define ISL1208_REG_SR  0x07
35 36 37
#define ISL1208_REG_SR_ARST    (1<<7)	/* auto reset */
#define ISL1208_REG_SR_XTOSCB  (1<<6)	/* crystal oscillator */
#define ISL1208_REG_SR_WRTC    (1<<4)	/* write rtc */
38
#define ISL1208_REG_SR_EVT     (1<<3)	/* event */
39 40 41
#define ISL1208_REG_SR_ALM     (1<<2)	/* alarm */
#define ISL1208_REG_SR_BAT     (1<<1)	/* battery */
#define ISL1208_REG_SR_RTCF    (1<<0)	/* rtc fail */
42
#define ISL1208_REG_INT 0x08
43 44
#define ISL1208_REG_INT_ALME   (1<<6)   /* alarm enable */
#define ISL1208_REG_INT_IM     (1<<7)   /* interrupt/alarm mode */
45 46
#define ISL1219_REG_EV  0x09
#define ISL1219_REG_EV_EVEN    (1<<4)   /* event detection enable */
47
#define ISL1219_REG_EV_EVIENB  (1<<7)   /* event in pull-up disable */
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#define ISL1208_REG_ATR 0x0a
#define ISL1208_REG_DTR 0x0b

/* alarm section */
#define ISL1208_REG_SCA 0x0c
#define ISL1208_REG_MNA 0x0d
#define ISL1208_REG_HRA 0x0e
#define ISL1208_REG_DTA 0x0f
#define ISL1208_REG_MOA 0x10
#define ISL1208_REG_DWA 0x11
#define ISL1208_ALARM_SECTION_LEN 6

/* user section */
#define ISL1208_REG_USR1 0x12
#define ISL1208_REG_USR2 0x13
#define ISL1208_USR_SECTION_LEN 2

65 66 67 68 69 70 71 72 73
/* event section */
#define ISL1219_REG_SCT 0x14
#define ISL1219_REG_MNT 0x15
#define ISL1219_REG_HRT 0x16
#define ISL1219_REG_DTT 0x17
#define ISL1219_REG_MOT 0x18
#define ISL1219_REG_YRT 0x19
#define ISL1219_EVT_SECTION_LEN 6

74
static struct i2c_driver isl1208_driver;
75

76 77 78 79 80 81 82
/* ISL1208 various variants */
enum {
	TYPE_ISL1208 = 0,
	TYPE_ISL1218,
	TYPE_ISL1219,
};

83 84 85
/* block read */
static int
isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
86
		      unsigned len)
87 88 89
{
	u8 reg_addr[1] = { reg };
	struct i2c_msg msgs[2] = {
90 91 92 93 94 95 96 97 98 99 100
		{
			.addr = client->addr,
			.len = sizeof(reg_addr),
			.buf = reg_addr
		},
		{
			.addr = client->addr,
			.flags = I2C_M_RD,
			.len = len,
			.buf = buf
		}
101 102 103
	};
	int ret;

104 105
	WARN_ON(reg > ISL1219_REG_YRT);
	WARN_ON(reg + len > ISL1219_REG_YRT + 1);
106 107 108 109 110 111 112 113 114 115

	ret = i2c_transfer(client->adapter, msgs, 2);
	if (ret > 0)
		ret = 0;
	return ret;
}

/* block write */
static int
isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
116
		     unsigned len)
117 118 119
{
	u8 i2c_buf[ISL1208_REG_USR2 + 2];
	struct i2c_msg msgs[1] = {
120 121 122 123 124
		{
			.addr = client->addr,
			.len = len + 1,
			.buf = i2c_buf
		}
125 126 127
	};
	int ret;

128 129
	WARN_ON(reg > ISL1219_REG_YRT);
	WARN_ON(reg + len > ISL1219_REG_YRT + 1);
130 131 132 133 134 135 136 137 138 139

	i2c_buf[0] = reg;
	memcpy(&i2c_buf[1], &buf[0], len);

	ret = i2c_transfer(client->adapter, msgs, 1);
	if (ret > 0)
		ret = 0;
	return ret;
}

140
/* simple check to see whether we have a isl1208 */
141 142
static int
isl1208_i2c_validate_client(struct i2c_client *client)
143 144 145 146 147 148 149 150 151 152 153 154 155
{
	u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
	u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
		0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
	};
	int i;
	int ret;

	ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
	if (ret < 0)
		return ret;

	for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
156
		if (regs[i] & zero_mask[i])	/* check if bits are cleared */
157 158 159 160 161 162
			return -ENODEV;
	}

	return 0;
}

163 164
static int
isl1208_i2c_get_sr(struct i2c_client *client)
165
{
166
	return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
167 168
}

169 170
static int
isl1208_i2c_get_atr(struct i2c_client *client)
171 172 173
{
	int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
	if (atr < 0)
174
		return atr;
175 176 177 178 179 180 181 182 183 184 185 186

	/* The 6bit value in the ATR register controls the load
	 * capacitance C_load * in steps of 0.25pF
	 *
	 * bit (1<<5) of the ATR register is inverted
	 *
	 * C_load(ATR=0x20) =  4.50pF
	 * C_load(ATR=0x00) = 12.50pF
	 * C_load(ATR=0x1f) = 20.25pF
	 *
	 */

187 188 189
	atr &= 0x3f;		/* mask out lsb */
	atr ^= 1 << 5;		/* invert 6th bit */
	atr += 2 * 9;		/* add offset of 4.5pF; unit[atr] = 0.25pF */
190 191 192 193

	return atr;
}

194 195
static int
isl1208_i2c_get_dtr(struct i2c_client *client)
196 197 198 199 200 201
{
	int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
	if (dtr < 0)
		return -EIO;

	/* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
202
	dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
203 204 205 206

	return dtr;
}

207 208
static int
isl1208_i2c_get_usr(struct i2c_client *client)
209 210 211 212
{
	u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
	int ret;

213 214
	ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
				    ISL1208_USR_SECTION_LEN);
215 216 217 218 219 220
	if (ret < 0)
		return ret;

	return (buf[1] << 8) | buf[0];
}

221 222
static int
isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
223 224 225 226 227 228
{
	u8 buf[ISL1208_USR_SECTION_LEN];

	buf[0] = usr & 0xff;
	buf[1] = (usr >> 8) & 0xff;

229 230
	return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
				    ISL1208_USR_SECTION_LEN);
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
static int
isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
{
	int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);

	if (icr < 0) {
		dev_err(&client->dev, "%s: reading INT failed\n", __func__);
		return icr;
	}

	if (enable)
		icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
	else
		icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);

	icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
	if (icr < 0) {
		dev_err(&client->dev, "%s: writing INT failed\n", __func__);
		return icr;
	}

	return 0;
}

257 258
static int
isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
{
	struct i2c_client *const client = to_i2c_client(dev);
	int sr, dtr, atr, usr;

	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return sr;
	}

	seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
		   (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
		   (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
		   (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
		   (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
		   (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
275
		   (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
276 277 278 279 280

	seq_printf(seq, "batt_status\t: %s\n",
		   (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");

	dtr = isl1208_i2c_get_dtr(client);
281
	if (dtr >= 0 - 1)
282 283 284 285 286
		seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);

	atr = isl1208_i2c_get_atr(client);
	if (atr >= 0)
		seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
287
			   atr >> 2, (atr & 0x3) * 25);
288 289 290 291 292 293 294 295

	usr = isl1208_i2c_get_usr(client);
	if (usr >= 0)
		seq_printf(seq, "user_data\t: 0x%.4x\n", usr);

	return 0;
}

296 297
static int
isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
{
	int sr;
	u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };

	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return -EIO;
	}

	sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading RTC section failed\n",
			__func__);
		return sr;
	}

A
Adrian Bunk 已提交
315 316
	tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
	tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
317 318 319

	/* HR field has a more complex interpretation */
	{
320
		const u8 _hr = regs[ISL1208_REG_HR];
321
		if (_hr & ISL1208_REG_HR_MIL)	/* 24h format */
A
Adrian Bunk 已提交
322
			tm->tm_hour = bcd2bin(_hr & 0x3f);
323 324
		else {
			/* 12h format */
A
Adrian Bunk 已提交
325
			tm->tm_hour = bcd2bin(_hr & 0x1f);
326
			if (_hr & ISL1208_REG_HR_PM)	/* PM flag set */
327 328 329 330
				tm->tm_hour += 12;
		}
	}

A
Adrian Bunk 已提交
331 332 333 334
	tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
	tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1;	/* rtc starts at 1 */
	tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
	tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
335 336 337 338

	return 0;
}

339 340
static int
isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
341 342 343
{
	struct rtc_time *const tm = &alarm->time;
	u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
344
	int icr, yr, sr = isl1208_i2c_get_sr(client);
345 346 347 348 349 350 351

	if (sr < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return sr;
	}

	sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
352
				   ISL1208_ALARM_SECTION_LEN);
353 354 355 356 357 358 359
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading alarm section failed\n",
			__func__);
		return sr;
	}

	/* MSB of each alarm register is an enable bit */
A
Adrian Bunk 已提交
360 361 362 363
	tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
	tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
	tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
	tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
364
	tm->tm_mon =
A
Adrian Bunk 已提交
365 366
		bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
	tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	/* The alarm doesn't store the year so get it from the rtc section */
	yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
	if (yr < 0) {
		dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
		return yr;
	}
	tm->tm_year = bcd2bin(yr) + 100;

	icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
	if (icr < 0) {
		dev_err(&client->dev, "%s: reading INT failed\n", __func__);
		return icr;
	}
	alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);

	return 0;
}

static int
isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
{
	struct rtc_time *alarm_tm = &alarm->time;
	u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
	const int offs = ISL1208_REG_SCA;
	struct rtc_time rtc_tm;
	int err, enable;

	err = isl1208_i2c_read_time(client, &rtc_tm);
	if (err)
		return err;

	/* If the alarm time is before the current time disable the alarm */
400
	if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
		enable = 0x00;
	else
		enable = 0x80;

	/* Program the alarm and enable it for each setting */
	regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
	regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
	regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
		ISL1208_REG_HR_MIL | enable;

	regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
	regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
	regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;

	/* write ALARM registers */
	err = isl1208_i2c_set_regs(client, offs, regs,
				  ISL1208_ALARM_SECTION_LEN);
	if (err < 0) {
		dev_err(&client->dev, "%s: writing ALARM section failed\n",
			__func__);
		return err;
	}

	err = isl1208_rtc_toggle_alarm(client, enable);
	if (err)
		return err;

428 429 430
	return 0;
}

431 432
static int
isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
433 434 435 436
{
	return isl1208_i2c_read_time(to_i2c_client(dev), tm);
}

437 438
static int
isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
439 440 441 442
{
	int sr;
	u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };

443 444 445 446 447 448 449
	/* The clock has an 8 bit wide bcd-coded register (they never learn)
	 * for the year. tm_year is an offset from 1900 and we are interested
	 * in the 2000-2099 range, so any value less than 100 is invalid.
	 */
	if (tm->tm_year < 100)
		return -EINVAL;

A
Adrian Bunk 已提交
450 451 452
	regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
	regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
	regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
453

A
Adrian Bunk 已提交
454 455 456
	regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
	regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
	regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
457

A
Adrian Bunk 已提交
458
	regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
459 460 461 462 463 464 465 466

	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return sr;
	}

	/* set WRTC */
467
	sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
				       sr | ISL1208_REG_SR_WRTC);
	if (sr < 0) {
		dev_err(&client->dev, "%s: writing SR failed\n", __func__);
		return sr;
	}

	/* write RTC registers */
	sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
	if (sr < 0) {
		dev_err(&client->dev, "%s: writing RTC section failed\n",
			__func__);
		return sr;
	}

	/* clear WRTC again */
483 484 485 486 487
	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return sr;
	}
488
	sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
489 490 491 492 493 494 495 496 497 498
				       sr & ~ISL1208_REG_SR_WRTC);
	if (sr < 0) {
		dev_err(&client->dev, "%s: writing SR failed\n", __func__);
		return sr;
	}

	return 0;
}


499 500
static int
isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
501 502 503 504
{
	return isl1208_i2c_set_time(to_i2c_client(dev), tm);
}

505 506
static int
isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
507 508 509 510
{
	return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
}

511 512 513 514 515 516
static int
isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
	return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
}

517 518 519 520
static ssize_t timestamp0_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
521
	struct i2c_client *client = to_i2c_client(dev->parent);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	int sr;

	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(dev, "%s: reading SR failed\n", __func__);
		return sr;
	}

	sr &= ~ISL1208_REG_SR_EVT;

	sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
	if (sr < 0)
		dev_err(dev, "%s: writing SR failed\n",
			__func__);

	return count;
};

static ssize_t timestamp0_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
543
	struct i2c_client *client = to_i2c_client(dev->parent);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
	struct rtc_time tm;
	int sr;

	sr = isl1208_i2c_get_sr(client);
	if (sr < 0) {
		dev_err(dev, "%s: reading SR failed\n", __func__);
		return sr;
	}

	if (!(sr & ISL1208_REG_SR_EVT))
		return 0;

	sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
				   ISL1219_EVT_SECTION_LEN);
	if (sr < 0) {
		dev_err(dev, "%s: reading event section failed\n",
			__func__);
		return 0;
	}

	/* MSB of each alarm register is an enable bit */
	tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
	tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
	tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
	tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
	tm.tm_mon =
		bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
	tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;

	sr = rtc_valid_tm(&tm);
	if (sr)
		return sr;

	return sprintf(buf, "%llu\n",
				(unsigned long long)rtc_tm_to_time64(&tm));
};

static DEVICE_ATTR_RW(timestamp0);

584 585 586 587 588
static irqreturn_t
isl1208_rtc_interrupt(int irq, void *data)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
	struct i2c_client *client = data;
589
	struct rtc_device *rtc = i2c_get_clientdata(client);
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	int handled = 0, sr, err;

	/*
	 * I2C reads get NAK'ed if we read straight away after an interrupt?
	 * Using a mdelay/msleep didn't seem to help either, so we work around
	 * this by continually trying to read the register for a short time.
	 */
	while (1) {
		sr = isl1208_i2c_get_sr(client);
		if (sr >= 0)
			break;

		if (time_after(jiffies, timeout)) {
			dev_err(&client->dev, "%s: reading SR failed\n",
				__func__);
			return sr;
		}
	}

	if (sr & ISL1208_REG_SR_ALM) {
		dev_dbg(&client->dev, "alarm!\n");

612 613
		rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		/* Clear the alarm */
		sr &= ~ISL1208_REG_SR_ALM;
		sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
		if (sr < 0)
			dev_err(&client->dev, "%s: writing SR failed\n",
				__func__);
		else
			handled = 1;

		/* Disable the alarm */
		err = isl1208_rtc_toggle_alarm(client, 0);
		if (err)
			return err;
	}

629 630 631 632 633 634 635
	if (sr & ISL1208_REG_SR_EVT) {
		sysfs_notify(&rtc->dev.kobj, NULL,
			     dev_attr_timestamp0.attr.name);
		dev_warn(&client->dev, "event detected");
		handled = 1;
	}

636 637 638
	return handled ? IRQ_HANDLED : IRQ_NONE;
}

639
static const struct rtc_class_ops isl1208_rtc_ops = {
640 641 642 643
	.proc = isl1208_rtc_proc,
	.read_time = isl1208_rtc_read_time,
	.set_time = isl1208_rtc_set_time,
	.read_alarm = isl1208_rtc_read_alarm,
644
	.set_alarm = isl1208_rtc_set_alarm,
645 646 647 648
};

/* sysfs interface */

649 650 651
static ssize_t
isl1208_sysfs_show_atrim(struct device *dev,
			 struct device_attribute *attr, char *buf)
652
{
653
	int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
654 655 656
	if (atr < 0)
		return atr;

657
	return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
658
}
659

660 661
static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);

662 663 664
static ssize_t
isl1208_sysfs_show_dtrim(struct device *dev,
			 struct device_attribute *attr, char *buf)
665
{
666
	int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
667 668 669 670 671
	if (dtr < 0)
		return dtr;

	return sprintf(buf, "%d ppm\n", dtr);
}
672

673 674
static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);

675 676 677
static ssize_t
isl1208_sysfs_show_usr(struct device *dev,
		       struct device_attribute *attr, char *buf)
678
{
679
	int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
680 681 682 683 684 685
	if (usr < 0)
		return usr;

	return sprintf(buf, "0x%.4x\n", usr);
}

686 687 688 689
static ssize_t
isl1208_sysfs_store_usr(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count)
690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
	int usr = -1;

	if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
		if (sscanf(buf, "%x", &usr) != 1)
			return -EINVAL;
	} else {
		if (sscanf(buf, "%d", &usr) != 1)
			return -EINVAL;
	}

	if (usr < 0 || usr > 0xffff)
		return -EINVAL;

704 705 706 707
	if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
		return -EIO;

	return count;
708
}
709

710 711 712
static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
		   isl1208_sysfs_store_usr);

713 714 715 716 717 718
static struct attribute *isl1208_rtc_attrs[] = {
	&dev_attr_atrim.attr,
	&dev_attr_dtrim.attr,
	&dev_attr_usr.attr,
	NULL
};
719

720 721 722
static const struct attribute_group isl1208_rtc_sysfs_files = {
	.attrs	= isl1208_rtc_attrs,
};
723

724 725 726 727 728 729 730 731 732
static struct attribute *isl1219_rtc_attrs[] = {
	&dev_attr_timestamp0.attr,
	NULL
};

static const struct attribute_group isl1219_rtc_sysfs_files = {
	.attrs	= isl1219_rtc_attrs,
};

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
static int isl1208_setup_irq(struct i2c_client *client, int irq)
{
	int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
					isl1208_rtc_interrupt,
					IRQF_SHARED | IRQF_ONESHOT,
					isl1208_driver.driver.name,
					client);
	if (!rc) {
		device_init_wakeup(&client->dev, 1);
		enable_irq_wake(irq);
	} else {
		dev_err(&client->dev,
			"Unable to request irq %d, no alarm support\n",
			irq);
	}
	return rc;
}

751
static int
752
isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
753 754 755
{
	int rc = 0;
	struct rtc_device *rtc;
756
	int evdet_irq = -1;
757

758 759
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
		return -ENODEV;
760

761 762 763
	if (isl1208_i2c_validate_client(client) < 0)
		return -ENODEV;

764
	rtc = devm_rtc_allocate_device(&client->dev);
765 766
	if (IS_ERR(rtc))
		return PTR_ERR(rtc);
767

768 769
	rtc->ops = &isl1208_rtc_ops;

770
	i2c_set_clientdata(client, rtc);
771

772
	rc = isl1208_i2c_get_sr(client);
773
	if (rc < 0) {
774
		dev_err(&client->dev, "reading status failed\n");
775
		return rc;
776 777 778
	}

	if (rc & ISL1208_REG_SR_RTCF)
779
		dev_warn(&client->dev, "rtc power failure detected, "
780 781
			 "please set clock.\n");

782
	if (id->driver_data == TYPE_ISL1219) {
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		struct device_node *np = client->dev.of_node;
		u32 evienb;

		rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
		if (rc < 0) {
			dev_err(&client->dev, "failed to read EV reg\n");
			return rc;
		}
		rc |= ISL1219_REG_EV_EVEN;
		if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
			if (evienb)
				rc |= ISL1219_REG_EV_EVIENB;
			else
				rc &= ~ISL1219_REG_EV_EVIENB;
		}
		rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
799 800 801 802 803 804 805
		if (rc < 0) {
			dev_err(&client->dev, "could not enable tamper detection\n");
			return rc;
		}
		rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
		if (rc)
			return rc;
806
		evdet_irq = of_irq_get_byname(np, "evdet");
807 808
	}

809
	rc = rtc_add_group(rtc, &isl1208_rtc_sysfs_files);
810
	if (rc)
811
		return rc;
812

813 814 815 816 817 818 819 820 821
	if (client->irq > 0)
		rc = isl1208_setup_irq(client, client->irq);
	if (rc)
		return rc;

	if (evdet_irq > 0 && evdet_irq != client->irq)
		rc = isl1208_setup_irq(client, evdet_irq);
	if (rc)
		return rc;
822

823
	return rtc_register_device(rtc);
824 825
}

826
static const struct i2c_device_id isl1208_id[] = {
827 828 829
	{ "isl1208", TYPE_ISL1208 },
	{ "isl1218", TYPE_ISL1218 },
	{ "isl1219", TYPE_ISL1219 },
830 831 832 833
	{ }
};
MODULE_DEVICE_TABLE(i2c, isl1208_id);

834 835 836
static const struct of_device_id isl1208_of_match[] = {
	{ .compatible = "isil,isl1208" },
	{ .compatible = "isil,isl1218" },
837
	{ .compatible = "isil,isl1219" },
838 839 840 841
	{ }
};
MODULE_DEVICE_TABLE(of, isl1208_of_match);

842 843
static struct i2c_driver isl1208_driver = {
	.driver = {
844 845 846
		.name = "rtc-isl1208",
		.of_match_table = of_match_ptr(isl1208_of_match),
	},
847
	.probe = isl1208_probe,
848
	.id_table = isl1208_id,
849
};
850

851
module_i2c_driver(isl1208_driver);
852 853 854 855

MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
MODULE_LICENSE("GPL");