intel_pmic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/export.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. #define PMIC_REGS_OPREGION_ID 0x8f
  23. struct intel_pmic_regs_handler_ctx {
  24. unsigned int val;
  25. u16 addr;
  26. };
  27. struct intel_pmic_opregion {
  28. struct mutex lock;
  29. struct acpi_lpat_conversion_table *lpat_table;
  30. struct regmap *regmap;
  31. struct intel_pmic_opregion_data *data;
  32. struct intel_pmic_regs_handler_ctx ctx;
  33. };
  34. static int pmic_get_reg_bit(int address, struct pmic_table *table,
  35. int count, int *reg, int *bit)
  36. {
  37. int i;
  38. for (i = 0; i < count; i++) {
  39. if (table[i].address == address) {
  40. *reg = table[i].reg;
  41. if (bit)
  42. *bit = table[i].bit;
  43. return 0;
  44. }
  45. }
  46. return -ENOENT;
  47. }
  48. static acpi_status intel_pmic_power_handler(u32 function,
  49. acpi_physical_address address, u32 bits, u64 *value64,
  50. void *handler_context, void *region_context)
  51. {
  52. struct intel_pmic_opregion *opregion = region_context;
  53. struct regmap *regmap = opregion->regmap;
  54. struct intel_pmic_opregion_data *d = opregion->data;
  55. int reg, bit, result;
  56. if (bits != 32 || !value64)
  57. return AE_BAD_PARAMETER;
  58. if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1))
  59. return AE_BAD_PARAMETER;
  60. result = pmic_get_reg_bit(address, d->power_table,
  61. d->power_table_count, &reg, &bit);
  62. if (result == -ENOENT)
  63. return AE_BAD_PARAMETER;
  64. mutex_lock(&opregion->lock);
  65. result = function == ACPI_READ ?
  66. d->get_power(regmap, reg, bit, value64) :
  67. d->update_power(regmap, reg, bit, *value64 == 1);
  68. mutex_unlock(&opregion->lock);
  69. return result ? AE_ERROR : AE_OK;
  70. }
  71. static int pmic_read_temp(struct intel_pmic_opregion *opregion,
  72. int reg, u64 *value)
  73. {
  74. int raw_temp, temp;
  75. if (!opregion->data->get_raw_temp)
  76. return -ENXIO;
  77. raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg);
  78. if (raw_temp < 0)
  79. return raw_temp;
  80. if (!opregion->lpat_table) {
  81. *value = raw_temp;
  82. return 0;
  83. }
  84. temp = acpi_lpat_raw_to_temp(opregion->lpat_table, raw_temp);
  85. if (temp < 0)
  86. return temp;
  87. *value = temp;
  88. return 0;
  89. }
  90. static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg,
  91. u32 function, u64 *value)
  92. {
  93. return function == ACPI_READ ?
  94. pmic_read_temp(opregion, reg, value) : -EINVAL;
  95. }
  96. static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg,
  97. u32 function, u64 *value)
  98. {
  99. int raw_temp;
  100. if (function == ACPI_READ)
  101. return pmic_read_temp(opregion, reg, value);
  102. if (!opregion->data->update_aux)
  103. return -ENXIO;
  104. if (opregion->lpat_table) {
  105. raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value);
  106. if (raw_temp < 0)
  107. return raw_temp;
  108. } else {
  109. raw_temp = *value;
  110. }
  111. return opregion->data->update_aux(opregion->regmap, reg, raw_temp);
  112. }
  113. static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg,
  114. int bit, u32 function, u64 *value)
  115. {
  116. struct intel_pmic_opregion_data *d = opregion->data;
  117. struct regmap *regmap = opregion->regmap;
  118. if (!d->get_policy || !d->update_policy)
  119. return -ENXIO;
  120. if (function == ACPI_READ)
  121. return d->get_policy(regmap, reg, bit, value);
  122. if (*value != 0 && *value != 1)
  123. return -EINVAL;
  124. return d->update_policy(regmap, reg, bit, *value);
  125. }
  126. static bool pmic_thermal_is_temp(int address)
  127. {
  128. return (address <= 0x3c) && !(address % 12);
  129. }
  130. static bool pmic_thermal_is_aux(int address)
  131. {
  132. return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) ||
  133. (address >= 8 && address <= 0x44 && !((address - 8) % 12));
  134. }
  135. static bool pmic_thermal_is_pen(int address)
  136. {
  137. return address >= 0x48 && address <= 0x5c;
  138. }
  139. static acpi_status intel_pmic_thermal_handler(u32 function,
  140. acpi_physical_address address, u32 bits, u64 *value64,
  141. void *handler_context, void *region_context)
  142. {
  143. struct intel_pmic_opregion *opregion = region_context;
  144. struct intel_pmic_opregion_data *d = opregion->data;
  145. int reg, bit, result;
  146. if (bits != 32 || !value64)
  147. return AE_BAD_PARAMETER;
  148. result = pmic_get_reg_bit(address, d->thermal_table,
  149. d->thermal_table_count, &reg, &bit);
  150. if (result == -ENOENT)
  151. return AE_BAD_PARAMETER;
  152. mutex_lock(&opregion->lock);
  153. if (pmic_thermal_is_temp(address))
  154. result = pmic_thermal_temp(opregion, reg, function, value64);
  155. else if (pmic_thermal_is_aux(address))
  156. result = pmic_thermal_aux(opregion, reg, function, value64);
  157. else if (pmic_thermal_is_pen(address))
  158. result = pmic_thermal_pen(opregion, reg, bit,
  159. function, value64);
  160. else
  161. result = -EINVAL;
  162. mutex_unlock(&opregion->lock);
  163. if (result < 0) {
  164. if (result == -EINVAL)
  165. return AE_BAD_PARAMETER;
  166. else
  167. return AE_ERROR;
  168. }
  169. return AE_OK;
  170. }
  171. static acpi_status intel_pmic_regs_handler(u32 function,
  172. acpi_physical_address address, u32 bits, u64 *value64,
  173. void *handler_context, void *region_context)
  174. {
  175. struct intel_pmic_opregion *opregion = region_context;
  176. int result = 0;
  177. switch (address) {
  178. case 0:
  179. return AE_OK;
  180. case 1:
  181. opregion->ctx.addr |= (*value64 & 0xff) << 8;
  182. return AE_OK;
  183. case 2:
  184. opregion->ctx.addr |= *value64 & 0xff;
  185. return AE_OK;
  186. case 3:
  187. opregion->ctx.val = *value64 & 0xff;
  188. return AE_OK;
  189. case 4:
  190. if (*value64) {
  191. result = regmap_write(opregion->regmap, opregion->ctx.addr,
  192. opregion->ctx.val);
  193. } else {
  194. result = regmap_read(opregion->regmap, opregion->ctx.addr,
  195. &opregion->ctx.val);
  196. if (result == 0)
  197. *value64 = opregion->ctx.val;
  198. }
  199. memset(&opregion->ctx, 0x00, sizeof(opregion->ctx));
  200. }
  201. if (result < 0) {
  202. if (result == -EINVAL)
  203. return AE_BAD_PARAMETER;
  204. else
  205. return AE_ERROR;
  206. }
  207. return AE_OK;
  208. }
  209. int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
  210. struct regmap *regmap,
  211. struct intel_pmic_opregion_data *d)
  212. {
  213. acpi_status status;
  214. struct intel_pmic_opregion *opregion;
  215. int ret;
  216. if (!dev || !regmap || !d)
  217. return -EINVAL;
  218. if (!handle)
  219. return -ENODEV;
  220. opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
  221. if (!opregion)
  222. return -ENOMEM;
  223. mutex_init(&opregion->lock);
  224. opregion->regmap = regmap;
  225. opregion->lpat_table = acpi_lpat_get_conversion_table(handle);
  226. status = acpi_install_address_space_handler(handle,
  227. PMIC_POWER_OPREGION_ID,
  228. intel_pmic_power_handler,
  229. NULL, opregion);
  230. if (ACPI_FAILURE(status)) {
  231. ret = -ENODEV;
  232. goto out_error;
  233. }
  234. status = acpi_install_address_space_handler(handle,
  235. PMIC_THERMAL_OPREGION_ID,
  236. intel_pmic_thermal_handler,
  237. NULL, opregion);
  238. if (ACPI_FAILURE(status)) {
  239. acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID,
  240. intel_pmic_power_handler);
  241. ret = -ENODEV;
  242. goto out_remove_power_handler;
  243. }
  244. status = acpi_install_address_space_handler(handle,
  245. PMIC_REGS_OPREGION_ID, intel_pmic_regs_handler, NULL,
  246. opregion);
  247. if (ACPI_FAILURE(status)) {
  248. ret = -ENODEV;
  249. goto out_remove_thermal_handler;
  250. }
  251. opregion->data = d;
  252. return 0;
  253. out_remove_thermal_handler:
  254. acpi_remove_address_space_handler(handle, PMIC_THERMAL_OPREGION_ID,
  255. intel_pmic_thermal_handler);
  256. out_remove_power_handler:
  257. acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID,
  258. intel_pmic_power_handler);
  259. out_error:
  260. acpi_lpat_free_conversion_table(opregion->lpat_table);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);