Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 2 | /* rtc-ds1347.c |
| 3 | * |
| 4 | * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible |
| 5 | * Real Time Clock |
| 6 | * |
| 7 | * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com> |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/rtc.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/bcd.h> |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 17 | #include <linux/regmap.h> |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 18 | |
| 19 | /* Registers in ds1347 rtc */ |
| 20 | |
| 21 | #define DS1347_SECONDS_REG 0x01 |
| 22 | #define DS1347_MINUTES_REG 0x03 |
| 23 | #define DS1347_HOURS_REG 0x05 |
| 24 | #define DS1347_DATE_REG 0x07 |
| 25 | #define DS1347_MONTH_REG 0x09 |
| 26 | #define DS1347_DAY_REG 0x0B |
| 27 | #define DS1347_YEAR_REG 0x0D |
| 28 | #define DS1347_CONTROL_REG 0x0F |
| 29 | #define DS1347_STATUS_REG 0x17 |
| 30 | #define DS1347_CLOCK_BURST 0x3F |
| 31 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 32 | static const struct regmap_range ds1347_ranges[] = { |
| 33 | { |
| 34 | .range_min = DS1347_SECONDS_REG, |
| 35 | .range_max = DS1347_STATUS_REG, |
| 36 | }, |
| 37 | }; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 38 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 39 | static const struct regmap_access_table ds1347_access_table = { |
| 40 | .yes_ranges = ds1347_ranges, |
| 41 | .n_yes_ranges = ARRAY_SIZE(ds1347_ranges), |
| 42 | }; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 43 | |
| 44 | static int ds1347_read_time(struct device *dev, struct rtc_time *dt) |
| 45 | { |
| 46 | struct spi_device *spi = to_spi_device(dev); |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 47 | struct regmap *map; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 48 | int err; |
| 49 | unsigned char buf[8]; |
| 50 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 51 | map = spi_get_drvdata(spi); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 52 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 53 | err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 54 | if (err) |
| 55 | return err; |
| 56 | |
| 57 | dt->tm_sec = bcd2bin(buf[0]); |
| 58 | dt->tm_min = bcd2bin(buf[1]); |
| 59 | dt->tm_hour = bcd2bin(buf[2] & 0x3F); |
| 60 | dt->tm_mday = bcd2bin(buf[3]); |
| 61 | dt->tm_mon = bcd2bin(buf[4]) - 1; |
| 62 | dt->tm_wday = bcd2bin(buf[5]) - 1; |
| 63 | dt->tm_year = bcd2bin(buf[6]) + 100; |
| 64 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 65 | return 0; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int ds1347_set_time(struct device *dev, struct rtc_time *dt) |
| 69 | { |
| 70 | struct spi_device *spi = to_spi_device(dev); |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 71 | struct regmap *map; |
| 72 | unsigned char buf[8]; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 73 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 74 | map = spi_get_drvdata(spi); |
| 75 | |
| 76 | buf[0] = bin2bcd(dt->tm_sec); |
| 77 | buf[1] = bin2bcd(dt->tm_min); |
| 78 | buf[2] = (bin2bcd(dt->tm_hour) & 0x3F); |
| 79 | buf[3] = bin2bcd(dt->tm_mday); |
| 80 | buf[4] = bin2bcd(dt->tm_mon + 1); |
| 81 | buf[5] = bin2bcd(dt->tm_wday + 1); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 82 | |
| 83 | /* year in linux is from 1900 i.e in range of 100 |
| 84 | in rtc it is from 00 to 99 */ |
| 85 | dt->tm_year = dt->tm_year % 100; |
| 86 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 87 | buf[6] = bin2bcd(dt->tm_year); |
| 88 | buf[7] = bin2bcd(0x00); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 89 | |
| 90 | /* write the rtc settings */ |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 91 | return regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static const struct rtc_class_ops ds1347_rtc_ops = { |
| 95 | .read_time = ds1347_read_time, |
| 96 | .set_time = ds1347_set_time, |
| 97 | }; |
| 98 | |
| 99 | static int ds1347_probe(struct spi_device *spi) |
| 100 | { |
| 101 | struct rtc_device *rtc; |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 102 | struct regmap_config config; |
| 103 | struct regmap *map; |
| 104 | unsigned int data; |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 105 | int res; |
| 106 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 107 | memset(&config, 0, sizeof(config)); |
| 108 | config.reg_bits = 8; |
| 109 | config.val_bits = 8; |
| 110 | config.read_flag_mask = 0x80; |
| 111 | config.max_register = 0x3F; |
| 112 | config.wr_table = &ds1347_access_table; |
| 113 | |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 114 | /* spi setup with ds1347 in mode 3 and bits per word as 8 */ |
| 115 | spi->mode = SPI_MODE_3; |
| 116 | spi->bits_per_word = 8; |
| 117 | spi_setup(spi); |
| 118 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 119 | map = devm_regmap_init_spi(spi, &config); |
| 120 | |
| 121 | if (IS_ERR(map)) { |
| 122 | dev_err(&spi->dev, "ds1347 regmap init spi failed\n"); |
| 123 | return PTR_ERR(map); |
| 124 | } |
| 125 | |
| 126 | spi_set_drvdata(spi, map); |
| 127 | |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 128 | /* RTC Settings */ |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 129 | res = regmap_read(map, DS1347_SECONDS_REG, &data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 130 | if (res) |
| 131 | return res; |
| 132 | |
| 133 | /* Disable the write protect of rtc */ |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 134 | regmap_read(map, DS1347_CONTROL_REG, &data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 135 | data = data & ~(1<<7); |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 136 | regmap_write(map, DS1347_CONTROL_REG, data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 137 | |
| 138 | /* Enable the oscillator , disable the oscillator stop flag, |
| 139 | and glitch filter to reduce current consumption */ |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 140 | regmap_read(map, DS1347_STATUS_REG, &data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 141 | data = data & 0x1B; |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 142 | regmap_write(map, DS1347_STATUS_REG, data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 143 | |
| 144 | /* display the settings */ |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 145 | regmap_read(map, DS1347_CONTROL_REG, &data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 146 | dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data); |
| 147 | |
Raghavendra Ganiga | ee85bb5 | 2016-08-31 22:56:41 +0530 | [diff] [blame] | 148 | regmap_read(map, DS1347_STATUS_REG, &data); |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 149 | dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data); |
| 150 | |
| 151 | rtc = devm_rtc_device_register(&spi->dev, "ds1347", |
| 152 | &ds1347_rtc_ops, THIS_MODULE); |
| 153 | |
| 154 | if (IS_ERR(rtc)) |
| 155 | return PTR_ERR(rtc); |
| 156 | |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static struct spi_driver ds1347_driver = { |
| 161 | .driver = { |
| 162 | .name = "ds1347", |
Raghavendra Ganiga | 617b26a | 2014-04-03 14:50:16 -0700 | [diff] [blame] | 163 | }, |
| 164 | .probe = ds1347_probe, |
| 165 | }; |
| 166 | |
| 167 | module_spi_driver(ds1347_driver); |
| 168 | |
| 169 | MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER"); |
| 170 | MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>"); |
| 171 | MODULE_LICENSE("GPL v2"); |