1 /* drivers/char/max6902.c 2 * 3 * Copyright (C) 2006 8D Technologies inc. 4 * Copyright (C) 2004 Compulab Ltd. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * Driver for MAX6902 spi RTC 11 * 12 * Changelog: 13 * 14 * 24-May-2006: Raphael Assenat <raph@8d.com> 15 * - Major rework 16 * Converted to rtc_device and uses the SPI layer. 17 * 18 * ??-???-2005: Someone at Compulab 19 * - Initial driver creation. 20 */ 21 22 #include <linux/config.h> 23 #include <linux/module.h> 24 #include <linux/version.h> 25 26 #include <linux/kernel.h> 27 #include <linux/platform_device.h> 28 #include <linux/init.h> 29 #include <linux/rtc.h> 30 #include <linux/spi/spi.h> 31 #include <linux/bcd.h> 32 #include <linux/delay.h> 33 34 #define MAX6902_REG_SECONDS 0x01 35 #define MAX6902_REG_MINUTES 0x03 36 #define MAX6902_REG_HOURS 0x05 37 #define MAX6902_REG_DATE 0x07 38 #define MAX6902_REG_MONTH 0x09 39 #define MAX6902_REG_DAY 0x0B 40 #define MAX6902_REG_YEAR 0x0D 41 #define MAX6902_REG_CONTROL 0x0F 42 #define MAX6902_REG_CENTURY 0x13 43 44 #undef MAX6902_DEBUG 45 46 struct max6902 { 47 struct rtc_device *rtc; 48 u8 buf[9]; /* Burst read cmd + 8 registers */ 49 u8 tx_buf[2]; 50 u8 rx_buf[2]; 51 }; 52 53 static void max6902_set_reg(struct device *dev, unsigned char address, 54 unsigned char data) 55 { 56 struct spi_device *spi = to_spi_device(dev); 57 unsigned char buf[2]; 58 59 /* MSB must be '0' to write */ 60 buf[0] = address & 0x7f; 61 buf[1] = data; 62 63 spi_write(spi, buf, 2); 64 } 65 66 static int max6902_get_reg(struct device *dev, unsigned char address, 67 unsigned char *data) 68 { 69 struct spi_device *spi = to_spi_device(dev); 70 struct max6902 *chip = dev_get_drvdata(dev); 71 struct spi_message message; 72 struct spi_transfer xfer; 73 int status; 74 75 if (!data) 76 return -EINVAL; 77 78 /* Build our spi message */ 79 spi_message_init(&message); 80 memset(&xfer, 0, sizeof(xfer)); 81 xfer.len = 2; 82 /* Can tx_buf and rx_buf be equal? The doc in spi.h is not sure... */ 83 xfer.tx_buf = chip->tx_buf; 84 xfer.rx_buf = chip->rx_buf; 85 86 /* Set MSB to indicate read */ 87 chip->tx_buf[0] = address | 0x80; 88 89 spi_message_add_tail(&xfer, &message); 90 91 /* do the i/o */ 92 status = spi_sync(spi, &message); 93 if (status == 0) 94 status = message.status; 95 else 96 return status; 97 98 *data = chip->rx_buf[1]; 99 100 return status; 101 } 102 103 static int max6902_get_datetime(struct device *dev, struct rtc_time *dt) 104 { 105 unsigned char tmp; 106 int century; 107 int err; 108 struct spi_device *spi = to_spi_device(dev); 109 struct max6902 *chip = dev_get_drvdata(dev); 110 struct spi_message message; 111 struct spi_transfer xfer; 112 int status; 113 114 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp); 115 if (err) 116 return err; 117 118 /* build the message */ 119 spi_message_init(&message); 120 memset(&xfer, 0, sizeof(xfer)); 121 xfer.len = 1 + 7; /* Burst read command + 7 registers */ 122 xfer.tx_buf = chip->buf; 123 xfer.rx_buf = chip->buf; 124 chip->buf[0] = 0xbf; /* Burst read */ 125 spi_message_add_tail(&xfer, &message); 126 127 /* do the i/o */ 128 status = spi_sync(spi, &message); 129 if (status == 0) 130 status = message.status; 131 else 132 return status; 133 134 /* The chip sends data in this order: 135 * Seconds, Minutes, Hours, Date, Month, Day, Year */ 136 dt->tm_sec = BCD2BIN(chip->buf[1]); 137 dt->tm_min = BCD2BIN(chip->buf[2]); 138 dt->tm_hour = BCD2BIN(chip->buf[3]); 139 dt->tm_mday = BCD2BIN(chip->buf[4]); 140 dt->tm_mon = BCD2BIN(chip->buf[5] - 1); 141 dt->tm_wday = BCD2BIN(chip->buf[6]); 142 dt->tm_year = BCD2BIN(chip->buf[7]); 143 144 century = BCD2BIN(tmp) * 100; 145 146 dt->tm_year += century; 147 dt->tm_year -= 1900; 148 149 #ifdef MAX6902_DEBUG 150 printk("\n%s : Read RTC values\n",__FUNCTION__); 151 printk("tm_hour: %i\n",dt->tm_hour); 152 printk("tm_min : %i\n",dt->tm_min); 153 printk("tm_sec : %i\n",dt->tm_sec); 154 printk("tm_year: %i\n",dt->tm_year); 155 printk("tm_mon : %i\n",dt->tm_mon); 156 printk("tm_mday: %i\n",dt->tm_mday); 157 printk("tm_wday: %i\n",dt->tm_wday); 158 #endif 159 160 return 0; 161 } 162 163 static int max6902_set_datetime(struct device *dev, struct rtc_time *dt) 164 { 165 dt->tm_year = dt->tm_year+1900; 166 167 #ifdef MAX6902_DEBUG 168 printk("\n%s : Setting RTC values\n",__FUNCTION__); 169 printk("tm_sec : %i\n",dt->tm_sec); 170 printk("tm_min : %i\n",dt->tm_min); 171 printk("tm_hour: %i\n",dt->tm_hour); 172 printk("tm_mday: %i\n",dt->tm_mday); 173 printk("tm_wday: %i\n",dt->tm_wday); 174 printk("tm_year: %i\n",dt->tm_year); 175 #endif 176 177 /* Remove write protection */ 178 max6902_set_reg(dev, 0xF, 0); 179 180 max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec)); 181 max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min)); 182 max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour)); 183 184 max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday)); 185 max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1)); 186 max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday)); 187 max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100)); 188 max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100)); 189 190 /* Compulab used a delay here. However, the datasheet 191 * does not mention a delay being required anywhere... */ 192 /* delay(2000); */ 193 194 /* Write protect */ 195 max6902_set_reg(dev, 0xF, 0x80); 196 197 return 0; 198 } 199 200 static int max6902_read_time(struct device *dev, struct rtc_time *tm) 201 { 202 return max6902_get_datetime(dev, tm); 203 } 204 205 static int max6902_set_time(struct device *dev, struct rtc_time *tm) 206 { 207 return max6902_set_datetime(dev, tm); 208 } 209 210 static struct rtc_class_ops max6902_rtc_ops = { 211 .read_time = max6902_read_time, 212 .set_time = max6902_set_time, 213 }; 214 215 static int __devinit max6902_probe(struct spi_device *spi) 216 { 217 struct rtc_device *rtc; 218 unsigned char tmp; 219 struct max6902 *chip; 220 int res; 221 222 rtc = rtc_device_register("max6902", 223 &spi->dev, &max6902_rtc_ops, THIS_MODULE); 224 if (IS_ERR(rtc)) 225 return PTR_ERR(rtc); 226 227 spi->mode = SPI_MODE_3; 228 spi->bits_per_word = 8; 229 spi_setup(spi); 230 231 chip = kzalloc(sizeof *chip, GFP_KERNEL); 232 if (!chip) { 233 rtc_device_unregister(rtc); 234 return -ENOMEM; 235 } 236 chip->rtc = rtc; 237 dev_set_drvdata(&spi->dev, chip); 238 239 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); 240 if (res) { 241 rtc_device_unregister(rtc); 242 return res; 243 } 244 245 return 0; 246 } 247 248 static int __devexit max6902_remove(struct spi_device *spi) 249 { 250 struct max6902 *chip = platform_get_drvdata(spi); 251 struct rtc_device *rtc = chip->rtc; 252 253 if (rtc) 254 rtc_device_unregister(rtc); 255 256 kfree(chip); 257 258 return 0; 259 } 260 261 static struct spi_driver max6902_driver = { 262 .driver = { 263 .name = "max6902", 264 .bus = &spi_bus_type, 265 .owner = THIS_MODULE, 266 }, 267 .probe = max6902_probe, 268 .remove = __devexit_p(max6902_remove), 269 }; 270 271 static __init int max6902_init(void) 272 { 273 printk("max6902 spi driver\n"); 274 return spi_register_driver(&max6902_driver); 275 } 276 module_init(max6902_init); 277 278 static __exit void max6902_exit(void) 279 { 280 spi_unregister_driver(&max6902_driver); 281 } 282 module_exit(max6902_exit); 283 284 MODULE_DESCRIPTION ("max6902 spi RTC driver"); 285 MODULE_AUTHOR ("Raphael Assenat"); 286 MODULE_LICENSE ("GPL"); 287