acpi_cmos_rtc.c 2.2 KB

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