rtc-ftrtc010.c 5.78 KB
Newer Older
1
/*
2
 *  Faraday Technology FTRTC010 driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Original code for older kernel 2.6.15 are from Stormlinksemi
 * first update from Janos Laube for > 2.6.29 kernels
 *
 * checkpatch fixes and usage of rtc-lib code
 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
 */

#include <linux/rtc.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/module.h>
29
#include <linux/mod_devicetable.h>
30
#include <linux/clk.h>
31

32
#define DRV_NAME        "rtc-ftrtc010"
33 34 35 36 37 38

MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
MODULE_DESCRIPTION("RTC driver for Gemini SoC");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);

39
struct ftrtc010_rtc {
40 41 42
	struct rtc_device	*rtc_dev;
	void __iomem		*rtc_base;
	int			rtc_irq;
43 44
	struct clk		*pclk;
	struct clk		*extclk;
45 46
};

47 48 49 50 51 52 53 54 55 56
enum ftrtc010_rtc_offsets {
	FTRTC010_RTC_SECOND		= 0x00,
	FTRTC010_RTC_MINUTE		= 0x04,
	FTRTC010_RTC_HOUR		= 0x08,
	FTRTC010_RTC_DAYS		= 0x0C,
	FTRTC010_RTC_ALARM_SECOND	= 0x10,
	FTRTC010_RTC_ALARM_MINUTE	= 0x14,
	FTRTC010_RTC_ALARM_HOUR		= 0x18,
	FTRTC010_RTC_RECORD		= 0x1C,
	FTRTC010_RTC_CR			= 0x20,
57 58
};

59
static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
60 61 62 63 64 65 66 67 68 69 70 71 72
{
	return IRQ_HANDLED;
}

/*
 * Looks like the RTC in the Gemini SoC is (totaly) broken
 * We can't read/write directly the time from RTC registers.
 * We must do some "offset" calculation to get the real time
 *
 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
 * the same thing, without the rtc-lib.c calls.
 */

73
static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
74
{
75
	struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
76

77 78
	u32 days, hour, min, sec, offset;
	timeu64_t time;
79

80 81 82 83 84
	sec  = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
	min  = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
	hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
	days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
	offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
85 86 87

	time = offset + days * 86400 + hour * 3600 + min * 60 + sec;

88
	rtc_time64_to_tm(time, tm);
89 90 91 92

	return 0;
}

93
static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
94
{
95
	struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
96 97
	u32 sec, min, hour, day, offset;
	timeu64_t time;
98

99
	time = rtc_tm_to_time64(tm);
100

101 102 103 104
	sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
	min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
	hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
	day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
105 106 107

	offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);

108 109
	writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
	writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
110 111 112 113

	return 0;
}

114 115 116
static const struct rtc_class_ops ftrtc010_rtc_ops = {
	.read_time     = ftrtc010_rtc_read_time,
	.set_time      = ftrtc010_rtc_set_time,
117 118
};

119
static int ftrtc010_rtc_probe(struct platform_device *pdev)
120
{
121
	u32 days, hour, min, sec;
122
	struct ftrtc010_rtc *rtc;
123 124 125 126 127 128 129 130 131
	struct device *dev = &pdev->dev;
	struct resource *res;
	int ret;

	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
	if (unlikely(!rtc))
		return -ENOMEM;
	platform_set_drvdata(pdev, rtc);

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	rtc->pclk = devm_clk_get(dev, "PCLK");
	if (IS_ERR(rtc->pclk)) {
		dev_err(dev, "could not get PCLK\n");
	} else {
		ret = clk_prepare_enable(rtc->pclk);
		if (ret) {
			dev_err(dev, "failed to enable PCLK\n");
			return ret;
		}
	}
	rtc->extclk = devm_clk_get(dev, "EXTCLK");
	if (IS_ERR(rtc->extclk)) {
		dev_err(dev, "could not get EXTCLK\n");
	} else {
		ret = clk_prepare_enable(rtc->extclk);
		if (ret) {
			dev_err(dev, "failed to enable EXTCLK\n");
			return ret;
		}
	}

153 154 155 156 157 158 159 160 161 162 163
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res)
		return -ENODEV;

	rtc->rtc_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;

	rtc->rtc_base = devm_ioremap(dev, res->start,
164
				     resource_size(res));
165 166
	if (!rtc->rtc_base)
		return -ENOMEM;
167

168 169 170 171 172 173
	rtc->rtc_dev = devm_rtc_allocate_device(dev);
	if (IS_ERR(rtc->rtc_dev))
		return PTR_ERR(rtc->rtc_dev);

	rtc->rtc_dev->ops = &ftrtc010_rtc_ops;

174 175 176 177 178 179 180 181 182
	sec  = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
	min  = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
	hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
	days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);

	rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
				  min * 60 + sec;
	rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;

183
	ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
184 185 186 187
			       IRQF_SHARED, pdev->name, dev);
	if (unlikely(ret))
		return ret;

188
	return rtc_register_device(rtc->rtc_dev);
189 190
}

191
static int ftrtc010_rtc_remove(struct platform_device *pdev)
192
{
193
	struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
194

195 196 197 198
	if (!IS_ERR(rtc->extclk))
		clk_disable_unprepare(rtc->extclk);
	if (!IS_ERR(rtc->pclk))
		clk_disable_unprepare(rtc->pclk);
199 200 201 202

	return 0;
}

203
static const struct of_device_id ftrtc010_rtc_dt_match[] = {
204
	{ .compatible = "cortina,gemini-rtc" },
205
	{ .compatible = "faraday,ftrtc010" },
206 207
	{ }
};
208
MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
209

210
static struct platform_driver ftrtc010_rtc_driver = {
211 212
	.driver		= {
		.name	= DRV_NAME,
213
		.of_match_table = ftrtc010_rtc_dt_match,
214
	},
215 216
	.probe		= ftrtc010_rtc_probe,
	.remove		= ftrtc010_rtc_remove,
217 218
};

219
module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);