Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ST M48T86 / Dallas DS12887 RTC driver |
| 3 | * Copyright (c) 2006 Tower Technologies |
| 4 | * |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This drivers only supports the clock running in BCD and 24H mode. |
| 12 | * If it will be ever adapted to binary and 12H mode, care must be taken |
| 13 | * to not introduce bugs. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/platform_device.h> |
Alexandre Belloni | 803bb30 | 2016-06-26 22:57:52 +0200 | [diff] [blame] | 19 | #include <linux/platform_data/rtc-m48t86.h> |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 20 | #include <linux/bcd.h> |
| 21 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 22 | #define M48T86_SEC 0x00 |
| 23 | #define M48T86_SECALRM 0x01 |
| 24 | #define M48T86_MIN 0x02 |
| 25 | #define M48T86_MINALRM 0x03 |
| 26 | #define M48T86_HOUR 0x04 |
| 27 | #define M48T86_HOURALRM 0x05 |
| 28 | #define M48T86_DOW 0x06 /* 1 = sunday */ |
| 29 | #define M48T86_DOM 0x07 |
| 30 | #define M48T86_MONTH 0x08 /* 1 - 12 */ |
| 31 | #define M48T86_YEAR 0x09 /* 0 - 99 */ |
| 32 | #define M48T86_A 0x0a |
| 33 | #define M48T86_B 0x0b |
| 34 | #define M48T86_B_SET BIT(7) |
| 35 | #define M48T86_B_DM BIT(2) |
| 36 | #define M48T86_B_H24 BIT(1) |
| 37 | #define M48T86_C 0x0c |
| 38 | #define M48T86_D 0x0d |
| 39 | #define M48T86_D_VRT BIT(7) |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 40 | |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 41 | static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 42 | { |
| 43 | unsigned char reg; |
| 44 | struct platform_device *pdev = to_platform_device(dev); |
Jingoo Han | b5f0902 | 2013-11-12 15:10:44 -0800 | [diff] [blame] | 45 | struct m48t86_ops *ops = dev_get_platdata(&pdev->dev); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 46 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 47 | reg = ops->readbyte(M48T86_B); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 48 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 49 | if (reg & M48T86_B_DM) { |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 50 | /* data (binary) mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 51 | tm->tm_sec = ops->readbyte(M48T86_SEC); |
| 52 | tm->tm_min = ops->readbyte(M48T86_MIN); |
| 53 | tm->tm_hour = ops->readbyte(M48T86_HOUR) & 0x3f; |
| 54 | tm->tm_mday = ops->readbyte(M48T86_DOM); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 55 | /* tm_mon is 0-11 */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 56 | tm->tm_mon = ops->readbyte(M48T86_MONTH) - 1; |
| 57 | tm->tm_year = ops->readbyte(M48T86_YEAR) + 100; |
| 58 | tm->tm_wday = ops->readbyte(M48T86_DOW); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 59 | } else { |
| 60 | /* bcd mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 61 | tm->tm_sec = bcd2bin(ops->readbyte(M48T86_SEC)); |
| 62 | tm->tm_min = bcd2bin(ops->readbyte(M48T86_MIN)); |
| 63 | tm->tm_hour = bcd2bin(ops->readbyte(M48T86_HOUR) & 0x3f); |
| 64 | tm->tm_mday = bcd2bin(ops->readbyte(M48T86_DOM)); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 65 | /* tm_mon is 0-11 */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 66 | tm->tm_mon = bcd2bin(ops->readbyte(M48T86_MONTH)) - 1; |
| 67 | tm->tm_year = bcd2bin(ops->readbyte(M48T86_YEAR)) + 100; |
| 68 | tm->tm_wday = bcd2bin(ops->readbyte(M48T86_DOW)); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* correct the hour if the clock is in 12h mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 72 | if (!(reg & M48T86_B_H24)) |
| 73 | if (ops->readbyte(M48T86_HOUR) & 0x80) |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 74 | tm->tm_hour += 12; |
| 75 | |
Wan ZongShun | 52142ed | 2010-08-10 18:02:17 -0700 | [diff] [blame] | 76 | return rtc_valid_tm(tm); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 80 | { |
| 81 | unsigned char reg; |
| 82 | struct platform_device *pdev = to_platform_device(dev); |
Jingoo Han | b5f0902 | 2013-11-12 15:10:44 -0800 | [diff] [blame] | 83 | struct m48t86_ops *ops = dev_get_platdata(&pdev->dev); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 84 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 85 | reg = ops->readbyte(M48T86_B); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 86 | |
| 87 | /* update flag and 24h mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 88 | reg |= M48T86_B_SET | M48T86_B_H24; |
| 89 | ops->writebyte(reg, M48T86_B); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 90 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 91 | if (reg & M48T86_B_DM) { |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 92 | /* data (binary) mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 93 | ops->writebyte(tm->tm_sec, M48T86_SEC); |
| 94 | ops->writebyte(tm->tm_min, M48T86_MIN); |
| 95 | ops->writebyte(tm->tm_hour, M48T86_HOUR); |
| 96 | ops->writebyte(tm->tm_mday, M48T86_DOM); |
| 97 | ops->writebyte(tm->tm_mon + 1, M48T86_MONTH); |
| 98 | ops->writebyte(tm->tm_year % 100, M48T86_YEAR); |
| 99 | ops->writebyte(tm->tm_wday, M48T86_DOW); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 100 | } else { |
| 101 | /* bcd mode */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 102 | ops->writebyte(bin2bcd(tm->tm_sec), M48T86_SEC); |
| 103 | ops->writebyte(bin2bcd(tm->tm_min), M48T86_MIN); |
| 104 | ops->writebyte(bin2bcd(tm->tm_hour), M48T86_HOUR); |
| 105 | ops->writebyte(bin2bcd(tm->tm_mday), M48T86_DOM); |
| 106 | ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_MONTH); |
| 107 | ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_YEAR); |
| 108 | ops->writebyte(bin2bcd(tm->tm_wday), M48T86_DOW); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* update ended */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 112 | reg &= ~M48T86_B_SET; |
| 113 | ops->writebyte(reg, M48T86_B); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq) |
| 119 | { |
| 120 | unsigned char reg; |
| 121 | struct platform_device *pdev = to_platform_device(dev); |
Jingoo Han | b5f0902 | 2013-11-12 15:10:44 -0800 | [diff] [blame] | 122 | struct m48t86_ops *ops = dev_get_platdata(&pdev->dev); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 123 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 124 | reg = ops->readbyte(M48T86_B); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 125 | |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 126 | seq_printf(seq, "mode\t\t: %s\n", |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 127 | (reg & M48T86_B_DM) ? "binary" : "bcd"); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 128 | |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 129 | reg = ops->readbyte(M48T86_D); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 130 | |
| 131 | seq_printf(seq, "battery\t\t: %s\n", |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 132 | (reg & M48T86_D_VRT) ? "ok" : "exhausted"); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 137 | static const struct rtc_class_ops m48t86_rtc_ops = { |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 138 | .read_time = m48t86_rtc_read_time, |
| 139 | .set_time = m48t86_rtc_set_time, |
| 140 | .proc = m48t86_rtc_proc, |
| 141 | }; |
| 142 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 143 | static int m48t86_rtc_probe(struct platform_device *dev) |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 144 | { |
| 145 | unsigned char reg; |
Jingoo Han | b5f0902 | 2013-11-12 15:10:44 -0800 | [diff] [blame] | 146 | struct m48t86_ops *ops = dev_get_platdata(&dev->dev); |
Jingoo Han | d5b6bb0 | 2013-04-29 16:19:42 -0700 | [diff] [blame] | 147 | struct rtc_device *rtc; |
| 148 | |
| 149 | rtc = devm_rtc_device_register(&dev->dev, "m48t86", |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 150 | &m48t86_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 151 | |
Alessandro Zummo | d1d65b7 | 2006-04-10 22:54:45 -0700 | [diff] [blame] | 152 | if (IS_ERR(rtc)) |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 153 | return PTR_ERR(rtc); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 154 | |
| 155 | platform_set_drvdata(dev, rtc); |
| 156 | |
| 157 | /* read battery status */ |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 158 | reg = ops->readbyte(M48T86_D); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 159 | dev_info(&dev->dev, "battery %s\n", |
H Hartley Sweeten | 68b54f4 | 2017-02-10 11:11:55 -0700 | [diff] [blame^] | 160 | (reg & M48T86_D_VRT) ? "ok" : "exhausted"); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 165 | static struct platform_driver m48t86_rtc_platform_driver = { |
| 166 | .driver = { |
| 167 | .name = "rtc-m48t86", |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 168 | }, |
| 169 | .probe = m48t86_rtc_probe, |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 172 | module_platform_driver(m48t86_rtc_platform_driver); |
Alessandro Zummo | 1d98af8 | 2006-03-27 01:16:47 -0800 | [diff] [blame] | 173 | |
| 174 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 175 | MODULE_DESCRIPTION("M48T86 RTC driver"); |
| 176 | MODULE_LICENSE("GPL"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 177 | MODULE_ALIAS("platform:rtc-m48t86"); |