xref: /linux/drivers/rtc/rtc-rx8025.c (revision 0abc920116303e81702a38429a1b61a896e02b37)
13c2b9075SWolfgang Grandegger /*
23c2b9075SWolfgang Grandegger  * Driver for Epson's RTC module RX-8025 SA/NB
33c2b9075SWolfgang Grandegger  *
43c2b9075SWolfgang Grandegger  * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
53c2b9075SWolfgang Grandegger  *
63c2b9075SWolfgang Grandegger  * Copyright (C) 2005 by Digi International Inc.
73c2b9075SWolfgang Grandegger  * All rights reserved.
83c2b9075SWolfgang Grandegger  *
93c2b9075SWolfgang Grandegger  * Modified by fengjh at rising.com.cn
103c2b9075SWolfgang Grandegger  * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors>
113c2b9075SWolfgang Grandegger  * 2006.11
123c2b9075SWolfgang Grandegger  *
133c2b9075SWolfgang Grandegger  * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
143c2b9075SWolfgang Grandegger  * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
153c2b9075SWolfgang Grandegger  * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
163c2b9075SWolfgang Grandegger  *
173c2b9075SWolfgang Grandegger  * This program is free software; you can redistribute it and/or
183c2b9075SWolfgang Grandegger  * modify it under the terms of the GNU General Public License
193c2b9075SWolfgang Grandegger  * version 2 as published by the Free Software Foundation.
203c2b9075SWolfgang Grandegger  */
213c2b9075SWolfgang Grandegger #include <linux/kernel.h>
223c2b9075SWolfgang Grandegger #include <linux/module.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
243c2b9075SWolfgang Grandegger #include <linux/init.h>
253c2b9075SWolfgang Grandegger #include <linux/bcd.h>
263c2b9075SWolfgang Grandegger #include <linux/i2c.h>
273c2b9075SWolfgang Grandegger #include <linux/list.h>
283c2b9075SWolfgang Grandegger #include <linux/rtc.h>
293c2b9075SWolfgang Grandegger 
303c2b9075SWolfgang Grandegger /* Register definitions */
313c2b9075SWolfgang Grandegger #define RX8025_REG_SEC		0x00
323c2b9075SWolfgang Grandegger #define RX8025_REG_MIN		0x01
333c2b9075SWolfgang Grandegger #define RX8025_REG_HOUR		0x02
343c2b9075SWolfgang Grandegger #define RX8025_REG_WDAY		0x03
353c2b9075SWolfgang Grandegger #define RX8025_REG_MDAY		0x04
363c2b9075SWolfgang Grandegger #define RX8025_REG_MONTH	0x05
373c2b9075SWolfgang Grandegger #define RX8025_REG_YEAR		0x06
383c2b9075SWolfgang Grandegger #define RX8025_REG_DIGOFF	0x07
393c2b9075SWolfgang Grandegger #define RX8025_REG_ALWMIN	0x08
403c2b9075SWolfgang Grandegger #define RX8025_REG_ALWHOUR	0x09
413c2b9075SWolfgang Grandegger #define RX8025_REG_ALWWDAY	0x0a
423c2b9075SWolfgang Grandegger #define RX8025_REG_ALDMIN	0x0b
433c2b9075SWolfgang Grandegger #define RX8025_REG_ALDHOUR	0x0c
443c2b9075SWolfgang Grandegger /* 0x0d is reserved */
453c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL1	0x0e
463c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL2	0x0f
473c2b9075SWolfgang Grandegger 
483c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT	(7 << 0)
493c2b9075SWolfgang Grandegger /* 1 Hz periodic level irq */
503c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT_1HZ	4
513c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_TEST	(1 << 3)
523c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_1224	(1 << 5)
533c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_DALE	(1 << 6)
543c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_WALE	(1 << 7)
553c2b9075SWolfgang Grandegger 
563c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_DAFG	(1 << 0)
573c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_WAFG	(1 << 1)
583c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_CTFG	(1 << 2)
593c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_PON	(1 << 4)
603c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_XST	(1 << 5)
613c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL2_VDET	(1 << 6)
623c2b9075SWolfgang Grandegger 
633c2b9075SWolfgang Grandegger /* Clock precision adjustment */
643c2b9075SWolfgang Grandegger #define RX8025_ADJ_RESOLUTION	3050 /* in ppb */
653c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MAX	62
663c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MIN	-62
673c2b9075SWolfgang Grandegger 
683c2b9075SWolfgang Grandegger static const struct i2c_device_id rx8025_id[] = {
693c2b9075SWolfgang Grandegger 	{ "rx8025", 0 },
703c2b9075SWolfgang Grandegger 	{ }
713c2b9075SWolfgang Grandegger };
723c2b9075SWolfgang Grandegger MODULE_DEVICE_TABLE(i2c, rx8025_id);
733c2b9075SWolfgang Grandegger 
743c2b9075SWolfgang Grandegger struct rx8025_data {
753c2b9075SWolfgang Grandegger 	struct i2c_client *client;
763c2b9075SWolfgang Grandegger 	struct rtc_device *rtc;
773c2b9075SWolfgang Grandegger 	struct work_struct work;
783c2b9075SWolfgang Grandegger 	u8 ctrl1;
793c2b9075SWolfgang Grandegger 	unsigned exiting:1;
803c2b9075SWolfgang Grandegger };
813c2b9075SWolfgang Grandegger 
823c2b9075SWolfgang Grandegger static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value)
833c2b9075SWolfgang Grandegger {
843c2b9075SWolfgang Grandegger 	int ret = i2c_smbus_read_byte_data(client, (number << 4) | 0x08);
853c2b9075SWolfgang Grandegger 
863c2b9075SWolfgang Grandegger 	if (ret < 0) {
873c2b9075SWolfgang Grandegger 		dev_err(&client->dev, "Unable to read register #%d\n", number);
883c2b9075SWolfgang Grandegger 		return ret;
893c2b9075SWolfgang Grandegger 	}
903c2b9075SWolfgang Grandegger 
913c2b9075SWolfgang Grandegger 	*value = ret;
923c2b9075SWolfgang Grandegger 	return 0;
933c2b9075SWolfgang Grandegger }
943c2b9075SWolfgang Grandegger 
953c2b9075SWolfgang Grandegger static int rx8025_read_regs(struct i2c_client *client,
963c2b9075SWolfgang Grandegger 			    int number, u8 length, u8 *values)
973c2b9075SWolfgang Grandegger {
983c2b9075SWolfgang Grandegger 	int ret = i2c_smbus_read_i2c_block_data(client, (number << 4) | 0x08,
993c2b9075SWolfgang Grandegger 						length, values);
1003c2b9075SWolfgang Grandegger 
1013c2b9075SWolfgang Grandegger 	if (ret != length) {
1023c2b9075SWolfgang Grandegger 		dev_err(&client->dev, "Unable to read registers #%d..#%d\n",
1033c2b9075SWolfgang Grandegger 			number, number + length - 1);
1043c2b9075SWolfgang Grandegger 		return ret < 0 ? ret : -EIO;
1053c2b9075SWolfgang Grandegger 	}
1063c2b9075SWolfgang Grandegger 
1073c2b9075SWolfgang Grandegger 	return 0;
1083c2b9075SWolfgang Grandegger }
1093c2b9075SWolfgang Grandegger 
1103c2b9075SWolfgang Grandegger static int rx8025_write_reg(struct i2c_client *client, int number, u8 value)
1113c2b9075SWolfgang Grandegger {
1123c2b9075SWolfgang Grandegger 	int ret = i2c_smbus_write_byte_data(client, number << 4, value);
1133c2b9075SWolfgang Grandegger 
1143c2b9075SWolfgang Grandegger 	if (ret)
1153c2b9075SWolfgang Grandegger 		dev_err(&client->dev, "Unable to write register #%d\n",
1163c2b9075SWolfgang Grandegger 			number);
1173c2b9075SWolfgang Grandegger 
1183c2b9075SWolfgang Grandegger 	return ret;
1193c2b9075SWolfgang Grandegger }
1203c2b9075SWolfgang Grandegger 
1213c2b9075SWolfgang Grandegger static int rx8025_write_regs(struct i2c_client *client,
1223c2b9075SWolfgang Grandegger 			     int number, u8 length, u8 *values)
1233c2b9075SWolfgang Grandegger {
1243c2b9075SWolfgang Grandegger 	int ret = i2c_smbus_write_i2c_block_data(client, (number << 4) | 0x08,
1253c2b9075SWolfgang Grandegger 						 length, values);
1263c2b9075SWolfgang Grandegger 
1273c2b9075SWolfgang Grandegger 	if (ret)
1283c2b9075SWolfgang Grandegger 		dev_err(&client->dev, "Unable to write registers #%d..#%d\n",
1293c2b9075SWolfgang Grandegger 			number, number + length - 1);
1303c2b9075SWolfgang Grandegger 
1313c2b9075SWolfgang Grandegger 	return ret;
1323c2b9075SWolfgang Grandegger }
1333c2b9075SWolfgang Grandegger 
1343c2b9075SWolfgang Grandegger static irqreturn_t rx8025_irq(int irq, void *dev_id)
1353c2b9075SWolfgang Grandegger {
1363c2b9075SWolfgang Grandegger 	struct i2c_client *client = dev_id;
1373c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
1383c2b9075SWolfgang Grandegger 
1393c2b9075SWolfgang Grandegger 	disable_irq_nosync(irq);
1403c2b9075SWolfgang Grandegger 	schedule_work(&rx8025->work);
1413c2b9075SWolfgang Grandegger 	return IRQ_HANDLED;
1423c2b9075SWolfgang Grandegger }
1433c2b9075SWolfgang Grandegger 
1443c2b9075SWolfgang Grandegger static void rx8025_work(struct work_struct *work)
1453c2b9075SWolfgang Grandegger {
1463c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = container_of(work, struct rx8025_data,
1473c2b9075SWolfgang Grandegger 						  work);
1483c2b9075SWolfgang Grandegger 	struct i2c_client *client = rx8025->client;
1493c2b9075SWolfgang Grandegger 	struct mutex *lock = &rx8025->rtc->ops_lock;
1503c2b9075SWolfgang Grandegger 	u8 status;
1513c2b9075SWolfgang Grandegger 
1523c2b9075SWolfgang Grandegger 	mutex_lock(lock);
1533c2b9075SWolfgang Grandegger 
1543c2b9075SWolfgang Grandegger 	if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status))
1553c2b9075SWolfgang Grandegger 		goto out;
1563c2b9075SWolfgang Grandegger 
1573c2b9075SWolfgang Grandegger 	if (!(status & RX8025_BIT_CTRL2_XST))
1583c2b9075SWolfgang Grandegger 		dev_warn(&client->dev, "Oscillation stop was detected,"
1593c2b9075SWolfgang Grandegger 			 "you may have to readjust the clock\n");
1603c2b9075SWolfgang Grandegger 
1613c2b9075SWolfgang Grandegger 	if (status & RX8025_BIT_CTRL2_CTFG) {
1623c2b9075SWolfgang Grandegger 		/* periodic */
1633c2b9075SWolfgang Grandegger 		status &= ~RX8025_BIT_CTRL2_CTFG;
1643c2b9075SWolfgang Grandegger 		local_irq_disable();
1653c2b9075SWolfgang Grandegger 		rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
1663c2b9075SWolfgang Grandegger 		local_irq_enable();
1673c2b9075SWolfgang Grandegger 	}
1683c2b9075SWolfgang Grandegger 
1693c2b9075SWolfgang Grandegger 	if (status & RX8025_BIT_CTRL2_DAFG) {
1703c2b9075SWolfgang Grandegger 		/* alarm */
1713c2b9075SWolfgang Grandegger 		status &= RX8025_BIT_CTRL2_DAFG;
1723c2b9075SWolfgang Grandegger 		if (rx8025_write_reg(client, RX8025_REG_CTRL1,
1733c2b9075SWolfgang Grandegger 				     rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
1743c2b9075SWolfgang Grandegger 			goto out;
1753c2b9075SWolfgang Grandegger 		local_irq_disable();
1763c2b9075SWolfgang Grandegger 		rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
1773c2b9075SWolfgang Grandegger 		local_irq_enable();
1783c2b9075SWolfgang Grandegger 	}
1793c2b9075SWolfgang Grandegger 
1803c2b9075SWolfgang Grandegger 	/* acknowledge IRQ */
1813c2b9075SWolfgang Grandegger 	rx8025_write_reg(client, RX8025_REG_CTRL2,
1823c2b9075SWolfgang Grandegger 			 status | RX8025_BIT_CTRL2_XST);
1833c2b9075SWolfgang Grandegger 
1843c2b9075SWolfgang Grandegger out:
1853c2b9075SWolfgang Grandegger 	if (!rx8025->exiting)
1863c2b9075SWolfgang Grandegger 		enable_irq(client->irq);
1873c2b9075SWolfgang Grandegger 
1883c2b9075SWolfgang Grandegger 	mutex_unlock(lock);
1893c2b9075SWolfgang Grandegger }
1903c2b9075SWolfgang Grandegger 
1913c2b9075SWolfgang Grandegger static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
1923c2b9075SWolfgang Grandegger {
1933c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
1943c2b9075SWolfgang Grandegger 	u8 date[7];
1953c2b9075SWolfgang Grandegger 	int err;
1963c2b9075SWolfgang Grandegger 
1973c2b9075SWolfgang Grandegger 	err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
1983c2b9075SWolfgang Grandegger 	if (err)
1993c2b9075SWolfgang Grandegger 		return err;
2003c2b9075SWolfgang Grandegger 
2013c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: read 0x%02x 0x%02x "
2023c2b9075SWolfgang Grandegger 		"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
2033c2b9075SWolfgang Grandegger 		date[0], date[1], date[2], date[3], date[4],
2043c2b9075SWolfgang Grandegger 		date[5], date[6]);
2053c2b9075SWolfgang Grandegger 
2063c2b9075SWolfgang Grandegger 	dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
2073c2b9075SWolfgang Grandegger 	dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
2083c2b9075SWolfgang Grandegger 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
2093c2b9075SWolfgang Grandegger 		dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
2103c2b9075SWolfgang Grandegger 	else
2113c2b9075SWolfgang Grandegger 		dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
2123c2b9075SWolfgang Grandegger 			+ (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
2133c2b9075SWolfgang Grandegger 
2143c2b9075SWolfgang Grandegger 	dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
2153c2b9075SWolfgang Grandegger 	dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
2163c2b9075SWolfgang Grandegger 	dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]);
2173c2b9075SWolfgang Grandegger 
2183c2b9075SWolfgang Grandegger 	if (dt->tm_year < 70)
2193c2b9075SWolfgang Grandegger 		dt->tm_year += 100;
2203c2b9075SWolfgang Grandegger 
2213c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
2223c2b9075SWolfgang Grandegger 		dt->tm_sec, dt->tm_min, dt->tm_hour,
2233c2b9075SWolfgang Grandegger 		dt->tm_mday, dt->tm_mon, dt->tm_year);
2243c2b9075SWolfgang Grandegger 
2253c2b9075SWolfgang Grandegger 	return rtc_valid_tm(dt);
2263c2b9075SWolfgang Grandegger }
2273c2b9075SWolfgang Grandegger 
2283c2b9075SWolfgang Grandegger static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
2293c2b9075SWolfgang Grandegger {
2303c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
2313c2b9075SWolfgang Grandegger 	u8 date[7];
2323c2b9075SWolfgang Grandegger 
2333c2b9075SWolfgang Grandegger 	/*
2343c2b9075SWolfgang Grandegger 	 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
2353c2b9075SWolfgang Grandegger 	 * year.  Next time this is wrong is 2100, which will not be a leap
2363c2b9075SWolfgang Grandegger 	 * year.
2373c2b9075SWolfgang Grandegger 	 */
2383c2b9075SWolfgang Grandegger 
2393c2b9075SWolfgang Grandegger 	/*
2403c2b9075SWolfgang Grandegger 	 * Here the read-only bits are written as "0".  I'm not sure if that
2413c2b9075SWolfgang Grandegger 	 * is sound.
2423c2b9075SWolfgang Grandegger 	 */
2433c2b9075SWolfgang Grandegger 	date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
2443c2b9075SWolfgang Grandegger 	date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
2453c2b9075SWolfgang Grandegger 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
2463c2b9075SWolfgang Grandegger 		date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
2473c2b9075SWolfgang Grandegger 	else
2483c2b9075SWolfgang Grandegger 		date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
2493c2b9075SWolfgang Grandegger 			| bin2bcd((dt->tm_hour + 11) % 12 + 1);
2503c2b9075SWolfgang Grandegger 
2513c2b9075SWolfgang Grandegger 	date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
2523c2b9075SWolfgang Grandegger 	date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
2533c2b9075SWolfgang Grandegger 	date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
2543c2b9075SWolfgang Grandegger 	date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year % 100);
2553c2b9075SWolfgang Grandegger 
2563c2b9075SWolfgang Grandegger 	dev_dbg(dev,
2573c2b9075SWolfgang Grandegger 		"%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
2583c2b9075SWolfgang Grandegger 		__func__,
2593c2b9075SWolfgang Grandegger 		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
2603c2b9075SWolfgang Grandegger 
2613c2b9075SWolfgang Grandegger 	return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
2623c2b9075SWolfgang Grandegger }
2633c2b9075SWolfgang Grandegger 
2643c2b9075SWolfgang Grandegger static int rx8025_init_client(struct i2c_client *client, int *need_reset)
2653c2b9075SWolfgang Grandegger {
2663c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
2673c2b9075SWolfgang Grandegger 	u8 ctrl[2], ctrl2;
2683c2b9075SWolfgang Grandegger 	int need_clear = 0;
2693c2b9075SWolfgang Grandegger 	int err;
2703c2b9075SWolfgang Grandegger 
2713c2b9075SWolfgang Grandegger 	err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
2723c2b9075SWolfgang Grandegger 	if (err)
2733c2b9075SWolfgang Grandegger 		goto out;
2743c2b9075SWolfgang Grandegger 
2753c2b9075SWolfgang Grandegger 	/* Keep test bit zero ! */
2763c2b9075SWolfgang Grandegger 	rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
2773c2b9075SWolfgang Grandegger 
2783c2b9075SWolfgang Grandegger 	if (ctrl[1] & RX8025_BIT_CTRL2_PON) {
2793c2b9075SWolfgang Grandegger 		dev_warn(&client->dev, "power-on reset was detected, "
2803c2b9075SWolfgang Grandegger 			 "you may have to readjust the clock\n");
2813c2b9075SWolfgang Grandegger 		*need_reset = 1;
2823c2b9075SWolfgang Grandegger 	}
2833c2b9075SWolfgang Grandegger 
2843c2b9075SWolfgang Grandegger 	if (ctrl[1] & RX8025_BIT_CTRL2_VDET) {
2853c2b9075SWolfgang Grandegger 		dev_warn(&client->dev, "a power voltage drop was detected, "
2863c2b9075SWolfgang Grandegger 			 "you may have to readjust the clock\n");
2873c2b9075SWolfgang Grandegger 		*need_reset = 1;
2883c2b9075SWolfgang Grandegger 	}
2893c2b9075SWolfgang Grandegger 
2903c2b9075SWolfgang Grandegger 	if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) {
2913c2b9075SWolfgang Grandegger 		dev_warn(&client->dev, "Oscillation stop was detected,"
2923c2b9075SWolfgang Grandegger 			 "you may have to readjust the clock\n");
2933c2b9075SWolfgang Grandegger 		*need_reset = 1;
2943c2b9075SWolfgang Grandegger 	}
2953c2b9075SWolfgang Grandegger 
2963c2b9075SWolfgang Grandegger 	if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
2973c2b9075SWolfgang Grandegger 		dev_warn(&client->dev, "Alarm was detected\n");
2983c2b9075SWolfgang Grandegger 		need_clear = 1;
2993c2b9075SWolfgang Grandegger 	}
3003c2b9075SWolfgang Grandegger 
3013c2b9075SWolfgang Grandegger 	if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
3023c2b9075SWolfgang Grandegger 		need_clear = 1;
3033c2b9075SWolfgang Grandegger 
3043c2b9075SWolfgang Grandegger 	if (*need_reset || need_clear) {
3053c2b9075SWolfgang Grandegger 		ctrl2 = ctrl[0];
3063c2b9075SWolfgang Grandegger 		ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
3073c2b9075SWolfgang Grandegger 			   RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
3083c2b9075SWolfgang Grandegger 			   RX8025_BIT_CTRL2_DAFG);
3093c2b9075SWolfgang Grandegger 		ctrl2 |= RX8025_BIT_CTRL2_XST;
3103c2b9075SWolfgang Grandegger 
3113c2b9075SWolfgang Grandegger 		err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
3123c2b9075SWolfgang Grandegger 	}
3133c2b9075SWolfgang Grandegger out:
3143c2b9075SWolfgang Grandegger 	return err;
3153c2b9075SWolfgang Grandegger }
3163c2b9075SWolfgang Grandegger 
3173c2b9075SWolfgang Grandegger /* Alarm support */
3183c2b9075SWolfgang Grandegger static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
3193c2b9075SWolfgang Grandegger {
3203c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
3213c2b9075SWolfgang Grandegger 	struct i2c_client *client = rx8025->client;
3223c2b9075SWolfgang Grandegger 	u8 ctrl2, ald[2];
3233c2b9075SWolfgang Grandegger 	int err;
3243c2b9075SWolfgang Grandegger 
3253c2b9075SWolfgang Grandegger 	if (client->irq <= 0)
3263c2b9075SWolfgang Grandegger 		return -EINVAL;
3273c2b9075SWolfgang Grandegger 
3283c2b9075SWolfgang Grandegger 	err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
3293c2b9075SWolfgang Grandegger 	if (err)
3303c2b9075SWolfgang Grandegger 		return err;
3313c2b9075SWolfgang Grandegger 
3323c2b9075SWolfgang Grandegger 	err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2);
3333c2b9075SWolfgang Grandegger 	if (err)
3343c2b9075SWolfgang Grandegger 		return err;
3353c2b9075SWolfgang Grandegger 
3363c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
3373c2b9075SWolfgang Grandegger 		__func__, ald[0], ald[1], ctrl2);
3383c2b9075SWolfgang Grandegger 
3393c2b9075SWolfgang Grandegger 	/* Hardware alarms precision is 1 minute! */
3403c2b9075SWolfgang Grandegger 	t->time.tm_sec = 0;
3413c2b9075SWolfgang Grandegger 	t->time.tm_min = bcd2bin(ald[0] & 0x7f);
3423c2b9075SWolfgang Grandegger 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
3433c2b9075SWolfgang Grandegger 		t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
3443c2b9075SWolfgang Grandegger 	else
3453c2b9075SWolfgang Grandegger 		t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
3463c2b9075SWolfgang Grandegger 			+ (ald[1] & 0x20 ? 12 : 0);
3473c2b9075SWolfgang Grandegger 
3483c2b9075SWolfgang Grandegger 	t->time.tm_wday = -1;
3493c2b9075SWolfgang Grandegger 	t->time.tm_mday = -1;
3503c2b9075SWolfgang Grandegger 	t->time.tm_mon = -1;
3513c2b9075SWolfgang Grandegger 	t->time.tm_year = -1;
3523c2b9075SWolfgang Grandegger 
3533c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
3543c2b9075SWolfgang Grandegger 		__func__,
3553c2b9075SWolfgang Grandegger 		t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
3563c2b9075SWolfgang Grandegger 		t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
3573c2b9075SWolfgang Grandegger 	t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
3583c2b9075SWolfgang Grandegger 	t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
3593c2b9075SWolfgang Grandegger 
3603c2b9075SWolfgang Grandegger 	return err;
3613c2b9075SWolfgang Grandegger }
3623c2b9075SWolfgang Grandegger 
3633c2b9075SWolfgang Grandegger static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
3643c2b9075SWolfgang Grandegger {
3653c2b9075SWolfgang Grandegger 	struct i2c_client *client = to_i2c_client(dev);
3663c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
3673c2b9075SWolfgang Grandegger 	u8 ald[2];
3683c2b9075SWolfgang Grandegger 	int err;
3693c2b9075SWolfgang Grandegger 
3703c2b9075SWolfgang Grandegger 	if (client->irq <= 0)
3713c2b9075SWolfgang Grandegger 		return -EINVAL;
3723c2b9075SWolfgang Grandegger 
3733c2b9075SWolfgang Grandegger 	/* Hardware alarm precision is 1 minute! */
3743c2b9075SWolfgang Grandegger 	ald[0] = bin2bcd(t->time.tm_min);
3753c2b9075SWolfgang Grandegger 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
3763c2b9075SWolfgang Grandegger 		ald[1] = bin2bcd(t->time.tm_hour);
3773c2b9075SWolfgang Grandegger 	else
3783c2b9075SWolfgang Grandegger 		ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
3793c2b9075SWolfgang Grandegger 			| bin2bcd((t->time.tm_hour + 11) % 12 + 1);
3803c2b9075SWolfgang Grandegger 
3813c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
3823c2b9075SWolfgang Grandegger 
3833c2b9075SWolfgang Grandegger 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
3843c2b9075SWolfgang Grandegger 		rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
3853c2b9075SWolfgang Grandegger 		err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
3863c2b9075SWolfgang Grandegger 				       rx8025->ctrl1);
3873c2b9075SWolfgang Grandegger 		if (err)
3883c2b9075SWolfgang Grandegger 			return err;
3893c2b9075SWolfgang Grandegger 	}
3903c2b9075SWolfgang Grandegger 	err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
3913c2b9075SWolfgang Grandegger 	if (err)
3923c2b9075SWolfgang Grandegger 		return err;
3933c2b9075SWolfgang Grandegger 
3943c2b9075SWolfgang Grandegger 	if (t->enabled) {
3953c2b9075SWolfgang Grandegger 		rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
3963c2b9075SWolfgang Grandegger 		err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
3973c2b9075SWolfgang Grandegger 				       rx8025->ctrl1);
3983c2b9075SWolfgang Grandegger 		if (err)
3993c2b9075SWolfgang Grandegger 			return err;
4003c2b9075SWolfgang Grandegger 	}
4013c2b9075SWolfgang Grandegger 
4023c2b9075SWolfgang Grandegger 	return 0;
4033c2b9075SWolfgang Grandegger }
4043c2b9075SWolfgang Grandegger 
4053c2b9075SWolfgang Grandegger static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
4063c2b9075SWolfgang Grandegger {
4073c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
4083c2b9075SWolfgang Grandegger 	u8 ctrl1;
4093c2b9075SWolfgang Grandegger 	int err;
4103c2b9075SWolfgang Grandegger 
4113c2b9075SWolfgang Grandegger 	ctrl1 = rx8025->ctrl1;
4123c2b9075SWolfgang Grandegger 	if (enabled)
4133c2b9075SWolfgang Grandegger 		ctrl1 |= RX8025_BIT_CTRL1_DALE;
4143c2b9075SWolfgang Grandegger 	else
4153c2b9075SWolfgang Grandegger 		ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
4163c2b9075SWolfgang Grandegger 
4173c2b9075SWolfgang Grandegger 	if (ctrl1 != rx8025->ctrl1) {
4183c2b9075SWolfgang Grandegger 		rx8025->ctrl1 = ctrl1;
4193c2b9075SWolfgang Grandegger 		err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
4203c2b9075SWolfgang Grandegger 				       rx8025->ctrl1);
4213c2b9075SWolfgang Grandegger 		if (err)
4223c2b9075SWolfgang Grandegger 			return err;
4233c2b9075SWolfgang Grandegger 	}
4243c2b9075SWolfgang Grandegger 	return 0;
4253c2b9075SWolfgang Grandegger }
4263c2b9075SWolfgang Grandegger 
4273c2b9075SWolfgang Grandegger static struct rtc_class_ops rx8025_rtc_ops = {
4283c2b9075SWolfgang Grandegger 	.read_time = rx8025_get_time,
4293c2b9075SWolfgang Grandegger 	.set_time = rx8025_set_time,
4303c2b9075SWolfgang Grandegger 	.read_alarm = rx8025_read_alarm,
4313c2b9075SWolfgang Grandegger 	.set_alarm = rx8025_set_alarm,
4323c2b9075SWolfgang Grandegger 	.alarm_irq_enable = rx8025_alarm_irq_enable,
4333c2b9075SWolfgang Grandegger };
4343c2b9075SWolfgang Grandegger 
4353c2b9075SWolfgang Grandegger /*
4363c2b9075SWolfgang Grandegger  * Clock precision adjustment support
4373c2b9075SWolfgang Grandegger  *
4383c2b9075SWolfgang Grandegger  * According to the RX8025 SA/NB application manual the frequency and
439b770ffd4SThomas Weber  * temperature characteristics can be approximated using the following
4403c2b9075SWolfgang Grandegger  * equation:
4413c2b9075SWolfgang Grandegger  *
4423c2b9075SWolfgang Grandegger  *   df = a * (ut - t)**2
4433c2b9075SWolfgang Grandegger  *
4443c2b9075SWolfgang Grandegger  *   df: Frequency deviation in any temperature
4453c2b9075SWolfgang Grandegger  *   a : Coefficient = (-35 +-5) * 10**-9
4463c2b9075SWolfgang Grandegger  *   ut: Ultimate temperature in degree = +25 +-5 degree
4473c2b9075SWolfgang Grandegger  *   t : Any temperature in degree
4483c2b9075SWolfgang Grandegger  *
4493c2b9075SWolfgang Grandegger  * Note that the clock adjustment in ppb must be entered (which is
4503c2b9075SWolfgang Grandegger  * the negative value of the deviation).
4513c2b9075SWolfgang Grandegger  */
4523c2b9075SWolfgang Grandegger static int rx8025_get_clock_adjust(struct device *dev, int *adj)
4533c2b9075SWolfgang Grandegger {
4543c2b9075SWolfgang Grandegger 	struct i2c_client *client = to_i2c_client(dev);
4553c2b9075SWolfgang Grandegger 	u8 digoff;
4563c2b9075SWolfgang Grandegger 	int err;
4573c2b9075SWolfgang Grandegger 
4583c2b9075SWolfgang Grandegger 	err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff);
4593c2b9075SWolfgang Grandegger 	if (err)
4603c2b9075SWolfgang Grandegger 		return err;
4613c2b9075SWolfgang Grandegger 
4623c2b9075SWolfgang Grandegger 	*adj = digoff >= 64 ? digoff - 128 : digoff;
4633c2b9075SWolfgang Grandegger 	if (*adj > 0)
4643c2b9075SWolfgang Grandegger 		(*adj)--;
4653c2b9075SWolfgang Grandegger 	*adj *= -RX8025_ADJ_RESOLUTION;
4663c2b9075SWolfgang Grandegger 
4673c2b9075SWolfgang Grandegger 	return 0;
4683c2b9075SWolfgang Grandegger }
4693c2b9075SWolfgang Grandegger 
4703c2b9075SWolfgang Grandegger static int rx8025_set_clock_adjust(struct device *dev, int adj)
4713c2b9075SWolfgang Grandegger {
4723c2b9075SWolfgang Grandegger 	struct i2c_client *client = to_i2c_client(dev);
4733c2b9075SWolfgang Grandegger 	u8 digoff;
4743c2b9075SWolfgang Grandegger 	int err;
4753c2b9075SWolfgang Grandegger 
4763c2b9075SWolfgang Grandegger 	adj /= -RX8025_ADJ_RESOLUTION;
4773c2b9075SWolfgang Grandegger 	if (adj > RX8025_ADJ_DATA_MAX)
4783c2b9075SWolfgang Grandegger 		adj = RX8025_ADJ_DATA_MAX;
4793c2b9075SWolfgang Grandegger 	else if (adj < RX8025_ADJ_DATA_MIN)
4803c2b9075SWolfgang Grandegger 		adj = RX8025_ADJ_DATA_MIN;
4813c2b9075SWolfgang Grandegger 	else if (adj > 0)
4823c2b9075SWolfgang Grandegger 		adj++;
4833c2b9075SWolfgang Grandegger 	else if (adj < 0)
4843c2b9075SWolfgang Grandegger 		adj += 128;
4853c2b9075SWolfgang Grandegger 	digoff = adj;
4863c2b9075SWolfgang Grandegger 
4873c2b9075SWolfgang Grandegger 	err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
4883c2b9075SWolfgang Grandegger 	if (err)
4893c2b9075SWolfgang Grandegger 		return err;
4903c2b9075SWolfgang Grandegger 
4913c2b9075SWolfgang Grandegger 	dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
4923c2b9075SWolfgang Grandegger 
4933c2b9075SWolfgang Grandegger 	return 0;
4943c2b9075SWolfgang Grandegger }
4953c2b9075SWolfgang Grandegger 
4963c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
4973c2b9075SWolfgang Grandegger 					      struct device_attribute *attr,
4983c2b9075SWolfgang Grandegger 					      char *buf)
4993c2b9075SWolfgang Grandegger {
5003c2b9075SWolfgang Grandegger 	int err, adj;
5013c2b9075SWolfgang Grandegger 
5023c2b9075SWolfgang Grandegger 	err = rx8025_get_clock_adjust(dev, &adj);
5033c2b9075SWolfgang Grandegger 	if (err)
5043c2b9075SWolfgang Grandegger 		return err;
5053c2b9075SWolfgang Grandegger 
5063c2b9075SWolfgang Grandegger 	return sprintf(buf, "%d\n", adj);
5073c2b9075SWolfgang Grandegger }
5083c2b9075SWolfgang Grandegger 
5093c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
5103c2b9075SWolfgang Grandegger 					       struct device_attribute *attr,
5113c2b9075SWolfgang Grandegger 					       const char *buf, size_t count)
5123c2b9075SWolfgang Grandegger {
5133c2b9075SWolfgang Grandegger 	int adj, err;
5143c2b9075SWolfgang Grandegger 
5153c2b9075SWolfgang Grandegger 	if (sscanf(buf, "%i", &adj) != 1)
5163c2b9075SWolfgang Grandegger 		return -EINVAL;
5173c2b9075SWolfgang Grandegger 
5183c2b9075SWolfgang Grandegger 	err = rx8025_set_clock_adjust(dev, adj);
5193c2b9075SWolfgang Grandegger 
5203c2b9075SWolfgang Grandegger 	return err ? err : count;
5213c2b9075SWolfgang Grandegger }
5223c2b9075SWolfgang Grandegger 
5233c2b9075SWolfgang Grandegger static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
5243c2b9075SWolfgang Grandegger 		   rx8025_sysfs_show_clock_adjust,
5253c2b9075SWolfgang Grandegger 		   rx8025_sysfs_store_clock_adjust);
5263c2b9075SWolfgang Grandegger 
5273c2b9075SWolfgang Grandegger static int rx8025_sysfs_register(struct device *dev)
5283c2b9075SWolfgang Grandegger {
5293c2b9075SWolfgang Grandegger 	return device_create_file(dev, &dev_attr_clock_adjust_ppb);
5303c2b9075SWolfgang Grandegger }
5313c2b9075SWolfgang Grandegger 
5323c2b9075SWolfgang Grandegger static void rx8025_sysfs_unregister(struct device *dev)
5333c2b9075SWolfgang Grandegger {
5343c2b9075SWolfgang Grandegger 	device_remove_file(dev, &dev_attr_clock_adjust_ppb);
5353c2b9075SWolfgang Grandegger }
5363c2b9075SWolfgang Grandegger 
5373c2b9075SWolfgang Grandegger static int __devinit rx8025_probe(struct i2c_client *client,
5383c2b9075SWolfgang Grandegger 				  const struct i2c_device_id *id)
5393c2b9075SWolfgang Grandegger {
5403c2b9075SWolfgang Grandegger 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5413c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025;
5423c2b9075SWolfgang Grandegger 	int err, need_reset = 0;
5433c2b9075SWolfgang Grandegger 
5443c2b9075SWolfgang Grandegger 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
5453c2b9075SWolfgang Grandegger 				     | I2C_FUNC_SMBUS_I2C_BLOCK)) {
5463c2b9075SWolfgang Grandegger 		dev_err(&adapter->dev,
5473c2b9075SWolfgang Grandegger 			"doesn't support required functionality\n");
5483c2b9075SWolfgang Grandegger 		err = -EIO;
5493c2b9075SWolfgang Grandegger 		goto errout;
5503c2b9075SWolfgang Grandegger 	}
5513c2b9075SWolfgang Grandegger 
5523c2b9075SWolfgang Grandegger 	rx8025 = kzalloc(sizeof(*rx8025), GFP_KERNEL);
5533c2b9075SWolfgang Grandegger 	if (!rx8025) {
5543c2b9075SWolfgang Grandegger 		dev_err(&adapter->dev, "failed to alloc memory\n");
5553c2b9075SWolfgang Grandegger 		err = -ENOMEM;
5563c2b9075SWolfgang Grandegger 		goto errout;
5573c2b9075SWolfgang Grandegger 	}
5583c2b9075SWolfgang Grandegger 
5593c2b9075SWolfgang Grandegger 	rx8025->client = client;
5603c2b9075SWolfgang Grandegger 	i2c_set_clientdata(client, rx8025);
5613c2b9075SWolfgang Grandegger 	INIT_WORK(&rx8025->work, rx8025_work);
5623c2b9075SWolfgang Grandegger 
5633c2b9075SWolfgang Grandegger 	err = rx8025_init_client(client, &need_reset);
5643c2b9075SWolfgang Grandegger 	if (err)
5653c2b9075SWolfgang Grandegger 		goto errout_free;
5663c2b9075SWolfgang Grandegger 
5673c2b9075SWolfgang Grandegger 	if (need_reset) {
5683c2b9075SWolfgang Grandegger 		struct rtc_time tm;
5693c2b9075SWolfgang Grandegger 		dev_info(&client->dev,
5703c2b9075SWolfgang Grandegger 			 "bad conditions detected, resetting date\n");
5713c2b9075SWolfgang Grandegger 		rtc_time_to_tm(0, &tm);	/* 1970/1/1 */
5723c2b9075SWolfgang Grandegger 		rx8025_set_time(&client->dev, &tm);
5733c2b9075SWolfgang Grandegger 	}
5743c2b9075SWolfgang Grandegger 
5753c2b9075SWolfgang Grandegger 	rx8025->rtc = rtc_device_register(client->name, &client->dev,
5763c2b9075SWolfgang Grandegger 					  &rx8025_rtc_ops, THIS_MODULE);
5773c2b9075SWolfgang Grandegger 	if (IS_ERR(rx8025->rtc)) {
5783c2b9075SWolfgang Grandegger 		err = PTR_ERR(rx8025->rtc);
5793c2b9075SWolfgang Grandegger 		dev_err(&client->dev, "unable to register the class device\n");
5803c2b9075SWolfgang Grandegger 		goto errout_free;
5813c2b9075SWolfgang Grandegger 	}
5823c2b9075SWolfgang Grandegger 
5833c2b9075SWolfgang Grandegger 	if (client->irq > 0) {
5843c2b9075SWolfgang Grandegger 		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
5853c2b9075SWolfgang Grandegger 		err = request_irq(client->irq, rx8025_irq,
5863c2b9075SWolfgang Grandegger 				  0, "rx8025", client);
5873c2b9075SWolfgang Grandegger 		if (err) {
5883c2b9075SWolfgang Grandegger 			dev_err(&client->dev, "unable to request IRQ\n");
5893c2b9075SWolfgang Grandegger 			goto errout_reg;
5903c2b9075SWolfgang Grandegger 		}
5913c2b9075SWolfgang Grandegger 	}
5923c2b9075SWolfgang Grandegger 
5933c2b9075SWolfgang Grandegger 	rx8025->rtc->irq_freq = 1;
5943c2b9075SWolfgang Grandegger 	rx8025->rtc->max_user_freq = 1;
5953c2b9075SWolfgang Grandegger 
5963c2b9075SWolfgang Grandegger 	err = rx8025_sysfs_register(&client->dev);
5973c2b9075SWolfgang Grandegger 	if (err)
5983c2b9075SWolfgang Grandegger 		goto errout_irq;
5993c2b9075SWolfgang Grandegger 
6003c2b9075SWolfgang Grandegger 	return 0;
6013c2b9075SWolfgang Grandegger 
6023c2b9075SWolfgang Grandegger errout_irq:
6033c2b9075SWolfgang Grandegger 	if (client->irq > 0)
6043c2b9075SWolfgang Grandegger 		free_irq(client->irq, client);
6053c2b9075SWolfgang Grandegger 
6063c2b9075SWolfgang Grandegger errout_reg:
6073c2b9075SWolfgang Grandegger 	rtc_device_unregister(rx8025->rtc);
6083c2b9075SWolfgang Grandegger 
6093c2b9075SWolfgang Grandegger errout_free:
6103c2b9075SWolfgang Grandegger 	kfree(rx8025);
6113c2b9075SWolfgang Grandegger 
6123c2b9075SWolfgang Grandegger errout:
6133c2b9075SWolfgang Grandegger 	dev_err(&adapter->dev, "probing for rx8025 failed\n");
6143c2b9075SWolfgang Grandegger 	return err;
6153c2b9075SWolfgang Grandegger }
6163c2b9075SWolfgang Grandegger 
6173c2b9075SWolfgang Grandegger static int __devexit rx8025_remove(struct i2c_client *client)
6183c2b9075SWolfgang Grandegger {
6193c2b9075SWolfgang Grandegger 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
6203c2b9075SWolfgang Grandegger 	struct mutex *lock = &rx8025->rtc->ops_lock;
6213c2b9075SWolfgang Grandegger 
6223c2b9075SWolfgang Grandegger 	if (client->irq > 0) {
6233c2b9075SWolfgang Grandegger 		mutex_lock(lock);
6243c2b9075SWolfgang Grandegger 		rx8025->exiting = 1;
6253c2b9075SWolfgang Grandegger 		mutex_unlock(lock);
6263c2b9075SWolfgang Grandegger 
6273c2b9075SWolfgang Grandegger 		free_irq(client->irq, client);
6289db8995bSTejun Heo 		cancel_work_sync(&rx8025->work);
6293c2b9075SWolfgang Grandegger 	}
6303c2b9075SWolfgang Grandegger 
6313c2b9075SWolfgang Grandegger 	rx8025_sysfs_unregister(&client->dev);
6323c2b9075SWolfgang Grandegger 	rtc_device_unregister(rx8025->rtc);
6333c2b9075SWolfgang Grandegger 	kfree(rx8025);
6343c2b9075SWolfgang Grandegger 	return 0;
6353c2b9075SWolfgang Grandegger }
6363c2b9075SWolfgang Grandegger 
6373c2b9075SWolfgang Grandegger static struct i2c_driver rx8025_driver = {
6383c2b9075SWolfgang Grandegger 	.driver = {
6393c2b9075SWolfgang Grandegger 		.name = "rtc-rx8025",
6403c2b9075SWolfgang Grandegger 		.owner = THIS_MODULE,
6413c2b9075SWolfgang Grandegger 	},
6423c2b9075SWolfgang Grandegger 	.probe		= rx8025_probe,
6433c2b9075SWolfgang Grandegger 	.remove		= __devexit_p(rx8025_remove),
6443c2b9075SWolfgang Grandegger 	.id_table	= rx8025_id,
6453c2b9075SWolfgang Grandegger };
6463c2b9075SWolfgang Grandegger 
647*0abc9201SAxel Lin module_i2c_driver(rx8025_driver);
6483c2b9075SWolfgang Grandegger 
6493c2b9075SWolfgang Grandegger MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
6503c2b9075SWolfgang Grandegger MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
6513c2b9075SWolfgang Grandegger MODULE_LICENSE("GPL");
652