18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for Epson RTC-9701JE
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Magnus Damm
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on rtc-max6902.c
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2006 8D Technologies inc.
108c2ecf20Sopenharmony_ci * Copyright (C) 2004 Compulab Ltd.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/rtc.h>
198c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
208c2ecf20Sopenharmony_ci#include <linux/bcd.h>
218c2ecf20Sopenharmony_ci#include <linux/delay.h>
228c2ecf20Sopenharmony_ci#include <linux/bitops.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define RSECCNT	0x00	/* Second Counter */
258c2ecf20Sopenharmony_ci#define RMINCNT	0x01	/* Minute Counter */
268c2ecf20Sopenharmony_ci#define RHRCNT	0x02	/* Hour Counter */
278c2ecf20Sopenharmony_ci#define RWKCNT	0x03	/* Week Counter */
288c2ecf20Sopenharmony_ci#define RDAYCNT	0x04	/* Day Counter */
298c2ecf20Sopenharmony_ci#define RMONCNT	0x05	/* Month Counter */
308c2ecf20Sopenharmony_ci#define RYRCNT	0x06	/* Year Counter */
318c2ecf20Sopenharmony_ci#define R100CNT	0x07	/* Y100 Counter */
328c2ecf20Sopenharmony_ci#define RMINAR	0x08	/* Minute Alarm */
338c2ecf20Sopenharmony_ci#define RHRAR	0x09	/* Hour Alarm */
348c2ecf20Sopenharmony_ci#define RWKAR	0x0a	/* Week/Day Alarm */
358c2ecf20Sopenharmony_ci#define RTIMCNT	0x0c	/* Interval Timer */
368c2ecf20Sopenharmony_ci#define REXT	0x0d	/* Extension Register */
378c2ecf20Sopenharmony_ci#define RFLAG	0x0e	/* RTC Flag Register */
388c2ecf20Sopenharmony_ci#define RCR	0x0f	/* RTC Control Register */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int write_reg(struct device *dev, int address, unsigned char data)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
438c2ecf20Sopenharmony_ci	unsigned char buf[2];
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	buf[0] = address & 0x7f;
468c2ecf20Sopenharmony_ci	buf[1] = data;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return spi_write(spi, buf, ARRAY_SIZE(buf));
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int read_regs(struct device *dev, unsigned char *regs, int no_regs)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
548c2ecf20Sopenharmony_ci	u8 txbuf[1], rxbuf[1];
558c2ecf20Sopenharmony_ci	int k, ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	for (k = 0; ret == 0 && k < no_regs; k++) {
608c2ecf20Sopenharmony_ci		txbuf[0] = 0x80 | regs[k];
618c2ecf20Sopenharmony_ci		ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1);
628c2ecf20Sopenharmony_ci		regs[k] = rxbuf[0];
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return ret;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int r9701_get_datetime(struct device *dev, struct rtc_time *dt)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	int ret;
718c2ecf20Sopenharmony_ci	unsigned char buf[] = { RSECCNT, RMINCNT, RHRCNT,
728c2ecf20Sopenharmony_ci				RDAYCNT, RMONCNT, RYRCNT };
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	ret = read_regs(dev, buf, ARRAY_SIZE(buf));
758c2ecf20Sopenharmony_ci	if (ret)
768c2ecf20Sopenharmony_ci		return ret;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	dt->tm_sec = bcd2bin(buf[0]); /* RSECCNT */
798c2ecf20Sopenharmony_ci	dt->tm_min = bcd2bin(buf[1]); /* RMINCNT */
808c2ecf20Sopenharmony_ci	dt->tm_hour = bcd2bin(buf[2]); /* RHRCNT */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	dt->tm_mday = bcd2bin(buf[3]); /* RDAYCNT */
838c2ecf20Sopenharmony_ci	dt->tm_mon = bcd2bin(buf[4]) - 1; /* RMONCNT */
848c2ecf20Sopenharmony_ci	dt->tm_year = bcd2bin(buf[5]) + 100; /* RYRCNT */
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int r9701_set_datetime(struct device *dev, struct rtc_time *dt)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = write_reg(dev, RHRCNT, bin2bcd(dt->tm_hour));
948c2ecf20Sopenharmony_ci	ret = ret ? ret : write_reg(dev, RMINCNT, bin2bcd(dt->tm_min));
958c2ecf20Sopenharmony_ci	ret = ret ? ret : write_reg(dev, RSECCNT, bin2bcd(dt->tm_sec));
968c2ecf20Sopenharmony_ci	ret = ret ? ret : write_reg(dev, RDAYCNT, bin2bcd(dt->tm_mday));
978c2ecf20Sopenharmony_ci	ret = ret ? ret : write_reg(dev, RMONCNT, bin2bcd(dt->tm_mon + 1));
988c2ecf20Sopenharmony_ci	ret = ret ? ret : write_reg(dev, RYRCNT, bin2bcd(dt->tm_year - 100));
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return ret;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic const struct rtc_class_ops r9701_rtc_ops = {
1048c2ecf20Sopenharmony_ci	.read_time	= r9701_get_datetime,
1058c2ecf20Sopenharmony_ci	.set_time	= r9701_set_datetime,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int r9701_probe(struct spi_device *spi)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
1118c2ecf20Sopenharmony_ci	unsigned char tmp;
1128c2ecf20Sopenharmony_ci	int res;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	tmp = R100CNT;
1158c2ecf20Sopenharmony_ci	res = read_regs(&spi->dev, &tmp, 1);
1168c2ecf20Sopenharmony_ci	if (res || tmp != 0x20) {
1178c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "cannot read RTC register\n");
1188c2ecf20Sopenharmony_ci		return -ENODEV;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	rtc = devm_rtc_allocate_device(&spi->dev);
1228c2ecf20Sopenharmony_ci	if (IS_ERR(rtc))
1238c2ecf20Sopenharmony_ci		return PTR_ERR(rtc);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, rtc);
1268c2ecf20Sopenharmony_ci	rtc->ops = &r9701_rtc_ops;
1278c2ecf20Sopenharmony_ci	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
1288c2ecf20Sopenharmony_ci	rtc->range_max = RTC_TIMESTAMP_END_2099;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	return rtc_register_device(rtc);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic struct spi_driver r9701_driver = {
1348c2ecf20Sopenharmony_ci	.driver = {
1358c2ecf20Sopenharmony_ci		.name	= "rtc-r9701",
1368c2ecf20Sopenharmony_ci	},
1378c2ecf20Sopenharmony_ci	.probe	= r9701_probe,
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cimodule_spi_driver(r9701_driver);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("r9701 spi RTC driver");
1438c2ecf20Sopenharmony_ciMODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1448c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1458c2ecf20Sopenharmony_ciMODULE_ALIAS("spi:rtc-r9701");
146