Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
  4 *
  5 * Copyright (C) 2000 Silicon Graphics, Inc.
  6 * Written by Ulf Carlsson (ulfc@engr.sgi.com)
  7 *
  8 * Copyright (C) 2008 Thomas Bogendoerfer
  9 *
 10 * Based on code written by Paul Gortmaker.
 
 
 
 
 
 11 */
 12
 13#include <linux/module.h>
 14#include <linux/rtc.h>
 15#include <linux/slab.h>
 16#include <linux/platform_device.h>
 17#include <linux/bcd.h>
 18#include <linux/io.h>
 19#include <linux/err.h>
 
 20
 21struct m48t35_rtc {
 22	u8	pad[0x7ff8];    /* starts at 0x7ff8 */
 23#ifdef CONFIG_SGI_IP27
 24	u8	hour;
 25	u8	min;
 26	u8	sec;
 27	u8	control;
 28	u8	year;
 29	u8	month;
 30	u8	date;
 31	u8	day;
 32#else
 33	u8	control;
 34	u8	sec;
 35	u8	min;
 36	u8	hour;
 37	u8	day;
 38	u8	date;
 39	u8	month;
 40	u8	year;
 41#endif
 42};
 43
 44#define M48T35_RTC_SET		0x80
 45#define M48T35_RTC_READ		0x40
 46
 47struct m48t35_priv {
 48	struct rtc_device *rtc;
 49	struct m48t35_rtc __iomem *reg;
 50	size_t size;
 51	unsigned long baseaddr;
 52	spinlock_t lock;
 53};
 54
 55static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
 56{
 57	struct m48t35_priv *priv = dev_get_drvdata(dev);
 58	u8 control;
 59
 60	/*
 61	 * Only the values that we read from the RTC are set. We leave
 62	 * tm_wday, tm_yday and tm_isdst untouched. Even though the
 63	 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
 64	 * by the RTC when initially set to a non-zero value.
 65	 */
 66	spin_lock_irq(&priv->lock);
 67	control = readb(&priv->reg->control);
 68	writeb(control | M48T35_RTC_READ, &priv->reg->control);
 69	tm->tm_sec = readb(&priv->reg->sec);
 70	tm->tm_min = readb(&priv->reg->min);
 71	tm->tm_hour = readb(&priv->reg->hour);
 72	tm->tm_mday = readb(&priv->reg->date);
 73	tm->tm_mon = readb(&priv->reg->month);
 74	tm->tm_year = readb(&priv->reg->year);
 75	writeb(control, &priv->reg->control);
 76	spin_unlock_irq(&priv->lock);
 77
 78	tm->tm_sec = bcd2bin(tm->tm_sec);
 79	tm->tm_min = bcd2bin(tm->tm_min);
 80	tm->tm_hour = bcd2bin(tm->tm_hour);
 81	tm->tm_mday = bcd2bin(tm->tm_mday);
 82	tm->tm_mon = bcd2bin(tm->tm_mon);
 83	tm->tm_year = bcd2bin(tm->tm_year);
 84
 85	/*
 86	 * Account for differences between how the RTC uses the values
 87	 * and how they are defined in a struct rtc_time;
 88	 */
 89	tm->tm_year += 70;
 90	if (tm->tm_year <= 69)
 91		tm->tm_year += 100;
 92
 93	tm->tm_mon--;
 94	return 0;
 95}
 96
 97static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
 98{
 99	struct m48t35_priv *priv = dev_get_drvdata(dev);
100	unsigned char mon, day, hrs, min, sec;
101	unsigned int yrs;
102	u8 control;
103
104	yrs = tm->tm_year + 1900;
105	mon = tm->tm_mon + 1;   /* tm_mon starts at zero */
106	day = tm->tm_mday;
107	hrs = tm->tm_hour;
108	min = tm->tm_min;
109	sec = tm->tm_sec;
110
111	if (yrs < 1970)
112		return -EINVAL;
113
114	yrs -= 1970;
115	if (yrs > 255)    /* They are unsigned */
116		return -EINVAL;
117
118	if (yrs > 169)
119		return -EINVAL;
120
121	if (yrs >= 100)
122		yrs -= 100;
123
124	sec = bin2bcd(sec);
125	min = bin2bcd(min);
126	hrs = bin2bcd(hrs);
127	day = bin2bcd(day);
128	mon = bin2bcd(mon);
129	yrs = bin2bcd(yrs);
130
131	spin_lock_irq(&priv->lock);
132	control = readb(&priv->reg->control);
133	writeb(control | M48T35_RTC_SET, &priv->reg->control);
134	writeb(yrs, &priv->reg->year);
135	writeb(mon, &priv->reg->month);
136	writeb(day, &priv->reg->date);
137	writeb(hrs, &priv->reg->hour);
138	writeb(min, &priv->reg->min);
139	writeb(sec, &priv->reg->sec);
140	writeb(control, &priv->reg->control);
141	spin_unlock_irq(&priv->lock);
142	return 0;
143}
144
145static const struct rtc_class_ops m48t35_ops = {
146	.read_time	= m48t35_read_time,
147	.set_time	= m48t35_set_time,
148};
149
150static int m48t35_probe(struct platform_device *pdev)
151{
152	struct resource *res;
153	struct m48t35_priv *priv;
 
154
155	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
156	if (!res)
157		return -ENODEV;
158	priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
159	if (!priv)
160		return -ENOMEM;
161
162	priv->size = resource_size(res);
163	if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
164				     pdev->name))
165		return -EBUSY;
166
 
 
 
 
 
 
167	priv->baseaddr = res->start;
168	priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
169	if (!priv->reg)
170		return -ENOMEM;
 
 
171
172	spin_lock_init(&priv->lock);
173
174	platform_set_drvdata(pdev, priv);
175
176	priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
177				  &m48t35_ops, THIS_MODULE);
178	return PTR_ERR_OR_ZERO(priv->rtc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179}
180
181static struct platform_driver m48t35_platform_driver = {
182	.driver		= {
183		.name	= "rtc-m48t35",
 
184	},
185	.probe		= m48t35_probe,
 
186};
187
188module_platform_driver(m48t35_platform_driver);
 
 
 
 
 
 
 
 
189
190MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
191MODULE_DESCRIPTION("M48T35 RTC driver");
192MODULE_LICENSE("GPL");
 
