aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-pcf2123.c
diff options
context:
space:
mode:
authorJoshua Clayton <stillcompiling@gmail.com>2016-01-04 13:31:22 -0500
committerAlexandre Belloni <alexandre.belloni@free-electrons.com>2016-03-14 12:07:58 -0400
commit1e094b946c1e2186b412118c2af3cc602cad3ac4 (patch)
tree3e14496f0fbcb4a7daec78badf3f56289ae7038c /drivers/rtc/rtc-pcf2123.c
parent809b453b76e1544d245b7285fe570f3f20b8bd89 (diff)
rtc: pcf2123: refactor chip reset into a function
Refactor chip reset items into its own function, isolating it from the rest of the device probe. Subsequent commits will avoid calling this code. Signed-off-by: Joshua Clayton <stillcompiling@gmail.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Diffstat (limited to 'drivers/rtc/rtc-pcf2123.c')
-rw-r--r--drivers/rtc/rtc-pcf2123.c64
1 files changed, 36 insertions, 28 deletions
diff --git a/drivers/rtc/rtc-pcf2123.c b/drivers/rtc/rtc-pcf2123.c
index 0f2ce4546f62..2d886d435e14 100644
--- a/drivers/rtc/rtc-pcf2123.c
+++ b/drivers/rtc/rtc-pcf2123.c
@@ -272,6 +272,40 @@ static int pcf2123_rtc_set_time(struct device *dev, struct rtc_time *tm)
272 return 0; 272 return 0;
273} 273}
274 274
275static int pcf2123_reset(struct device *dev)
276{
277 int ret;
278 u8 rxbuf[2];
279
280 ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_SW_RESET);
281 if (ret < 0)
282 return ret;
283
284 /* Stop the counter */
285 dev_dbg(dev, "stopping RTC\n");
286 ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_STOP);
287 if (ret < 0)
288 return ret;
289
290 /* See if the counter was actually stopped */
291 dev_dbg(dev, "checking for presence of RTC\n");
292 ret = pcf2123_read(dev, PCF2123_REG_CTRL1, rxbuf, sizeof(rxbuf));
293 if (ret < 0)
294 return ret;
295
296 dev_dbg(dev, "received data from RTC (0x%02X 0x%02X)\n",
297 rxbuf[0], rxbuf[1]);
298 if (!(rxbuf[0] & CTRL1_STOP))
299 return -ENODEV;
300
301 /* Start the counter */
302 ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_CLEAR);
303 if (ret < 0)
304 return ret;
305
306 return 0;
307}
308
275static const struct rtc_class_ops pcf2123_rtc_ops = { 309static const struct rtc_class_ops pcf2123_rtc_ops = {
276 .read_time = pcf2123_rtc_read_time, 310 .read_time = pcf2123_rtc_read_time,
277 .set_time = pcf2123_rtc_set_time, 311 .set_time = pcf2123_rtc_set_time,
@@ -281,7 +315,6 @@ static int pcf2123_probe(struct spi_device *spi)
281{ 315{
282 struct rtc_device *rtc; 316 struct rtc_device *rtc;
283 struct pcf2123_plat_data *pdata; 317 struct pcf2123_plat_data *pdata;
284 u8 rxbuf[2];
285 int ret, i; 318 int ret, i;
286 319
287 pdata = devm_kzalloc(&spi->dev, sizeof(struct pcf2123_plat_data), 320 pdata = devm_kzalloc(&spi->dev, sizeof(struct pcf2123_plat_data),
@@ -290,29 +323,9 @@ static int pcf2123_probe(struct spi_device *spi)
290 return -ENOMEM; 323 return -ENOMEM;
291 spi->dev.platform_data = pdata; 324 spi->dev.platform_data = pdata;
292 325
293 /* Send a software reset command */ 326 ret = pcf2123_reset(&spi->dev);
294 dev_dbg(&spi->dev, "resetting RTC\n"); 327 if (ret < 0) {
295 ret = pcf2123_write_reg(&spi->dev, PCF2123_REG_CTRL1, CTRL1_SW_RESET);
296 if (ret < 0)
297 goto kfree_exit;
298
299 /* Stop the counter */
300 dev_dbg(&spi->dev, "stopping RTC\n");
301 ret = pcf2123_write_reg(&spi->dev, PCF2123_REG_CTRL1, CTRL1_STOP);
302 if (ret < 0)
303 goto kfree_exit;
304
305 /* See if the counter was actually stopped */
306 dev_dbg(&spi->dev, "checking for presence of RTC\n");
307 ret = pcf2123_read(&spi->dev, PCF2123_REG_CTRL1, rxbuf, sizeof(rxbuf));
308 dev_dbg(&spi->dev, "received data from RTC (0x%02X 0x%02X)\n",
309 rxbuf[0], rxbuf[1]);
310 if (ret < 0)
311 goto kfree_exit;
312
313 if (!(rxbuf[0] & 0x20)) {
314 dev_err(&spi->dev, "chip not found\n"); 328 dev_err(&spi->dev, "chip not found\n");
315 ret = -ENODEV;
316 goto kfree_exit; 329 goto kfree_exit;
317 } 330 }
318 331
@@ -320,11 +333,6 @@ static int pcf2123_probe(struct spi_device *spi)
320 dev_info(&spi->dev, "spiclk %u KHz.\n", 333 dev_info(&spi->dev, "spiclk %u KHz.\n",
321 (spi->max_speed_hz + 500) / 1000); 334 (spi->max_speed_hz + 500) / 1000);
322 335
323 /* Start the counter */
324 ret = pcf2123_write_reg(&spi->dev, PCF2123_REG_CTRL1, CTRL1_CLEAR);
325 if (ret < 0)
326 goto kfree_exit;
327
328 /* Finalize the initialization */ 336 /* Finalize the initialization */
329 rtc = devm_rtc_device_register(&spi->dev, pcf2123_driver.driver.name, 337 rtc = devm_rtc_device_register(&spi->dev, pcf2123_driver.driver.name,
330 &pcf2123_rtc_ops, THIS_MODULE); 338 &pcf2123_rtc_ops, THIS_MODULE);