Linux Audio

Check our new training course

Loading...
v3.5.6
 
  1/*
  2 * Copyright (C) 2007-2009 ST-Ericsson AB
  3 * License terms: GNU General Public License (GPL) version 2
  4 * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC.
  5 * Author: Linus Walleij <linus.walleij@stericsson.com>
  6 * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net>
  7 * Copyright 2006 (c) MontaVista Software, Inc.
  8 */
  9#include <linux/init.h>
 10#include <linux/module.h>
 
 11#include <linux/rtc.h>
 12#include <linux/clk.h>
 13#include <linux/interrupt.h>
 14#include <linux/pm.h>
 15#include <linux/platform_device.h>
 16#include <linux/io.h>
 17#include <linux/slab.h>
 18
 19/*
 20 * Registers in the COH 901 331
 21 */
 22/* Alarm value 32bit (R/W) */
 23#define COH901331_ALARM		0x00U
 24/* Used to set current time 32bit (R/W) */
 25#define COH901331_SET_TIME	0x04U
 26/* Indication if current time is valid 32bit (R/-) */
 27#define COH901331_VALID		0x08U
 28/* Read the current time 32bit (R/-) */
 29#define COH901331_CUR_TIME	0x0cU
 30/* Event register for the "alarm" interrupt */
 31#define COH901331_IRQ_EVENT	0x10U
 32/* Mask register for the "alarm" interrupt */
 33#define COH901331_IRQ_MASK	0x14U
 34/* Force register for the "alarm" interrupt */
 35#define COH901331_IRQ_FORCE	0x18U
 36
 37/*
 38 * Reference to RTC block clock
 39 * Notice that the frequent clk_enable()/clk_disable() on this
 40 * clock is mainly to be able to turn on/off other clocks in the
 41 * hierarchy as needed, the RTC clock is always on anyway.
 42 */
 43struct coh901331_port {
 44	struct rtc_device *rtc;
 45	struct clk *clk;
 46	u32 phybase;
 47	u32 physize;
 48	void __iomem *virtbase;
 49	int irq;
 50#ifdef CONFIG_PM
 51	u32 irqmaskstore;
 52#endif
 53};
 54
 55static irqreturn_t coh901331_interrupt(int irq, void *data)
 56{
 57	struct coh901331_port *rtap = data;
 58
 59	clk_enable(rtap->clk);
 60	/* Ack IRQ */
 61	writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
 62	/*
 63	 * Disable the interrupt. This is necessary because
 64	 * the RTC lives on a lower-clocked line and will
 65	 * not release the IRQ line until after a few (slower)
 66	 * clock cycles. The interrupt will be re-enabled when
 67	 * a new alarm is set anyway.
 68	 */
 69	writel(0, rtap->virtbase + COH901331_IRQ_MASK);
 70	clk_disable(rtap->clk);
 71
 72	/* Set alarm flag */
 73	rtc_update_irq(rtap->rtc, 1, RTC_AF);
 74
 75	return IRQ_HANDLED;
 76}
 77
 78static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
 79{
 80	struct coh901331_port *rtap = dev_get_drvdata(dev);
 81
 82	clk_enable(rtap->clk);
 83	/* Check if the time is valid */
 84	if (readl(rtap->virtbase + COH901331_VALID)) {
 85		rtc_time_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
 86		clk_disable(rtap->clk);
 87		return rtc_valid_tm(tm);
 88	}
 
 
 89	clk_disable(rtap->clk);
 90	return -EINVAL;
 91}
 92
 93static int coh901331_set_mmss(struct device *dev, unsigned long secs)
 94{
 95	struct coh901331_port *rtap = dev_get_drvdata(dev);
 96
 97	clk_enable(rtap->clk);
 98	writel(secs, rtap->virtbase + COH901331_SET_TIME);
 99	clk_disable(rtap->clk);
100
101	return 0;
102}
103
104static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
105{
106	struct coh901331_port *rtap = dev_get_drvdata(dev);
107
108	clk_enable(rtap->clk);
109	rtc_time_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
110	alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
111	alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
112	clk_disable(rtap->clk);
113
114	return 0;
115}
116
117static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118{
119	struct coh901331_port *rtap = dev_get_drvdata(dev);
120	unsigned long time;
121
122	rtc_tm_to_time(&alarm->time, &time);
123	clk_enable(rtap->clk);
124	writel(time, rtap->virtbase + COH901331_ALARM);
125	writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
126	clk_disable(rtap->clk);
127
128	return 0;
129}
130
131static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
132{
133	struct coh901331_port *rtap = dev_get_drvdata(dev);
134
135	clk_enable(rtap->clk);
136	if (enabled)
137		writel(1, rtap->virtbase + COH901331_IRQ_MASK);
138	else
139		writel(0, rtap->virtbase + COH901331_IRQ_MASK);
140	clk_disable(rtap->clk);
141
142	return 0;
143}
144
145static struct rtc_class_ops coh901331_ops = {
146	.read_time = coh901331_read_time,
147	.set_mmss = coh901331_set_mmss,
148	.read_alarm = coh901331_read_alarm,
149	.set_alarm = coh901331_set_alarm,
150	.alarm_irq_enable = coh901331_alarm_irq_enable,
151};
152
153static int __exit coh901331_remove(struct platform_device *pdev)
154{
155	struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
156
157	if (rtap) {
158		free_irq(rtap->irq, rtap);
159		rtc_device_unregister(rtap->rtc);
160		clk_put(rtap->clk);
161		iounmap(rtap->virtbase);
162		release_mem_region(rtap->phybase, rtap->physize);
163		platform_set_drvdata(pdev, NULL);
164		kfree(rtap);
165	}
166
167	return 0;
168}
169
170
171static int __init coh901331_probe(struct platform_device *pdev)
172{
173	int ret;
174	struct coh901331_port *rtap;
175	struct resource *res;
176
177	rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
 
178	if (!rtap)
179		return -ENOMEM;
180
181	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182	if (!res) {
183		ret = -ENOENT;
184		goto out_no_resource;
185	}
186	rtap->phybase = res->start;
187	rtap->physize = resource_size(res);
188
189	if (request_mem_region(rtap->phybase, rtap->physize,
190			       "rtc-coh901331") == NULL) {
191		ret = -EBUSY;
192		goto out_no_memregion;
193	}
194
195	rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
196	if (!rtap->virtbase) {
197		ret = -ENOMEM;
198		goto out_no_remap;
199	}
200
201	rtap->irq = platform_get_irq(pdev, 0);
202	if (request_irq(rtap->irq, coh901331_interrupt, 0,
203			"RTC COH 901 331 Alarm", rtap)) {
204		ret = -EIO;
205		goto out_no_irq;
206	}
207
208	rtap->clk = clk_get(&pdev->dev, NULL);
209	if (IS_ERR(rtap->clk)) {
210		ret = PTR_ERR(rtap->clk);
211		dev_err(&pdev->dev, "could not get clock\n");
212		goto out_no_clk;
213	}
214
 
 
 
 
 
 
 
215	/* We enable/disable the clock only to assure it works */
216	ret = clk_enable(rtap->clk);
217	if (ret) {
218		dev_err(&pdev->dev, "could not enable clock\n");
219		goto out_no_clk_enable;
220	}
221	clk_disable(rtap->clk);
222
223	platform_set_drvdata(pdev, rtap);
224	rtap->rtc = rtc_device_register("coh901331", &pdev->dev, &coh901331_ops,
225					 THIS_MODULE);
226	if (IS_ERR(rtap->rtc)) {
227		ret = PTR_ERR(rtap->rtc);
228		goto out_no_rtc;
229	}
230
231	return 0;
232
233 out_no_rtc:
234	platform_set_drvdata(pdev, NULL);
235 out_no_clk_enable:
236	clk_put(rtap->clk);
237 out_no_clk:
238	free_irq(rtap->irq, rtap);
239 out_no_irq:
240	iounmap(rtap->virtbase);
241 out_no_remap:
242	platform_set_drvdata(pdev, NULL);
243 out_no_memregion:
244	release_mem_region(rtap->phybase, SZ_4K);
245 out_no_resource:
246	kfree(rtap);
247	return ret;
248}
249
250#ifdef CONFIG_PM
251static int coh901331_suspend(struct platform_device *pdev, pm_message_t state)
252{
253	struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
254
255	/*
256	 * If this RTC alarm will be used for waking the system up,
257	 * don't disable it of course. Else we just disable the alarm
258	 * and await suspension.
259	 */
260	if (device_may_wakeup(&pdev->dev)) {
261		enable_irq_wake(rtap->irq);
262	} else {
263		clk_enable(rtap->clk);
264		rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
265		writel(0, rtap->virtbase + COH901331_IRQ_MASK);
266		clk_disable(rtap->clk);
267	}
 
268	return 0;
269}
270
271static int coh901331_resume(struct platform_device *pdev)
272{
273	struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
 
274
275	if (device_may_wakeup(&pdev->dev)) {
 
 
 
 
276		disable_irq_wake(rtap->irq);
277	} else {
278		clk_enable(rtap->clk);
279		writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
280		clk_disable(rtap->clk);
281	}
282	return 0;
283}
284#else
285#define coh901331_suspend NULL
286#define coh901331_resume NULL
287#endif
288
 
 
289static void coh901331_shutdown(struct platform_device *pdev)
290{
291	struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
292
293	clk_enable(rtap->clk);
294	writel(0, rtap->virtbase + COH901331_IRQ_MASK);
295	clk_disable(rtap->clk);
296}
297
 
 
 
 
 
 
298static struct platform_driver coh901331_driver = {
299	.driver = {
300		.name = "rtc-coh901331",
301		.owner = THIS_MODULE,
 
302	},
303	.remove = __exit_p(coh901331_remove),
304	.suspend = coh901331_suspend,
305	.resume = coh901331_resume,
306	.shutdown = coh901331_shutdown,
307};
308
309static int __init coh901331_init(void)
310{
311	return platform_driver_probe(&coh901331_driver, coh901331_probe);
312}
313
314static void __exit coh901331_exit(void)
315{
316	platform_driver_unregister(&coh901331_driver);
317}
318
319module_init(coh901331_init);
320module_exit(coh901331_exit);
321
322MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
323MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
324MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2007-2009 ST-Ericsson AB
 
  4 * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC.
  5 * Author: Linus Walleij <linus.walleij@stericsson.com>
  6 * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net>
  7 * Copyright 2006 (c) MontaVista Software, Inc.
  8 */
  9#include <linux/init.h>
 10#include <linux/module.h>
 11#include <linux/mod_devicetable.h>
 12#include <linux/rtc.h>
 13#include <linux/clk.h>
 14#include <linux/interrupt.h>
 15#include <linux/pm.h>
 16#include <linux/platform_device.h>
 17#include <linux/io.h>
 18#include <linux/slab.h>
 19
 20/*
 21 * Registers in the COH 901 331
 22 */
 23/* Alarm value 32bit (R/W) */
 24#define COH901331_ALARM		0x00U
 25/* Used to set current time 32bit (R/W) */
 26#define COH901331_SET_TIME	0x04U
 27/* Indication if current time is valid 32bit (R/-) */
 28#define COH901331_VALID		0x08U
 29/* Read the current time 32bit (R/-) */
 30#define COH901331_CUR_TIME	0x0cU
 31/* Event register for the "alarm" interrupt */
 32#define COH901331_IRQ_EVENT	0x10U
 33/* Mask register for the "alarm" interrupt */
 34#define COH901331_IRQ_MASK	0x14U
 35/* Force register for the "alarm" interrupt */
 36#define COH901331_IRQ_FORCE	0x18U
 37
 38/*
 39 * Reference to RTC block clock
 40 * Notice that the frequent clk_enable()/clk_disable() on this
 41 * clock is mainly to be able to turn on/off other clocks in the
 42 * hierarchy as needed, the RTC clock is always on anyway.
 43 */
 44struct coh901331_port {
 45	struct rtc_device *rtc;
 46	struct clk *clk;
 
 
 47	void __iomem *virtbase;
 48	int irq;
 49#ifdef CONFIG_PM_SLEEP
 50	u32 irqmaskstore;
 51#endif
 52};
 53
 54static irqreturn_t coh901331_interrupt(int irq, void *data)
 55{
 56	struct coh901331_port *rtap = data;
 57
 58	clk_enable(rtap->clk);
 59	/* Ack IRQ */
 60	writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
 61	/*
 62	 * Disable the interrupt. This is necessary because
 63	 * the RTC lives on a lower-clocked line and will
 64	 * not release the IRQ line until after a few (slower)
 65	 * clock cycles. The interrupt will be re-enabled when
 66	 * a new alarm is set anyway.
 67	 */
 68	writel(0, rtap->virtbase + COH901331_IRQ_MASK);
 69	clk_disable(rtap->clk);
 70
 71	/* Set alarm flag */
 72	rtc_update_irq(rtap->rtc, 1, RTC_AF);
 73
 74	return IRQ_HANDLED;
 75}
 76
 77static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
 78{
 79	struct coh901331_port *rtap = dev_get_drvdata(dev);
 80
 81	clk_enable(rtap->clk);
 82	/* Check if the time is valid */
 83	if (!readl(rtap->virtbase + COH901331_VALID)) {
 
 84		clk_disable(rtap->clk);
 85		return -EINVAL;
 86	}
 87
 88	rtc_time64_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
 89	clk_disable(rtap->clk);
 90	return 0;
 91}
 92
 93static int coh901331_set_time(struct device *dev, struct rtc_time *tm)
 94{
 95	struct coh901331_port *rtap = dev_get_drvdata(dev);
 96
 97	clk_enable(rtap->clk);
 98	writel(rtc_tm_to_time64(tm), rtap->virtbase + COH901331_SET_TIME);
 99	clk_disable(rtap->clk);
100
101	return 0;
102}
103
104static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
105{
106	struct coh901331_port *rtap = dev_get_drvdata(dev);
107
108	clk_enable(rtap->clk);
109	rtc_time64_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
110	alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
111	alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
112	clk_disable(rtap->clk);
113
114	return 0;
115}
116
117static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118{
119	struct coh901331_port *rtap = dev_get_drvdata(dev);
120	unsigned long time =  rtc_tm_to_time64(&alarm->time);
121
 
122	clk_enable(rtap->clk);
123	writel(time, rtap->virtbase + COH901331_ALARM);
124	writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
125	clk_disable(rtap->clk);
126
127	return 0;
128}
129
130static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
131{
132	struct coh901331_port *rtap = dev_get_drvdata(dev);
133
134	clk_enable(rtap->clk);
135	if (enabled)
136		writel(1, rtap->virtbase + COH901331_IRQ_MASK);
137	else
138		writel(0, rtap->virtbase + COH901331_IRQ_MASK);
139	clk_disable(rtap->clk);
140
141	return 0;
142}
143
144static const struct rtc_class_ops coh901331_ops = {
145	.read_time = coh901331_read_time,
146	.set_time = coh901331_set_time,
147	.read_alarm = coh901331_read_alarm,
148	.set_alarm = coh901331_set_alarm,
149	.alarm_irq_enable = coh901331_alarm_irq_enable,
150};
151
152static int __exit coh901331_remove(struct platform_device *pdev)
153{
154	struct coh901331_port *rtap = platform_get_drvdata(pdev);
155
156	if (rtap)
157		clk_unprepare(rtap->clk);
 
 
 
 
 
 
 
158
159	return 0;
160}
161
162
163static int __init coh901331_probe(struct platform_device *pdev)
164{
165	int ret;
166	struct coh901331_port *rtap;
 
167
168	rtap = devm_kzalloc(&pdev->dev,
169			    sizeof(struct coh901331_port), GFP_KERNEL);
170	if (!rtap)
171		return -ENOMEM;
172
173	rtap->virtbase  = devm_platform_ioremap_resource(pdev, 0);
174	if (IS_ERR(rtap->virtbase))
175		return PTR_ERR(rtap->virtbase);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177	rtap->irq = platform_get_irq(pdev, 0);
178	if (devm_request_irq(&pdev->dev, rtap->irq, coh901331_interrupt, 0,
179			     "RTC COH 901 331 Alarm", rtap))
180		return -EIO;
 
 
181
182	rtap->clk = devm_clk_get(&pdev->dev, NULL);
183	if (IS_ERR(rtap->clk)) {
184		ret = PTR_ERR(rtap->clk);
185		dev_err(&pdev->dev, "could not get clock\n");
186		return ret;
187	}
188
189	rtap->rtc = devm_rtc_allocate_device(&pdev->dev);
190	if (IS_ERR(rtap->rtc))
191		return PTR_ERR(rtap->rtc);
192
193	rtap->rtc->ops = &coh901331_ops;
194	rtap->rtc->range_max = U32_MAX;
195
196	/* We enable/disable the clock only to assure it works */
197	ret = clk_prepare_enable(rtap->clk);
198	if (ret) {
199		dev_err(&pdev->dev, "could not enable clock\n");
200		return ret;
201	}
202	clk_disable(rtap->clk);
203
204	platform_set_drvdata(pdev, rtap);
205
206	ret = rtc_register_device(rtap->rtc);
207	if (ret)
 
208		goto out_no_rtc;
 
209
210	return 0;
211
212 out_no_rtc:
213	clk_unprepare(rtap->clk);
 
 
 
 
 
 
 
 
 
 
 
 
214	return ret;
215}
216
217#ifdef CONFIG_PM_SLEEP
218static int coh901331_suspend(struct device *dev)
219{
220	struct coh901331_port *rtap = dev_get_drvdata(dev);
221
222	/*
223	 * If this RTC alarm will be used for waking the system up,
224	 * don't disable it of course. Else we just disable the alarm
225	 * and await suspension.
226	 */
227	if (device_may_wakeup(dev)) {
228		enable_irq_wake(rtap->irq);
229	} else {
230		clk_enable(rtap->clk);
231		rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
232		writel(0, rtap->virtbase + COH901331_IRQ_MASK);
233		clk_disable(rtap->clk);
234	}
235	clk_unprepare(rtap->clk);
236	return 0;
237}
238
239static int coh901331_resume(struct device *dev)
240{
241	int ret;
242	struct coh901331_port *rtap = dev_get_drvdata(dev);
243
244	ret = clk_prepare(rtap->clk);
245	if (ret)
246		return ret;
247
248	if (device_may_wakeup(dev)) {
249		disable_irq_wake(rtap->irq);
250	} else {
251		clk_enable(rtap->clk);
252		writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
253		clk_disable(rtap->clk);
254	}
255	return 0;
256}
 
 
 
257#endif
258
259static SIMPLE_DEV_PM_OPS(coh901331_pm_ops, coh901331_suspend, coh901331_resume);
260
261static void coh901331_shutdown(struct platform_device *pdev)
262{
263	struct coh901331_port *rtap = platform_get_drvdata(pdev);
264
265	clk_enable(rtap->clk);
266	writel(0, rtap->virtbase + COH901331_IRQ_MASK);
267	clk_disable_unprepare(rtap->clk);
268}
269
270static const struct of_device_id coh901331_dt_match[] = {
271	{ .compatible = "stericsson,coh901331" },
272	{},
273};
274MODULE_DEVICE_TABLE(of, coh901331_dt_match);
275
276static struct platform_driver coh901331_driver = {
277	.driver = {
278		.name = "rtc-coh901331",
279		.pm = &coh901331_pm_ops,
280		.of_match_table = coh901331_dt_match,
281	},
282	.remove = __exit_p(coh901331_remove),
 
 
283	.shutdown = coh901331_shutdown,
284};
285
286module_platform_driver_probe(coh901331_driver, coh901331_probe);
 
 
 
 
 
 
 
 
 
 
 
287
288MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
289MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
290MODULE_LICENSE("GPL");