rtc-ds1216.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Dallas DS1216 RTC driver
 *
 * Copyright (c) 2007 Thomas Bogendoerfer
 *
 */

#include <linux/module.h>
#include <linux/rtc.h>
#include <linux/platform_device.h>
#include <linux/bcd.h>

A
Alessandro Zummo 已提交
13
#define DRV_VERSION "0.2"
14 15 16 17 18 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

struct ds1216_regs {
	u8 tsec;
	u8 sec;
	u8 min;
	u8 hour;
	u8 wday;
	u8 mday;
	u8 month;
	u8 year;
};

#define DS1216_HOUR_1224	(1 << 7)
#define DS1216_HOUR_AMPM	(1 << 5)

struct ds1216_priv {
	struct rtc_device *rtc;
	void __iomem *ioaddr;
	size_t size;
	unsigned long baseaddr;
};

static const u8 magic[] = {
	0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
};

/*
 * Read the 64 bit we'd like to have - It a series
 * of 64 bits showing up in the LSB of the base register.
 *
 */
static void ds1216_read(u8 __iomem *ioaddr, u8 *buf)
{
	unsigned char c;
	int i, j;

	for (i = 0; i < 8; i++) {
		c = 0;
		for (j = 0; j < 8; j++)
			c |= (readb(ioaddr) & 0x1) << j;
		buf[i] = c;
	}
}

static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf)
{
	unsigned char c;
	int i, j;

	for (i = 0; i < 8; i++) {
		c = buf[i];
		for (j = 0; j < 8; j++) {
			writeb(c, ioaddr);
			c = c >> 1;
		}
	}
}

static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr)
{
	/* Reset magic pointer */
	readb(ioaddr);
	/* Write 64 bit magic to DS1216 */
	ds1216_write(ioaddr, magic);
}

static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ds1216_priv *priv = platform_get_drvdata(pdev);
	struct ds1216_regs regs;

	ds1216_switch_ds_to_clock(priv->ioaddr);
	ds1216_read(priv->ioaddr, (u8 *)&regs);

A
Adrian Bunk 已提交
89 90
	tm->tm_sec = bcd2bin(regs.sec);
	tm->tm_min = bcd2bin(regs.min);
91 92
	if (regs.hour & DS1216_HOUR_1224) {
		/* AM/PM mode */
A
Adrian Bunk 已提交
93
		tm->tm_hour = bcd2bin(regs.hour & 0x1f);
94 95 96
		if (regs.hour & DS1216_HOUR_AMPM)
			tm->tm_hour += 12;
	} else
A
Adrian Bunk 已提交
97
		tm->tm_hour = bcd2bin(regs.hour & 0x3f);
98
	tm->tm_wday = (regs.wday & 7) - 1;
A
Adrian Bunk 已提交
99 100 101
	tm->tm_mday = bcd2bin(regs.mday & 0x3f);
	tm->tm_mon = bcd2bin(regs.month & 0x1f);
	tm->tm_year = bcd2bin(regs.year);
102 103
	if (tm->tm_year < 70)
		tm->tm_year += 100;
A
Alessandro Zummo 已提交
104 105

	return rtc_valid_tm(tm);
106 107 108 109 110 111 112 113 114 115 116 117
}

static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ds1216_priv *priv = platform_get_drvdata(pdev);
	struct ds1216_regs regs;

	ds1216_switch_ds_to_clock(priv->ioaddr);
	ds1216_read(priv->ioaddr, (u8 *)&regs);

	regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
A
Adrian Bunk 已提交
118 119
	regs.sec = bin2bcd(tm->tm_sec);
	regs.min = bin2bcd(tm->tm_min);
120 121 122 123 124
	regs.hour &= DS1216_HOUR_1224;
	if (regs.hour && tm->tm_hour > 12) {
		regs.hour |= DS1216_HOUR_AMPM;
		tm->tm_hour -= 12;
	}
A
Adrian Bunk 已提交
125
	regs.hour |= bin2bcd(tm->tm_hour);
126 127
	regs.wday &= ~7;
	regs.wday |= tm->tm_wday;
A
Adrian Bunk 已提交
128 129 130
	regs.mday = bin2bcd(tm->tm_mday);
	regs.month = bin2bcd(tm->tm_mon);
	regs.year = bin2bcd(tm->tm_year % 100);
131 132 133 134 135 136 137 138 139 140 141

	ds1216_switch_ds_to_clock(priv->ioaddr);
	ds1216_write(priv->ioaddr, (u8 *)&regs);
	return 0;
}

static const struct rtc_class_ops ds1216_rtc_ops = {
	.read_time	= ds1216_rtc_read_time,
	.set_time	= ds1216_rtc_set_time,
};

A
Alessandro Zummo 已提交
142
static int __init ds1216_rtc_probe(struct platform_device *pdev)
143 144 145 146 147 148 149 150 151 152 153 154
{
	struct resource *res;
	struct ds1216_priv *priv;
	int ret = 0;
	u8 dummy[8];

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;
	priv = kzalloc(sizeof *priv, GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
A
Alessandro Zummo 已提交
155 156 157 158

	platform_set_drvdata(pdev, priv);

	priv->size = resource_size(res);
159 160 161 162 163 164 165 166 167 168
	if (!request_mem_region(res->start, priv->size, pdev->name)) {
		ret = -EBUSY;
		goto out;
	}
	priv->baseaddr = res->start;
	priv->ioaddr = ioremap(priv->baseaddr, priv->size);
	if (!priv->ioaddr) {
		ret = -ENOMEM;
		goto out;
	}
A
Alessandro Zummo 已提交
169
	priv->rtc = rtc_device_register("ds1216", &pdev->dev,
170
				  &ds1216_rtc_ops, THIS_MODULE);
A
Alessandro Zummo 已提交
171 172
	if (IS_ERR(priv->rtc)) {
		ret = PTR_ERR(priv->rtc);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		goto out;
	}

	/* dummy read to get clock into a known state */
	ds1216_read(priv->ioaddr, dummy);
	return 0;

out:
	if (priv->ioaddr)
		iounmap(priv->ioaddr);
	if (priv->baseaddr)
		release_mem_region(priv->baseaddr, priv->size);
	kfree(priv);
	return ret;
}

A
Alessandro Zummo 已提交
189
static int __exit ds1216_rtc_remove(struct platform_device *pdev)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
	struct ds1216_priv *priv = platform_get_drvdata(pdev);

	rtc_device_unregister(priv->rtc);
	iounmap(priv->ioaddr);
	release_mem_region(priv->baseaddr, priv->size);
	kfree(priv);
	return 0;
}

static struct platform_driver ds1216_rtc_platform_driver = {
	.driver		= {
		.name	= "rtc-ds1216",
		.owner	= THIS_MODULE,
	},
A
Alessandro Zummo 已提交
205
	.remove		= __exit_p(ds1216_rtc_remove),
206 207 208 209
};

static int __init ds1216_rtc_init(void)
{
A
Alessandro Zummo 已提交
210
	return platform_driver_probe(&ds1216_rtc_platform_driver, ds1216_rtc_probe);
211 212 213 214 215 216 217 218 219 220 221
}

static void __exit ds1216_rtc_exit(void)
{
	platform_driver_unregister(&ds1216_rtc_platform_driver);
}

MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
MODULE_DESCRIPTION("DS1216 RTC driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);
222
MODULE_ALIAS("platform:rtc-ds1216");
223 224 225

module_init(ds1216_rtc_init);
module_exit(ds1216_rtc_exit);