Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 2 | /* |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 3 | * 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] | 4 | * |
| 5 | * Copyright (C) 2008 Mercury IMC Ltd |
| 6 | * Written by Mark Jackson <mpfj@mimc.co.uk> |
| 7 | * |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 8 | * NOTE: Currently this driver only supports the bare minimum for read |
| 9 | * and write the RTC. The extra features provided by the chip family |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 10 | * (alarms, trickle charger, different control registers) are unavailable. |
| 11 | */ |
| 12 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/rtc.h> |
| 17 | #include <linux/spi/spi.h> |
| 18 | #include <linux/bcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Ivan Grimaldi | fa395fb | 2015-09-18 17:27:57 +0200 | [diff] [blame] | 20 | #include <linux/of.h> |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 21 | |
| 22 | #define DS1390_REG_100THS 0x00 |
| 23 | #define DS1390_REG_SECONDS 0x01 |
| 24 | #define DS1390_REG_MINUTES 0x02 |
| 25 | #define DS1390_REG_HOURS 0x03 |
| 26 | #define DS1390_REG_DAY 0x04 |
| 27 | #define DS1390_REG_DATE 0x05 |
| 28 | #define DS1390_REG_MONTH_CENT 0x06 |
| 29 | #define DS1390_REG_YEAR 0x07 |
| 30 | |
| 31 | #define DS1390_REG_ALARM_100THS 0x08 |
| 32 | #define DS1390_REG_ALARM_SECONDS 0x09 |
| 33 | #define DS1390_REG_ALARM_MINUTES 0x0A |
| 34 | #define DS1390_REG_ALARM_HOURS 0x0B |
| 35 | #define DS1390_REG_ALARM_DAY_DATE 0x0C |
| 36 | |
| 37 | #define DS1390_REG_CONTROL 0x0D |
| 38 | #define DS1390_REG_STATUS 0x0E |
| 39 | #define DS1390_REG_TRICKLE 0x0F |
| 40 | |
Ivan Grimaldi | fa395fb | 2015-09-18 17:27:57 +0200 | [diff] [blame] | 41 | #define DS1390_TRICKLE_CHARGER_ENABLE 0xA0 |
| 42 | #define DS1390_TRICKLE_CHARGER_250_OHM 0x01 |
| 43 | #define DS1390_TRICKLE_CHARGER_2K_OHM 0x02 |
| 44 | #define DS1390_TRICKLE_CHARGER_4K_OHM 0x03 |
| 45 | #define DS1390_TRICKLE_CHARGER_NO_DIODE 0x04 |
| 46 | #define DS1390_TRICKLE_CHARGER_DIODE 0x08 |
| 47 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 48 | struct ds1390 { |
| 49 | struct rtc_device *rtc; |
| 50 | u8 txrx_buf[9]; /* cmd + 8 registers */ |
| 51 | }; |
| 52 | |
Ivan Grimaldi | fa395fb | 2015-09-18 17:27:57 +0200 | [diff] [blame] | 53 | static void ds1390_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 '1' to write */ |
| 60 | buf[0] = address | 0x80; |
| 61 | buf[1] = data; |
| 62 | |
| 63 | spi_write(spi, buf, 2); |
| 64 | } |
| 65 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 66 | static int ds1390_get_reg(struct device *dev, unsigned char address, |
| 67 | unsigned char *data) |
| 68 | { |
| 69 | struct spi_device *spi = to_spi_device(dev); |
| 70 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 71 | int status; |
| 72 | |
| 73 | if (!data) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | /* Clear MSB to indicate read */ |
| 77 | chip->txrx_buf[0] = address & 0x7f; |
| 78 | /* do the i/o */ |
| 79 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); |
| 80 | if (status != 0) |
| 81 | return status; |
| 82 | |
Ivan Grimaldi | 2ec6882 | 2015-09-18 17:27:56 +0200 | [diff] [blame] | 83 | *data = chip->txrx_buf[0]; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Ivan Grimaldi | fa395fb | 2015-09-18 17:27:57 +0200 | [diff] [blame] | 88 | static void ds1390_trickle_of_init(struct spi_device *spi) |
| 89 | { |
| 90 | u32 ohms = 0; |
| 91 | u8 value; |
| 92 | |
| 93 | if (of_property_read_u32(spi->dev.of_node, "trickle-resistor-ohms", |
| 94 | &ohms)) |
| 95 | goto out; |
| 96 | |
| 97 | /* Enable charger */ |
| 98 | value = DS1390_TRICKLE_CHARGER_ENABLE; |
| 99 | if (of_property_read_bool(spi->dev.of_node, "trickle-diode-disable")) |
| 100 | value |= DS1390_TRICKLE_CHARGER_NO_DIODE; |
| 101 | else |
| 102 | value |= DS1390_TRICKLE_CHARGER_DIODE; |
| 103 | |
| 104 | /* Resistor select */ |
| 105 | switch (ohms) { |
| 106 | case 250: |
| 107 | value |= DS1390_TRICKLE_CHARGER_250_OHM; |
| 108 | break; |
| 109 | case 2000: |
| 110 | value |= DS1390_TRICKLE_CHARGER_2K_OHM; |
| 111 | break; |
| 112 | case 4000: |
| 113 | value |= DS1390_TRICKLE_CHARGER_4K_OHM; |
| 114 | break; |
| 115 | default: |
| 116 | dev_warn(&spi->dev, |
| 117 | "Unsupported ohm value %02ux in dt\n", ohms); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | ds1390_set_reg(&spi->dev, DS1390_REG_TRICKLE, value); |
| 122 | |
| 123 | out: |
| 124 | return; |
| 125 | } |
| 126 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 127 | static int ds1390_read_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 128 | { |
| 129 | struct spi_device *spi = to_spi_device(dev); |
| 130 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 131 | int status; |
| 132 | |
| 133 | /* build the message */ |
| 134 | chip->txrx_buf[0] = DS1390_REG_SECONDS; |
| 135 | |
| 136 | /* do the i/o */ |
| 137 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); |
| 138 | if (status != 0) |
| 139 | return status; |
| 140 | |
| 141 | /* The chip sends data in this order: |
| 142 | * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ |
| 143 | dt->tm_sec = bcd2bin(chip->txrx_buf[0]); |
| 144 | dt->tm_min = bcd2bin(chip->txrx_buf[1]); |
| 145 | dt->tm_hour = bcd2bin(chip->txrx_buf[2]); |
| 146 | dt->tm_wday = bcd2bin(chip->txrx_buf[3]); |
| 147 | dt->tm_mday = bcd2bin(chip->txrx_buf[4]); |
| 148 | /* mask off century bit */ |
| 149 | dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; |
| 150 | /* adjust for century bit */ |
| 151 | dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); |
| 152 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 153 | return 0; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 156 | static int ds1390_set_time(struct device *dev, struct rtc_time *dt) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 157 | { |
| 158 | struct spi_device *spi = to_spi_device(dev); |
| 159 | struct ds1390 *chip = dev_get_drvdata(dev); |
| 160 | |
| 161 | /* build the message */ |
| 162 | chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; |
| 163 | chip->txrx_buf[1] = bin2bcd(dt->tm_sec); |
| 164 | chip->txrx_buf[2] = bin2bcd(dt->tm_min); |
| 165 | chip->txrx_buf[3] = bin2bcd(dt->tm_hour); |
| 166 | chip->txrx_buf[4] = bin2bcd(dt->tm_wday); |
| 167 | chip->txrx_buf[5] = bin2bcd(dt->tm_mday); |
| 168 | chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | |
| 169 | ((dt->tm_year > 99) ? 0x80 : 0x00); |
| 170 | chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); |
| 171 | |
| 172 | /* do the i/o */ |
| 173 | return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); |
| 174 | } |
| 175 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 176 | static const struct rtc_class_ops ds1390_rtc_ops = { |
| 177 | .read_time = ds1390_read_time, |
| 178 | .set_time = ds1390_set_time, |
| 179 | }; |
| 180 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 181 | static int ds1390_probe(struct spi_device *spi) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 182 | { |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 183 | unsigned char tmp; |
| 184 | struct ds1390 *chip; |
| 185 | int res; |
| 186 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 187 | spi->mode = SPI_MODE_3; |
| 188 | spi->bits_per_word = 8; |
| 189 | spi_setup(spi); |
| 190 | |
Jingoo Han | 1ce95ba | 2013-04-29 16:20:39 -0700 | [diff] [blame] | 191 | chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL); |
Jingoo Han | 7841fe5 | 2014-04-03 14:49:40 -0700 | [diff] [blame] | 192 | if (!chip) |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 193 | return -ENOMEM; |
Jingoo Han | 7841fe5 | 2014-04-03 14:49:40 -0700 | [diff] [blame] | 194 | |
Jingoo Han | ccbc20f | 2013-04-29 16:20:20 -0700 | [diff] [blame] | 195 | spi_set_drvdata(spi, chip); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 196 | |
| 197 | res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 198 | if (res != 0) { |
| 199 | dev_err(&spi->dev, "unable to read device\n"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 200 | return res; |
| 201 | } |
| 202 | |
Ivan Grimaldi | fa395fb | 2015-09-18 17:27:57 +0200 | [diff] [blame] | 203 | if (spi->dev.of_node) |
| 204 | ds1390_trickle_of_init(spi); |
| 205 | |
Jingoo Han | 1ce95ba | 2013-04-29 16:20:39 -0700 | [diff] [blame] | 206 | chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390", |
| 207 | &ds1390_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 208 | if (IS_ERR(chip->rtc)) { |
| 209 | dev_err(&spi->dev, "unable to register device\n"); |
| 210 | res = PTR_ERR(chip->rtc); |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return res; |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Alexandre Belloni | 3ba2c76 | 2017-10-27 13:09:47 +0200 | [diff] [blame] | 216 | static const struct of_device_id ds1390_of_match[] = { |
| 217 | { .compatible = "dallas,ds1390" }, |
| 218 | {} |
| 219 | }; |
| 220 | MODULE_DEVICE_TABLE(of, ds1390_of_match); |
| 221 | |
Mark Brown | da87639 | 2021-09-23 20:49:21 +0100 | [diff] [blame] | 222 | static const struct spi_device_id ds1390_spi_ids[] = { |
| 223 | { .name = "ds1390" }, |
| 224 | {} |
| 225 | }; |
| 226 | MODULE_DEVICE_TABLE(spi, ds1390_spi_ids); |
| 227 | |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 228 | static struct spi_driver ds1390_driver = { |
| 229 | .driver = { |
| 230 | .name = "rtc-ds1390", |
Alexandre Belloni | 3ba2c76 | 2017-10-27 13:09:47 +0200 | [diff] [blame] | 231 | .of_match_table = of_match_ptr(ds1390_of_match), |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 232 | }, |
| 233 | .probe = ds1390_probe, |
Mark Brown | da87639 | 2021-09-23 20:49:21 +0100 | [diff] [blame] | 234 | .id_table = ds1390_spi_ids, |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 237 | module_spi_driver(ds1390_driver); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 238 | |
Alessandro Zummo | 7b9b2ef | 2009-01-06 14:42:16 -0800 | [diff] [blame] | 239 | MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver"); |
Mark Jackson | 06de180 | 2008-11-12 13:27:07 -0800 | [diff] [blame] | 240 | MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); |
| 241 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 242 | MODULE_ALIAS("spi:rtc-ds1390"); |