intel_pmic_bxtwc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * intel_pmic_bxtwc.c - Intel BXT WhiskeyCove PMIC operation region driver
  3. *
  4. * Copyright (C) 2015 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 WHISKEY_COVE_ALRT_HIGH_BIT_MASK 0x0F
  22. #define WHISKEY_COVE_ADC_HIGH_BIT(x) (((x & 0x0F) << 8))
  23. #define WHISKEY_COVE_ADC_CURSRC(x) (((x & 0xF0) >> 4))
  24. #define VR_MODE_DISABLED 0
  25. #define VR_MODE_AUTO BIT(0)
  26. #define VR_MODE_NORMAL BIT(1)
  27. #define VR_MODE_SWITCH BIT(2)
  28. #define VR_MODE_ECO (BIT(0)|BIT(1))
  29. #define VSWITCH2_OUTPUT BIT(5)
  30. #define VSWITCH1_OUTPUT BIT(4)
  31. #define VUSBPHY_CHARGE BIT(1)
  32. static struct pmic_table power_table[] = {
  33. {
  34. .address = 0x0,
  35. .reg = 0x63,
  36. .bit = VR_MODE_AUTO,
  37. }, /* VDD1 -> VDD1CNT */
  38. {
  39. .address = 0x04,
  40. .reg = 0x65,
  41. .bit = VR_MODE_AUTO,
  42. }, /* VDD2 -> VDD2CNT */
  43. {
  44. .address = 0x08,
  45. .reg = 0x67,
  46. .bit = VR_MODE_AUTO,
  47. }, /* VDD3 -> VDD3CNT */
  48. {
  49. .address = 0x0c,
  50. .reg = 0x6d,
  51. .bit = VR_MODE_AUTO,
  52. }, /* VLFX -> VFLEXCNT */
  53. {
  54. .address = 0x10,
  55. .reg = 0x6f,
  56. .bit = VR_MODE_NORMAL,
  57. }, /* VP1A -> VPROG1ACNT */
  58. {
  59. .address = 0x14,
  60. .reg = 0x70,
  61. .bit = VR_MODE_NORMAL,
  62. }, /* VP1B -> VPROG1BCNT */
  63. {
  64. .address = 0x18,
  65. .reg = 0x71,
  66. .bit = VR_MODE_NORMAL,
  67. }, /* VP1C -> VPROG1CCNT */
  68. {
  69. .address = 0x1c,
  70. .reg = 0x72,
  71. .bit = VR_MODE_NORMAL,
  72. }, /* VP1D -> VPROG1DCNT */
  73. {
  74. .address = 0x20,
  75. .reg = 0x73,
  76. .bit = VR_MODE_NORMAL,
  77. }, /* VP2A -> VPROG2ACNT */
  78. {
  79. .address = 0x24,
  80. .reg = 0x74,
  81. .bit = VR_MODE_NORMAL,
  82. }, /* VP2B -> VPROG2BCNT */
  83. {
  84. .address = 0x28,
  85. .reg = 0x75,
  86. .bit = VR_MODE_NORMAL,
  87. }, /* VP2C -> VPROG2CCNT */
  88. {
  89. .address = 0x2c,
  90. .reg = 0x76,
  91. .bit = VR_MODE_NORMAL,
  92. }, /* VP3A -> VPROG3ACNT */
  93. {
  94. .address = 0x30,
  95. .reg = 0x77,
  96. .bit = VR_MODE_NORMAL,
  97. }, /* VP3B -> VPROG3BCNT */
  98. {
  99. .address = 0x34,
  100. .reg = 0x78,
  101. .bit = VSWITCH2_OUTPUT,
  102. }, /* VSW2 -> VLD0CNT Bit 5*/
  103. {
  104. .address = 0x38,
  105. .reg = 0x78,
  106. .bit = VSWITCH1_OUTPUT,
  107. }, /* VSW1 -> VLD0CNT Bit 4 */
  108. {
  109. .address = 0x3c,
  110. .reg = 0x78,
  111. .bit = VUSBPHY_CHARGE,
  112. }, /* VUPY -> VLDOCNT Bit 1 */
  113. {
  114. .address = 0x40,
  115. .reg = 0x7b,
  116. .bit = VR_MODE_NORMAL,
  117. }, /* VRSO -> VREFSOCCNT*/
  118. {
  119. .address = 0x44,
  120. .reg = 0xA0,
  121. .bit = VR_MODE_NORMAL,
  122. }, /* VP1E -> VPROG1ECNT */
  123. {
  124. .address = 0x48,
  125. .reg = 0xA1,
  126. .bit = VR_MODE_NORMAL,
  127. }, /* VP1F -> VPROG1FCNT */
  128. {
  129. .address = 0x4c,
  130. .reg = 0xA2,
  131. .bit = VR_MODE_NORMAL,
  132. }, /* VP2D -> VPROG2DCNT */
  133. {
  134. .address = 0x50,
  135. .reg = 0xA3,
  136. .bit = VR_MODE_NORMAL,
  137. }, /* VP4A -> VPROG4ACNT */
  138. {
  139. .address = 0x54,
  140. .reg = 0xA4,
  141. .bit = VR_MODE_NORMAL,
  142. }, /* VP4B -> VPROG4BCNT */
  143. {
  144. .address = 0x58,
  145. .reg = 0xA5,
  146. .bit = VR_MODE_NORMAL,
  147. }, /* VP4C -> VPROG4CCNT */
  148. {
  149. .address = 0x5c,
  150. .reg = 0xA6,
  151. .bit = VR_MODE_NORMAL,
  152. }, /* VP4D -> VPROG4DCNT */
  153. {
  154. .address = 0x60,
  155. .reg = 0xA7,
  156. .bit = VR_MODE_NORMAL,
  157. }, /* VP5A -> VPROG5ACNT */
  158. {
  159. .address = 0x64,
  160. .reg = 0xA8,
  161. .bit = VR_MODE_NORMAL,
  162. }, /* VP5B -> VPROG5BCNT */
  163. {
  164. .address = 0x68,
  165. .reg = 0xA9,
  166. .bit = VR_MODE_NORMAL,
  167. }, /* VP6A -> VPROG6ACNT */
  168. {
  169. .address = 0x6c,
  170. .reg = 0xAA,
  171. .bit = VR_MODE_NORMAL,
  172. }, /* VP6B -> VPROG6BCNT */
  173. {
  174. .address = 0x70,
  175. .reg = 0x36,
  176. .bit = BIT(2),
  177. }, /* SDWN_N -> MODEMCTRL Bit 2 */
  178. {
  179. .address = 0x74,
  180. .reg = 0x36,
  181. .bit = BIT(0),
  182. } /* MOFF -> MODEMCTRL Bit 0 */
  183. };
  184. static struct pmic_table thermal_table[] = {
  185. {
  186. .address = 0x00,
  187. .reg = 0x4F39
  188. },
  189. {
  190. .address = 0x04,
  191. .reg = 0x4F24
  192. },
  193. {
  194. .address = 0x08,
  195. .reg = 0x4F26
  196. },
  197. {
  198. .address = 0x0c,
  199. .reg = 0x4F3B
  200. },
  201. {
  202. .address = 0x10,
  203. .reg = 0x4F28
  204. },
  205. {
  206. .address = 0x14,
  207. .reg = 0x4F2A
  208. },
  209. {
  210. .address = 0x18,
  211. .reg = 0x4F3D
  212. },
  213. {
  214. .address = 0x1c,
  215. .reg = 0x4F2C
  216. },
  217. {
  218. .address = 0x20,
  219. .reg = 0x4F2E
  220. },
  221. {
  222. .address = 0x24,
  223. .reg = 0x4F3F
  224. },
  225. {
  226. .address = 0x28,
  227. .reg = 0x4F30
  228. },
  229. {
  230. .address = 0x30,
  231. .reg = 0x4F41
  232. },
  233. {
  234. .address = 0x34,
  235. .reg = 0x4F32
  236. },
  237. {
  238. .address = 0x3c,
  239. .reg = 0x4F43
  240. },
  241. {
  242. .address = 0x40,
  243. .reg = 0x4F34
  244. },
  245. {
  246. .address = 0x48,
  247. .reg = 0x4F6A,
  248. .bit = 0,
  249. },
  250. {
  251. .address = 0x4C,
  252. .reg = 0x4F6A,
  253. .bit = 1
  254. },
  255. {
  256. .address = 0x50,
  257. .reg = 0x4F6A,
  258. .bit = 2
  259. },
  260. {
  261. .address = 0x54,
  262. .reg = 0x4F6A,
  263. .bit = 4
  264. },
  265. {
  266. .address = 0x58,
  267. .reg = 0x4F6A,
  268. .bit = 5
  269. },
  270. {
  271. .address = 0x5C,
  272. .reg = 0x4F6A,
  273. .bit = 3
  274. },
  275. };
  276. static int intel_bxtwc_pmic_get_power(struct regmap *regmap, int reg,
  277. int bit, u64 *value)
  278. {
  279. int data;
  280. if (regmap_read(regmap, reg, &data))
  281. return -EIO;
  282. *value = (data & bit) ? 1 : 0;
  283. return 0;
  284. }
  285. static int intel_bxtwc_pmic_update_power(struct regmap *regmap, int reg,
  286. int bit, bool on)
  287. {
  288. u8 val, mask = bit;
  289. if (on)
  290. val = 0xFF;
  291. else
  292. val = 0x0;
  293. return regmap_update_bits(regmap, reg, mask, val);
  294. }
  295. static int intel_bxtwc_pmic_get_raw_temp(struct regmap *regmap, int reg)
  296. {
  297. unsigned int val, adc_val, reg_val;
  298. u8 temp_l, temp_h, cursrc;
  299. unsigned long rlsb;
  300. static const unsigned long rlsb_array[] = {
  301. 0, 260420, 130210, 65100, 32550, 16280,
  302. 8140, 4070, 2030, 0, 260420, 130210 };
  303. if (regmap_read(regmap, reg, &val))
  304. return -EIO;
  305. temp_l = (u8) val;
  306. if (regmap_read(regmap, (reg - 1), &val))
  307. return -EIO;
  308. temp_h = (u8) val;
  309. reg_val = temp_l | WHISKEY_COVE_ADC_HIGH_BIT(temp_h);
  310. cursrc = WHISKEY_COVE_ADC_CURSRC(temp_h);
  311. rlsb = rlsb_array[cursrc];
  312. adc_val = reg_val * rlsb / 1000;
  313. return adc_val;
  314. }
  315. static int
  316. intel_bxtwc_pmic_update_aux(struct regmap *regmap, int reg, int raw)
  317. {
  318. u32 bsr_num;
  319. u16 resi_val, count = 0, thrsh = 0;
  320. u8 alrt_h, alrt_l, cursel = 0;
  321. bsr_num = raw;
  322. bsr_num /= (1 << 5);
  323. count = fls(bsr_num) - 1;
  324. cursel = clamp_t(s8, (count - 7), 0, 7);
  325. thrsh = raw / (1 << (4 + cursel));
  326. resi_val = (cursel << 9) | thrsh;
  327. alrt_h = (resi_val >> 8) & WHISKEY_COVE_ALRT_HIGH_BIT_MASK;
  328. if (regmap_update_bits(regmap,
  329. reg - 1,
  330. WHISKEY_COVE_ALRT_HIGH_BIT_MASK,
  331. alrt_h))
  332. return -EIO;
  333. alrt_l = (u8)resi_val;
  334. return regmap_write(regmap, reg, alrt_l);
  335. }
  336. static int
  337. intel_bxtwc_pmic_get_policy(struct regmap *regmap, int reg, int bit, u64 *value)
  338. {
  339. u8 mask = BIT(bit);
  340. unsigned int val;
  341. if (regmap_read(regmap, reg, &val))
  342. return -EIO;
  343. *value = (val & mask) >> bit;
  344. return 0;
  345. }
  346. static int
  347. intel_bxtwc_pmic_update_policy(struct regmap *regmap,
  348. int reg, int bit, int enable)
  349. {
  350. u8 mask = BIT(bit), val = enable << bit;
  351. return regmap_update_bits(regmap, reg, mask, val);
  352. }
  353. static struct intel_pmic_opregion_data intel_bxtwc_pmic_opregion_data = {
  354. .get_power = intel_bxtwc_pmic_get_power,
  355. .update_power = intel_bxtwc_pmic_update_power,
  356. .get_raw_temp = intel_bxtwc_pmic_get_raw_temp,
  357. .update_aux = intel_bxtwc_pmic_update_aux,
  358. .get_policy = intel_bxtwc_pmic_get_policy,
  359. .update_policy = intel_bxtwc_pmic_update_policy,
  360. .power_table = power_table,
  361. .power_table_count = ARRAY_SIZE(power_table),
  362. .thermal_table = thermal_table,
  363. .thermal_table_count = ARRAY_SIZE(thermal_table),
  364. };
  365. static int intel_bxtwc_pmic_opregion_probe(struct platform_device *pdev)
  366. {
  367. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  368. return intel_pmic_install_opregion_handler(&pdev->dev,
  369. ACPI_HANDLE(pdev->dev.parent),
  370. pmic->regmap,
  371. &intel_bxtwc_pmic_opregion_data);
  372. }
  373. static struct platform_device_id bxt_wc_opregion_id_table[] = {
  374. { .name = "bxt_wcove_region" },
  375. {},
  376. };
  377. static struct platform_driver intel_bxtwc_pmic_opregion_driver = {
  378. .probe = intel_bxtwc_pmic_opregion_probe,
  379. .driver = {
  380. .name = "bxt_whiskey_cove_pmic",
  381. },
  382. .id_table = bxt_wc_opregion_id_table,
  383. };
  384. static int __init intel_bxtwc_pmic_opregion_driver_init(void)
  385. {
  386. return platform_driver_register(&intel_bxtwc_pmic_opregion_driver);
  387. }
  388. device_initcall(intel_bxtwc_pmic_opregion_driver_init);