Alexandre Belloni | 4fdf4d2 | 2019-04-19 10:00:04 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [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> |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/rtc.h> |
| 11 | #include <linux/platform_device.h> |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 12 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 14 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 15 | #define EP93XX_RTC_DATA 0x000 |
| 16 | #define EP93XX_RTC_MATCH 0x004 |
| 17 | #define EP93XX_RTC_STATUS 0x008 |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 18 | #define EP93XX_RTC_STATUS_INTR BIT(0) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 19 | #define EP93XX_RTC_LOAD 0x00C |
| 20 | #define EP93XX_RTC_CONTROL 0x010 |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 21 | #define EP93XX_RTC_CONTROL_MIE BIT(0) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [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 |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 27 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 28 | struct ep93xx_rtc { |
| 29 | void __iomem *mmio_base; |
Axel Lin | bf6ed02 | 2011-08-10 21:11:26 +0800 | [diff] [blame] | 30 | struct rtc_device *rtc; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload, |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 34 | unsigned short *delete) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 35 | { |
Nikita Shubin | 00c3348 | 2020-12-01 12:55:07 +0300 | [diff] [blame] | 36 | struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 37 | unsigned long comp; |
| 38 | |
H Hartley Sweeten | ff32ff1 | 2012-03-21 10:55:31 -0700 | [diff] [blame] | 39 | comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 40 | |
| 41 | if (preload) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 42 | *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK) |
| 43 | >> EP93XX_RTC_SWCOMP_INT_SHIFT; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 44 | |
| 45 | if (delete) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 46 | *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK) |
| 47 | >> EP93XX_RTC_SWCOMP_DEL_SHIFT; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 53 | { |
Nikita Shubin | 00c3348 | 2020-12-01 12:55:07 +0300 | [diff] [blame] | 54 | struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 55 | unsigned long time; |
| 56 | |
Colin Ian King | 725412d9 | 2018-11-01 14:53:52 +0000 | [diff] [blame] | 57 | time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 58 | |
Alexandre Belloni | 886a77e | 2019-04-19 10:00:02 +0200 | [diff] [blame] | 59 | rtc_time64_to_tm(time, tm); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Alexandre Belloni | ef9440a | 2019-04-19 10:00:03 +0200 | [diff] [blame] | 63 | static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 64 | { |
Nikita Shubin | 00c3348 | 2020-12-01 12:55:07 +0300 | [diff] [blame] | 65 | struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev); |
Alexandre Belloni | ef9440a | 2019-04-19 10:00:03 +0200 | [diff] [blame] | 66 | unsigned long secs = rtc_tm_to_time64(tm); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 67 | |
H Hartley Sweeten | ff32ff1 | 2012-03-21 10:55:31 -0700 | [diff] [blame] | 68 | writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 72 | static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq) |
| 73 | { |
| 74 | unsigned short preload, delete; |
| 75 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 76 | ep93xx_rtc_get_swcomp(dev, &preload, &delete); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 77 | |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 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 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 84 | static const struct rtc_class_ops ep93xx_rtc_ops = { |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 85 | .read_time = ep93xx_rtc_read_time, |
Alexandre Belloni | ef9440a | 2019-04-19 10:00:03 +0200 | [diff] [blame] | 86 | .set_time = ep93xx_rtc_set_time, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 87 | .proc = ep93xx_rtc_proc, |
| 88 | }; |
| 89 | |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 90 | static ssize_t comp_preload_show(struct device *dev, |
| 91 | struct device_attribute *attr, char *buf) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 92 | { |
| 93 | unsigned short preload; |
| 94 | |
Alexandre Belloni | 09cd030 | 2019-04-19 10:00:00 +0200 | [diff] [blame] | 95 | ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 96 | |
| 97 | return sprintf(buf, "%d\n", preload); |
| 98 | } |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 99 | static DEVICE_ATTR_RO(comp_preload); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 100 | |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 101 | static ssize_t comp_delete_show(struct device *dev, |
| 102 | struct device_attribute *attr, char *buf) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 103 | { |
| 104 | unsigned short delete; |
| 105 | |
Alexandre Belloni | 09cd030 | 2019-04-19 10:00:00 +0200 | [diff] [blame] | 106 | ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 107 | |
| 108 | return sprintf(buf, "%d\n", delete); |
| 109 | } |
Alexandre Belloni | d71c771 | 2019-04-19 10:00:05 +0200 | [diff] [blame] | 110 | static DEVICE_ATTR_RO(comp_delete); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 111 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame] | 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 | }; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 121 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 122 | static int ep93xx_rtc_probe(struct platform_device *pdev) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 123 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 124 | struct ep93xx_rtc *ep93xx_rtc; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 125 | int err; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 126 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame] | 127 | ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL); |
| 128 | if (!ep93xx_rtc) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 129 | return -ENOMEM; |
| 130 | |
YueHaibing | 09ef18b | 2019-10-06 18:29:20 +0800 | [diff] [blame] | 131 | ep93xx_rtc->mmio_base = devm_platform_ioremap_resource(pdev, 0); |
Julia Lawall | 7c1d69e | 2013-09-11 14:24:27 -0700 | [diff] [blame] | 132 | if (IS_ERR(ep93xx_rtc->mmio_base)) |
| 133 | return PTR_ERR(ep93xx_rtc->mmio_base); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 134 | |
Axel Lin | bf6ed02 | 2011-08-10 21:11:26 +0800 | [diff] [blame] | 135 | platform_set_drvdata(pdev, ep93xx_rtc); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 136 | |
Alexandre Belloni | bac68b3 | 2019-04-19 09:59:59 +0200 | [diff] [blame] | 137 | ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev); |
Alexandre Belloni | b809d19 | 2019-04-19 09:59:58 +0200 | [diff] [blame] | 138 | if (IS_ERR(ep93xx_rtc->rtc)) |
| 139 | return PTR_ERR(ep93xx_rtc->rtc); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 140 | |
Alexandre Belloni | bac68b3 | 2019-04-19 09:59:59 +0200 | [diff] [blame] | 141 | ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops; |
Alexandre Belloni | 2d4fc6d | 2019-04-19 10:00:01 +0200 | [diff] [blame] | 142 | ep93xx_rtc->rtc->range_max = U32_MAX; |
Alexandre Belloni | bac68b3 | 2019-04-19 09:59:59 +0200 | [diff] [blame] | 143 | |
Alexandre Belloni | 09cd030 | 2019-04-19 10:00:00 +0200 | [diff] [blame] | 144 | err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files); |
Alexandre Belloni | bac68b3 | 2019-04-19 09:59:59 +0200 | [diff] [blame] | 145 | if (err) |
| 146 | return err; |
| 147 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 148 | return devm_rtc_register_device(ep93xx_rtc->rtc); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 151 | static struct platform_driver ep93xx_rtc_driver = { |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 152 | .driver = { |
| 153 | .name = "ep93xx-rtc", |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 154 | }, |
H Hartley Sweeten | 84d56b3 | 2012-05-29 15:07:36 -0700 | [diff] [blame] | 155 | .probe = ep93xx_rtc_probe, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
H Hartley Sweeten | 84d56b3 | 2012-05-29 15:07:36 -0700 | [diff] [blame] | 158 | module_platform_driver(ep93xx_rtc_driver); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 159 | |
| 160 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 161 | MODULE_DESCRIPTION("EP93XX RTC driver"); |
| 162 | MODULE_LICENSE("GPL"); |
H Hartley Sweeten | 84d56b3 | 2012-05-29 15:07:36 -0700 | [diff] [blame] | 163 | MODULE_ALIAS("platform:ep93xx-rtc"); |