Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Driver for the Epson RTC module RX-8010 SJ |
| 4 | * |
| 5 | * Copyright(C) Timesys Corporation 2015 |
| 6 | * Copyright(C) General Electric Company 2015 |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/bcd.h> |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 14 | #include <linux/regmap.h> |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 15 | #include <linux/rtc.h> |
| 16 | |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 17 | #define RX8010_SEC 0x10 |
| 18 | #define RX8010_MIN 0x11 |
| 19 | #define RX8010_HOUR 0x12 |
| 20 | #define RX8010_WDAY 0x13 |
| 21 | #define RX8010_MDAY 0x14 |
| 22 | #define RX8010_MONTH 0x15 |
| 23 | #define RX8010_YEAR 0x16 |
| 24 | #define RX8010_RESV17 0x17 |
| 25 | #define RX8010_ALMIN 0x18 |
| 26 | #define RX8010_ALHOUR 0x19 |
| 27 | #define RX8010_ALWDAY 0x1A |
| 28 | #define RX8010_TCOUNT0 0x1B |
| 29 | #define RX8010_TCOUNT1 0x1C |
| 30 | #define RX8010_EXT 0x1D |
| 31 | #define RX8010_FLAG 0x1E |
| 32 | #define RX8010_CTRL 0x1F |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 33 | /* 0x20 to 0x2F are user registers */ |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 34 | #define RX8010_RESV30 0x30 |
| 35 | #define RX8010_RESV31 0x31 |
| 36 | #define RX8010_IRQ 0x32 |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 37 | |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 38 | #define RX8010_EXT_WADA BIT(3) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 39 | |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 40 | #define RX8010_FLAG_VLF BIT(1) |
| 41 | #define RX8010_FLAG_AF BIT(3) |
| 42 | #define RX8010_FLAG_TF BIT(4) |
| 43 | #define RX8010_FLAG_UF BIT(5) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 44 | |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 45 | #define RX8010_CTRL_AIE BIT(3) |
| 46 | #define RX8010_CTRL_UIE BIT(5) |
| 47 | #define RX8010_CTRL_STOP BIT(6) |
| 48 | #define RX8010_CTRL_TEST BIT(7) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 49 | |
Bartosz Golaszewski | e9e4c2d | 2020-09-14 17:45:52 +0200 | [diff] [blame] | 50 | #define RX8010_ALARM_AE BIT(7) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 51 | |
| 52 | static const struct i2c_device_id rx8010_id[] = { |
| 53 | { "rx8010", 0 }, |
| 54 | { } |
| 55 | }; |
| 56 | MODULE_DEVICE_TABLE(i2c, rx8010_id); |
| 57 | |
Alexandre Belloni | 782d3eb | 2021-02-02 12:22:15 +0100 | [diff] [blame] | 58 | static const __maybe_unused struct of_device_id rx8010_of_match[] = { |
Javier Martinez Canillas | 81b779c | 2017-03-03 11:29:16 -0300 | [diff] [blame] | 59 | { .compatible = "epson,rx8010" }, |
| 60 | { } |
| 61 | }; |
| 62 | MODULE_DEVICE_TABLE(of, rx8010_of_match); |
| 63 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 64 | struct rx8010_data { |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 65 | struct regmap *regs; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 66 | struct rtc_device *rtc; |
| 67 | u8 ctrlreg; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id) |
| 71 | { |
| 72 | struct i2c_client *client = dev_id; |
| 73 | struct rx8010_data *rx8010 = i2c_get_clientdata(client); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 74 | int flagreg, err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 75 | |
Alexandre Belloni | 2dbbedb | 2021-01-19 23:06:50 +0100 | [diff] [blame] | 76 | rtc_lock(rx8010->rtc); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 77 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 78 | err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); |
| 79 | if (err) { |
Alexandre Belloni | 2dbbedb | 2021-01-19 23:06:50 +0100 | [diff] [blame] | 80 | rtc_unlock(rx8010->rtc); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 81 | return IRQ_NONE; |
| 82 | } |
| 83 | |
| 84 | if (flagreg & RX8010_FLAG_VLF) |
| 85 | dev_warn(&client->dev, "Frequency stop detected\n"); |
| 86 | |
| 87 | if (flagreg & RX8010_FLAG_TF) { |
| 88 | flagreg &= ~RX8010_FLAG_TF; |
| 89 | rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF); |
| 90 | } |
| 91 | |
| 92 | if (flagreg & RX8010_FLAG_AF) { |
| 93 | flagreg &= ~RX8010_FLAG_AF; |
| 94 | rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF); |
| 95 | } |
| 96 | |
| 97 | if (flagreg & RX8010_FLAG_UF) { |
| 98 | flagreg &= ~RX8010_FLAG_UF; |
| 99 | rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF); |
| 100 | } |
| 101 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 102 | err = regmap_write(rx8010->regs, RX8010_FLAG, flagreg); |
Alexandre Belloni | 2dbbedb | 2021-01-19 23:06:50 +0100 | [diff] [blame] | 103 | rtc_unlock(rx8010->rtc); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 104 | return err ? IRQ_NONE : IRQ_HANDLED; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int rx8010_get_time(struct device *dev, struct rtc_time *dt) |
| 108 | { |
| 109 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Bartosz Golaszewski | f702699 | 2020-09-14 17:45:54 +0200 | [diff] [blame] | 110 | u8 date[RX8010_YEAR - RX8010_SEC + 1]; |
Bartosz Golaszewski | 7567797 | 2020-09-14 17:45:51 +0200 | [diff] [blame] | 111 | int flagreg, err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 112 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 113 | err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); |
| 114 | if (err) |
| 115 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 116 | |
| 117 | if (flagreg & RX8010_FLAG_VLF) { |
| 118 | dev_warn(dev, "Frequency stop detected\n"); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 122 | err = regmap_bulk_read(rx8010->regs, RX8010_SEC, date, sizeof(date)); |
| 123 | if (err) |
| 124 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 125 | |
| 126 | dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f); |
| 127 | dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f); |
| 128 | dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f); |
| 129 | dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f); |
| 130 | dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1; |
| 131 | dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100; |
| 132 | dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f); |
| 133 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 134 | return 0; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int rx8010_set_time(struct device *dev, struct rtc_time *dt) |
| 138 | { |
| 139 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Bartosz Golaszewski | f702699 | 2020-09-14 17:45:54 +0200 | [diff] [blame] | 140 | u8 date[RX8010_YEAR - RX8010_SEC + 1]; |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 141 | int err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 142 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 143 | /* set STOP bit before changing clock/calendar */ |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 144 | err = regmap_set_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP); |
| 145 | if (err) |
Bartosz Golaszewski | 13952c9 | 2020-09-14 17:45:53 +0200 | [diff] [blame] | 146 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 147 | |
| 148 | date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec); |
| 149 | date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min); |
| 150 | date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour); |
| 151 | date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday); |
| 152 | date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1); |
| 153 | date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100); |
| 154 | date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday); |
| 155 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 156 | err = regmap_bulk_write(rx8010->regs, RX8010_SEC, date, sizeof(date)); |
| 157 | if (err) |
Bartosz Golaszewski | 13952c9 | 2020-09-14 17:45:53 +0200 | [diff] [blame] | 158 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 159 | |
| 160 | /* clear STOP bit after changing clock/calendar */ |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 161 | err = regmap_clear_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP); |
| 162 | if (err) |
Bartosz Golaszewski | 13952c9 | 2020-09-14 17:45:53 +0200 | [diff] [blame] | 163 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 164 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 165 | err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_VLF); |
| 166 | if (err) |
| 167 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 168 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Bartosz Golaszewski | ba1bcaf | 2020-09-17 13:46:56 +0200 | [diff] [blame] | 172 | static int rx8010_init(struct device *dev) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 173 | { |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 174 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 175 | u8 ctrl[2]; |
Bartosz Golaszewski | b3ff7fd | 2020-09-14 17:45:55 +0200 | [diff] [blame] | 176 | int need_clear = 0, err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 177 | |
| 178 | /* Initialize reserved registers as specified in datasheet */ |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 179 | err = regmap_write(rx8010->regs, RX8010_RESV17, 0xD8); |
| 180 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 181 | return err; |
| 182 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 183 | err = regmap_write(rx8010->regs, RX8010_RESV30, 0x00); |
| 184 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 185 | return err; |
| 186 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 187 | err = regmap_write(rx8010->regs, RX8010_RESV31, 0x08); |
| 188 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 189 | return err; |
| 190 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 191 | err = regmap_write(rx8010->regs, RX8010_IRQ, 0x00); |
| 192 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 193 | return err; |
| 194 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 195 | err = regmap_bulk_read(rx8010->regs, RX8010_FLAG, ctrl, 2); |
| 196 | if (err) |
| 197 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 198 | |
| 199 | if (ctrl[0] & RX8010_FLAG_VLF) |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 200 | dev_warn(dev, "Frequency stop was detected\n"); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 201 | |
| 202 | if (ctrl[0] & RX8010_FLAG_AF) { |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 203 | dev_warn(dev, "Alarm was detected\n"); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 204 | need_clear = 1; |
| 205 | } |
| 206 | |
| 207 | if (ctrl[0] & RX8010_FLAG_TF) |
| 208 | need_clear = 1; |
| 209 | |
| 210 | if (ctrl[0] & RX8010_FLAG_UF) |
| 211 | need_clear = 1; |
| 212 | |
| 213 | if (need_clear) { |
| 214 | ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 215 | err = regmap_write(rx8010->regs, RX8010_FLAG, ctrl[0]); |
| 216 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 217 | return err; |
| 218 | } |
| 219 | |
| 220 | rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST); |
| 221 | |
Akshay Bhat | 804a6cf | 2017-11-08 14:58:14 -0500 | [diff] [blame] | 222 | return 0; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 226 | { |
| 227 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 228 | u8 alarmvals[3]; |
Bartosz Golaszewski | 7567797 | 2020-09-14 17:45:51 +0200 | [diff] [blame] | 229 | int flagreg, err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 230 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 231 | err = regmap_bulk_read(rx8010->regs, RX8010_ALMIN, alarmvals, 3); |
| 232 | if (err) |
| 233 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 234 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 235 | err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); |
| 236 | if (err) |
| 237 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 238 | |
| 239 | t->time.tm_sec = 0; |
| 240 | t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); |
| 241 | t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); |
| 242 | |
Uwe Kleine-König | 56d86a7 | 2016-06-28 10:43:45 +0200 | [diff] [blame] | 243 | if (!(alarmvals[2] & RX8010_ALARM_AE)) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 244 | t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f); |
| 245 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 246 | t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE); |
| 247 | t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled; |
| 248 | |
Akshay Bhat | 804a6cf | 2017-11-08 14:58:14 -0500 | [diff] [blame] | 249 | return 0; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 253 | { |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 254 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
| 255 | u8 alarmvals[3]; |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 256 | int err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 257 | |
| 258 | if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) { |
| 259 | rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 260 | err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); |
| 261 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 262 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 265 | err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF); |
| 266 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 267 | return err; |
| 268 | |
| 269 | alarmvals[0] = bin2bcd(t->time.tm_min); |
| 270 | alarmvals[1] = bin2bcd(t->time.tm_hour); |
| 271 | alarmvals[2] = bin2bcd(t->time.tm_mday); |
| 272 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 273 | err = regmap_bulk_write(rx8010->regs, RX8010_ALMIN, alarmvals, 2); |
| 274 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 275 | return err; |
| 276 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 277 | err = regmap_clear_bits(rx8010->regs, RX8010_EXT, RX8010_EXT_WADA); |
| 278 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 279 | return err; |
| 280 | |
| 281 | if (alarmvals[2] == 0) |
| 282 | alarmvals[2] |= RX8010_ALARM_AE; |
| 283 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 284 | err = regmap_write(rx8010->regs, RX8010_ALWDAY, alarmvals[2]); |
| 285 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 286 | return err; |
| 287 | |
| 288 | if (t->enabled) { |
| 289 | if (rx8010->rtc->uie_rtctimer.enabled) |
| 290 | rx8010->ctrlreg |= RX8010_CTRL_UIE; |
| 291 | if (rx8010->rtc->aie_timer.enabled) |
| 292 | rx8010->ctrlreg |= |
| 293 | (RX8010_CTRL_AIE | RX8010_CTRL_UIE); |
| 294 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 295 | err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); |
| 296 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 297 | return err; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int rx8010_alarm_irq_enable(struct device *dev, |
| 304 | unsigned int enabled) |
| 305 | { |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 306 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 307 | int err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 308 | u8 ctrl; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 309 | |
| 310 | ctrl = rx8010->ctrlreg; |
| 311 | |
| 312 | if (enabled) { |
| 313 | if (rx8010->rtc->uie_rtctimer.enabled) |
| 314 | ctrl |= RX8010_CTRL_UIE; |
| 315 | if (rx8010->rtc->aie_timer.enabled) |
| 316 | ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE); |
| 317 | } else { |
| 318 | if (!rx8010->rtc->uie_rtctimer.enabled) |
| 319 | ctrl &= ~RX8010_CTRL_UIE; |
| 320 | if (!rx8010->rtc->aie_timer.enabled) |
| 321 | ctrl &= ~RX8010_CTRL_AIE; |
| 322 | } |
| 323 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 324 | err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF); |
| 325 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 326 | return err; |
| 327 | |
| 328 | if (ctrl != rx8010->ctrlreg) { |
| 329 | rx8010->ctrlreg = ctrl; |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 330 | err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); |
| 331 | if (err) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 332 | return err; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 339 | { |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 340 | struct rx8010_data *rx8010 = dev_get_drvdata(dev); |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 341 | int tmp, flagreg, err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 342 | |
| 343 | switch (cmd) { |
| 344 | case RTC_VL_READ: |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 345 | err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); |
| 346 | if (err) |
| 347 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 348 | |
Alexandre Belloni | 8049c11 | 2019-12-14 23:02:59 +0100 | [diff] [blame] | 349 | tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0; |
| 350 | return put_user(tmp, (unsigned int __user *)arg); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 351 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 352 | default: |
| 353 | return -ENOIOCTLCMD; |
| 354 | } |
| 355 | } |
| 356 | |
Alexandre Belloni | aaaea29 | 2021-01-11 00:17:44 +0100 | [diff] [blame] | 357 | static const struct rtc_class_ops rx8010_rtc_ops = { |
Bartosz Golaszewski | d3b1429 | 2020-09-14 17:45:48 +0200 | [diff] [blame] | 358 | .read_time = rx8010_get_time, |
| 359 | .set_time = rx8010_set_time, |
| 360 | .ioctl = rx8010_ioctl, |
| 361 | .read_alarm = rx8010_read_alarm, |
| 362 | .set_alarm = rx8010_set_alarm, |
| 363 | .alarm_irq_enable = rx8010_alarm_irq_enable, |
| 364 | }; |
| 365 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 366 | static const struct regmap_config rx8010_regmap_config = { |
| 367 | .name = "rx8010-rtc", |
| 368 | .reg_bits = 8, |
| 369 | .val_bits = 8, |
| 370 | }; |
| 371 | |
Bartosz Golaszewski | cee015d | 2020-09-14 17:45:59 +0200 | [diff] [blame] | 372 | static int rx8010_probe(struct i2c_client *client) |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 373 | { |
Bartosz Golaszewski | 955a123 | 2020-09-14 17:45:56 +0200 | [diff] [blame] | 374 | struct device *dev = &client->dev; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 375 | struct rx8010_data *rx8010; |
| 376 | int err = 0; |
| 377 | |
Bartosz Golaszewski | 666f214 | 2020-09-14 17:45:57 +0200 | [diff] [blame] | 378 | rx8010 = devm_kzalloc(dev, sizeof(*rx8010), GFP_KERNEL); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 379 | if (!rx8010) |
| 380 | return -ENOMEM; |
| 381 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 382 | i2c_set_clientdata(client, rx8010); |
| 383 | |
Bartosz Golaszewski | 9868bc1 | 2020-09-14 17:46:00 +0200 | [diff] [blame] | 384 | rx8010->regs = devm_regmap_init_i2c(client, &rx8010_regmap_config); |
| 385 | if (IS_ERR(rx8010->regs)) |
| 386 | return PTR_ERR(rx8010->regs); |
| 387 | |
Bartosz Golaszewski | ba1bcaf | 2020-09-17 13:46:56 +0200 | [diff] [blame] | 388 | err = rx8010_init(dev); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 389 | if (err) |
| 390 | return err; |
| 391 | |
Bartosz Golaszewski | 0ce6277 | 2020-09-14 17:45:58 +0200 | [diff] [blame] | 392 | rx8010->rtc = devm_rtc_allocate_device(dev); |
| 393 | if (IS_ERR(rx8010->rtc)) |
| 394 | return PTR_ERR(rx8010->rtc); |
| 395 | |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 396 | if (client->irq > 0) { |
Bartosz Golaszewski | 955a123 | 2020-09-14 17:45:56 +0200 | [diff] [blame] | 397 | dev_info(dev, "IRQ %d supplied\n", client->irq); |
| 398 | err = devm_request_threaded_irq(dev, client->irq, NULL, |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 399 | rx8010_irq_1_handler, |
| 400 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 401 | "rx8010", client); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 402 | if (err) { |
Bartosz Golaszewski | 955a123 | 2020-09-14 17:45:56 +0200 | [diff] [blame] | 403 | dev_err(dev, "unable to request IRQ\n"); |
Bartosz Golaszewski | d3b1429 | 2020-09-14 17:45:48 +0200 | [diff] [blame] | 404 | return err; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 405 | } |
Bartosz Golaszewski | d3b1429 | 2020-09-14 17:45:48 +0200 | [diff] [blame] | 406 | } else { |
Alexandre Belloni | aaaea29 | 2021-01-11 00:17:44 +0100 | [diff] [blame] | 407 | clear_bit(RTC_FEATURE_ALARM, rx8010->rtc->features); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Alexandre Belloni | aaaea29 | 2021-01-11 00:17:44 +0100 | [diff] [blame] | 410 | rx8010->rtc->ops = &rx8010_rtc_ops; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 411 | rx8010->rtc->max_user_freq = 1; |
Bartosz Golaszewski | 2fc1af3 | 2020-09-14 17:46:01 +0200 | [diff] [blame] | 412 | rx8010->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
| 413 | rx8010->rtc->range_max = RTC_TIMESTAMP_END_2099; |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 414 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 415 | return devm_rtc_register_device(rx8010->rtc); |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static struct i2c_driver rx8010_driver = { |
| 419 | .driver = { |
| 420 | .name = "rtc-rx8010", |
Javier Martinez Canillas | 81b779c | 2017-03-03 11:29:16 -0300 | [diff] [blame] | 421 | .of_match_table = of_match_ptr(rx8010_of_match), |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 422 | }, |
Bartosz Golaszewski | cee015d | 2020-09-14 17:45:59 +0200 | [diff] [blame] | 423 | .probe_new = rx8010_probe, |
Akshay Bhat | ed13d89 | 2015-12-03 14:41:21 -0500 | [diff] [blame] | 424 | .id_table = rx8010_id, |
| 425 | }; |
| 426 | |
| 427 | module_i2c_driver(rx8010_driver); |
| 428 | |
| 429 | MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>"); |
| 430 | MODULE_DESCRIPTION("Epson RX8010SJ RTC driver"); |
| 431 | MODULE_LICENSE("GPL v2"); |