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> |
| 12 | |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 13 | #define DRV_VERSION "0.2" |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 14 | |
| 15 | struct ds1216_regs { |
| 16 | u8 tsec; |
| 17 | u8 sec; |
| 18 | u8 min; |
| 19 | u8 hour; |
| 20 | u8 wday; |
| 21 | u8 mday; |
| 22 | u8 month; |
| 23 | u8 year; |
| 24 | }; |
| 25 | |
| 26 | #define DS1216_HOUR_1224 (1 << 7) |
| 27 | #define DS1216_HOUR_AMPM (1 << 5) |
| 28 | |
| 29 | struct ds1216_priv { |
| 30 | struct rtc_device *rtc; |
| 31 | void __iomem *ioaddr; |
| 32 | size_t size; |
| 33 | unsigned long baseaddr; |
| 34 | }; |
| 35 | |
| 36 | static const u8 magic[] = { |
| 37 | 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Read the 64 bit we'd like to have - It a series |
| 42 | * of 64 bits showing up in the LSB of the base register. |
| 43 | * |
| 44 | */ |
| 45 | static void ds1216_read(u8 __iomem *ioaddr, u8 *buf) |
| 46 | { |
| 47 | unsigned char c; |
| 48 | int i, j; |
| 49 | |
| 50 | for (i = 0; i < 8; i++) { |
| 51 | c = 0; |
| 52 | for (j = 0; j < 8; j++) |
| 53 | c |= (readb(ioaddr) & 0x1) << j; |
| 54 | buf[i] = c; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf) |
| 59 | { |
| 60 | unsigned char c; |
| 61 | int i, j; |
| 62 | |
| 63 | for (i = 0; i < 8; i++) { |
| 64 | c = buf[i]; |
| 65 | for (j = 0; j < 8; j++) { |
| 66 | writeb(c, ioaddr); |
| 67 | c = c >> 1; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr) |
| 73 | { |
| 74 | /* Reset magic pointer */ |
| 75 | readb(ioaddr); |
| 76 | /* Write 64 bit magic to DS1216 */ |
| 77 | ds1216_write(ioaddr, magic); |
| 78 | } |
| 79 | |
| 80 | static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 81 | { |
| 82 | struct platform_device *pdev = to_platform_device(dev); |
| 83 | struct ds1216_priv *priv = platform_get_drvdata(pdev); |
| 84 | struct ds1216_regs regs; |
| 85 | |
| 86 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 87 | ds1216_read(priv->ioaddr, (u8 *)®s); |
| 88 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 89 | tm->tm_sec = bcd2bin(regs.sec); |
| 90 | tm->tm_min = bcd2bin(regs.min); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 91 | if (regs.hour & DS1216_HOUR_1224) { |
| 92 | /* AM/PM mode */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 93 | tm->tm_hour = bcd2bin(regs.hour & 0x1f); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 94 | if (regs.hour & DS1216_HOUR_AMPM) |
| 95 | tm->tm_hour += 12; |
| 96 | } else |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 97 | tm->tm_hour = bcd2bin(regs.hour & 0x3f); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 98 | tm->tm_wday = (regs.wday & 7) - 1; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 99 | tm->tm_mday = bcd2bin(regs.mday & 0x3f); |
| 100 | tm->tm_mon = bcd2bin(regs.month & 0x1f); |
| 101 | tm->tm_year = bcd2bin(regs.year); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 102 | if (tm->tm_year < 70) |
| 103 | tm->tm_year += 100; |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 104 | |
| 105 | return rtc_valid_tm(tm); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 109 | { |
| 110 | struct platform_device *pdev = to_platform_device(dev); |
| 111 | struct ds1216_priv *priv = platform_get_drvdata(pdev); |
| 112 | struct ds1216_regs regs; |
| 113 | |
| 114 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 115 | ds1216_read(priv->ioaddr, (u8 *)®s); |
| 116 | |
| 117 | regs.tsec = 0; /* clear 0.1 and 0.01 seconds */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 118 | regs.sec = bin2bcd(tm->tm_sec); |
| 119 | regs.min = bin2bcd(tm->tm_min); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 120 | regs.hour &= DS1216_HOUR_1224; |
| 121 | if (regs.hour && tm->tm_hour > 12) { |
| 122 | regs.hour |= DS1216_HOUR_AMPM; |
| 123 | tm->tm_hour -= 12; |
| 124 | } |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 125 | regs.hour |= bin2bcd(tm->tm_hour); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 126 | regs.wday &= ~7; |
| 127 | regs.wday |= tm->tm_wday; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 128 | regs.mday = bin2bcd(tm->tm_mday); |
| 129 | regs.month = bin2bcd(tm->tm_mon); |
| 130 | regs.year = bin2bcd(tm->tm_year % 100); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 131 | |
| 132 | ds1216_switch_ds_to_clock(priv->ioaddr); |
| 133 | ds1216_write(priv->ioaddr, (u8 *)®s); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static const struct rtc_class_ops ds1216_rtc_ops = { |
| 138 | .read_time = ds1216_rtc_read_time, |
| 139 | .set_time = ds1216_rtc_set_time, |
| 140 | }; |
| 141 | |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 142 | static int __init ds1216_rtc_probe(struct platform_device *pdev) |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 143 | { |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 144 | struct resource *res; |
| 145 | struct ds1216_priv *priv; |
| 146 | int ret = 0; |
| 147 | u8 dummy[8]; |
| 148 | |
| 149 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 150 | if (!res) |
| 151 | return -ENODEV; |
| 152 | priv = kzalloc(sizeof *priv, GFP_KERNEL); |
| 153 | if (!priv) |
| 154 | return -ENOMEM; |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 155 | |
| 156 | platform_set_drvdata(pdev, priv); |
| 157 | |
| 158 | priv->size = resource_size(res); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 159 | if (!request_mem_region(res->start, priv->size, pdev->name)) { |
| 160 | ret = -EBUSY; |
| 161 | goto out; |
| 162 | } |
| 163 | priv->baseaddr = res->start; |
| 164 | priv->ioaddr = ioremap(priv->baseaddr, priv->size); |
| 165 | if (!priv->ioaddr) { |
| 166 | ret = -ENOMEM; |
| 167 | goto out; |
| 168 | } |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 169 | priv->rtc = rtc_device_register("ds1216", &pdev->dev, |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 170 | &ds1216_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 171 | if (IS_ERR(priv->rtc)) { |
| 172 | ret = PTR_ERR(priv->rtc); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 173 | goto out; |
| 174 | } |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 175 | |
| 176 | /* dummy read to get clock into a known state */ |
| 177 | ds1216_read(priv->ioaddr, dummy); |
| 178 | return 0; |
| 179 | |
| 180 | out: |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 181 | if (priv->ioaddr) |
| 182 | iounmap(priv->ioaddr); |
| 183 | if (priv->baseaddr) |
| 184 | release_mem_region(priv->baseaddr, priv->size); |
| 185 | kfree(priv); |
| 186 | return ret; |
| 187 | } |
| 188 | |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 189 | static int __exit ds1216_rtc_remove(struct platform_device *pdev) |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 190 | { |
| 191 | struct ds1216_priv *priv = platform_get_drvdata(pdev); |
| 192 | |
| 193 | rtc_device_unregister(priv->rtc); |
| 194 | iounmap(priv->ioaddr); |
| 195 | release_mem_region(priv->baseaddr, priv->size); |
| 196 | kfree(priv); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static struct platform_driver ds1216_rtc_platform_driver = { |
| 201 | .driver = { |
| 202 | .name = "rtc-ds1216", |
| 203 | .owner = THIS_MODULE, |
| 204 | }, |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 205 | .remove = __exit_p(ds1216_rtc_remove), |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static int __init ds1216_rtc_init(void) |
| 209 | { |
Alessandro Zummo | 97a1f95 | 2009-01-06 14:42:23 -0800 | [diff] [blame] | 210 | return platform_driver_probe(&ds1216_rtc_platform_driver, ds1216_rtc_probe); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void __exit ds1216_rtc_exit(void) |
| 214 | { |
| 215 | platform_driver_unregister(&ds1216_rtc_platform_driver); |
| 216 | } |
| 217 | |
| 218 | MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>"); |
| 219 | MODULE_DESCRIPTION("DS1216 RTC driver"); |
| 220 | MODULE_LICENSE("GPL"); |
| 221 | MODULE_VERSION(DRV_VERSION); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 222 | MODULE_ALIAS("platform:rtc-ds1216"); |
Thomas Bogendoerfer | 537739d | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 223 | |
| 224 | module_init(ds1216_rtc_init); |
| 225 | module_exit(ds1216_rtc_exit); |