rtc-rx8581.c 6.92 KB
Newer Older
1 2 3
/*
 * An I2C driver for the Epson RX8581 RTC
 *
4 5
 * Author: Martyn Welch <martyn.welch@ge.com>
 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
 * Copyright 2005-06 Tower Technologies
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/bcd.h>
18
#include <linux/regmap.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/rtc.h>
#include <linux/log2.h>

#define RX8581_REG_SC		0x00 /* Second in BCD */
#define RX8581_REG_MN		0x01 /* Minute in BCD */
#define RX8581_REG_HR		0x02 /* Hour in BCD */
#define RX8581_REG_DW		0x03 /* Day of Week */
#define RX8581_REG_DM		0x04 /* Day of Month in BCD */
#define RX8581_REG_MO		0x05 /* Month in BCD */
#define RX8581_REG_YR		0x06 /* Year in BCD */
#define RX8581_REG_RAM		0x07 /* RAM */
#define RX8581_REG_AMN		0x08 /* Alarm Min in BCD*/
#define RX8581_REG_AHR		0x09 /* Alarm Hour in BCD */
#define RX8581_REG_ADM		0x0A
#define RX8581_REG_ADW		0x0A
#define RX8581_REG_TMR0		0x0B
#define RX8581_REG_TMR1		0x0C
#define RX8581_REG_EXT		0x0D /* Extension Register */
#define RX8581_REG_FLAG		0x0E /* Flag Register */
#define RX8581_REG_CTRL		0x0F /* Control Register */


/* Flag Register bit definitions */
#define RX8581_FLAG_UF		0x20 /* Update */
#define RX8581_FLAG_TF		0x10 /* Timer */
#define RX8581_FLAG_AF		0x08 /* Alarm */
#define RX8581_FLAG_VLF		0x02 /* Voltage Low */

/* Control Register bit definitions */
#define RX8581_CTRL_UIE		0x20 /* Update Interrupt Enable */
#define RX8581_CTRL_TIE		0x10 /* Timer Interrupt Enable */
#define RX8581_CTRL_AIE		0x08 /* Alarm Interrupt Enable */
#define RX8581_CTRL_STOP	0x02 /* STOP bit */
#define RX8581_CTRL_RESET	0x01 /* RESET bit */

54
struct rx8581 {
55
	struct regmap		*regmap;
56 57 58
	struct rtc_device	*rtc;
};

59 60 61 62
/*
 * In the routines that deal directly with the rx8581 hardware, we use
 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
 */
63
static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
64
{
65
	struct i2c_client *client = to_i2c_client(dev);
66
	unsigned char date[7];
67 68
	unsigned int data;
	int err;
69
	struct rx8581 *rx8581 = i2c_get_clientdata(client);
70 71 72 73 74 75

	/* First we ensure that the "update flag" is not set, we read the
	 * time and date then re-read the "update flag". If the update flag
	 * has been set, we know that the time has changed during the read so
	 * we repeat the whole process again.
	 */
76 77 78
	err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
	if (err < 0)
		return err;
79

80 81 82 83 84 85
	if (data & RX8581_FLAG_VLF) {
		dev_warn(dev,
			 "low voltage detected, date/time is not reliable.\n");
		return -EINVAL;
	}

86 87 88
	do {
		/* If update flag set, clear it */
		if (data & RX8581_FLAG_UF) {
89 90 91 92
			err = regmap_write(rx8581->regmap, RX8581_REG_FLAG,
					  data & ~RX8581_FLAG_UF);
			if (err < 0)
				return err;
93 94 95
		}

		/* Now read time and date */
96 97 98 99
		err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
				       sizeof(date));
		if (err < 0)
			return err;
100 101

		/* Check flag register */
102 103 104
		err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
		if (err < 0)
			return err;
105 106
	} while (data & RX8581_FLAG_UF);

107
	dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
108 109 110 111 112 113 114 115 116 117
		"wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
		__func__,
		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);

	tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
	tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
	tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
	tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
	tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
	tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
118
	tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
119

120
	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
121 122 123 124 125
		"mday=%d, mon=%d, year=%d, wday=%d\n",
		__func__,
		tm->tm_sec, tm->tm_min, tm->tm_hour,
		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);

126
	return 0;
127 128
}

129
static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
130
{
131
	struct i2c_client *client = to_i2c_client(dev);
132
	int err;
133
	unsigned char buf[7];
134
	struct rx8581 *rx8581 = i2c_get_clientdata(client);
135

136
	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		"mday=%d, mon=%d, year=%d, wday=%d\n",
		__func__,
		tm->tm_sec, tm->tm_min, tm->tm_hour,
		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);

	/* hours, minutes and seconds */
	buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
	buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
	buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);

	buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);

	/* month, 1 - 12 */
	buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);

	/* year and century */
153
	buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
154 155 156
	buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);

	/* Stop the clock */
157 158 159 160
	err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
				 RX8581_CTRL_STOP, RX8581_CTRL_STOP);
	if (err < 0)
		return err;
161 162

	/* write register's data */
163 164 165 166
	err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
				buf, sizeof(buf));
	if (err < 0)
		return err;
167

168
	/* get VLF and clear it */
169 170 171 172
	err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
				 RX8581_FLAG_VLF, 0);
	if (err < 0)
		return err;
173

174
	/* Restart the clock */
175 176
	return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
				 RX8581_CTRL_STOP, 0);
177 178 179 180 181 182 183
}

static const struct rtc_class_ops rx8581_rtc_ops = {
	.read_time	= rx8581_rtc_read_time,
	.set_time	= rx8581_rtc_set_time,
};

184 185
static int rx8581_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
186
{
187
	struct rx8581	  *rx8581;
188 189 190 191 192
	static const struct regmap_config config = {
		.reg_bits = 8,
		.val_bits = 8,
		.max_register = 0xf,
	};
193 194 195

	dev_dbg(&client->dev, "%s\n", __func__);

196 197 198
	rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
	if (!rx8581)
		return -ENOMEM;
199

200
	i2c_set_clientdata(client, rx8581);
201 202 203 204

	rx8581->regmap = devm_regmap_init_i2c(client, &config);
	if (IS_ERR(rx8581->regmap))
		return PTR_ERR(rx8581->regmap);
205

206 207
	rx8581->rtc = devm_rtc_allocate_device(&client->dev);
	if (IS_ERR(rx8581->rtc))
208
		return PTR_ERR(rx8581->rtc);
209

210
	rx8581->rtc->ops = &rx8581_rtc_ops;
211 212
	rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
	rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
213 214
	rx8581->rtc->start_secs = 0;
	rx8581->rtc->set_start_time = true;
215 216

	return rtc_register_device(rx8581->rtc);
217 218 219 220 221 222 223 224
}

static const struct i2c_device_id rx8581_id[] = {
	{ "rx8581", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, rx8581_id);

225 226 227 228 229 230
static const struct of_device_id rx8581_of_match[] = {
	{ .compatible = "epson,rx8581" },
	{ }
};
MODULE_DEVICE_TABLE(of, rx8581_of_match);

231 232 233
static struct i2c_driver rx8581_driver = {
	.driver		= {
		.name	= "rtc-rx8581",
234
		.of_match_table = of_match_ptr(rx8581_of_match),
235 236 237 238 239
	},
	.probe		= rx8581_probe,
	.id_table	= rx8581_id,
};

240
module_i2c_driver(rx8581_driver);
241

242
MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
243 244
MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
MODULE_LICENSE("GPL");