m_adcproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/msm_adc.h>
  14. #define KELVINMIL_DEGMIL 273160
  15. static const struct adc_map_pt adcmap_batttherm[] = {
  16. {2020, -30},
  17. {1923, -20},
  18. {1796, -10},
  19. {1640, 0},
  20. {1459, 10},
  21. {1260, 20},
  22. {1159, 25},
  23. {1059, 30},
  24. {871, 40},
  25. {706, 50},
  26. {567, 60},
  27. {453, 70},
  28. {364, 80}
  29. };
  30. static const struct adc_map_pt adcmap_msmtherm[] = {
  31. {2150, -30},
  32. {2107, -20},
  33. {2037, -10},
  34. {1929, 0},
  35. {1776, 10},
  36. {1579, 20},
  37. {1467, 25},
  38. {1349, 30},
  39. {1108, 40},
  40. {878, 50},
  41. {677, 60},
  42. {513, 70},
  43. {385, 80},
  44. {287, 90},
  45. {215, 100},
  46. {186, 110},
  47. {107, 120}
  48. };
  49. static const struct adc_map_pt adcmap_ntcg104ef104fb[] = {
  50. {696483, -40960},
  51. {649148, -39936},
  52. {605368, -38912},
  53. {564809, -37888},
  54. {527215, -36864},
  55. {492322, -35840},
  56. {460007, -34816},
  57. {429982, -33792},
  58. {402099, -32768},
  59. {376192, -31744},
  60. {352075, -30720},
  61. {329714, -29696},
  62. {308876, -28672},
  63. {289480, -27648},
  64. {271417, -26624},
  65. {254574, -25600},
  66. {238903, -24576},
  67. {224276, -23552},
  68. {210631, -22528},
  69. {197896, -21504},
  70. {186007, -20480},
  71. {174899, -19456},
  72. {164521, -18432},
  73. {154818, -17408},
  74. {145744, -16384},
  75. {137265, -15360},
  76. {129307, -14336},
  77. {121866, -13312},
  78. {114896, -12288},
  79. {108365, -11264},
  80. {102252, -10240},
  81. {96499, -9216},
  82. {91111, -8192},
  83. {86055, -7168},
  84. {81308, -6144},
  85. {76857, -5120},
  86. {72660, -4096},
  87. {68722, -3072},
  88. {65020, -2048},
  89. {61538, -1024},
  90. {58261, 0},
  91. {55177, 1024},
  92. {52274, 2048},
  93. {49538, 3072},
  94. {46962, 4096},
  95. {44531, 5120},
  96. {42243, 6144},
  97. {40083, 7168},
  98. {38045, 8192},
  99. {36122, 9216},
  100. {34308, 10240},
  101. {32592, 11264},
  102. {30972, 12288},
  103. {29442, 13312},
  104. {27995, 14336},
  105. {26624, 15360},
  106. {25333, 16384},
  107. {24109, 17408},
  108. {22951, 18432},
  109. {21854, 19456},
  110. {20807, 20480},
  111. {19831, 21504},
  112. {18899, 22528},
  113. {18016, 23552},
  114. {17178, 24576},
  115. {16384, 25600},
  116. {15631, 26624},
  117. {14916, 27648},
  118. {14237, 28672},
  119. {13593, 29696},
  120. {12976, 30720},
  121. {12400, 31744},
  122. {11848, 32768},
  123. {11324, 33792},
  124. {10825, 34816},
  125. {10354, 35840},
  126. {9900, 36864},
  127. {9471, 37888},
  128. {9062, 38912},
  129. {8674, 39936},
  130. {8306, 40960},
  131. {7951, 41984},
  132. {7616, 43008},
  133. {7296, 44032},
  134. {6991, 45056},
  135. {6701, 46080},
  136. {6424, 47104},
  137. {6160, 48128},
  138. {5908, 49152},
  139. {5667, 50176},
  140. {5439, 51200},
  141. {5219, 52224},
  142. {5010, 53248},
  143. {4810, 54272},
  144. {4619, 55296},
  145. {4440, 56320},
  146. {4263, 57344},
  147. {4097, 58368},
  148. {3938, 59392},
  149. {3785, 60416},
  150. {3637, 61440},
  151. {3501, 62464},
  152. {3368, 63488},
  153. {3240, 64512},
  154. {3118, 65536},
  155. {2998, 66560},
  156. {2889, 67584},
  157. {2782, 68608},
  158. {2680, 69632},
  159. {2581, 70656},
  160. {2490, 71680},
  161. {2397, 72704},
  162. {2310, 73728},
  163. {2227, 74752},
  164. {2147, 75776},
  165. {2064, 76800},
  166. {1998, 77824},
  167. {1927, 78848},
  168. {1860, 79872},
  169. {1795, 80896},
  170. {1736, 81920},
  171. {1673, 82944},
  172. {1615, 83968},
  173. {1560, 84992},
  174. {1507, 86016},
  175. {1456, 87040},
  176. {1407, 88064},
  177. {1360, 89088},
  178. {1314, 90112},
  179. {1271, 91136},
  180. {1228, 92160},
  181. {1189, 93184},
  182. {1150, 94208},
  183. {1112, 95232},
  184. {1076, 96256},
  185. {1042, 97280},
  186. {1008, 98304},
  187. {976, 99328},
  188. {945, 100352},
  189. {915, 101376},
  190. {886, 102400},
  191. {859, 103424},
  192. {832, 104448},
  193. {807, 105472},
  194. {782, 106496},
  195. {756, 107520},
  196. {735, 108544},
  197. {712, 109568},
  198. {691, 110592},
  199. {670, 111616},
  200. {650, 112640},
  201. {631, 113664},
  202. {612, 114688},
  203. {594, 115712},
  204. {577, 116736},
  205. {560, 117760},
  206. {544, 118784},
  207. {528, 119808},
  208. {513, 120832},
  209. {498, 121856},
  210. {483, 122880},
  211. {470, 123904},
  212. {457, 124928},
  213. {444, 125952},
  214. {431, 126976},
  215. {419, 128000}
  216. };
  217. static int32_t
  218. adc_map_linear(const struct adc_map_pt *pts,
  219. uint32_t tablesize, int32_t input, int64_t *output)
  220. {
  221. bool descending = 1;
  222. uint32_t i = 0;
  223. if ((pts == NULL) || (output == NULL))
  224. return -EINVAL;
  225. /* Check if table is descending or ascending */
  226. if (tablesize > 1) {
  227. if (pts[0].x < pts[1].x)
  228. descending = 0;
  229. }
  230. while (i < tablesize) {
  231. if ((descending == 1) && (pts[i].x < input)) {
  232. /* table entry is less than measured
  233. value and table is descending, stop */
  234. break;
  235. } else if ((descending == 0) &&
  236. (pts[i].x > input)) {
  237. /* table entry is greater than measured
  238. value and table is ascending, stop */
  239. break;
  240. } else
  241. i++;
  242. }
  243. if (i == 0)
  244. *output = pts[0].y;
  245. else if (i == tablesize)
  246. *output = pts[tablesize-1].y;
  247. else {
  248. /* result is between search_index and search_index-1 */
  249. /* interpolate linearly */
  250. *output = (((int32_t) ((pts[i].y - pts[i-1].y)*
  251. (input - pts[i-1].x))/
  252. (pts[i].x - pts[i-1].x))+
  253. pts[i-1].y);
  254. }
  255. return 0;
  256. }
  257. int32_t scale_default(int32_t adc_code,
  258. const struct adc_properties *adc_properties,
  259. const struct chan_properties *chan_properties,
  260. struct adc_chan_result *adc_chan_result)
  261. {
  262. bool negative_rawfromoffset = 0;
  263. int32_t rawfromoffset = adc_code - chan_properties->adc_graph->offset;
  264. if (!chan_properties->gain_numerator ||
  265. !chan_properties->gain_denominator)
  266. return -EINVAL;
  267. adc_chan_result->adc_code = adc_code;
  268. if (rawfromoffset < 0) {
  269. if (adc_properties->bipolar) {
  270. rawfromoffset = (rawfromoffset ^ -1) + 1;
  271. negative_rawfromoffset = 1;
  272. } else
  273. rawfromoffset = 0;
  274. }
  275. if (rawfromoffset >= 1 << adc_properties->bitresolution)
  276. rawfromoffset = (1 << adc_properties->bitresolution) - 1;
  277. adc_chan_result->measurement = (int64_t)rawfromoffset*
  278. chan_properties->adc_graph->dx*
  279. chan_properties->gain_denominator;
  280. /* do_div only perform positive integer division! */
  281. do_div(adc_chan_result->measurement, chan_properties->adc_graph->dy*
  282. chan_properties->gain_numerator);
  283. if (negative_rawfromoffset)
  284. adc_chan_result->measurement =
  285. (adc_chan_result->measurement ^ -1) + 1;
  286. /* Note: adc_chan_result->measurement is in the unit of
  287. * adc_properties.adc_reference. For generic channel processing,
  288. * channel measurement is a scale/ratio relative to the adc
  289. * reference input */
  290. adc_chan_result->physical = (int32_t) adc_chan_result->measurement;
  291. return 0;
  292. }
  293. int32_t scale_batt_therm(int32_t adc_code,
  294. const struct adc_properties *adc_properties,
  295. const struct chan_properties *chan_properties,
  296. struct adc_chan_result *adc_chan_result)
  297. {
  298. scale_default(adc_code, adc_properties, chan_properties,
  299. adc_chan_result);
  300. /* convert mV ---> degC using the table */
  301. return adc_map_linear(
  302. adcmap_batttherm,
  303. sizeof(adcmap_batttherm)/sizeof(adcmap_batttherm[0]),
  304. adc_chan_result->physical,
  305. &adc_chan_result->physical);
  306. }
  307. int32_t scale_msm_therm(int32_t adc_code,
  308. const struct adc_properties *adc_properties,
  309. const struct chan_properties *chan_properties,
  310. struct adc_chan_result *adc_chan_result)
  311. {
  312. scale_default(adc_code, adc_properties, chan_properties,
  313. adc_chan_result);
  314. /* convert mV ---> degC using the table */
  315. return adc_map_linear(
  316. adcmap_msmtherm,
  317. sizeof(adcmap_msmtherm)/sizeof(adcmap_msmtherm[0]),
  318. adc_chan_result->physical,
  319. &adc_chan_result->physical);
  320. }
  321. int32_t scale_pmic_therm(int32_t adc_code,
  322. const struct adc_properties *adc_properties,
  323. const struct chan_properties *chan_properties,
  324. struct adc_chan_result *adc_chan_result)
  325. {
  326. /* 2mV/K */
  327. int32_t rawfromoffset = adc_code - chan_properties->adc_graph->offset;
  328. if (!chan_properties->gain_numerator ||
  329. !chan_properties->gain_denominator)
  330. return -EINVAL;
  331. adc_chan_result->adc_code = adc_code;
  332. if (rawfromoffset > 0) {
  333. if (rawfromoffset >= 1 << adc_properties->bitresolution)
  334. rawfromoffset = (1 << adc_properties->bitresolution)
  335. - 1;
  336. adc_chan_result->measurement = (int64_t)rawfromoffset*
  337. chan_properties->adc_graph->dx*
  338. chan_properties->gain_denominator*1000;
  339. do_div(adc_chan_result->measurement,
  340. chan_properties->adc_graph->dy*
  341. chan_properties->gain_numerator*2);
  342. } else {
  343. adc_chan_result->measurement = 0;
  344. }
  345. /* Note: adc_chan_result->measurement is in the unit of
  346. adc_properties.adc_reference */
  347. adc_chan_result->physical = (int32_t)adc_chan_result->measurement;
  348. /* Change to .001 deg C */
  349. adc_chan_result->physical -= KELVINMIL_DEGMIL;
  350. adc_chan_result->measurement <<= 1;
  351. return 0;
  352. }
  353. /* Scales the ADC code to 0.001 degrees C using the map
  354. * table for the XO thermistor.
  355. */
  356. int32_t tdkntcgtherm(int32_t adc_code,
  357. const struct adc_properties *adc_properties,
  358. const struct chan_properties *chan_properties,
  359. struct adc_chan_result *adc_chan_result)
  360. {
  361. int32_t offset = chan_properties->adc_graph->offset,
  362. dy = chan_properties->adc_graph->dy,
  363. dx = chan_properties->adc_graph->dx,
  364. fullscale_calibrated_adc_code;
  365. uint32_t rt_r25;
  366. uint32_t num1, num2, denom;
  367. adc_chan_result->adc_code = adc_code;
  368. fullscale_calibrated_adc_code = dy + offset;
  369. /* The above is a short cut in math that would reduce a lot of
  370. computation whereas the below expression
  371. (adc_properties->adc_reference*dy+dx*offset+(dx>>1))/dx
  372. is a more generic formula when the 2 reference voltages are
  373. different than 0 and full scale voltage. */
  374. if ((dy == 0) || (dx == 0) ||
  375. (offset >= fullscale_calibrated_adc_code)) {
  376. return -EINVAL;
  377. } else {
  378. if (adc_code >= fullscale_calibrated_adc_code) {
  379. rt_r25 = (uint32_t)-1;
  380. } else if (adc_code <= offset) {
  381. rt_r25 = 0;
  382. } else {
  383. /* The formula used is (adc_code of current reading - offset)/
  384. * (the calibrated fullscale adc code - adc_code of current reading).
  385. * For this channel, at this time, chan_properties->gain_numerator =
  386. * chan_properties->gain_denominator = 1, so no need to incorporate
  387. * into the formula even though we could and multiply/divide by 1
  388. * which yields the same result but expensive on computation. */
  389. num1 = (adc_code - offset) << 14;
  390. num2 = (fullscale_calibrated_adc_code - adc_code) >> 1;
  391. denom = fullscale_calibrated_adc_code - adc_code;
  392. if ((int)denom <= 0)
  393. rt_r25 = 0x7FFFFFFF;
  394. else
  395. rt_r25 = (num1 + num2) / denom;
  396. }
  397. if (rt_r25 > 0x7FFFFFFF)
  398. rt_r25 = 0x7FFFFFFF;
  399. adc_map_linear(adcmap_ntcg104ef104fb,
  400. sizeof(adcmap_ntcg104ef104fb)/sizeof(adcmap_ntcg104ef104fb[0]),
  401. (int32_t)rt_r25, &adc_chan_result->physical);
  402. }
  403. return 0;
  404. }
  405. int32_t scale_xtern_chgr_cur(int32_t adc_code,
  406. const struct adc_properties *adc_properties,
  407. const struct chan_properties *chan_properties,
  408. struct adc_chan_result *adc_chan_result)
  409. {
  410. int32_t rawfromoffset = adc_code - chan_properties->adc_graph->offset;
  411. if (!chan_properties->gain_numerator ||
  412. !chan_properties->gain_denominator)
  413. return -EINVAL;
  414. adc_chan_result->adc_code = adc_code;
  415. if (rawfromoffset > 0) {
  416. if (rawfromoffset >= 1 << adc_properties->bitresolution)
  417. rawfromoffset = (1 << adc_properties->bitresolution)
  418. - 1;
  419. adc_chan_result->measurement = ((int64_t)rawfromoffset * 5)*
  420. chan_properties->adc_graph->dx*
  421. chan_properties->gain_denominator;
  422. do_div(adc_chan_result->measurement,
  423. chan_properties->adc_graph->dy*
  424. chan_properties->gain_numerator);
  425. } else {
  426. adc_chan_result->measurement = 0;
  427. }
  428. adc_chan_result->physical = (int32_t) adc_chan_result->measurement;
  429. return 0;
  430. }