David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * A driver for the RTC embedded in the Cirrus Logic EP93XX processors |
| 4 | * Copyright (c) 2006 Tower Technologies |
| 5 | * |
| 6 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/rtc.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/gfp.h> |
| 14 | |
| 15 | #define EP93XX_RTC_DATA 0x000 |
| 16 | #define EP93XX_RTC_MATCH 0x004 |
| 17 | #define EP93XX_RTC_STATUS 0x008 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 18 | #define EP93XX_RTC_STATUS_INTR BIT(0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | #define EP93XX_RTC_LOAD 0x00C |
| 20 | #define EP93XX_RTC_CONTROL 0x010 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 21 | #define EP93XX_RTC_CONTROL_MIE BIT(0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #define EP93XX_RTC_SWCOMP 0x108 |
| 23 | #define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000 |
| 24 | #define EP93XX_RTC_SWCOMP_DEL_SHIFT 16 |
| 25 | #define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff |
| 26 | #define EP93XX_RTC_SWCOMP_INT_SHIFT 0 |
| 27 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | struct ep93xx_rtc { |
| 29 | void __iomem *mmio_base; |
| 30 | struct rtc_device *rtc; |
| 31 | }; |
| 32 | |
| 33 | static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 34 | unsigned short *delete) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | { |
| 36 | struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev); |
| 37 | unsigned long comp; |
| 38 | |
| 39 | comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP); |
| 40 | |
| 41 | if (preload) |
| 42 | *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK) |
| 43 | >> EP93XX_RTC_SWCOMP_INT_SHIFT; |
| 44 | |
| 45 | if (delete) |
| 46 | *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK) |
| 47 | >> EP93XX_RTC_SWCOMP_DEL_SHIFT; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 53 | { |
| 54 | struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev); |
| 55 | unsigned long time; |
| 56 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 57 | time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 59 | rtc_time64_to_tm(time, tm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 63 | static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | { |
| 65 | struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 66 | unsigned long secs = rtc_tm_to_time64(tm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | |
| 68 | writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq) |
| 73 | { |
| 74 | unsigned short preload, delete; |
| 75 | |
| 76 | ep93xx_rtc_get_swcomp(dev, &preload, &delete); |
| 77 | |
| 78 | seq_printf(seq, "preload\t\t: %d\n", preload); |
| 79 | seq_printf(seq, "delete\t\t: %d\n", delete); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static const struct rtc_class_ops ep93xx_rtc_ops = { |
| 85 | .read_time = ep93xx_rtc_read_time, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 86 | .set_time = ep93xx_rtc_set_time, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 87 | .proc = ep93xx_rtc_proc, |
| 88 | }; |
| 89 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 90 | static ssize_t comp_preload_show(struct device *dev, |
| 91 | struct device_attribute *attr, char *buf) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 92 | { |
| 93 | unsigned short preload; |
| 94 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 95 | ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | |
| 97 | return sprintf(buf, "%d\n", preload); |
| 98 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 99 | static DEVICE_ATTR_RO(comp_preload); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 101 | static ssize_t comp_delete_show(struct device *dev, |
| 102 | struct device_attribute *attr, char *buf) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 103 | { |
| 104 | unsigned short delete; |
| 105 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 106 | ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 107 | |
| 108 | return sprintf(buf, "%d\n", delete); |
| 109 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 110 | static DEVICE_ATTR_RO(comp_delete); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 111 | |
| 112 | static struct attribute *ep93xx_rtc_attrs[] = { |
| 113 | &dev_attr_comp_preload.attr, |
| 114 | &dev_attr_comp_delete.attr, |
| 115 | NULL |
| 116 | }; |
| 117 | |
| 118 | static const struct attribute_group ep93xx_rtc_sysfs_files = { |
| 119 | .attrs = ep93xx_rtc_attrs, |
| 120 | }; |
| 121 | |
| 122 | static int ep93xx_rtc_probe(struct platform_device *pdev) |
| 123 | { |
| 124 | struct ep93xx_rtc *ep93xx_rtc; |
| 125 | struct resource *res; |
| 126 | int err; |
| 127 | |
| 128 | ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL); |
| 129 | if (!ep93xx_rtc) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 133 | ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 134 | if (IS_ERR(ep93xx_rtc->mmio_base)) |
| 135 | return PTR_ERR(ep93xx_rtc->mmio_base); |
| 136 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | platform_set_drvdata(pdev, ep93xx_rtc); |
| 138 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 139 | ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev); |
| 140 | if (IS_ERR(ep93xx_rtc->rtc)) |
| 141 | return PTR_ERR(ep93xx_rtc->rtc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 143 | ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops; |
| 144 | ep93xx_rtc->rtc->range_max = U32_MAX; |
| 145 | |
| 146 | err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 147 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 148 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 149 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 150 | return rtc_register_device(ep93xx_rtc->rtc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static struct platform_driver ep93xx_rtc_driver = { |
| 154 | .driver = { |
| 155 | .name = "ep93xx-rtc", |
| 156 | }, |
| 157 | .probe = ep93xx_rtc_probe, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | module_platform_driver(ep93xx_rtc_driver); |
| 161 | |
| 162 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 163 | MODULE_DESCRIPTION("EP93XX RTC driver"); |
| 164 | MODULE_LICENSE("GPL"); |
| 165 | MODULE_ALIAS("platform:ep93xx-rtc"); |