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 32*a5771c6cSAlessandro 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 42*a5771c6cSAlessandro 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 */ 51*a5771c6cSAlessandro Zummo *data = address | 0x80; 528e12ecc2SRaphael Assenat 53*a5771c6cSAlessandro Zummo return spi_write_then_read(spi, data, 1, data, 1); 548e12ecc2SRaphael Assenat } 558e12ecc2SRaphael Assenat 56*a5771c6cSAlessandro Zummo static int max6902_read_time(struct device *dev, struct rtc_time *dt) 578e12ecc2SRaphael Assenat { 58*a5771c6cSAlessandro Zummo int err, century; 598e12ecc2SRaphael Assenat struct spi_device *spi = to_spi_device(dev); 60*a5771c6cSAlessandro Zummo unsigned char buf[8]; 618e12ecc2SRaphael Assenat 62*a5771c6cSAlessandro Zummo buf[0] = 0xbf; /* Burst read */ 63*a5771c6cSAlessandro Zummo 64*a5771c6cSAlessandro Zummo err = spi_write_then_read(spi, buf, 1, buf, 8); 65*a5771c6cSAlessandro 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 */ 70*a5771c6cSAlessandro Zummo dt->tm_sec = bcd2bin(buf[0]); 71*a5771c6cSAlessandro Zummo dt->tm_min = bcd2bin(buf[1]); 72*a5771c6cSAlessandro Zummo dt->tm_hour = bcd2bin(buf[2]); 73*a5771c6cSAlessandro Zummo dt->tm_mday = bcd2bin(buf[3]); 74*a5771c6cSAlessandro Zummo dt->tm_mon = bcd2bin(buf[4]) - 1; 75*a5771c6cSAlessandro Zummo dt->tm_wday = bcd2bin(buf[5]); 76*a5771c6cSAlessandro Zummo dt->tm_year = bcd2bin(buf[6]); 778e12ecc2SRaphael Assenat 78*a5771c6cSAlessandro Zummo /* Read century */ 79*a5771c6cSAlessandro Zummo err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]); 80*a5771c6cSAlessandro Zummo if (err != 0) 81*a5771c6cSAlessandro Zummo return err; 82*a5771c6cSAlessandro Zummo 83*a5771c6cSAlessandro Zummo century = bcd2bin(buf[0]) * 100; 848e12ecc2SRaphael Assenat 858e12ecc2SRaphael Assenat dt->tm_year += century; 868e12ecc2SRaphael Assenat dt->tm_year -= 1900; 878e12ecc2SRaphael Assenat 88*a5771c6cSAlessandro Zummo return rtc_valid_tm(dt); 898e12ecc2SRaphael Assenat } 908e12ecc2SRaphael Assenat 91*a5771c6cSAlessandro 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 1238e12ecc2SRaphael Assenat static int __devinit 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 129*a5771c6cSAlessandro Zummo spi->mode = SPI_MODE_3; 130*a5771c6cSAlessandro Zummo spi->bits_per_word = 8; 131*a5771c6cSAlessandro Zummo spi_setup(spi); 132*a5771c6cSAlessandro Zummo 133*a5771c6cSAlessandro Zummo res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); 134*a5771c6cSAlessandro Zummo if (res != 0) 135*a5771c6cSAlessandro Zummo return res; 136*a5771c6cSAlessandro Zummo 1378e12ecc2SRaphael Assenat rtc = rtc_device_register("max6902", 1388e12ecc2SRaphael Assenat &spi->dev, &max6902_rtc_ops, THIS_MODULE); 1398e12ecc2SRaphael Assenat if (IS_ERR(rtc)) 1408e12ecc2SRaphael Assenat return PTR_ERR(rtc); 1418e12ecc2SRaphael Assenat 1428e12ecc2SRaphael Assenat return 0; 1438e12ecc2SRaphael Assenat } 1448e12ecc2SRaphael Assenat 1458e12ecc2SRaphael Assenat static int __devexit max6902_remove(struct spi_device *spi) 1468e12ecc2SRaphael Assenat { 147*a5771c6cSAlessandro Zummo struct rtc_device *rtc = platform_get_drvdata(spi); 1488e12ecc2SRaphael Assenat 1498e12ecc2SRaphael Assenat rtc_device_unregister(rtc); 1508e12ecc2SRaphael Assenat return 0; 1518e12ecc2SRaphael Assenat } 1528e12ecc2SRaphael Assenat 1538e12ecc2SRaphael Assenat static struct spi_driver max6902_driver = { 1548e12ecc2SRaphael Assenat .driver = { 1555c076fceSDavid Brownell .name = "rtc-max6902", 1568e12ecc2SRaphael Assenat .bus = &spi_bus_type, 1578e12ecc2SRaphael Assenat .owner = THIS_MODULE, 1588e12ecc2SRaphael Assenat }, 1598e12ecc2SRaphael Assenat .probe = max6902_probe, 1608e12ecc2SRaphael Assenat .remove = __devexit_p(max6902_remove), 1618e12ecc2SRaphael Assenat }; 1628e12ecc2SRaphael Assenat 1638e12ecc2SRaphael Assenat static __init int max6902_init(void) 1648e12ecc2SRaphael Assenat { 1658e12ecc2SRaphael Assenat return spi_register_driver(&max6902_driver); 1668e12ecc2SRaphael Assenat } 1678e12ecc2SRaphael Assenat module_init(max6902_init); 1688e12ecc2SRaphael Assenat 1698e12ecc2SRaphael Assenat static __exit void max6902_exit(void) 1708e12ecc2SRaphael Assenat { 1718e12ecc2SRaphael Assenat spi_unregister_driver(&max6902_driver); 1728e12ecc2SRaphael Assenat } 1738e12ecc2SRaphael Assenat module_exit(max6902_exit); 1748e12ecc2SRaphael Assenat 1758e12ecc2SRaphael Assenat MODULE_DESCRIPTION ("max6902 spi RTC driver"); 1768e12ecc2SRaphael Assenat MODULE_AUTHOR ("Raphael Assenat"); 1778e12ecc2SRaphael Assenat MODULE_LICENSE ("GPL"); 178