Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
2fa97feb LT |
2 | /* |
3 | * ACPI support for CMOS RTC Address Space access | |
4 | * | |
5 | * Copyright (C) 2013, Intel Corporation | |
6 | * Authors: Lan Tianyu <tianyu.lan@intel.com> | |
2fa97feb LT |
7 | */ |
8 | ||
606e56c6 HG |
9 | #define pr_fmt(fmt) "ACPI: " fmt |
10 | ||
2fa97feb LT |
11 | #include <linux/acpi.h> |
12 | #include <linux/device.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
463a8630 | 16 | #include <linux/mc146818rtc.h> |
2fa97feb LT |
17 | |
18 | #include "internal.h" | |
19 | ||
2fa97feb LT |
20 | static const struct acpi_device_id acpi_cmos_rtc_ids[] = { |
21 | { "PNP0B00" }, | |
22 | { "PNP0B01" }, | |
23 | { "PNP0B02" }, | |
24 | {} | |
25 | }; | |
26 | ||
27 | static acpi_status | |
28 | acpi_cmos_rtc_space_handler(u32 function, acpi_physical_address address, | |
29 | u32 bits, u64 *value64, | |
30 | void *handler_context, void *region_context) | |
31 | { | |
32 | int i; | |
9389f46e | 33 | u8 *value = (u8 *)value64; |
2fa97feb LT |
34 | |
35 | if (address > 0xff || !value64) | |
36 | return AE_BAD_PARAMETER; | |
37 | ||
38 | if (function != ACPI_WRITE && function != ACPI_READ) | |
39 | return AE_BAD_PARAMETER; | |
40 | ||
41 | spin_lock_irq(&rtc_lock); | |
42 | ||
43 | for (i = 0; i < DIV_ROUND_UP(bits, 8); ++i, ++address, ++value) | |
44 | if (function == ACPI_READ) | |
45 | *value = CMOS_READ(address); | |
46 | else | |
47 | CMOS_WRITE(*value, address); | |
48 | ||
49 | spin_unlock_irq(&rtc_lock); | |
50 | ||
51 | return AE_OK; | |
52 | } | |
53 | ||
596ca52a | 54 | int acpi_install_cmos_rtc_space_handler(acpi_handle handle) |
2fa97feb LT |
55 | { |
56 | acpi_status status; | |
57 | ||
596ca52a | 58 | status = acpi_install_address_space_handler(handle, |
2fa97feb LT |
59 | ACPI_ADR_SPACE_CMOS, |
60 | &acpi_cmos_rtc_space_handler, | |
61 | NULL, NULL); | |
62 | if (ACPI_FAILURE(status)) { | |
606e56c6 | 63 | pr_err("Error installing CMOS-RTC region handler\n"); |
2fa97feb LT |
64 | return -ENODEV; |
65 | } | |
66 | ||
eec15edb | 67 | return 1; |
2fa97feb | 68 | } |
596ca52a | 69 | EXPORT_SYMBOL_GPL(acpi_install_cmos_rtc_space_handler); |
2fa97feb | 70 | |
596ca52a | 71 | void acpi_remove_cmos_rtc_space_handler(acpi_handle handle) |
2fa97feb | 72 | { |
596ca52a | 73 | if (ACPI_FAILURE(acpi_remove_address_space_handler(handle, |
2fa97feb | 74 | ACPI_ADR_SPACE_CMOS, &acpi_cmos_rtc_space_handler))) |
606e56c6 | 75 | pr_err("Error removing CMOS-RTC region handler\n"); |
2fa97feb | 76 | } |
596ca52a ZR |
77 | EXPORT_SYMBOL_GPL(acpi_remove_cmos_rtc_space_handler); |
78 | ||
79 | static int acpi_cmos_rtc_attach_handler(struct acpi_device *adev, const struct acpi_device_id *id) | |
80 | { | |
81 | return acpi_install_cmos_rtc_space_handler(adev->handle); | |
82 | } | |
83 | ||
84 | static void acpi_cmos_rtc_detach_handler(struct acpi_device *adev) | |
85 | { | |
86 | acpi_remove_cmos_rtc_space_handler(adev->handle); | |
87 | } | |
2fa97feb LT |
88 | |
89 | static struct acpi_scan_handler cmos_rtc_handler = { | |
90 | .ids = acpi_cmos_rtc_ids, | |
596ca52a ZR |
91 | .attach = acpi_cmos_rtc_attach_handler, |
92 | .detach = acpi_cmos_rtc_detach_handler, | |
2fa97feb LT |
93 | }; |
94 | ||
95 | void __init acpi_cmos_rtc_init(void) | |
96 | { | |
97 | acpi_scan_add_handler(&cmos_rtc_handler); | |
98 | } |