0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/rtc.h>
0017 #include <linux/spi/spi.h>
0018 #include <linux/bcd.h>
0019 #include <linux/slab.h>
0020 #include <linux/of.h>
0021
0022 #define DS1390_REG_100THS 0x00
0023 #define DS1390_REG_SECONDS 0x01
0024 #define DS1390_REG_MINUTES 0x02
0025 #define DS1390_REG_HOURS 0x03
0026 #define DS1390_REG_DAY 0x04
0027 #define DS1390_REG_DATE 0x05
0028 #define DS1390_REG_MONTH_CENT 0x06
0029 #define DS1390_REG_YEAR 0x07
0030
0031 #define DS1390_REG_ALARM_100THS 0x08
0032 #define DS1390_REG_ALARM_SECONDS 0x09
0033 #define DS1390_REG_ALARM_MINUTES 0x0A
0034 #define DS1390_REG_ALARM_HOURS 0x0B
0035 #define DS1390_REG_ALARM_DAY_DATE 0x0C
0036
0037 #define DS1390_REG_CONTROL 0x0D
0038 #define DS1390_REG_STATUS 0x0E
0039 #define DS1390_REG_TRICKLE 0x0F
0040
0041 #define DS1390_TRICKLE_CHARGER_ENABLE 0xA0
0042 #define DS1390_TRICKLE_CHARGER_250_OHM 0x01
0043 #define DS1390_TRICKLE_CHARGER_2K_OHM 0x02
0044 #define DS1390_TRICKLE_CHARGER_4K_OHM 0x03
0045 #define DS1390_TRICKLE_CHARGER_NO_DIODE 0x04
0046 #define DS1390_TRICKLE_CHARGER_DIODE 0x08
0047
0048 struct ds1390 {
0049 struct rtc_device *rtc;
0050 u8 txrx_buf[9];
0051 };
0052
0053 static void ds1390_set_reg(struct device *dev, unsigned char address,
0054 unsigned char data)
0055 {
0056 struct spi_device *spi = to_spi_device(dev);
0057 unsigned char buf[2];
0058
0059
0060 buf[0] = address | 0x80;
0061 buf[1] = data;
0062
0063 spi_write(spi, buf, 2);
0064 }
0065
0066 static int ds1390_get_reg(struct device *dev, unsigned char address,
0067 unsigned char *data)
0068 {
0069 struct spi_device *spi = to_spi_device(dev);
0070 struct ds1390 *chip = dev_get_drvdata(dev);
0071 int status;
0072
0073 if (!data)
0074 return -EINVAL;
0075
0076
0077 chip->txrx_buf[0] = address & 0x7f;
0078
0079 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
0080 if (status != 0)
0081 return status;
0082
0083 *data = chip->txrx_buf[0];
0084
0085 return 0;
0086 }
0087
0088 static void ds1390_trickle_of_init(struct spi_device *spi)
0089 {
0090 u32 ohms = 0;
0091 u8 value;
0092
0093 if (of_property_read_u32(spi->dev.of_node, "trickle-resistor-ohms",
0094 &ohms))
0095 goto out;
0096
0097
0098 value = DS1390_TRICKLE_CHARGER_ENABLE;
0099 if (of_property_read_bool(spi->dev.of_node, "trickle-diode-disable"))
0100 value |= DS1390_TRICKLE_CHARGER_NO_DIODE;
0101 else
0102 value |= DS1390_TRICKLE_CHARGER_DIODE;
0103
0104
0105 switch (ohms) {
0106 case 250:
0107 value |= DS1390_TRICKLE_CHARGER_250_OHM;
0108 break;
0109 case 2000:
0110 value |= DS1390_TRICKLE_CHARGER_2K_OHM;
0111 break;
0112 case 4000:
0113 value |= DS1390_TRICKLE_CHARGER_4K_OHM;
0114 break;
0115 default:
0116 dev_warn(&spi->dev,
0117 "Unsupported ohm value %02ux in dt\n", ohms);
0118 return;
0119 }
0120
0121 ds1390_set_reg(&spi->dev, DS1390_REG_TRICKLE, value);
0122
0123 out:
0124 return;
0125 }
0126
0127 static int ds1390_read_time(struct device *dev, struct rtc_time *dt)
0128 {
0129 struct spi_device *spi = to_spi_device(dev);
0130 struct ds1390 *chip = dev_get_drvdata(dev);
0131 int status;
0132
0133
0134 chip->txrx_buf[0] = DS1390_REG_SECONDS;
0135
0136
0137 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
0138 if (status != 0)
0139 return status;
0140
0141
0142
0143 dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
0144 dt->tm_min = bcd2bin(chip->txrx_buf[1]);
0145 dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
0146 dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
0147 dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
0148
0149 dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
0150
0151 dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
0152
0153 return 0;
0154 }
0155
0156 static int ds1390_set_time(struct device *dev, struct rtc_time *dt)
0157 {
0158 struct spi_device *spi = to_spi_device(dev);
0159 struct ds1390 *chip = dev_get_drvdata(dev);
0160
0161
0162 chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
0163 chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
0164 chip->txrx_buf[2] = bin2bcd(dt->tm_min);
0165 chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
0166 chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
0167 chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
0168 chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
0169 ((dt->tm_year > 99) ? 0x80 : 0x00);
0170 chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
0171
0172
0173 return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
0174 }
0175
0176 static const struct rtc_class_ops ds1390_rtc_ops = {
0177 .read_time = ds1390_read_time,
0178 .set_time = ds1390_set_time,
0179 };
0180
0181 static int ds1390_probe(struct spi_device *spi)
0182 {
0183 unsigned char tmp;
0184 struct ds1390 *chip;
0185 int res;
0186
0187 spi->mode = SPI_MODE_3;
0188 spi->bits_per_word = 8;
0189 spi_setup(spi);
0190
0191 chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
0192 if (!chip)
0193 return -ENOMEM;
0194
0195 spi_set_drvdata(spi, chip);
0196
0197 res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
0198 if (res != 0) {
0199 dev_err(&spi->dev, "unable to read device\n");
0200 return res;
0201 }
0202
0203 if (spi->dev.of_node)
0204 ds1390_trickle_of_init(spi);
0205
0206 chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390",
0207 &ds1390_rtc_ops, THIS_MODULE);
0208 if (IS_ERR(chip->rtc)) {
0209 dev_err(&spi->dev, "unable to register device\n");
0210 res = PTR_ERR(chip->rtc);
0211 }
0212
0213 return res;
0214 }
0215
0216 static const struct of_device_id ds1390_of_match[] = {
0217 { .compatible = "dallas,ds1390" },
0218 {}
0219 };
0220 MODULE_DEVICE_TABLE(of, ds1390_of_match);
0221
0222 static const struct spi_device_id ds1390_spi_ids[] = {
0223 { .name = "ds1390" },
0224 {}
0225 };
0226 MODULE_DEVICE_TABLE(spi, ds1390_spi_ids);
0227
0228 static struct spi_driver ds1390_driver = {
0229 .driver = {
0230 .name = "rtc-ds1390",
0231 .of_match_table = of_match_ptr(ds1390_of_match),
0232 },
0233 .probe = ds1390_probe,
0234 .id_table = ds1390_spi_ids,
0235 };
0236
0237 module_spi_driver(ds1390_driver);
0238
0239 MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver");
0240 MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
0241 MODULE_LICENSE("GPL");
0242 MODULE_ALIAS("spi:rtc-ds1390");