1 // SPDX-License-Identifier: GPL-2.0-only
2 /* rtc-max6916.c
3  *
4  * Driver for MAXIM  max6916 Low Current, SPI Compatible
5  * Real Time Clock
6  *
7  * Author : Venkat Prashanth B U <venkat.prashanth2498@gmail.com>
8  */
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/rtc.h>
15 #include <linux/spi/spi.h>
16 #include <linux/bcd.h>
17 
18 /* Registers in max6916 rtc */
19 
20 #define MAX6916_SECONDS_REG	0x01
21 #define MAX6916_MINUTES_REG	0x02
22 #define MAX6916_HOURS_REG	0x03
23 #define MAX6916_DATE_REG	0x04
24 #define MAX6916_MONTH_REG	0x05
25 #define MAX6916_DAY_REG	0x06
26 #define MAX6916_YEAR_REG	0x07
27 #define MAX6916_CONTROL_REG	0x08
28 #define MAX6916_STATUS_REG	0x0C
29 #define MAX6916_CLOCK_BURST	0x3F
30 
max6916_read_reg(struct device * dev,unsigned char address,unsigned char * data)31 static int max6916_read_reg(struct device *dev, unsigned char address,
32 			    unsigned char *data)
33 {
34 	struct spi_device *spi = to_spi_device(dev);
35 
36 	*data = address | 0x80;
37 
38 	return spi_write_then_read(spi, data, 1, data, 1);
39 }
40 
max6916_write_reg(struct device * dev,unsigned char address,unsigned char data)41 static int max6916_write_reg(struct device *dev, unsigned char address,
42 			     unsigned char data)
43 {
44 	struct spi_device *spi = to_spi_device(dev);
45 	unsigned char buf[2];
46 
47 	buf[0] = address & 0x7F;
48 	buf[1] = data;
49 
50 	return spi_write_then_read(spi, buf, 2, NULL, 0);
51 }
52 
max6916_read_time(struct device * dev,struct rtc_time * dt)53 static int max6916_read_time(struct device *dev, struct rtc_time *dt)
54 {
55 	struct spi_device *spi = to_spi_device(dev);
56 	int err;
57 	unsigned char buf[8];
58 
59 	buf[0] = MAX6916_CLOCK_BURST | 0x80;
60 
61 	err = spi_write_then_read(spi, buf, 1, buf, 8);
62 
63 	if (err)
64 		return err;
65 
66 	dt->tm_sec = bcd2bin(buf[0]);
67 	dt->tm_min = bcd2bin(buf[1]);
68 	dt->tm_hour = bcd2bin(buf[2] & 0x3F);
69 	dt->tm_mday = bcd2bin(buf[3]);
70 	dt->tm_mon = bcd2bin(buf[4]) - 1;
71 	dt->tm_wday = bcd2bin(buf[5]) - 1;
72 	dt->tm_year = bcd2bin(buf[6]) + 100;
73 
74 	return 0;
75 }
76 
max6916_set_time(struct device * dev,struct rtc_time * dt)77 static int max6916_set_time(struct device *dev, struct rtc_time *dt)
78 {
79 	struct spi_device *spi = to_spi_device(dev);
80 	unsigned char buf[9];
81 
82 	if (dt->tm_year < 100 || dt->tm_year > 199) {
83 		dev_err(&spi->dev, "Year must be between 2000 and 2099. It's %d.\n",
84 			dt->tm_year + 1900);
85 		return -EINVAL;
86 	}
87 
88 	buf[0] = MAX6916_CLOCK_BURST & 0x7F;
89 	buf[1] = bin2bcd(dt->tm_sec);
90 	buf[2] = bin2bcd(dt->tm_min);
91 	buf[3] = (bin2bcd(dt->tm_hour) & 0X3F);
92 	buf[4] = bin2bcd(dt->tm_mday);
93 	buf[5] = bin2bcd(dt->tm_mon + 1);
94 	buf[6] = bin2bcd(dt->tm_wday + 1);
95 	buf[7] = bin2bcd(dt->tm_year % 100);
96 	buf[8] = bin2bcd(0x00);
97 
98 	/* write the rtc settings */
99 	return spi_write_then_read(spi, buf, 9, NULL, 0);
100 }
101 
102 static const struct rtc_class_ops max6916_rtc_ops = {
103 	.read_time = max6916_read_time,
104 	.set_time = max6916_set_time,
105 };
106 
max6916_probe(struct spi_device * spi)107 static int max6916_probe(struct spi_device *spi)
108 {
109 	struct rtc_device *rtc;
110 	unsigned char data;
111 	int res;
112 
113 	/* spi setup with max6916 in mode 3 and bits per word as 8 */
114 	spi->mode = SPI_MODE_3;
115 	spi->bits_per_word = 8;
116 	spi_setup(spi);
117 
118 	/* RTC Settings */
119 	res = max6916_read_reg(&spi->dev, MAX6916_SECONDS_REG, &data);
120 	if (res)
121 		return res;
122 
123 	/* Disable the write protect of rtc */
124 	max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
125 	data = data & ~(1 << 7);
126 	max6916_write_reg(&spi->dev, MAX6916_CONTROL_REG, data);
127 
128 	/*Enable oscillator,disable oscillator stop flag, glitch filter*/
129 	max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
130 	data = data & 0x1B;
131 	max6916_write_reg(&spi->dev, MAX6916_STATUS_REG, data);
132 
133 	/* display the settings */
134 	max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
135 	dev_info(&spi->dev, "MAX6916 RTC CTRL Reg = 0x%02x\n", data);
136 
137 	max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
138 	dev_info(&spi->dev, "MAX6916 RTC Status Reg = 0x%02x\n", data);
139 
140 	rtc = devm_rtc_device_register(&spi->dev, "max6916",
141 				       &max6916_rtc_ops, THIS_MODULE);
142 	if (IS_ERR(rtc))
143 		return PTR_ERR(rtc);
144 
145 	spi_set_drvdata(spi, rtc);
146 
147 	return 0;
148 }
149 
150 static struct spi_driver max6916_driver = {
151 	.driver = {
152 		.name = "max6916",
153 	},
154 	.probe = max6916_probe,
155 };
156 module_spi_driver(max6916_driver);
157 
158 MODULE_DESCRIPTION("MAX6916 SPI RTC DRIVER");
159 MODULE_AUTHOR("Venkat Prashanth B U <venkat.prashanth2498@gmail.com>");
160 MODULE_LICENSE("GPL v2");
161