intel_pmic_xpower.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/module.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 <linux/iio/consumer.h>
  21. #include "intel_pmic.h"
  22. #define XPOWER_GPADC_LOW 0x5b
  23. static struct pmic_table power_table[] = {
  24. {
  25. .address = 0x00,
  26. .reg = 0x13,
  27. .bit = 0x05,
  28. }, /* ALD1 */
  29. {
  30. .address = 0x04,
  31. .reg = 0x13,
  32. .bit = 0x06,
  33. }, /* ALD2 */
  34. {
  35. .address = 0x08,
  36. .reg = 0x13,
  37. .bit = 0x07,
  38. }, /* ALD3 */
  39. {
  40. .address = 0x0c,
  41. .reg = 0x12,
  42. .bit = 0x03,
  43. }, /* DLD1 */
  44. {
  45. .address = 0x10,
  46. .reg = 0x12,
  47. .bit = 0x04,
  48. }, /* DLD2 */
  49. {
  50. .address = 0x14,
  51. .reg = 0x12,
  52. .bit = 0x05,
  53. }, /* DLD3 */
  54. {
  55. .address = 0x18,
  56. .reg = 0x12,
  57. .bit = 0x06,
  58. }, /* DLD4 */
  59. {
  60. .address = 0x1c,
  61. .reg = 0x12,
  62. .bit = 0x00,
  63. }, /* ELD1 */
  64. {
  65. .address = 0x20,
  66. .reg = 0x12,
  67. .bit = 0x01,
  68. }, /* ELD2 */
  69. {
  70. .address = 0x24,
  71. .reg = 0x12,
  72. .bit = 0x02,
  73. }, /* ELD3 */
  74. {
  75. .address = 0x28,
  76. .reg = 0x13,
  77. .bit = 0x02,
  78. }, /* FLD1 */
  79. {
  80. .address = 0x2c,
  81. .reg = 0x13,
  82. .bit = 0x03,
  83. }, /* FLD2 */
  84. {
  85. .address = 0x30,
  86. .reg = 0x13,
  87. .bit = 0x04,
  88. }, /* FLD3 */
  89. {
  90. .address = 0x34,
  91. .reg = 0x10,
  92. .bit = 0x03,
  93. }, /* BUC1 */
  94. {
  95. .address = 0x38,
  96. .reg = 0x10,
  97. .bit = 0x06,
  98. }, /* BUC2 */
  99. {
  100. .address = 0x3c,
  101. .reg = 0x10,
  102. .bit = 0x05,
  103. }, /* BUC3 */
  104. {
  105. .address = 0x40,
  106. .reg = 0x10,
  107. .bit = 0x04,
  108. }, /* BUC4 */
  109. {
  110. .address = 0x44,
  111. .reg = 0x10,
  112. .bit = 0x01,
  113. }, /* BUC5 */
  114. {
  115. .address = 0x48,
  116. .reg = 0x10,
  117. .bit = 0x00
  118. }, /* BUC6 */
  119. };
  120. /* TMP0 - TMP5 are the same, all from GPADC */
  121. static struct pmic_table thermal_table[] = {
  122. {
  123. .address = 0x00,
  124. .reg = XPOWER_GPADC_LOW
  125. },
  126. {
  127. .address = 0x0c,
  128. .reg = XPOWER_GPADC_LOW
  129. },
  130. {
  131. .address = 0x18,
  132. .reg = XPOWER_GPADC_LOW
  133. },
  134. {
  135. .address = 0x24,
  136. .reg = XPOWER_GPADC_LOW
  137. },
  138. {
  139. .address = 0x30,
  140. .reg = XPOWER_GPADC_LOW
  141. },
  142. {
  143. .address = 0x3c,
  144. .reg = XPOWER_GPADC_LOW
  145. },
  146. };
  147. static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
  148. int bit, u64 *value)
  149. {
  150. int data;
  151. if (regmap_read(regmap, reg, &data))
  152. return -EIO;
  153. *value = (data & BIT(bit)) ? 1 : 0;
  154. return 0;
  155. }
  156. static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
  157. int bit, bool on)
  158. {
  159. int data;
  160. if (regmap_read(regmap, reg, &data))
  161. return -EIO;
  162. if (on)
  163. data |= BIT(bit);
  164. else
  165. data &= ~BIT(bit);
  166. if (regmap_write(regmap, reg, data))
  167. return -EIO;
  168. return 0;
  169. }
  170. /**
  171. * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
  172. *
  173. * @regmap: regmap of the PMIC device
  174. * @reg: register to get the reading
  175. *
  176. * We could get the sensor value by manipulating the HW regs here, but since
  177. * the axp288 IIO driver may also access the same regs at the same time, the
  178. * APIs provided by IIO subsystem are used here instead to avoid problems. As
  179. * a result, the two passed in params are of no actual use.
  180. *
  181. * Return a positive value on success, errno on failure.
  182. */
  183. static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
  184. {
  185. struct iio_channel *gpadc_chan;
  186. int ret, val;
  187. gpadc_chan = iio_channel_get(NULL, "axp288-system-temp");
  188. if (IS_ERR_OR_NULL(gpadc_chan))
  189. return -EACCES;
  190. ret = iio_read_channel_raw(gpadc_chan, &val);
  191. if (ret < 0)
  192. val = ret;
  193. iio_channel_release(gpadc_chan);
  194. return val;
  195. }
  196. static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
  197. .get_power = intel_xpower_pmic_get_power,
  198. .update_power = intel_xpower_pmic_update_power,
  199. .get_raw_temp = intel_xpower_pmic_get_raw_temp,
  200. .power_table = power_table,
  201. .power_table_count = ARRAY_SIZE(power_table),
  202. .thermal_table = thermal_table,
  203. .thermal_table_count = ARRAY_SIZE(thermal_table),
  204. };
  205. static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
  206. acpi_physical_address address, u32 bit_width, u64 *value,
  207. void *handler_context, void *region_context)
  208. {
  209. return AE_OK;
  210. }
  211. static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
  212. {
  213. struct device *parent = pdev->dev.parent;
  214. struct axp20x_dev *axp20x = dev_get_drvdata(parent);
  215. acpi_status status;
  216. int result;
  217. status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
  218. ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
  219. NULL, NULL);
  220. if (ACPI_FAILURE(status))
  221. return -ENODEV;
  222. result = intel_pmic_install_opregion_handler(&pdev->dev,
  223. ACPI_HANDLE(parent), axp20x->regmap,
  224. &intel_xpower_pmic_opregion_data);
  225. if (result)
  226. acpi_remove_address_space_handler(ACPI_HANDLE(parent),
  227. ACPI_ADR_SPACE_GPIO,
  228. intel_xpower_pmic_gpio_handler);
  229. return result;
  230. }
  231. static struct platform_driver intel_xpower_pmic_opregion_driver = {
  232. .probe = intel_xpower_pmic_opregion_probe,
  233. .driver = {
  234. .name = "axp288_pmic_acpi",
  235. },
  236. };
  237. static int __init intel_xpower_pmic_opregion_driver_init(void)
  238. {
  239. return platform_driver_register(&intel_xpower_pmic_opregion_driver);
  240. }
  241. module_init(intel_xpower_pmic_opregion_driver_init);
  242. MODULE_DESCRIPTION("XPower AXP288 ACPI operation region driver");
  243. MODULE_LICENSE("GPL");