18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* rtc-max6916.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Driver for MAXIM max6916 Low Current, SPI Compatible 58c2ecf20Sopenharmony_ci * Real Time Clock 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author : Venkat Prashanth B U <venkat.prashanth2498@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 188c2ecf20Sopenharmony_ci/* Registers in max6916 rtc */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define MAX6916_SECONDS_REG 0x01 218c2ecf20Sopenharmony_ci#define MAX6916_MINUTES_REG 0x02 228c2ecf20Sopenharmony_ci#define MAX6916_HOURS_REG 0x03 238c2ecf20Sopenharmony_ci#define MAX6916_DATE_REG 0x04 248c2ecf20Sopenharmony_ci#define MAX6916_MONTH_REG 0x05 258c2ecf20Sopenharmony_ci#define MAX6916_DAY_REG 0x06 268c2ecf20Sopenharmony_ci#define MAX6916_YEAR_REG 0x07 278c2ecf20Sopenharmony_ci#define MAX6916_CONTROL_REG 0x08 288c2ecf20Sopenharmony_ci#define MAX6916_STATUS_REG 0x0C 298c2ecf20Sopenharmony_ci#define MAX6916_CLOCK_BURST 0x3F 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic int max6916_read_reg(struct device *dev, unsigned char address, 328c2ecf20Sopenharmony_ci unsigned char *data) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci *data = address | 0x80; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci return spi_write_then_read(spi, data, 1, data, 1); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic int max6916_write_reg(struct device *dev, unsigned char address, 428c2ecf20Sopenharmony_ci unsigned char data) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 458c2ecf20Sopenharmony_ci unsigned char buf[2]; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci buf[0] = address & 0x7F; 488c2ecf20Sopenharmony_ci buf[1] = data; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return spi_write_then_read(spi, buf, 2, NULL, 0); 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int max6916_read_time(struct device *dev, struct rtc_time *dt) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 568c2ecf20Sopenharmony_ci int err; 578c2ecf20Sopenharmony_ci unsigned char buf[8]; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci buf[0] = MAX6916_CLOCK_BURST | 0x80; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci err = spi_write_then_read(spi, buf, 1, buf, 8); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (err) 648c2ecf20Sopenharmony_ci return err; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci dt->tm_sec = bcd2bin(buf[0]); 678c2ecf20Sopenharmony_ci dt->tm_min = bcd2bin(buf[1]); 688c2ecf20Sopenharmony_ci dt->tm_hour = bcd2bin(buf[2] & 0x3F); 698c2ecf20Sopenharmony_ci dt->tm_mday = bcd2bin(buf[3]); 708c2ecf20Sopenharmony_ci dt->tm_mon = bcd2bin(buf[4]) - 1; 718c2ecf20Sopenharmony_ci dt->tm_wday = bcd2bin(buf[5]) - 1; 728c2ecf20Sopenharmony_ci dt->tm_year = bcd2bin(buf[6]) + 100; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return 0; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic int max6916_set_time(struct device *dev, struct rtc_time *dt) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 808c2ecf20Sopenharmony_ci unsigned char buf[9]; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci if (dt->tm_year < 100 || dt->tm_year > 199) { 838c2ecf20Sopenharmony_ci dev_err(&spi->dev, "Year must be between 2000 and 2099. It's %d.\n", 848c2ecf20Sopenharmony_ci dt->tm_year + 1900); 858c2ecf20Sopenharmony_ci return -EINVAL; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci buf[0] = MAX6916_CLOCK_BURST & 0x7F; 898c2ecf20Sopenharmony_ci buf[1] = bin2bcd(dt->tm_sec); 908c2ecf20Sopenharmony_ci buf[2] = bin2bcd(dt->tm_min); 918c2ecf20Sopenharmony_ci buf[3] = (bin2bcd(dt->tm_hour) & 0X3F); 928c2ecf20Sopenharmony_ci buf[4] = bin2bcd(dt->tm_mday); 938c2ecf20Sopenharmony_ci buf[5] = bin2bcd(dt->tm_mon + 1); 948c2ecf20Sopenharmony_ci buf[6] = bin2bcd(dt->tm_wday + 1); 958c2ecf20Sopenharmony_ci buf[7] = bin2bcd(dt->tm_year % 100); 968c2ecf20Sopenharmony_ci buf[8] = bin2bcd(0x00); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* write the rtc settings */ 998c2ecf20Sopenharmony_ci return spi_write_then_read(spi, buf, 9, NULL, 0); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic const struct rtc_class_ops max6916_rtc_ops = { 1038c2ecf20Sopenharmony_ci .read_time = max6916_read_time, 1048c2ecf20Sopenharmony_ci .set_time = max6916_set_time, 1058c2ecf20Sopenharmony_ci}; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int max6916_probe(struct spi_device *spi) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct rtc_device *rtc; 1108c2ecf20Sopenharmony_ci unsigned char data; 1118c2ecf20Sopenharmony_ci int res; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* spi setup with max6916 in mode 3 and bits per word as 8 */ 1148c2ecf20Sopenharmony_ci spi->mode = SPI_MODE_3; 1158c2ecf20Sopenharmony_ci spi->bits_per_word = 8; 1168c2ecf20Sopenharmony_ci spi_setup(spi); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* RTC Settings */ 1198c2ecf20Sopenharmony_ci res = max6916_read_reg(&spi->dev, MAX6916_SECONDS_REG, &data); 1208c2ecf20Sopenharmony_ci if (res) 1218c2ecf20Sopenharmony_ci return res; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* Disable the write protect of rtc */ 1248c2ecf20Sopenharmony_ci max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data); 1258c2ecf20Sopenharmony_ci data = data & ~(1 << 7); 1268c2ecf20Sopenharmony_ci max6916_write_reg(&spi->dev, MAX6916_CONTROL_REG, data); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /*Enable oscillator,disable oscillator stop flag, glitch filter*/ 1298c2ecf20Sopenharmony_ci max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data); 1308c2ecf20Sopenharmony_ci data = data & 0x1B; 1318c2ecf20Sopenharmony_ci max6916_write_reg(&spi->dev, MAX6916_STATUS_REG, data); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* display the settings */ 1348c2ecf20Sopenharmony_ci max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data); 1358c2ecf20Sopenharmony_ci dev_info(&spi->dev, "MAX6916 RTC CTRL Reg = 0x%02x\n", data); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data); 1388c2ecf20Sopenharmony_ci dev_info(&spi->dev, "MAX6916 RTC Status Reg = 0x%02x\n", data); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci rtc = devm_rtc_device_register(&spi->dev, "max6916", 1418c2ecf20Sopenharmony_ci &max6916_rtc_ops, THIS_MODULE); 1428c2ecf20Sopenharmony_ci if (IS_ERR(rtc)) 1438c2ecf20Sopenharmony_ci return PTR_ERR(rtc); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci spi_set_drvdata(spi, rtc); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci return 0; 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic struct spi_driver max6916_driver = { 1518c2ecf20Sopenharmony_ci .driver = { 1528c2ecf20Sopenharmony_ci .name = "max6916", 1538c2ecf20Sopenharmony_ci }, 1548c2ecf20Sopenharmony_ci .probe = max6916_probe, 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_cimodule_spi_driver(max6916_driver); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAX6916 SPI RTC DRIVER"); 1598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Venkat Prashanth B U <venkat.prashanth2498@gmail.com>"); 1608c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 161