193MODULE_ALIAS("platform:rtc-m48t35");
v3.1
 
  1/*
  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>
 19#include <linux/slab.h>
 20#include <linux/platform_device.h>
 21#include <linux/bcd.h>
 22#include <linux/io.h>
 23
 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
144static int __devinit m48t35_probe(struct platform_device *pdev)
145{
146	struct resource *res;
147	struct m48t35_priv *priv;
148	int ret = 0;
149
150	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151	if (!res)
152		return -ENODEV;
153	priv = kzalloc(sizeof(struct m48t35_priv), GFP_KERNEL);
154	if (!priv)
155		return -ENOMEM;
156
157	priv->size = resource_size(res);
158	/*
159	 * kludge: remove the #ifndef after ioc3 resource
160	 * conflicts are resolved
161	 */
162#ifndef CONFIG_SGI_IP27
163	if (!request_mem_region(res->start, priv->size, pdev->name)) {
164		ret = -EBUSY;
165		goto out;
166	}
167#endif
168	priv->baseaddr = res->start;
169	priv->reg = ioremap(priv->baseaddr, priv->size);
170	if (!priv->reg) {
171		ret = -ENOMEM;
172		goto out;
173	}
174
175	spin_lock_init(&priv->lock);
176
177	platform_set_drvdata(pdev, priv);
178
179	priv->rtc = rtc_device_register("m48t35", &pdev->dev,
180				  &m48t35_ops, THIS_MODULE);
181	if (IS_ERR(priv->rtc)) {
182		ret = PTR_ERR(priv->rtc);
183		goto out;
184	}
185
186	return 0;
187
188out:
189	if (priv->reg)
190		iounmap(priv->reg);
191	if (priv->baseaddr)
192		release_mem_region(priv->baseaddr, priv->size);
193	kfree(priv);
194	return ret;
195}
196
197static int __devexit m48t35_remove(struct platform_device *pdev)
198{
199	struct m48t35_priv *priv = platform_get_drvdata(pdev);
200
201	rtc_device_unregister(priv->rtc);
202	iounmap(priv->reg);
203#ifndef CONFIG_SGI_IP27
204	release_mem_region(priv->baseaddr, priv->size);
205#endif
206	kfree(priv);
207	return 0;
208}
209
210static struct platform_driver m48t35_platform_driver = {
211	.driver		= {
212		.name	= "rtc-m48t35",
213		.owner	= THIS_MODULE,
214	},
215	.probe		= m48t35_probe,
216	.remove		= __devexit_p(m48t35_remove),
217};
218
219static int __init m48t35_init(void)
220{
221	return platform_driver_register(&m48t35_platform_driver);
222}
223
224static void __exit m48t35_exit(void)
225{
226	platform_driver_unregister(&m48t35_platform_driver);
227}
228
229MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
230MODULE_DESCRIPTION("M48T35 RTC driver");
231MODULE_LICENSE("GPL");
232MODULE_VERSION(DRV_VERSION);
233MODULE_ALIAS("platform:rtc-m48t35");
234
235module_init(m48t35_init);
236module_exit(m48t35_exit);