xref: /linux/drivers/rtc/rtc-max6902.c (revision a5ef73f06cb648c3f41edfb5920cbc208368e5bd)
1f30c2269SUwe Zeisberger /* drivers/rtc/rtc-max6902.c
28e12ecc2SRaphael Assenat  *
38e12ecc2SRaphael Assenat  * Copyright (C) 2006 8D Technologies inc.
48e12ecc2SRaphael Assenat  * Copyright (C) 2004 Compulab Ltd.
58e12ecc2SRaphael Assenat  *
68e12ecc2SRaphael Assenat  * This program is free software; you can redistribute it and/or modify
78e12ecc2SRaphael Assenat  * it under the terms of the GNU General Public License version 2 as
88e12ecc2SRaphael Assenat  * published by the Free Software Foundation.
98e12ecc2SRaphael Assenat  *
108e12ecc2SRaphael Assenat  * Driver for MAX6902 spi RTC
118e12ecc2SRaphael Assenat  *
128e12ecc2SRaphael Assenat  */
138e12ecc2SRaphael Assenat 
148e12ecc2SRaphael Assenat #include <linux/module.h>
158e12ecc2SRaphael Assenat #include <linux/kernel.h>
168e12ecc2SRaphael Assenat #include <linux/platform_device.h>
178e12ecc2SRaphael Assenat #include <linux/init.h>
188e12ecc2SRaphael Assenat #include <linux/rtc.h>
198e12ecc2SRaphael Assenat #include <linux/spi/spi.h>
208e12ecc2SRaphael Assenat #include <linux/bcd.h>
218e12ecc2SRaphael Assenat 
228e12ecc2SRaphael Assenat #define MAX6902_REG_SECONDS		0x01
238e12ecc2SRaphael Assenat #define MAX6902_REG_MINUTES		0x03
248e12ecc2SRaphael Assenat #define MAX6902_REG_HOURS		0x05
258e12ecc2SRaphael Assenat #define MAX6902_REG_DATE		0x07
268e12ecc2SRaphael Assenat #define MAX6902_REG_MONTH		0x09
278e12ecc2SRaphael Assenat #define MAX6902_REG_DAY			0x0B
288e12ecc2SRaphael Assenat #define MAX6902_REG_YEAR		0x0D
298e12ecc2SRaphael Assenat #define MAX6902_REG_CONTROL		0x0F
308e12ecc2SRaphael Assenat #define MAX6902_REG_CENTURY		0x13
318e12ecc2SRaphael Assenat 
32a5771c6cSAlessandro Zummo static int max6902_set_reg(struct device *dev, unsigned char address,
338e12ecc2SRaphael Assenat 				unsigned char data)
348e12ecc2SRaphael Assenat {
358e12ecc2SRaphael Assenat 	struct spi_device *spi = to_spi_device(dev);
368e12ecc2SRaphael Assenat 	unsigned char buf[2];
378e12ecc2SRaphael Assenat 
388e12ecc2SRaphael Assenat 	/* MSB must be '0' to write */
398e12ecc2SRaphael Assenat 	buf[0] = address & 0x7f;
408e12ecc2SRaphael Assenat 	buf[1] = data;
418e12ecc2SRaphael Assenat 
42a5771c6cSAlessandro Zummo 	return spi_write_then_read(spi, buf, 2, NULL, 0);
438e12ecc2SRaphael Assenat }
448e12ecc2SRaphael Assenat 
458e12ecc2SRaphael Assenat static int max6902_get_reg(struct device *dev, unsigned char address,
468e12ecc2SRaphael Assenat 				unsigned char *data)
478e12ecc2SRaphael Assenat {
488e12ecc2SRaphael Assenat 	struct spi_device *spi = to_spi_device(dev);
498e12ecc2SRaphael Assenat 
508e12ecc2SRaphael Assenat 	/* Set MSB to indicate read */
51a5771c6cSAlessandro Zummo 	*data = address | 0x80;
528e12ecc2SRaphael Assenat 
53a5771c6cSAlessandro Zummo 	return spi_write_then_read(spi, data, 1, data, 1);
548e12ecc2SRaphael Assenat }
558e12ecc2SRaphael Assenat 
56a5771c6cSAlessandro Zummo static int max6902_read_time(struct device *dev, struct rtc_time *dt)
578e12ecc2SRaphael Assenat {
58a5771c6cSAlessandro Zummo 	int err, century;
598e12ecc2SRaphael Assenat 	struct spi_device *spi = to_spi_device(dev);
60a5771c6cSAlessandro Zummo 	unsigned char buf[8];
618e12ecc2SRaphael Assenat 
62a5771c6cSAlessandro Zummo 	buf[0] = 0xbf;	/* Burst read */
63a5771c6cSAlessandro Zummo 
64a5771c6cSAlessandro Zummo 	err = spi_write_then_read(spi, buf, 1, buf, 8);
65a5771c6cSAlessandro Zummo 	if (err != 0)
668e12ecc2SRaphael Assenat 		return err;
678e12ecc2SRaphael Assenat 
688e12ecc2SRaphael Assenat 	/* The chip sends data in this order:
698e12ecc2SRaphael Assenat 	 * Seconds, Minutes, Hours, Date, Month, Day, Year */
70a5771c6cSAlessandro Zummo 	dt->tm_sec	= bcd2bin(buf[0]);
71a5771c6cSAlessandro Zummo 	dt->tm_min	= bcd2bin(buf[1]);
72a5771c6cSAlessandro Zummo 	dt->tm_hour	= bcd2bin(buf[2]);
73a5771c6cSAlessandro Zummo 	dt->tm_mday	= bcd2bin(buf[3]);
74a5771c6cSAlessandro Zummo 	dt->tm_mon	= bcd2bin(buf[4]) - 1;
75a5771c6cSAlessandro Zummo 	dt->tm_wday	= bcd2bin(buf[5]);
76a5771c6cSAlessandro Zummo 	dt->tm_year	= bcd2bin(buf[6]);
778e12ecc2SRaphael Assenat 
78a5771c6cSAlessandro Zummo 	/* Read century */
79a5771c6cSAlessandro Zummo 	err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
80a5771c6cSAlessandro Zummo 	if (err != 0)
81a5771c6cSAlessandro Zummo 		return err;
82a5771c6cSAlessandro Zummo 
83a5771c6cSAlessandro Zummo 	century = bcd2bin(buf[0]) * 100;
848e12ecc2SRaphael Assenat 
858e12ecc2SRaphael Assenat 	dt->tm_year += century;
868e12ecc2SRaphael Assenat 	dt->tm_year -= 1900;
878e12ecc2SRaphael Assenat 
88a5771c6cSAlessandro Zummo 	return rtc_valid_tm(dt);
898e12ecc2SRaphael Assenat }
908e12ecc2SRaphael Assenat 
91a5771c6cSAlessandro Zummo static int max6902_set_time(struct device *dev, struct rtc_time *dt)
928e12ecc2SRaphael Assenat {
938e12ecc2SRaphael Assenat 	dt->tm_year = dt->tm_year + 1900;
948e12ecc2SRaphael Assenat 
958e12ecc2SRaphael Assenat 	/* Remove write protection */
968e12ecc2SRaphael Assenat 	max6902_set_reg(dev, 0xF, 0);
978e12ecc2SRaphael Assenat 
98fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x01, bin2bcd(dt->tm_sec));
99fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x03, bin2bcd(dt->tm_min));
100fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour));
1018e12ecc2SRaphael Assenat 
102fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday));
103fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon + 1));
104fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday));
105fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year % 100));
106fe20ba70SAdrian Bunk 	max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year / 100));
1078e12ecc2SRaphael Assenat 
1088e12ecc2SRaphael Assenat 	/* Compulab used a delay here. However, the datasheet
1098e12ecc2SRaphael Assenat 	 * does not mention a delay being required anywhere... */
1108e12ecc2SRaphael Assenat 	/* delay(2000); */
1118e12ecc2SRaphael Assenat 
1128e12ecc2SRaphael Assenat 	/* Write protect */
1138e12ecc2SRaphael Assenat 	max6902_set_reg(dev, 0xF, 0x80);
1148e12ecc2SRaphael Assenat 
1158e12ecc2SRaphael Assenat 	return 0;
1168e12ecc2SRaphael Assenat }
1178e12ecc2SRaphael Assenat 
118ff8371acSDavid Brownell static const struct rtc_class_ops max6902_rtc_ops = {
1198e12ecc2SRaphael Assenat 	.read_time	= max6902_read_time,
1208e12ecc2SRaphael Assenat 	.set_time	= max6902_set_time,
1218e12ecc2SRaphael Assenat };
1228e12ecc2SRaphael Assenat 
1235a167f45SGreg Kroah-Hartman static int max6902_probe(struct spi_device *spi)
1248e12ecc2SRaphael Assenat {
1258e12ecc2SRaphael Assenat 	struct rtc_device *rtc;
1268e12ecc2SRaphael Assenat 	unsigned char tmp;
1278e12ecc2SRaphael Assenat 	int res;
1288e12ecc2SRaphael Assenat 
129a5771c6cSAlessandro Zummo 	spi->mode = SPI_MODE_3;
130a5771c6cSAlessandro Zummo 	spi->bits_per_word = 8;
131a5771c6cSAlessandro Zummo 	spi_setup(spi);
132a5771c6cSAlessandro Zummo 
133a5771c6cSAlessandro Zummo 	res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
134a5771c6cSAlessandro Zummo 	if (res != 0)
135a5771c6cSAlessandro Zummo 		return res;
136a5771c6cSAlessandro Zummo 
13704353a73SJingoo Han 	rtc = devm_rtc_device_register(&spi->dev, "max6902",
13804353a73SJingoo Han 				&max6902_rtc_ops, THIS_MODULE);
1398e12ecc2SRaphael Assenat 	if (IS_ERR(rtc))
1408e12ecc2SRaphael Assenat 		return PTR_ERR(rtc);
1418e12ecc2SRaphael Assenat 
142*a5ef73f0SJingoo Han 	spi_set_drvdata(spi, rtc);
1438e12ecc2SRaphael Assenat 	return 0;
1448e12ecc2SRaphael Assenat }
1458e12ecc2SRaphael Assenat 
1465a167f45SGreg Kroah-Hartman static int max6902_remove(struct spi_device *spi)
1478e12ecc2SRaphael Assenat {
1488e12ecc2SRaphael Assenat 	return 0;
1498e12ecc2SRaphael Assenat }
1508e12ecc2SRaphael Assenat 
1518e12ecc2SRaphael Assenat static struct spi_driver max6902_driver = {
1528e12ecc2SRaphael Assenat 	.driver = {
1535c076fceSDavid Brownell 		.name	= "rtc-max6902",
1548e12ecc2SRaphael Assenat 		.owner	= THIS_MODULE,
1558e12ecc2SRaphael Assenat 	},
1568e12ecc2SRaphael Assenat 	.probe	= max6902_probe,
1575a167f45SGreg Kroah-Hartman 	.remove = max6902_remove,
1588e12ecc2SRaphael Assenat };
1598e12ecc2SRaphael Assenat 
160109e9418SAxel Lin module_spi_driver(max6902_driver);
1618e12ecc2SRaphael Assenat 
1628e12ecc2SRaphael Assenat MODULE_DESCRIPTION ("max6902 spi RTC driver");
1638e12ecc2SRaphael Assenat MODULE_AUTHOR ("Raphael Assenat");
1648e12ecc2SRaphael Assenat MODULE_LICENSE ("GPL");
165e0626e38SAnton Vorontsov MODULE_ALIAS("spi:rtc-max6902");
166