blob: 37444246e5e49e2989cbe743c2b284e9e8d9a4a0 [file] [log] [blame]
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +02001/*
2 * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
3 *
4 * Copyright (C) 2000 Silicon Graphics, Inc.
5 * Written by Ulf Carlsson (ulfc@engr.sgi.com)
6 *
7 * Copyright (C) 2008 Thomas Bogendoerfer
8 *
9 * Based on code written by Paul Gortmaker.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +020020#include <linux/platform_device.h>
21#include <linux/bcd.h>
Geert Uytterhoevend7a61192008-10-16 09:28:47 +020022#include <linux/io.h>
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +020023
24#define DRV_VERSION "1.0"
25
26struct m48t35_rtc {
27 u8 pad[0x7ff8]; /* starts at 0x7ff8 */
28 u8 control;
29 u8 sec;
30 u8 min;
31 u8 hour;
32 u8 day;
33 u8 date;
34 u8 month;
35 u8 year;
36};
37
38#define M48T35_RTC_SET 0x80
39#define M48T35_RTC_READ 0x40
40
41struct m48t35_priv {
42 struct rtc_device *rtc;
43 struct m48t35_rtc __iomem *reg;
44 size_t size;
45 unsigned long baseaddr;
46 spinlock_t lock;
47};
48
49static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
50{
51 struct m48t35_priv *priv = dev_get_drvdata(dev);
52 u8 control;
53
54 /*
55 * Only the values that we read from the RTC are set. We leave
56 * tm_wday, tm_yday and tm_isdst untouched. Even though the
57 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
58 * by the RTC when initially set to a non-zero value.
59 */
60 spin_lock_irq(&priv->lock);
61 control = readb(&priv->reg->control);
62 writeb(control | M48T35_RTC_READ, &priv->reg->control);
63 tm->tm_sec = readb(&priv->reg->sec);
64 tm->tm_min = readb(&priv->reg->min);
65 tm->tm_hour = readb(&priv->reg->hour);
66 tm->tm_mday = readb(&priv->reg->date);
67 tm->tm_mon = readb(&priv->reg->month);
68 tm->tm_year = readb(&priv->reg->year);
69 writeb(control, &priv->reg->control);
70 spin_unlock_irq(&priv->lock);
71
72 tm->tm_sec = bcd2bin(tm->tm_sec);
73 tm->tm_min = bcd2bin(tm->tm_min);
74 tm->tm_hour = bcd2bin(tm->tm_hour);
75 tm->tm_mday = bcd2bin(tm->tm_mday);
76 tm->tm_mon = bcd2bin(tm->tm_mon);
77 tm->tm_year = bcd2bin(tm->tm_year);
78
79 /*
80 * Account for differences between how the RTC uses the values
81 * and how they are defined in a struct rtc_time;
82 */
83 tm->tm_year += 70;
84 if (tm->tm_year <= 69)
85 tm->tm_year += 100;
86
87 tm->tm_mon--;
88 return rtc_valid_tm(tm);
89}
90
91static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
92{
93 struct m48t35_priv *priv = dev_get_drvdata(dev);
94 unsigned char mon, day, hrs, min, sec;
95 unsigned int yrs;
96 u8 control;
97
98 yrs = tm->tm_year + 1900;
99 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
100 day = tm->tm_mday;
101 hrs = tm->tm_hour;
102 min = tm->tm_min;
103 sec = tm->tm_sec;
104
105 if (yrs < 1970)
106 return -EINVAL;
107
108 yrs -= 1970;
109 if (yrs > 255) /* They are unsigned */
110 return -EINVAL;
111
112 if (yrs > 169)
113 return -EINVAL;
114
115 if (yrs >= 100)
116 yrs -= 100;
117
118 sec = bin2bcd(sec);
119 min = bin2bcd(min);
120 hrs = bin2bcd(hrs);
121 day = bin2bcd(day);
122 mon = bin2bcd(mon);
123 yrs = bin2bcd(yrs);
124
125 spin_lock_irq(&priv->lock);
126 control = readb(&priv->reg->control);
127 writeb(control | M48T35_RTC_SET, &priv->reg->control);
128 writeb(yrs, &priv->reg->year);
129 writeb(mon, &priv->reg->month);
130 writeb(day, &priv->reg->date);
131 writeb(hrs, &priv->reg->hour);
132 writeb(min, &priv->reg->min);
133 writeb(sec, &priv->reg->sec);
134 writeb(control, &priv->reg->control);
135 spin_unlock_irq(&priv->lock);
136 return 0;
137}
138
139static const struct rtc_class_ops m48t35_ops = {
140 .read_time = m48t35_read_time,
141 .set_time = m48t35_set_time,
142};
143
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800144static int m48t35_probe(struct platform_device *pdev)
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200145{
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200146 struct resource *res;
147 struct m48t35_priv *priv;
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200148
149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 if (!res)
151 return -ENODEV;
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700152 priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200153 if (!priv)
154 return -ENOMEM;
155
Joe Perches28f65c112011-06-09 09:13:32 -0700156 priv->size = resource_size(res);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200157 /*
158 * kludge: remove the #ifndef after ioc3 resource
159 * conflicts are resolved
160 */
161#ifndef CONFIG_SGI_IP27
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700162 if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
163 pdev->name))
164 return -EBUSY;
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200165#endif
166 priv->baseaddr = res->start;
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700167 priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
168 if (!priv->reg)
169 return -ENOMEM;
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800170
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200171 spin_lock_init(&priv->lock);
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800172
173 platform_set_drvdata(pdev, priv);
174
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700175 priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200176 &m48t35_ops, THIS_MODULE);
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700177 if (IS_ERR(priv->rtc))
178 return PTR_ERR(priv->rtc);
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800179
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200180 return 0;
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200181}
182
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800183static int m48t35_remove(struct platform_device *pdev)
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200184{
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200185 return 0;
186}
187
188static struct platform_driver m48t35_platform_driver = {
189 .driver = {
190 .name = "rtc-m48t35",
191 .owner = THIS_MODULE,
192 },
193 .probe = m48t35_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800194 .remove = m48t35_remove,
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200195};
196
Axel Lin0c4eae62012-01-10 15:10:48 -0800197module_platform_driver(m48t35_platform_driver);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200198
199MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
200MODULE_DESCRIPTION("M48T35 RTC driver");
201MODULE_LICENSE("GPL");
202MODULE_VERSION(DRV_VERSION);
203MODULE_ALIAS("platform:rtc-m48t35");