intel_pmic_xpower.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * intel_pmic_xpower.c - XPower AXP288 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/init.h>
  16. #include <linux/acpi.h>
  17. #include <linux/mfd/axp20x.h>
  18. #include <linux/regmap.h>
  19. #include <linux/platform_device.h>
  20. #include "intel_pmic.h"
  21. #define XPOWER_GPADC_LOW 0x5b
  22. #define XPOWER_GPI1_CTRL 0x92
  23. #define GPI1_LDO_MASK GENMASK(2, 0)
  24. #define GPI1_LDO_ON (3 << 0)
  25. #define GPI1_LDO_OFF (4 << 0)
  26. #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
  27. #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
  28. #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
  29. #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
  30. #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
  31. static struct pmic_table power_table[] = {
  32. {
  33. .address = 0x00,
  34. .reg = 0x13,
  35. .bit = 0x05,
  36. }, /* ALD1 */
  37. {
  38. .address = 0x04,
  39. .reg = 0x13,
  40. .bit = 0x06,
  41. }, /* ALD2 */
  42. {
  43. .address = 0x08,
  44. .reg = 0x13,
  45. .bit = 0x07,
  46. }, /* ALD3 */
  47. {
  48. .address = 0x0c,
  49. .reg = 0x12,
  50. .bit = 0x03,
  51. }, /* DLD1 */
  52. {
  53. .address = 0x10,
  54. .reg = 0x12,
  55. .bit = 0x04,
  56. }, /* DLD2 */
  57. {
  58. .address = 0x14,
  59. .reg = 0x12,
  60. .bit = 0x05,
  61. }, /* DLD3 */
  62. {
  63. .address = 0x18,
  64. .reg = 0x12,
  65. .bit = 0x06,
  66. }, /* DLD4 */
  67. {
  68. .address = 0x1c,
  69. .reg = 0x12,
  70. .bit = 0x00,
  71. }, /* ELD1 */
  72. {
  73. .address = 0x20,
  74. .reg = 0x12,
  75. .bit = 0x01,
  76. }, /* ELD2 */
  77. {
  78. .address = 0x24,
  79. .reg = 0x12,
  80. .bit = 0x02,
  81. }, /* ELD3 */
  82. {
  83. .address = 0x28,
  84. .reg = 0x13,
  85. .bit = 0x02,
  86. }, /* FLD1 */
  87. {
  88. .address = 0x2c,
  89. .reg = 0x13,
  90. .bit = 0x03,
  91. }, /* FLD2 */
  92. {
  93. .address = 0x30,
  94. .reg = 0x13,
  95. .bit = 0x04,
  96. }, /* FLD3 */
  97. {
  98. .address = 0x34,
  99. .reg = 0x10,
  100. .bit = 0x03,
  101. }, /* BUC1 */
  102. {
  103. .address = 0x38,
  104. .reg = 0x10,
  105. .bit = 0x06,
  106. }, /* BUC2 */
  107. {
  108. .address = 0x3c,
  109. .reg = 0x10,
  110. .bit = 0x05,
  111. }, /* BUC3 */
  112. {
  113. .address = 0x40,
  114. .reg = 0x10,
  115. .bit = 0x04,
  116. }, /* BUC4 */
  117. {
  118. .address = 0x44,
  119. .reg = 0x10,
  120. .bit = 0x01,
  121. }, /* BUC5 */
  122. {
  123. .address = 0x48,
  124. .reg = 0x10,
  125. .bit = 0x00
  126. }, /* BUC6 */
  127. {
  128. .address = 0x4c,
  129. .reg = 0x92,
  130. }, /* GPI1 */
  131. };
  132. /* TMP0 - TMP5 are the same, all from GPADC */
  133. static struct pmic_table thermal_table[] = {
  134. {
  135. .address = 0x00,
  136. .reg = XPOWER_GPADC_LOW
  137. },
  138. {
  139. .address = 0x0c,
  140. .reg = XPOWER_GPADC_LOW
  141. },
  142. {
  143. .address = 0x18,
  144. .reg = XPOWER_GPADC_LOW
  145. },
  146. {
  147. .address = 0x24,
  148. .reg = XPOWER_GPADC_LOW
  149. },
  150. {
  151. .address = 0x30,
  152. .reg = XPOWER_GPADC_LOW
  153. },
  154. {
  155. .address = 0x3c,
  156. .reg = XPOWER_GPADC_LOW
  157. },
  158. };
  159. static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
  160. int bit, u64 *value)
  161. {
  162. int data;
  163. if (regmap_read(regmap, reg, &data))
  164. return -EIO;
  165. /* GPIO1 LDO regulator needs special handling */
  166. if (reg == XPOWER_GPI1_CTRL)
  167. *value = ((data & GPI1_LDO_MASK) == GPI1_LDO_ON);
  168. else
  169. *value = (data & BIT(bit)) ? 1 : 0;
  170. return 0;
  171. }
  172. static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
  173. int bit, bool on)
  174. {
  175. int data;
  176. /* GPIO1 LDO regulator needs special handling */
  177. if (reg == XPOWER_GPI1_CTRL)
  178. return regmap_update_bits(regmap, reg, GPI1_LDO_MASK,
  179. on ? GPI1_LDO_ON : GPI1_LDO_OFF);
  180. if (regmap_read(regmap, reg, &data))
  181. return -EIO;
  182. if (on)
  183. data |= BIT(bit);
  184. else
  185. data &= ~BIT(bit);
  186. if (regmap_write(regmap, reg, data))
  187. return -EIO;
  188. return 0;
  189. }
  190. /**
  191. * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
  192. *
  193. * @regmap: regmap of the PMIC device
  194. * @reg: register to get the reading
  195. *
  196. * Return a positive value on success, errno on failure.
  197. */
  198. static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
  199. {
  200. int ret, adc_ts_pin_ctrl;
  201. u8 buf[2];
  202. /*
  203. * The current-source used for the battery temp-sensor (TS) is shared
  204. * with the GPADC. For proper fuel-gauge and charger operation the TS
  205. * current-source needs to be permanently on. But to read the GPADC we
  206. * need to temporary switch the TS current-source to ondemand, so that
  207. * the GPADC can use it, otherwise we will always read an all 0 value.
  208. *
  209. * Note that the switching from on to on-ondemand is not necessary
  210. * when the TS current-source is off (this happens on devices which
  211. * do not use the TS-pin).
  212. */
  213. ret = regmap_read(regmap, AXP288_ADC_TS_PIN_CTRL, &adc_ts_pin_ctrl);
  214. if (ret)
  215. return ret;
  216. if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
  217. ret = regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
  218. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  219. AXP288_ADC_TS_CURRENT_ON_ONDEMAND);
  220. if (ret)
  221. return ret;
  222. /* Wait a bit after switching the current-source */
  223. usleep_range(6000, 10000);
  224. }
  225. ret = regmap_bulk_read(regmap, AXP288_GP_ADC_H, buf, 2);
  226. if (ret == 0)
  227. ret = (buf[0] << 4) + ((buf[1] >> 4) & 0x0f);
  228. if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
  229. regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
  230. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  231. AXP288_ADC_TS_CURRENT_ON);
  232. }
  233. return ret;
  234. }
  235. static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
  236. .get_power = intel_xpower_pmic_get_power,
  237. .update_power = intel_xpower_pmic_update_power,
  238. .get_raw_temp = intel_xpower_pmic_get_raw_temp,
  239. .power_table = power_table,
  240. .power_table_count = ARRAY_SIZE(power_table),
  241. .thermal_table = thermal_table,
  242. .thermal_table_count = ARRAY_SIZE(thermal_table),
  243. };
  244. static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
  245. acpi_physical_address address, u32 bit_width, u64 *value,
  246. void *handler_context, void *region_context)
  247. {
  248. return AE_OK;
  249. }
  250. static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
  251. {
  252. struct device *parent = pdev->dev.parent;
  253. struct axp20x_dev *axp20x = dev_get_drvdata(parent);
  254. acpi_status status;
  255. int result;
  256. status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
  257. ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
  258. NULL, NULL);
  259. if (ACPI_FAILURE(status))
  260. return -ENODEV;
  261. result = intel_pmic_install_opregion_handler(&pdev->dev,
  262. ACPI_HANDLE(parent), axp20x->regmap,
  263. &intel_xpower_pmic_opregion_data);
  264. if (result)
  265. acpi_remove_address_space_handler(ACPI_HANDLE(parent),
  266. ACPI_ADR_SPACE_GPIO,
  267. intel_xpower_pmic_gpio_handler);
  268. return result;
  269. }
  270. static struct platform_driver intel_xpower_pmic_opregion_driver = {
  271. .probe = intel_xpower_pmic_opregion_probe,
  272. .driver = {
  273. .name = "axp288_pmic_acpi",
  274. },
  275. };
  276. builtin_platform_driver(intel_xpower_pmic_opregion_driver);