Debian Codegraph @

drivers/acpi/x86/cmos_rtc.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ACPI support for CMOS RTC Address Space access
4 *
5 * Copyright (C) 2013, Intel Corporation
6 * Authors: Lan Tianyu <[email protected]>
7 */
8
9 #define pr_fmt(fmt) "ACPI: " fmt
10
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>
16 #include <linux/mc146818rtc.h>
17
18 #include "../internal.h"
19
20 static const struct acpi_device_id acpi_cmos_rtc_ids[] = {
21 { "PNP0B00" },
22 { "PNP0B01" },
23 { "PNP0B02" },
24 {}
25 };
26
27 static bool cmos_rtc_space_handler_present __read_mostly;
28
29 static acpi_status acpi_cmos_rtc_space_handler(u32 function,
30 acpi_physical_address address,
31 u32 bits, u64 *value64,
32 void *handler_context,
33 void *region_context)
34 {
35 unsigned int i, bytes = DIV_ROUND_UP(bits, 8);
36 u8 *value = (u8 *)value64;
37
38 if (address > 0xff || !value64)
39 return AE_BAD_PARAMETER;
40
41 guard(spinlock_irq)(&rtc_lock);
42
43 if (function == ACPI_WRITE) {
44 for (i = 0; i < bytes; i++, address++, value++)
45 CMOS_WRITE(*value, address);
46
47 return AE_OK;
48 }
49
50 if (function == ACPI_READ) {
51 for (i = 0; i < bytes; i++, address++, value++)
52 *value = CMOS_READ(address);
53
54 return AE_OK;
55 }
56
57 return AE_BAD_PARAMETER;
58 }
59
60 int acpi_install_cmos_rtc_space_handler(acpi_handle handle)
61 {
62 acpi_status status;
63
64 if (cmos_rtc_space_handler_present)
65 return 0;
66
67 status = acpi_install_address_space_handler(handle,
68 ACPI_ADR_SPACE_CMOS,
69 acpi_cmos_rtc_space_handler,
70 NULL, NULL);
71 if (ACPI_FAILURE(status)) {
72 pr_err("Failed to install CMOS-RTC address space handler\n");
73 return -ENODEV;
74 }
75
76 cmos_rtc_space_handler_present = true;
77
78 return 1;
79 }
80 EXPORT_SYMBOL_GPL(acpi_install_cmos_rtc_space_handler);
81
82 void acpi_remove_cmos_rtc_space_handler(acpi_handle handle)
83 {
84 acpi_status status;
85
86 if (cmos_rtc_space_handler_present)
87 return;
88
89 status = acpi_remove_address_space_handler(handle,
90 ACPI_ADR_SPACE_CMOS,
91 acpi_cmos_rtc_space_handler);
92 if (ACPI_FAILURE(status))
93 pr_err("Failed to remove CMOS-RTC address space handler\n");
94 }
95 EXPORT_SYMBOL_GPL(acpi_remove_cmos_rtc_space_handler);
96
97 static int acpi_cmos_rtc_attach(struct acpi_device *adev,
98 const struct acpi_device_id *id)
99 {
100 int ret;
101
102 ret = acpi_install_cmos_rtc_space_handler(adev->handle);
103 if (ret < 0)
104 return ret;
105
106 return 1;
107 }
108
109 static struct acpi_scan_handler cmos_rtc_handler = {
110 .ids = acpi_cmos_rtc_ids,
111 .attach = acpi_cmos_rtc_attach,
112 };
113
114 void __init acpi_cmos_rtc_init(void)
115 {
116 acpi_scan_add_handler(&cmos_rtc_handler);
117 }