rtc-m48t86.c 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * ST M48T86 / Dallas DS12887 RTC driver
 * Copyright (c) 2006 Tower Technologies
 *
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 *
 * 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.
 *
 * This drivers only supports the clock running in BCD and 24H mode.
 * If it will be ever adapted to binary and 12H mode, care must be taken
 * to not introduce bugs.
 */

#include <linux/module.h>
#include <linux/rtc.h>
#include <linux/platform_device.h>
19
#include <linux/platform_data/rtc-m48t86.h>
20
#include <linux/bcd.h>
21
#include <linux/io.h>
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#define M48T86_SEC		0x00
#define M48T86_SECALRM		0x01
#define M48T86_MIN		0x02
#define M48T86_MINALRM		0x03
#define M48T86_HOUR		0x04
#define M48T86_HOURALRM		0x05
#define M48T86_DOW		0x06 /* 1 = sunday */
#define M48T86_DOM		0x07
#define M48T86_MONTH		0x08 /* 1 - 12 */
#define M48T86_YEAR		0x09 /* 0 - 99 */
#define M48T86_A		0x0a
#define M48T86_B		0x0b
#define M48T86_B_SET		BIT(7)
#define M48T86_B_DM		BIT(2)
#define M48T86_B_H24		BIT(1)
#define M48T86_C		0x0c
#define M48T86_D		0x0d
#define M48T86_D_VRT		BIT(7)
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
struct m48t86_rtc_info {
	void __iomem *index_reg;
	void __iomem *data_reg;
	struct rtc_device *rtc;
	struct m48t86_ops *ops;
};

static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
{
	struct m48t86_rtc_info *info = dev_get_drvdata(dev);
	unsigned char value;

	if (info->ops) {
		value = info->ops->readbyte(addr);
	} else {
		writeb(addr, info->index_reg);
		value = readb(info->data_reg);
	}
	return value;
}

static void m48t86_writeb(struct device *dev,
			  unsigned char value, unsigned long addr)
{
	struct m48t86_rtc_info *info = dev_get_drvdata(dev);

	if (info->ops) {
		info->ops->writebyte(value, addr);
	} else {
		writeb(addr, info->index_reg);
		writeb(value, info->data_reg);
	}
}

76 77 78 79
static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	unsigned char reg;

80
	reg = m48t86_readb(dev, M48T86_B);
81

82
	if (reg & M48T86_B_DM) {
83
		/* data (binary) mode */
84 85 86 87
		tm->tm_sec	= m48t86_readb(dev, M48T86_SEC);
		tm->tm_min	= m48t86_readb(dev, M48T86_MIN);
		tm->tm_hour	= m48t86_readb(dev, M48T86_HOUR) & 0x3f;
		tm->tm_mday	= m48t86_readb(dev, M48T86_DOM);
88
		/* tm_mon is 0-11 */
89 90 91
		tm->tm_mon	= m48t86_readb(dev, M48T86_MONTH) - 1;
		tm->tm_year	= m48t86_readb(dev, M48T86_YEAR) + 100;
		tm->tm_wday	= m48t86_readb(dev, M48T86_DOW);
92 93
	} else {
		/* bcd mode */
94 95 96 97 98
		tm->tm_sec	= bcd2bin(m48t86_readb(dev, M48T86_SEC));
		tm->tm_min	= bcd2bin(m48t86_readb(dev, M48T86_MIN));
		tm->tm_hour	= bcd2bin(m48t86_readb(dev, M48T86_HOUR) &
					  0x3f);
		tm->tm_mday	= bcd2bin(m48t86_readb(dev, M48T86_DOM));
99
		/* tm_mon is 0-11 */
100 101 102
		tm->tm_mon	= bcd2bin(m48t86_readb(dev, M48T86_MONTH)) - 1;
		tm->tm_year	= bcd2bin(m48t86_readb(dev, M48T86_YEAR)) + 100;
		tm->tm_wday	= bcd2bin(m48t86_readb(dev, M48T86_DOW));
103 104 105
	}

	/* correct the hour if the clock is in 12h mode */
106
	if (!(reg & M48T86_B_H24))
107
		if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
108 109
			tm->tm_hour += 12;

110
	return rtc_valid_tm(tm);
111 112 113 114 115 116
}

static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	unsigned char reg;

