Skip to content

Commit e89b60d

Browse files
rtc: pcf85063: switch to regmap
Switch to regmap to simplify register accesses and remove the need for pcf85063_stop_clock/pcf85063_start_clock. Signed-off-by: Alexandre Belloni <[email protected]>
1 parent 802a779 commit e89b60d

File tree

2 files changed

+62
-93
lines changed

2 files changed

+62
-93
lines changed

drivers/rtc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ config RTC_DRV_PCF8523
439439

440440
config RTC_DRV_PCF85063
441441
tristate "NXP PCF85063"
442+
select REGMAP_I2C
442443
help
443444
If you say yes here you get support for the PCF85063 RTC chip
444445

drivers/rtc/rtc-pcf85063.c

Lines changed: 61 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <linux/bcd.h>
1111
#include <linux/rtc.h>
1212
#include <linux/module.h>
13+
#include <linux/of_device.h>
14+
#include <linux/regmap.h>
1315

1416
/*
1517
* Information for this driver was pulled from the following datasheets.
@@ -28,50 +30,14 @@
2830
#define PCF85063_REG_SC 0x04 /* datetime */
2931
#define PCF85063_REG_SC_OS 0x80
3032

31-
static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
32-
{
33-
int rc;
34-
u8 reg;
35-
36-
rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
37-
if (rc < 0) {
38-
dev_err(&client->dev, "Failing to stop the clock\n");
39-
return -EIO;
40-
}
41-
42-
/* stop the clock */
43-
reg = rc | PCF85063_REG_CTRL1_STOP;
44-
45-
rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
46-
if (rc < 0) {
47-
dev_err(&client->dev, "Failing to stop the clock\n");
48-
return -EIO;
49-
}
50-
51-
*ctrl1 = reg;
52-
53-
return 0;
54-
}
55-
56-
static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
57-
{
58-
int rc;
59-
60-
/* start the clock */
61-
ctrl1 &= ~PCF85063_REG_CTRL1_STOP;
62-
63-
rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
64-
if (rc < 0) {
65-
dev_err(&client->dev, "Failing to start the clock\n");
66-
return -EIO;
67-
}
68-
69-
return 0;
70-
}
33+
struct pcf85063 {
34+
struct rtc_device *rtc;
35+
struct regmap *regmap;
36+
};
7137

7238
static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
7339
{
74-
struct i2c_client *client = to_i2c_client(dev);
40+
struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
7541
int rc;
7642
u8 regs[7];
7743

@@ -81,16 +47,14 @@ static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
8147
* event, the access must be finished within one second. So, read all
8248
* time/date registers in one turn.
8349
*/
84-
rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
85-
sizeof(regs), regs);
86-
if (rc != sizeof(regs)) {
87-
dev_err(&client->dev, "date/time register read error\n");
88-
return -EIO;
89-
}
50+
rc = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_SC, regs,
51+
sizeof(regs));
52+
if (rc)
53+
return rc;
9054

9155
/* if the clock has lost its power it makes no sense to use its time */
9256
if (regs[0] & PCF85063_REG_SC_OS) {
93-
dev_warn(&client->dev, "Power loss detected, invalid time\n");
57+
dev_warn(&pcf85063->rtc->dev, "Power loss detected, invalid time\n");
9458
return -EINVAL;
9559
}
9660

@@ -108,17 +72,18 @@ static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
10872

10973
static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
11074
{
111-
struct i2c_client *client = to_i2c_client(dev);
75+
struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
11276
int rc;
11377
u8 regs[7];
114-
u8 ctrl1;
11578

11679
/*
11780
* to accurately set the time, reset the divider chain and keep it in
11881
* reset state until all time/date registers are written
11982
*/
120-
rc = pcf85063_stop_clock(client, &ctrl1);
121-
if (rc != 0)
83+
rc = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
84+
PCF85063_REG_CTRL1_STOP,
85+
PCF85063_REG_CTRL1_STOP);
86+
if (rc)
12287
return rc;
12388

12489
/* hours, minutes and seconds */
@@ -140,90 +105,93 @@ static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
140105
regs[6] = bin2bcd(tm->tm_year - 100);
141106

