| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * A SPI driver for the Ricoh RS5C348 RTC |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | * |
|---|
| 10 | 7 | * The board specific init code should provide characteristics of this |
|---|
| 11 | 8 | * device: |
|---|
| .. | .. |
|---|
| 66 | 63 | u8 txbuf[5+7], *txp; |
|---|
| 67 | 64 | int ret; |
|---|
| 68 | 65 | |
|---|
| 66 | + ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); |
|---|
| 67 | + if (ret < 0) |
|---|
| 68 | + return ret; |
|---|
| 69 | + if (ret & RS5C348_BIT_XSTP) { |
|---|
| 70 | + txbuf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2); |
|---|
| 71 | + txbuf[1] = 0; |
|---|
| 72 | + ret = spi_write_then_read(spi, txbuf, 2, NULL, 0); |
|---|
| 73 | + if (ret < 0) |
|---|
| 74 | + return ret; |
|---|
| 75 | + } |
|---|
| 76 | + |
|---|
| 69 | 77 | /* Transfer 5 bytes before writing SEC. This gives 31us for carry. */ |
|---|
| 70 | 78 | txp = txbuf; |
|---|
| 71 | 79 | txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */ |
|---|
| .. | .. |
|---|
| 101 | 109 | struct rs5c348_plat_data *pdata = dev_get_platdata(&spi->dev); |
|---|
| 102 | 110 | u8 txbuf[5], rxbuf[7]; |
|---|
| 103 | 111 | int ret; |
|---|
| 112 | + |
|---|
| 113 | + ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); |
|---|
| 114 | + if (ret < 0) |
|---|
| 115 | + return ret; |
|---|
| 116 | + if (ret & RS5C348_BIT_VDET) |
|---|
| 117 | + dev_warn(&spi->dev, "voltage-low detected.\n"); |
|---|
| 118 | + if (ret & RS5C348_BIT_XSTP) { |
|---|
| 119 | + dev_warn(&spi->dev, "oscillator-stop detected.\n"); |
|---|
| 120 | + return -EINVAL; |
|---|
| 121 | + } |
|---|
| 104 | 122 | |
|---|
| 105 | 123 | /* Transfer 5 byte befores reading SEC. This gives 31us for carry. */ |
|---|
| 106 | 124 | txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */ |
|---|
| .. | .. |
|---|
| 143 | 161 | .set_time = rs5c348_rtc_set_time, |
|---|
| 144 | 162 | }; |
|---|
| 145 | 163 | |
|---|
| 146 | | -static struct spi_driver rs5c348_driver; |
|---|
| 147 | | - |
|---|
| 148 | 164 | static int rs5c348_probe(struct spi_device *spi) |
|---|
| 149 | 165 | { |
|---|
| 150 | 166 | int ret; |
|---|
| .. | .. |
|---|
| 161 | 177 | ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_SECS)); |
|---|
| 162 | 178 | if (ret < 0 || (ret & 0x80)) { |
|---|
| 163 | 179 | dev_err(&spi->dev, "not found.\n"); |
|---|
| 164 | | - goto kfree_exit; |
|---|
| 180 | + return ret; |
|---|
| 165 | 181 | } |
|---|
| 166 | 182 | |
|---|
| 167 | 183 | dev_info(&spi->dev, "spiclk %u KHz.\n", |
|---|
| 168 | 184 | (spi->max_speed_hz + 500) / 1000); |
|---|
| 169 | 185 | |
|---|
| 170 | | - /* turn RTC on if it was not on */ |
|---|
| 171 | | - ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); |
|---|
| 172 | | - if (ret < 0) |
|---|
| 173 | | - goto kfree_exit; |
|---|
| 174 | | - if (ret & (RS5C348_BIT_XSTP | RS5C348_BIT_VDET)) { |
|---|
| 175 | | - u8 buf[2]; |
|---|
| 176 | | - struct rtc_time tm; |
|---|
| 177 | | - if (ret & RS5C348_BIT_VDET) |
|---|
| 178 | | - dev_warn(&spi->dev, "voltage-low detected.\n"); |
|---|
| 179 | | - if (ret & RS5C348_BIT_XSTP) |
|---|
| 180 | | - dev_warn(&spi->dev, "oscillator-stop detected.\n"); |
|---|
| 181 | | - rtc_time_to_tm(0, &tm); /* 1970/1/1 */ |
|---|
| 182 | | - ret = rs5c348_rtc_set_time(&spi->dev, &tm); |
|---|
| 183 | | - if (ret < 0) |
|---|
| 184 | | - goto kfree_exit; |
|---|
| 185 | | - buf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2); |
|---|
| 186 | | - buf[1] = 0; |
|---|
| 187 | | - ret = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0); |
|---|
| 188 | | - if (ret < 0) |
|---|
| 189 | | - goto kfree_exit; |
|---|
| 190 | | - } |
|---|
| 191 | | - |
|---|
| 192 | 186 | ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL1)); |
|---|
| 193 | 187 | if (ret < 0) |
|---|
| 194 | | - goto kfree_exit; |
|---|
| 188 | + return ret; |
|---|
| 195 | 189 | if (ret & RS5C348_BIT_24H) |
|---|
| 196 | 190 | pdata->rtc_24h = 1; |
|---|
| 197 | 191 | |
|---|
| 198 | | - rtc = devm_rtc_device_register(&spi->dev, rs5c348_driver.driver.name, |
|---|
| 199 | | - &rs5c348_rtc_ops, THIS_MODULE); |
|---|
| 200 | | - |
|---|
| 201 | | - if (IS_ERR(rtc)) { |
|---|
| 202 | | - ret = PTR_ERR(rtc); |
|---|
| 203 | | - goto kfree_exit; |
|---|
| 204 | | - } |
|---|
| 192 | + rtc = devm_rtc_allocate_device(&spi->dev); |
|---|
| 193 | + if (IS_ERR(rtc)) |
|---|
| 194 | + return PTR_ERR(rtc); |
|---|
| 205 | 195 | |
|---|
| 206 | 196 | pdata->rtc = rtc; |
|---|
| 207 | 197 | |
|---|
| 208 | | - return 0; |
|---|
| 209 | | - kfree_exit: |
|---|
| 210 | | - return ret; |
|---|
| 198 | + rtc->ops = &rs5c348_rtc_ops; |
|---|
| 199 | + |
|---|
| 200 | + return rtc_register_device(rtc); |
|---|
| 211 | 201 | } |
|---|
| 212 | 202 | |
|---|
| 213 | 203 | static struct spi_driver rs5c348_driver = { |
|---|