tsens-v0_1.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/nvmem-consumer.h>
  7. #include <linux/platform_device.h>
  8. #include "tsens.h"
  9. /* ----- SROT ------ */
  10. #define SROT_CTRL_OFF 0x0000
  11. /* ----- TM ------ */
  12. #define TM_INT_EN_OFF 0x0000
  13. #define TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF 0x0004
  14. #define TM_Sn_STATUS_OFF 0x0030
  15. #define TM_TRDY_OFF 0x005c
  16. /* extra data for 8974 */
  17. #define BKP_SEL 0x3
  18. #define BKP_REDUN_SEL 0xe0000000
  19. #define BIT_APPEND 0x3
  20. static struct tsens_legacy_calibration_format tsens_8916_nvmem = {
  21. .base_len = 7,
  22. .base_shift = 3,
  23. .sp_len = 5,
  24. .mode = { 0, 29, 1 },
  25. .invalid = { 0, 31, 1 },
  26. .base = { { 0, 0 }, { 1, 25 } },
  27. .sp = {
  28. { { 0, 7 }, { 0, 12 } },
  29. { { 0, 17 }, { 0, 22 } },
  30. { { 0, 27 }, { 1, 0 } },
  31. { { 1, 5 }, { 1, 10 } },
  32. { { 1, 15 }, { 1, 20 } },
  33. },
  34. };
  35. static struct tsens_legacy_calibration_format tsens_8974_nvmem = {
  36. .base_len = 8,
  37. .base_shift = 2,
  38. .sp_len = 6,
  39. .mode = { 1, 30 },
  40. .invalid = { 3, 30 },
  41. .base = { { 0, 0 }, { 2, 12 } },
  42. .sp = {
  43. { { 0, 8 }, { 2, 20 } },
  44. { { 0, 14 }, { 2, 26 } },
  45. { { 0, 20 }, { 3, 0 } },
  46. { { 0, 26 }, { 3, 6 } },
  47. { { 1, 0 }, { 3, 12 } },
  48. { { 1, 6 }, { 3, 18 } },
  49. { { 1, 12 }, { 3, 24 } },
  50. { { 1, 18 }, { 4, 0 } },
  51. { { 1, 24 }, { 4, 6 } },
  52. { { 2, 0 }, { 4, 12 } },
  53. { { 2, 6 }, { 4, 18 } },
  54. },
  55. };
  56. static struct tsens_legacy_calibration_format tsens_8974_backup_nvmem = {
  57. .base_len = 8,
  58. .base_shift = 2,
  59. .sp_len = 6,
  60. .mode = { 4, 30, 1 },
  61. .invalid = { 5, 30, 1 },
  62. .base = { { 0, 0 }, { 2, 18 } },
  63. .sp = {
  64. { { 0, 8 }, { 2, 26 } },
  65. { { 0, 14 }, { 3, 0 } },
  66. { { 0, 20 }, { 3, 6 } },
  67. { { 0, 26 }, { 3, 12 } },
  68. { { 1, 0 }, { 3, 18 } },
  69. { { 1, 6 }, { 3, 24, 1 } },
  70. { { 1, 12 }, { 4, 0, 1 } },
  71. { { 1, 18 }, { 4, 6, 1 } },
  72. { { 2, 0 }, { 4, 12, 1 } },
  73. { { 2, 6 }, { 4, 18, 1 } },
  74. { { 2, 12 }, { 4, 24, 1 } },
  75. },
  76. };
  77. static int calibrate_8916(struct tsens_priv *priv)
  78. {
  79. u32 p1[5], p2[5];
  80. u32 *qfprom_cdata, *qfprom_csel;
  81. int mode, ret;
  82. ret = tsens_calibrate_nvmem(priv, 3);
  83. if (!ret)
  84. return 0;
  85. qfprom_cdata = (u32 *)qfprom_read(priv->dev, "calib");
  86. if (IS_ERR(qfprom_cdata))
  87. return PTR_ERR(qfprom_cdata);
  88. qfprom_csel = (u32 *)qfprom_read(priv->dev, "calib_sel");
  89. if (IS_ERR(qfprom_csel)) {
  90. kfree(qfprom_cdata);
  91. return PTR_ERR(qfprom_csel);
  92. }
  93. mode = tsens_read_calibration_legacy(priv, &tsens_8916_nvmem,
  94. p1, p2,
  95. qfprom_cdata, qfprom_csel);
  96. compute_intercept_slope(priv, p1, p2, mode);
  97. kfree(qfprom_cdata);
  98. kfree(qfprom_csel);
  99. return 0;
  100. }
  101. static void fixup_8974_points(int mode, u32 *p1, u32 *p2)
  102. {
  103. int i;
  104. if (mode == NO_PT_CALIB) {
  105. p1[0] += 2;
  106. p1[1] += 9;
  107. p1[2] += 3;
  108. p1[3] += 9;
  109. p1[4] += 5;
  110. p1[5] += 9;
  111. p1[6] += 7;
  112. p1[7] += 10;
  113. p1[8] += 8;
  114. p1[9] += 9;
  115. p1[10] += 8;
  116. } else {
  117. for (i = 0; i < 11; i++) {
  118. /*
  119. * ONE_PT_CALIB requires using addition here instead of
  120. * using OR operation.
  121. */
  122. p1[i] += BIT_APPEND;
  123. p2[i] += BIT_APPEND;
  124. }
  125. }
  126. }
  127. static int calibrate_8974_nvmem(struct tsens_priv *priv)
  128. {
  129. u32 p1[11], p2[11];
  130. u32 backup;
  131. int ret, mode;
  132. ret = nvmem_cell_read_variable_le_u32(priv->dev, "use_backup", &backup);
  133. if (ret == -ENOENT)
  134. dev_warn(priv->dev, "Please migrate to separate nvmem cells for calibration data\n");
  135. if (ret < 0)
  136. return ret;
  137. mode = tsens_read_calibration(priv, 2, p1, p2, backup == BKP_SEL);
  138. if (mode < 0)
  139. return mode;
  140. fixup_8974_points(mode, p1, p2);
  141. compute_intercept_slope(priv, p1, p2, mode);
  142. return 0;
  143. }
  144. static int calibrate_8974(struct tsens_priv *priv)
  145. {
  146. u32 p1[11], p2[11];
  147. u32 *calib, *bkp;
  148. u32 calib_redun_sel;
  149. int mode, ret;
  150. ret = calibrate_8974_nvmem(priv);
  151. if (ret == 0)
  152. return 0;
  153. calib = (u32 *)qfprom_read(priv->dev, "calib");
  154. if (IS_ERR(calib))
  155. return PTR_ERR(calib);
  156. bkp = (u32 *)qfprom_read(priv->dev, "calib_backup");
  157. if (IS_ERR(bkp)) {
  158. kfree(calib);
  159. return PTR_ERR(bkp);
  160. }
  161. calib_redun_sel = FIELD_GET(BKP_REDUN_SEL, bkp[1]);
  162. if (calib_redun_sel == BKP_SEL)
  163. mode = tsens_read_calibration_legacy(priv, &tsens_8974_backup_nvmem,
  164. p1, p2,
  165. bkp, calib);
  166. else
  167. mode = tsens_read_calibration_legacy(priv, &tsens_8974_nvmem,
  168. p1, p2,
  169. calib, NULL);
  170. fixup_8974_points(mode, p1, p2);
  171. compute_intercept_slope(priv, p1, p2, mode);
  172. kfree(calib);
  173. kfree(bkp);
  174. return 0;
  175. }
  176. static int __init init_8226(struct tsens_priv *priv)
  177. {
  178. priv->sensor[0].slope = 2901;
  179. priv->sensor[1].slope = 2846;
  180. priv->sensor[2].slope = 3038;
  181. priv->sensor[3].slope = 2955;
  182. priv->sensor[4].slope = 2901;
  183. priv->sensor[5].slope = 2846;
  184. return init_common(priv);
  185. }
  186. static int __init init_8909(struct tsens_priv *priv)
  187. {
  188. int i;
  189. for (i = 0; i < priv->num_sensors; ++i)
  190. priv->sensor[i].slope = 3000;
  191. priv->sensor[0].p1_calib_offset = 0;
  192. priv->sensor[0].p2_calib_offset = 0;
  193. priv->sensor[1].p1_calib_offset = -10;
  194. priv->sensor[1].p2_calib_offset = -6;
  195. priv->sensor[2].p1_calib_offset = 0;
  196. priv->sensor[2].p2_calib_offset = 0;
  197. priv->sensor[3].p1_calib_offset = -9;
  198. priv->sensor[3].p2_calib_offset = -9;
  199. priv->sensor[4].p1_calib_offset = -8;
  200. priv->sensor[4].p2_calib_offset = -10;
  201. return init_common(priv);
  202. }
  203. static int __init init_8939(struct tsens_priv *priv) {
  204. priv->sensor[0].slope = 2911;
  205. priv->sensor[1].slope = 2789;
  206. priv->sensor[2].slope = 2906;
  207. priv->sensor[3].slope = 2763;
  208. priv->sensor[4].slope = 2922;
  209. priv->sensor[5].slope = 2867;
  210. priv->sensor[6].slope = 2833;
  211. priv->sensor[7].slope = 2838;
  212. priv->sensor[8].slope = 2840;
  213. /* priv->sensor[9].slope = 2852; */
  214. return init_common(priv);
  215. }
  216. static int __init init_9607(struct tsens_priv *priv)
  217. {
  218. int i;
  219. for (i = 0; i < priv->num_sensors; ++i)
  220. priv->sensor[i].slope = 3000;
  221. priv->sensor[0].p1_calib_offset = 1;
  222. priv->sensor[0].p2_calib_offset = 1;
  223. priv->sensor[1].p1_calib_offset = -4;
  224. priv->sensor[1].p2_calib_offset = -2;
  225. priv->sensor[2].p1_calib_offset = 4;
  226. priv->sensor[2].p2_calib_offset = 8;
  227. priv->sensor[3].p1_calib_offset = -3;
  228. priv->sensor[3].p2_calib_offset = -5;
  229. priv->sensor[4].p1_calib_offset = -4;
  230. priv->sensor[4].p2_calib_offset = -4;
  231. return init_common(priv);
  232. }
  233. /* v0.1: 8226, 8909, 8916, 8939, 8974, 9607 */
  234. static struct tsens_features tsens_v0_1_feat = {
  235. .ver_major = VER_0_1,
  236. .crit_int = 0,
  237. .combo_int = 0,
  238. .adc = 1,
  239. .srot_split = 1,
  240. .max_sensors = 11,
  241. .trip_min_temp = -40000,
  242. .trip_max_temp = 120000,
  243. };
  244. static const struct reg_field tsens_v0_1_regfields[MAX_REGFIELDS] = {
  245. /* ----- SROT ------ */
  246. /* No VERSION information */
  247. /* CTRL_OFFSET */
  248. [TSENS_EN] = REG_FIELD(SROT_CTRL_OFF, 0, 0),
  249. [TSENS_SW_RST] = REG_FIELD(SROT_CTRL_OFF, 1, 1),
  250. /* ----- TM ------ */
  251. /* INTERRUPT ENABLE */
  252. [INT_EN] = REG_FIELD(TM_INT_EN_OFF, 0, 0),
  253. /* UPPER/LOWER TEMPERATURE THRESHOLDS */
  254. REG_FIELD_FOR_EACH_SENSOR11(LOW_THRESH, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 0, 9),
  255. REG_FIELD_FOR_EACH_SENSOR11(UP_THRESH, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 10, 19),
  256. /* UPPER/LOWER INTERRUPTS [CLEAR/STATUS] */
  257. REG_FIELD_FOR_EACH_SENSOR11(LOW_INT_CLEAR, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 20, 20),
  258. REG_FIELD_FOR_EACH_SENSOR11(UP_INT_CLEAR, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 21, 21),
  259. /* NO CRITICAL INTERRUPT SUPPORT on v0.1 */
  260. /* Sn_STATUS */
  261. REG_FIELD_FOR_EACH_SENSOR11(LAST_TEMP, TM_Sn_STATUS_OFF, 0, 9),
  262. /* No VALID field on v0.1 */
  263. /* xxx_STATUS bits: 1 == threshold violated */
  264. REG_FIELD_FOR_EACH_SENSOR11(MIN_STATUS, TM_Sn_STATUS_OFF, 10, 10),
  265. REG_FIELD_FOR_EACH_SENSOR11(LOWER_STATUS, TM_Sn_STATUS_OFF, 11, 11),
  266. REG_FIELD_FOR_EACH_SENSOR11(UPPER_STATUS, TM_Sn_STATUS_OFF, 12, 12),
  267. /* No CRITICAL field on v0.1 */
  268. REG_FIELD_FOR_EACH_SENSOR11(MAX_STATUS, TM_Sn_STATUS_OFF, 13, 13),
  269. /* TRDY: 1=ready, 0=in progress */
  270. [TRDY] = REG_FIELD(TM_TRDY_OFF, 0, 0),
  271. };
  272. static const struct tsens_ops ops_8226 = {
  273. .init = init_8226,
  274. .calibrate = tsens_calibrate_common,
  275. .get_temp = get_temp_common,
  276. };
  277. struct tsens_plat_data data_8226 = {
  278. .num_sensors = 6,
  279. .ops = &ops_8226,
  280. .feat = &tsens_v0_1_feat,
  281. .fields = tsens_v0_1_regfields,
  282. };
  283. static const struct tsens_ops ops_8909 = {
  284. .init = init_8909,
  285. .calibrate = tsens_calibrate_common,
  286. .get_temp = get_temp_common,
  287. };
  288. struct tsens_plat_data data_8909 = {
  289. .num_sensors = 5,
  290. .ops = &ops_8909,
  291. .feat = &tsens_v0_1_feat,
  292. .fields = tsens_v0_1_regfields,
  293. };
  294. static const struct tsens_ops ops_8916 = {
  295. .init = init_common,
  296. .calibrate = calibrate_8916,
  297. .get_temp = get_temp_common,
  298. };
  299. struct tsens_plat_data data_8916 = {
  300. .num_sensors = 5,
  301. .ops = &ops_8916,
  302. .hw_ids = (unsigned int []){0, 1, 2, 4, 5 },
  303. .feat = &tsens_v0_1_feat,
  304. .fields = tsens_v0_1_regfields,
  305. };
  306. static const struct tsens_ops ops_8939 = {
  307. .init = init_8939,
  308. .calibrate = tsens_calibrate_common,
  309. .get_temp = get_temp_common,
  310. };
  311. struct tsens_plat_data data_8939 = {
  312. .num_sensors = 9,
  313. .ops = &ops_8939,
  314. .hw_ids = (unsigned int []){ 0, 1, 2, 3, 5, 6, 7, 8, 9, /* 10 */ },
  315. .feat = &tsens_v0_1_feat,
  316. .fields = tsens_v0_1_regfields,
  317. };
  318. static const struct tsens_ops ops_8974 = {
  319. .init = init_common,
  320. .calibrate = calibrate_8974,
  321. .get_temp = get_temp_common,
  322. };
  323. struct tsens_plat_data data_8974 = {
  324. .num_sensors = 11,
  325. .ops = &ops_8974,
  326. .feat = &tsens_v0_1_feat,
  327. .fields = tsens_v0_1_regfields,
  328. };
  329. static const struct tsens_ops ops_9607 = {
  330. .init = init_9607,
  331. .calibrate = tsens_calibrate_common,
  332. .get_temp = get_temp_common,
  333. };
  334. struct tsens_plat_data data_9607 = {
  335. .num_sensors = 5,
  336. .ops = &ops_9607,
  337. .feat = &tsens_v0_1_feat,
  338. .fields = tsens_v0_1_regfields,
  339. };