blob: 3dc075cb2b49762ff61a93ab297d2ee9c656564e [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 MediaTek Inc.
4 * Author: Andrew Yang <andrew.yang@mediatek.com>
5 */
6
7#include <linux/delay.h>
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/regmap.h>
11#include <linux/rtc.h>
12#include <linux/irqdomain.h>
13#include <linux/platform_device.h>
14#include <linux/of_address.h>
15#include <linux/of_device.h>
16#include <linux/of_irq.h>
17#include <linux/io.h>
18#include <linux/mfd/mt6330/registers.h>
19#include <linux/mfd/mt6330/core.h>
20#include <linux/nvmem-provider.h>
21
22
23#define RTC_BBPU 0x0000
24#define RTC_BBPU_CBUSY BIT(6)
25
26#define RTC_WRTGR_MT6330 0x3a
27
28#define RTC_IRQ_STA 0x0002
29#define RTC_IRQ_STA_AL BIT(0)
30#define RTC_IRQ_STA_LP BIT(3)
31
32#define RTC_IRQ_EN 0x0004
33#define RTC_IRQ_EN_AL BIT(0)
34#define RTC_IRQ_EN_ONESHOT BIT(2)
35#define RTC_IRQ_EN_LP BIT(3)
36#define RTC_IRQ_EN_ONESHOT_AL (RTC_IRQ_EN_ONESHOT | RTC_IRQ_EN_AL)
37
38#define RTC_TC_SEC_MASK 0x3f
39#define RTC_TC_MIN_MASK 0x3f
40#define RTC_TC_HOU_MASK 0x1f
41#define RTC_TC_DOM_MASK 0x1f
42#define RTC_TC_DOW_MASK 0x7
43#define RTC_TC_MTH_MASK 0xf
44#define RTC_TC_YEA_MASK 0x7f
45
46#define RTC_AL_SEC_MASK 0x003f
47#define RTC_AL_MIN_MASK 0x003f
48#define RTC_AL_HOU_MASK 0x001f
49#define RTC_AL_DOM_MASK 0x001f
50#define RTC_AL_DOW_MASK 0x0007
51#define RTC_AL_MTH_MASK 0x000f
52#define RTC_AL_YEA_MASK 0x007f
53
54#define RTC_AL_MASK 0x0008
55#define RTC_AL_MASK_DOW BIT(4)
56
57#define RTC_TC_SEC 0x000a
58/* Min, Hour, Dom... register offset to RTC_TC_SEC */
59#define RTC_OFFSET_SEC 0
60#define RTC_OFFSET_MIN 1
61#define RTC_OFFSET_HOUR 2
62#define RTC_OFFSET_DOM 3
63#define RTC_OFFSET_DOW 4
64#define RTC_OFFSET_MTH 5
65#define RTC_OFFSET_YEAR 6
66#define RTC_OFFSET_COUNT 7
67
68#define RTC_AL_SEC 0x0018
69#define RTC_AL_HOU_L 0x001c
70#define RTC_AL_HOU_H 0x001d
71#define RTC_AL_MTH_L 0x0022
72#define RTC_AL_MTH_H 0x0023
73
74#define RTC_AL_SEC_MASK 0x003f
75#define RTC_AL_MIN_MASK 0x003f
76#define RTC_AL_HOU_MASK 0x001f
77#define RTC_AL_DOM_MASK 0x001f
78#define RTC_AL_DOW_MASK 0x0007
79#define RTC_AL_MTH_MASK 0x000f
80#define RTC_AL_YEA_MASK 0x007f
81
82#define RTC_PDN2 0x002e
83#define RTC_PDN2_PWRON_ALARM BIT(4)
84
85#define RTC_SPAR0_L 0x0030
86#define RTC_INT_CNT_L 0x0040
87
88#define RTC_MIN_YEAR 1968
89#define RTC_BASE_YEAR 1900
90#define RTC_NUM_YEARS 128
91#define RTC_MIN_YEAR_OFFSET (RTC_MIN_YEAR - RTC_BASE_YEAR)
92
93#define SPARE_REG_WIDTH 1
94
95enum mtk_rtc_spare_enum {
96 SPARE_AL_HOU,
97 SPARE_AL_MTH,
98 SPARE_SPAR0,
99 SPARE_RG_MAX,
100};
101
102enum rtc_eosc_cali_td {
103 EOSC_CALI_TD_01_SEC = 0x3,
104 EOSC_CALI_TD_02_SEC,
105 EOSC_CALI_TD_04_SEC,
106 EOSC_CALI_TD_08_SEC,
107 EOSC_CALI_TD_16_SEC,
108};
109
110enum cali_field_enum {
111 RTC_EOSC32_CK_PDN,
112 EOSC_CALI_TD,
113 CALI_FILED_MAX
114};
115
116enum {
117 RTC_STATE_INIT,
118 RTC_STATE_R,
119 RTC_STATE_W,
120 RTC_STATE_WT,
121};
122
123struct mtk_rtc_compatible {
124 u32 wrtgr_addr;
125 const struct reg_field *spare_reg_fields;
126 const struct reg_field *cali_reg_fields;
127};
128
129struct mt6330_rtc {
130 struct device *dev;
131 struct rtc_device *rtc_dev;
132 struct mutex lock;
133 struct regmap *regmap;
134 int irq;
135 u32 addr_base;
136 const struct mtk_rtc_compatible *dev_comp;
137 struct regmap_field *spare[SPARE_RG_MAX];
138 struct regmap_field *cali[CALI_FILED_MAX];
139 bool cali_is_supported;
140};
141
142static int mtk_rtc_write_trigger(struct mt6330_rtc *rtc);
143
144static const struct reg_field mt6330_cali_reg_fields[CALI_FILED_MAX] = {
145 [RTC_EOSC32_CK_PDN] = REG_FIELD(MT6330_SCK_TOP_CKPDN_CON0_L, 2, 2),
146 [EOSC_CALI_TD] = REG_FIELD(MT6330_RTC_AL_DOW_L, 8, 10),
147};
148
149static const struct reg_field mtk_rtc_spare_reg_fields[SPARE_RG_MAX] = {
150 [SPARE_AL_HOU] = REG_FIELD(RTC_AL_HOU_H, 0, 7),
151 [SPARE_AL_MTH] = REG_FIELD(RTC_AL_MTH_H, 0, 7),
152 [SPARE_SPAR0] = REG_FIELD(RTC_SPAR0_L, 0, 7),
153};
154
155static const struct mtk_rtc_compatible mt6330_rtc_compat = {
156 .wrtgr_addr = RTC_WRTGR_MT6330,
157 .spare_reg_fields = mtk_rtc_spare_reg_fields,
158 .cali_reg_fields = mt6330_cali_reg_fields,
159};
160
161static const struct of_device_id mt6330_rtc_of_match[] = {
162 { .compatible = "mediatek,mt6330-rtc",
163 .data = (void *)&mt6330_rtc_compat, },
164 {}
165};
166MODULE_DEVICE_TABLE(of, mt6330_rtc_of_match);
167
168static int rtc_eosc_cali_td;
169module_param(rtc_eosc_cali_td, int, 0644);
170
171int rtc_state = RTC_STATE_INIT;
172
173static int mtk_rtc_write_lock(struct regmap *map, int lock)
174{
175 int ret;
176 unsigned int tmp, orig = 0;
177
178 ret = regmap_read(map, MT6330_SCK_TOP_CON0_H, &orig);
179 if (ret != 0)
180 return ret;
181
182 tmp = orig & (~0x01);
183 tmp |= lock & 0x01;
184 if (tmp != orig)
185 ret = regmap_write(map, MT6330_SCK_TOP_CON0_H, tmp);
186
187 return ret;
188}
189
190static int rtc_bulk_read(struct mt6330_rtc *rtc, unsigned int reg,
191 void *val, size_t val_count)
192{
193 int ret;
194
195 if ((reg >= rtc->addr_base + RTC_BBPU) && (reg <= rtc->addr_base + RTC_INT_CNT_L)) {
196 if (rtc_state == RTC_STATE_INIT || rtc_state == RTC_STATE_WT) {
197 ret = mtk_rtc_write_lock(rtc->regmap, 1);
198 if (ret != 0)
199 return ret;
200 } else if (rtc_state == RTC_STATE_W) {
201 // Trigger pending write before read
202 mtk_rtc_write_trigger(rtc);
203 ret = mtk_rtc_write_lock(rtc->regmap, 1);
204 if (ret != 0)
205 return ret;
206 }
207 // else, should be already locked, don't need to lock again.
208 rtc_state = RTC_STATE_R;
209 }
210 // else, not access RTC register
211
212 ret = regmap_bulk_read(rtc->regmap, reg, val, val_count);
213
214 return ret;
215}
216
217static int rtc_bulk_write(struct mt6330_rtc *rtc, unsigned int reg,
218 const void *val, size_t val_count)
219{
220 int ret;
221
222 if ((reg >= rtc->addr_base + RTC_BBPU) && (reg <= rtc->addr_base + RTC_INT_CNT_L)) {
223 if (reg == rtc->addr_base + rtc->dev_comp->wrtgr_addr) {
224 if (rtc_state == RTC_STATE_INIT || rtc_state == RTC_STATE_R ||
225 rtc_state == RTC_STATE_WT)
226 return 0; // nothing to write
227 ret = mtk_rtc_write_lock(rtc->regmap, 0);
228 if (ret != 0)
229 return ret;
230 rtc_state = RTC_STATE_WT;
231 } else {
232 if (rtc_state == RTC_STATE_INIT) {
233 // lock writing path first to clear any unexpected writing data
234 ret = mtk_rtc_write_lock(rtc->regmap, 1);
235 if (ret != 0)
236 return ret;
237 ret = mtk_rtc_write_lock(rtc->regmap, 0);
238 if (ret != 0)
239 return ret;
240 } else if (rtc_state == RTC_STATE_R) {
241 ret = mtk_rtc_write_lock(rtc->regmap, 0);
242 if (ret != 0)
243 return ret;
244 }
245 // else, should be already unlocked, don't need to unlock again.
246 rtc_state = RTC_STATE_W;
247 }
248 }
249 // else, not access RTC register
250
rjw4d9a6f22022-05-31 14:23:08 +0800251 ret = regmap_bulk_write(rtc->regmap, reg, val, val_count);
xjb04a4022021-11-25 15:01:52 +0800252
253 if (reg == rtc->addr_base + rtc->dev_comp->wrtgr_addr) {
254 // need 1 ms delay to make sure write completely
255 mdelay(1);
256 }
257
258 return ret;
259}
260
261static int rtc_write(struct mt6330_rtc *rtc, unsigned int reg,
262 unsigned int val)
263{
264 rtc_bulk_write(rtc, reg, &val, 2);
265 return 0;
266}
267
268static int rtc_read(struct mt6330_rtc *rtc, unsigned int reg,
269 unsigned int *val)
270{
271 rtc_bulk_read(rtc, reg, val, 2);
272 return 0;
273}
274
275static int rtc_field_read(struct mt6330_rtc *rtc, const struct reg_field *field, unsigned int *val)
276{
277 int ret;
278 unsigned int reg_val = 0;
279 unsigned int mask;
280 unsigned int shift;
281
282 mask = GENMASK(field->msb, field->lsb);
283 shift = field->lsb;
284
285 ret = rtc_read(rtc, field->reg, &reg_val);
286 if (ret != 0)
287 return ret;
288
289 reg_val &= mask;
290 reg_val >>= shift;
291 *val = reg_val;
292
293 return ret;
294}
295
296static int rtc_update_bits(struct mt6330_rtc *rtc, unsigned int reg,
297 unsigned int mask, unsigned int val)
298{
299 int ret;
300 unsigned int tmp, orig = 0;
301
302 ret = rtc_read(rtc, reg, &orig);
303 if (ret != 0)
304 return ret;
305
306 tmp = orig & ~mask;
307 tmp |= val & mask;
308 if (tmp != orig)
309 ret = rtc_write(rtc, reg, tmp);
310
311 return ret;
312}
313
314static int rtc_field_write(struct mt6330_rtc *rtc, const struct reg_field *field, unsigned int val)
315{
316 unsigned int mask;
317 unsigned int shift;
318
319 mask = GENMASK(field->msb, field->lsb);
320 shift = field->lsb;
321
322 rtc_update_bits(rtc, field->reg, mask, val << shift);
323 return 0;
324}
325
326static int mtk_rtc_write_trigger(struct mt6330_rtc *rtc)
327{
328 unsigned long timeout = jiffies + HZ;
329 int ret;
330 u32 data = 0;
331
332 ret = rtc_write(rtc,
333 rtc->addr_base + rtc->dev_comp->wrtgr_addr, 1);
334 if (ret < 0)
335 return ret;
336
337 while (1) {
338 ret = rtc_read(rtc, rtc->addr_base + RTC_BBPU,
339 &data);
340 if (ret < 0)
341 break;
342 if (!(data & RTC_BBPU_CBUSY))
343 break;
344 if (time_after(jiffies, timeout)) {
345 ret = -ETIMEDOUT;
346 break;
347 }
348 cpu_relax();
349 }
350
351 return ret;
352}
353
354static int rtc_nvram_read(void *priv, unsigned int offset, void *val,
355 size_t bytes)
356{
357 struct mt6330_rtc *rtc = dev_get_drvdata(priv);
358 unsigned int ival;
359 int ret;
360 u8 *buf = val;
361
362 mutex_lock(&rtc->lock);
363
364 for (; bytes; bytes--) {
365 ret = rtc_field_read(rtc, &rtc->dev_comp->spare_reg_fields[offset++], &ival);
366 if (ret)
367 goto out;
368 *buf++ = (u8)ival;
369 }
370out:
371 mutex_unlock(&rtc->lock);
372 return ret;
373}
374
375static int rtc_nvram_write(void *priv, unsigned int offset, void *val,
376 size_t bytes)
377{
378 struct mt6330_rtc *rtc = dev_get_drvdata(priv);
379 unsigned int ival;
380 int ret;
381 u8 *buf = val;
382
383 mutex_lock(&rtc->lock);
384
385 for (; bytes; bytes--) {
386 ival = *buf++;
387 ret = rtc_field_write(rtc, &rtc->dev_comp->spare_reg_fields[offset++], ival);
388 if (ret)
389 goto out;
390 }
391 mtk_rtc_write_trigger(rtc);
392out:
393 mutex_unlock(&rtc->lock);
394 return ret;
395}
396
397static irqreturn_t mtk_rtc_irq_handler_thread(int irq, void *data)
398{
399 struct mt6330_rtc *rtc = data;
400 u32 irqsta = 0, irqen;
401 int ret;
402
403 ret = rtc_read(rtc, rtc->addr_base + RTC_IRQ_STA, &irqsta);
404 if ((ret >= 0) && (irqsta & RTC_IRQ_STA_AL)) {
405 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
406 irqen = irqsta & ~RTC_IRQ_EN_AL;
407 mutex_lock(&rtc->lock);
408 if (rtc_write(rtc, rtc->addr_base + RTC_IRQ_EN,
409 irqen) == 0)
410 mtk_rtc_write_trigger(rtc);
411 mutex_unlock(&rtc->lock);
412
413 return IRQ_HANDLED;
414 }
415
416 return IRQ_NONE;
417}
418
419static int __mtk_rtc_read_time(struct mt6330_rtc *rtc,
420 struct rtc_time *tm, int *sec)
421{
422 int ret;
423 u16 data[RTC_OFFSET_COUNT] = { 0 };
424
425 mutex_lock(&rtc->lock);
426 ret = rtc_bulk_read(rtc, rtc->addr_base + RTC_TC_SEC,
427 data, RTC_OFFSET_COUNT * 2);
428 if (ret < 0)
429 goto exit;
430
431 tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_TC_SEC_MASK;
432 tm->tm_min = data[RTC_OFFSET_MIN] & RTC_TC_MIN_MASK;
433 tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_TC_HOU_MASK;
434 tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_TC_DOM_MASK;
435 tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_TC_MTH_MASK;
436 tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_TC_YEA_MASK;
437
438 ret = rtc_read(rtc, rtc->addr_base + RTC_TC_SEC, sec);
439 *sec &= RTC_TC_SEC_MASK;
440exit:
441 mutex_unlock(&rtc->lock);
442 return ret;
443}
444
445static int mtk_rtc_read_time(struct device *dev, struct rtc_time *tm)
446{
447 time64_t time;
448 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
449 int days, sec, ret;
450
451 do {
452 ret = __mtk_rtc_read_time(rtc, tm, &sec);
453 if (ret < 0)
454 goto exit;
455 } while (sec < tm->tm_sec);
456
457 /* HW register use 7 bits to store year data, minus
458 * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
459 * RTC_MIN_YEAR_OFFSET back after read year from register
460 */
461 tm->tm_year += RTC_MIN_YEAR_OFFSET;
462
463 /* HW register start mon from one, but tm_mon start from zero. */
464 tm->tm_mon--;
465 time = rtc_tm_to_time64(tm);
466
467 /* rtc_tm_to_time64 covert Gregorian date to seconds since
468 * 01-01-1970 00:00:00, and this date is Thursday.
469 */
470 days = div_s64(time, 86400);
471 tm->tm_wday = (days + 4) % 7;
472
473exit:
474 return ret;
475}
476
477static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm)
478{
479 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
480 int ret;
481 u16 data[RTC_OFFSET_COUNT];
482
483 if (tm->tm_year > 195) {
484 dev_err(rtc->dev, "%s: invalid year %04d > 2095\n",
485 __func__, tm->tm_year + RTC_BASE_YEAR);
486 return -EINVAL;
487 }
488
489 tm->tm_year -= RTC_MIN_YEAR_OFFSET;
490 tm->tm_mon++;
491
492 data[RTC_OFFSET_SEC] = tm->tm_sec;
493 data[RTC_OFFSET_MIN] = tm->tm_min;
494 data[RTC_OFFSET_HOUR] = tm->tm_hour;
495 data[RTC_OFFSET_DOM] = tm->tm_mday;
496 data[RTC_OFFSET_MTH] = tm->tm_mon;
497 data[RTC_OFFSET_YEAR] = tm->tm_year;
498
499 mutex_lock(&rtc->lock);
500 ret = rtc_bulk_write(rtc, rtc->addr_base + RTC_TC_SEC,
501 data, RTC_OFFSET_COUNT * 2);
502 if (ret < 0)
503 goto exit;
504
505 /* Time register write to hardware after call trigger function */
506 ret = mtk_rtc_write_trigger(rtc);
507
508exit:
509 mutex_unlock(&rtc->lock);
510 return ret;
511}
512
513static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
514{
515 struct rtc_time *tm = &alm->time;
516 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
517 u32 irqen = 0, pdn2 = 0;
518 int ret;
519 u16 data[RTC_OFFSET_COUNT] = { 0 };
520
521 mutex_lock(&rtc->lock);
522 ret = rtc_read(rtc, rtc->addr_base + RTC_IRQ_EN, &irqen);
523 if (ret < 0)
524 goto err_exit;
525 ret = rtc_read(rtc, rtc->addr_base + RTC_PDN2, &pdn2);
526 if (ret < 0)
527 goto err_exit;
528
529 ret = rtc_bulk_read(rtc, rtc->addr_base + RTC_AL_SEC,
530 data, RTC_OFFSET_COUNT * 2);
531 if (ret < 0)
532 goto err_exit;
533
534 alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
535 alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
536 mutex_unlock(&rtc->lock);
537
538 tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
539 tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
540 tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
541 tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
542 tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
543 tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
544
545 tm->tm_year += RTC_MIN_YEAR_OFFSET;
546 tm->tm_mon--;
547
548 return 0;
549err_exit:
550 mutex_unlock(&rtc->lock);
551 return ret;
552}
553
554static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
555{
556 struct rtc_time *tm = &alm->time;
557 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
558 int ret;
559 u16 data[RTC_OFFSET_COUNT] = { 0 };
560
561 if (tm->tm_year > 195) {
562 dev_err(rtc->dev, "%s: invalid year %04d > 2095\n",
563 __func__, tm->tm_year + RTC_BASE_YEAR);
564 return -EINVAL;
565 }
566
567 tm->tm_year -= RTC_MIN_YEAR_OFFSET;
568 tm->tm_mon++;
569
570 mutex_lock(&rtc->lock);
571 ret = rtc_bulk_read(rtc, rtc->addr_base + RTC_AL_SEC,
572 data, RTC_OFFSET_COUNT * 2);
573 if (ret < 0)
574 goto exit;
575
576 data[RTC_OFFSET_SEC] = ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
577 (tm->tm_sec & RTC_AL_SEC_MASK));
578 data[RTC_OFFSET_MIN] = ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
579 (tm->tm_min & RTC_AL_MIN_MASK));
580 data[RTC_OFFSET_HOUR] = ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
581 (tm->tm_hour & RTC_AL_HOU_MASK));
582 data[RTC_OFFSET_DOM] = ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
583 (tm->tm_mday & RTC_AL_DOM_MASK));
584 data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
585 (tm->tm_mon & RTC_AL_MTH_MASK));
586 data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
587 (tm->tm_year & RTC_AL_YEA_MASK));
588
589 if (alm->enabled) {
590 ret = rtc_bulk_read(rtc, rtc->addr_base + RTC_AL_SEC,
591 data, RTC_OFFSET_COUNT * 2);
592 if (ret < 0)
593 goto exit;
594 data[RTC_OFFSET_SEC] =
595 ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
596 (tm->tm_sec & RTC_AL_SEC_MASK));
597 data[RTC_OFFSET_MIN] =
598 ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
599 (tm->tm_min & RTC_AL_MIN_MASK));
600 data[RTC_OFFSET_HOUR] =
601 ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
602 (tm->tm_hour & RTC_AL_HOU_MASK));
603 data[RTC_OFFSET_DOM] =
604 ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
605 (tm->tm_mday & RTC_AL_DOM_MASK));
606 data[RTC_OFFSET_MTH] =
607 ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
608 (tm->tm_mon & RTC_AL_MTH_MASK));
609 data[RTC_OFFSET_YEAR] =
610 ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
611 (tm->tm_year & RTC_AL_YEA_MASK));
612 ret = rtc_bulk_write(rtc,
613 rtc->addr_base + RTC_AL_SEC,
614 data, RTC_OFFSET_COUNT * 2);
615 if (ret < 0)
616 goto exit;
617 ret = rtc_write(rtc, rtc->addr_base + RTC_AL_MASK,
618 RTC_AL_MASK_DOW);
619 if (ret < 0)
620 goto exit;
621 ret = rtc_update_bits(rtc,
622 rtc->addr_base + RTC_IRQ_EN,
623 RTC_IRQ_EN_ONESHOT_AL,
624 RTC_IRQ_EN_ONESHOT_AL);
625 if (ret < 0)
626 goto exit;
627 } else {
628 ret = rtc_update_bits(rtc,
629 rtc->addr_base + RTC_IRQ_EN,
630 RTC_IRQ_EN_ONESHOT_AL, 0);
631 if (ret < 0)
632 goto exit;
633 }
634
635 /* All alarm time register write to hardware after calling
636 * mtk_rtc_write_trigger. This can avoid race condition if alarm
637 * occur happen during writing alarm time register.
638 */
639 ret = mtk_rtc_write_trigger(rtc);
640exit:
641 mutex_unlock(&rtc->lock);
642 return ret;
643}
644
645static const struct rtc_class_ops mtk_rtc_ops = {
xjb04a4022021-11-25 15:01:52 +0800646 .read_time = mtk_rtc_read_time,
647 .set_time = mtk_rtc_set_time,
648 .read_alarm = mtk_rtc_read_alarm,
649 .set_alarm = mtk_rtc_set_alarm,
650};
651
652static void mtk_rtc_enable_k_eosc(struct device *dev)
653{
654 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
655 u32 td;
656
657 if (!rtc->cali_is_supported)
658 return;
659
660 /* Truning on eosc cali mode clock */
661 rtc_field_write(rtc, &rtc->dev_comp->cali_reg_fields[RTC_EOSC32_CK_PDN], 0);
662
663 if (rtc_eosc_cali_td) {
664 dev_notice(dev, "%s: rtc_eosc_cali_td = %d\n",
665 __func__, rtc_eosc_cali_td);
666 switch (rtc_eosc_cali_td) {
667 case 1:
668 td = EOSC_CALI_TD_01_SEC;
669 break;
670 case 2:
671 td = EOSC_CALI_TD_02_SEC;
672 break;
673 case 4:
674 td = EOSC_CALI_TD_04_SEC;
675 break;
676 case 16:
677 td = EOSC_CALI_TD_16_SEC;
678 break;
679 default:
680 td = EOSC_CALI_TD_08_SEC;
681 break;
682 }
683 rtc_field_write(rtc, &rtc->dev_comp->cali_reg_fields[EOSC_CALI_TD], td);
684 }
685}
686
687static void mtk_rtc_shutdown(struct platform_device *pdev)
688{
689 mtk_rtc_enable_k_eosc(&pdev->dev);
690}
691
692static int mtk_rtc_config_eosc_cali(struct device *dev)
693{
694 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
695 int i;
696
697 for (i = 0; i < CALI_FILED_MAX; i++) {
698 rtc->cali[i] = devm_regmap_field_alloc(dev, rtc->regmap,
699 rtc->dev_comp->cali_reg_fields[i]);
700 if (IS_ERR(rtc->cali[i])) {
701 dev_err(rtc->dev, "cali regmap field[%d] err= %ld\n",
702 i, PTR_ERR(rtc->cali[i]));
703 return PTR_ERR(rtc->cali[i]);
704 }
705 }
706 rtc->cali_is_supported = true;
707
708 return 0;
709}
710
711static int mtk_rtc_set_spare(struct device *dev)
712{
713 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
714 struct reg_field tmp[SPARE_RG_MAX];
715 int i, ret;
716 struct nvmem_config nvmem_cfg = {
717 .name = "mtk_rtc_nvmem",
718 .word_size = SPARE_REG_WIDTH,
719 .stride = 1,
720 .size = SPARE_RG_MAX * SPARE_REG_WIDTH,
721 .reg_read = rtc_nvram_read,
722 .reg_write = rtc_nvram_write,
723 .priv = dev,
724 };
725
726 memcpy(tmp, rtc->dev_comp->spare_reg_fields, sizeof(tmp));
727
728 for (i = 0; i < SPARE_RG_MAX; i++) {
729 tmp[i].reg += rtc->addr_base;
730 rtc->spare[i] = devm_regmap_field_alloc(rtc->dev,
731 rtc->regmap,
732 tmp[i]);
733 if (IS_ERR(rtc->spare[i])) {
734 dev_err(rtc->dev, "spare regmap field[%d] err= %ld\n",
735 i, PTR_ERR(rtc->spare[i]));
736 return PTR_ERR(rtc->spare[i]);
737 }
738 }
739
740 ret = rtc_nvmem_register(rtc->rtc_dev, &nvmem_cfg);
741 if (ret)
742 dev_err(rtc->dev, "nvmem register failed\n");
743
744 return ret;
745}
746
747static int mtk_rtc_probe(struct platform_device *pdev)
748{
749 struct resource *res;
750 struct mt6330_chip *mt6330_chip = dev_get_drvdata(pdev->dev.parent);
751 struct mt6330_rtc *rtc;
752 const struct of_device_id *of_id;
753 int ret;
754
755 rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6330_rtc), GFP_KERNEL);
756 if (!rtc)
757 return -ENOMEM;
758
759 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
760 rtc->addr_base = res->start;
761
762 of_id = of_match_device(mt6330_rtc_of_match, &pdev->dev);
763 if (!of_id) {
764 dev_err(&pdev->dev, "Failed to probe of_node\n");
765 return -EINVAL;
766 }
767 rtc->dev_comp = of_id->data;
768
769 rtc->irq = platform_get_irq(pdev, 0);
770 if (rtc->irq < 0)
771 return rtc->irq;
772
773 rtc->regmap = mt6330_chip->regmap;
774 rtc->dev = &pdev->dev;
775 mutex_init(&rtc->lock);
776
777 platform_set_drvdata(pdev, rtc);
778
779 rtc->rtc_dev = devm_rtc_allocate_device(rtc->dev);
780 if (IS_ERR(rtc->rtc_dev))
781 return PTR_ERR(rtc->rtc_dev);
782
783 ret = request_threaded_irq(rtc->irq, NULL,
784 mtk_rtc_irq_handler_thread,
785 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
786 "mt6330-rtc", rtc);
787 if (ret) {
788 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
789 rtc->irq, ret);
790 goto out_dispose_irq;
791 }
792
793 device_init_wakeup(&pdev->dev, 1);
794
795 rtc->rtc_dev->ops = &mtk_rtc_ops;
796
797 ret = rtc_register_device(rtc->rtc_dev);
798 if (ret) {
799 dev_err(&pdev->dev, "register rtc device failed\n");
800 goto out_free_irq;
801 }
802
803 if (rtc->dev_comp->spare_reg_fields)
804 if (mtk_rtc_set_spare(&pdev->dev))
805 dev_err(&pdev->dev, "spare is not supported\n");
806
807 if (rtc->dev_comp->cali_reg_fields)
808 if (mtk_rtc_config_eosc_cali(&pdev->dev))
809 dev_err(&pdev->dev, "config eosc cali failed\n");
810
811 return 0;
812
813out_free_irq:
814 free_irq(rtc->irq, rtc->rtc_dev);
815out_dispose_irq:
816 irq_dispose_mapping(rtc->irq);
817 return ret;
818}
819
820static int mtk_rtc_remove(struct platform_device *pdev)
821{
822 struct mt6330_rtc *rtc = platform_get_drvdata(pdev);
823
824 free_irq(rtc->irq, rtc->rtc_dev);
825 irq_dispose_mapping(rtc->irq);
826
827 return 0;
828}
829
830#ifdef CONFIG_PM_SLEEP
831static int mt6330_rtc_suspend(struct device *dev)
832{
833 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
834
835 if (device_may_wakeup(dev))
836 enable_irq_wake(rtc->irq);
837
838 return 0;
839}
840
841static int mt6330_rtc_resume(struct device *dev)
842{
843 struct mt6330_rtc *rtc = dev_get_drvdata(dev);
844
845 if (device_may_wakeup(dev))
846 disable_irq_wake(rtc->irq);
847
848 return 0;
849}
850#endif
851
852static SIMPLE_DEV_PM_OPS(mt6330_pm_ops, mt6330_rtc_suspend,
853 mt6330_rtc_resume);
854
855static struct platform_driver mtk_rtc_driver = {
856 .driver = {
857 .name = "mt6330-rtc",
858 .of_match_table = mt6330_rtc_of_match,
859 .pm = &mt6330_pm_ops,
860 },
861 .probe = mtk_rtc_probe,
862 .remove = mtk_rtc_remove,
863 .shutdown = mtk_rtc_shutdown,
864};
865
866module_platform_driver(mtk_rtc_driver);
867
868MODULE_LICENSE("GPL v2");
869MODULE_AUTHOR("Andrew Yang <andrew.yang@mediatek.com>");
870MODULE_DESCRIPTION("RTC Driver for MediaTek MT6330 PMIC");