Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An SPI driver for the Philips PCF2123 RTC |
| 3 | * Copyright 2009 Cyber Switching, Inc. |
| 4 | * |
| 5 | * Author: Chris Verges <chrisv@cyberswitching.com> |
| 6 | * Maintainers: http://www.cyberswitching.com |
| 7 | * |
| 8 | * based on the RS5C348 driver in this same directory. |
| 9 | * |
| 10 | * Thanks to Christian Pellegrin <chripell@fsfe.org> for |
| 11 | * the sysfs contributions to this driver. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | * |
| 17 | * Please note that the CS is active high, so platform data |
| 18 | * should look something like: |
| 19 | * |
| 20 | * static struct spi_board_info ek_spi_devices[] = { |
Sachin Kamat | 369015f | 2013-07-03 15:06:01 -0700 | [diff] [blame] | 21 | * ... |
| 22 | * { |
| 23 | * .modalias = "rtc-pcf2123", |
| 24 | * .chip_select = 1, |
| 25 | * .controller_data = (void *)AT91_PIN_PA10, |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 26 | * .max_speed_hz = 1000 * 1000, |
| 27 | * .mode = SPI_CS_HIGH, |
| 28 | * .bus_num = 0, |
| 29 | * }, |
| 30 | * ... |
| 31 | *}; |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #include <linux/bcd.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/device.h> |
| 38 | #include <linux/errno.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/kernel.h> |
Joshua Clayton | 3fc7007 | 2015-02-13 14:40:29 -0800 | [diff] [blame] | 41 | #include <linux/of.h> |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 42 | #include <linux/string.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 44 | #include <linux/rtc.h> |
| 45 | #include <linux/spi/spi.h> |
Paul Gortmaker | 2113852 | 2011-05-27 09:57:25 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 47 | #include <linux/regmap.h> |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 48 | |
Joshua Clayton | 245cb74 | 2016-01-04 10:31:19 -0800 | [diff] [blame] | 49 | /* REGISTERS */ |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 50 | #define PCF2123_REG_CTRL1 (0x00) /* Control Register 1 */ |
| 51 | #define PCF2123_REG_CTRL2 (0x01) /* Control Register 2 */ |
| 52 | #define PCF2123_REG_SC (0x02) /* datetime */ |
| 53 | #define PCF2123_REG_MN (0x03) |
| 54 | #define PCF2123_REG_HR (0x04) |
| 55 | #define PCF2123_REG_DM (0x05) |
| 56 | #define PCF2123_REG_DW (0x06) |
| 57 | #define PCF2123_REG_MO (0x07) |
| 58 | #define PCF2123_REG_YR (0x08) |
Joshua Clayton | 245cb74 | 2016-01-04 10:31:19 -0800 | [diff] [blame] | 59 | #define PCF2123_REG_ALRM_MN (0x09) /* Alarm Registers */ |
| 60 | #define PCF2123_REG_ALRM_HR (0x0a) |
| 61 | #define PCF2123_REG_ALRM_DM (0x0b) |
| 62 | #define PCF2123_REG_ALRM_DW (0x0c) |
| 63 | #define PCF2123_REG_OFFSET (0x0d) /* Clock Rate Offset Register */ |
| 64 | #define PCF2123_REG_TMR_CLKOUT (0x0e) /* Timer Registers */ |
| 65 | #define PCF2123_REG_CTDWN_TMR (0x0f) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 66 | |
Joshua Clayton | 245cb74 | 2016-01-04 10:31:19 -0800 | [diff] [blame] | 67 | /* PCF2123_REG_CTRL1 BITS */ |
| 68 | #define CTRL1_CLEAR (0) /* Clear */ |
| 69 | #define CTRL1_CORR_INT BIT(1) /* Correction irq enable */ |
| 70 | #define CTRL1_12_HOUR BIT(2) /* 12 hour time */ |
| 71 | #define CTRL1_SW_RESET (BIT(3) | BIT(4) | BIT(6)) /* Software reset */ |
| 72 | #define CTRL1_STOP BIT(5) /* Stop the clock */ |
| 73 | #define CTRL1_EXT_TEST BIT(7) /* External clock test mode */ |
| 74 | |
| 75 | /* PCF2123_REG_CTRL2 BITS */ |
| 76 | #define CTRL2_TIE BIT(0) /* Countdown timer irq enable */ |
| 77 | #define CTRL2_AIE BIT(1) /* Alarm irq enable */ |
| 78 | #define CTRL2_TF BIT(2) /* Countdown timer flag */ |
| 79 | #define CTRL2_AF BIT(3) /* Alarm flag */ |
| 80 | #define CTRL2_TI_TP BIT(4) /* Irq pin generates pulse */ |
| 81 | #define CTRL2_MSF BIT(5) /* Minute or second irq flag */ |
| 82 | #define CTRL2_SI BIT(6) /* Second irq enable */ |
| 83 | #define CTRL2_MI BIT(7) /* Minute irq enable */ |
| 84 | |
| 85 | /* PCF2123_REG_SC BITS */ |
| 86 | #define OSC_HAS_STOPPED BIT(7) /* Clock has been stopped */ |
| 87 | |
| 88 | /* PCF2123_REG_ALRM_XX BITS */ |
| 89 | #define ALRM_ENABLE BIT(7) /* MN, HR, DM, or DW alarm enable */ |
| 90 | |
| 91 | /* PCF2123_REG_TMR_CLKOUT BITS */ |
| 92 | #define CD_TMR_4096KHZ (0) /* 4096 KHz countdown timer */ |
| 93 | #define CD_TMR_64HZ (1) /* 64 Hz countdown timer */ |
| 94 | #define CD_TMR_1HZ (2) /* 1 Hz countdown timer */ |
| 95 | #define CD_TMR_60th_HZ (3) /* 60th Hz countdown timer */ |
| 96 | #define CD_TMR_TE BIT(3) /* Countdown timer enable */ |
| 97 | |
| 98 | /* PCF2123_REG_OFFSET BITS */ |
Martin Kepplinger | 82df3e0 | 2016-04-18 12:17:44 +0200 | [diff] [blame] | 99 | #define OFFSET_SIGN_BIT 6 /* 2's complement sign bit */ |
Joshua Clayton | 245cb74 | 2016-01-04 10:31:19 -0800 | [diff] [blame] | 100 | #define OFFSET_COARSE BIT(7) /* Coarse mode offset */ |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 101 | #define OFFSET_STEP (2170) /* Offset step in parts per billion */ |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 102 | #define OFFSET_MASK GENMASK(6, 0) /* Offset value */ |
Joshua Clayton | 245cb74 | 2016-01-04 10:31:19 -0800 | [diff] [blame] | 103 | |
| 104 | /* READ/WRITE ADDRESS BITS */ |
| 105 | #define PCF2123_WRITE BIT(4) |
| 106 | #define PCF2123_READ (BIT(4) | BIT(7)) |
| 107 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 108 | |
| 109 | static struct spi_driver pcf2123_driver; |
| 110 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 111 | struct pcf2123_plat_data { |
| 112 | struct rtc_device *rtc; |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 113 | struct regmap *map; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 116 | static const struct regmap_config pcf2123_regmap_config = { |
| 117 | .reg_bits = 8, |
| 118 | .val_bits = 8, |
| 119 | .read_flag_mask = PCF2123_READ, |
| 120 | .write_flag_mask = PCF2123_WRITE, |
| 121 | .max_register = PCF2123_REG_CTDWN_TMR, |
| 122 | }; |
Joshua Clayton | 809b453 | 2016-01-04 10:31:21 -0800 | [diff] [blame] | 123 | |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 124 | static int pcf2123_read_offset(struct device *dev, long *offset) |
| 125 | { |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 126 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
| 127 | int ret, val; |
| 128 | unsigned int reg; |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 129 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 130 | ret = regmap_read(pdata->map, PCF2123_REG_OFFSET, ®); |
| 131 | if (ret) |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 132 | return ret; |
| 133 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 134 | val = sign_extend32((reg & OFFSET_MASK), OFFSET_SIGN_BIT); |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 135 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 136 | if (reg & OFFSET_COARSE) |
| 137 | val *= 2; |
| 138 | |
| 139 | *offset = ((long)val) * OFFSET_STEP; |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * The offset register is a 7 bit signed value with a coarse bit in bit 7. |
| 146 | * The main difference between the two is normal offset adjusts the first |
| 147 | * second of n minutes every other hour, with 61, 62 and 63 being shoved |
| 148 | * into the 60th minute. |
| 149 | * The coarse adjustment does the same, but every hour. |
| 150 | * the two overlap, with every even normal offset value corresponding |
| 151 | * to a coarse offset. Based on this algorithm, it seems that despite the |
| 152 | * name, coarse offset is a better fit for overlapping values. |
| 153 | */ |
| 154 | static int pcf2123_set_offset(struct device *dev, long offset) |
| 155 | { |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 156 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 157 | s8 reg; |
| 158 | |
| 159 | if (offset > OFFSET_STEP * 127) |
| 160 | reg = 127; |
| 161 | else if (offset < OFFSET_STEP * -128) |
| 162 | reg = -128; |
| 163 | else |
| 164 | reg = (s8)((offset + (OFFSET_STEP >> 1)) / OFFSET_STEP); |
| 165 | |
| 166 | /* choose fine offset only for odd values in the normal range */ |
| 167 | if (reg & 1 && reg <= 63 && reg >= -64) { |
| 168 | /* Normal offset. Clear the coarse bit */ |
| 169 | reg &= ~OFFSET_COARSE; |
| 170 | } else { |
| 171 | /* Coarse offset. Divide by 2 and set the coarse bit */ |
| 172 | reg >>= 1; |
| 173 | reg |= OFFSET_COARSE; |
| 174 | } |
| 175 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 176 | return regmap_write(pdata->map, PCF2123_REG_OFFSET, (unsigned int)reg); |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 179 | static int pcf2123_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 180 | { |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 181 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
Joshua Clayton | 66c056d | 2016-01-04 10:31:20 -0800 | [diff] [blame] | 182 | u8 rxbuf[7]; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 183 | int ret; |
| 184 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 185 | ret = regmap_bulk_read(pdata->map, PCF2123_REG_SC, rxbuf, |
| 186 | sizeof(rxbuf)); |
| 187 | if (ret) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 188 | return ret; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 189 | |
Joshua Clayton | f07fa92 | 2016-01-04 10:31:23 -0800 | [diff] [blame] | 190 | if (rxbuf[0] & OSC_HAS_STOPPED) { |
| 191 | dev_info(dev, "clock was stopped. Time is not valid\n"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 195 | tm->tm_sec = bcd2bin(rxbuf[0] & 0x7F); |
| 196 | tm->tm_min = bcd2bin(rxbuf[1] & 0x7F); |
| 197 | tm->tm_hour = bcd2bin(rxbuf[2] & 0x3F); /* rtc hr 0-23 */ |
| 198 | tm->tm_mday = bcd2bin(rxbuf[3] & 0x3F); |
| 199 | tm->tm_wday = rxbuf[4] & 0x07; |
| 200 | tm->tm_mon = bcd2bin(rxbuf[5] & 0x1F) - 1; /* rtc mn 1-12 */ |
| 201 | tm->tm_year = bcd2bin(rxbuf[6]); |
| 202 | if (tm->tm_year < 70) |
| 203 | tm->tm_year += 100; /* assume we are in 1970...2069 */ |
| 204 | |
Dylan Howey | c33850b | 2019-05-03 19:52:12 +0000 | [diff] [blame] | 205 | dev_dbg(dev, "%s: tm is %ptR\n", __func__, tm); |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 206 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 207 | return 0; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int pcf2123_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 211 | { |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 212 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
| 213 | u8 txbuf[7]; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 214 | int ret; |
| 215 | |
Dylan Howey | c33850b | 2019-05-03 19:52:12 +0000 | [diff] [blame] | 216 | dev_dbg(dev, "%s: tm is %ptR\n", __func__, tm); |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 217 | |
| 218 | /* Stop the counter first */ |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 219 | ret = regmap_write(pdata->map, PCF2123_REG_CTRL1, CTRL1_STOP); |
| 220 | if (ret) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 221 | return ret; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 222 | |
| 223 | /* Set the new time */ |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 224 | txbuf[0] = bin2bcd(tm->tm_sec & 0x7F); |
| 225 | txbuf[1] = bin2bcd(tm->tm_min & 0x7F); |
| 226 | txbuf[2] = bin2bcd(tm->tm_hour & 0x3F); |
| 227 | txbuf[3] = bin2bcd(tm->tm_mday & 0x3F); |
| 228 | txbuf[4] = tm->tm_wday & 0x07; |
| 229 | txbuf[5] = bin2bcd((tm->tm_mon + 1) & 0x1F); /* rtc mn 1-12 */ |
| 230 | txbuf[6] = bin2bcd(tm->tm_year < 100 ? tm->tm_year : tm->tm_year - 100); |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 231 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 232 | ret = regmap_bulk_write(pdata->map, PCF2123_REG_SC, txbuf, |
| 233 | sizeof(txbuf)); |
| 234 | if (ret) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 235 | return ret; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 236 | |
| 237 | /* Start the counter */ |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 238 | ret = regmap_write(pdata->map, PCF2123_REG_CTRL1, CTRL1_CLEAR); |
| 239 | if (ret) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 240 | return ret; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Dylan Howey | e32e60a | 2019-05-03 19:52:12 +0000 | [diff] [blame^] | 245 | static int pcf2123_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 246 | { |
| 247 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
| 248 | u8 rxbuf[4]; |
| 249 | int ret; |
| 250 | unsigned int val = 0; |
| 251 | |
| 252 | ret = regmap_bulk_read(pdata->map, PCF2123_REG_ALRM_MN, rxbuf, |
| 253 | sizeof(rxbuf)); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | |
| 257 | alm->time.tm_min = bcd2bin(rxbuf[0] & 0x7F); |
| 258 | alm->time.tm_hour = bcd2bin(rxbuf[1] & 0x3F); |
| 259 | alm->time.tm_mday = bcd2bin(rxbuf[2] & 0x3F); |
| 260 | alm->time.tm_wday = bcd2bin(rxbuf[3] & 0x07); |
| 261 | |
| 262 | dev_dbg(dev, "%s: alm is %ptR\n", __func__, &alm->time); |
| 263 | |
| 264 | ret = regmap_read(pdata->map, PCF2123_REG_CTRL2, &val); |
| 265 | if (ret) |
| 266 | return ret; |
| 267 | |
| 268 | alm->enabled = !!(val & CTRL2_AIE); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int pcf2123_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 274 | { |
| 275 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
| 276 | u8 txbuf[4]; |
| 277 | int ret; |
| 278 | |
| 279 | dev_dbg(dev, "%s: alm is %ptR\n", __func__, &alm->time); |
| 280 | |
| 281 | /* Ensure alarm flag is clear */ |
| 282 | ret = regmap_update_bits(pdata->map, PCF2123_REG_CTRL2, CTRL2_AF, 0); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | /* Disable alarm interrupt */ |
| 287 | ret = regmap_update_bits(pdata->map, PCF2123_REG_CTRL2, CTRL2_AIE, 0); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
| 291 | /* Set new alarm */ |
| 292 | txbuf[0] = bin2bcd(alm->time.tm_min & 0x7F); |
| 293 | txbuf[1] = bin2bcd(alm->time.tm_hour & 0x3F); |
| 294 | txbuf[2] = bin2bcd(alm->time.tm_mday & 0x3F); |
| 295 | txbuf[3] = bin2bcd(alm->time.tm_wday & 0x07); |
| 296 | |
| 297 | ret = regmap_bulk_write(pdata->map, PCF2123_REG_ALRM_MN, txbuf, |
| 298 | sizeof(txbuf)); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | |
| 302 | /* Enable alarm interrupt */ |
| 303 | if (alm->enabled) { |
| 304 | ret = regmap_update_bits(pdata->map, PCF2123_REG_CTRL2, |
| 305 | CTRL2_AIE, CTRL2_AIE); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static irqreturn_t pcf2123_rtc_irq(int irq, void *dev) |
| 314 | { |
| 315 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
| 316 | struct mutex *lock = &pdata->rtc->ops_lock; |
| 317 | unsigned int val = 0; |
| 318 | int ret = IRQ_NONE; |
| 319 | |
| 320 | mutex_lock(lock); |
| 321 | regmap_read(pdata->map, PCF2123_REG_CTRL2, &val); |
| 322 | |
| 323 | /* Alarm? */ |
| 324 | if (val & CTRL2_AF) { |
| 325 | ret = IRQ_HANDLED; |
| 326 | |
| 327 | /* Clear alarm flag */ |
| 328 | regmap_update_bits(pdata->map, PCF2123_REG_CTRL2, CTRL2_AF, 0); |
| 329 | |
| 330 | rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF); |
| 331 | } |
| 332 | |
| 333 | mutex_unlock(lock); |
| 334 | |
| 335 | return ret; |
| 336 | } |
| 337 | |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 338 | static int pcf2123_reset(struct device *dev) |
| 339 | { |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 340 | struct pcf2123_plat_data *pdata = dev_get_platdata(dev); |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 341 | int ret; |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 342 | unsigned int val = 0; |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 343 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 344 | ret = regmap_write(pdata->map, PCF2123_REG_CTRL1, CTRL1_SW_RESET); |
| 345 | if (ret) |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 346 | return ret; |
| 347 | |
| 348 | /* Stop the counter */ |
| 349 | dev_dbg(dev, "stopping RTC\n"); |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 350 | ret = regmap_write(pdata->map, PCF2123_REG_CTRL1, CTRL1_STOP); |
| 351 | if (ret) |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 352 | return ret; |
| 353 | |
| 354 | /* See if the counter was actually stopped */ |
| 355 | dev_dbg(dev, "checking for presence of RTC\n"); |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 356 | ret = regmap_read(pdata->map, PCF2123_REG_CTRL1, &val); |
| 357 | if (ret) |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 358 | return ret; |
| 359 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 360 | dev_dbg(dev, "received data from RTC (0x%08X)\n", val); |
| 361 | if (!(val & CTRL1_STOP)) |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 362 | return -ENODEV; |
| 363 | |
| 364 | /* Start the counter */ |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 365 | ret = regmap_write(pdata->map, PCF2123_REG_CTRL1, CTRL1_CLEAR); |
| 366 | if (ret) |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 367 | return ret; |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 372 | static const struct rtc_class_ops pcf2123_rtc_ops = { |
| 373 | .read_time = pcf2123_rtc_read_time, |
| 374 | .set_time = pcf2123_rtc_set_time, |
Joshua Clayton | bae2f64 | 2016-02-05 12:41:13 -0800 | [diff] [blame] | 375 | .read_offset = pcf2123_read_offset, |
| 376 | .set_offset = pcf2123_set_offset, |
Dylan Howey | e32e60a | 2019-05-03 19:52:12 +0000 | [diff] [blame^] | 377 | .read_alarm = pcf2123_rtc_read_alarm, |
| 378 | .set_alarm = pcf2123_rtc_set_alarm, |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 379 | }; |
| 380 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 381 | static int pcf2123_probe(struct spi_device *spi) |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 382 | { |
| 383 | struct rtc_device *rtc; |
Joshua Clayton | f07fa92 | 2016-01-04 10:31:23 -0800 | [diff] [blame] | 384 | struct rtc_time tm; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 385 | struct pcf2123_plat_data *pdata; |
Dylan Howey | e32e60a | 2019-05-03 19:52:12 +0000 | [diff] [blame^] | 386 | int ret = 0; |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 387 | |
Jingoo Han | dd48ccc | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 388 | pdata = devm_kzalloc(&spi->dev, sizeof(struct pcf2123_plat_data), |
| 389 | GFP_KERNEL); |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 390 | if (!pdata) |
| 391 | return -ENOMEM; |
| 392 | spi->dev.platform_data = pdata; |
| 393 | |
Dylan Howey | 790d033 | 2019-05-03 19:52:10 +0000 | [diff] [blame] | 394 | pdata->map = devm_regmap_init_spi(spi, &pcf2123_regmap_config); |
| 395 | |
| 396 | if (IS_ERR(pdata->map)) { |
| 397 | dev_err(&spi->dev, "regmap init failed.\n"); |
| 398 | goto kfree_exit; |
| 399 | } |
| 400 | |
Joshua Clayton | f07fa92 | 2016-01-04 10:31:23 -0800 | [diff] [blame] | 401 | ret = pcf2123_rtc_read_time(&spi->dev, &tm); |
Joshua Clayton | 1e094b9 | 2016-01-04 10:31:22 -0800 | [diff] [blame] | 402 | if (ret < 0) { |
Joshua Clayton | f07fa92 | 2016-01-04 10:31:23 -0800 | [diff] [blame] | 403 | ret = pcf2123_reset(&spi->dev); |
| 404 | if (ret < 0) { |
| 405 | dev_err(&spi->dev, "chip not found\n"); |
| 406 | goto kfree_exit; |
| 407 | } |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 410 | dev_info(&spi->dev, "spiclk %u KHz.\n", |
| 411 | (spi->max_speed_hz + 500) / 1000); |
| 412 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 413 | /* Finalize the initialization */ |
Jingoo Han | dd48ccc | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 414 | rtc = devm_rtc_device_register(&spi->dev, pcf2123_driver.driver.name, |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 415 | &pcf2123_rtc_ops, THIS_MODULE); |
| 416 | |
| 417 | if (IS_ERR(rtc)) { |
| 418 | dev_err(&spi->dev, "failed to register.\n"); |
| 419 | ret = PTR_ERR(rtc); |
| 420 | goto kfree_exit; |
| 421 | } |
| 422 | |
| 423 | pdata->rtc = rtc; |
| 424 | |
Dylan Howey | e32e60a | 2019-05-03 19:52:12 +0000 | [diff] [blame^] | 425 | /* Register alarm irq */ |
| 426 | if (spi->irq > 0) { |
| 427 | ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, |
| 428 | pcf2123_rtc_irq, |
| 429 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 430 | pcf2123_driver.driver.name, &spi->dev); |
| 431 | if (!ret) |
| 432 | device_init_wakeup(&spi->dev, true); |
| 433 | else |
| 434 | dev_err(&spi->dev, "could not request irq.\n"); |
| 435 | } |
| 436 | |
| 437 | /* The PCF2123's alarm only has minute accuracy. Must add timer |
| 438 | * support to this driver to generate interrupts more than once |
| 439 | * per minute. |
| 440 | */ |
| 441 | pdata->rtc->uie_unsupported = 1; |
| 442 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 443 | return 0; |
Chris Verges | f3d2570a | 2009-09-22 16:46:22 -0700 | [diff] [blame] | 444 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 445 | kfree_exit: |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 446 | spi->dev.platform_data = NULL; |
| 447 | return ret; |
| 448 | } |
| 449 | |
Joshua Clayton | 3fc7007 | 2015-02-13 14:40:29 -0800 | [diff] [blame] | 450 | #ifdef CONFIG_OF |
| 451 | static const struct of_device_id pcf2123_dt_ids[] = { |
| 452 | { .compatible = "nxp,rtc-pcf2123", }, |
Alexandre Belloni | 3c3d710 | 2018-12-18 22:42:23 +0100 | [diff] [blame] | 453 | { .compatible = "microcrystal,rv2123", }, |
Joshua Clayton | 3fc7007 | 2015-02-13 14:40:29 -0800 | [diff] [blame] | 454 | { /* sentinel */ } |
| 455 | }; |
| 456 | MODULE_DEVICE_TABLE(of, pcf2123_dt_ids); |
| 457 | #endif |
| 458 | |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 459 | static struct spi_driver pcf2123_driver = { |
| 460 | .driver = { |
| 461 | .name = "rtc-pcf2123", |
Joshua Clayton | 3fc7007 | 2015-02-13 14:40:29 -0800 | [diff] [blame] | 462 | .of_match_table = of_match_ptr(pcf2123_dt_ids), |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 463 | }, |
| 464 | .probe = pcf2123_probe, |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 467 | module_spi_driver(pcf2123_driver); |
Chris Verges | 7f3923a | 2009-09-22 16:46:20 -0700 | [diff] [blame] | 468 | |
| 469 | MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>"); |
| 470 | MODULE_DESCRIPTION("NXP PCF2123 RTC driver"); |
| 471 | MODULE_LICENSE("GPL"); |