acpi_cmos_rtc.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #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 <asm-generic/rtc.h>
  17. #include "internal.h"
  18. ACPI_MODULE_NAME("cmos rtc");
  19. static const struct acpi_device_id acpi_cmos_rtc_ids[] = {
  20. { "PNP0B00" },
  21. { "PNP0B01" },
  22. { "PNP0B02" },
  23. {}
  24. };
  25. static acpi_status
  26. acpi_cmos_rtc_space_handler(u32 function, acpi_physical_address address,
  27. u32 bits, u64 *value64,
  28. void *handler_context, void *region_context)
  29. {
  30. int i;
  31. u8 *value = (u8 *)value64;
  32. if (address > 0xff || !value64)
  33. return AE_BAD_PARAMETER;
  34. if (function != ACPI_WRITE && function != ACPI_READ)
  35. return AE_BAD_PARAMETER;
  36. spin_lock_irq(&rtc_lock);
  37. for (i = 0; i < DIV_ROUND_UP(bits, 8); ++i, ++address, ++value)
  38. if (function == ACPI_READ)
  39. *value = CMOS_READ(address);
  40. else
  41. CMOS_WRITE(*value, address);
  42. spin_unlock_irq(&rtc_lock);
  43. return AE_OK;
  44. }
  45. static int acpi_install_cmos_rtc_space_handler(struct acpi_device *adev,
  46. const struct acpi_device_id *id)
  47. {
  48. acpi_status status;
  49. status = acpi_install_address_space_handler(adev->handle,
  50. ACPI_ADR_SPACE_CMOS,
  51. &acpi_cmos_rtc_space_handler,
  52. NULL, NULL);
  53. if (ACPI_FAILURE(status)) {
  54. pr_err(PREFIX "Error installing CMOS-RTC region handler\n");
  55. return -ENODEV;
  56. }
  57. return 1;
  58. }
  59. static void acpi_remove_cmos_rtc_space_handler(struct acpi_device *adev)
  60. {
  61. if (ACPI_FAILURE(acpi_remove_address_space_handler(adev->handle,
  62. ACPI_ADR_SPACE_CMOS, &acpi_cmos_rtc_space_handler)))
  63. pr_err(PREFIX "Error removing CMOS-RTC region handler\n");
  64. }
  65. static struct acpi_scan_handler cmos_rtc_handler = {
  66. .ids = acpi_cmos_rtc_ids,
  67. .attach = acpi_install_cmos_rtc_space_handler,
  68. .detach = acpi_remove_cmos_rtc_space_handler,
  69. };
  70. void __init acpi_cmos_rtc_init(void)
  71. {
  72. acpi_scan_add_handler(&cmos_rtc_handler);
  73. }