intel_pmic.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel_pmic.c - Intel PMIC operation region driver
  4. *
  5. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/acpi.h>
  9. #include <linux/mfd/intel_soc_pmic.h>
  10. #include <linux/regmap.h>
  11. #include <acpi/acpi_lpat.h>
  12. #include "intel_pmic.h"
  13. #define PMIC_POWER_OPREGION_ID 0x8d
  14. #define PMIC_THERMAL_OPREGION_ID 0x8c
  15. #define PMIC_REGS_OPREGION_ID 0x8f
  16. struct intel_pmic_regs_handler_ctx {
  17. unsigned int val;
  18. u16 addr;
  19. };
  20. struct intel_pmic_opregion {
  21. struct mutex lock;
  22. struct acpi_lpat_conversion_table *lpat_table;
  23. struct regmap *regmap;
  24. const struct intel_pmic_opregion_data *data;
  25. struct intel_pmic_regs_handler_ctx ctx;
  26. };
  27. static struct intel_pmic_opregion *intel_pmic_opregion;
  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. const 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 = opregion->data->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. int bit, u32 function, u64 *value)
  109. {
  110. const 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, bit, value);
  116. if (*value != 0 && *value != 1)
  117. return -EINVAL;
  118. return d->update_policy(regmap, reg, bit, *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. const struct intel_pmic_opregion_data *d = opregion->data;
  139. int reg, bit, 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, &bit);
  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, bit,
  153. function, value64);
  154. else
  155. result = -EINVAL;
  156. mutex_unlock(&opregion->lock);
  157. if (result < 0) {
  158. if (result == -EINVAL)
  159. return AE_BAD_PARAMETER;
  160. else
  161. return AE_ERROR;
  162. }
  163. return AE_OK;
  164. }
  165. static acpi_status intel_pmic_regs_handler(u32 function,
  166. acpi_physical_address address, u32 bits, u64 *value64,
  167. void *handler_context, void *region_context)
  168. {
  169. struct intel_pmic_opregion *opregion = region_context;
  170. int result = -EINVAL;
  171. if (function == ACPI_WRITE) {
  172. switch (address) {
  173. case 0:
  174. return AE_OK;
  175. case 1:
  176. opregion->ctx.addr |= (*value64 & 0xff) << 8;
  177. return AE_OK;
  178. case 2:
  179. opregion->ctx.addr |= *value64 & 0xff;
  180. return AE_OK;
  181. case 3:
  182. opregion->ctx.val = *value64 & 0xff;
  183. return AE_OK;
  184. case 4:
  185. if (*value64) {
  186. result = regmap_write(opregion->regmap, opregion->ctx.addr,
  187. opregion->ctx.val);
  188. } else {
  189. result = regmap_read(opregion->regmap, opregion->ctx.addr,
  190. &opregion->ctx.val);
  191. }
  192. opregion->ctx.addr = 0;
  193. }
  194. }
  195. if (function == ACPI_READ && address == 3) {
  196. *value64 = opregion->ctx.val;
  197. return AE_OK;
  198. }
  199. if (result < 0) {
  200. if (result == -EINVAL)
  201. return AE_BAD_PARAMETER;
  202. else
  203. return AE_ERROR;
  204. }
  205. return AE_OK;
  206. }
  207. int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
  208. struct regmap *regmap,
  209. const struct intel_pmic_opregion_data *d)
  210. {
  211. acpi_status status = AE_OK;
  212. struct intel_pmic_opregion *opregion;
  213. int ret;
  214. if (!dev || !regmap || !d)
  215. return -EINVAL;
  216. if (!handle)
  217. return -ENODEV;
  218. opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
  219. if (!opregion)
  220. return -ENOMEM;
  221. mutex_init(&opregion->lock);
  222. opregion->regmap = regmap;
  223. opregion->lpat_table = acpi_lpat_get_conversion_table(handle);
  224. if (d->power_table_count)
  225. status = acpi_install_address_space_handler(handle,
  226. PMIC_POWER_OPREGION_ID,
  227. intel_pmic_power_handler,
  228. NULL, opregion);
  229. if (ACPI_FAILURE(status)) {
  230. ret = -ENODEV;
  231. goto out_error;
  232. }
  233. if (d->thermal_table_count)
  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. ret = -ENODEV;
  240. goto out_remove_power_handler;
  241. }
  242. status = acpi_install_address_space_handler(handle,
  243. PMIC_REGS_OPREGION_ID, intel_pmic_regs_handler, NULL,
  244. opregion);
  245. if (ACPI_FAILURE(status)) {
  246. ret = -ENODEV;
  247. goto out_remove_thermal_handler;
  248. }
  249. opregion->data = d;
  250. intel_pmic_opregion = opregion;
  251. return 0;
  252. out_remove_thermal_handler:
  253. if (d->thermal_table_count)
  254. acpi_remove_address_space_handler(handle,
  255. PMIC_THERMAL_OPREGION_ID,
  256. intel_pmic_thermal_handler);
  257. out_remove_power_handler:
  258. if (d->power_table_count)
  259. acpi_remove_address_space_handler(handle,
  260. PMIC_POWER_OPREGION_ID,
  261. intel_pmic_power_handler);
  262. out_error:
  263. acpi_lpat_free_conversion_table(opregion->lpat_table);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);
  267. /**
  268. * intel_soc_pmic_exec_mipi_pmic_seq_element - Execute PMIC MIPI sequence
  269. * @i2c_address: I2C client address for the PMIC
  270. * @reg_address: PMIC register address
  271. * @value: New value for the register bits to change
  272. * @mask: Mask indicating which register bits to change
  273. *
  274. * DSI LCD panels describe an initialization sequence in the i915 VBT (Video
  275. * BIOS Tables) using so called MIPI sequences. One possible element in these
  276. * sequences is a PMIC specific element of 15 bytes.
  277. *
  278. * This function executes these PMIC specific elements sending the embedded
  279. * commands to the PMIC.
  280. *
  281. * Return 0 on success, < 0 on failure.
  282. */
  283. int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address,
  284. u32 value, u32 mask)
  285. {
  286. const struct intel_pmic_opregion_data *d;
  287. int ret;
  288. if (!intel_pmic_opregion) {
  289. pr_warn("%s: No PMIC registered\n", __func__);
  290. return -ENXIO;
  291. }
  292. d = intel_pmic_opregion->data;
  293. mutex_lock(&intel_pmic_opregion->lock);
  294. if (d->exec_mipi_pmic_seq_element) {
  295. ret = d->exec_mipi_pmic_seq_element(intel_pmic_opregion->regmap,
  296. i2c_address, reg_address,
  297. value, mask);
  298. } else if (d->pmic_i2c_address) {
  299. if (i2c_address == d->pmic_i2c_address) {
  300. ret = regmap_update_bits(intel_pmic_opregion->regmap,
  301. reg_address, mask, value);
  302. } else {
  303. pr_err("%s: Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
  304. __func__, i2c_address, reg_address, value, mask);
  305. ret = -ENXIO;
  306. }
  307. } else {
  308. pr_warn("%s: Not implemented\n", __func__);
  309. pr_warn("%s: i2c-addr: 0x%x reg-addr 0x%x value 0x%x mask 0x%x\n",
  310. __func__, i2c_address, reg_address, value, mask);
  311. ret = -EOPNOTSUPP;
  312. }
  313. mutex_unlock(&intel_pmic_opregion->lock);
  314. return ret;
  315. }
  316. EXPORT_SYMBOL_GPL(intel_soc_pmic_exec_mipi_pmic_seq_element);