linux/drivers/acpi/acpi_cmos_rtc.c
<<
>>
Prefs
   1/*
   2 * ACPI support for CMOS RTC Address Space access
   3 *
   4 * Copyright (C) 2013, Intel Corporation
   5 * Authors: Lan Tianyu <tianyu.lan@intel.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11
  12#include <linux/acpi.h>
  13#include <linux/device.h>
  14#include <linux/err.h>
  15#include <linux/kernel.h>
  16#include <linux/module.h>
  17#include <linux/mc146818rtc.h>
  18
  19#include "internal.h"
  20
  21static const struct acpi_device_id acpi_cmos_rtc_ids[] = {
  22        { "PNP0B00" },
  23        { "PNP0B01" },
  24        { "PNP0B02" },
  25        {}
  26};
  27
  28static acpi_status
  29acpi_cmos_rtc_space_handler(u32 function, acpi_physical_address address,
  30                      u32 bits, u64 *value64,
  31                      void *handler_context, void *region_context)
  32{
  33        int i;
  34        u8 *value = (u8 *)value64;
  35
  36        if (address > 0xff || !value64)
  37                return AE_BAD_PARAMETER;
  38
  39        if (function != ACPI_WRITE && function != ACPI_READ)
  40                return AE_BAD_PARAMETER;
  41
  42        spin_lock_irq(&rtc_lock);
  43
  44        for (i = 0; i < DIV_ROUND_UP(bits, 8); ++i, ++address, ++value)
  45                if (function == ACPI_READ)
  46                        *value = CMOS_READ(address);
  47                else
  48                        CMOS_WRITE(*value, address);
  49
  50        spin_unlock_irq(&rtc_lock);
  51
  52        return AE_OK;
  53}
  54
  55static int acpi_install_cmos_rtc_space_handler(struct acpi_device *adev,
  56                const struct acpi_device_id *id)
  57{
  58        acpi_status status;
  59
  60        status = acpi_install_address_space_handler(adev->handle,
  61                        ACPI_ADR_SPACE_CMOS,
  62                        &acpi_cmos_rtc_space_handler,
  63                        NULL, NULL);
  64        if (ACPI_FAILURE(status)) {
  65                pr_err(PREFIX "Error installing CMOS-RTC region handler\n");
  66                return -ENODEV;
  67        }
  68
  69        return 1;
  70}
  71
  72static void acpi_remove_cmos_rtc_space_handler(struct acpi_device *adev)
  73{
  74        if (ACPI_FAILURE(acpi_remove_address_space_handler(adev->handle,
  75                        ACPI_ADR_SPACE_CMOS, &acpi_cmos_rtc_space_handler)))
  76                pr_err(PREFIX "Error removing CMOS-RTC region handler\n");
  77}
  78
  79static struct acpi_scan_handler cmos_rtc_handler = {
  80        .ids = acpi_cmos_rtc_ids,
  81        .attach = acpi_install_cmos_rtc_space_handler,
  82        .detach = acpi_remove_cmos_rtc_space_handler,
  83};
  84
  85void __init acpi_cmos_rtc_init(void)
  86{
  87        acpi_scan_add_handler(&cmos_rtc_handler);
  88}
  89