summaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-ds1347.c
diff options
context:
space:
mode:
authorRaghavendra Ganiga <ravi23ganiga@gmail.com>2016-08-31 13:26:41 -0400
committerAlexandre Belloni <alexandre.belloni@free-electrons.com>2016-09-21 18:14:40 -0400
commitee85bb5bbe4ae690b48625874bc2b6ecc981326e (patch)
tree15ec03b1bc8339d055e59f0d3dfb5128a7251145 /drivers/rtc/rtc-ds1347.c
parent68669d55f7ad31832692254485a07b6e412ae082 (diff)
rtc: ds1347: changed raw spi calls to register map calls
This patch changes calls of spi read write calls to register map read and write calls in rtc ds1347 Signed-off-by: Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Diffstat (limited to 'drivers/rtc/rtc-ds1347.c')
-rw-r--r--drivers/rtc/rtc-ds1347.c96
1 files changed, 53 insertions, 43 deletions
diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
index 641e8e8a0dd7..ccfc9d43eb1e 100644
--- a/drivers/rtc/rtc-ds1347.c
+++ b/drivers/rtc/rtc-ds1347.c
@@ -18,6 +18,7 @@
18#include <linux/rtc.h> 18#include <linux/rtc.h>
19#include <linux/spi/spi.h> 19#include <linux/spi/spi.h>
20#include <linux/bcd.h> 20#include <linux/bcd.h>
21#include <linux/regmap.h>
21 22
22/* Registers in ds1347 rtc */ 23/* Registers in ds1347 rtc */
23 24
@@ -32,37 +33,28 @@
32#define DS1347_STATUS_REG 0x17 33#define DS1347_STATUS_REG 0x17
33#define DS1347_CLOCK_BURST 0x3F 34#define DS1347_CLOCK_BURST 0x3F
34 35
35static int ds1347_read_reg(struct device *dev, unsigned char address, 36static const struct regmap_range ds1347_ranges[] = {
36 unsigned char *data) 37 {
37{ 38 .range_min = DS1347_SECONDS_REG,
38 struct spi_device *spi = to_spi_device(dev); 39 .range_max = DS1347_STATUS_REG,
39 40 },
40 *data = address | 0x80; 41};
41
42 return spi_write_then_read(spi, data, 1, data, 1);
43}
44
45static int ds1347_write_reg(struct device *dev, unsigned char address,
46 unsigned char data)
47{
48 struct spi_device *spi = to_spi_device(dev);
49 unsigned char buf[2];
50
51 buf[0] = address & 0x7F;
52 buf[1] = data;
53 42
54 return spi_write_then_read(spi, buf, 2, NULL, 0); 43static const struct regmap_access_table ds1347_access_table = {
55} 44 .yes_ranges = ds1347_ranges,
45 .n_yes_ranges = ARRAY_SIZE(ds1347_ranges),
46};
56 47
57static int ds1347_read_time(struct device *dev, struct rtc_time *dt) 48static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
58{ 49{
59 struct spi_device *spi = to_spi_device(dev); 50 struct spi_device *spi = to_spi_device(dev);
51 struct regmap *map;
60 int err; 52 int err;
61 unsigned char buf[8]; 53 unsigned char buf[8];
62 54
63 buf[0] = DS1347_CLOCK_BURST | 0x80; 55 map = spi_get_drvdata(spi);
64 56
65 err = spi_write_then_read(spi, buf, 1, buf, 8); 57 err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
66 if (err) 58 if (err)
67 return err; 59 return err;
68 60
@@ -80,25 +72,27 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
80static int ds1347_set_time(struct device *dev, struct rtc_time *dt) 72static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
81{ 73{
82 struct spi_device *spi = to_spi_device(dev); 74 struct spi_device *spi = to_spi_device(dev);
83 unsigned char buf[9]; 75 struct regmap *map;
76 unsigned char buf[8];
77
78 map = spi_get_drvdata(spi);
84 79
85 buf[0] = DS1347_CLOCK_BURST & 0x7F; 80 buf[0] = bin2bcd(dt->tm_sec);
86 buf[1] = bin2bcd(dt->tm_sec); 81 buf[1] = bin2bcd(dt->tm_min);
87 buf[2] = bin2bcd(dt->tm_min); 82 buf[2] = (bin2bcd(dt->tm_hour) & 0x3F);
88 buf[3] = (bin2bcd(dt->tm_hour) & 0x3F); 83 buf[3] = bin2bcd(dt->tm_mday);
89 buf[4] = bin2bcd(dt->tm_mday); 84 buf[4] = bin2bcd(dt->tm_mon + 1);
90 buf[5] = bin2bcd(dt->tm_mon + 1); 85 buf[5] = bin2bcd(dt->tm_wday + 1);
91 buf[6] = bin2bcd(dt->tm_wday + 1);
92 86
93 /* year in linux is from 1900 i.e in range of 100 87 /* year in linux is from 1900 i.e in range of 100
94 in rtc it is from 00 to 99 */ 88 in rtc it is from 00 to 99 */
95 dt->tm_year = dt->tm_year % 100; 89 dt->tm_year = dt->tm_year % 100;
96 90
97 buf[7] = bin2bcd(dt->tm_year); 91 buf[6] = bin2bcd(dt->tm_year);
98 buf[8] = bin2bcd(0x00); 92 buf[7] = bin2bcd(0x00);
99 93
100 /* write the rtc settings */ 94 /* write the rtc settings */
101 return spi_write_then_read(spi, buf, 9, NULL, 0); 95 return regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
102} 96}
103 97
104static const struct rtc_class_ops ds1347_rtc_ops = { 98static const struct rtc_class_ops ds1347_rtc_ops = {
@@ -109,35 +103,53 @@ static const struct rtc_class_ops ds1347_rtc_ops = {
109static int ds1347_probe(struct spi_device *spi) 103static int ds1347_probe(struct spi_device *spi)
110{ 104{
111 struct rtc_device *rtc; 105 struct rtc_device *rtc;
112 unsigned char data; 106 struct regmap_config config;
107 struct regmap *map;
108 unsigned int data;
113 int res; 109 int res;
114 110
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
115 /* spi setup with ds1347 in mode 3 and bits per word as 8 */ 118 /* spi setup with ds1347 in mode 3 and bits per word as 8 */
116 spi->mode = SPI_MODE_3; 119 spi->mode = SPI_MODE_3;
117 spi->bits_per_word = 8; 120 spi->bits_per_word = 8;
118 spi_setup(spi); 121 spi_setup(spi);
119 122
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
120 /* RTC Settings */ 132 /* RTC Settings */
121 res = ds1347_read_reg(&spi->dev, DS1347_SECONDS_REG, &data); 133 res = regmap_read(map, DS1347_SECONDS_REG, &data);
122 if (res) 134 if (res)
123 return res; 135 return res;
124 136
125 /* Disable the write protect of rtc */ 137 /* Disable the write protect of rtc */
126 ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data); 138 regmap_read(map, DS1347_CONTROL_REG, &data);
127 data = data & ~(1<<7); 139 data = data & ~(1<<7);
128 ds1347_write_reg(&spi->dev, DS1347_CONTROL_REG, data); 140 regmap_write(map, DS1347_CONTROL_REG, data);
129 141
130 /* Enable the oscillator , disable the oscillator stop flag, 142 /* Enable the oscillator , disable the oscillator stop flag,
131 and glitch filter to reduce current consumption */ 143 and glitch filter to reduce current consumption */
132 ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data); 144 regmap_read(map, DS1347_STATUS_REG, &data);
133 data = data & 0x1B; 145 data = data & 0x1B;
134 ds1347_write_reg(&spi->dev, DS1347_STATUS_REG, data); 146 regmap_write(map, DS1347_STATUS_REG, data);
135 147
136 /* display the settings */ 148 /* display the settings */
137 ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data); 149 regmap_read(map, DS1347_CONTROL_REG, &data);
138 dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data); 150 dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
139 151
140 ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data); 152 regmap_read(map, DS1347_STATUS_REG, &data);
141 dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data); 153 dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
142 154
143 rtc = devm_rtc_device_register(&spi->dev, "ds1347", 155 rtc = devm_rtc_device_register(&spi->dev, "ds1347",
@@ -146,8 +158,6 @@ static int ds1347_probe(struct spi_device *spi)
146 if (IS_ERR(rtc)) 158 if (IS_ERR(rtc))
147 return PTR_ERR(rtc); 159 return PTR_ERR(rtc);
148 160
149 spi_set_drvdata(spi, rtc);
150
151 return 0; 161 return 0;
152} 162}
153 163