blob: 411adb3f86a1d89e2f9ad39683315a5b8440ce2e [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>
Sachin Kamat8ff7b7d2013-07-03 15:07:50 -070023#include <linux/err.h>
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +020024
25#define DRV_VERSION "1.0"
26
27struct m48t35_rtc {
28 u8 pad[0x7ff8]; /* starts at 0x7ff8 */
29 u8 control;
30 u8 sec;
31 u8 min;
32 u8 hour;
33 u8 day;
34 u8 date;
35 u8 month;
36 u8 year;
37};
38
39#define M48T35_RTC_SET 0x80
40#define M48T35_RTC_READ 0x40
41
42struct m48t35_priv {
43 struct rtc_device *rtc;
44 struct m48t35_rtc __iomem *reg;
45 size_t size;
46 unsigned long baseaddr;
47 spinlock_t lock;
48};
49
50static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
51{
52 struct m48t35_priv *priv = dev_get_drvdata(dev);
53 u8 control;
54
55 /*
56 * Only the values that we read from the RTC are set. We leave
57 * tm_wday, tm_yday and tm_isdst untouched. Even though the
58 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
59 * by the RTC when initially set to a non-zero value.
60 */
61 spin_lock_irq(&priv->lock);
62 control = readb(&priv->reg->control);
63 writeb(control | M48T35_RTC_READ, &priv->reg->control);
64 tm->tm_sec = readb(&priv->reg->sec);
65 tm->tm_min = readb(&priv->reg->min);
66 tm->tm_hour = readb(&priv->reg->hour);
67 tm->tm_mday = readb(&priv->reg->date);
68 tm->tm_mon = readb(&priv->reg->month);
69 tm->tm_year = readb(&priv->reg->year);
70 writeb(control, &priv->reg->control);
71 spin_unlock_irq(&priv->lock);
72
73 tm->tm_sec = bcd2bin(tm->tm_sec);
74 tm->tm_min = bcd2bin(tm->tm_min);
75 tm->tm_hour = bcd2bin(tm->tm_hour);
76 tm->tm_mday = bcd2bin(tm->tm_mday);
77 tm->tm_mon = bcd2bin(tm->tm_mon);
78 tm->tm_year = bcd2bin(tm->tm_year);
79
80 /*
81 * Account for differences between how the RTC uses the values
82 * and how they are defined in a struct rtc_time;
83 */
84 tm->tm_year += 70;
85 if (tm->tm_year <= 69)
86 tm->tm_year += 100;
87
88 tm->tm_mon--;
89 return rtc_valid_tm(tm);
90}
91
92static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
93{
94 struct m48t35_priv *priv = dev_get_drvdata(dev);
95 unsigned char mon, day, hrs, min, sec;
96 unsigned int yrs;
97 u8 control;
98
99 yrs = tm->tm_year + 1900;
100 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
101 day = tm->tm_mday;
102 hrs = tm->tm_hour;
103 min = tm->tm_min;
104 sec = tm->tm_sec;
105
106 if (yrs < 1970)
107 return -EINVAL;
108
109 yrs -= 1970;
110 if (yrs > 255) /* They are unsigned */
111 return -EINVAL;
112
113 if (yrs > 169)
114 return -EINVAL;
115
116 if (yrs >= 100)
117 yrs -= 100;
118
119 sec = bin2bcd(sec);
120 min = bin2bcd(min);
121 hrs = bin2bcd(hrs);
122 day = bin2bcd(day);
123 mon = bin2bcd(mon);
124 yrs = bin2bcd(yrs);
125
126 spin_lock_irq(&priv->lock);
127 control = readb(&priv->reg->control);
128 writeb(control | M48T35_RTC_SET, &priv->reg->control);
129 writeb(yrs, &priv->reg->year);
130 writeb(mon, &priv->reg->month);
131 writeb(day, &priv->reg->date);
132 writeb(hrs, &priv->reg->hour);
133 writeb(min, &priv->reg->min);
134 writeb(sec, &priv->reg->sec);
135 writeb(control, &priv->reg->control);
136 spin_unlock_irq(&priv->lock);
137 return 0;
138}
139
140static const struct rtc_class_ops m48t35_ops = {
141 .read_time = m48t35_read_time,
142 .set_time = m48t35_set_time,
143};
144
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800145static int m48t35_probe(struct platform_device *pdev)
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200146{
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200147 struct resource *res;
148 struct m48t35_priv *priv;
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200149
150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 if (!res)
152 return -ENODEV;
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700153 priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200154 if (!priv)
155 return -ENOMEM;
156
Joe Perches28f65c112011-06-09 09:13:32 -0700157 priv->size = resource_size(res);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200158 /*
159 * kludge: remove the #ifndef after ioc3 resource
160 * conflicts are resolved
161 */
162#ifndef CONFIG_SGI_IP27
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700163 if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
164 pdev->name))
165 return -EBUSY;
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200166#endif
167 priv->baseaddr = res->start;
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700168 priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
169 if (!priv->reg)
170 return -ENOMEM;
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800171
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200172 spin_lock_init(&priv->lock);
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800173
174 platform_set_drvdata(pdev, priv);
175
Sachin Kamat4f58cd92013-04-29 16:20:32 -0700176 priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200177 &m48t35_ops, THIS_MODULE);
Sachin Kamatdac30a92013-07-15 15:43:32 +0530178 return PTR_ERR_OR_ZERO(priv->rtc);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200179}
180
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200181static struct platform_driver m48t35_platform_driver = {
182 .driver = {
183 .name = "rtc-m48t35",
184 .owner = THIS_MODULE,
185 },
186 .probe = m48t35_probe,
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200187};
188
Axel Lin0c4eae62012-01-10 15:10:48 -0800189module_platform_driver(m48t35_platform_driver);
Thomas Bogendoerferd1dbd82e2008-10-14 17:17:32 +0200190
191MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
192MODULE_DESCRIPTION("M48T35 RTC driver");
193MODULE_LICENSE("GPL");
194MODULE_VERSION(DRV_VERSION);
195MODULE_ALIAS("platform:rtc-m48t35");