142107
/* write all registers at once */
143-
rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
144-
sizeof(regs), regs);
145-
if (rc < 0) {
146-
dev_err(&client->dev, "date/time register write error\n");
108+
rc = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_SC,
109+
regs, sizeof(regs));
110+
if (rc)
147111
return rc;
148-
}
149112

150113
/*
151114
* Write the control register as a separate action since the size of
152115
* the register space is different between the PCF85063TP and
153116
* PCF85063A devices. The rollover point can not be used.
154117
*/
155-
rc = pcf85063_start_clock(client, ctrl1);
156-
if (rc != 0)
157-
return rc;
158-
159-
return 0;
118+
return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
119+
PCF85063_REG_CTRL1_STOP, 0);
160120
}
161121

162122
static const struct rtc_class_ops pcf85063_rtc_ops = {
163123
.read_time = pcf85063_rtc_read_time,
164124
.set_time = pcf85063_rtc_set_time
165125
};
166126

167-
static int pcf85063_load_capacitance(struct i2c_client *client)
127+
static int pcf85063_load_capacitance(struct pcf85063 *pcf85063,
128+
const struct device_node *np)
168129
{
169-
u32 load;
170-
int rc;
171-
u8 reg;
172-
173-
rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
174-
if (rc < 0)
175-
return rc;
176-
177-
reg = rc;
178-
load = 7000;
179-
of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
180-
&load);
130+
u32 load = 7000;
131+
u8 reg = 0;
181132

133+
of_property_read_u32(np, "quartz-load-femtofarads", &load);
182134
switch (load) {
183135
default:
184-
dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
136+
dev_warn(&pcf85063->rtc->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
185137
load);
186138
/* fall through */
187139
case 7000:
188-
reg &= ~PCF85063_REG_CTRL1_CAP_SEL;
189140
break;
190141
case 12500:
191-
reg |= PCF85063_REG_CTRL1_CAP_SEL;
142+
reg = PCF85063_REG_CTRL1_CAP_SEL;
192143
break;
193144
}
194145

195-
rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
196-
197-
return rc;
146+
return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
147+
PCF85063_REG_CTRL1_CAP_SEL, reg);
198148
}
199149

150+
static const struct regmap_config regmap_config = {
151+
.reg_bits = 8,
152+
.val_bits = 8,
153+
.max_register = 0x11,
154+
};
155+
200156
static int pcf85063_probe(struct i2c_client *client)
201157
{
202-
struct rtc_device *rtc;
158+
struct pcf85063 *pcf85063;
159+
unsigned int tmp;
203160
int err;
204161

205162
dev_dbg(&client->dev, "%s\n", __func__);
206163

207-
err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
208-
if (err < 0) {
164+
pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
165+
GFP_KERNEL);
166+
if (!pcf85063)
167+
return -ENOMEM;
168+
169+
pcf85063->regmap = devm_regmap_init_i2c(client, &regmap_config);
170+
if (IS_ERR(pcf85063->regmap))
171+
return PTR_ERR(pcf85063->regmap);
172+
173+
i2c_set_clientdata(client, pcf85063);
174+
175+
err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL1, &tmp);
176+
if (err) {
209177
dev_err(&client->dev, "RTC chip is not present\n");
210178
return err;
211179
}
212180

213-
err = pcf85063_load_capacitance(client);
181+
pcf85063->rtc = devm_rtc_allocate_device(&client->dev);
182+
if (IS_ERR(pcf85063->rtc))
183+
return PTR_ERR(pcf85063->rtc);
184+
185+
err = pcf85063_load_capacitance(pcf85063, client->dev.of_node);
214186
if (err < 0)
215187
dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
216188
err);
217189

218-
rtc = devm_rtc_allocate_device(&client->dev);
219-
if (IS_ERR(rtc))
220-
return PTR_ERR(rtc);
221-
222-
rtc->ops = &pcf85063_rtc_ops;
223-
rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
224-
rtc->range_max = RTC_TIMESTAMP_END_2099;
190+
pcf85063->rtc->ops = &pcf85063_rtc_ops;
191+
pcf85063->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
192+
pcf85063->rtc->range_max = RTC_TIMESTAMP_END_2099;
225193

226-
return rtc_register_device(rtc);
194+
return rtc_register_device(pcf85063->rtc);
227195
}
228196

229197
#ifdef CONFIG_OF

0 commit comments

Comments
 (0)