Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
1
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
03c4cd6
Documentation
LICENSES
arch
block
certs
crypto
drivers
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cdrom
char
clk
clocksource
comedi
connector
counter
cpufreq
cpuidle
crypto
cxl
dax
dca
devfreq
dio
dma-buf
dma
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hte
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
parisc
parport
pci
pcmcia
peci
perf
phy
pinctrl
platform
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
Kconfig
Makefile
class.c
dev.c
interface.c
lib.c
lib_test.c
nvmem.c
proc.c
rtc-88pm80x.c
rtc-88pm860x.c
rtc-ab-b5ze-s3.c
rtc-ab-eoz9.c
rtc-ab8500.c
rtc-abx80x.c
rtc-ac100.c
rtc-armada38x.c
rtc-as3722.c
rtc-asm9260.c
rtc-aspeed.c
rtc-at91rm9200.c
rtc-at91sam9.c
rtc-au1xxx.c
rtc-bd70528.c
rtc-bq32k.c
rtc-bq4802.c
rtc-brcmstb-waketimer.c
rtc-cadence.c
rtc-cmos.c
rtc-core.h
rtc-cpcap.c
rtc-cros-ec.c
rtc-da9052.c
rtc-da9055.c
rtc-da9063.c
rtc-davinci.c
rtc-digicolor.c
rtc-dm355evm.c
rtc-ds1216.c
rtc-ds1286.c
rtc-ds1302.c
rtc-ds1305.c
rtc-ds1307.c
rtc-ds1343.c
rtc-ds1347.c
rtc-ds1374.c
rtc-ds1390.c
rtc-ds1511.c
rtc-ds1553.c
rtc-ds1672.c
rtc-ds1685.c
rtc-ds1742.c
rtc-ds2404.c
rtc-ds3232.c
rtc-efi.c
rtc-em3027.c
rtc-ep93xx.c
rtc-fm3130.c
rtc-fsl-ftm-alarm.c
rtc-ftrtc010.c
rtc-gamecube.c
rtc-generic.c
rtc-goldfish.c
rtc-hid-sensor-time.c
rtc-hym8563.c
rtc-imx-sc.c
rtc-imxdi.c
rtc-isl12022.c
rtc-isl12026.c
rtc-isl1208.c
rtc-jz4740.c
rtc-lp8788.c
rtc-lpc24xx.c
rtc-lpc32xx.c
rtc-ls1x.c
rtc-m41t80.c
rtc-m41t93.c
rtc-m41t94.c
rtc-m48t35.c
rtc-m48t59.c
rtc-m48t86.c
rtc-max6900.c
rtc-max6902.c
rtc-max6916.c
rtc-max77686.c
rtc-max8907.c
rtc-max8925.c
rtc-max8997.c
rtc-max8998.c
rtc-mc13xxx.c
rtc-mc146818-lib.c
rtc-mcp795.c
rtc-meson-vrtc.c
rtc-meson.c
rtc-moxart.c
rtc-mpc5121.c
rtc-mpfs.c
rtc-msc313.c
rtc-msm6242.c
rtc-mt2712.c
rtc-mt6397.c
rtc-mt7622.c
rtc-mv.c
rtc-mxc.c
rtc-mxc_v2.c
rtc-nct3018y.c
rtc-ntxec.c
rtc-omap.c
rtc-opal.c
rtc-optee.c
rtc-palmas.c
rtc-pcap.c
rtc-pcf2123.c
rtc-pcf2127.c
rtc-pcf50633.c
rtc-pcf85063.c
rtc-pcf8523.c
rtc-pcf85363.c
rtc-pcf8563.c
rtc-pcf8583.c
rtc-pic32.c
rtc-pl030.c
rtc-pl031.c
rtc-pm8xxx.c
rtc-ps3.c
rtc-pxa.c
rtc-r7301.c
rtc-r9701.c
rtc-rc5t583.c
rtc-rc5t619.c
rtc-rk808.c
rtc-rp5c01.c
rtc-rs5c313.c
rtc-rs5c348.c
rtc-rs5c372.c
rtc-rtd119x.c
rtc-rv3028.c
rtc-rv3029c2.c
rtc-rv3032.c
rtc-rv8803.c
rtc-rx4581.c
rtc-rx6110.c
rtc-rx8010.c
rtc-rx8025.c
rtc-rx8581.c
rtc-rzn1.c
rtc-s35390a.c
rtc-s3c.c
rtc-s3c.h
rtc-s5m.c
rtc-sa1100.c
rtc-sa1100.h
rtc-sc27xx.c
rtc-sd3078.c
rtc-sh.c
rtc-snvs.c
rtc-spear.c
rtc-st-lpc.c
rtc-starfire.c
rtc-stk17ta8.c
rtc-stm32.c
rtc-stmp3xxx.c
rtc-sun4v.c
rtc-sun6i.c
rtc-sunplus.c
rtc-sunxi.c
rtc-tegra.c
rtc-test.c
rtc-ti-k3.c
rtc-tps6586x.c
rtc-tps65910.c
rtc-twl.c
rtc-v3020.c
rtc-vt8500.c
rtc-wilco-ec.c
rtc-wm831x.c
rtc-wm8350.c
rtc-x1205.c
rtc-xgene.c
rtc-zynqmp.c
sysfs.c
s390
sbus
scsi
sh
siox
slimbus
soc
soundwire
spi
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
ufs
uio
usb
vdpa
vfio
vhost
video
virt
virtio
vlynq
vme
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
rtc
/
rtc-spear.c
Copy path
Blame
Blame
Latest commit
History
History
494 lines (411 loc) · 12.5 KB
Breadcrumbs
linux
/
drivers
/
rtc
/
rtc-spear.c
Top
File metadata and controls
Code
Blame
494 lines (411 loc) · 12.5 KB
Raw
// SPDX-License-Identifier: GPL-2.0-only /* * drivers/rtc/rtc-spear.c * * Copyright (C) 2010 ST Microelectronics * Rajeev Kumar<rajeev-dlh.kumar@st.com> */ #include <linux/bcd.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/io.h> #include <linux/irq.h> #include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> #include <linux/rtc.h> #include <linux/slab.h> #include <linux/spinlock.h> /* RTC registers */ #define TIME_REG 0x00 #define DATE_REG 0x04 #define ALARM_TIME_REG 0x08 #define ALARM_DATE_REG 0x0C #define CTRL_REG 0x10 #define STATUS_REG 0x14 /* TIME_REG & ALARM_TIME_REG */ #define SECONDS_UNITS (0xf<<0) /* seconds units position */ #define SECONDS_TENS (0x7<<4) /* seconds tens position */ #define MINUTES_UNITS (0xf<<8) /* minutes units position */ #define MINUTES_TENS (0x7<<12) /* minutes tens position */ #define HOURS_UNITS (0xf<<16) /* hours units position */ #define HOURS_TENS (0x3<<20) /* hours tens position */ /* DATE_REG & ALARM_DATE_REG */ #define DAYS_UNITS (0xf<<0) /* days units position */ #define DAYS_TENS (0x3<<4) /* days tens position */ #define MONTHS_UNITS (0xf<<8) /* months units position */ #define MONTHS_TENS (0x1<<12) /* months tens position */ #define YEARS_UNITS (0xf<<16) /* years units position */ #define YEARS_TENS (0xf<<20) /* years tens position */ #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */ #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */ /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/ #define SECOND_SHIFT 0x00 /* seconds units */ #define MINUTE_SHIFT 0x08 /* minutes units position */ #define HOUR_SHIFT 0x10 /* hours units position */ #define MDAY_SHIFT 0x00 /* Month day shift */ #define MONTH_SHIFT 0x08 /* Month shift */ #define YEAR_SHIFT 0x10 /* Year shift */ #define SECOND_MASK 0x7F #define MIN_MASK 0x7F #define HOUR_MASK 0x3F #define DAY_MASK 0x3F #define MONTH_MASK 0x7F #define YEAR_MASK 0xFFFF /* date reg equal to time reg, for debug only */ #define TIME_BYP (1<<9) #define INT_ENABLE (1<<31) /* interrupt enable */ /* STATUS_REG */ #define CLK_UNCONNECTED (1<<0) #define PEND_WR_TIME (1<<2) #define PEND_WR_DATE (1<<3) #define LOST_WR_TIME (1<<4) #define LOST_WR_DATE (1<<5) #define RTC_INT_MASK (1<<31) #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE) #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE) struct spear_rtc_config { struct rtc_device *rtc; struct clk *clk; spinlock_t lock; void __iomem *ioaddr; unsigned int irq_wake; }; static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config) { unsigned int val; unsigned long flags; spin_lock_irqsave(&config->lock, flags); val = readl(config->ioaddr + STATUS_REG); val |= RTC_INT_MASK; writel(val, config->ioaddr + STATUS_REG); spin_unlock_irqrestore(&config->lock, flags); } static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config) { unsigned int val; val = readl(config->ioaddr + CTRL_REG); if (!(val & INT_ENABLE)) { spear_rtc_clear_interrupt(config); val |= INT_ENABLE; writel(val, config->ioaddr + CTRL_REG); } } static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config) { unsigned int val; val = readl(config->ioaddr + CTRL_REG); if (val & INT_ENABLE) { val &= ~INT_ENABLE; writel(val, config->ioaddr + CTRL_REG); } } static inline int is_write_complete(struct spear_rtc_config *config) { int ret = 0; unsigned long flags; spin_lock_irqsave(&config->lock, flags); if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL) ret = -EIO; spin_unlock_irqrestore(&config->lock, flags); return ret; } static void rtc_wait_not_busy(struct spear_rtc_config *config) { int status, count = 0; unsigned long flags; /* Assuming BUSY may stay active for 80 msec) */ for (count = 0; count < 80; count++) { spin_lock_irqsave(&config->lock, flags); status = readl(config->ioaddr + STATUS_REG); spin_unlock_irqrestore(&config->lock, flags); if ((status & STATUS_BUSY) == 0) break; /* check status busy, after each msec */ msleep(1); } } static irqreturn_t spear_rtc_irq(int irq, void *dev_id) { struct spear_rtc_config *config = dev_id; unsigned long events = 0; unsigned int irq_data; spin_lock(&config->lock); irq_data = readl(config->ioaddr + STATUS_REG); spin_unlock(&config->lock); if ((irq_data & RTC_INT_MASK)) { spear_rtc_clear_interrupt(config); events = RTC_IRQF | RTC_AF; rtc_update_irq(config->rtc, 1, events); return IRQ_HANDLED; } else return IRQ_NONE; } static void tm2bcd(struct rtc_time *tm) { tm->tm_sec = bin2bcd(tm->tm_sec); tm->tm_min = bin2bcd(tm->tm_min); tm->tm_hour = bin2bcd(tm->tm_hour); tm->tm_mday = bin2bcd(tm->tm_mday); tm->tm_mon = bin2bcd(tm->tm_mon + 1); tm->tm_year = bin2bcd(tm->tm_year); } static void bcd2tm(struct rtc_time *tm) { tm->tm_sec = bcd2bin(tm->tm_sec); tm->tm_min = bcd2bin(tm->tm_min); tm->tm_hour = bcd2bin(tm->tm_hour); tm->tm_mday = bcd2bin(tm->tm_mday); tm->tm_mon = bcd2bin(tm->tm_mon) - 1; /* epoch == 1900 */ tm->tm_year = bcd2bin(tm->tm_year); } /* * spear_rtc_read_time - set the time * @dev: rtc device in use * @tm: holds date and time * * This function read time and date. On success it will return 0 * otherwise -ve error is returned. */ static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm) { struct spear_rtc_config *config = dev_get_drvdata(dev); unsigned int time, date; /* we don't report wday/yday/isdst ... */ rtc_wait_not_busy(config); do { time = readl(config->ioaddr + TIME_REG); date = readl(config->ioaddr + DATE_REG); } while (time == readl(config->ioaddr + TIME_REG)); tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK; tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK; tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK; tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK; tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK; tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK; bcd2tm(tm); return 0; } /* * spear_rtc_set_time - set the time * @dev: rtc device in use * @tm: holds date and time * * This function set time and date. On success it will return 0 * otherwise -ve error is returned. */ static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm) { struct spear_rtc_config *config = dev_get_drvdata(dev); unsigned int time, date; tm2bcd(tm); rtc_wait_not_busy(config); time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) | (tm->tm_hour << HOUR_SHIFT); date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) | (tm->tm_year << YEAR_SHIFT); writel(time, config->ioaddr + TIME_REG); writel(date, config->ioaddr + DATE_REG); return is_write_complete(config); } /* * spear_rtc_read_alarm - read the alarm time * @dev: rtc device in use * @alm: holds alarm date and time * * This function read alarm time and date. On success it will return 0 * otherwise -ve error is returned. */ static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) { struct spear_rtc_config *config = dev_get_drvdata(dev); unsigned int time, date; rtc_wait_not_busy(config); time = readl(config->ioaddr + ALARM_TIME_REG); date = readl(config->ioaddr + ALARM_DATE_REG); alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK; alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK; alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK; alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK; alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK; alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK; bcd2tm(&alm->time); alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE; return 0; } /* * spear_rtc_set_alarm - set the alarm time * @dev: rtc device in use * @alm: holds alarm date and time * * This function set alarm time and date. On success it will return 0 * otherwise -ve error is returned. */ static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) { struct spear_rtc_config *config = dev_get_drvdata(dev); unsigned int time, date; int err; tm2bcd(&alm->time); rtc_wait_not_busy(config); time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min << MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT); date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon << MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT); writel(time, config->ioaddr + ALARM_TIME_REG); writel(date, config->ioaddr + ALARM_DATE_REG); err = is_write_complete(config); if (err < 0) return err; if (alm->enabled) spear_rtc_enable_interrupt(config); else spear_rtc_disable_interrupt(config); return 0; } static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct spear_rtc_config *config = dev_get_drvdata(dev); int ret = 0; spear_rtc_clear_interrupt(config); switch (enabled) { case 0: /* alarm off */ spear_rtc_disable_interrupt(config); break; case 1: /* alarm on */ spear_rtc_enable_interrupt(config); break; default: ret = -EINVAL; break; } return ret; } static const struct rtc_class_ops spear_rtc_ops = { .read_time = spear_rtc_read_time, .set_time = spear_rtc_set_time, .read_alarm = spear_rtc_read_alarm, .set_alarm = spear_rtc_set_alarm, .alarm_irq_enable = spear_alarm_irq_enable, }; static int spear_rtc_probe(struct platform_device *pdev) { struct spear_rtc_config *config; int status = 0; int irq; config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL); if (!config) return -ENOMEM; config->rtc = devm_rtc_allocate_device(&pdev->dev); if (IS_ERR(config->rtc)) return PTR_ERR(config->rtc); /* alarm irqs */ irq = platform_get_irq(pdev, 0); if (irq < 0) return irq; status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name, config); if (status) { dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n", irq); return status; } config->ioaddr = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(config->ioaddr)) return PTR_ERR(config->ioaddr); config->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(config->clk)) return PTR_ERR(config->clk); status = clk_prepare_enable(config->clk); if (status < 0) return status; spin_lock_init(&config->lock); platform_set_drvdata(pdev, config); config->rtc->ops = &spear_rtc_ops; config->rtc->range_min = RTC_TIMESTAMP_BEGIN_0000; config->rtc->range_max = RTC_TIMESTAMP_END_9999; status = devm_rtc_register_device(config->rtc); if (status) goto err_disable_clock; if (!device_can_wakeup(&pdev->dev)) device_init_wakeup(&pdev->dev, 1); return 0; err_disable_clock: clk_disable_unprepare(config->clk); return status; } static int spear_rtc_remove(struct platform_device *pdev) { struct spear_rtc_config *config = platform_get_drvdata(pdev); spear_rtc_disable_interrupt(config); clk_disable_unprepare(config->clk); device_init_wakeup(&pdev->dev, 0); return 0; } #ifdef CONFIG_PM_SLEEP static int spear_rtc_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct spear_rtc_config *config = platform_get_drvdata(pdev); int irq; irq = platform_get_irq(pdev, 0); if (device_may_wakeup(&pdev->dev)) { if (!enable_irq_wake(irq)) config->irq_wake = 1; } else { spear_rtc_disable_interrupt(config); clk_disable(config->clk); } return 0; } static int spear_rtc_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct spear_rtc_config *config = platform_get_drvdata(pdev); int irq; irq = platform_get_irq(pdev, 0); if (device_may_wakeup(&pdev->dev)) { if (config->irq_wake) { disable_irq_wake(irq); config->irq_wake = 0; } } else { clk_enable(config->clk); spear_rtc_enable_interrupt(config); } return 0; } #endif static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume); static void spear_rtc_shutdown(struct platform_device *pdev) { struct spear_rtc_config *config = platform_get_drvdata(pdev); spear_rtc_disable_interrupt(config); clk_disable(config->clk); } #ifdef CONFIG_OF static const struct of_device_id spear_rtc_id_table[] = { { .compatible = "st,spear600-rtc" }, {} }; MODULE_DEVICE_TABLE(of, spear_rtc_id_table); #endif static struct platform_driver spear_rtc_driver = { .probe = spear_rtc_probe, .remove = spear_rtc_remove, .shutdown = spear_rtc_shutdown, .driver = { .name = "rtc-spear", .pm = &spear_rtc_pm_ops, .of_match_table = of_match_ptr(spear_rtc_id_table), }, }; module_platform_driver(spear_rtc_driver); MODULE_ALIAS("platform:rtc-spear"); MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>"); MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)"); MODULE_LICENSE("GPL");
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
You can’t perform that action at this time.