1 /* 2 * rtc-ds1390.c -- driver for DS1390/93/94 3 * 4 * Copyright (C) 2008 Mercury IMC Ltd 5 * Written by Mark Jackson <mpfj@mimc.co.uk> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * NOTE : Currently this driver only supports the bare minimum for read 12 * and write the RTC. The extra features provided by the chip family 13 * (alarms, trickle charger, different control registers) are unavailable. 14 */ 15 16 #include <linux/platform_device.h> 17 #include <linux/rtc.h> 18 #include <linux/spi/spi.h> 19 #include <linux/bcd.h> 20 21 #define DS1390_REG_100THS 0x00 22 #define DS1390_REG_SECONDS 0x01 23 #define DS1390_REG_MINUTES 0x02 24 #define DS1390_REG_HOURS 0x03 25 #define DS1390_REG_DAY 0x04 26 #define DS1390_REG_DATE 0x05 27 #define DS1390_REG_MONTH_CENT 0x06 28 #define DS1390_REG_YEAR 0x07 29 30 #define DS1390_REG_ALARM_100THS 0x08 31 #define DS1390_REG_ALARM_SECONDS 0x09 32 #define DS1390_REG_ALARM_MINUTES 0x0A 33 #define DS1390_REG_ALARM_HOURS 0x0B 34 #define DS1390_REG_ALARM_DAY_DATE 0x0C 35 36 #define DS1390_REG_CONTROL 0x0D 37 #define DS1390_REG_STATUS 0x0E 38 #define DS1390_REG_TRICKLE 0x0F 39 40 struct ds1390 { 41 struct rtc_device *rtc; 42 u8 txrx_buf[9]; /* cmd + 8 registers */ 43 }; 44 45 static void ds1390_set_reg(struct device *dev, unsigned char address, 46 unsigned char data) 47 { 48 struct spi_device *spi = to_spi_device(dev); 49 struct ds1390 *chip = dev_get_drvdata(dev); 50 51 /* Set MSB to indicate write */ 52 chip->txrx_buf[0] = address | 0x80; 53 chip->txrx_buf[1] = data; 54 55 /* do the i/o */ 56 spi_write_then_read(spi, chip->txrx_buf, 2, NULL, 0); 57 } 58 59 static int ds1390_get_reg(struct device *dev, unsigned char address, 60 unsigned char *data) 61 { 62 struct spi_device *spi = to_spi_device(dev); 63 struct ds1390 *chip = dev_get_drvdata(dev); 64 int status; 65 66 if (!data) 67 return -EINVAL; 68 69 /* Clear MSB to indicate read */ 70 chip->txrx_buf[0] = address & 0x7f; 71 /* do the i/o */ 72 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); 73 if (status != 0) 74 return status; 75 76 *data = chip->txrx_buf[1]; 77 78 return 0; 79 } 80 81 static int ds1390_get_datetime(struct device *dev, struct rtc_time *dt) 82 { 83 struct spi_device *spi = to_spi_device(dev); 84 struct ds1390 *chip = dev_get_drvdata(dev); 85 int status; 86 87 /* build the message */ 88 chip->txrx_buf[0] = DS1390_REG_SECONDS; 89 90 /* do the i/o */ 91 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); 92 if (status != 0) 93 return status; 94 95 /* The chip sends data in this order: 96 * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ 97 dt->tm_sec = bcd2bin(chip->txrx_buf[0]); 98 dt->tm_min = bcd2bin(chip->txrx_buf[1]); 99 dt->tm_hour = bcd2bin(chip->txrx_buf[2]); 100 dt->tm_wday = bcd2bin(chip->txrx_buf[3]); 101 dt->tm_mday = bcd2bin(chip->txrx_buf[4]); 102 /* mask off century bit */ 103 dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; 104 /* adjust for century bit */ 105 dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); 106 107 return rtc_valid_tm(dt); 108 } 109 110 static int ds1390_set_datetime(struct device *dev, struct rtc_time *dt) 111 { 112 struct spi_device *spi = to_spi_device(dev); 113 struct ds1390 *chip = dev_get_drvdata(dev); 114 115 /* build the message */ 116 chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; 117 chip->txrx_buf[1] = bin2bcd(dt->tm_sec); 118 chip->txrx_buf[2] = bin2bcd(dt->tm_min); 119 chip->txrx_buf[3] = bin2bcd(dt->tm_hour); 120 chip->txrx_buf[4] = bin2bcd(dt->tm_wday); 121 chip->txrx_buf[5] = bin2bcd(dt->tm_mday); 122 chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | 123 ((dt->tm_year > 99) ? 0x80 : 0x00); 124 chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); 125 126 /* do the i/o */ 127 return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); 128 } 129 130 static int ds1390_read_time(struct device *dev, struct rtc_time *tm) 131 { 132 return ds1390_get_datetime(dev, tm); 133 } 134 135 static int ds1390_set_time(struct device *dev, struct rtc_time *tm) 136 { 137 return ds1390_set_datetime(dev, tm); 138 } 139 140 static const struct rtc_class_ops ds1390_rtc_ops = { 141 .read_time = ds1390_read_time, 142 .set_time = ds1390_set_time, 143 }; 144 145 static int __devinit ds1390_probe(struct spi_device *spi) 146 { 147 struct rtc_device *rtc; 148 unsigned char tmp; 149 struct ds1390 *chip; 150 int res; 151 152 printk(KERN_DEBUG "DS1390 SPI RTC driver\n"); 153 154 rtc = rtc_device_register("ds1390", 155 &spi->dev, &ds1390_rtc_ops, THIS_MODULE); 156 if (IS_ERR(rtc)) { 157 printk(KERN_ALERT "RTC : unable to register device\n"); 158 return PTR_ERR(rtc); 159 } 160 161 spi->mode = SPI_MODE_3; 162 spi->bits_per_word = 8; 163 spi_setup(spi); 164 165 chip = kzalloc(sizeof *chip, GFP_KERNEL); 166 if (!chip) { 167 printk(KERN_ALERT "RTC : unable to allocate device memory\n"); 168 rtc_device_unregister(rtc); 169 return -ENOMEM; 170 } 171 chip->rtc = rtc; 172 dev_set_drvdata(&spi->dev, chip); 173 174 res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp); 175 if (res) { 176 printk(KERN_ALERT "RTC : unable to read device\n"); 177 rtc_device_unregister(rtc); 178 return res; 179 } 180 181 return 0; 182 } 183 184 static int __devexit ds1390_remove(struct spi_device *spi) 185 { 186 struct ds1390 *chip = platform_get_drvdata(spi); 187 struct rtc_device *rtc = chip->rtc; 188 189 if (rtc) 190 rtc_device_unregister(rtc); 191 192 kfree(chip); 193 194 return 0; 195 } 196 197 static struct spi_driver ds1390_driver = { 198 .driver = { 199 .name = "rtc-ds1390", 200 .owner = THIS_MODULE, 201 }, 202 .probe = ds1390_probe, 203 .remove = __devexit_p(ds1390_remove), 204 }; 205 206 static __init int ds1390_init(void) 207 { 208 return spi_register_driver(&ds1390_driver); 209 } 210 module_init(ds1390_init); 211 212 static __exit void ds1390_exit(void) 213 { 214 spi_unregister_driver(&ds1390_driver); 215 } 216 module_exit(ds1390_exit); 217 218 MODULE_DESCRIPTION("DS1390/93/94 SPI RTC driver"); 219 MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); 220 MODULE_LICENSE("GPL"); 221