18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* rtc-ds1347.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
58c2ecf20Sopenharmony_ci * Real Time Clock
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/rtc.h>
158c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
168c2ecf20Sopenharmony_ci#include <linux/bcd.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Registers in ds1347 rtc */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DS1347_SECONDS_REG	0x01
228c2ecf20Sopenharmony_ci#define DS1347_MINUTES_REG	0x03
238c2ecf20Sopenharmony_ci#define DS1347_HOURS_REG	0x05
248c2ecf20Sopenharmony_ci#define DS1347_DATE_REG		0x07
258c2ecf20Sopenharmony_ci#define DS1347_MONTH_REG	0x09
268c2ecf20Sopenharmony_ci#define DS1347_DAY_REG		0x0B
278c2ecf20Sopenharmony_ci#define DS1347_YEAR_REG		0x0D
288c2ecf20Sopenharmony_ci#define DS1347_CONTROL_REG	0x0F
298c2ecf20Sopenharmony_ci#define DS1347_CENTURY_REG	0x13
308c2ecf20Sopenharmony_ci#define DS1347_STATUS_REG	0x17
318c2ecf20Sopenharmony_ci#define DS1347_CLOCK_BURST	0x3F
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define DS1347_WP_BIT		BIT(7)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define DS1347_NEOSC_BIT	BIT(7)
368c2ecf20Sopenharmony_ci#define DS1347_OSF_BIT		BIT(2)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct regmap_range ds1347_ranges[] = {
398c2ecf20Sopenharmony_ci	{
408c2ecf20Sopenharmony_ci		.range_min = DS1347_SECONDS_REG,
418c2ecf20Sopenharmony_ci		.range_max = DS1347_STATUS_REG,
428c2ecf20Sopenharmony_ci	},
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic const struct regmap_access_table ds1347_access_table = {
468c2ecf20Sopenharmony_ci	.yes_ranges = ds1347_ranges,
478c2ecf20Sopenharmony_ci	.n_yes_ranges = ARRAY_SIZE(ds1347_ranges),
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int ds1347_read_time(struct device *dev, struct rtc_time *dt)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct regmap *map = dev_get_drvdata(dev);
538c2ecf20Sopenharmony_ci	unsigned int status, century, secs;
548c2ecf20Sopenharmony_ci	unsigned char buf[8];
558c2ecf20Sopenharmony_ci	int err;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	err = regmap_read(map, DS1347_STATUS_REG, &status);
588c2ecf20Sopenharmony_ci	if (err)
598c2ecf20Sopenharmony_ci		return err;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (status & DS1347_OSF_BIT)
628c2ecf20Sopenharmony_ci		return -EINVAL;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	do {
658c2ecf20Sopenharmony_ci		err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
668c2ecf20Sopenharmony_ci		if (err)
678c2ecf20Sopenharmony_ci			return err;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci		err = regmap_read(map, DS1347_CENTURY_REG, &century);
708c2ecf20Sopenharmony_ci		if (err)
718c2ecf20Sopenharmony_ci			return err;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		err = regmap_read(map, DS1347_SECONDS_REG, &secs);
748c2ecf20Sopenharmony_ci		if (err)
758c2ecf20Sopenharmony_ci			return err;
768c2ecf20Sopenharmony_ci	} while (buf[0] != secs);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	dt->tm_sec = bcd2bin(buf[0]);
798c2ecf20Sopenharmony_ci	dt->tm_min = bcd2bin(buf[1] & 0x7f);
808c2ecf20Sopenharmony_ci	dt->tm_hour = bcd2bin(buf[2] & 0x3F);
818c2ecf20Sopenharmony_ci	dt->tm_mday = bcd2bin(buf[3]);
828c2ecf20Sopenharmony_ci	dt->tm_mon = bcd2bin(buf[4]) - 1;
838c2ecf20Sopenharmony_ci	dt->tm_wday = bcd2bin(buf[5]) - 1;
848c2ecf20Sopenharmony_ci	dt->tm_year = (bcd2bin(century) * 100) + bcd2bin(buf[6]) - 1900;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int ds1347_set_time(struct device *dev, struct rtc_time *dt)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct regmap *map = dev_get_drvdata(dev);
928c2ecf20Sopenharmony_ci	unsigned int century;
938c2ecf20Sopenharmony_ci	unsigned char buf[8];
948c2ecf20Sopenharmony_ci	int err;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	err = regmap_update_bits(map, DS1347_STATUS_REG,
978c2ecf20Sopenharmony_ci				 DS1347_NEOSC_BIT, DS1347_NEOSC_BIT);
988c2ecf20Sopenharmony_ci	if (err)
998c2ecf20Sopenharmony_ci		return err;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	buf[0] = bin2bcd(dt->tm_sec);
1028c2ecf20Sopenharmony_ci	buf[1] = bin2bcd(dt->tm_min);
1038c2ecf20Sopenharmony_ci	buf[2] = (bin2bcd(dt->tm_hour) & 0x3F);
1048c2ecf20Sopenharmony_ci	buf[3] = bin2bcd(dt->tm_mday);
1058c2ecf20Sopenharmony_ci	buf[4] = bin2bcd(dt->tm_mon + 1);
1068c2ecf20Sopenharmony_ci	buf[5] = bin2bcd(dt->tm_wday + 1);
1078c2ecf20Sopenharmony_ci	buf[6] = bin2bcd(dt->tm_year % 100);
1088c2ecf20Sopenharmony_ci	buf[7] = bin2bcd(0x00);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	err = regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
1118c2ecf20Sopenharmony_ci	if (err)
1128c2ecf20Sopenharmony_ci		return err;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	century = (dt->tm_year / 100) + 19;
1158c2ecf20Sopenharmony_ci	err = regmap_write(map, DS1347_CENTURY_REG, bin2bcd(century));
1168c2ecf20Sopenharmony_ci	if (err)
1178c2ecf20Sopenharmony_ci		return err;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return regmap_update_bits(map, DS1347_STATUS_REG,
1208c2ecf20Sopenharmony_ci				  DS1347_NEOSC_BIT | DS1347_OSF_BIT, 0);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const struct rtc_class_ops ds1347_rtc_ops = {
1248c2ecf20Sopenharmony_ci	.read_time = ds1347_read_time,
1258c2ecf20Sopenharmony_ci	.set_time = ds1347_set_time,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic int ds1347_probe(struct spi_device *spi)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
1318c2ecf20Sopenharmony_ci	struct regmap_config config;
1328c2ecf20Sopenharmony_ci	struct regmap *map;
1338c2ecf20Sopenharmony_ci	int err;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	memset(&config, 0, sizeof(config));
1368c2ecf20Sopenharmony_ci	config.reg_bits = 8;
1378c2ecf20Sopenharmony_ci	config.val_bits = 8;
1388c2ecf20Sopenharmony_ci	config.read_flag_mask = 0x80;
1398c2ecf20Sopenharmony_ci	config.max_register = 0x3F;
1408c2ecf20Sopenharmony_ci	config.wr_table = &ds1347_access_table;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* spi setup with ds1347 in mode 3 and bits per word as 8 */
1438c2ecf20Sopenharmony_ci	spi->mode = SPI_MODE_3;
1448c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
1458c2ecf20Sopenharmony_ci	spi_setup(spi);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	map = devm_regmap_init_spi(spi, &config);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
1508c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "ds1347 regmap init spi failed\n");
1518c2ecf20Sopenharmony_ci		return PTR_ERR(map);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, map);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Disable the write protect of rtc */
1578c2ecf20Sopenharmony_ci	err = regmap_update_bits(map, DS1347_CONTROL_REG, DS1347_WP_BIT, 0);
1588c2ecf20Sopenharmony_ci	if (err)
1598c2ecf20Sopenharmony_ci		return err;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	rtc = devm_rtc_allocate_device(&spi->dev);
1628c2ecf20Sopenharmony_ci	if (IS_ERR(rtc))
1638c2ecf20Sopenharmony_ci		return PTR_ERR(rtc);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	rtc->ops = &ds1347_rtc_ops;
1668c2ecf20Sopenharmony_ci	rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
1678c2ecf20Sopenharmony_ci	rtc->range_max = RTC_TIMESTAMP_END_9999;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return rtc_register_device(rtc);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic struct spi_driver ds1347_driver = {
1738c2ecf20Sopenharmony_ci	.driver = {
1748c2ecf20Sopenharmony_ci		.name = "ds1347",
1758c2ecf20Sopenharmony_ci	},
1768c2ecf20Sopenharmony_ci	.probe = ds1347_probe,
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cimodule_spi_driver(ds1347_driver);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
1828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
1838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
184