Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 1 | /* drivers/rtc/rtc-max6902.c |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 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 | * |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/spi/spi.h> |
| 20 | #include <linux/bcd.h> |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 21 | |
| 22 | #define MAX6902_REG_SECONDS 0x01 |
| 23 | #define MAX6902_REG_MINUTES 0x03 |
| 24 | #define MAX6902_REG_HOURS 0x05 |
| 25 | #define MAX6902_REG_DATE 0x07 |
| 26 | #define MAX6902_REG_MONTH 0x09 |
| 27 | #define MAX6902_REG_DAY 0x0B |
| 28 | #define MAX6902_REG_YEAR 0x0D |
| 29 | #define MAX6902_REG_CONTROL 0x0F |
| 30 | #define MAX6902_REG_CENTURY 0x13 |
| 31 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 32 | static int max6902_set_reg(struct device *dev, unsigned char address, |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 33 | unsigned char data) |
| 34 | { |
| 35 | struct spi_device *spi = to_spi_device(dev); |
| 36 | unsigned char buf[2]; |
| 37 | |
| 38 | /* MSB must be '0' to write */ |
| 39 | buf[0] = address & 0x7f; |
| 40 | buf[1] = data; |
| 41 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 42 | return spi_write_then_read(spi, buf, 2, NULL, 0); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int max6902_get_reg(struct device *dev, unsigned char address, |
| 46 | unsigned char *data) |
| 47 | { |
| 48 | struct spi_device *spi = to_spi_device(dev); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 49 | |
| 50 | /* Set MSB to indicate read */ |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 51 | *data = address | 0x80; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 52 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 53 | return spi_write_then_read(spi, data, 1, data, 1); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 56 | static int max6902_read_time(struct device *dev, struct rtc_time *dt) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 57 | { |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 58 | int err, century; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 59 | struct spi_device *spi = to_spi_device(dev); |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 60 | unsigned char buf[8]; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 61 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 62 | buf[0] = 0xbf; /* Burst read */ |
| 63 | |
| 64 | err = spi_write_then_read(spi, buf, 1, buf, 8); |
| 65 | if (err != 0) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 66 | return err; |
| 67 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 68 | /* The chip sends data in this order: |
| 69 | * Seconds, Minutes, Hours, Date, Month, Day, Year */ |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 70 | dt->tm_sec = bcd2bin(buf[0]); |
| 71 | dt->tm_min = bcd2bin(buf[1]); |
| 72 | dt->tm_hour = bcd2bin(buf[2]); |
| 73 | dt->tm_mday = bcd2bin(buf[3]); |
| 74 | dt->tm_mon = bcd2bin(buf[4]) - 1; |
| 75 | dt->tm_wday = bcd2bin(buf[5]); |
| 76 | dt->tm_year = bcd2bin(buf[6]); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 77 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 78 | /* Read century */ |
| 79 | err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]); |
| 80 | if (err != 0) |
| 81 | return err; |
| 82 | |
| 83 | century = bcd2bin(buf[0]) * 100; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 84 | |
| 85 | dt->tm_year += century; |
| 86 | dt->tm_year -= 1900; |
| 87 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 88 | return rtc_valid_tm(dt); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 91 | static int max6902_set_time(struct device *dev, struct rtc_time *dt) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 92 | { |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 93 | dt->tm_year = dt->tm_year + 1900; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 94 | |
| 95 | /* Remove write protection */ |
| 96 | max6902_set_reg(dev, 0xF, 0); |
| 97 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 98 | max6902_set_reg(dev, 0x01, bin2bcd(dt->tm_sec)); |
| 99 | max6902_set_reg(dev, 0x03, bin2bcd(dt->tm_min)); |
| 100 | max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour)); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 101 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 102 | max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday)); |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 103 | max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon + 1)); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 104 | max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday)); |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 105 | max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year % 100)); |
| 106 | max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year / 100)); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 107 | |
| 108 | /* Compulab used a delay here. However, the datasheet |
| 109 | * does not mention a delay being required anywhere... */ |
| 110 | /* delay(2000); */ |
| 111 | |
| 112 | /* Write protect */ |
| 113 | max6902_set_reg(dev, 0xF, 0x80); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 118 | static const struct rtc_class_ops max6902_rtc_ops = { |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 119 | .read_time = max6902_read_time, |
| 120 | .set_time = max6902_set_time, |
| 121 | }; |
| 122 | |
| 123 | static int __devinit max6902_probe(struct spi_device *spi) |
| 124 | { |
| 125 | struct rtc_device *rtc; |
| 126 | unsigned char tmp; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 127 | int res; |
| 128 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 129 | spi->mode = SPI_MODE_3; |
| 130 | spi->bits_per_word = 8; |
| 131 | spi_setup(spi); |
| 132 | |
| 133 | res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); |
| 134 | if (res != 0) |
| 135 | return res; |
| 136 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 137 | rtc = rtc_device_register("max6902", |
| 138 | &spi->dev, &max6902_rtc_ops, THIS_MODULE); |
| 139 | if (IS_ERR(rtc)) |
| 140 | return PTR_ERR(rtc); |
| 141 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int __devexit max6902_remove(struct spi_device *spi) |
| 146 | { |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 147 | struct rtc_device *rtc = platform_get_drvdata(spi); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 148 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 149 | rtc_device_unregister(rtc); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static struct spi_driver max6902_driver = { |
| 154 | .driver = { |
David Brownell | 5c076fc | 2007-08-22 14:01:57 -0700 | [diff] [blame] | 155 | .name = "rtc-max6902", |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 156 | .bus = &spi_bus_type, |
| 157 | .owner = THIS_MODULE, |
| 158 | }, |
David Brownell | 5c076fc | 2007-08-22 14:01:57 -0700 | [diff] [blame] | 159 | .probe = max6902_probe, |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 160 | .remove = __devexit_p(max6902_remove), |
| 161 | }; |
| 162 | |
| 163 | static __init int max6902_init(void) |
| 164 | { |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 165 | return spi_register_driver(&max6902_driver); |
| 166 | } |
| 167 | module_init(max6902_init); |
| 168 | |
| 169 | static __exit void max6902_exit(void) |
| 170 | { |
| 171 | spi_unregister_driver(&max6902_driver); |
| 172 | } |
| 173 | module_exit(max6902_exit); |
| 174 | |
| 175 | MODULE_DESCRIPTION ("max6902 spi RTC driver"); |
| 176 | MODULE_AUTHOR ("Raphael Assenat"); |
| 177 | MODULE_LICENSE ("GPL"); |