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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 23 | |
| 24 | #define DS1390_REG_100THS 0x00 |
| 25 | #define DS1390_REG_SECONDS 0x01 |
| 26 | #define DS1390_REG_MINUTES 0x02 |
| 27 | #define DS1390_REG_HOURS 0x03 |
| 28 | #define DS1390_REG_DAY 0x04 |
| 29 | #define DS1390_REG_DATE 0x05 |
| 30 | #define DS1390_REG_MONTH_CENT 0x06 |
| 31 | #define DS1390_REG_YEAR 0x07 |
| 32 | |
| 33 | #define DS1390_REG_ALARM_100THS 0x08 |
| 34 | #define DS1390_REG_ALARM_SECONDS 0x09 |
| 35 | #define DS1390_REG_ALARM_MINUTES 0x0A |
| 36 | #define DS1390_REG_ALARM_HOURS 0x0B |
| 37 | #define DS1390_REG_ALARM_DAY_DATE 0x0C |
| 38 | |
| 39 | #define DS1390_REG_CONTROL 0x0D |
| 40 | #define DS1390_REG_STATUS 0x0E |
| 41 | #define DS1390_REG_TRICKLE 0x0F |
| 42 | |
| 43 | struct ds1390 { |
| 44 | struct rtc_device *rtc; |
| 45 | u8 txrx_buf[9]; /* cmd + 8 registers */ |
| 46 | }; |
| 47 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 48 | static int ds1390_get_reg(struct device *dev, unsigned char address, |
| 49 | unsigned char *data) |
| 50 | { |
| 51 | struct spi_device *spi = to_spi_device(dev); |
| 52 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 53 | int status; |
| 54 | |
| 55 | if (!data) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | /* Clear MSB to indicate read */ |
| 59 | chip->txrx_buf[0] = address & 0x7f; |
| 60 | /* do the i/o */ |
| 61 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); |
| 62 | if (status != 0) |
| 63 | return status; |
| 64 | |
Ivan Grimaldi | 2ec6882 | 2015-09-18 17:27:56 +0200 | [diff] [blame^] | 65 | *data = chip->txrx_buf[0]; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 70 | static int ds1390_read_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 71 | { |
| 72 | struct spi_device *spi = to_spi_device(dev); |
| 73 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 74 | int status; |
| 75 | |
| 76 | /* build the message */ |
| 77 | chip->txrx_buf[0] = DS1390_REG_SECONDS; |
| 78 | |
| 79 | /* do the i/o */ |
| 80 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); |
| 81 | if (status != 0) |
| 82 | return status; |
| 83 | |
| 84 | /* The chip sends data in this order: |
| 85 | * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ |
| 86 | dt->tm_sec = bcd2bin(chip->txrx_buf[0]); |
| 87 | dt->tm_min = bcd2bin(chip->txrx_buf[1]); |
| 88 | dt->tm_hour = bcd2bin(chip->txrx_buf[2]); |
| 89 | dt->tm_wday = bcd2bin(chip->txrx_buf[3]); |
| 90 | dt->tm_mday = bcd2bin(chip->txrx_buf[4]); |
| 91 | /* mask off century bit */ |
| 92 | dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; |
| 93 | /* adjust for century bit */ |
| 94 | dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); |
| 95 | |
| 96 | return rtc_valid_tm(dt); |
| 97 | } |
| 98 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 99 | static int ds1390_set_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 100 | { |
| 101 | struct spi_device *spi = to_spi_device(dev); |
| 102 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 103 | |
| 104 | /* build the message */ |
| 105 | chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; |
| 106 | chip->txrx_buf[1] = bin2bcd(dt->tm_sec); |
| 107 | chip->txrx_buf[2] = bin2bcd(dt->tm_min); |
| 108 | chip->txrx_buf[3] = bin2bcd(dt->tm_hour); |
| 109 | chip->txrx_buf[4] = bin2bcd(dt->tm_wday); |
| 110 | chip->txrx_buf[5] = bin2bcd(dt->tm_mday); |
| 111 | chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | |
| 112 | ((dt->tm_year > 99) ? 0x80 : 0x00); |
| 113 | chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); |
| 114 | |
| 115 | /* do the i/o */ |
| 116 | return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); |
| 117 | } |
| 118 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 119 | static const struct rtc_class_ops ds1390_rtc_ops = { |
| 120 | .read_time = ds1390_read_time, |
| 121 | .set_time = ds1390_set_time, |
| 122 | }; |
| 123 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 124 | static int ds1390_probe(struct spi_device *spi) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 125 | { |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 126 | unsigned char tmp; |
| 127 | struct ds1390 *chip; |
| 128 | int res; |
| 129 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 130 | spi->mode = SPI_MODE_3; |
| 131 | spi->bits_per_word = 8; |
| 132 | spi_setup(spi); |
| 133 | |
Jingoo Han | 1ce95ba | 2013-04-29 16:20:39 -0700 | [diff] [blame] | 134 | chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL); |
Jingoo Han | 7841fe5 | 2014-04-03 14:49:40 -0700 | [diff] [blame] | 135 | if (!chip) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 136 | return -ENOMEM; |
Jingoo Han | 7841fe5 | 2014-04-03 14:49:40 -0700 | [diff] [blame] | 137 | |
Jingoo Han | ccbc20f | 2013-04-29 16:20:20 -0700 | [diff] [blame] | 138 | spi_set_drvdata(spi, chip); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 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"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 143 | return res; |
| 144 | } |
| 145 | |
Jingoo Han | 1ce95ba | 2013-04-29 16:20:39 -0700 | [diff] [blame] | 146 | chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390", |
| 147 | &ds1390_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 148 | if (IS_ERR(chip->rtc)) { |
| 149 | dev_err(&spi->dev, "unable to register device\n"); |
| 150 | res = PTR_ERR(chip->rtc); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return res; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 156 | static struct spi_driver ds1390_driver = { |
| 157 | .driver = { |
| 158 | .name = "rtc-ds1390", |
| 159 | .owner = THIS_MODULE, |
| 160 | }, |
| 161 | .probe = ds1390_probe, |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 164 | module_spi_driver(ds1390_driver); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 165 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 166 | MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 167 | MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); |
| 168 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 169 | MODULE_ALIAS("spi:rtc-ds1390"); |