18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * rtc-ds1390.c -- driver for the Dallas/Maxim DS1390/93/94 SPI RTC 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Mercury IMC Ltd 68c2ecf20Sopenharmony_ci * Written by Mark Jackson <mpfj@mimc.co.uk> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * NOTE: Currently this driver only supports the bare minimum for read 98c2ecf20Sopenharmony_ci * and write the RTC. The extra features provided by the chip family 108c2ecf20Sopenharmony_ci * (alarms, trickle charger, different control registers) are unavailable. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 168c2ecf20Sopenharmony_ci#include <linux/rtc.h> 178c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 188c2ecf20Sopenharmony_ci#include <linux/bcd.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/of.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define DS1390_REG_100THS 0x00 238c2ecf20Sopenharmony_ci#define DS1390_REG_SECONDS 0x01 248c2ecf20Sopenharmony_ci#define DS1390_REG_MINUTES 0x02 258c2ecf20Sopenharmony_ci#define DS1390_REG_HOURS 0x03 268c2ecf20Sopenharmony_ci#define DS1390_REG_DAY 0x04 278c2ecf20Sopenharmony_ci#define DS1390_REG_DATE 0x05 288c2ecf20Sopenharmony_ci#define DS1390_REG_MONTH_CENT 0x06 298c2ecf20Sopenharmony_ci#define DS1390_REG_YEAR 0x07 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define DS1390_REG_ALARM_100THS 0x08 328c2ecf20Sopenharmony_ci#define DS1390_REG_ALARM_SECONDS 0x09 338c2ecf20Sopenharmony_ci#define DS1390_REG_ALARM_MINUTES 0x0A 348c2ecf20Sopenharmony_ci#define DS1390_REG_ALARM_HOURS 0x0B 358c2ecf20Sopenharmony_ci#define DS1390_REG_ALARM_DAY_DATE 0x0C 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define DS1390_REG_CONTROL 0x0D 388c2ecf20Sopenharmony_ci#define DS1390_REG_STATUS 0x0E 398c2ecf20Sopenharmony_ci#define DS1390_REG_TRICKLE 0x0F 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_ENABLE 0xA0 428c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_250_OHM 0x01 438c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_2K_OHM 0x02 448c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_4K_OHM 0x03 458c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_NO_DIODE 0x04 468c2ecf20Sopenharmony_ci#define DS1390_TRICKLE_CHARGER_DIODE 0x08 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistruct ds1390 { 498c2ecf20Sopenharmony_ci struct rtc_device *rtc; 508c2ecf20Sopenharmony_ci u8 txrx_buf[9]; /* cmd + 8 registers */ 518c2ecf20Sopenharmony_ci}; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic void ds1390_set_reg(struct device *dev, unsigned char address, 548c2ecf20Sopenharmony_ci unsigned char data) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 578c2ecf20Sopenharmony_ci unsigned char buf[2]; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci /* MSB must be '1' to write */ 608c2ecf20Sopenharmony_ci buf[0] = address | 0x80; 618c2ecf20Sopenharmony_ci buf[1] = data; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci spi_write(spi, buf, 2); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic int ds1390_get_reg(struct device *dev, unsigned char address, 678c2ecf20Sopenharmony_ci unsigned char *data) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 708c2ecf20Sopenharmony_ci struct ds1390 *chip = dev_get_drvdata(dev); 718c2ecf20Sopenharmony_ci int status; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (!data) 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Clear MSB to indicate read */ 778c2ecf20Sopenharmony_ci chip->txrx_buf[0] = address & 0x7f; 788c2ecf20Sopenharmony_ci /* do the i/o */ 798c2ecf20Sopenharmony_ci status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); 808c2ecf20Sopenharmony_ci if (status != 0) 818c2ecf20Sopenharmony_ci return status; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci *data = chip->txrx_buf[0]; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic void ds1390_trickle_of_init(struct spi_device *spi) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci u32 ohms = 0; 918c2ecf20Sopenharmony_ci u8 value; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (of_property_read_u32(spi->dev.of_node, "trickle-resistor-ohms", 948c2ecf20Sopenharmony_ci &ohms)) 958c2ecf20Sopenharmony_ci goto out; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci /* Enable charger */ 988c2ecf20Sopenharmony_ci value = DS1390_TRICKLE_CHARGER_ENABLE; 998c2ecf20Sopenharmony_ci if (of_property_read_bool(spi->dev.of_node, "trickle-diode-disable")) 1008c2ecf20Sopenharmony_ci value |= DS1390_TRICKLE_CHARGER_NO_DIODE; 1018c2ecf20Sopenharmony_ci else 1028c2ecf20Sopenharmony_ci value |= DS1390_TRICKLE_CHARGER_DIODE; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* Resistor select */ 1058c2ecf20Sopenharmony_ci switch (ohms) { 1068c2ecf20Sopenharmony_ci case 250: 1078c2ecf20Sopenharmony_ci value |= DS1390_TRICKLE_CHARGER_250_OHM; 1088c2ecf20Sopenharmony_ci break; 1098c2ecf20Sopenharmony_ci case 2000: 1108c2ecf20Sopenharmony_ci value |= DS1390_TRICKLE_CHARGER_2K_OHM; 1118c2ecf20Sopenharmony_ci break; 1128c2ecf20Sopenharmony_ci case 4000: 1138c2ecf20Sopenharmony_ci value |= DS1390_TRICKLE_CHARGER_4K_OHM; 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci default: 1168c2ecf20Sopenharmony_ci dev_warn(&spi->dev, 1178c2ecf20Sopenharmony_ci "Unsupported ohm value %02ux in dt\n", ohms); 1188c2ecf20Sopenharmony_ci return; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci ds1390_set_reg(&spi->dev, DS1390_REG_TRICKLE, value); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ciout: 1248c2ecf20Sopenharmony_ci return; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic int ds1390_read_time(struct device *dev, struct rtc_time *dt) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 1308c2ecf20Sopenharmony_ci struct ds1390 *chip = dev_get_drvdata(dev); 1318c2ecf20Sopenharmony_ci int status; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* build the message */ 1348c2ecf20Sopenharmony_ci chip->txrx_buf[0] = DS1390_REG_SECONDS; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* do the i/o */ 1378c2ecf20Sopenharmony_ci status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); 1388c2ecf20Sopenharmony_ci if (status != 0) 1398c2ecf20Sopenharmony_ci return status; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci /* The chip sends data in this order: 1428c2ecf20Sopenharmony_ci * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ 1438c2ecf20Sopenharmony_ci dt->tm_sec = bcd2bin(chip->txrx_buf[0]); 1448c2ecf20Sopenharmony_ci dt->tm_min = bcd2bin(chip->txrx_buf[1]); 1458c2ecf20Sopenharmony_ci dt->tm_hour = bcd2bin(chip->txrx_buf[2]); 1468c2ecf20Sopenharmony_ci dt->tm_wday = bcd2bin(chip->txrx_buf[3]); 1478c2ecf20Sopenharmony_ci dt->tm_mday = bcd2bin(chip->txrx_buf[4]); 1488c2ecf20Sopenharmony_ci /* mask off century bit */ 1498c2ecf20Sopenharmony_ci dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; 1508c2ecf20Sopenharmony_ci /* adjust for century bit */ 1518c2ecf20Sopenharmony_ci dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic int ds1390_set_time(struct device *dev, struct rtc_time *dt) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 1598c2ecf20Sopenharmony_ci struct ds1390 *chip = dev_get_drvdata(dev); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* build the message */ 1628c2ecf20Sopenharmony_ci chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; 1638c2ecf20Sopenharmony_ci chip->txrx_buf[1] = bin2bcd(dt->tm_sec); 1648c2ecf20Sopenharmony_ci chip->txrx_buf[2] = bin2bcd(dt->tm_min); 1658c2ecf20Sopenharmony_ci chip->txrx_buf[3] = bin2bcd(dt->tm_hour); 1668c2ecf20Sopenharmony_ci chip->txrx_buf[4] = bin2bcd(dt->tm_wday); 1678c2ecf20Sopenharmony_ci chip->txrx_buf[5] = bin2bcd(dt->tm_mday); 1688c2ecf20Sopenharmony_ci chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | 1698c2ecf20Sopenharmony_ci ((dt->tm_year > 99) ? 0x80 : 0x00); 1708c2ecf20Sopenharmony_ci chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* do the i/o */ 1738c2ecf20Sopenharmony_ci return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic const struct rtc_class_ops ds1390_rtc_ops = { 1778c2ecf20Sopenharmony_ci .read_time = ds1390_read_time, 1788c2ecf20Sopenharmony_ci .set_time = ds1390_set_time, 1798c2ecf20Sopenharmony_ci}; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic int ds1390_probe(struct spi_device *spi) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci unsigned char tmp; 1848c2ecf20Sopenharmony_ci struct ds1390 *chip; 1858c2ecf20Sopenharmony_ci int res; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci spi->mode = SPI_MODE_3; 1888c2ecf20Sopenharmony_ci spi->bits_per_word = 8; 1898c2ecf20Sopenharmony_ci spi_setup(spi); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL); 1928c2ecf20Sopenharmony_ci if (!chip) 1938c2ecf20Sopenharmony_ci return -ENOMEM; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci spi_set_drvdata(spi, chip); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp); 1988c2ecf20Sopenharmony_ci if (res != 0) { 1998c2ecf20Sopenharmony_ci dev_err(&spi->dev, "unable to read device\n"); 2008c2ecf20Sopenharmony_ci return res; 2018c2ecf20Sopenharmony_ci } 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci if (spi->dev.of_node) 2048c2ecf20Sopenharmony_ci ds1390_trickle_of_init(spi); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390", 2078c2ecf20Sopenharmony_ci &ds1390_rtc_ops, THIS_MODULE); 2088c2ecf20Sopenharmony_ci if (IS_ERR(chip->rtc)) { 2098c2ecf20Sopenharmony_ci dev_err(&spi->dev, "unable to register device\n"); 2108c2ecf20Sopenharmony_ci res = PTR_ERR(chip->rtc); 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci return res; 2148c2ecf20Sopenharmony_ci} 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_cistatic const struct of_device_id ds1390_of_match[] = { 2178c2ecf20Sopenharmony_ci { .compatible = "dallas,ds1390" }, 2188c2ecf20Sopenharmony_ci {} 2198c2ecf20Sopenharmony_ci}; 2208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ds1390_of_match); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic struct spi_driver ds1390_driver = { 2238c2ecf20Sopenharmony_ci .driver = { 2248c2ecf20Sopenharmony_ci .name = "rtc-ds1390", 2258c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(ds1390_of_match), 2268c2ecf20Sopenharmony_ci }, 2278c2ecf20Sopenharmony_ci .probe = ds1390_probe, 2288c2ecf20Sopenharmony_ci}; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cimodule_spi_driver(ds1390_driver); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver"); 2338c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); 2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2358c2ecf20Sopenharmony_ciMODULE_ALIAS("spi:rtc-ds1390"); 236