Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 1 | /* |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 2 | * rtc-ds1390.c -- driver for the Dallas/Maxim DS1390/93/94 SPI RTC |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 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 | * |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 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 |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 13 | * (alarms, trickle charger, different control registers) are unavailable. |
| 14 | */ |
| 15 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/spi/spi.h> |
| 21 | #include <linux/bcd.h> |
| 22 | |
| 23 | #define DS1390_REG_100THS 0x00 |
| 24 | #define DS1390_REG_SECONDS 0x01 |
| 25 | #define DS1390_REG_MINUTES 0x02 |
| 26 | #define DS1390_REG_HOURS 0x03 |
| 27 | #define DS1390_REG_DAY 0x04 |
| 28 | #define DS1390_REG_DATE 0x05 |
| 29 | #define DS1390_REG_MONTH_CENT 0x06 |
| 30 | #define DS1390_REG_YEAR 0x07 |
| 31 | |
| 32 | #define DS1390_REG_ALARM_100THS 0x08 |
| 33 | #define DS1390_REG_ALARM_SECONDS 0x09 |
| 34 | #define DS1390_REG_ALARM_MINUTES 0x0A |
| 35 | #define DS1390_REG_ALARM_HOURS 0x0B |
| 36 | #define DS1390_REG_ALARM_DAY_DATE 0x0C |
| 37 | |
| 38 | #define DS1390_REG_CONTROL 0x0D |
| 39 | #define DS1390_REG_STATUS 0x0E |
| 40 | #define DS1390_REG_TRICKLE 0x0F |
| 41 | |
| 42 | struct ds1390 { |
| 43 | struct rtc_device *rtc; |
| 44 | u8 txrx_buf[9]; /* cmd + 8 registers */ |
| 45 | }; |
| 46 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 47 | static int ds1390_get_reg(struct device *dev, unsigned char address, |
| 48 | unsigned char *data) |
| 49 | { |
| 50 | struct spi_device *spi = to_spi_device(dev); |
| 51 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 52 | int status; |
| 53 | |
| 54 | if (!data) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | /* Clear MSB to indicate read */ |
| 58 | chip->txrx_buf[0] = address & 0x7f; |
| 59 | /* do the i/o */ |
| 60 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); |
| 61 | if (status != 0) |
| 62 | return status; |
| 63 | |
| 64 | *data = chip->txrx_buf[1]; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 69 | static int ds1390_read_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 70 | { |
| 71 | struct spi_device *spi = to_spi_device(dev); |
| 72 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 73 | int status; |
| 74 | |
| 75 | /* build the message */ |
| 76 | chip->txrx_buf[0] = DS1390_REG_SECONDS; |
| 77 | |
| 78 | /* do the i/o */ |
| 79 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); |
| 80 | if (status != 0) |
| 81 | return status; |
| 82 | |
| 83 | /* The chip sends data in this order: |
| 84 | * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ |
| 85 | dt->tm_sec = bcd2bin(chip->txrx_buf[0]); |
| 86 | dt->tm_min = bcd2bin(chip->txrx_buf[1]); |
| 87 | dt->tm_hour = bcd2bin(chip->txrx_buf[2]); |
| 88 | dt->tm_wday = bcd2bin(chip->txrx_buf[3]); |
| 89 | dt->tm_mday = bcd2bin(chip->txrx_buf[4]); |
| 90 | /* mask off century bit */ |
| 91 | dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; |
| 92 | /* adjust for century bit */ |
| 93 | dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); |
| 94 | |
| 95 | return rtc_valid_tm(dt); |
| 96 | } |
| 97 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 98 | static int ds1390_set_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 99 | { |
| 100 | struct spi_device *spi = to_spi_device(dev); |
| 101 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 102 | |
| 103 | /* build the message */ |
| 104 | chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; |
| 105 | chip->txrx_buf[1] = bin2bcd(dt->tm_sec); |
| 106 | chip->txrx_buf[2] = bin2bcd(dt->tm_min); |
| 107 | chip->txrx_buf[3] = bin2bcd(dt->tm_hour); |
| 108 | chip->txrx_buf[4] = bin2bcd(dt->tm_wday); |
| 109 | chip->txrx_buf[5] = bin2bcd(dt->tm_mday); |
| 110 | chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | |
| 111 | ((dt->tm_year > 99) ? 0x80 : 0x00); |
| 112 | chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); |
| 113 | |
| 114 | /* do the i/o */ |
| 115 | return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); |
| 116 | } |
| 117 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 118 | static const struct rtc_class_ops ds1390_rtc_ops = { |
| 119 | .read_time = ds1390_read_time, |
| 120 | .set_time = ds1390_set_time, |
| 121 | }; |
| 122 | |
| 123 | static int __devinit ds1390_probe(struct spi_device *spi) |
| 124 | { |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 125 | unsigned char tmp; |
| 126 | struct ds1390 *chip; |
| 127 | int res; |
| 128 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 129 | spi->mode = SPI_MODE_3; |
| 130 | spi->bits_per_word = 8; |
| 131 | spi_setup(spi); |
| 132 | |
| 133 | chip = kzalloc(sizeof *chip, GFP_KERNEL); |
| 134 | if (!chip) { |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 135 | dev_err(&spi->dev, "unable to allocate device memory\n"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 136 | return -ENOMEM; |
| 137 | } |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 138 | dev_set_drvdata(&spi->dev, chip); |
| 139 | |
| 140 | res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 141 | if (res != 0) { |
| 142 | dev_err(&spi->dev, "unable to read device\n"); |
| 143 | kfree(chip); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 144 | return res; |
| 145 | } |
| 146 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 147 | chip->rtc = rtc_device_register("ds1390", |
| 148 | &spi->dev, &ds1390_rtc_ops, THIS_MODULE); |
| 149 | if (IS_ERR(chip->rtc)) { |
| 150 | dev_err(&spi->dev, "unable to register device\n"); |
| 151 | res = PTR_ERR(chip->rtc); |
| 152 | kfree(chip); |
| 153 | } |
| 154 | |
| 155 | return res; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int __devexit ds1390_remove(struct spi_device *spi) |
| 159 | { |
| 160 | struct ds1390 *chip = platform_get_drvdata(spi); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 161 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 162 | rtc_device_unregister(chip->rtc); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 163 | kfree(chip); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct spi_driver ds1390_driver = { |
| 169 | .driver = { |
| 170 | .name = "rtc-ds1390", |
| 171 | .owner = THIS_MODULE, |
| 172 | }, |
| 173 | .probe = ds1390_probe, |
| 174 | .remove = __devexit_p(ds1390_remove), |
| 175 | }; |
| 176 | |
| 177 | static __init int ds1390_init(void) |
| 178 | { |
| 179 | return spi_register_driver(&ds1390_driver); |
| 180 | } |
| 181 | module_init(ds1390_init); |
| 182 | |
| 183 | static __exit void ds1390_exit(void) |
| 184 | { |
| 185 | spi_unregister_driver(&ds1390_driver); |
| 186 | } |
| 187 | module_exit(ds1390_exit); |
| 188 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 189 | MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 190 | MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); |
| 191 | MODULE_LICENSE("GPL"); |