intel_pmic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * intel_pmic.c - Intel PMIC operation region driver
  3. *
  4. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/acpi.h>
  17. #include <linux/regmap.h>
  18. #include <acpi/acpi_lpat.h>
  19. #include "intel_pmic.h"
  20. #define PMIC_POWER_OPREGION_ID 0x8d
  21. #define PMIC_THERMAL_OPREGION_ID 0x8c
  22. struct intel_pmic_opregion {
  23. struct mutex lock;
  24. struct acpi_lpat_conversion_table *lpat_table;
  25. struct regmap *regmap;
  26. struct intel_pmic_opregion_data *data;
  27. };
  28. static int pmic_get_reg_bit(int address, struct pmic_table *table,
  29. int count, int *reg, int *bit)
  30. {
  31. int i;
  32. for (i = 0; i < count; i++) {
  33. if (table[i].address == address) {
  34. *reg = table[i].reg;
  35. if (bit)
  36. *bit = table[i].bit;
  37. return 0;
  38. }
  39. }
  40. return -ENOENT;
  41. }
  42. static acpi_status intel_pmic_power_handler(u32 function,
  43. acpi_physical_address address, u32 bits, u64 *value64,
  44. void *handler_context, void *region_context)
  45. {
  46. struct intel_pmic_opregion *opregion = region_context;
  47. struct regmap *regmap = opregion->regmap;
  48. struct intel_pmic_opregion_data *d = opregion->data;
  49. int reg, bit, result;
  50. if (bits != 32 || !value64)
  51. return AE_BAD_PARAMETER;
  52. if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1))
  53. return AE_BAD_PARAMETER;
  54. result = pmic_get_reg_bit(address, d->power_table,
  55. d->power_table_count, &reg, &bit);
  56. if (result == -ENOENT)
  57. return AE_BAD_PARAMETER;
  58. mutex_lock(&opregion->lock);
  59. result = function == ACPI_READ ?
  60. d->get_power(regmap, reg, bit, value64) :
  61. d->update_power(regmap, reg, bit, *value64 == 1);
  62. mutex_unlock(&opregion->lock);
  63. return result ? AE_ERROR : AE_OK;
  64. }
  65. static int pmic_read_temp(struct intel_pmic_opregion *opregion,
  66. int reg, u64 *value)
  67. {
  68. int raw_temp, temp;
  69. if (!opregion->data->get_raw_temp)
  70. return -ENXIO;
  71. raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg);
  72. if (raw_temp < 0)
  73. return raw_temp;
  74. if (!opregion->lpat_table) {
  75. *value = raw_temp;
  76. return 0;
  77. }
  78. temp = acpi_lpat_raw_to_temp(opregion->lpat_table, raw_temp);
  79. if (temp < 0)
  80. return temp;
  81. *value = temp;
  82. return 0;
  83. }
  84. static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg,
  85. u32 function, u64 *value)
  86. {
  87. return function == ACPI_READ ?
  88. pmic_read_temp(opregion, reg, value) : -EINVAL;
  89. }
  90. static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg,
  91. u32 function, u64 *value)
  92. {
  93. int raw_temp;
  94. if (function == ACPI_READ)
  95. return pmic_read_temp(opregion, reg, value);
  96. if (!opregion->data->update_aux)
  97. return -ENXIO;
  98. if (opregion->lpat_table) {
  99. raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value);
  100. if (raw_temp < 0)
  101. return raw_temp;
  102. } else {
  103. raw_temp = *value;
  104. }
  105. return opregion->data->update_aux(opregion->regmap, reg, raw_temp);
  106. }
  107. static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg,
  108. u32 function, u64 *value)
  109. {
  110. struct intel_pmic_opregion_data *d = opregion->data;
  111. struct regmap *regmap = opregion->regmap;
  112. if (!d->get_policy || !d->update_policy)
  113. return -ENXIO;
  114. if (function == ACPI_READ)
  115. return d->get_policy(regmap, reg, value);
  116. if (*value != 0 && *value != 1)
  117. return -EINVAL;
  118. return d->update_policy(regmap, reg, *value);
  119. }
  120. static bool pmic_thermal_is_temp(int address)
  121. {
  122. return (address <= 0x3c) && !(address % 12);
  123. }
  124. static bool pmic_thermal_is_aux(int address)
  125. {
  126. return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) ||
  127. (address >= 8 && address <= 0x44 && !((address - 8) % 12));
  128. }
  129. static bool pmic_thermal_is_pen(int address)
  130. {
  131. return address >= 0x48 && address <= 0x5c;
  132. }
  133. static acpi_status intel_pmic_thermal_handler(u32 function,
  134. acpi_physical_address address, u32 bits, u64 *value64,
  135. void *handler_context, void *region_context)
  136. {
  137. struct intel_pmic_opregion *opregion = region_context;
  138. struct intel_pmic_opregion_data *d = opregion->data;
  139. int reg, result;
  140. if (bits != 32 || !value64)
  141. return AE_BAD_PARAMETER;
  142. result = pmic_get_reg_bit(address, d->thermal_table,
  143. d->thermal_table_count, &reg, NULL);
  144. if (result == -ENOENT)
  145. return AE_BAD_PARAMETER;
  146. mutex_lock(&opregion->lock);
  147. if (pmic_thermal_is_temp(address))
  148. result = pmic_thermal_temp(opregion, reg, function, value64);
  149. else if (pmic_thermal_is_aux(address))
  150. result = pmic_thermal_aux(opregion, reg, function, value64);
  151. else if (pmic_thermal_is_pen(address))
  152. result = pmic_thermal_pen(opregion, reg, function, value64);
  153. else
  154. result = -EINVAL;
  155. mutex_unlock(&opregion->lock);
  156. if (result < 0) {
  157. if (result == -EINVAL)
  158. return AE_BAD_PARAMETER;
  159. else
  160. return AE_ERROR;
  161. }
  162. return AE_OK;
  163. }
  164. int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
  165. struct regmap *regmap,
  166. struct intel_pmic_opregion_data *d)
  167. {
  168. acpi_status status;
  169. struct intel_pmic_opregion *opregion;
  170. int ret;
  171. if (!dev || !regmap || !d)
  172. return -EINVAL;
  173. if (!handle)
  174. return -ENODEV;
  175. opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
  176. if (!opregion)
  177. return -ENOMEM;
  178. mutex_init(&opregion->lock);
  179. opregion->regmap = regmap;
  180. opregion->lpat_table = acpi_lpat_get_conversion_table(handle);
  181. status = acpi_install_address_space_handler(handle,
  182. PMIC_POWER_OPREGION_ID,
  183. intel_pmic_power_handler,
  184. NULL, opregion);
  185. if (ACPI_FAILURE(status)) {
  186. ret = -ENODEV;
  187. goto out_error;
  188. }
  189. status = acpi_install_address_space_handler(handle,
  190. PMIC_THERMAL_OPREGION_ID,
  191. intel_pmic_thermal_handler,
  192. NULL, opregion);
  193. if (ACPI_FAILURE(status)) {
  194. acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID,
  195. intel_pmic_power_handler);
  196. ret = -ENODEV;
  197. goto out_error;
  198. }
  199. opregion->data = d;
  200. return 0;
  201. out_error:
  202. acpi_lpat_free_conversion_table(opregion->lpat_table);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);
  206. MODULE_LICENSE("GPL");