Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dallas DS1216 RTC driver |
| 3 | * |
| 4 | * Copyright (c) 2007 Thomas Bogendoerfer |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/rtc.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/bcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 13 | |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 14 | struct ds1216_regs { |
| 15 | u8 tsec; |
| 16 | u8 sec; |
| 17 | u8 min; |
| 18 | u8 hour; |
| 19 | u8 wday; |
| 20 | u8 mday; |
| 21 | u8 month; |
| 22 | u8 year; |
| 23 | }; |
| 24 | |
| 25 | #define DS1216_HOUR_1224 (1 << 7) |
| 26 | #define DS1216_HOUR_AMPM (1 << 5) |
| 27 | |
| 28 | struct ds1216_priv { |
| 29 | struct rtc_device *rtc; |
| 30 | void __iomem *ioaddr; |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static const u8 magic[] = { |
| 34 | 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Read the 64 bit we'd like to have - It a series |
| 39 | * of 64 bits showing up in the LSB of the base register. |
| 40 | * |
| 41 | */ |
| 42 | static void ds1216_read(u8 __iomem *ioaddr, u8 *buf) |
| 43 | { |
| 44 | unsigned char c; |
| 45 | int i, j; |
| 46 | |
| 47 | for (i = 0; i < 8; i++) { |
| 48 | c = 0; |
| 49 | for (j = 0; j < 8; j++) |
| 50 | c |= (readb(ioaddr) & 0x1) << j; |
| 51 | buf[i] = c; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf) |
| 56 | { |
| 57 | unsigned char c; |
| 58 | int i, j; |
| 59 | |
| 60 | for (i = 0; i < 8; i++) { |
| 61 | c = buf[i]; |
| 62 | for (j = 0; j < 8; j++) { |
| 63 | writeb(c, ioaddr); |
| 64 | c = c >> 1; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr) |
| 70 | { |
| 71 | /* Reset magic pointer */ |
| 72 | readb(ioaddr); |
| 73 | /* Write 64 bit magic to DS1216 */ |
| 74 | ds1216_write(ioaddr, magic); |
| 75 | } |
| 76 | |
| 77 | static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 78 | { |
| 79 | struct platform_device *pdev = to_platform_device(dev); |
| 80 | struct ds1216_priv *priv = platform_get_drvdata(pdev); |
| 81 | struct ds1216_regs regs; |
| 82 | |
| 83 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 84 | ds1216_read(priv->ioaddr, (u8 *)®s); |
| 85 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 86 | tm->tm_sec = bcd2bin(regs.sec); |
| 87 | tm->tm_min = bcd2bin(regs.min); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 88 | if (regs.hour & DS1216_HOUR_1224) { |
| 89 | /* AM/PM mode */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 90 | tm->tm_hour = bcd2bin(regs.hour & 0x1f); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 91 | if (regs.hour & DS1216_HOUR_AMPM) |
| 92 | tm->tm_hour += 12; |
| 93 | } else |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 94 | tm->tm_hour = bcd2bin(regs.hour & 0x3f); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 95 | tm->tm_wday = (regs.wday & 7) - 1; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 96 | tm->tm_mday = bcd2bin(regs.mday & 0x3f); |
| 97 | tm->tm_mon = bcd2bin(regs.month & 0x1f); |
| 98 | tm->tm_year = bcd2bin(regs.year); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 99 | if (tm->tm_year < 70) |
| 100 | tm->tm_year += 100; |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 101 | |
| 102 | return rtc_valid_tm(tm); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 106 | { |
| 107 | struct platform_device *pdev = to_platform_device(dev); |
| 108 | struct ds1216_priv *priv = platform_get_drvdata(pdev); |
| 109 | struct ds1216_regs regs; |
| 110 | |
| 111 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 112 | ds1216_read(priv->ioaddr, (u8 *)®s); |
| 113 | |
| 114 | regs.tsec = 0; /* clear 0.1 and 0.01 seconds */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 115 | regs.sec = bin2bcd(tm->tm_sec); |
| 116 | regs.min = bin2bcd(tm->tm_min); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 117 | regs.hour &= DS1216_HOUR_1224; |
| 118 | if (regs.hour && tm->tm_hour > 12) { |
| 119 | regs.hour |= DS1216_HOUR_AMPM; |
| 120 | tm->tm_hour -= 12; |
| 121 | } |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 122 | regs.hour |= bin2bcd(tm->tm_hour); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 123 | regs.wday &= ~7; |
| 124 | regs.wday |= tm->tm_wday; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 125 | regs.mday = bin2bcd(tm->tm_mday); |
| 126 | regs.month = bin2bcd(tm->tm_mon); |
| 127 | regs.year = bin2bcd(tm->tm_year % 100); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 128 | |
| 129 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 130 | ds1216_write(priv->ioaddr, (u8 *)®s); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static const struct rtc_class_ops ds1216_rtc_ops = { |
| 135 | .read_time = ds1216_rtc_read_time, |
| 136 | .set_time = ds1216_rtc_set_time, |
| 137 | }; |
| 138 | |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 139 | static int __init ds1216_rtc_probe(struct platform_device *pdev) |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 140 | { |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 141 | struct resource *res; |
| 142 | struct ds1216_priv *priv; |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 143 | u8 dummy[8]; |
| 144 | |
Jingoo Han | 15c1567 | 2013-04-29 16:20:37 -0700 | [diff] [blame] | 145 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 146 | if (!priv) |
| 147 | return -ENOMEM; |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 148 | |
| 149 | platform_set_drvdata(pdev, priv); |
| 150 | |
Julia Lawall | e044253 | 2013-08-14 11:11:21 +0200 | [diff] [blame] | 151 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jingoo Han | 15c1567 | 2013-04-29 16:20:37 -0700 | [diff] [blame] | 152 | priv->ioaddr = devm_ioremap_resource(&pdev->dev, res); |
| 153 | if (IS_ERR(priv->ioaddr)) |
| 154 | return PTR_ERR(priv->ioaddr); |
| 155 | |
| 156 | priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216", |
| 157 | &ds1216_rtc_ops, THIS_MODULE); |
| 158 | if (IS_ERR(priv->rtc)) |
| 159 | return PTR_ERR(priv->rtc); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 160 | |
| 161 | /* dummy read to get clock into a known state */ |
| 162 | ds1216_read(priv->ioaddr, dummy); |
| 163 | return 0; |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 166 | static struct platform_driver ds1216_rtc_platform_driver = { |
| 167 | .driver = { |
| 168 | .name = "rtc-ds1216", |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 169 | }, |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Fabio Porcedda | 061d2a3 | 2013-07-03 15:08:03 -0700 | [diff] [blame] | 172 | module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 173 | |
| 174 | MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>"); |
| 175 | MODULE_DESCRIPTION("DS1216 RTC driver"); |
| 176 | MODULE_LICENSE("GPL"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 177 | MODULE_ALIAS("platform:rtc-ds1216"); |