blob: c981301efbe5e8c8cf309854cd415260eb93f6be [file] [log] [blame]
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +01001/*
2 * Oki MSM6242 RTC Driver
3 *
4 * Copyright 2009 Geert Uytterhoeven
5 *
6 * Based on the A2000 TOD code in arch/m68k/amiga/config.c
7 * Copyright (C) 1993 Hamish Macdonald
8 */
9
Joe Perchesa737e832015-04-16 12:46:14 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +010012#include <linux/delay.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +010019
20
21enum {
22 MSM6242_SECOND1 = 0x0, /* 1-second digit register */
23 MSM6242_SECOND10 = 0x1, /* 10-second digit register */
24 MSM6242_MINUTE1 = 0x2, /* 1-minute digit register */
25 MSM6242_MINUTE10 = 0x3, /* 10-minute digit register */
26 MSM6242_HOUR1 = 0x4, /* 1-hour digit register */
27 MSM6242_HOUR10 = 0x5, /* PM/AM, 10-hour digit register */
28 MSM6242_DAY1 = 0x6, /* 1-day digit register */
29 MSM6242_DAY10 = 0x7, /* 10-day digit register */
30 MSM6242_MONTH1 = 0x8, /* 1-month digit register */
31 MSM6242_MONTH10 = 0x9, /* 10-month digit register */
32 MSM6242_YEAR1 = 0xa, /* 1-year digit register */
33 MSM6242_YEAR10 = 0xb, /* 10-year digit register */
34 MSM6242_WEEK = 0xc, /* Week register */
35 MSM6242_CD = 0xd, /* Control Register D */
36 MSM6242_CE = 0xe, /* Control Register E */
37 MSM6242_CF = 0xf, /* Control Register F */
38};
39
40#define MSM6242_HOUR10_AM (0 << 2)
41#define MSM6242_HOUR10_PM (1 << 2)
42#define MSM6242_HOUR10_HR_MASK (3 << 0)
43
44#define MSM6242_WEEK_SUNDAY 0
45#define MSM6242_WEEK_MONDAY 1
46#define MSM6242_WEEK_TUESDAY 2
47#define MSM6242_WEEK_WEDNESDAY 3
48#define MSM6242_WEEK_THURSDAY 4
49#define MSM6242_WEEK_FRIDAY 5
50#define MSM6242_WEEK_SATURDAY 6
51
52#define MSM6242_CD_30_S_ADJ (1 << 3) /* 30-second adjustment */
53#define MSM6242_CD_IRQ_FLAG (1 << 2)
54#define MSM6242_CD_BUSY (1 << 1)
55#define MSM6242_CD_HOLD (1 << 0)
56
57#define MSM6242_CE_T_MASK (3 << 2)
58#define MSM6242_CE_T_64HZ (0 << 2) /* period 1/64 second */
59#define MSM6242_CE_T_1HZ (1 << 2) /* period 1 second */
60#define MSM6242_CE_T_1MINUTE (2 << 2) /* period 1 minute */
61#define MSM6242_CE_T_1HOUR (3 << 2) /* period 1 hour */
62
63#define MSM6242_CE_ITRPT_STND (1 << 1)
64#define MSM6242_CE_MASK (1 << 0) /* STD.P output control */
65
66#define MSM6242_CF_TEST (1 << 3)
67#define MSM6242_CF_12H (0 << 2)
68#define MSM6242_CF_24H (1 << 2)
69#define MSM6242_CF_STOP (1 << 1)
70#define MSM6242_CF_REST (1 << 0) /* reset */
71
72
73struct msm6242_priv {
74 u32 __iomem *regs;
75 struct rtc_device *rtc;
76};
77
78static inline unsigned int msm6242_read(struct msm6242_priv *priv,
79 unsigned int reg)
80{
81 return __raw_readl(&priv->regs[reg]) & 0xf;
82}
83
84static inline void msm6242_write(struct msm6242_priv *priv, unsigned int val,
85 unsigned int reg)
86{
John Stultzd8ce1482011-02-02 17:53:42 -080087 __raw_writel(val, &priv->regs[reg]);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +010088}
89
90static inline void msm6242_set(struct msm6242_priv *priv, unsigned int val,
91 unsigned int reg)
92{
93 msm6242_write(priv, msm6242_read(priv, reg) | val, reg);
94}
95
96static inline void msm6242_clear(struct msm6242_priv *priv, unsigned int val,
97 unsigned int reg)
98{
99 msm6242_write(priv, msm6242_read(priv, reg) & ~val, reg);
100}
101
102static void msm6242_lock(struct msm6242_priv *priv)
103{
104 int cnt = 5;
105
106 msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD);
107
108 while ((msm6242_read(priv, MSM6242_CD) & MSM6242_CD_BUSY) && cnt) {
109 msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD);
110 udelay(70);
111 msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD);
112 cnt--;
113 }
114
115 if (!cnt)
Joe Perchesa737e832015-04-16 12:46:14 -0700116 pr_warn("timed out waiting for RTC (0x%x)\n",
Sachin Kamat2600f712013-07-03 15:05:58 -0700117 msm6242_read(priv, MSM6242_CD));
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100118}
119
120static void msm6242_unlock(struct msm6242_priv *priv)
121{
122 msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD);
123}
124
125static int msm6242_read_time(struct device *dev, struct rtc_time *tm)
126{
127 struct msm6242_priv *priv = dev_get_drvdata(dev);
128
129 msm6242_lock(priv);
130
131 tm->tm_sec = msm6242_read(priv, MSM6242_SECOND10) * 10 +
132 msm6242_read(priv, MSM6242_SECOND1);
133 tm->tm_min = msm6242_read(priv, MSM6242_MINUTE10) * 10 +
134 msm6242_read(priv, MSM6242_MINUTE1);
Kars de Jonga4e032e2019-11-16 12:05:48 +0100135 tm->tm_hour = (msm6242_read(priv, MSM6242_HOUR10) &
136 MSM6242_HOUR10_HR_MASK) * 10 +
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100137 msm6242_read(priv, MSM6242_HOUR1);
138 tm->tm_mday = msm6242_read(priv, MSM6242_DAY10) * 10 +
139 msm6242_read(priv, MSM6242_DAY1);
140 tm->tm_wday = msm6242_read(priv, MSM6242_WEEK);
141 tm->tm_mon = msm6242_read(priv, MSM6242_MONTH10) * 10 +
142 msm6242_read(priv, MSM6242_MONTH1) - 1;
143 tm->tm_year = msm6242_read(priv, MSM6242_YEAR10) * 10 +
144 msm6242_read(priv, MSM6242_YEAR1);
145 if (tm->tm_year <= 69)
146 tm->tm_year += 100;
147
148 if (!(msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)) {
149 unsigned int pm = msm6242_read(priv, MSM6242_HOUR10) &
150 MSM6242_HOUR10_PM;
151 if (!pm && tm->tm_hour == 12)
152 tm->tm_hour = 0;
153 else if (pm && tm->tm_hour != 12)
154 tm->tm_hour += 12;
155 }
156
157 msm6242_unlock(priv);
158
159 return rtc_valid_tm(tm);
160}
161
162static int msm6242_set_time(struct device *dev, struct rtc_time *tm)
163{
164 struct msm6242_priv *priv = dev_get_drvdata(dev);
165
166 msm6242_lock(priv);
167
168 msm6242_write(priv, tm->tm_sec / 10, MSM6242_SECOND10);
169 msm6242_write(priv, tm->tm_sec % 10, MSM6242_SECOND1);
170 msm6242_write(priv, tm->tm_min / 10, MSM6242_MINUTE10);
171 msm6242_write(priv, tm->tm_min % 10, MSM6242_MINUTE1);
172 if (msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)
173 msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
174 else if (tm->tm_hour >= 12)
175 msm6242_write(priv, MSM6242_HOUR10_PM + (tm->tm_hour - 12) / 10,
176 MSM6242_HOUR10);
177 else
178 msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
179 msm6242_write(priv, tm->tm_hour % 10, MSM6242_HOUR1);
180 msm6242_write(priv, tm->tm_mday / 10, MSM6242_DAY10);
181 msm6242_write(priv, tm->tm_mday % 10, MSM6242_DAY1);
182 if (tm->tm_wday != -1)
183 msm6242_write(priv, tm->tm_wday, MSM6242_WEEK);
184 msm6242_write(priv, (tm->tm_mon + 1) / 10, MSM6242_MONTH10);
185 msm6242_write(priv, (tm->tm_mon + 1) % 10, MSM6242_MONTH1);
186 if (tm->tm_year >= 100)
187 tm->tm_year -= 100;
188 msm6242_write(priv, tm->tm_year / 10, MSM6242_YEAR10);
189 msm6242_write(priv, tm->tm_year % 10, MSM6242_YEAR1);
190
191 msm6242_unlock(priv);
192 return 0;
193}
194
195static const struct rtc_class_ops msm6242_rtc_ops = {
196 .read_time = msm6242_read_time,
197 .set_time = msm6242_set_time,
198};
199
Jingoo Hanc40dcf62013-04-29 16:20:45 -0700200static int __init msm6242_rtc_probe(struct platform_device *pdev)
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100201{
202 struct resource *res;
203 struct msm6242_priv *priv;
204 struct rtc_device *rtc;
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100205
Jingoo Hanc40dcf62013-04-29 16:20:45 -0700206 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100207 if (!res)
208 return -ENODEV;
209
Jingoo Hanc40dcf62013-04-29 16:20:45 -0700210 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100211 if (!priv)
212 return -ENOMEM;
213
Jingoo Hanc40dcf62013-04-29 16:20:45 -0700214 priv->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
215 if (!priv->regs)
216 return -ENOMEM;
217 platform_set_drvdata(pdev, priv);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100218
Jingoo Hanc40dcf62013-04-29 16:20:45 -0700219 rtc = devm_rtc_device_register(&pdev->dev, "rtc-msm6242",
220 &msm6242_rtc_ops, THIS_MODULE);
Jingoo Hane3aa7522013-07-03 15:06:29 -0700221 if (IS_ERR(rtc))
222 return PTR_ERR(rtc);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100223
224 priv->rtc = rtc;
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100225 return 0;
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100226}
227
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100228static struct platform_driver msm6242_rtc_driver = {
229 .driver = {
230 .name = "rtc-msm6242",
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100231 },
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100232};
233
Jingoo Han19cccfb2013-04-29 16:18:43 -0700234module_platform_driver_probe(msm6242_rtc_driver, msm6242_rtc_probe);
Geert Uytterhoeven4f9b9bb2009-03-18 23:29:27 +0100235
236MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
237MODULE_LICENSE("GPL");
238MODULE_DESCRIPTION("Oki MSM6242 RTC driver");
239MODULE_ALIAS("platform:rtc-msm6242");