rtc-m48t86.c 4.95 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 <[email protected]>
 *
 * 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 21
#include <linux/bcd.h>

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#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)
40 41 42 43 44

static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	unsigned char reg;
	struct platform_device *pdev = to_platform_device(dev);
45
	struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
46

47
	reg = ops->readbyte(M48T86_B);
48

49
	if (reg & M48T86_B_DM) {
50
		/* data (binary) mode */
51 52 53 54
		tm->tm_sec	= ops->readbyte(M48T86_SEC);
		tm->tm_min	= ops->readbyte(M48T86_MIN);
		tm->tm_hour	= ops->readbyte(M48T86_HOUR) & 0x3f;
		tm->tm_mday	= ops->readbyte(M48T86_DOM);
55
		/* tm_mon is 0-11 */
56 57 58
		tm->tm_mon	= ops->readbyte(M48T86_MONTH) - 1;
		tm->tm_year	= ops->readbyte(M48T86_YEAR) + 100;
		tm->tm_wday	= ops->readbyte(M48T86_DOW);
59 60
	} else {
		/* bcd mode */
61 62 63 64
		tm->tm_sec	= bcd2bin(ops->readbyte(M48T86_SEC));
		tm->tm_min	= bcd2bin(ops->readbyte(M48T86_MIN));
		tm->tm_hour	= bcd2bin(ops->readbyte(M48T86_HOUR) & 0x3f);
		tm->tm_mday	= bcd2bin(ops->readbyte(M48T86_DOM));
65
		/* tm_mon is 0-11 */
66 67 68
		tm->tm_mon	= bcd2bin(ops->readbyte(M48T86_MONTH)) - 1;
		tm->tm_year	= bcd2bin(ops->readbyte(M48T86_YEAR)) + 100;
		tm->tm_wday	= bcd2bin(ops->readbyte(M48T86_DOW));
69 70 71
	}

	/* correct the hour if the clock is in 12h mode */
72 73
	if (!(reg & M48T86_B_H24))
		if (ops->readbyte(M48T86_HOUR) & 0x80)
74 75
			tm->tm_hour += 12;

76
	return rtc_valid_tm(tm);
77 78 79 80 81 82
}

static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	unsigned char reg;
	struct platform_device *pdev = to_platform_device(dev);
83
	struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
84

85
	reg = ops->readbyte(M48T86_B);
86 87

	/* update flag and 24h mode */
88 89
	reg |= M48T86_B_SET | M48T86_B_H24;
	ops->writebyte(reg, M48T86_B);
90

91
	if (reg & M48T86_B_DM) {
92
		/* data (binary) mode */
93 94 95 96 97 98 99
		ops->writebyte(tm->tm_sec, M48T86_SEC);
		ops->writebyte(tm->tm_min, M48T86_MIN);
		ops->writebyte(tm->tm_hour, M48T86_HOUR);
		ops->writebyte(tm->tm_mday, M48T86_DOM);
		ops->writebyte(tm->tm_mon + 1, M48T86_MONTH);
		ops->writebyte(tm->tm_year % 100, M48T86_YEAR);
		ops->writebyte(tm->tm_wday, M48T86_DOW);
100 101
	} else {
		/* bcd mode */
102 103 104 105 106 107 108
		ops->writebyte(bin2bcd(tm->tm_sec), M48T86_SEC);
		ops->writebyte(bin2bcd(tm->tm_min), M48T86_MIN);
		ops->writebyte(bin2bcd(tm->tm_hour), M48T86_HOUR);
		ops->writebyte(bin2bcd(tm->tm_mday), M48T86_DOM);
		ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
		ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_YEAR);
		ops->writebyte(bin2bcd(tm->tm_wday), M48T86_DOW);
109 110 111
	}

	/* update ended */
112 113
	reg &= ~M48T86_B_SET;
	ops->writebyte(reg, M48T86_B);
114 115 116 117 118 119 120 121

	return 0;
}

static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
{
	unsigned char reg;
	struct platform_device *pdev = to_platform_device(dev);
122
	struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
123

124
	reg = ops->readbyte(M48T86_B);
125 126

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

129
	reg = ops->readbyte(M48T86_D);
130 131

	seq_printf(seq, "battery\t\t: %s\n",
132
		   (reg & M48T86_D_VRT) ? "ok" : "exhausted");
133 134 135 136

	return 0;
}

137
static const struct rtc_class_ops m48t86_rtc_ops = {
138 139 140 141 142
	.read_time	= m48t86_rtc_read_time,
	.set_time	= m48t86_rtc_set_time,
	.proc		= m48t86_rtc_proc,
};

143
static int m48t86_rtc_probe(struct platform_device *dev)
144 145
{
	unsigned char reg;
146
	struct m48t86_ops *ops = dev_get_platdata(&dev->dev);
147 148 149
	struct rtc_device *rtc;

	rtc = devm_rtc_device_register(&dev->dev, "m48t86",
150
				       &m48t86_rtc_ops, THIS_MODULE);
151

152
	if (IS_ERR(rtc))
153 154 155 156 157
		return PTR_ERR(rtc);

	platform_set_drvdata(dev, rtc);

	/* read battery status */
158
	reg = ops->readbyte(M48T86_D);
159
	dev_info(&dev->dev, "battery %s\n",
160
		 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
161 162 163 164 165 166 167 168 169 170 171

	return 0;
}

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

172
module_platform_driver(m48t86_rtc_platform_driver);
173 174 175 176

MODULE_AUTHOR("Alessandro Zummo <[email protected]>");
MODULE_DESCRIPTION("M48T86 RTC driver");
MODULE_LICENSE("GPL");
177
MODULE_ALIAS("platform:rtc-m48t86");