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