intel_pmic_crc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * intel_pmic_crc.c - Intel CrystalCove 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/intel_soc_pmic.h>
  18. #include <linux/regmap.h>
  19. #include <linux/platform_device.h>
  20. #include "intel_pmic.h"
  21. #define PWR_SOURCE_SELECT BIT(1)
  22. #define PMIC_A0LOCK_REG 0xc5
  23. static struct pmic_table power_table[] = {
  24. {
  25. .address = 0x24,
  26. .reg = 0x66,
  27. .bit = 0x00,
  28. },
  29. {
  30. .address = 0x48,
  31. .reg = 0x5d,
  32. .bit = 0x00,
  33. },
  34. };
  35. static struct pmic_table thermal_table[] = {
  36. {
  37. .address = 0x00,
  38. .reg = 0x75
  39. },
  40. {
  41. .address = 0x04,
  42. .reg = 0x95
  43. },
  44. {
  45. .address = 0x08,
  46. .reg = 0x97
  47. },
  48. {
  49. .address = 0x0c,
  50. .reg = 0x77
  51. },
  52. {
  53. .address = 0x10,
  54. .reg = 0x9a
  55. },
  56. {
  57. .address = 0x14,
  58. .reg = 0x9c
  59. },
  60. {
  61. .address = 0x18,
  62. .reg = 0x79
  63. },
  64. {
  65. .address = 0x1c,
  66. .reg = 0x9f
  67. },
  68. {
  69. .address = 0x20,
  70. .reg = 0xa1
  71. },
  72. {
  73. .address = 0x48,
  74. .reg = 0x94
  75. },
  76. {
  77. .address = 0x4c,
  78. .reg = 0x99
  79. },
  80. {
  81. .address = 0x50,
  82. .reg = 0x9e
  83. },
  84. };
  85. static int intel_crc_pmic_get_power(struct regmap *regmap, int reg,
  86. int bit, u64 *value)
  87. {
  88. int data;
  89. if (regmap_read(regmap, reg, &data))
  90. return -EIO;
  91. *value = (data & PWR_SOURCE_SELECT) && (data & BIT(bit)) ? 1 : 0;
  92. return 0;
  93. }
  94. static int intel_crc_pmic_update_power(struct regmap *regmap, int reg,
  95. int bit, bool on)
  96. {
  97. int data;
  98. if (regmap_read(regmap, reg, &data))
  99. return -EIO;
  100. if (on) {
  101. data |= PWR_SOURCE_SELECT | BIT(bit);
  102. } else {
  103. data &= ~BIT(bit);
  104. data |= PWR_SOURCE_SELECT;
  105. }
  106. if (regmap_write(regmap, reg, data))
  107. return -EIO;
  108. return 0;
  109. }
  110. static int intel_crc_pmic_get_raw_temp(struct regmap *regmap, int reg)
  111. {
  112. int temp_l, temp_h;
  113. /*
  114. * Raw temperature value is 10bits: 8bits in reg
  115. * and 2bits in reg-1: bit0,1
  116. */
  117. if (regmap_read(regmap, reg, &temp_l) ||
  118. regmap_read(regmap, reg - 1, &temp_h))
  119. return -EIO;
  120. return temp_l | (temp_h & 0x3) << 8;
  121. }
  122. static int intel_crc_pmic_update_aux(struct regmap *regmap, int reg, int raw)
  123. {
  124. return regmap_write(regmap, reg, raw) ||
  125. regmap_update_bits(regmap, reg - 1, 0x3, raw >> 8) ? -EIO : 0;
  126. }
  127. static int intel_crc_pmic_get_policy(struct regmap *regmap,
  128. int reg, int bit, u64 *value)
  129. {
  130. int pen;
  131. if (regmap_read(regmap, reg, &pen))
  132. return -EIO;
  133. *value = pen >> 7;
  134. return 0;
  135. }
  136. static int intel_crc_pmic_update_policy(struct regmap *regmap,
  137. int reg, int bit, int enable)
  138. {
  139. int alert0;
  140. /* Update to policy enable bit requires unlocking a0lock */
  141. if (regmap_read(regmap, PMIC_A0LOCK_REG, &alert0))
  142. return -EIO;
  143. if (regmap_update_bits(regmap, PMIC_A0LOCK_REG, 0x01, 0))
  144. return -EIO;
  145. if (regmap_update_bits(regmap, reg, 0x80, enable << 7))
  146. return -EIO;
  147. /* restore alert0 */
  148. if (regmap_write(regmap, PMIC_A0LOCK_REG, alert0))
  149. return -EIO;
  150. return 0;
  151. }
  152. static struct intel_pmic_opregion_data intel_crc_pmic_opregion_data = {
  153. .get_power = intel_crc_pmic_get_power,
  154. .update_power = intel_crc_pmic_update_power,
  155. .get_raw_temp = intel_crc_pmic_get_raw_temp,
  156. .update_aux = intel_crc_pmic_update_aux,
  157. .get_policy = intel_crc_pmic_get_policy,
  158. .update_policy = intel_crc_pmic_update_policy,
  159. .power_table = power_table,
  160. .power_table_count= ARRAY_SIZE(power_table),
  161. .thermal_table = thermal_table,
  162. .thermal_table_count = ARRAY_SIZE(thermal_table),
  163. };
  164. static int intel_crc_pmic_opregion_probe(struct platform_device *pdev)
  165. {
  166. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  167. return intel_pmic_install_opregion_handler(&pdev->dev,
  168. ACPI_HANDLE(pdev->dev.parent), pmic->regmap,
  169. &intel_crc_pmic_opregion_data);
  170. }
  171. static struct platform_driver intel_crc_pmic_opregion_driver = {
  172. .probe = intel_crc_pmic_opregion_probe,
  173. .driver = {
  174. .name = "crystal_cove_pmic",
  175. },
  176. };
  177. static int __init intel_crc_pmic_opregion_driver_init(void)
  178. {
  179. return platform_driver_register(&intel_crc_pmic_opregion_driver);
  180. }
  181. device_initcall(intel_crc_pmic_opregion_driver_init);