David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 1 | /* rtc-bq4802.c: TI BQ4802 RTC driver. |
| 2 | * |
| 3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/bcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 14 | |
| 15 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); |
| 16 | MODULE_DESCRIPTION("TI BQ4802 RTC driver"); |
| 17 | MODULE_LICENSE("GPL"); |
| 18 | |
| 19 | struct bq4802 { |
| 20 | void __iomem *regs; |
David S. Miller | 503acc8 | 2008-09-09 19:50:04 -0700 | [diff] [blame] | 21 | unsigned long ioport; |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 22 | struct rtc_device *rtc; |
| 23 | spinlock_t lock; |
| 24 | struct resource *r; |
| 25 | u8 (*read)(struct bq4802 *, int); |
| 26 | void (*write)(struct bq4802 *, int, u8); |
| 27 | }; |
| 28 | |
| 29 | static u8 bq4802_read_io(struct bq4802 *p, int off) |
| 30 | { |
David S. Miller | 503acc8 | 2008-09-09 19:50:04 -0700 | [diff] [blame] | 31 | return inb(p->ioport + off); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void bq4802_write_io(struct bq4802 *p, int off, u8 val) |
| 35 | { |
David S. Miller | 503acc8 | 2008-09-09 19:50:04 -0700 | [diff] [blame] | 36 | outb(val, p->ioport + off); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static u8 bq4802_read_mem(struct bq4802 *p, int off) |
| 40 | { |
| 41 | return readb(p->regs + off); |
| 42 | } |
| 43 | |
| 44 | static void bq4802_write_mem(struct bq4802 *p, int off, u8 val) |
| 45 | { |
David S. Miller | 503acc8 | 2008-09-09 19:50:04 -0700 | [diff] [blame] | 46 | writeb(val, p->regs + off); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int bq4802_read_time(struct device *dev, struct rtc_time *tm) |
| 50 | { |
| 51 | struct platform_device *pdev = to_platform_device(dev); |
| 52 | struct bq4802 *p = platform_get_drvdata(pdev); |
| 53 | unsigned long flags; |
| 54 | unsigned int century; |
| 55 | u8 val; |
| 56 | |
| 57 | spin_lock_irqsave(&p->lock, flags); |
| 58 | |
| 59 | val = p->read(p, 0x0e); |
| 60 | p->write(p, 0xe, val | 0x08); |
| 61 | |
| 62 | tm->tm_sec = p->read(p, 0x00); |
| 63 | tm->tm_min = p->read(p, 0x02); |
| 64 | tm->tm_hour = p->read(p, 0x04); |
| 65 | tm->tm_mday = p->read(p, 0x06); |
| 66 | tm->tm_mon = p->read(p, 0x09); |
| 67 | tm->tm_year = p->read(p, 0x0a); |
| 68 | tm->tm_wday = p->read(p, 0x08); |
| 69 | century = p->read(p, 0x0f); |
| 70 | |
| 71 | p->write(p, 0x0e, val); |
| 72 | |
| 73 | spin_unlock_irqrestore(&p->lock, flags); |
| 74 | |
Andrew Morton | e232cfd | 2008-10-18 20:28:43 -0700 | [diff] [blame] | 75 | tm->tm_sec = bcd2bin(tm->tm_sec); |
| 76 | tm->tm_min = bcd2bin(tm->tm_min); |
| 77 | tm->tm_hour = bcd2bin(tm->tm_hour); |
| 78 | tm->tm_mday = bcd2bin(tm->tm_mday); |
| 79 | tm->tm_mon = bcd2bin(tm->tm_mon); |
| 80 | tm->tm_year = bcd2bin(tm->tm_year); |
| 81 | tm->tm_wday = bcd2bin(tm->tm_wday); |
| 82 | century = bcd2bin(century); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 83 | |
| 84 | tm->tm_year += (century * 100); |
| 85 | tm->tm_year -= 1900; |
| 86 | |
| 87 | tm->tm_mon--; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int bq4802_set_time(struct device *dev, struct rtc_time *tm) |
| 93 | { |
| 94 | struct platform_device *pdev = to_platform_device(dev); |
| 95 | struct bq4802 *p = platform_get_drvdata(pdev); |
| 96 | u8 sec, min, hrs, day, mon, yrs, century, val; |
| 97 | unsigned long flags; |
| 98 | unsigned int year; |
| 99 | |
| 100 | year = tm->tm_year + 1900; |
| 101 | century = year / 100; |
| 102 | yrs = year % 100; |
| 103 | |
| 104 | mon = tm->tm_mon + 1; /* tm_mon starts at zero */ |
| 105 | day = tm->tm_mday; |
| 106 | hrs = tm->tm_hour; |
| 107 | min = tm->tm_min; |
| 108 | sec = tm->tm_sec; |
| 109 | |
Andrew Morton | e232cfd | 2008-10-18 20:28:43 -0700 | [diff] [blame] | 110 | sec = bin2bcd(sec); |
| 111 | min = bin2bcd(min); |
| 112 | hrs = bin2bcd(hrs); |
| 113 | day = bin2bcd(day); |
| 114 | mon = bin2bcd(mon); |
| 115 | yrs = bin2bcd(yrs); |
| 116 | century = bin2bcd(century); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 117 | |
| 118 | spin_lock_irqsave(&p->lock, flags); |
| 119 | |
| 120 | val = p->read(p, 0x0e); |
| 121 | p->write(p, 0x0e, val | 0x08); |
| 122 | |
| 123 | p->write(p, 0x00, sec); |
| 124 | p->write(p, 0x02, min); |
| 125 | p->write(p, 0x04, hrs); |
| 126 | p->write(p, 0x06, day); |
| 127 | p->write(p, 0x09, mon); |
| 128 | p->write(p, 0x0a, yrs); |
| 129 | p->write(p, 0x0f, century); |
| 130 | |
| 131 | p->write(p, 0x0e, val); |
| 132 | |
| 133 | spin_unlock_irqrestore(&p->lock, flags); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static const struct rtc_class_ops bq4802_ops = { |
| 139 | .read_time = bq4802_read_time, |
| 140 | .set_time = bq4802_set_time, |
| 141 | }; |
| 142 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 143 | static int bq4802_probe(struct platform_device *pdev) |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 144 | { |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 145 | struct bq4802 *p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 146 | int err = -ENOMEM; |
| 147 | |
| 148 | if (!p) |
| 149 | goto out; |
| 150 | |
| 151 | spin_lock_init(&p->lock); |
| 152 | |
| 153 | p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 154 | if (!p->r) { |
| 155 | p->r = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 156 | err = -EINVAL; |
| 157 | if (!p->r) |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 158 | goto out; |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 159 | } |
| 160 | if (p->r->flags & IORESOURCE_IO) { |
David S. Miller | 503acc8 | 2008-09-09 19:50:04 -0700 | [diff] [blame] | 161 | p->ioport = p->r->start; |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 162 | p->read = bq4802_read_io; |
| 163 | p->write = bq4802_write_io; |
| 164 | } else if (p->r->flags & IORESOURCE_MEM) { |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 165 | p->regs = devm_ioremap(&pdev->dev, p->r->start, |
| 166 | resource_size(p->r)); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 167 | p->read = bq4802_read_mem; |
| 168 | p->write = bq4802_write_mem; |
| 169 | } else { |
| 170 | err = -EINVAL; |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 171 | goto out; |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 174 | platform_set_drvdata(pdev, p); |
| 175 | |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 176 | p->rtc = devm_rtc_device_register(&pdev->dev, "bq4802", |
| 177 | &bq4802_ops, THIS_MODULE); |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 178 | if (IS_ERR(p->rtc)) { |
| 179 | err = PTR_ERR(p->rtc); |
Jingoo Han | 26c5f7d | 2013-04-29 16:20:36 -0700 | [diff] [blame] | 180 | goto out; |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 181 | } |
| 182 | |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 183 | err = 0; |
| 184 | out: |
| 185 | return err; |
| 186 | |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 187 | } |
| 188 | |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 189 | /* work with hotplug and coldplug */ |
| 190 | MODULE_ALIAS("platform:rtc-bq4802"); |
| 191 | |
| 192 | static struct platform_driver bq4802_driver = { |
| 193 | .driver = { |
| 194 | .name = "rtc-bq4802", |
| 195 | .owner = THIS_MODULE, |
| 196 | }, |
| 197 | .probe = bq4802_probe, |
David S. Miller | cca4c23 | 2008-08-29 01:29:53 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 200 | module_platform_driver(bq4802_driver); |