Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | /* drivers/rtc/rtc-max6902.c |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006 8D Technologies inc. |
| 5 | * Copyright (C) 2004 Compulab Ltd. |
| 6 | * |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 7 | * Driver for MAX6902 spi RTC |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/rtc.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/bcd.h> |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 17 | |
| 18 | #define MAX6902_REG_SECONDS 0x01 |
| 19 | #define MAX6902_REG_MINUTES 0x03 |
| 20 | #define MAX6902_REG_HOURS 0x05 |
| 21 | #define MAX6902_REG_DATE 0x07 |
| 22 | #define MAX6902_REG_MONTH 0x09 |
| 23 | #define MAX6902_REG_DAY 0x0B |
| 24 | #define MAX6902_REG_YEAR 0x0D |
| 25 | #define MAX6902_REG_CONTROL 0x0F |
| 26 | #define MAX6902_REG_CENTURY 0x13 |
| 27 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 28 | static int max6902_set_reg(struct device *dev, unsigned char address, |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 29 | unsigned char data) |
| 30 | { |
| 31 | struct spi_device *spi = to_spi_device(dev); |
| 32 | unsigned char buf[2]; |
| 33 | |
| 34 | /* MSB must be '0' to write */ |
| 35 | buf[0] = address & 0x7f; |
| 36 | buf[1] = data; |
| 37 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 38 | return spi_write_then_read(spi, buf, 2, NULL, 0); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static int max6902_get_reg(struct device *dev, unsigned char address, |
| 42 | unsigned char *data) |
| 43 | { |
| 44 | struct spi_device *spi = to_spi_device(dev); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 45 | |
| 46 | /* Set MSB to indicate read */ |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 47 | *data = address | 0x80; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 48 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 49 | return spi_write_then_read(spi, data, 1, data, 1); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 52 | static int max6902_read_time(struct device *dev, struct rtc_time *dt) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 53 | { |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 54 | int err, century; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 55 | struct spi_device *spi = to_spi_device(dev); |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 56 | unsigned char buf[8]; |
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 | buf[0] = 0xbf; /* Burst read */ |
| 59 | |
| 60 | err = spi_write_then_read(spi, buf, 1, buf, 8); |
| 61 | if (err != 0) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 62 | return err; |
| 63 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 64 | /* The chip sends data in this order: |
| 65 | * Seconds, Minutes, Hours, Date, Month, Day, Year */ |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 66 | dt->tm_sec = bcd2bin(buf[0]); |
| 67 | dt->tm_min = bcd2bin(buf[1]); |
| 68 | dt->tm_hour = bcd2bin(buf[2]); |
| 69 | dt->tm_mday = bcd2bin(buf[3]); |
| 70 | dt->tm_mon = bcd2bin(buf[4]) - 1; |
| 71 | dt->tm_wday = bcd2bin(buf[5]); |
| 72 | dt->tm_year = bcd2bin(buf[6]); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 73 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 74 | /* Read century */ |
| 75 | err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]); |
| 76 | if (err != 0) |
| 77 | return err; |
| 78 | |
| 79 | century = bcd2bin(buf[0]) * 100; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 80 | |
| 81 | dt->tm_year += century; |
| 82 | dt->tm_year -= 1900; |
| 83 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 84 | return 0; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 87 | static int max6902_set_time(struct device *dev, struct rtc_time *dt) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 88 | { |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 89 | dt->tm_year = dt->tm_year + 1900; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 90 | |
| 91 | /* Remove write protection */ |
Axel Lin | 08348d2 | 2013-04-29 16:20:57 -0700 | [diff] [blame] | 92 | max6902_set_reg(dev, MAX6902_REG_CONTROL, 0); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 93 | |
Axel Lin | 08348d2 | 2013-04-29 16:20:57 -0700 | [diff] [blame] | 94 | max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec)); |
| 95 | max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min)); |
| 96 | max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour)); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 97 | |
Axel Lin | 08348d2 | 2013-04-29 16:20:57 -0700 | [diff] [blame] | 98 | max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday)); |
| 99 | max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1)); |
| 100 | max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday)); |
| 101 | max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100)); |
| 102 | max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100)); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 103 | |
| 104 | /* Compulab used a delay here. However, the datasheet |
| 105 | * does not mention a delay being required anywhere... */ |
| 106 | /* delay(2000); */ |
| 107 | |
| 108 | /* Write protect */ |
Axel Lin | 08348d2 | 2013-04-29 16:20:57 -0700 | [diff] [blame] | 109 | max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 114 | static const struct rtc_class_ops max6902_rtc_ops = { |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 115 | .read_time = max6902_read_time, |
| 116 | .set_time = max6902_set_time, |
| 117 | }; |
| 118 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 119 | static int max6902_probe(struct spi_device *spi) |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 120 | { |
| 121 | struct rtc_device *rtc; |
| 122 | unsigned char tmp; |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 123 | int res; |
| 124 | |
Alessandro Zummo | a5771c6 | 2009-01-06 14:42:19 -0800 | [diff] [blame] | 125 | spi->mode = SPI_MODE_3; |
| 126 | spi->bits_per_word = 8; |
| 127 | spi_setup(spi); |
| 128 | |
| 129 | res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); |
| 130 | if (res != 0) |
| 131 | return res; |
| 132 | |
Jingoo Han | 04353a7 | 2013-04-29 16:19:43 -0700 | [diff] [blame] | 133 | rtc = devm_rtc_device_register(&spi->dev, "max6902", |
| 134 | &max6902_rtc_ops, THIS_MODULE); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 135 | if (IS_ERR(rtc)) |
| 136 | return PTR_ERR(rtc); |
| 137 | |
Jingoo Han | a5ef73f | 2013-04-29 16:20:22 -0700 | [diff] [blame] | 138 | spi_set_drvdata(spi, rtc); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 142 | static struct spi_driver max6902_driver = { |
| 143 | .driver = { |
David Brownell | 5c076fc | 2007-08-22 14:01:57 -0700 | [diff] [blame] | 144 | .name = "rtc-max6902", |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 145 | }, |
David Brownell | 5c076fc | 2007-08-22 14:01:57 -0700 | [diff] [blame] | 146 | .probe = max6902_probe, |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 149 | module_spi_driver(max6902_driver); |
Raphael Assenat | 8e12ecc | 2006-06-25 05:48:23 -0700 | [diff] [blame] | 150 | |
Sachin Kamat | 83246a1 | 2013-07-03 15:05:54 -0700 | [diff] [blame] | 151 | MODULE_DESCRIPTION("max6902 spi RTC driver"); |
| 152 | MODULE_AUTHOR("Raphael Assenat"); |
| 153 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 154 | MODULE_ALIAS("spi:rtc-max6902"); |