39
39
#define ISL1208_REG_SR_BAT (1<<1) /* battery */
40
40
#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
41
41
#define ISL1208_REG_INT 0x08
42
#define ISL1208_REG_INT_ALME (1<<6) /* alarm enable */
43
#define ISL1208_REG_INT_IM (1<<7) /* interrupt/alarm mode */
42
44
#define ISL1208_REG_09 0x09 /* reserved */
43
45
#define ISL1208_REG_ATR 0x0a
44
46
#define ISL1208_REG_DTR 0x0b
207
isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
209
int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
212
dev_err(&client->dev, "%s: reading INT failed\n", __func__);
217
icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
219
icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
221
icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
223
dev_err(&client->dev, "%s: writing INT failed\n", __func__);
205
231
isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
207
233
struct i2c_client *const client = to_i2c_client(dev);
289
315
struct rtc_time *const tm = &alarm->time;
290
316
u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
317
int icr, yr, sr = isl1208_i2c_get_sr(client);
293
sr = isl1208_i2c_get_sr(client);
295
320
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
313
338
bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
314
339
tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
341
/* The alarm doesn't store the year so get it from the rtc section */
342
yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
344
dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
347
tm->tm_year = bcd2bin(yr) + 100;
349
icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
351
dev_err(&client->dev, "%s: reading INT failed\n", __func__);
354
alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
360
isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
362
struct rtc_time *alarm_tm = &alarm->time;
363
u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
364
const int offs = ISL1208_REG_SCA;
365
unsigned long rtc_secs, alarm_secs;
366
struct rtc_time rtc_tm;
369
err = isl1208_i2c_read_time(client, &rtc_tm);
372
err = rtc_tm_to_time(&rtc_tm, &rtc_secs);
375
err = rtc_tm_to_time(alarm_tm, &alarm_secs);
379
/* If the alarm time is before the current time disable the alarm */
380
if (!alarm->enabled || alarm_secs <= rtc_secs)
385
/* Program the alarm and enable it for each setting */
386
regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
387
regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
388
regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
389
ISL1208_REG_HR_MIL | enable;
391
regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
392
regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
393
regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
395
/* write ALARM registers */
396
err = isl1208_i2c_set_regs(client, offs, regs,
397
ISL1208_ALARM_SECTION_LEN);
399
dev_err(&client->dev, "%s: writing ALARM section failed\n",
404
err = isl1208_rtc_toggle_alarm(client, enable);
391
483
return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
487
isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
489
return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
493
isl1208_rtc_interrupt(int irq, void *data)
495
unsigned long timeout = jiffies + msecs_to_jiffies(1000);
496
struct i2c_client *client = data;
497
int handled = 0, sr, err;
500
* I2C reads get NAK'ed if we read straight away after an interrupt?
501
* Using a mdelay/msleep didn't seem to help either, so we work around
502
* this by continually trying to read the register for a short time.
505
sr = isl1208_i2c_get_sr(client);
509
if (time_after(jiffies, timeout)) {
510
dev_err(&client->dev, "%s: reading SR failed\n",
516
if (sr & ISL1208_REG_SR_ALM) {
517
dev_dbg(&client->dev, "alarm!\n");
519
/* Clear the alarm */
520
sr &= ~ISL1208_REG_SR_ALM;
521
sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
523
dev_err(&client->dev, "%s: writing SR failed\n",
528
/* Disable the alarm */
529
err = isl1208_rtc_toggle_alarm(client, 0);
534
return handled ? IRQ_HANDLED : IRQ_NONE;
394
537
static const struct rtc_class_ops isl1208_rtc_ops = {
395
538
.proc = isl1208_rtc_proc,
396
539
.read_time = isl1208_rtc_read_time,
397
540
.set_time = isl1208_rtc_set_time,
398
541
.read_alarm = isl1208_rtc_read_alarm,
399
/*.set_alarm = isl1208_rtc_set_alarm, */
542
.set_alarm = isl1208_rtc_set_alarm,
402
545
/* sysfs interface */
488
631
dev_info(&client->dev,
489
632
"chip found, driver version " DRV_VERSION "\n");
634
if (client->irq > 0) {
635
rc = request_threaded_irq(client->irq, NULL,
636
isl1208_rtc_interrupt,
638
isl1208_driver.driver.name, client);
640
device_init_wakeup(&client->dev, 1);
641
enable_irq_wake(client->irq);
643
dev_err(&client->dev,
644
"Unable to request irq %d, no alarm support\n",
491
650
rtc = rtc_device_register(isl1208_driver.driver.name,
492
651
&client->dev, &isl1208_rtc_ops,
497
658
i2c_set_clientdata(client, rtc);