intel_pmic_xpower.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * XPower AXP288 PMIC operation region driver
  4. *
  5. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/init.h>
  9. #include <linux/mfd/axp20x.h>
  10. #include <linux/regmap.h>
  11. #include <linux/platform_device.h>
  12. #include <asm/iosf_mbi.h>
  13. #include "intel_pmic.h"
  14. #define XPOWER_GPADC_LOW 0x5b
  15. #define XPOWER_GPI1_CTRL 0x92
  16. #define GPI1_LDO_MASK GENMASK(2, 0)
  17. #define GPI1_LDO_ON (3 << 0)
  18. #define GPI1_LDO_OFF (4 << 0)
  19. #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
  20. #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
  21. #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
  22. #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
  23. #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
  24. static struct pmic_table power_table[] = {
  25. {
  26. .address = 0x00,
  27. .reg = 0x13,
  28. .bit = 0x05,
  29. }, /* ALD1 */
  30. {
  31. .address = 0x04,
  32. .reg = 0x13,
  33. .bit = 0x06,
  34. }, /* ALD2 */
  35. {
  36. .address = 0x08,
  37. .reg = 0x13,
  38. .bit = 0x07,
  39. }, /* ALD3 */
  40. {
  41. .address = 0x0c,
  42. .reg = 0x12,
  43. .bit = 0x03,
  44. }, /* DLD1 */
  45. {
  46. .address = 0x10,
  47. .reg = 0x12,
  48. .bit = 0x04,
  49. }, /* DLD2 */
  50. {
  51. .address = 0x14,
  52. .reg = 0x12,
  53. .bit = 0x05,
  54. }, /* DLD3 */
  55. {
  56. .address = 0x18,
  57. .reg = 0x12,
  58. .bit = 0x06,
  59. }, /* DLD4 */
  60. {
  61. .address = 0x1c,
  62. .reg = 0x12,
  63. .bit = 0x00,
  64. }, /* ELD1 */
  65. {
  66. .address = 0x20,
  67. .reg = 0x12,
  68. .bit = 0x01,
  69. }, /* ELD2 */
  70. {
  71. .address = 0x24,
  72. .reg = 0x12,
  73. .bit = 0x02,
  74. }, /* ELD3 */
  75. {
  76. .address = 0x28,
  77. .reg = 0x13,
  78. .bit = 0x02,
  79. }, /* FLD1 */
  80. {
  81. .address = 0x2c,
  82. .reg = 0x13,
  83. .bit = 0x03,
  84. }, /* FLD2 */
  85. {
  86. .address = 0x30,
  87. .reg = 0x13,
  88. .bit = 0x04,
  89. }, /* FLD3 */
  90. {
  91. .address = 0x34,
  92. .reg = 0x10,
  93. .bit = 0x03,
  94. }, /* BUC1 */
  95. {
  96. .address = 0x38,
  97. .reg = 0x10,
  98. .bit = 0x06,
  99. }, /* BUC2 */
  100. {
  101. .address = 0x3c,
  102. .reg = 0x10,
  103. .bit = 0x05,
  104. }, /* BUC3 */
  105. {
  106. .address = 0x40,
  107. .reg = 0x10,
  108. .bit = 0x04,
  109. }, /* BUC4 */
  110. {
  111. .address = 0x44,
  112. .reg = 0x10,
  113. .bit = 0x01,
  114. }, /* BUC5 */
  115. {
  116. .address = 0x48,
  117. .reg = 0x10,
  118. .bit = 0x00
  119. }, /* BUC6 */
  120. {
  121. .address = 0x4c,
  122. .reg = 0x92,
  123. }, /* GPI1 */
  124. };
  125. /* TMP0 - TMP5 are the same, all from GPADC */
  126. static struct pmic_table thermal_table[] = {
  127. {
  128. .address = 0x00,
  129. .reg = XPOWER_GPADC_LOW
  130. },
  131. {
  132. .address = 0x0c,
  133. .reg = XPOWER_GPADC_LOW
  134. },
  135. {
  136. .address = 0x18,
  137. .reg = XPOWER_GPADC_LOW
  138. },
  139. {
  140. .address = 0x24,
  141. .reg = XPOWER_GPADC_LOW
  142. },
  143. {
  144. .address = 0x30,
  145. .reg = XPOWER_GPADC_LOW
  146. },
  147. {
  148. .address = 0x3c,
  149. .reg = XPOWER_GPADC_LOW
  150. },
  151. };
  152. static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
  153. int bit, u64 *value)
  154. {
  155. int data;
  156. if (regmap_read(regmap, reg, &data))
  157. return -EIO;
  158. /* GPIO1 LDO regulator needs special handling */
  159. if (reg == XPOWER_GPI1_CTRL)
  160. *value = ((data & GPI1_LDO_MASK) == GPI1_LDO_ON);
  161. else
  162. *value = (data & BIT(bit)) ? 1 : 0;
  163. return 0;
  164. }
  165. static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
  166. int bit, bool on)
  167. {
  168. int data, ret;
  169. ret = iosf_mbi_block_punit_i2c_access();
  170. if (ret)
  171. return ret;
  172. /* GPIO1 LDO regulator needs special handling */
  173. if (reg == XPOWER_GPI1_CTRL) {
  174. ret = regmap_update_bits(regmap, reg, GPI1_LDO_MASK,
  175. on ? GPI1_LDO_ON : GPI1_LDO_OFF);
  176. goto out;
  177. }
  178. if (regmap_read(regmap, reg, &data)) {
  179. ret = -EIO;
  180. goto out;
  181. }
  182. if (on)
  183. data |= BIT(bit);
  184. else
  185. data &= ~BIT(bit);
  186. if (regmap_write(regmap, reg, data))
  187. ret = -EIO;
  188. out:
  189. iosf_mbi_unblock_punit_i2c_access();
  190. return ret;
  191. }
  192. /**
  193. * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
  194. *
  195. * @regmap: regmap of the PMIC device
  196. * @reg: register to get the reading
  197. *
  198. * Return a positive value on success, errno on failure.
  199. */
  200. static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
  201. {
  202. int ret, adc_ts_pin_ctrl;
  203. u8 buf[2];
  204. /*
  205. * The current-source used for the battery temp-sensor (TS) is shared
  206. * with the GPADC. For proper fuel-gauge and charger operation the TS
  207. * current-source needs to be permanently on. But to read the GPADC we
  208. * need to temporary switch the TS current-source to ondemand, so that
  209. * the GPADC can use it, otherwise we will always read an all 0 value.
  210. *
  211. * Note that the switching from on to on-ondemand is not necessary
  212. * when the TS current-source is off (this happens on devices which
  213. * do not use the TS-pin).
  214. */
  215. ret = regmap_read(regmap, AXP288_ADC_TS_PIN_CTRL, &adc_ts_pin_ctrl);
  216. if (ret)
  217. return ret;
  218. if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
  219. /*
  220. * AXP288_ADC_TS_PIN_CTRL reads are cached by the regmap, so
  221. * this does to a single I2C-transfer, and thus there is no
  222. * need to explicitly call iosf_mbi_block_punit_i2c_access().
  223. */
  224. ret = regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
  225. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  226. AXP288_ADC_TS_CURRENT_ON_ONDEMAND);
  227. if (ret)
  228. return ret;
  229. /* Wait a bit after switching the current-source */
  230. usleep_range(6000, 10000);
  231. }
  232. ret = iosf_mbi_block_punit_i2c_access();
  233. if (ret)
  234. return ret;
  235. ret = regmap_bulk_read(regmap, AXP288_GP_ADC_H, buf, 2);
  236. if (ret == 0)
  237. ret = (buf[0] << 4) + ((buf[1] >> 4) & 0x0f);
  238. if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
  239. regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
  240. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  241. AXP288_ADC_TS_CURRENT_ON);
  242. }
  243. iosf_mbi_unblock_punit_i2c_access();
  244. return ret;
  245. }
  246. static int intel_xpower_exec_mipi_pmic_seq_element(struct regmap *regmap,
  247. u16 i2c_address, u32 reg_address,
  248. u32 value, u32 mask)
  249. {
  250. int ret;
  251. if (i2c_address != 0x34) {
  252. pr_err("%s: Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
  253. __func__, i2c_address, reg_address, value, mask);
  254. return -ENXIO;
  255. }
  256. ret = iosf_mbi_block_punit_i2c_access();
  257. if (ret)
  258. return ret;
  259. ret = regmap_update_bits(regmap, reg_address, mask, value);
  260. iosf_mbi_unblock_punit_i2c_access();
  261. return ret;
  262. }
  263. static int intel_xpower_lpat_raw_to_temp(struct acpi_lpat_conversion_table *lpat_table,
  264. int raw)
  265. {
  266. struct acpi_lpat first = lpat_table->lpat[0];
  267. struct acpi_lpat last = lpat_table->lpat[lpat_table->lpat_count - 1];
  268. /*
  269. * Some LPAT tables in the ACPI Device for the AXP288 PMIC for some
  270. * reason only describe a small temperature range, e.g. 27° - 37°
  271. * Celcius. Resulting in errors when the tablet is idle in a cool room.
  272. *
  273. * To avoid these errors clamp the raw value to be inside the LPAT.
  274. */
  275. if (first.raw < last.raw)
  276. raw = clamp(raw, first.raw, last.raw);
  277. else
  278. raw = clamp(raw, last.raw, first.raw);
  279. return acpi_lpat_raw_to_temp(lpat_table, raw);
  280. }
  281. static const struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
  282. .get_power = intel_xpower_pmic_get_power,
  283. .update_power = intel_xpower_pmic_update_power,
  284. .get_raw_temp = intel_xpower_pmic_get_raw_temp,
  285. .exec_mipi_pmic_seq_element = intel_xpower_exec_mipi_pmic_seq_element,
  286. .lpat_raw_to_temp = intel_xpower_lpat_raw_to_temp,
  287. .power_table = power_table,
  288. .power_table_count = ARRAY_SIZE(power_table),
  289. .thermal_table = thermal_table,
  290. .thermal_table_count = ARRAY_SIZE(thermal_table),
  291. .pmic_i2c_address = 0x34,
  292. };
  293. static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
  294. acpi_physical_address address, u32 bit_width, u64 *value,
  295. void *handler_context, void *region_context)
  296. {
  297. return AE_OK;
  298. }
  299. static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
  300. {
  301. struct device *parent = pdev->dev.parent;
  302. struct axp20x_dev *axp20x = dev_get_drvdata(parent);
  303. acpi_status status;
  304. int result;
  305. status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
  306. ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
  307. NULL, NULL);
  308. if (ACPI_FAILURE(status))
  309. return -ENODEV;
  310. result = intel_pmic_install_opregion_handler(&pdev->dev,
  311. ACPI_HANDLE(parent), axp20x->regmap,
  312. &intel_xpower_pmic_opregion_data);
  313. if (result)
  314. acpi_remove_address_space_handler(ACPI_HANDLE(parent),
  315. ACPI_ADR_SPACE_GPIO,
  316. intel_xpower_pmic_gpio_handler);
  317. return result;
  318. }
  319. static struct platform_driver intel_xpower_pmic_opregion_driver = {
  320. .probe = intel_xpower_pmic_opregion_probe,
  321. .driver = {
  322. .name = "axp288_pmic_acpi",
  323. },
  324. };
  325. builtin_platform_driver(intel_xpower_pmic_opregion_driver);