int3406_thermal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * INT3406 thermal driver for display participant device
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Authors: Aaron Lu <aaron.lu@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/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/acpi.h>
  15. #include <linux/backlight.h>
  16. #include <linux/thermal.h>
  17. #include <acpi/video.h>
  18. #define INT3406_BRIGHTNESS_LIMITS_CHANGED 0x80
  19. struct int3406_thermal_data {
  20. int upper_limit;
  21. int lower_limit;
  22. acpi_handle handle;
  23. struct acpi_video_device_brightness *br;
  24. struct backlight_device *raw_bd;
  25. struct thermal_cooling_device *cooling_dev;
  26. };
  27. /*
  28. * According to the ACPI spec,
  29. * "Each brightness level is represented by a number between 0 and 100,
  30. * and can be thought of as a percentage. For example, 50 can be 50%
  31. * power consumption or 50% brightness, as defined by the OEM."
  32. *
  33. * As int3406 device uses this value to communicate with the native
  34. * graphics driver, we make the assumption that it represents
  35. * the percentage of brightness only
  36. */
  37. #define ACPI_TO_RAW(v, d) (d->raw_bd->props.max_brightness * v / 100)
  38. #define RAW_TO_ACPI(v, d) (v * 100 / d->raw_bd->props.max_brightness)
  39. static int
  40. int3406_thermal_get_max_state(struct thermal_cooling_device *cooling_dev,
  41. unsigned long *state)
  42. {
  43. struct int3406_thermal_data *d = cooling_dev->devdata;
  44. *state = d->upper_limit - d->lower_limit;
  45. return 0;
  46. }
  47. static int
  48. int3406_thermal_set_cur_state(struct thermal_cooling_device *cooling_dev,
  49. unsigned long state)
  50. {
  51. struct int3406_thermal_data *d = cooling_dev->devdata;
  52. int acpi_level, raw_level;
  53. if (state > d->upper_limit - d->lower_limit)
  54. return -EINVAL;
  55. acpi_level = d->br->levels[d->upper_limit - state];
  56. raw_level = ACPI_TO_RAW(acpi_level, d);
  57. return backlight_device_set_brightness(d->raw_bd, raw_level);
  58. }
  59. static int
  60. int3406_thermal_get_cur_state(struct thermal_cooling_device *cooling_dev,
  61. unsigned long *state)
  62. {
  63. struct int3406_thermal_data *d = cooling_dev->devdata;
  64. int acpi_level;
  65. int index;
  66. acpi_level = RAW_TO_ACPI(d->raw_bd->props.brightness, d);
  67. /*
  68. * There is no 1:1 mapping between the firmware interface level
  69. * with the raw interface level, we will have to find one that is
  70. * right above it.
  71. */
  72. for (index = d->lower_limit; index < d->upper_limit; index++) {
  73. if (acpi_level <= d->br->levels[index])
  74. break;
  75. }
  76. *state = d->upper_limit - index;
  77. return 0;
  78. }
  79. static const struct thermal_cooling_device_ops video_cooling_ops = {
  80. .get_max_state = int3406_thermal_get_max_state,
  81. .get_cur_state = int3406_thermal_get_cur_state,
  82. .set_cur_state = int3406_thermal_set_cur_state,
  83. };
  84. static int int3406_thermal_get_index(int *array, int nr, int value)
  85. {
  86. int i;
  87. for (i = 2; i < nr; i++) {
  88. if (array[i] == value)
  89. break;
  90. }
  91. return i == nr ? -ENOENT : i;
  92. }
  93. static void int3406_thermal_get_limit(struct int3406_thermal_data *d)
  94. {
  95. acpi_status status;
  96. unsigned long long lower_limit, upper_limit;
  97. status = acpi_evaluate_integer(d->handle, "DDDL", NULL, &lower_limit);
  98. if (ACPI_SUCCESS(status))
  99. d->lower_limit = int3406_thermal_get_index(d->br->levels,
  100. d->br->count, lower_limit);
  101. status = acpi_evaluate_integer(d->handle, "DDPC", NULL, &upper_limit);
  102. if (ACPI_SUCCESS(status))
  103. d->upper_limit = int3406_thermal_get_index(d->br->levels,
  104. d->br->count, upper_limit);
  105. /* lower_limit and upper_limit should be always set */
  106. d->lower_limit = d->lower_limit > 0 ? d->lower_limit : 2;
  107. d->upper_limit = d->upper_limit > 0 ? d->upper_limit : d->br->count - 1;
  108. }
  109. static void int3406_notify(acpi_handle handle, u32 event, void *data)
  110. {
  111. if (event == INT3406_BRIGHTNESS_LIMITS_CHANGED)
  112. int3406_thermal_get_limit(data);
  113. }
  114. static int int3406_thermal_probe(struct platform_device *pdev)
  115. {
  116. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  117. struct int3406_thermal_data *d;
  118. struct backlight_device *bd;
  119. int ret;
  120. if (!ACPI_HANDLE(&pdev->dev))
  121. return -ENODEV;
  122. d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
  123. if (!d)
  124. return -ENOMEM;
  125. d->handle = ACPI_HANDLE(&pdev->dev);
  126. bd = backlight_device_get_by_type(BACKLIGHT_RAW);
  127. if (!bd)
  128. return -ENODEV;
  129. d->raw_bd = bd;
  130. ret = acpi_video_get_levels(ACPI_COMPANION(&pdev->dev), &d->br, NULL);
  131. if (ret)
  132. return ret;
  133. int3406_thermal_get_limit(d);
  134. d->cooling_dev = thermal_cooling_device_register(acpi_device_bid(adev),
  135. d, &video_cooling_ops);
  136. if (IS_ERR(d->cooling_dev))
  137. goto err;
  138. ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
  139. int3406_notify, d);
  140. if (ret)
  141. goto err_cdev;
  142. platform_set_drvdata(pdev, d);
  143. return 0;
  144. err_cdev:
  145. thermal_cooling_device_unregister(d->cooling_dev);
  146. err:
  147. kfree(d->br);
  148. return -ENODEV;
  149. }
  150. static int int3406_thermal_remove(struct platform_device *pdev)
  151. {
  152. struct int3406_thermal_data *d = platform_get_drvdata(pdev);
  153. thermal_cooling_device_unregister(d->cooling_dev);
  154. kfree(d->br);
  155. return 0;
  156. }
  157. static const struct acpi_device_id int3406_thermal_match[] = {
  158. {"INT3406", 0},
  159. {}
  160. };
  161. MODULE_DEVICE_TABLE(acpi, int3406_thermal_match);
  162. static struct platform_driver int3406_thermal_driver = {
  163. .probe = int3406_thermal_probe,
  164. .remove = int3406_thermal_remove,
  165. .driver = {
  166. .name = "int3406 thermal",
  167. .acpi_match_table = int3406_thermal_match,
  168. },
  169. };
  170. module_platform_driver(int3406_thermal_driver);
  171. MODULE_DESCRIPTION("INT3406 Thermal driver");
  172. MODULE_LICENSE("GPL v2");