Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* drivers/rtc/rtc-max6902.c
3 *
4 * Copyright (C) 2006 8D Technologies inc.
5 * Copyright (C) 2004 Compulab Ltd.
6 *
7 * Driver for MAX6902 spi RTC
8 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/platform_device.h>
13#include <linux/init.h>
14#include <linux/rtc.h>
15#include <linux/spi/spi.h>
16#include <linux/bcd.h>
17
18#define MAX6902_REG_SECONDS 0x01
19#define MAX6902_REG_MINUTES 0x03
20#define MAX6902_REG_HOURS 0x05
21#define MAX6902_REG_DATE 0x07
22#define MAX6902_REG_MONTH 0x09
23#define MAX6902_REG_DAY 0x0B
24#define MAX6902_REG_YEAR 0x0D
25#define MAX6902_REG_CONTROL 0x0F
26#define MAX6902_REG_CENTURY 0x13
27
28static int max6902_set_reg(struct device *dev, unsigned char address,
29 unsigned char data)
30{
31 struct spi_device *spi = to_spi_device(dev);
32 unsigned char buf[2];
33
34 /* MSB must be '0' to write */
35 buf[0] = address & 0x7f;
36 buf[1] = data;
37
38 return spi_write_then_read(spi, buf, 2, NULL, 0);
39}
40
41static int max6902_get_reg(struct device *dev, unsigned char address,
42 unsigned char *data)
43{
44 struct spi_device *spi = to_spi_device(dev);
45
46 /* Set MSB to indicate read */
47 *data = address | 0x80;
48
49 return spi_write_then_read(spi, data, 1, data, 1);
50}
51
52static int max6902_read_time(struct device *dev, struct rtc_time *dt)
53{
54 int err, century;
55 struct spi_device *spi = to_spi_device(dev);
56 unsigned char buf[8];
57
58 buf[0] = 0xbf; /* Burst read */
59
60 err = spi_write_then_read(spi, buf, 1, buf, 8);
61 if (err != 0)
62 return err;
63
64 /* The chip sends data in this order:
65 * Seconds, Minutes, Hours, Date, Month, Day, Year */
66 dt->tm_sec = bcd2bin(buf[0]);
67 dt->tm_min = bcd2bin(buf[1]);
68 dt->tm_hour = bcd2bin(buf[2]);
69 dt->tm_mday = bcd2bin(buf[3]);
70 dt->tm_mon = bcd2bin(buf[4]) - 1;
71 dt->tm_wday = bcd2bin(buf[5]);
72 dt->tm_year = bcd2bin(buf[6]);
73
74 /* Read century */
75 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
76 if (err != 0)
77 return err;
78
79 century = bcd2bin(buf[0]) * 100;
80
81 dt->tm_year += century;
82 dt->tm_year -= 1900;
83
84 return 0;
85}
86
87static int max6902_set_time(struct device *dev, struct rtc_time *dt)
88{
89 dt->tm_year = dt->tm_year + 1900;
90
91 /* Remove write protection */
92 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
93
94 max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
95 max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
96 max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
97
98 max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
99 max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
100 max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
101 max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
102 max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
103
104 /* Compulab used a delay here. However, the datasheet
105 * does not mention a delay being required anywhere... */
106 /* delay(2000); */
107
108 /* Write protect */
109 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
110
111 return 0;
112}
113
114static const struct rtc_class_ops max6902_rtc_ops = {
115 .read_time = max6902_read_time,
116 .set_time = max6902_set_time,
117};
118
119static int max6902_probe(struct spi_device *spi)
120{
121 struct rtc_device *rtc;
122 unsigned char tmp;
123 int res;
124
125 spi->mode = SPI_MODE_3;
126 spi->bits_per_word = 8;
127 spi_setup(spi);
128
129 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
130 if (res != 0)
131 return res;
132
133 rtc = devm_rtc_device_register(&spi->dev, "max6902",
134 &max6902_rtc_ops, THIS_MODULE);
135 if (IS_ERR(rtc))
136 return PTR_ERR(rtc);
137
138 spi_set_drvdata(spi, rtc);
139 return 0;
140}
141
142static struct spi_driver max6902_driver = {
143 .driver = {
144 .name = "rtc-max6902",
145 },
146 .probe = max6902_probe,
147};
148
149module_spi_driver(max6902_driver);
150
151MODULE_DESCRIPTION("max6902 spi RTC driver");
152MODULE_AUTHOR("Raphael Assenat");
153MODULE_LICENSE("GPL");
154MODULE_ALIAS("spi:rtc-max6902");
1/* drivers/rtc/rtc-max6902.c
2 *
3 * Copyright (C) 2006 8D Technologies inc.
4 * Copyright (C) 2004 Compulab Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Driver for MAX6902 spi RTC
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/rtc.h>
19#include <linux/spi/spi.h>
20#include <linux/bcd.h>
21
22#define MAX6902_REG_SECONDS 0x01
23#define MAX6902_REG_MINUTES 0x03
24#define MAX6902_REG_HOURS 0x05
25#define MAX6902_REG_DATE 0x07
26#define MAX6902_REG_MONTH 0x09
27#define MAX6902_REG_DAY 0x0B
28#define MAX6902_REG_YEAR 0x0D
29#define MAX6902_REG_CONTROL 0x0F
30#define MAX6902_REG_CENTURY 0x13
31
32static int max6902_set_reg(struct device *dev, unsigned char address,
33 unsigned char data)
34{
35 struct spi_device *spi = to_spi_device(dev);
36 unsigned char buf[2];
37
38 /* MSB must be '0' to write */
39 buf[0] = address & 0x7f;
40 buf[1] = data;
41
42 return spi_write_then_read(spi, buf, 2, NULL, 0);
43}
44
45static int max6902_get_reg(struct device *dev, unsigned char address,
46 unsigned char *data)
47{
48 struct spi_device *spi = to_spi_device(dev);
49
50 /* Set MSB to indicate read */
51 *data = address | 0x80;
52
53 return spi_write_then_read(spi, data, 1, data, 1);
54}
55
56static int max6902_read_time(struct device *dev, struct rtc_time *dt)
57{
58 int err, century;
59 struct spi_device *spi = to_spi_device(dev);
60 unsigned char buf[8];
61
62 buf[0] = 0xbf; /* Burst read */
63
64 err = spi_write_then_read(spi, buf, 1, buf, 8);
65 if (err != 0)
66 return err;
67
68 /* The chip sends data in this order:
69 * Seconds, Minutes, Hours, Date, Month, Day, Year */
70 dt->tm_sec = bcd2bin(buf[0]);
71 dt->tm_min = bcd2bin(buf[1]);
72 dt->tm_hour = bcd2bin(buf[2]);
73 dt->tm_mday = bcd2bin(buf[3]);
74 dt->tm_mon = bcd2bin(buf[4]) - 1;
75 dt->tm_wday = bcd2bin(buf[5]);
76 dt->tm_year = bcd2bin(buf[6]);
77
78 /* Read century */
79 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
80 if (err != 0)
81 return err;
82
83 century = bcd2bin(buf[0]) * 100;
84
85 dt->tm_year += century;
86 dt->tm_year -= 1900;
87
88 return rtc_valid_tm(dt);
89}
90
91static int max6902_set_time(struct device *dev, struct rtc_time *dt)
92{
93 dt->tm_year = dt->tm_year + 1900;
94
95 /* Remove write protection */
96 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
97
98 max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
99 max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
100 max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
101
102 max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
103 max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
104 max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
105 max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
106 max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
107
108 /* Compulab used a delay here. However, the datasheet
109 * does not mention a delay being required anywhere... */
110 /* delay(2000); */
111
112 /* Write protect */
113 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
114
115 return 0;
116}
117
118static const struct rtc_class_ops max6902_rtc_ops = {
119 .read_time = max6902_read_time,
120 .set_time = max6902_set_time,
121};
122
123static int max6902_probe(struct spi_device *spi)
124{
125 struct rtc_device *rtc;
126 unsigned char tmp;
127 int res;
128
129 spi->mode = SPI_MODE_3;
130 spi->bits_per_word = 8;
131 spi_setup(spi);
132
133 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
134 if (res != 0)
135 return res;
136
137 rtc = devm_rtc_device_register(&spi->dev, "max6902",
138 &max6902_rtc_ops, THIS_MODULE);
139 if (IS_ERR(rtc))
140 return PTR_ERR(rtc);
141
142 spi_set_drvdata(spi, rtc);
143 return 0;
144}
145
146static struct spi_driver max6902_driver = {
147 .driver = {
148 .name = "rtc-max6902",
149 .owner = THIS_MODULE,
150 },
151 .probe = max6902_probe,
152};
153
154module_spi_driver(max6902_driver);
155
156MODULE_DESCRIPTION("max6902 spi RTC driver");
157MODULE_AUTHOR("Raphael Assenat");
158MODULE_LICENSE("GPL");
159MODULE_ALIAS("spi:rtc-max6902");