blob: 32f64c942621c03502cb118b551f9174c671cc3b [file] [log] [blame]
Alessandro Zummo1d98af82006-03-27 01:16:47 -08001/*
2 * ST M48T86 / Dallas DS12887 RTC driver
3 * Copyright (c) 2006 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This drivers only supports the clock running in BCD and 24H mode.
12 * If it will be ever adapted to binary and 12H mode, care must be taken
13 * to not introduce bugs.
14 */
15
16#include <linux/module.h>
17#include <linux/rtc.h>
18#include <linux/platform_device.h>
19#include <linux/m48t86.h>
20#include <linux/bcd.h>
21
22#define M48T86_REG_SEC 0x00
23#define M48T86_REG_SECALRM 0x01
24#define M48T86_REG_MIN 0x02
25#define M48T86_REG_MINALRM 0x03
Alessandro Zummof90a6502006-04-10 22:54:42 -070026#define M48T86_REG_HOUR 0x04
Alessandro Zummo1d98af82006-03-27 01:16:47 -080027#define M48T86_REG_HOURALRM 0x05
28#define M48T86_REG_DOW 0x06 /* 1 = sunday */
29#define M48T86_REG_DOM 0x07
30#define M48T86_REG_MONTH 0x08 /* 1 - 12 */
31#define M48T86_REG_YEAR 0x09 /* 0 - 99 */
32#define M48T86_REG_A 0x0A
33#define M48T86_REG_B 0x0B
34#define M48T86_REG_C 0x0C
35#define M48T86_REG_D 0x0D
36
37#define M48T86_REG_B_H24 (1 << 1)
38#define M48T86_REG_B_DM (1 << 2)
39#define M48T86_REG_B_SET (1 << 7)
40#define M48T86_REG_D_VRT (1 << 7)
41
42#define DRV_VERSION "0.1"
43
44
45static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
46{
47 unsigned char reg;
48 struct platform_device *pdev = to_platform_device(dev);
Jingoo Hanb5f09022013-11-12 15:10:44 -080049 struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080050
Andrew Morton2d7b20c2006-06-04 02:51:42 -070051 reg = ops->readbyte(M48T86_REG_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080052
53 if (reg & M48T86_REG_B_DM) {
54 /* data (binary) mode */
Andrew Morton2d7b20c2006-06-04 02:51:42 -070055 tm->tm_sec = ops->readbyte(M48T86_REG_SEC);
56 tm->tm_min = ops->readbyte(M48T86_REG_MIN);
57 tm->tm_hour = ops->readbyte(M48T86_REG_HOUR) & 0x3F;
58 tm->tm_mday = ops->readbyte(M48T86_REG_DOM);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080059 /* tm_mon is 0-11 */
Andrew Morton2d7b20c2006-06-04 02:51:42 -070060 tm->tm_mon = ops->readbyte(M48T86_REG_MONTH) - 1;
61 tm->tm_year = ops->readbyte(M48T86_REG_YEAR) + 100;
62 tm->tm_wday = ops->readbyte(M48T86_REG_DOW);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080063 } else {
64 /* bcd mode */
Adrian Bunkfe20ba72008-10-18 20:28:41 -070065 tm->tm_sec = bcd2bin(ops->readbyte(M48T86_REG_SEC));
66 tm->tm_min = bcd2bin(ops->readbyte(M48T86_REG_MIN));
67 tm->tm_hour = bcd2bin(ops->readbyte(M48T86_REG_HOUR) & 0x3F);
68 tm->tm_mday = bcd2bin(ops->readbyte(M48T86_REG_DOM));
Alessandro Zummo1d98af82006-03-27 01:16:47 -080069 /* tm_mon is 0-11 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -070070 tm->tm_mon = bcd2bin(ops->readbyte(M48T86_REG_MONTH)) - 1;
71 tm->tm_year = bcd2bin(ops->readbyte(M48T86_REG_YEAR)) + 100;
72 tm->tm_wday = bcd2bin(ops->readbyte(M48T86_REG_DOW));
Alessandro Zummo1d98af82006-03-27 01:16:47 -080073 }
74
75 /* correct the hour if the clock is in 12h mode */
76 if (!(reg & M48T86_REG_B_H24))
Andrew Morton2d7b20c2006-06-04 02:51:42 -070077 if (ops->readbyte(M48T86_REG_HOUR) & 0x80)
Alessandro Zummo1d98af82006-03-27 01:16:47 -080078 tm->tm_hour += 12;
79
Wan ZongShun52142ed2010-08-10 18:02:17 -070080 return rtc_valid_tm(tm);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080081}
82
83static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
84{
85 unsigned char reg;
86 struct platform_device *pdev = to_platform_device(dev);
Jingoo Hanb5f09022013-11-12 15:10:44 -080087 struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080088
Andrew Morton2d7b20c2006-06-04 02:51:42 -070089 reg = ops->readbyte(M48T86_REG_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080090
91 /* update flag and 24h mode */
92 reg |= M48T86_REG_B_SET | M48T86_REG_B_H24;
Andrew Morton2d7b20c2006-06-04 02:51:42 -070093 ops->writebyte(reg, M48T86_REG_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080094
95 if (reg & M48T86_REG_B_DM) {
96 /* data (binary) mode */
Andrew Morton2d7b20c2006-06-04 02:51:42 -070097 ops->writebyte(tm->tm_sec, M48T86_REG_SEC);
98 ops->writebyte(tm->tm_min, M48T86_REG_MIN);
99 ops->writebyte(tm->tm_hour, M48T86_REG_HOUR);
100 ops->writebyte(tm->tm_mday, M48T86_REG_DOM);
101 ops->writebyte(tm->tm_mon + 1, M48T86_REG_MONTH);
102 ops->writebyte(tm->tm_year % 100, M48T86_REG_YEAR);
103 ops->writebyte(tm->tm_wday, M48T86_REG_DOW);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800104 } else {
105 /* bcd mode */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700106 ops->writebyte(bin2bcd(tm->tm_sec), M48T86_REG_SEC);
107 ops->writebyte(bin2bcd(tm->tm_min), M48T86_REG_MIN);
108 ops->writebyte(bin2bcd(tm->tm_hour), M48T86_REG_HOUR);
109 ops->writebyte(bin2bcd(tm->tm_mday), M48T86_REG_DOM);
110 ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH);
111 ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR);
112 ops->writebyte(bin2bcd(tm->tm_wday), M48T86_REG_DOW);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800113 }
114
115 /* update ended */
116 reg &= ~M48T86_REG_B_SET;
Andrew Morton2d7b20c2006-06-04 02:51:42 -0700117 ops->writebyte(reg, M48T86_REG_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800118
119 return 0;
120}
121
122static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
123{
124 unsigned char reg;
125 struct platform_device *pdev = to_platform_device(dev);
Jingoo Hanb5f09022013-11-12 15:10:44 -0800126 struct m48t86_ops *ops = dev_get_platdata(&pdev->dev);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800127
Andrew Morton2d7b20c2006-06-04 02:51:42 -0700128 reg = ops->readbyte(M48T86_REG_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800129
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800130 seq_printf(seq, "mode\t\t: %s\n",
131 (reg & M48T86_REG_B_DM) ? "binary" : "bcd");
132
Andrew Morton2d7b20c2006-06-04 02:51:42 -0700133 reg = ops->readbyte(M48T86_REG_D);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800134
135 seq_printf(seq, "battery\t\t: %s\n",
136 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
137
138 return 0;
139}
140
David Brownellff8371a2006-09-30 23:28:17 -0700141static const struct rtc_class_ops m48t86_rtc_ops = {
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800142 .read_time = m48t86_rtc_read_time,
143 .set_time = m48t86_rtc_set_time,
144 .proc = m48t86_rtc_proc,
145};
146
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800147static int m48t86_rtc_probe(struct platform_device *dev)
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800148{
149 unsigned char reg;
Jingoo Hanb5f09022013-11-12 15:10:44 -0800150 struct m48t86_ops *ops = dev_get_platdata(&dev->dev);
Jingoo Hand5b6bb02013-04-29 16:19:42 -0700151 struct rtc_device *rtc;
152
153 rtc = devm_rtc_device_register(&dev->dev, "m48t86",
154 &m48t86_rtc_ops, THIS_MODULE);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800155
Alessandro Zummod1d65b72006-04-10 22:54:45 -0700156 if (IS_ERR(rtc))
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800157 return PTR_ERR(rtc);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800158
159 platform_set_drvdata(dev, rtc);
160
161 /* read battery status */
Andrew Morton2d7b20c2006-06-04 02:51:42 -0700162 reg = ops->readbyte(M48T86_REG_D);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800163 dev_info(&dev->dev, "battery %s\n",
164 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
165
166 return 0;
167}
168
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800169static struct platform_driver m48t86_rtc_platform_driver = {
170 .driver = {
171 .name = "rtc-m48t86",
172 .owner = THIS_MODULE,
173 },
174 .probe = m48t86_rtc_probe,
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800175};
176
Axel Lin0c4eae62012-01-10 15:10:48 -0800177module_platform_driver(m48t86_rtc_platform_driver);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800178
179MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
180MODULE_DESCRIPTION("M48T86 RTC driver");
181MODULE_LICENSE("GPL");
182MODULE_VERSION(DRV_VERSION);
Kay Sieversad28a072008-04-10 21:29:25 -0700183MODULE_ALIAS("platform:rtc-m48t86");