Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: ds1307: Enable the mcp794xx alarm after programming time

Alarm interrupt enable register is at offset 0x7, while the time
registers for the alarm follow that. When we program Alarm interrupt
enable prior to programming the time, it is possible that previous
time value could be close or match at the time of alarm enable
resulting in interrupt trigger which is unexpected (and does not match
the time we expect it to trigger).

To prevent this scenario from occuring, program the ALM0_EN bit only
after the alarm time is appropriately programmed.

Ofcourse, I2C programming is non-atomic, so there are loopholes where
the interrupt wont trigger if the time requested is in the past at
the time of programming the ALM0_EN bit. However, we will not have
unexpected interrupts while the time is programmed after the interrupt
are enabled.

Signed-off-by: Nishanth Menon <nm@ti.com>
Reviewed-by: Grygorii Strashko <grygorii.strashko@linaro.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>

authored by

Nishanth Menon and committed by
Alexandre Belloni
e3edd671 4be1f6bb

+6 -6
+6 -6
drivers/rtc/rtc-ds1307.c
··· 742 742 regs[6] &= ~MCP794XX_BIT_ALMX_IF; 743 743 /* Set alarm match: second, minute, hour, day, date, month. */ 744 744 regs[6] |= MCP794XX_MSK_ALMX_MATCH; 745 - 746 - if (t->enabled) 747 - regs[0] |= MCP794XX_BIT_ALM0_EN; 748 - else 749 - regs[0] &= ~MCP794XX_BIT_ALM0_EN; 745 + /* Disable interrupt. We will not enable until completely programmed */ 746 + regs[0] &= ~MCP794XX_BIT_ALM0_EN; 750 747 751 748 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs); 752 749 if (ret < 0) 753 750 return ret; 754 751 755 - return 0; 752 + if (!t->enabled) 753 + return 0; 754 + regs[0] |= MCP794XX_BIT_ALM0_EN; 755 + return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]); 756 756 } 757 757 758 758 static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)