Merge tag 'kvm-4.14-1' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux/fpc-iii.git] / drivers / rtc / rtc-max6916.c
blob623ab27b2757cda88eb75486a88e87369f0034bc
1 /* rtc-max6916.c
3 * Driver for MAXIM max6916 Low Current, SPI Compatible
4 * Real Time Clock
6 * Author : Venkat Prashanth B U <venkat.prashanth2498@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/rtc.h>
19 #include <linux/spi/spi.h>
20 #include <linux/bcd.h>
22 /* Registers in max6916 rtc */
24 #define MAX6916_SECONDS_REG 0x01
25 #define MAX6916_MINUTES_REG 0x02
26 #define MAX6916_HOURS_REG 0x03
27 #define MAX6916_DATE_REG 0x04
28 #define MAX6916_MONTH_REG 0x05
29 #define MAX6916_DAY_REG 0x06
30 #define MAX6916_YEAR_REG 0x07
31 #define MAX6916_CONTROL_REG 0x08
32 #define MAX6916_STATUS_REG 0x0C
33 #define MAX6916_CLOCK_BURST 0x3F
35 static int max6916_read_reg(struct device *dev, unsigned char address,
36 unsigned char *data)
38 struct spi_device *spi = to_spi_device(dev);
40 *data = address | 0x80;
42 return spi_write_then_read(spi, data, 1, data, 1);
45 static int max6916_write_reg(struct device *dev, unsigned char address,
46 unsigned char data)
48 struct spi_device *spi = to_spi_device(dev);
49 unsigned char buf[2];
51 buf[0] = address & 0x7F;
52 buf[1] = data;
54 return spi_write_then_read(spi, buf, 2, NULL, 0);
57 static int max6916_read_time(struct device *dev, struct rtc_time *dt)
59 struct spi_device *spi = to_spi_device(dev);
60 int err;
61 unsigned char buf[8];
63 buf[0] = MAX6916_CLOCK_BURST | 0x80;
65 err = spi_write_then_read(spi, buf, 1, buf, 8);
67 if (err)
68 return err;
70 dt->tm_sec = bcd2bin(buf[0]);
71 dt->tm_min = bcd2bin(buf[1]);
72 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
73 dt->tm_mday = bcd2bin(buf[3]);
74 dt->tm_mon = bcd2bin(buf[4]) - 1;
75 dt->tm_wday = bcd2bin(buf[5]) - 1;
76 dt->tm_year = bcd2bin(buf[6]) + 100;
78 return rtc_valid_tm(dt);
81 static int max6916_set_time(struct device *dev, struct rtc_time *dt)
83 struct spi_device *spi = to_spi_device(dev);
84 unsigned char buf[9];
86 if (dt->tm_year < 100 || dt->tm_year > 199) {
87 dev_err(&spi->dev, "Year must be between 2000 and 2099. It's %d.\n",
88 dt->tm_year + 1900);
89 return -EINVAL;
92 buf[0] = MAX6916_CLOCK_BURST & 0x7F;
93 buf[1] = bin2bcd(dt->tm_sec);
94 buf[2] = bin2bcd(dt->tm_min);
95 buf[3] = (bin2bcd(dt->tm_hour) & 0X3F);
96 buf[4] = bin2bcd(dt->tm_mday);
97 buf[5] = bin2bcd(dt->tm_mon + 1);
98 buf[6] = bin2bcd(dt->tm_wday + 1);
99 buf[7] = bin2bcd(dt->tm_year % 100);
100 buf[8] = bin2bcd(0x00);
102 /* write the rtc settings */
103 return spi_write_then_read(spi, buf, 9, NULL, 0);
106 static const struct rtc_class_ops max6916_rtc_ops = {
107 .read_time = max6916_read_time,
108 .set_time = max6916_set_time,
111 static int max6916_probe(struct spi_device *spi)
113 struct rtc_device *rtc;
114 unsigned char data;
115 int res;
117 /* spi setup with max6916 in mode 3 and bits per word as 8 */
118 spi->mode = SPI_MODE_3;
119 spi->bits_per_word = 8;
120 spi_setup(spi);
122 /* RTC Settings */
123 res = max6916_read_reg(&spi->dev, MAX6916_SECONDS_REG, &data);
124 if (res)
125 return res;
127 /* Disable the write protect of rtc */
128 max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
129 data = data & ~(1 << 7);
130 max6916_write_reg(&spi->dev, MAX6916_CONTROL_REG, data);
132 /*Enable oscillator,disable oscillator stop flag, glitch filter*/
133 max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
134 data = data & 0x1B;
135 max6916_write_reg(&spi->dev, MAX6916_STATUS_REG, data);
137 /* display the settings */
138 max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
139 dev_info(&spi->dev, "MAX6916 RTC CTRL Reg = 0x%02x\n", data);
141 max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
142 dev_info(&spi->dev, "MAX6916 RTC Status Reg = 0x%02x\n", data);
144 rtc = devm_rtc_device_register(&spi->dev, "max6916",
145 &max6916_rtc_ops, THIS_MODULE);
146 if (IS_ERR(rtc))
147 return PTR_ERR(rtc);
149 spi_set_drvdata(spi, rtc);
151 return 0;
154 static struct spi_driver max6916_driver = {
155 .driver = {
156 .name = "max6916",
158 .probe = max6916_probe,
160 module_spi_driver(max6916_driver);
162 MODULE_DESCRIPTION("MAX6916 SPI RTC DRIVER");
163 MODULE_AUTHOR("Venkat Prashanth B U <venkat.prashanth2498@gmail.com>");
164 MODULE_LICENSE("GPL v2");