| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * An RTC driver for Allwinner A31/A23 |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 8 | 9 | * An RTC driver for Allwinner A10/A20 |
|---|
| 9 | 10 | * |
|---|
| 10 | 11 | * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com> |
|---|
| 11 | | - * |
|---|
| 12 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 13 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 14 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 15 | | - * (at your option) any later version. |
|---|
| 16 | | - * |
|---|
| 17 | | - * This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 18 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 19 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|---|
| 20 | | - * more details. |
|---|
| 21 | 12 | */ |
|---|
| 22 | 13 | |
|---|
| 23 | 14 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 41 | 32 | /* Control register */ |
|---|
| 42 | 33 | #define SUN6I_LOSC_CTRL 0x0000 |
|---|
| 43 | 34 | #define SUN6I_LOSC_CTRL_KEY (0x16aa << 16) |
|---|
| 35 | +#define SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS BIT(15) |
|---|
| 44 | 36 | #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9) |
|---|
| 45 | 37 | #define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8) |
|---|
| 46 | 38 | #define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7) |
|---|
| 39 | +#define SUN6I_LOSC_CTRL_EXT_LOSC_EN BIT(4) |
|---|
| 47 | 40 | #define SUN6I_LOSC_CTRL_EXT_OSC BIT(0) |
|---|
| 48 | 41 | #define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7) |
|---|
| 49 | 42 | |
|---|
| .. | .. |
|---|
| 115 | 108 | * driver, even though it is somewhat limited. |
|---|
| 116 | 109 | */ |
|---|
| 117 | 110 | #define SUN6I_YEAR_MIN 1970 |
|---|
| 118 | | -#define SUN6I_YEAR_MAX 2033 |
|---|
| 119 | 111 | #define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900) |
|---|
| 112 | + |
|---|
| 113 | +/* |
|---|
| 114 | + * There are other differences between models, including: |
|---|
| 115 | + * |
|---|
| 116 | + * - number of GPIO pins that can be configured to hold a certain level |
|---|
| 117 | + * - crypto-key related registers (H5, H6) |
|---|
| 118 | + * - boot process related (super standby, secondary processor entry address) |
|---|
| 119 | + * registers (R40, H6) |
|---|
| 120 | + * - SYS power domain controls (R40) |
|---|
| 121 | + * - DCXO controls (H6) |
|---|
| 122 | + * - RC oscillator calibration (H6) |
|---|
| 123 | + * |
|---|
| 124 | + * These functions are not covered by this driver. |
|---|
| 125 | + */ |
|---|
| 126 | +struct sun6i_rtc_clk_data { |
|---|
| 127 | + unsigned long rc_osc_rate; |
|---|
| 128 | + unsigned int fixed_prescaler : 16; |
|---|
| 129 | + unsigned int has_prescaler : 1; |
|---|
| 130 | + unsigned int has_out_clk : 1; |
|---|
| 131 | + unsigned int has_losc_en : 1; |
|---|
| 132 | + unsigned int has_auto_swt : 1; |
|---|
| 133 | +}; |
|---|
| 120 | 134 | |
|---|
| 121 | 135 | struct sun6i_rtc_dev { |
|---|
| 122 | 136 | struct rtc_device *rtc; |
|---|
| 123 | | - struct device *dev; |
|---|
| 137 | + const struct sun6i_rtc_clk_data *data; |
|---|
| 124 | 138 | void __iomem *base; |
|---|
| 125 | 139 | int irq; |
|---|
| 126 | | - unsigned long alarm; |
|---|
| 140 | + time64_t alarm; |
|---|
| 127 | 141 | |
|---|
| 128 | 142 | struct clk_hw hw; |
|---|
| 129 | 143 | struct clk_hw *int_osc; |
|---|
| .. | .. |
|---|
| 139 | 153 | unsigned long parent_rate) |
|---|
| 140 | 154 | { |
|---|
| 141 | 155 | struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw); |
|---|
| 142 | | - u32 val; |
|---|
| 156 | + u32 val = 0; |
|---|
| 143 | 157 | |
|---|
| 144 | 158 | val = readl(rtc->base + SUN6I_LOSC_CTRL); |
|---|
| 145 | 159 | if (val & SUN6I_LOSC_CTRL_EXT_OSC) |
|---|
| 146 | 160 | return parent_rate; |
|---|
| 147 | 161 | |
|---|
| 148 | | - val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL); |
|---|
| 149 | | - val &= GENMASK(4, 0); |
|---|
| 162 | + if (rtc->data->fixed_prescaler) |
|---|
| 163 | + parent_rate /= rtc->data->fixed_prescaler; |
|---|
| 164 | + |
|---|
| 165 | + if (rtc->data->has_prescaler) { |
|---|
| 166 | + val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL); |
|---|
| 167 | + val &= GENMASK(4, 0); |
|---|
| 168 | + } |
|---|
| 150 | 169 | |
|---|
| 151 | 170 | return parent_rate / (val + 1); |
|---|
| 152 | 171 | } |
|---|
| .. | .. |
|---|
| 172 | 191 | val &= ~SUN6I_LOSC_CTRL_EXT_OSC; |
|---|
| 173 | 192 | val |= SUN6I_LOSC_CTRL_KEY; |
|---|
| 174 | 193 | val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0; |
|---|
| 194 | + if (rtc->data->has_losc_en) { |
|---|
| 195 | + val &= ~SUN6I_LOSC_CTRL_EXT_LOSC_EN; |
|---|
| 196 | + val |= index ? SUN6I_LOSC_CTRL_EXT_LOSC_EN : 0; |
|---|
| 197 | + } |
|---|
| 175 | 198 | writel(val, rtc->base + SUN6I_LOSC_CTRL); |
|---|
| 176 | 199 | spin_unlock_irqrestore(&rtc->lock, flags); |
|---|
| 177 | 200 | |
|---|
| .. | .. |
|---|
| 185 | 208 | .set_parent = sun6i_rtc_osc_set_parent, |
|---|
| 186 | 209 | }; |
|---|
| 187 | 210 | |
|---|
| 188 | | -static void __init sun6i_rtc_clk_init(struct device_node *node) |
|---|
| 211 | +static void __init sun6i_rtc_clk_init(struct device_node *node, |
|---|
| 212 | + const struct sun6i_rtc_clk_data *data) |
|---|
| 189 | 213 | { |
|---|
| 190 | 214 | struct clk_hw_onecell_data *clk_data; |
|---|
| 191 | 215 | struct sun6i_rtc_dev *rtc; |
|---|
| 192 | 216 | struct clk_init_data init = { |
|---|
| 193 | 217 | .ops = &sun6i_rtc_osc_ops, |
|---|
| 218 | + .name = "losc", |
|---|
| 194 | 219 | }; |
|---|
| 220 | + const char *iosc_name = "rtc-int-osc"; |
|---|
| 195 | 221 | const char *clkout_name = "osc32k-out"; |
|---|
| 196 | 222 | const char *parents[2]; |
|---|
| 223 | + u32 reg; |
|---|
| 197 | 224 | |
|---|
| 198 | 225 | rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); |
|---|
| 199 | 226 | if (!rtc) |
|---|
| 200 | 227 | return; |
|---|
| 201 | 228 | |
|---|
| 202 | | - clk_data = kzalloc(sizeof(*clk_data) + (sizeof(*clk_data->hws) * 2), |
|---|
| 203 | | - GFP_KERNEL); |
|---|
| 229 | + rtc->data = data; |
|---|
| 230 | + clk_data = kzalloc(struct_size(clk_data, hws, 3), GFP_KERNEL); |
|---|
| 204 | 231 | if (!clk_data) { |
|---|
| 205 | 232 | kfree(rtc); |
|---|
| 206 | 233 | return; |
|---|
| .. | .. |
|---|
| 214 | 241 | goto err; |
|---|
| 215 | 242 | } |
|---|
| 216 | 243 | |
|---|
| 217 | | - /* Switch to the external, more precise, oscillator */ |
|---|
| 218 | | - writel(SUN6I_LOSC_CTRL_KEY | SUN6I_LOSC_CTRL_EXT_OSC, |
|---|
| 219 | | - rtc->base + SUN6I_LOSC_CTRL); |
|---|
| 244 | + reg = SUN6I_LOSC_CTRL_KEY; |
|---|
| 245 | + if (rtc->data->has_auto_swt) { |
|---|
| 246 | + /* Bypass auto-switch to int osc, on ext losc failure */ |
|---|
| 247 | + reg |= SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS; |
|---|
| 248 | + writel(reg, rtc->base + SUN6I_LOSC_CTRL); |
|---|
| 249 | + } |
|---|
| 250 | + |
|---|
| 251 | + /* Switch to the external, more precise, oscillator, if present */ |
|---|
| 252 | + if (of_get_property(node, "clocks", NULL)) { |
|---|
| 253 | + reg |= SUN6I_LOSC_CTRL_EXT_OSC; |
|---|
| 254 | + if (rtc->data->has_losc_en) |
|---|
| 255 | + reg |= SUN6I_LOSC_CTRL_EXT_LOSC_EN; |
|---|
| 256 | + } |
|---|
| 257 | + writel(reg, rtc->base + SUN6I_LOSC_CTRL); |
|---|
| 220 | 258 | |
|---|
| 221 | 259 | /* Yes, I know, this is ugly. */ |
|---|
| 222 | 260 | sun6i_rtc = rtc; |
|---|
| 223 | 261 | |
|---|
| 224 | | - /* Deal with old DTs */ |
|---|
| 225 | | - if (!of_get_property(node, "clocks", NULL)) |
|---|
| 226 | | - goto err; |
|---|
| 262 | + of_property_read_string_index(node, "clock-output-names", 2, |
|---|
| 263 | + &iosc_name); |
|---|
| 227 | 264 | |
|---|
| 228 | 265 | rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL, |
|---|
| 229 | | - "rtc-int-osc", |
|---|
| 266 | + iosc_name, |
|---|
| 230 | 267 | NULL, 0, |
|---|
| 231 | | - 667000, |
|---|
| 268 | + rtc->data->rc_osc_rate, |
|---|
| 232 | 269 | 300000000); |
|---|
| 233 | 270 | if (IS_ERR(rtc->int_osc)) { |
|---|
| 234 | 271 | pr_crit("Couldn't register the internal oscillator\n"); |
|---|
| .. | .. |
|---|
| 236 | 273 | } |
|---|
| 237 | 274 | |
|---|
| 238 | 275 | parents[0] = clk_hw_get_name(rtc->int_osc); |
|---|
| 276 | + /* If there is no external oscillator, this will be NULL and ... */ |
|---|
| 239 | 277 | parents[1] = of_clk_get_parent_name(node, 0); |
|---|
| 240 | 278 | |
|---|
| 241 | 279 | rtc->hw.init = &init; |
|---|
| 242 | 280 | |
|---|
| 243 | 281 | init.parent_names = parents; |
|---|
| 282 | + /* ... number of clock parents will be 1. */ |
|---|
| 244 | 283 | init.num_parents = of_clk_get_parent_count(node) + 1; |
|---|
| 245 | 284 | of_property_read_string_index(node, "clock-output-names", 0, |
|---|
| 246 | 285 | &init.name); |
|---|
| .. | .. |
|---|
| 253 | 292 | |
|---|
| 254 | 293 | of_property_read_string_index(node, "clock-output-names", 1, |
|---|
| 255 | 294 | &clkout_name); |
|---|
| 256 | | - rtc->ext_losc = clk_register_gate(NULL, clkout_name, rtc->hw.init->name, |
|---|
| 295 | + rtc->ext_losc = clk_register_gate(NULL, clkout_name, init.name, |
|---|
| 257 | 296 | 0, rtc->base + SUN6I_LOSC_OUT_GATING, |
|---|
| 258 | 297 | SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0, |
|---|
| 259 | 298 | &rtc->lock); |
|---|
| .. | .. |
|---|
| 262 | 301 | goto err_register; |
|---|
| 263 | 302 | } |
|---|
| 264 | 303 | |
|---|
| 265 | | - clk_data->num = 2; |
|---|
| 304 | + clk_data->num = 3; |
|---|
| 266 | 305 | clk_data->hws[0] = &rtc->hw; |
|---|
| 267 | 306 | clk_data->hws[1] = __clk_get_hw(rtc->ext_losc); |
|---|
| 307 | + clk_data->hws[2] = rtc->int_osc; |
|---|
| 268 | 308 | of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data); |
|---|
| 269 | 309 | return; |
|---|
| 270 | 310 | |
|---|
| .. | .. |
|---|
| 273 | 313 | err: |
|---|
| 274 | 314 | kfree(clk_data); |
|---|
| 275 | 315 | } |
|---|
| 276 | | -CLK_OF_DECLARE_DRIVER(sun6i_rtc_clk, "allwinner,sun6i-a31-rtc", |
|---|
| 277 | | - sun6i_rtc_clk_init); |
|---|
| 316 | + |
|---|
| 317 | +static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = { |
|---|
| 318 | + .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */ |
|---|
| 319 | + .has_prescaler = 1, |
|---|
| 320 | +}; |
|---|
| 321 | + |
|---|
| 322 | +static void __init sun6i_a31_rtc_clk_init(struct device_node *node) |
|---|
| 323 | +{ |
|---|
| 324 | + sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data); |
|---|
| 325 | +} |
|---|
| 326 | +CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc", |
|---|
| 327 | + sun6i_a31_rtc_clk_init); |
|---|
| 328 | + |
|---|
| 329 | +static const struct sun6i_rtc_clk_data sun8i_a23_rtc_data = { |
|---|
| 330 | + .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */ |
|---|
| 331 | + .has_prescaler = 1, |
|---|
| 332 | + .has_out_clk = 1, |
|---|
| 333 | +}; |
|---|
| 334 | + |
|---|
| 335 | +static void __init sun8i_a23_rtc_clk_init(struct device_node *node) |
|---|
| 336 | +{ |
|---|
| 337 | + sun6i_rtc_clk_init(node, &sun8i_a23_rtc_data); |
|---|
| 338 | +} |
|---|
| 339 | +CLK_OF_DECLARE_DRIVER(sun8i_a23_rtc_clk, "allwinner,sun8i-a23-rtc", |
|---|
| 340 | + sun8i_a23_rtc_clk_init); |
|---|
| 341 | + |
|---|
| 342 | +static const struct sun6i_rtc_clk_data sun8i_h3_rtc_data = { |
|---|
| 343 | + .rc_osc_rate = 16000000, |
|---|
| 344 | + .fixed_prescaler = 32, |
|---|
| 345 | + .has_prescaler = 1, |
|---|
| 346 | + .has_out_clk = 1, |
|---|
| 347 | +}; |
|---|
| 348 | + |
|---|
| 349 | +static void __init sun8i_h3_rtc_clk_init(struct device_node *node) |
|---|
| 350 | +{ |
|---|
| 351 | + sun6i_rtc_clk_init(node, &sun8i_h3_rtc_data); |
|---|
| 352 | +} |
|---|
| 353 | +CLK_OF_DECLARE_DRIVER(sun8i_h3_rtc_clk, "allwinner,sun8i-h3-rtc", |
|---|
| 354 | + sun8i_h3_rtc_clk_init); |
|---|
| 355 | +/* As far as we are concerned, clocks for H5 are the same as H3 */ |
|---|
| 356 | +CLK_OF_DECLARE_DRIVER(sun50i_h5_rtc_clk, "allwinner,sun50i-h5-rtc", |
|---|
| 357 | + sun8i_h3_rtc_clk_init); |
|---|
| 358 | + |
|---|
| 359 | +static const struct sun6i_rtc_clk_data sun50i_h6_rtc_data = { |
|---|
| 360 | + .rc_osc_rate = 16000000, |
|---|
| 361 | + .fixed_prescaler = 32, |
|---|
| 362 | + .has_prescaler = 1, |
|---|
| 363 | + .has_out_clk = 1, |
|---|
| 364 | + .has_losc_en = 1, |
|---|
| 365 | + .has_auto_swt = 1, |
|---|
| 366 | +}; |
|---|
| 367 | + |
|---|
| 368 | +static void __init sun50i_h6_rtc_clk_init(struct device_node *node) |
|---|
| 369 | +{ |
|---|
| 370 | + sun6i_rtc_clk_init(node, &sun50i_h6_rtc_data); |
|---|
| 371 | +} |
|---|
| 372 | +CLK_OF_DECLARE_DRIVER(sun50i_h6_rtc_clk, "allwinner,sun50i-h6-rtc", |
|---|
| 373 | + sun50i_h6_rtc_clk_init); |
|---|
| 374 | + |
|---|
| 375 | +/* |
|---|
| 376 | + * The R40 user manual is self-conflicting on whether the prescaler is |
|---|
| 377 | + * fixed or configurable. The clock diagram shows it as fixed, but there |
|---|
| 378 | + * is also a configurable divider in the RTC block. |
|---|
| 379 | + */ |
|---|
| 380 | +static const struct sun6i_rtc_clk_data sun8i_r40_rtc_data = { |
|---|
| 381 | + .rc_osc_rate = 16000000, |
|---|
| 382 | + .fixed_prescaler = 512, |
|---|
| 383 | +}; |
|---|
| 384 | +static void __init sun8i_r40_rtc_clk_init(struct device_node *node) |
|---|
| 385 | +{ |
|---|
| 386 | + sun6i_rtc_clk_init(node, &sun8i_r40_rtc_data); |
|---|
| 387 | +} |
|---|
| 388 | +CLK_OF_DECLARE_DRIVER(sun8i_r40_rtc_clk, "allwinner,sun8i-r40-rtc", |
|---|
| 389 | + sun8i_r40_rtc_clk_init); |
|---|
| 390 | + |
|---|
| 391 | +static const struct sun6i_rtc_clk_data sun8i_v3_rtc_data = { |
|---|
| 392 | + .rc_osc_rate = 32000, |
|---|
| 393 | + .has_out_clk = 1, |
|---|
| 394 | +}; |
|---|
| 395 | + |
|---|
| 396 | +static void __init sun8i_v3_rtc_clk_init(struct device_node *node) |
|---|
| 397 | +{ |
|---|
| 398 | + sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data); |
|---|
| 399 | +} |
|---|
| 400 | +CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc", |
|---|
| 401 | + sun8i_v3_rtc_clk_init); |
|---|
| 278 | 402 | |
|---|
| 279 | 403 | static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id) |
|---|
| 280 | 404 | { |
|---|
| .. | .. |
|---|
| 368 | 492 | |
|---|
| 369 | 493 | wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN); |
|---|
| 370 | 494 | wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN); |
|---|
| 371 | | - rtc_time_to_tm(chip->alarm, &wkalrm->time); |
|---|
| 495 | + rtc_time64_to_tm(chip->alarm, &wkalrm->time); |
|---|
| 372 | 496 | |
|---|
| 373 | 497 | return 0; |
|---|
| 374 | 498 | } |
|---|
| .. | .. |
|---|
| 378 | 502 | struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); |
|---|
| 379 | 503 | struct rtc_time *alrm_tm = &wkalrm->time; |
|---|
| 380 | 504 | struct rtc_time tm_now; |
|---|
| 381 | | - unsigned long time_now = 0; |
|---|
| 382 | | - unsigned long time_set = 0; |
|---|
| 383 | | - unsigned long time_gap = 0; |
|---|
| 384 | | - int ret = 0; |
|---|
| 505 | + time64_t time_now, time_set; |
|---|
| 506 | + int ret; |
|---|
| 385 | 507 | |
|---|
| 386 | 508 | ret = sun6i_rtc_gettime(dev, &tm_now); |
|---|
| 387 | 509 | if (ret < 0) { |
|---|
| .. | .. |
|---|
| 389 | 511 | return -EINVAL; |
|---|
| 390 | 512 | } |
|---|
| 391 | 513 | |
|---|
| 392 | | - rtc_tm_to_time(alrm_tm, &time_set); |
|---|
| 393 | | - rtc_tm_to_time(&tm_now, &time_now); |
|---|
| 514 | + time_set = rtc_tm_to_time64(alrm_tm); |
|---|
| 515 | + time_now = rtc_tm_to_time64(&tm_now); |
|---|
| 394 | 516 | if (time_set <= time_now) { |
|---|
| 395 | 517 | dev_err(dev, "Date to set in the past\n"); |
|---|
| 396 | 518 | return -EINVAL; |
|---|
| 397 | 519 | } |
|---|
| 398 | 520 | |
|---|
| 399 | | - time_gap = time_set - time_now; |
|---|
| 400 | | - |
|---|
| 401 | | - if (time_gap > U32_MAX) { |
|---|
| 521 | + if ((time_set - time_now) > U32_MAX) { |
|---|
| 402 | 522 | dev_err(dev, "Date too far in the future\n"); |
|---|
| 403 | 523 | return -EINVAL; |
|---|
| 404 | 524 | } |
|---|
| .. | .. |
|---|
| 407 | 527 | writel(0, chip->base + SUN6I_ALRM_COUNTER); |
|---|
| 408 | 528 | usleep_range(100, 300); |
|---|
| 409 | 529 | |
|---|
| 410 | | - writel(time_gap, chip->base + SUN6I_ALRM_COUNTER); |
|---|
| 530 | + writel(time_set - time_now, chip->base + SUN6I_ALRM_COUNTER); |
|---|
| 411 | 531 | chip->alarm = time_set; |
|---|
| 412 | 532 | |
|---|
| 413 | 533 | sun6i_rtc_setaie(wkalrm->enabled, chip); |
|---|
| .. | .. |
|---|
| 438 | 558 | struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); |
|---|
| 439 | 559 | u32 date = 0; |
|---|
| 440 | 560 | u32 time = 0; |
|---|
| 441 | | - int year; |
|---|
| 442 | | - |
|---|
| 443 | | - year = rtc_tm->tm_year + 1900; |
|---|
| 444 | | - if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) { |
|---|
| 445 | | - dev_err(dev, "rtc only supports year in range %d - %d\n", |
|---|
| 446 | | - SUN6I_YEAR_MIN, SUN6I_YEAR_MAX); |
|---|
| 447 | | - return -EINVAL; |
|---|
| 448 | | - } |
|---|
| 449 | 561 | |
|---|
| 450 | 562 | rtc_tm->tm_year -= SUN6I_YEAR_OFF; |
|---|
| 451 | 563 | rtc_tm->tm_mon += 1; |
|---|
| .. | .. |
|---|
| 454 | 566 | SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) | |
|---|
| 455 | 567 | SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year); |
|---|
| 456 | 568 | |
|---|
| 457 | | - if (is_leap_year(year)) |
|---|
| 569 | + if (is_leap_year(rtc_tm->tm_year + SUN6I_YEAR_MIN)) |
|---|
| 458 | 570 | date |= SUN6I_LEAP_SET_VALUE(1); |
|---|
| 459 | 571 | |
|---|
| 460 | 572 | time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) | |
|---|
| .. | .. |
|---|
| 517 | 629 | .alarm_irq_enable = sun6i_rtc_alarm_irq_enable |
|---|
| 518 | 630 | }; |
|---|
| 519 | 631 | |
|---|
| 632 | +#ifdef CONFIG_PM_SLEEP |
|---|
| 633 | +/* Enable IRQ wake on suspend, to wake up from RTC. */ |
|---|
| 634 | +static int sun6i_rtc_suspend(struct device *dev) |
|---|
| 635 | +{ |
|---|
| 636 | + struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); |
|---|
| 637 | + |
|---|
| 638 | + if (device_may_wakeup(dev)) |
|---|
| 639 | + enable_irq_wake(chip->irq); |
|---|
| 640 | + |
|---|
| 641 | + return 0; |
|---|
| 642 | +} |
|---|
| 643 | + |
|---|
| 644 | +/* Disable IRQ wake on resume. */ |
|---|
| 645 | +static int sun6i_rtc_resume(struct device *dev) |
|---|
| 646 | +{ |
|---|
| 647 | + struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); |
|---|
| 648 | + |
|---|
| 649 | + if (device_may_wakeup(dev)) |
|---|
| 650 | + disable_irq_wake(chip->irq); |
|---|
| 651 | + |
|---|
| 652 | + return 0; |
|---|
| 653 | +} |
|---|
| 654 | +#endif |
|---|
| 655 | + |
|---|
| 656 | +static SIMPLE_DEV_PM_OPS(sun6i_rtc_pm_ops, |
|---|
| 657 | + sun6i_rtc_suspend, sun6i_rtc_resume); |
|---|
| 658 | + |
|---|
| 520 | 659 | static int sun6i_rtc_probe(struct platform_device *pdev) |
|---|
| 521 | 660 | { |
|---|
| 522 | 661 | struct sun6i_rtc_dev *chip = sun6i_rtc; |
|---|
| .. | .. |
|---|
| 526 | 665 | return -ENODEV; |
|---|
| 527 | 666 | |
|---|
| 528 | 667 | platform_set_drvdata(pdev, chip); |
|---|
| 529 | | - chip->dev = &pdev->dev; |
|---|
| 530 | 668 | |
|---|
| 531 | 669 | chip->irq = platform_get_irq(pdev, 0); |
|---|
| 532 | | - if (chip->irq < 0) { |
|---|
| 533 | | - dev_err(&pdev->dev, "No IRQ resource\n"); |
|---|
| 670 | + if (chip->irq < 0) |
|---|
| 534 | 671 | return chip->irq; |
|---|
| 535 | | - } |
|---|
| 536 | 672 | |
|---|
| 537 | 673 | ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq, |
|---|
| 538 | 674 | 0, dev_name(&pdev->dev), chip); |
|---|
| .. | .. |
|---|
| 569 | 705 | |
|---|
| 570 | 706 | clk_prepare_enable(chip->losc); |
|---|
| 571 | 707 | |
|---|
| 572 | | - chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i", |
|---|
| 573 | | - &sun6i_rtc_ops, THIS_MODULE); |
|---|
| 574 | | - if (IS_ERR(chip->rtc)) { |
|---|
| 575 | | - dev_err(&pdev->dev, "unable to register device\n"); |
|---|
| 708 | + device_init_wakeup(&pdev->dev, 1); |
|---|
| 709 | + |
|---|
| 710 | + chip->rtc = devm_rtc_allocate_device(&pdev->dev); |
|---|
| 711 | + if (IS_ERR(chip->rtc)) |
|---|
| 576 | 712 | return PTR_ERR(chip->rtc); |
|---|
| 577 | | - } |
|---|
| 713 | + |
|---|
| 714 | + chip->rtc->ops = &sun6i_rtc_ops; |
|---|
| 715 | + chip->rtc->range_max = 2019686399LL; /* 2033-12-31 23:59:59 */ |
|---|
| 716 | + |
|---|
| 717 | + ret = rtc_register_device(chip->rtc); |
|---|
| 718 | + if (ret) |
|---|
| 719 | + return ret; |
|---|
| 578 | 720 | |
|---|
| 579 | 721 | dev_info(&pdev->dev, "RTC enabled\n"); |
|---|
| 580 | 722 | |
|---|
| 581 | 723 | return 0; |
|---|
| 582 | 724 | } |
|---|
| 583 | 725 | |
|---|
| 726 | +/* |
|---|
| 727 | + * As far as RTC functionality goes, all models are the same. The |
|---|
| 728 | + * datasheets claim that different models have different number of |
|---|
| 729 | + * registers available for non-volatile storage, but experiments show |
|---|
| 730 | + * that all SoCs have 16 registers available for this purpose. |
|---|
| 731 | + */ |
|---|
| 584 | 732 | static const struct of_device_id sun6i_rtc_dt_ids[] = { |
|---|
| 585 | 733 | { .compatible = "allwinner,sun6i-a31-rtc" }, |
|---|
| 734 | + { .compatible = "allwinner,sun8i-a23-rtc" }, |
|---|
| 735 | + { .compatible = "allwinner,sun8i-h3-rtc" }, |
|---|
| 736 | + { .compatible = "allwinner,sun8i-r40-rtc" }, |
|---|
| 737 | + { .compatible = "allwinner,sun8i-v3-rtc" }, |
|---|
| 738 | + { .compatible = "allwinner,sun50i-h5-rtc" }, |
|---|
| 739 | + { .compatible = "allwinner,sun50i-h6-rtc" }, |
|---|
| 586 | 740 | { /* sentinel */ }, |
|---|
| 587 | 741 | }; |
|---|
| 588 | 742 | MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids); |
|---|
| .. | .. |
|---|
| 592 | 746 | .driver = { |
|---|
| 593 | 747 | .name = "sun6i-rtc", |
|---|
| 594 | 748 | .of_match_table = sun6i_rtc_dt_ids, |
|---|
| 749 | + .pm = &sun6i_rtc_pm_ops, |
|---|
| 595 | 750 | }, |
|---|
| 596 | 751 | }; |
|---|
| 597 | 752 | builtin_platform_driver(sun6i_rtc_driver); |
|---|