117
	reg = m48t86_readb(dev, M48T86_B);
118 119

	/* update flag and 24h mode */
120
	reg |= M48T86_B_SET | M48T86_B_H24;
121
	m48t86_writeb(dev, reg, M48T86_B);
122

123
	if (reg & M48T86_B_DM) {
124
		/* data (binary) mode */
125 126 127 128 129 130 131
		m48t86_writeb(dev, tm->tm_sec, M48T86_SEC);
		m48t86_writeb(dev, tm->tm_min, M48T86_MIN);
		m48t86_writeb(dev, tm->tm_hour, M48T86_HOUR);
		m48t86_writeb(dev, tm->tm_mday, M48T86_DOM);
		m48t86_writeb(dev, tm->tm_mon + 1, M48T86_MONTH);
		m48t86_writeb(dev, tm->tm_year % 100, M48T86_YEAR);
		m48t86_writeb(dev, tm->tm_wday, M48T86_DOW);
132 133
	} else {
		/* bcd mode */
134 135 136 137 138 139 140
		m48t86_writeb(dev, bin2bcd(tm->tm_sec), M48T86_SEC);
		m48t86_writeb(dev, bin2bcd(tm->tm_min), M48T86_MIN);
		m48t86_writeb(dev, bin2bcd(tm->tm_hour), M48T86_HOUR);
		m48t86_writeb(dev, bin2bcd(tm->tm_mday), M48T86_DOM);
		m48t86_writeb(dev, bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
		m48t86_writeb(dev, bin2bcd(tm->tm_year % 100), M48T86_YEAR);
		m48t86_writeb(dev, bin2bcd(tm->tm_wday), M48T86_DOW);
141 142 143
	}

	/* update ended */
144
	reg &= ~M48T86_B_SET;
145
	m48t86_writeb(dev, reg, M48T86_B);
146 147 148 149 150 151 152 153

	return 0;
}

static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
{
	unsigned char reg;

154
	reg = m48t86_readb(dev, M48T86_B);
155 156

	seq_printf(seq, "mode\t\t: %s\n",
157
		   (reg & M48T86_B_DM) ? "binary" : "bcd");
158

159
	reg = m48t86_readb(dev, M48T86_D);
160 161

	seq_printf(seq, "battery\t\t: %s\n",
162
		   (reg & M48T86_D_VRT) ? "ok" : "exhausted");
163 164 165 166

	return 0;
}

167
static const struct rtc_class_ops m48t86_rtc_ops = {
168 169 170 171 172
	.read_time	= m48t86_rtc_read_time,
	.set_time	= m48t86_rtc_set_time,
	.proc		= m48t86_rtc_proc,
};

173
static int m48t86_rtc_probe(struct platform_device *pdev)
174
{
175
	struct m48t86_rtc_info *info;
176
	unsigned char reg;
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	info->ops = dev_get_platdata(&pdev->dev);
	if (!info->ops) {
		struct resource *res;

		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
		if (!res)
			return -ENODEV;
		info->index_reg = devm_ioremap_resource(&pdev->dev, res);
		if (IS_ERR(info->index_reg))
			return PTR_ERR(info->index_reg);

		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
		if (!res)
			return -ENODEV;
		info->data_reg = devm_ioremap_resource(&pdev->dev, res);
		if (IS_ERR(info->data_reg))
			return PTR_ERR(info->data_reg);
	}
200

201
	dev_set_drvdata(&pdev->dev, info);
202

203 204 205 206
	info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
					     &m48t86_rtc_ops, THIS_MODULE);
	if (IS_ERR(info->rtc))
		return PTR_ERR(info->rtc);
207 208

	/* read battery status */
209 210
	reg = m48t86_readb(&pdev->dev, M48T86_D);
	dev_info(&pdev->dev, "battery %s\n",
211
		 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
212 213 214 215 216 217 218 219 220 221 222

	return 0;
}

static struct platform_driver m48t86_rtc_platform_driver = {
	.driver		= {
		.name	= "rtc-m48t86",
	},
	.probe		= m48t86_rtc_probe,
};

223
module_platform_driver(m48t86_rtc_platform_driver);
224 225 226 227

MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
MODULE_DESCRIPTION("M48T86 RTC driver");
MODULE_LICENSE("GPL");
228
MODULE_ALIAS("platform:rtc-m48t86");