intel_pmic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "intel_pmic.h"
  19. #define PMIC_POWER_OPREGION_ID 0x8d
  20. #define PMIC_THERMAL_OPREGION_ID 0x8c
  21. struct acpi_lpat {
  22. int temp;
  23. int raw;
  24. };
  25. struct intel_pmic_opregion {
  26. struct mutex lock;
  27. struct acpi_lpat *lpat;
  28. int lpat_count;
  29. struct regmap *regmap;
  30. struct intel_pmic_opregion_data *data;
  31. };
  32. static int pmic_get_reg_bit(int address, struct pmic_table *table,
  33. int count, int *reg, int *bit)
  34. {
  35. int i;
  36. for (i = 0; i < count; i++) {
  37. if (table[i].address == address) {
  38. *reg = table[i].reg;
  39. if (bit)
  40. *bit = table[i].bit;
  41. return 0;
  42. }
  43. }
  44. return -ENOENT;
  45. }
  46. /**
  47. * raw_to_temp(): Return temperature from raw value through LPAT table
  48. *
  49. * @lpat: the temperature_raw mapping table
  50. * @count: the count of the above mapping table
  51. * @raw: the raw value, used as a key to get the temerature from the
  52. * above mapping table
  53. *
  54. * A positive value will be returned on success, a negative errno will
  55. * be returned in error cases.
  56. */
  57. static int raw_to_temp(struct acpi_lpat *lpat, int count, int raw)
  58. {
  59. int i, delta_temp, delta_raw, temp;
  60. for (i = 0; i < count - 1; i++) {
  61. if ((raw >= lpat[i].raw && raw <= lpat[i+1].raw) ||
  62. (raw <= lpat[i].raw && raw >= lpat[i+1].raw))
  63. break;
  64. }
  65. if (i == count - 1)
  66. return -ENOENT;
  67. delta_temp = lpat[i+1].temp - lpat[i].temp;
  68. delta_raw = lpat[i+1].raw - lpat[i].raw;
  69. temp = lpat[i].temp + (raw - lpat[i].raw) * delta_temp / delta_raw;
  70. return temp;
  71. }
  72. /**
  73. * temp_to_raw(): Return raw value from temperature through LPAT table
  74. *
  75. * @lpat: the temperature_raw mapping table
  76. * @count: the count of the above mapping table
  77. * @temp: the temperature, used as a key to get the raw value from the
  78. * above mapping table
  79. *
  80. * A positive value will be returned on success, a negative errno will
  81. * be returned in error cases.
  82. */
  83. static int temp_to_raw(struct acpi_lpat *lpat, int count, int temp)
  84. {
  85. int i, delta_temp, delta_raw, raw;
  86. for (i = 0; i < count - 1; i++) {
  87. if (temp >= lpat[i].temp && temp <= lpat[i+1].temp)
  88. break;
  89. }
  90. if (i == count - 1)
  91. return -ENOENT;
  92. delta_temp = lpat[i+1].temp - lpat[i].temp;
  93. delta_raw = lpat[i+1].raw - lpat[i].raw;
  94. raw = lpat[i].raw + (temp - lpat[i].temp) * delta_raw / delta_temp;
  95. return raw;
  96. }
  97. static void pmic_thermal_lpat(struct intel_pmic_opregion *opregion,
  98. acpi_handle handle, struct device *dev)
  99. {
  100. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  101. union acpi_object *obj_p, *obj_e;
  102. int *lpat, i;
  103. acpi_status status;
  104. status = acpi_evaluate_object(handle, "LPAT", NULL, &buffer);
  105. if (ACPI_FAILURE(status))
  106. return;
  107. obj_p = (union acpi_object *)buffer.pointer;
  108. if (!obj_p || (obj_p->type != ACPI_TYPE_PACKAGE) ||
  109. (obj_p->package.count % 2) || (obj_p->package.count < 4))
  110. goto out;
  111. lpat = devm_kmalloc(dev, sizeof(int) * obj_p->package.count,
  112. GFP_KERNEL);
  113. if (!lpat)
  114. goto out;
  115. for (i = 0; i < obj_p->package.count; i++) {
  116. obj_e = &obj_p->package.elements[i];
  117. if (obj_e->type != ACPI_TYPE_INTEGER) {
  118. devm_kfree(dev, lpat);
  119. goto out;
  120. }
  121. lpat[i] = (s64)obj_e->integer.value;
  122. }
  123. opregion->lpat = (struct acpi_lpat *)lpat;
  124. opregion->lpat_count = obj_p->package.count / 2;
  125. out:
  126. kfree(buffer.pointer);
  127. }
  128. static acpi_status intel_pmic_power_handler(u32 function,
  129. acpi_physical_address address, u32 bits, u64 *value64,
  130. void *handler_context, void *region_context)
  131. {
  132. struct intel_pmic_opregion *opregion = region_context;
  133. struct regmap *regmap = opregion->regmap;
  134. struct intel_pmic_opregion_data *d = opregion->data;
  135. int reg, bit, result;
  136. if (bits != 32 || !value64)
  137. return AE_BAD_PARAMETER;
  138. if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1))
  139. return AE_BAD_PARAMETER;
  140. result = pmic_get_reg_bit(address, d->power_table,
  141. d->power_table_count, &reg, &bit);
  142. if (result == -ENOENT)
  143. return AE_BAD_PARAMETER;
  144. mutex_lock(&opregion->lock);
  145. result = function == ACPI_READ ?
  146. d->get_power(regmap, reg, bit, value64) :
  147. d->update_power(regmap, reg, bit, *value64 == 1);
  148. mutex_unlock(&opregion->lock);
  149. return result ? AE_ERROR : AE_OK;
  150. }
  151. static int pmic_read_temp(struct intel_pmic_opregion *opregion,
  152. int reg, u64 *value)
  153. {
  154. int raw_temp, temp;
  155. if (!opregion->data->get_raw_temp)
  156. return -ENXIO;
  157. raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg);
  158. if (raw_temp < 0)
  159. return raw_temp;
  160. if (!opregion->lpat) {
  161. *value = raw_temp;
  162. return 0;
  163. }
  164. temp = raw_to_temp(opregion->lpat, opregion->lpat_count, raw_temp);
  165. if (temp < 0)
  166. return temp;
  167. *value = temp;
  168. return 0;
  169. }
  170. static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg,
  171. u32 function, u64 *value)
  172. {
  173. return function == ACPI_READ ?
  174. pmic_read_temp(opregion, reg, value) : -EINVAL;
  175. }
  176. static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg,
  177. u32 function, u64 *value)
  178. {
  179. int raw_temp;
  180. if (function == ACPI_READ)
  181. return pmic_read_temp(opregion, reg, value);
  182. if (!opregion->data->update_aux)
  183. return -ENXIO;
  184. if (opregion->lpat) {
  185. raw_temp = temp_to_raw(opregion->lpat, opregion->lpat_count,
  186. *value);
  187. if (raw_temp < 0)
  188. return raw_temp;
  189. } else {
  190. raw_temp = *value;
  191. }
  192. return opregion->data->update_aux(opregion->regmap, reg, raw_temp);
  193. }
  194. static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg,
  195. u32 function, u64 *value)
  196. {
  197. struct intel_pmic_opregion_data *d = opregion->data;
  198. struct regmap *regmap = opregion->regmap;
  199. if (!d->get_policy || !d->update_policy)
  200. return -ENXIO;
  201. if (function == ACPI_READ)
  202. return d->get_policy(regmap, reg, value);
  203. if (*value != 0 && *value != 1)
  204. return -EINVAL;
  205. return d->update_policy(regmap, reg, *value);
  206. }
  207. static bool pmic_thermal_is_temp(int address)
  208. {
  209. return (address <= 0x3c) && !(address % 12);
  210. }
  211. static bool pmic_thermal_is_aux(int address)
  212. {
  213. return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) ||
  214. (address >= 8 && address <= 0x44 && !((address - 8) % 12));
  215. }
  216. static bool pmic_thermal_is_pen(int address)
  217. {
  218. return address >= 0x48 && address <= 0x5c;
  219. }
  220. static acpi_status intel_pmic_thermal_handler(u32 function,
  221. acpi_physical_address address, u32 bits, u64 *value64,
  222. void *handler_context, void *region_context)
  223. {
  224. struct intel_pmic_opregion *opregion = region_context;
  225. struct intel_pmic_opregion_data *d = opregion->data;
  226. int reg, result;
  227. if (bits != 32 || !value64)
  228. return AE_BAD_PARAMETER;
  229. result = pmic_get_reg_bit(address, d->thermal_table,
  230. d->thermal_table_count, &reg, NULL);
  231. if (result == -ENOENT)
  232. return AE_BAD_PARAMETER;
  233. mutex_lock(&opregion->lock);
  234. if (pmic_thermal_is_temp(address))
  235. result = pmic_thermal_temp(opregion, reg, function, value64);
  236. else if (pmic_thermal_is_aux(address))
  237. result = pmic_thermal_aux(opregion, reg, function, value64);
  238. else if (pmic_thermal_is_pen(address))
  239. result = pmic_thermal_pen(opregion, reg, function, value64);
  240. else
  241. result = -EINVAL;
  242. mutex_unlock(&opregion->lock);
  243. if (result < 0) {
  244. if (result == -EINVAL)
  245. return AE_BAD_PARAMETER;
  246. else
  247. return AE_ERROR;
  248. }
  249. return AE_OK;
  250. }
  251. int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
  252. struct regmap *regmap,
  253. struct intel_pmic_opregion_data *d)
  254. {
  255. acpi_status status;
  256. struct intel_pmic_opregion *opregion;
  257. if (!dev || !regmap || !d)
  258. return -EINVAL;
  259. if (!handle)
  260. return -ENODEV;
  261. opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
  262. if (!opregion)
  263. return -ENOMEM;
  264. mutex_init(&opregion->lock);
  265. opregion->regmap = regmap;
  266. pmic_thermal_lpat(opregion, handle, dev);
  267. status = acpi_install_address_space_handler(handle,
  268. PMIC_POWER_OPREGION_ID,
  269. intel_pmic_power_handler,
  270. NULL, opregion);
  271. if (ACPI_FAILURE(status))
  272. return -ENODEV;
  273. status = acpi_install_address_space_handler(handle,
  274. PMIC_THERMAL_OPREGION_ID,
  275. intel_pmic_thermal_handler,
  276. NULL, opregion);
  277. if (ACPI_FAILURE(status)) {
  278. acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID,
  279. intel_pmic_power_handler);
  280. return -ENODEV;
  281. }
  282. opregion->data = d;
  283. return 0;
  284. }
  285. EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);
  286. MODULE_LICENSE("GPL");