msm8974-tsens.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. /* Copyright (c) 2012-2013, 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. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/thermal.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.h>
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. #include <linux/msm_tsens.h>
  24. #include <linux/err.h>
  25. #include <linux/of.h>
  26. #include <mach/msm_iomap.h>
  27. #define TSENS_DRIVER_NAME "msm-tsens"
  28. /* TSENS register info */
  29. #define TSENS_UPPER_LOWER_INTERRUPT_CTRL(n) ((n) + 0x1000)
  30. #define TSENS_INTERRUPT_EN BIT(0)
  31. #define TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR(n) ((n) + 0x1004)
  32. #define TSENS_UPPER_STATUS_CLR BIT(21)
  33. #define TSENS_LOWER_STATUS_CLR BIT(20)
  34. #define TSENS_UPPER_THRESHOLD_MASK 0xffc00
  35. #define TSENS_LOWER_THRESHOLD_MASK 0x3ff
  36. #define TSENS_UPPER_THRESHOLD_SHIFT 10
  37. #define TSENS_S0_STATUS_ADDR(n) ((n) + 0x1030)
  38. #define TSENS_SN_ADDR_OFFSET 0x4
  39. #define TSENS_SN_STATUS_TEMP_MASK 0x3ff
  40. #define TSENS_SN_STATUS_LOWER_STATUS BIT(11)
  41. #define TSENS_SN_STATUS_UPPER_STATUS BIT(12)
  42. #define TSENS_STATUS_ADDR_OFFSET 2
  43. #define TSENS_TRDY_ADDR(n) ((n) + 0x105c)
  44. #define TSENS_TRDY_MASK BIT(0)
  45. #define TSENS_CTRL_ADDR(n) (n)
  46. #define TSENS_EN BIT(0)
  47. #define TSENS_SW_RST BIT(1)
  48. #define TSENS_ADC_CLK_SEL BIT(2)
  49. #define TSENS_SENSOR0_SHIFT 3
  50. #define TSENS_62_5_MS_MEAS_PERIOD 1
  51. #define TSENS_312_5_MS_MEAS_PERIOD 2
  52. #define TSENS_MEAS_PERIOD_SHIFT 18
  53. #define TSENS_SN_MIN_MAX_STATUS_CTRL(n) ((n) + 4)
  54. #define TSENS_GLOBAL_CONFIG(n) ((n) + 0x34)
  55. #define TSENS_S0_MAIN_CONFIG(n) ((n) + 0x38)
  56. #define TSENS_SN_REMOTE_CONFIG(n) ((n) + 0x3c)
  57. #define TSENS_EEPROM(n) ((n) + 0xd0)
  58. #define TSENS_EEPROM_REDUNDANCY_SEL(n) ((n) + 0x444)
  59. #define TSENS_EEPROM_BACKUP_REGION(n) ((n) + 0x440)
  60. #define TSENS_MAIN_CALIB_ADDR_RANGE 6
  61. #define TSENS_BACKUP_CALIB_ADDR_RANGE 4
  62. #define TSENS_EEPROM_8X26_1(n) ((n) + 0x1c0)
  63. #define TSENS_EEPROM_8X26_2(n) ((n) + 0x444)
  64. #define TSENS_8X26_MAIN_CALIB_ADDR_RANGE 4
  65. #define TSENS_EEPROM_8X10_1(n) ((n) + 0x1a4)
  66. #define TSENS_EEPROM_8X10_1_OFFSET 8
  67. #define TSENS_EEPROM_8X10_2(n) ((n) + 0x1a8)
  68. #define TSENS_EEPROM_8X10_SPARE_1(n) ((n) + 0xd8)
  69. #define TSENS_EEPROM_8X10_SPARE_2(n) ((n) + 0xdc)
  70. /* TSENS calibration Mask data */
  71. #define TSENS_BASE1_MASK 0xff
  72. #define TSENS0_POINT1_MASK 0x3f00
  73. #define TSENS1_POINT1_MASK 0xfc000
  74. #define TSENS2_POINT1_MASK 0x3f00000
  75. #define TSENS3_POINT1_MASK 0xfc000000
  76. #define TSENS4_POINT1_MASK 0x3f
  77. #define TSENS5_POINT1_MASK 0xfc0
  78. #define TSENS6_POINT1_MASK 0x3f000
  79. #define TSENS7_POINT1_MASK 0xfc0000
  80. #define TSENS8_POINT1_MASK 0x3f000000
  81. #define TSENS8_POINT1_MASK_BACKUP 0x3f
  82. #define TSENS9_POINT1_MASK 0x3f
  83. #define TSENS9_POINT1_MASK_BACKUP 0xfc0
  84. #define TSENS10_POINT1_MASK 0xfc0
  85. #define TSENS10_POINT1_MASK_BACKUP 0x3f000
  86. #define TSENS_CAL_SEL_0_1 0xc0000000
  87. #define TSENS_CAL_SEL_2 0x40000000
  88. #define TSENS_CAL_SEL_SHIFT 30
  89. #define TSENS_CAL_SEL_SHIFT_2 28
  90. #define TSENS_ONE_POINT_CALIB 0x1
  91. #define TSENS_ONE_POINT_CALIB_OPTION_2 0x2
  92. #define TSENS_TWO_POINT_CALIB 0x3
  93. #define TSENS0_POINT1_SHIFT 8
  94. #define TSENS1_POINT1_SHIFT 14
  95. #define TSENS2_POINT1_SHIFT 20
  96. #define TSENS3_POINT1_SHIFT 26
  97. #define TSENS5_POINT1_SHIFT 6
  98. #define TSENS6_POINT1_SHIFT 12
  99. #define TSENS7_POINT1_SHIFT 18
  100. #define TSENS8_POINT1_SHIFT 24
  101. #define TSENS9_POINT1_BACKUP_SHIFT 6
  102. #define TSENS10_POINT1_SHIFT 6
  103. #define TSENS10_POINT1_BACKUP_SHIFT 12
  104. #define TSENS_POINT2_BASE_SHIFT 12
  105. #define TSENS_POINT2_BASE_BACKUP_SHIFT 18
  106. #define TSENS0_POINT2_SHIFT 20
  107. #define TSENS0_POINT2_BACKUP_SHIFT 26
  108. #define TSENS1_POINT2_SHIFT 26
  109. #define TSENS2_POINT2_BACKUP_SHIFT 6
  110. #define TSENS3_POINT2_SHIFT 6
  111. #define TSENS3_POINT2_BACKUP_SHIFT 12
  112. #define TSENS4_POINT2_SHIFT 12
  113. #define TSENS4_POINT2_BACKUP_SHIFT 18
  114. #define TSENS5_POINT2_SHIFT 18
  115. #define TSENS5_POINT2_BACKUP_SHIFT 24
  116. #define TSENS6_POINT2_SHIFT 24
  117. #define TSENS7_POINT2_BACKUP_SHIFT 6
  118. #define TSENS8_POINT2_SHIFT 6
  119. #define TSENS8_POINT2_BACKUP_SHIFT 12
  120. #define TSENS9_POINT2_SHIFT 12
  121. #define TSENS9_POINT2_BACKUP_SHIFT 18
  122. #define TSENS10_POINT2_SHIFT 18
  123. #define TSENS10_POINT2_BACKUP_SHIFT 24
  124. #define TSENS_BASE2_MASK 0xff000
  125. #define TSENS_BASE2_BACKUP_MASK 0xfc0000
  126. #define TSENS0_POINT2_MASK 0x3f00000
  127. #define TSENS0_POINT2_BACKUP_MASK 0xfc000000
  128. #define TSENS1_POINT2_MASK 0xfc000000
  129. #define TSENS1_POINT2_BACKUP_MASK 0x3f
  130. #define TSENS2_POINT2_MASK 0x3f
  131. #define TSENS2_POINT2_BACKUP_MASK 0xfc0
  132. #define TSENS3_POINT2_MASK 0xfc0
  133. #define TSENS3_POINT2_BACKUP_MASK 0x3f000
  134. #define TSENS4_POINT2_MASK 0x3f000
  135. #define TSENS4_POINT2_BACKUP_MASK 0xfc0000
  136. #define TSENS5_POINT2_MASK 0xfc0000
  137. #define TSENS5_POINT2_BACKUP_MASK 0x3f000000
  138. #define TSENS6_POINT2_MASK 0x3f000000
  139. #define TSENS6_POINT2_BACKUP_MASK 0x3f
  140. #define TSENS7_POINT2_MASK 0x3f
  141. #define TSENS7_POINT2_BACKUP_MASK 0xfc0
  142. #define TSENS8_POINT2_MASK 0xfc0
  143. #define TSENS8_POINT2_BACKUP_MASK 0x3f000
  144. #define TSENS9_POINT2_MASK 0x3f000
  145. #define TSENS9_POINT2_BACKUP_MASK 0xfc0000
  146. #define TSENS10_POINT2_MASK 0xfc0000
  147. #define TSENS10_POINT2_BACKUP_MASK 0x3f000000
  148. #define TSENS_8X26_BASE0_MASK 0x1fe000
  149. #define TSENS0_8X26_POINT1_MASK 0x7e00000
  150. #define TSENS1_8X26_POINT1_MASK 0x3f
  151. #define TSENS2_8X26_POINT1_MASK 0xfc0
  152. #define TSENS3_8X26_POINT1_MASK 0x3f000
  153. #define TSENS4_8X26_POINT1_MASK 0xfc0000
  154. #define TSENS5_8X26_POINT1_MASK 0x3f000000
  155. #define TSENS6_8X26_POINT1_MASK 0x3f00000
  156. #define TSENS_8X26_TSENS_CAL_SEL 0xe0000000
  157. #define TSENS_8X26_BASE1_MASK 0xff
  158. #define TSENS0_8X26_POINT2_MASK 0x3f00
  159. #define TSENS1_8X26_POINT2_MASK 0xfc000
  160. #define TSENS2_8X26_POINT2_MASK 0x3f00000
  161. #define TSENS3_8X26_POINT2_MASK 0xfc000000
  162. #define TSENS4_8X26_POINT2_MASK 0x3f00000
  163. #define TSENS5_8X26_POINT2_MASK 0xfc000000
  164. #define TSENS6_8X26_POINT2_MASK 0x7e0000
  165. #define TSENS_8X26_CAL_SEL_SHIFT 29
  166. #define TSENS_8X26_BASE0_SHIFT 13
  167. #define TSENS0_8X26_POINT1_SHIFT 21
  168. #define TSENS2_8X26_POINT1_SHIFT 6
  169. #define TSENS3_8X26_POINT1_SHIFT 12
  170. #define TSENS4_8X26_POINT1_SHIFT 18
  171. #define TSENS5_8X26_POINT1_SHIFT 24
  172. #define TSENS6_8X26_POINT1_SHIFT 20
  173. #define TSENS0_8X26_POINT2_SHIFT 8
  174. #define TSENS1_8X26_POINT2_SHIFT 14
  175. #define TSENS2_8X26_POINT2_SHIFT 20
  176. #define TSENS3_8X26_POINT2_SHIFT 26
  177. #define TSENS4_8X26_POINT2_SHIFT 20
  178. #define TSENS5_8X26_POINT2_SHIFT 26
  179. #define TSENS6_8X26_POINT2_SHIFT 17
  180. #define TSENS_8X10_CAL_SEL_SHIFT 28
  181. #define TSENS_8X10_BASE1_SHIFT 8
  182. #define TSENS0_8X10_POINT1_SHIFT 16
  183. #define TSENS0_8X10_POINT2_SHIFT 22
  184. #define TSENS1_8X10_POINT2_SHIFT 6
  185. #define TSENS_8X10_BASE0_MASK 0xff
  186. #define TSENS_8X10_BASE1_MASK 0xff00
  187. #define TSENS0_8X10_POINT1_MASK 0x3f0000
  188. #define TSENS0_8X10_POINT2_MASK 0xfc00000
  189. #define TSENS_8X10_TSENS_CAL_SEL 0x70000000
  190. #define TSENS1_8X10_POINT1_MASK 0x3f
  191. #define TSENS1_8X10_POINT2_MASK 0xfc0
  192. #define TSENS_8X10_REDUN_SEL_MASK 0x6000000
  193. #define TSENS_8X10_REDUN_SEL_SHIFT 25
  194. #define TSENS_BIT_APPEND 0x3
  195. #define TSENS_CAL_DEGC_POINT1 30
  196. #define TSENS_CAL_DEGC_POINT2 120
  197. #define TSENS_SLOPE_FACTOR 1000
  198. /* TSENS register data */
  199. #define TSENS_TRDY_RDY_MIN_TIME 2000
  200. #define TSENS_TRDY_RDY_MAX_TIME 2100
  201. #define TSENS_THRESHOLD_MAX_CODE 0x3ff
  202. #define TSENS_THRESHOLD_MIN_CODE 0x0
  203. #define TSENS_GLOBAL_INIT_DATA 0x302f16c
  204. #define TSENS_S0_MAIN_CFG_INIT_DATA 0x1c3
  205. #define TSENS_SN_MIN_MAX_STATUS_CTRL_DATA 0x3ffc00
  206. #define TSENS_SN_REMOTE_CFG_DATA 0x11c3
  207. #define TSENS_QFPROM_BACKUP_SEL 0x3
  208. #define TSENS_QFPROM_BACKUP_REDUN_SEL 0xe0000000
  209. #define TSENS_QFPROM_BACKUP_REDUN_SHIFT 29
  210. enum tsens_calib_fuse_map_type {
  211. TSENS_CALIB_FUSE_MAP_8974 = 0,
  212. TSENS_CALIB_FUSE_MAP_8X26,
  213. TSENS_CALIB_FUSE_MAP_8X10,
  214. TSENS_CALIB_FUSE_MAP_NUM,
  215. };
  216. /* Trips: warm and cool */
  217. enum tsens_trip_type {
  218. TSENS_TRIP_WARM = 0,
  219. TSENS_TRIP_COOL,
  220. TSENS_TRIP_NUM,
  221. };
  222. struct tsens_tm_device_sensor {
  223. struct thermal_zone_device *tz_dev;
  224. enum thermal_device_mode mode;
  225. /* Physical HW sensor number */
  226. unsigned int sensor_hw_num;
  227. /* Software index. This is keep track of the HW/SW
  228. * sensor_ID mapping */
  229. unsigned int sensor_sw_id;
  230. struct work_struct work;
  231. int offset;
  232. int calib_data_point1;
  233. int calib_data_point2;
  234. uint32_t slope_mul_tsens_factor;
  235. };
  236. struct tsens_tm_device {
  237. struct platform_device *pdev;
  238. struct workqueue_struct *tsens_wq;
  239. bool prev_reading_avail;
  240. bool calibration_less_mode;
  241. bool tsens_local_init;
  242. int tsens_factor;
  243. uint32_t tsens_num_sensor;
  244. int tsens_irq;
  245. void *tsens_addr;
  246. void *tsens_calib_addr;
  247. int tsens_len;
  248. int calib_len;
  249. struct resource *res_tsens_mem;
  250. struct resource *res_calib_mem;
  251. struct work_struct tsens_work;
  252. uint32_t calib_mode;
  253. struct tsens_tm_device_sensor sensor[0];
  254. };
  255. struct tsens_tm_device *tmdev;
  256. int tsens_get_sw_id_mapping(int sensor_hw_num, int *sensor_sw_idx)
  257. {
  258. int i = 0;
  259. bool id_found = false;
  260. while (i < tmdev->tsens_num_sensor && !id_found) {
  261. if (sensor_hw_num == tmdev->sensor[i].sensor_hw_num) {
  262. *sensor_sw_idx = tmdev->sensor[i].sensor_sw_id;
  263. id_found = true;
  264. }
  265. i++;
  266. }
  267. if (!id_found)
  268. return -EINVAL;
  269. return 0;
  270. }
  271. EXPORT_SYMBOL(tsens_get_sw_id_mapping);
  272. int tsens_get_hw_id_mapping(int sensor_sw_id, int *sensor_hw_num)
  273. {
  274. int i = 0;
  275. bool id_found = false;
  276. while (i < tmdev->tsens_num_sensor && !id_found) {
  277. if (sensor_sw_id == tmdev->sensor[i].sensor_sw_id) {
  278. *sensor_hw_num = tmdev->sensor[i].sensor_hw_num;
  279. id_found = true;
  280. }
  281. i++;
  282. }
  283. if (!id_found)
  284. return -EINVAL;
  285. return 0;
  286. }
  287. EXPORT_SYMBOL(tsens_get_hw_id_mapping);
  288. static int tsens_tz_code_to_degc(int adc_code, int sensor_sw_id)
  289. {
  290. int degc, num, den, idx;
  291. idx = sensor_sw_id;
  292. num = ((adc_code * tmdev->tsens_factor) -
  293. tmdev->sensor[idx].offset);
  294. den = (int) tmdev->sensor[idx].slope_mul_tsens_factor;
  295. if (num > 0)
  296. degc = ((num + (den/2))/den);
  297. else if (num < 0)
  298. degc = ((num - (den/2))/den);
  299. else
  300. degc = num/den;
  301. pr_debug("raw_code:0x%x, sensor_num:%d, degc:%d\n",
  302. adc_code, idx, degc);
  303. return degc;
  304. }
  305. static int tsens_tz_degc_to_code(int degc, int idx)
  306. {
  307. int code = ((degc * tmdev->sensor[idx].slope_mul_tsens_factor)
  308. + tmdev->sensor[idx].offset)/tmdev->tsens_factor;
  309. if (code > TSENS_THRESHOLD_MAX_CODE)
  310. code = TSENS_THRESHOLD_MAX_CODE;
  311. else if (code < TSENS_THRESHOLD_MIN_CODE)
  312. code = TSENS_THRESHOLD_MIN_CODE;
  313. pr_debug("raw_code:0x%x, sensor_num:%d, degc:%d\n",
  314. code, idx, degc);
  315. return code;
  316. }
  317. static void msm_tsens_get_temp(int sensor_hw_num, unsigned long *temp)
  318. {
  319. unsigned int code, sensor_addr;
  320. int sensor_sw_id = -EINVAL, rc = 0;
  321. if (!tmdev->prev_reading_avail) {
  322. while (!(readl_relaxed(TSENS_TRDY_ADDR(tmdev->tsens_addr))
  323. & TSENS_TRDY_MASK))
  324. usleep_range(TSENS_TRDY_RDY_MIN_TIME,
  325. TSENS_TRDY_RDY_MAX_TIME);
  326. tmdev->prev_reading_avail = true;
  327. }
  328. sensor_addr =
  329. (unsigned int)TSENS_S0_STATUS_ADDR(tmdev->tsens_addr);
  330. code = readl_relaxed(sensor_addr +
  331. (sensor_hw_num << TSENS_STATUS_ADDR_OFFSET));
  332. /* Obtain SW index to map the corresponding thermal zone's
  333. * offset and slope for code to degc conversion. */
  334. rc = tsens_get_sw_id_mapping(sensor_hw_num, &sensor_sw_id);
  335. if (rc < 0) {
  336. pr_err("tsens mapping index not found\n");
  337. return;
  338. }
  339. *temp = tsens_tz_code_to_degc((code & TSENS_SN_STATUS_TEMP_MASK),
  340. sensor_sw_id);
  341. }
  342. static int tsens_tz_get_temp(struct thermal_zone_device *thermal,
  343. unsigned long *temp)
  344. {
  345. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  346. if (!tm_sensor || tm_sensor->mode != THERMAL_DEVICE_ENABLED || !temp)
  347. return -EINVAL;
  348. msm_tsens_get_temp(tm_sensor->sensor_hw_num, temp);
  349. return 0;
  350. }
  351. int tsens_get_temp(struct tsens_device *device, unsigned long *temp)
  352. {
  353. if (!tmdev)
  354. return -ENODEV;
  355. msm_tsens_get_temp(device->sensor_num, temp);
  356. return 0;
  357. }
  358. EXPORT_SYMBOL(tsens_get_temp);
  359. int tsens_get_max_sensor_num(uint32_t *tsens_num_sensors)
  360. {
  361. if (!tmdev)
  362. return -ENODEV;
  363. *tsens_num_sensors = tmdev->tsens_num_sensor;
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(tsens_get_max_sensor_num);
  367. static int tsens_tz_get_mode(struct thermal_zone_device *thermal,
  368. enum thermal_device_mode *mode)
  369. {
  370. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  371. if (!tm_sensor || !mode)
  372. return -EINVAL;
  373. *mode = tm_sensor->mode;
  374. return 0;
  375. }
  376. static int tsens_tz_get_trip_type(struct thermal_zone_device *thermal,
  377. int trip, enum thermal_trip_type *type)
  378. {
  379. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  380. if (!tm_sensor || trip < 0 || !type)
  381. return -EINVAL;
  382. switch (trip) {
  383. case TSENS_TRIP_WARM:
  384. *type = THERMAL_TRIP_CONFIGURABLE_HI;
  385. break;
  386. case TSENS_TRIP_COOL:
  387. *type = THERMAL_TRIP_CONFIGURABLE_LOW;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static int tsens_tz_activate_trip_type(struct thermal_zone_device *thermal,
  395. int trip, enum thermal_trip_activation_mode mode)
  396. {
  397. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  398. unsigned int reg_cntl, code, hi_code, lo_code, mask;
  399. if (!tm_sensor || trip < 0)
  400. return -EINVAL;
  401. lo_code = TSENS_THRESHOLD_MIN_CODE;
  402. hi_code = TSENS_THRESHOLD_MAX_CODE;
  403. reg_cntl = readl_relaxed((TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  404. (tmdev->tsens_addr) +
  405. (tm_sensor->sensor_hw_num *
  406. TSENS_SN_ADDR_OFFSET)));
  407. switch (trip) {
  408. case TSENS_TRIP_WARM:
  409. code = (reg_cntl & TSENS_UPPER_THRESHOLD_MASK)
  410. >> TSENS_UPPER_THRESHOLD_SHIFT;
  411. mask = TSENS_UPPER_STATUS_CLR;
  412. if (!(reg_cntl & TSENS_LOWER_STATUS_CLR))
  413. lo_code = (reg_cntl & TSENS_LOWER_THRESHOLD_MASK);
  414. break;
  415. case TSENS_TRIP_COOL:
  416. code = (reg_cntl & TSENS_LOWER_THRESHOLD_MASK);
  417. mask = TSENS_LOWER_STATUS_CLR;
  418. if (!(reg_cntl & TSENS_UPPER_STATUS_CLR))
  419. hi_code = (reg_cntl & TSENS_UPPER_THRESHOLD_MASK)
  420. >> TSENS_UPPER_THRESHOLD_SHIFT;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. if (mode == THERMAL_TRIP_ACTIVATION_DISABLED)
  426. writel_relaxed(reg_cntl | mask,
  427. (TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  428. (tmdev->tsens_addr) +
  429. (tm_sensor->sensor_hw_num * TSENS_SN_ADDR_OFFSET)));
  430. else {
  431. if (code < lo_code || code > hi_code) {
  432. pr_err("%s with invalid code %x\n", __func__, code);
  433. return -EINVAL;
  434. }
  435. writel_relaxed(reg_cntl & ~mask,
  436. (TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR(tmdev->tsens_addr) +
  437. (tm_sensor->sensor_hw_num * TSENS_SN_ADDR_OFFSET)));
  438. }
  439. mb();
  440. return 0;
  441. }
  442. static int tsens_tz_get_trip_temp(struct thermal_zone_device *thermal,
  443. int trip, unsigned long *temp)
  444. {
  445. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  446. unsigned int reg;
  447. int sensor_sw_id = -EINVAL, rc = 0;
  448. if (!tm_sensor || trip < 0 || !temp)
  449. return -EINVAL;
  450. reg = readl_relaxed(TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  451. (tmdev->tsens_addr) +
  452. (tm_sensor->sensor_hw_num * TSENS_SN_ADDR_OFFSET));
  453. switch (trip) {
  454. case TSENS_TRIP_WARM:
  455. reg = (reg & TSENS_UPPER_THRESHOLD_MASK) >>
  456. TSENS_UPPER_THRESHOLD_SHIFT;
  457. break;
  458. case TSENS_TRIP_COOL:
  459. reg = (reg & TSENS_LOWER_THRESHOLD_MASK);
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. rc = tsens_get_sw_id_mapping(tm_sensor->sensor_hw_num, &sensor_sw_id);
  465. if (rc < 0) {
  466. pr_err("tsens mapping index not found\n");
  467. return rc;
  468. }
  469. *temp = tsens_tz_code_to_degc(reg, sensor_sw_id);
  470. return 0;
  471. }
  472. static int tsens_tz_notify(struct thermal_zone_device *thermal,
  473. int count, enum thermal_trip_type type)
  474. {
  475. /* Critical temperature threshold are enabled and will
  476. * shutdown the device once critical thresholds are crossed. */
  477. pr_debug("%s debug\n", __func__);
  478. return 1;
  479. }
  480. static int tsens_tz_set_trip_temp(struct thermal_zone_device *thermal,
  481. int trip, long temp)
  482. {
  483. struct tsens_tm_device_sensor *tm_sensor = thermal->devdata;
  484. unsigned int reg_cntl;
  485. int code, hi_code, lo_code, code_err_chk, sensor_sw_id = 0, rc = 0;
  486. if (!tm_sensor || trip < 0)
  487. return -EINVAL;
  488. rc = tsens_get_sw_id_mapping(tm_sensor->sensor_hw_num, &sensor_sw_id);
  489. if (rc < 0) {
  490. pr_err("tsens mapping index not found\n");
  491. return rc;
  492. }
  493. code_err_chk = code = tsens_tz_degc_to_code(temp, sensor_sw_id);
  494. if (!tm_sensor || trip < 0)
  495. return -EINVAL;
  496. lo_code = TSENS_THRESHOLD_MIN_CODE;
  497. hi_code = TSENS_THRESHOLD_MAX_CODE;
  498. reg_cntl = readl_relaxed(TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  499. (tmdev->tsens_addr) + (tm_sensor->sensor_hw_num *
  500. TSENS_SN_ADDR_OFFSET));
  501. switch (trip) {
  502. case TSENS_TRIP_WARM:
  503. code <<= TSENS_UPPER_THRESHOLD_SHIFT;
  504. reg_cntl &= ~TSENS_UPPER_THRESHOLD_MASK;
  505. if (!(reg_cntl & TSENS_LOWER_STATUS_CLR))
  506. lo_code = (reg_cntl & TSENS_LOWER_THRESHOLD_MASK);
  507. break;
  508. case TSENS_TRIP_COOL:
  509. reg_cntl &= ~TSENS_LOWER_THRESHOLD_MASK;
  510. if (!(reg_cntl & TSENS_UPPER_STATUS_CLR))
  511. hi_code = (reg_cntl & TSENS_UPPER_THRESHOLD_MASK)
  512. >> TSENS_UPPER_THRESHOLD_SHIFT;
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. if (code_err_chk < lo_code || code_err_chk > hi_code)
  518. return -EINVAL;
  519. writel_relaxed(reg_cntl | code, (TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  520. (tmdev->tsens_addr) +
  521. (tm_sensor->sensor_hw_num *
  522. TSENS_SN_ADDR_OFFSET)));
  523. mb();
  524. return 0;
  525. }
  526. static struct thermal_zone_device_ops tsens_thermal_zone_ops = {
  527. .get_temp = tsens_tz_get_temp,
  528. .get_mode = tsens_tz_get_mode,
  529. .get_trip_type = tsens_tz_get_trip_type,
  530. .activate_trip_type = tsens_tz_activate_trip_type,
  531. .get_trip_temp = tsens_tz_get_trip_temp,
  532. .set_trip_temp = tsens_tz_set_trip_temp,
  533. .notify = tsens_tz_notify,
  534. };
  535. static void notify_uspace_tsens_fn(struct work_struct *work)
  536. {
  537. struct tsens_tm_device_sensor *tm = container_of(work,
  538. struct tsens_tm_device_sensor, work);
  539. sysfs_notify(&tm->tz_dev->device.kobj,
  540. NULL, "type");
  541. }
  542. static void tsens_scheduler_fn(struct work_struct *work)
  543. {
  544. struct tsens_tm_device *tm = container_of(work, struct tsens_tm_device,
  545. tsens_work);
  546. unsigned int i, status, threshold;
  547. unsigned int sensor_status_addr, sensor_status_ctrl_addr;
  548. int sensor_sw_id = -EINVAL, rc = 0;
  549. sensor_status_addr =
  550. (unsigned int)TSENS_S0_STATUS_ADDR(tmdev->tsens_addr);
  551. sensor_status_ctrl_addr =
  552. (unsigned int)TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR
  553. (tmdev->tsens_addr);
  554. for (i = 0; i < tm->tsens_num_sensor; i++) {
  555. bool upper_thr = false, lower_thr = false;
  556. uint32_t addr_offset;
  557. addr_offset = tm->sensor[i].sensor_hw_num *
  558. TSENS_SN_ADDR_OFFSET;
  559. status = readl_relaxed(sensor_status_addr + addr_offset);
  560. threshold = readl_relaxed(sensor_status_ctrl_addr +
  561. addr_offset);
  562. if (status & TSENS_SN_STATUS_UPPER_STATUS) {
  563. writel_relaxed(threshold | TSENS_UPPER_STATUS_CLR,
  564. TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR(
  565. tmdev->tsens_addr + addr_offset));
  566. upper_thr = true;
  567. }
  568. if (status & TSENS_SN_STATUS_LOWER_STATUS) {
  569. writel_relaxed(threshold | TSENS_LOWER_STATUS_CLR,
  570. TSENS_S0_UPPER_LOWER_STATUS_CTRL_ADDR(
  571. tmdev->tsens_addr + addr_offset));
  572. lower_thr = true;
  573. }
  574. if (upper_thr || lower_thr) {
  575. unsigned long temp;
  576. enum thermal_trip_type trip =
  577. THERMAL_TRIP_CONFIGURABLE_LOW;
  578. if (upper_thr)
  579. trip = THERMAL_TRIP_CONFIGURABLE_HI;
  580. tsens_tz_get_temp(tm->sensor[i].tz_dev, &temp);
  581. thermal_sensor_trip(tm->sensor[i].tz_dev, trip, temp);
  582. /* Notify user space */
  583. queue_work(tm->tsens_wq, &tm->sensor[i].work);
  584. rc = tsens_get_sw_id_mapping(
  585. tm->sensor[i].sensor_hw_num,
  586. &sensor_sw_id);
  587. if (rc < 0)
  588. pr_err("tsens mapping index not found\n");
  589. pr_debug("sensor:%d trigger temp (%d degC)\n",
  590. tm->sensor[i].sensor_hw_num,
  591. tsens_tz_code_to_degc((status &
  592. TSENS_SN_STATUS_TEMP_MASK),
  593. sensor_sw_id));
  594. }
  595. }
  596. mb();
  597. }
  598. static irqreturn_t tsens_isr(int irq, void *data)
  599. {
  600. queue_work(tmdev->tsens_wq, &tmdev->tsens_work);
  601. return IRQ_HANDLED;
  602. }
  603. static void tsens_hw_init(void)
  604. {
  605. unsigned int reg_cntl = 0, sensor_en = 0;
  606. unsigned int i;
  607. if (tmdev->tsens_local_init) {
  608. writel_relaxed(reg_cntl, TSENS_CTRL_ADDR(tmdev->tsens_addr));
  609. writel_relaxed(reg_cntl | TSENS_SW_RST,
  610. TSENS_CTRL_ADDR(tmdev->tsens_addr));
  611. reg_cntl |= (TSENS_62_5_MS_MEAS_PERIOD <<
  612. TSENS_MEAS_PERIOD_SHIFT);
  613. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  614. sensor_en |= (1 << tmdev->sensor[i].sensor_hw_num);
  615. sensor_en <<= TSENS_SENSOR0_SHIFT;
  616. reg_cntl |= (sensor_en | TSENS_EN);
  617. writel_relaxed(reg_cntl, TSENS_CTRL_ADDR(tmdev->tsens_addr));
  618. writel_relaxed(TSENS_GLOBAL_INIT_DATA,
  619. TSENS_GLOBAL_CONFIG(tmdev->tsens_addr));
  620. writel_relaxed(TSENS_S0_MAIN_CFG_INIT_DATA,
  621. TSENS_S0_MAIN_CONFIG(tmdev->tsens_addr));
  622. for (i = 0; i < tmdev->tsens_num_sensor; i++) {
  623. writel_relaxed(TSENS_SN_MIN_MAX_STATUS_CTRL_DATA,
  624. TSENS_SN_MIN_MAX_STATUS_CTRL(tmdev->tsens_addr)
  625. + (tmdev->sensor[i].sensor_hw_num *
  626. TSENS_SN_ADDR_OFFSET));
  627. writel_relaxed(TSENS_SN_REMOTE_CFG_DATA,
  628. TSENS_SN_REMOTE_CONFIG(tmdev->tsens_addr)
  629. + (tmdev->sensor[i].sensor_hw_num *
  630. TSENS_SN_ADDR_OFFSET));
  631. }
  632. pr_debug("Local TSENS control initialization\n");
  633. }
  634. writel_relaxed(TSENS_INTERRUPT_EN,
  635. TSENS_UPPER_LOWER_INTERRUPT_CTRL(tmdev->tsens_addr));
  636. }
  637. static int tsens_calib_8x10_sensors(void)
  638. {
  639. int i, tsens_base0_data = 0, tsens0_point1 = 0, tsens1_point1 = 0;
  640. int tsens0_point2 = 0, tsens1_point2 = 0;
  641. int tsens_base1_data = 0, tsens_calibration_mode = 0;
  642. uint32_t calib_data[2], calib_redun_sel;
  643. uint32_t calib_tsens_point1_data[2], calib_tsens_point2_data[2];
  644. if (tmdev->calibration_less_mode)
  645. goto calibration_less_mode;
  646. calib_redun_sel = readl_relaxed(
  647. TSENS_EEPROM_8X10_2(tmdev->tsens_calib_addr));
  648. calib_redun_sel = calib_redun_sel & TSENS_8X10_REDUN_SEL_MASK;
  649. calib_redun_sel >>= TSENS_8X10_REDUN_SEL_SHIFT;
  650. pr_debug("calib_redun_sel:%x\n", calib_redun_sel);
  651. if (calib_redun_sel == TSENS_QFPROM_BACKUP_SEL) {
  652. calib_data[0] = readl_relaxed(
  653. TSENS_EEPROM_8X10_SPARE_1(tmdev->tsens_calib_addr));
  654. calib_data[1] = readl_relaxed(
  655. TSENS_EEPROM_8X10_SPARE_2(tmdev->tsens_calib_addr));
  656. } else {
  657. calib_data[0] = readl_relaxed(
  658. TSENS_EEPROM_8X10_1(tmdev->tsens_calib_addr));
  659. calib_data[1] = readl_relaxed(
  660. (TSENS_EEPROM_8X10_1(tmdev->tsens_calib_addr) +
  661. TSENS_EEPROM_8X10_1_OFFSET));
  662. }
  663. tsens_calibration_mode = (calib_data[0] & TSENS_8X10_TSENS_CAL_SEL)
  664. >> TSENS_8X10_CAL_SEL_SHIFT;
  665. pr_debug("calib mode scheme:%x\n", tsens_calibration_mode);
  666. if ((tsens_calibration_mode == TSENS_TWO_POINT_CALIB) ||
  667. (tsens_calibration_mode == TSENS_ONE_POINT_CALIB_OPTION_2)) {
  668. tsens_base0_data = (calib_data[0] & TSENS_8X10_BASE0_MASK);
  669. tsens0_point1 = (calib_data[0] & TSENS0_8X10_POINT1_MASK) >>
  670. TSENS0_8X10_POINT1_SHIFT;
  671. tsens1_point1 = calib_data[1] & TSENS1_8X10_POINT1_MASK;
  672. } else
  673. goto calibration_less_mode;
  674. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  675. tsens_base1_data = (calib_data[0] & TSENS_8X10_BASE1_MASK) >>
  676. TSENS_8X10_BASE1_SHIFT;
  677. tsens0_point2 = (calib_data[0] & TSENS0_8X10_POINT2_MASK) >>
  678. TSENS0_8X10_POINT2_SHIFT;
  679. tsens1_point2 = (calib_data[1] & TSENS1_8X10_POINT2_MASK) >>
  680. TSENS1_8X10_POINT2_SHIFT;
  681. }
  682. if (tsens_calibration_mode == 0) {
  683. calibration_less_mode:
  684. pr_debug("TSENS is calibrationless mode\n");
  685. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  686. calib_tsens_point2_data[i] = 780;
  687. calib_tsens_point1_data[0] = 595;
  688. calib_tsens_point1_data[1] = 629;
  689. goto compute_intercept_slope;
  690. }
  691. if ((tsens_calibration_mode == TSENS_ONE_POINT_CALIB_OPTION_2) ||
  692. (tsens_calibration_mode == TSENS_TWO_POINT_CALIB)) {
  693. calib_tsens_point1_data[0] =
  694. ((((tsens_base0_data) + tsens0_point1) << 2) |
  695. TSENS_BIT_APPEND);
  696. calib_tsens_point1_data[1] =
  697. ((((tsens_base0_data) + tsens1_point1) << 2) |
  698. TSENS_BIT_APPEND);
  699. }
  700. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  701. pr_debug("two point calibration calculation\n");
  702. calib_tsens_point2_data[0] =
  703. (((tsens_base1_data + tsens0_point2) << 2) |
  704. TSENS_BIT_APPEND);
  705. calib_tsens_point2_data[1] =
  706. (((tsens_base1_data + tsens1_point2) << 2) |
  707. TSENS_BIT_APPEND);
  708. }
  709. compute_intercept_slope:
  710. for (i = 0; i < tmdev->tsens_num_sensor; i++) {
  711. int32_t num = 0, den = 0;
  712. tmdev->sensor[i].calib_data_point2 = calib_tsens_point2_data[i];
  713. tmdev->sensor[i].calib_data_point1 = calib_tsens_point1_data[i];
  714. pr_debug("sensor:%d - calib_data_point1:0x%x, calib_data_point2:0x%x\n",
  715. i, tmdev->sensor[i].calib_data_point1,
  716. tmdev->sensor[i].calib_data_point2);
  717. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  718. /* slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  719. temp_120_degc - temp_30_degc (x2 - x1) */
  720. num = tmdev->sensor[i].calib_data_point2 -
  721. tmdev->sensor[i].calib_data_point1;
  722. num *= tmdev->tsens_factor;
  723. den = TSENS_CAL_DEGC_POINT2 - TSENS_CAL_DEGC_POINT1;
  724. tmdev->sensor[i].slope_mul_tsens_factor = num/den;
  725. }
  726. tmdev->sensor[i].offset = (tmdev->sensor[i].calib_data_point1 *
  727. tmdev->tsens_factor) - (TSENS_CAL_DEGC_POINT1 *
  728. tmdev->sensor[i].slope_mul_tsens_factor);
  729. INIT_WORK(&tmdev->sensor[i].work, notify_uspace_tsens_fn);
  730. tmdev->prev_reading_avail = false;
  731. }
  732. return 0;
  733. }
  734. static int tsens_calib_8x26_sensors(void)
  735. {
  736. int i, tsens_base0_data = 0, tsens0_point1 = 0, tsens1_point1 = 0;
  737. int tsens2_point1 = 0, tsens3_point1 = 0, tsens4_point1 = 0;
  738. int tsens5_point1 = 0, tsens6_point1 = 0, tsens6_point2 = 0;
  739. int tsens0_point2 = 0, tsens1_point2 = 0, tsens2_point2 = 0;
  740. int tsens3_point2 = 0, tsens4_point2 = 0, tsens5_point2 = 0;
  741. int tsens_base1_data = 0, tsens_calibration_mode = 0;
  742. uint32_t calib_data[6];
  743. uint32_t calib_tsens_point1_data[7], calib_tsens_point2_data[7];
  744. if (tmdev->calibration_less_mode)
  745. goto calibration_less_mode;
  746. for (i = 0; i < TSENS_8X26_MAIN_CALIB_ADDR_RANGE; i++)
  747. calib_data[i] = readl_relaxed(
  748. (TSENS_EEPROM_8X26_1(tmdev->tsens_calib_addr))
  749. + (i * TSENS_SN_ADDR_OFFSET));
  750. calib_data[4] = readl_relaxed(
  751. (TSENS_EEPROM_8X26_2(tmdev->tsens_calib_addr)));
  752. calib_data[5] = readl_relaxed(
  753. (TSENS_EEPROM_8X26_2(tmdev->tsens_calib_addr)) + 0x8);
  754. tsens_calibration_mode = (calib_data[5] & TSENS_8X26_TSENS_CAL_SEL)
  755. >> TSENS_8X26_CAL_SEL_SHIFT;
  756. pr_debug("calib mode scheme:%x\n", tsens_calibration_mode);
  757. if ((tsens_calibration_mode == TSENS_TWO_POINT_CALIB) ||
  758. (tsens_calibration_mode == TSENS_ONE_POINT_CALIB_OPTION_2)) {
  759. tsens_base0_data = (calib_data[0] & TSENS_8X26_BASE0_MASK)
  760. >> TSENS_8X26_BASE0_SHIFT;
  761. tsens0_point1 = (calib_data[0] & TSENS0_8X26_POINT1_MASK) >>
  762. TSENS0_8X26_POINT1_SHIFT;
  763. tsens1_point1 = calib_data[1] & TSENS1_8X26_POINT1_MASK;
  764. tsens2_point1 = (calib_data[1] & TSENS2_8X26_POINT1_MASK) >>
  765. TSENS2_8X26_POINT1_SHIFT;
  766. tsens3_point1 = (calib_data[1] & TSENS3_8X26_POINT1_MASK) >>
  767. TSENS3_8X26_POINT1_SHIFT;
  768. tsens4_point1 = (calib_data[1] & TSENS4_8X26_POINT1_MASK) >>
  769. TSENS4_8X26_POINT1_SHIFT;
  770. tsens5_point1 = (calib_data[1] & TSENS5_8X26_POINT1_MASK) >>
  771. TSENS5_8X26_POINT1_SHIFT;
  772. tsens6_point1 = (calib_data[2] & TSENS6_8X26_POINT1_MASK) >>
  773. TSENS6_8X26_POINT1_SHIFT;
  774. } else
  775. goto calibration_less_mode;
  776. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  777. tsens_base1_data = (calib_data[3] & TSENS_8X26_BASE1_MASK);
  778. tsens0_point2 = (calib_data[3] & TSENS0_8X26_POINT2_MASK) >>
  779. TSENS0_8X26_POINT2_SHIFT;
  780. tsens1_point2 = (calib_data[3] & TSENS1_8X26_POINT2_MASK) >>
  781. TSENS1_8X26_POINT2_SHIFT;
  782. tsens2_point2 = (calib_data[3] & TSENS2_8X26_POINT2_MASK) >>
  783. TSENS2_8X26_POINT2_SHIFT;
  784. tsens3_point2 = (calib_data[3] & TSENS3_8X26_POINT2_MASK) >>
  785. TSENS3_8X26_POINT2_SHIFT;
  786. tsens4_point2 = (calib_data[4] & TSENS4_8X26_POINT2_MASK) >>
  787. TSENS4_8X26_POINT2_SHIFT;
  788. tsens5_point2 = (calib_data[4] & TSENS5_8X26_POINT2_MASK) >>
  789. TSENS5_8X26_POINT2_SHIFT;
  790. tsens6_point2 = (calib_data[5] & TSENS6_8X26_POINT2_MASK) >>
  791. TSENS6_8X26_POINT2_SHIFT;
  792. }
  793. if (tsens_calibration_mode == 0) {
  794. calibration_less_mode:
  795. pr_debug("TSENS is calibrationless mode\n");
  796. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  797. calib_tsens_point2_data[i] = 780;
  798. calib_tsens_point1_data[0] = 595;
  799. calib_tsens_point1_data[1] = 625;
  800. calib_tsens_point1_data[2] = 553;
  801. calib_tsens_point1_data[3] = 578;
  802. calib_tsens_point1_data[4] = 505;
  803. calib_tsens_point1_data[5] = 509;
  804. calib_tsens_point1_data[6] = 507;
  805. goto compute_intercept_slope;
  806. }
  807. if ((tsens_calibration_mode == TSENS_ONE_POINT_CALIB_OPTION_2) ||
  808. (tsens_calibration_mode == TSENS_TWO_POINT_CALIB)) {
  809. calib_tsens_point1_data[0] =
  810. ((((tsens_base0_data) + tsens0_point1) << 2) |
  811. TSENS_BIT_APPEND);
  812. calib_tsens_point1_data[1] =
  813. ((((tsens_base0_data) + tsens1_point1) << 2) |
  814. TSENS_BIT_APPEND);
  815. calib_tsens_point1_data[2] =
  816. ((((tsens_base0_data) + tsens2_point1) << 2) |
  817. TSENS_BIT_APPEND);
  818. calib_tsens_point1_data[3] =
  819. ((((tsens_base0_data) + tsens3_point1) << 2) |
  820. TSENS_BIT_APPEND);
  821. calib_tsens_point1_data[4] =
  822. ((((tsens_base0_data) + tsens4_point1) << 2) |
  823. TSENS_BIT_APPEND);
  824. calib_tsens_point1_data[5] =
  825. ((((tsens_base0_data) + tsens5_point1) << 2) |
  826. TSENS_BIT_APPEND);
  827. calib_tsens_point1_data[6] =
  828. ((((tsens_base0_data) + tsens6_point1) << 2) |
  829. TSENS_BIT_APPEND);
  830. }
  831. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  832. pr_debug("two point calibration calculation\n");
  833. calib_tsens_point2_data[0] =
  834. (((tsens_base1_data + tsens0_point2) << 2) |
  835. TSENS_BIT_APPEND);
  836. calib_tsens_point2_data[1] =
  837. (((tsens_base1_data + tsens1_point2) << 2) |
  838. TSENS_BIT_APPEND);
  839. calib_tsens_point2_data[2] =
  840. (((tsens_base1_data + tsens2_point2) << 2) |
  841. TSENS_BIT_APPEND);
  842. calib_tsens_point2_data[3] =
  843. (((tsens_base1_data + tsens3_point2) << 2) |
  844. TSENS_BIT_APPEND);
  845. calib_tsens_point2_data[4] =
  846. (((tsens_base1_data + tsens4_point2) << 2) |
  847. TSENS_BIT_APPEND);
  848. calib_tsens_point2_data[5] =
  849. (((tsens_base1_data + tsens5_point2) << 2) |
  850. TSENS_BIT_APPEND);
  851. calib_tsens_point2_data[6] =
  852. (((tsens_base1_data + tsens6_point2) << 2) |
  853. TSENS_BIT_APPEND);
  854. }
  855. compute_intercept_slope:
  856. for (i = 0; i < tmdev->tsens_num_sensor; i++) {
  857. int32_t num = 0, den = 0;
  858. tmdev->sensor[i].calib_data_point2 = calib_tsens_point2_data[i];
  859. tmdev->sensor[i].calib_data_point1 = calib_tsens_point1_data[i];
  860. pr_debug("sensor:%d - calib_data_point1:0x%x, calib_data_point2:0x%x\n",
  861. i, tmdev->sensor[i].calib_data_point1,
  862. tmdev->sensor[i].calib_data_point2);
  863. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  864. /* slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  865. temp_120_degc - temp_30_degc (x2 - x1) */
  866. num = tmdev->sensor[i].calib_data_point2 -
  867. tmdev->sensor[i].calib_data_point1;
  868. num *= tmdev->tsens_factor;
  869. den = TSENS_CAL_DEGC_POINT2 - TSENS_CAL_DEGC_POINT1;
  870. tmdev->sensor[i].slope_mul_tsens_factor = num/den;
  871. }
  872. tmdev->sensor[i].offset = (tmdev->sensor[i].calib_data_point1 *
  873. tmdev->tsens_factor) - (TSENS_CAL_DEGC_POINT1 *
  874. tmdev->sensor[i].slope_mul_tsens_factor);
  875. INIT_WORK(&tmdev->sensor[i].work, notify_uspace_tsens_fn);
  876. tmdev->prev_reading_avail = false;
  877. }
  878. return 0;
  879. }
  880. static int tsens_calib_8974_sensors(void)
  881. {
  882. int i, tsens_base1_data = 0, tsens0_point1 = 0, tsens1_point1 = 0;
  883. int tsens2_point1 = 0, tsens3_point1 = 0, tsens4_point1 = 0;
  884. int tsens5_point1 = 0, tsens6_point1 = 0, tsens7_point1 = 0;
  885. int tsens8_point1 = 0, tsens9_point1 = 0, tsens10_point1 = 0;
  886. int tsens0_point2 = 0, tsens1_point2 = 0, tsens2_point2 = 0;
  887. int tsens3_point2 = 0, tsens4_point2 = 0, tsens5_point2 = 0;
  888. int tsens6_point2 = 0, tsens7_point2 = 0, tsens8_point2 = 0;
  889. int tsens9_point2 = 0, tsens10_point2 = 0;
  890. int tsens_base2_data = 0, tsens_calibration_mode = 0, temp = 0;
  891. uint32_t calib_data[6], calib_redun_sel, calib_data_backup[4];
  892. uint32_t calib_tsens_point1_data[11], calib_tsens_point2_data[11];
  893. if (tmdev->calibration_less_mode)
  894. goto calibration_less_mode;
  895. calib_redun_sel = readl_relaxed(
  896. TSENS_EEPROM_REDUNDANCY_SEL(tmdev->tsens_calib_addr));
  897. calib_redun_sel = calib_redun_sel & TSENS_QFPROM_BACKUP_REDUN_SEL;
  898. calib_redun_sel >>= TSENS_QFPROM_BACKUP_REDUN_SHIFT;
  899. pr_debug("calib_redun_sel:%x\n", calib_redun_sel);
  900. for (i = 0; i < TSENS_MAIN_CALIB_ADDR_RANGE; i++) {
  901. calib_data[i] = readl_relaxed(
  902. (TSENS_EEPROM(tmdev->tsens_calib_addr))
  903. + (i * TSENS_SN_ADDR_OFFSET));
  904. pr_debug("calib raw data row%d:0x%x\n", i, calib_data[i]);
  905. }
  906. if (calib_redun_sel == TSENS_QFPROM_BACKUP_SEL) {
  907. tsens_calibration_mode = (calib_data[4] & TSENS_CAL_SEL_0_1)
  908. >> TSENS_CAL_SEL_SHIFT;
  909. temp = (calib_data[5] & TSENS_CAL_SEL_2)
  910. >> TSENS_CAL_SEL_SHIFT_2;
  911. tsens_calibration_mode |= temp;
  912. pr_debug("backup calib mode:%x\n", calib_redun_sel);
  913. for (i = 0; i < TSENS_BACKUP_CALIB_ADDR_RANGE; i++)
  914. calib_data_backup[i] = readl_relaxed(
  915. (TSENS_EEPROM_BACKUP_REGION(
  916. tmdev->tsens_calib_addr))
  917. + (i * TSENS_SN_ADDR_OFFSET));
  918. if ((tsens_calibration_mode == TSENS_ONE_POINT_CALIB)
  919. || (tsens_calibration_mode ==
  920. TSENS_TWO_POINT_CALIB) ||
  921. (tsens_calibration_mode ==
  922. TSENS_ONE_POINT_CALIB_OPTION_2)) {
  923. tsens_base1_data = (calib_data_backup[0] &
  924. TSENS_BASE1_MASK);
  925. tsens0_point1 = (calib_data_backup[0] &
  926. TSENS0_POINT1_MASK) >>
  927. TSENS0_POINT1_SHIFT;
  928. tsens1_point1 = (calib_data_backup[0] &
  929. TSENS1_POINT1_MASK) >> TSENS1_POINT1_SHIFT;
  930. tsens2_point1 = (calib_data_backup[0] &
  931. TSENS2_POINT1_MASK) >> TSENS2_POINT1_SHIFT;
  932. tsens3_point1 = (calib_data_backup[0] &
  933. TSENS3_POINT1_MASK) >> TSENS3_POINT1_SHIFT;
  934. tsens4_point1 = (calib_data_backup[1] &
  935. TSENS4_POINT1_MASK);
  936. tsens5_point1 = (calib_data_backup[1] &
  937. TSENS5_POINT1_MASK) >> TSENS5_POINT1_SHIFT;
  938. tsens6_point1 = (calib_data_backup[1] &
  939. TSENS6_POINT1_MASK) >> TSENS6_POINT1_SHIFT;
  940. tsens7_point1 = (calib_data_backup[1] &
  941. TSENS7_POINT1_MASK) >> TSENS7_POINT1_SHIFT;
  942. tsens8_point1 = (calib_data_backup[2] &
  943. TSENS8_POINT1_MASK_BACKUP) >>
  944. TSENS8_POINT1_SHIFT;
  945. tsens9_point1 = (calib_data_backup[2] &
  946. TSENS9_POINT1_MASK_BACKUP) >>
  947. TSENS9_POINT1_BACKUP_SHIFT;
  948. tsens10_point1 = (calib_data_backup[2] &
  949. TSENS10_POINT1_MASK_BACKUP) >>
  950. TSENS10_POINT1_BACKUP_SHIFT;
  951. } else
  952. goto calibration_less_mode;
  953. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  954. tsens_base2_data = (calib_data_backup[2] &
  955. TSENS_BASE2_BACKUP_MASK) >>
  956. TSENS_POINT2_BASE_BACKUP_SHIFT;
  957. tsens0_point2 = (calib_data_backup[2] &
  958. TSENS0_POINT2_BACKUP_MASK) >>
  959. TSENS0_POINT2_BACKUP_SHIFT;
  960. tsens1_point2 = (calib_data_backup[3] &
  961. TSENS1_POINT2_BACKUP_MASK);
  962. tsens2_point2 = (calib_data_backup[3] &
  963. TSENS2_POINT2_BACKUP_MASK) >>
  964. TSENS2_POINT2_BACKUP_SHIFT;
  965. tsens3_point2 = (calib_data_backup[3] &
  966. TSENS3_POINT2_BACKUP_MASK) >>
  967. TSENS3_POINT2_BACKUP_SHIFT;
  968. tsens4_point2 = (calib_data_backup[3] &
  969. TSENS4_POINT2_BACKUP_MASK) >>
  970. TSENS4_POINT2_BACKUP_SHIFT;
  971. tsens5_point2 = (calib_data[4] &
  972. TSENS5_POINT2_BACKUP_MASK) >>
  973. TSENS5_POINT2_BACKUP_SHIFT;
  974. tsens6_point2 = (calib_data[5] &
  975. TSENS6_POINT2_BACKUP_MASK);
  976. tsens7_point2 = (calib_data[5] &
  977. TSENS7_POINT2_BACKUP_MASK) >>
  978. TSENS7_POINT2_BACKUP_SHIFT;
  979. tsens8_point2 = (calib_data[5] &
  980. TSENS8_POINT2_BACKUP_MASK) >>
  981. TSENS8_POINT2_BACKUP_SHIFT;
  982. tsens9_point2 = (calib_data[5] &
  983. TSENS9_POINT2_BACKUP_MASK) >>
  984. TSENS9_POINT2_BACKUP_SHIFT;
  985. tsens10_point2 = (calib_data[5] &
  986. TSENS10_POINT2_BACKUP_MASK)
  987. >> TSENS10_POINT2_BACKUP_SHIFT;
  988. }
  989. } else {
  990. tsens_calibration_mode = (calib_data[1] & TSENS_CAL_SEL_0_1)
  991. >> TSENS_CAL_SEL_SHIFT;
  992. temp = (calib_data[3] & TSENS_CAL_SEL_2)
  993. >> TSENS_CAL_SEL_SHIFT_2;
  994. tsens_calibration_mode |= temp;
  995. pr_debug("calib mode scheme:%x\n", tsens_calibration_mode);
  996. if ((tsens_calibration_mode == TSENS_ONE_POINT_CALIB) ||
  997. (tsens_calibration_mode ==
  998. TSENS_ONE_POINT_CALIB_OPTION_2) ||
  999. (tsens_calibration_mode == TSENS_TWO_POINT_CALIB)) {
  1000. tsens_base1_data = (calib_data[0] & TSENS_BASE1_MASK);
  1001. tsens0_point1 = (calib_data[0] & TSENS0_POINT1_MASK) >>
  1002. TSENS0_POINT1_SHIFT;
  1003. tsens1_point1 = (calib_data[0] & TSENS1_POINT1_MASK) >>
  1004. TSENS1_POINT1_SHIFT;
  1005. tsens2_point1 = (calib_data[0] & TSENS2_POINT1_MASK) >>
  1006. TSENS2_POINT1_SHIFT;
  1007. tsens3_point1 = (calib_data[0] & TSENS3_POINT1_MASK) >>
  1008. TSENS3_POINT1_SHIFT;
  1009. tsens4_point1 = (calib_data[1] & TSENS4_POINT1_MASK);
  1010. tsens5_point1 = (calib_data[1] & TSENS5_POINT1_MASK) >>
  1011. TSENS5_POINT1_SHIFT;
  1012. tsens6_point1 = (calib_data[1] & TSENS6_POINT1_MASK) >>
  1013. TSENS6_POINT1_SHIFT;
  1014. tsens7_point1 = (calib_data[1] & TSENS7_POINT1_MASK) >>
  1015. TSENS7_POINT1_SHIFT;
  1016. tsens8_point1 = (calib_data[1] & TSENS8_POINT1_MASK) >>
  1017. TSENS8_POINT1_SHIFT;
  1018. tsens9_point1 = (calib_data[2] & TSENS9_POINT1_MASK);
  1019. tsens10_point1 = (calib_data[2] & TSENS10_POINT1_MASK)
  1020. >> TSENS10_POINT1_SHIFT;
  1021. } else
  1022. goto calibration_less_mode;
  1023. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  1024. tsens_base2_data = (calib_data[2] & TSENS_BASE2_MASK) >>
  1025. TSENS_POINT2_BASE_SHIFT;
  1026. tsens0_point2 = (calib_data[2] & TSENS0_POINT2_MASK) >>
  1027. TSENS0_POINT2_SHIFT;
  1028. tsens1_point2 = (calib_data[2] & TSENS1_POINT2_MASK) >>
  1029. TSENS1_POINT2_SHIFT;
  1030. tsens2_point2 = (calib_data[3] & TSENS2_POINT2_MASK);
  1031. tsens3_point2 = (calib_data[3] & TSENS3_POINT2_MASK) >>
  1032. TSENS3_POINT2_SHIFT;
  1033. tsens4_point2 = (calib_data[3] & TSENS4_POINT2_MASK) >>
  1034. TSENS4_POINT2_SHIFT;
  1035. tsens5_point2 = (calib_data[3] & TSENS5_POINT2_MASK) >>
  1036. TSENS5_POINT2_SHIFT;
  1037. tsens6_point2 = (calib_data[3] & TSENS6_POINT2_MASK) >>
  1038. TSENS6_POINT2_SHIFT;
  1039. tsens7_point2 = (calib_data[4] & TSENS7_POINT2_MASK);
  1040. tsens8_point2 = (calib_data[4] & TSENS8_POINT2_MASK) >>
  1041. TSENS8_POINT2_SHIFT;
  1042. tsens9_point2 = (calib_data[4] & TSENS9_POINT2_MASK) >>
  1043. TSENS9_POINT2_SHIFT;
  1044. tsens10_point2 = (calib_data[4] & TSENS10_POINT2_MASK)
  1045. >> TSENS10_POINT2_SHIFT;
  1046. }
  1047. if (tsens_calibration_mode == 0) {
  1048. calibration_less_mode:
  1049. pr_debug("TSENS is calibrationless mode\n");
  1050. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  1051. calib_tsens_point2_data[i] = 780;
  1052. calib_tsens_point1_data[0] = 502;
  1053. calib_tsens_point1_data[1] = 509;
  1054. calib_tsens_point1_data[2] = 503;
  1055. calib_tsens_point1_data[3] = 509;
  1056. calib_tsens_point1_data[4] = 505;
  1057. calib_tsens_point1_data[5] = 509;
  1058. calib_tsens_point1_data[6] = 507;
  1059. calib_tsens_point1_data[7] = 510;
  1060. calib_tsens_point1_data[8] = 508;
  1061. calib_tsens_point1_data[9] = 509;
  1062. calib_tsens_point1_data[10] = 508;
  1063. goto compute_intercept_slope;
  1064. }
  1065. }
  1066. if (tsens_calibration_mode == TSENS_ONE_POINT_CALIB) {
  1067. calib_tsens_point1_data[0] =
  1068. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1069. + tsens0_point1;
  1070. calib_tsens_point1_data[1] =
  1071. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1072. + tsens1_point1;
  1073. calib_tsens_point1_data[2] =
  1074. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1075. + tsens2_point1;
  1076. calib_tsens_point1_data[3] =
  1077. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1078. + tsens3_point1;
  1079. calib_tsens_point1_data[4] =
  1080. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1081. + tsens4_point1;
  1082. calib_tsens_point1_data[5] =
  1083. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1084. + tsens5_point1;
  1085. calib_tsens_point1_data[6] =
  1086. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1087. + tsens6_point1;
  1088. calib_tsens_point1_data[7] =
  1089. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1090. + tsens7_point1;
  1091. calib_tsens_point1_data[8] =
  1092. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1093. + tsens8_point1;
  1094. calib_tsens_point1_data[9] =
  1095. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1096. + tsens9_point1;
  1097. calib_tsens_point1_data[10] =
  1098. (((tsens_base1_data) << 2) | TSENS_BIT_APPEND)
  1099. + tsens10_point1;
  1100. }
  1101. if ((tsens_calibration_mode == TSENS_ONE_POINT_CALIB_OPTION_2) ||
  1102. (tsens_calibration_mode == TSENS_TWO_POINT_CALIB)) {
  1103. pr_debug("one point calibration calculation\n");
  1104. calib_tsens_point1_data[0] =
  1105. ((((tsens_base1_data) + tsens0_point1) << 2) |
  1106. TSENS_BIT_APPEND);
  1107. calib_tsens_point1_data[1] =
  1108. ((((tsens_base1_data) + tsens1_point1) << 2) |
  1109. TSENS_BIT_APPEND);
  1110. calib_tsens_point1_data[2] =
  1111. ((((tsens_base1_data) + tsens2_point1) << 2) |
  1112. TSENS_BIT_APPEND);
  1113. calib_tsens_point1_data[3] =
  1114. ((((tsens_base1_data) + tsens3_point1) << 2) |
  1115. TSENS_BIT_APPEND);
  1116. calib_tsens_point1_data[4] =
  1117. ((((tsens_base1_data) + tsens4_point1) << 2) |
  1118. TSENS_BIT_APPEND);
  1119. calib_tsens_point1_data[5] =
  1120. ((((tsens_base1_data) + tsens5_point1) << 2) |
  1121. TSENS_BIT_APPEND);
  1122. calib_tsens_point1_data[6] =
  1123. ((((tsens_base1_data) + tsens6_point1) << 2) |
  1124. TSENS_BIT_APPEND);
  1125. calib_tsens_point1_data[7] =
  1126. ((((tsens_base1_data) + tsens7_point1) << 2) |
  1127. TSENS_BIT_APPEND);
  1128. calib_tsens_point1_data[8] =
  1129. ((((tsens_base1_data) + tsens8_point1) << 2) |
  1130. TSENS_BIT_APPEND);
  1131. calib_tsens_point1_data[9] =
  1132. ((((tsens_base1_data) + tsens9_point1) << 2) |
  1133. TSENS_BIT_APPEND);
  1134. calib_tsens_point1_data[10] =
  1135. ((((tsens_base1_data) + tsens10_point1) << 2) |
  1136. TSENS_BIT_APPEND);
  1137. }
  1138. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  1139. pr_debug("two point calibration calculation\n");
  1140. calib_tsens_point2_data[0] =
  1141. (((tsens_base2_data + tsens0_point2) << 2) |
  1142. TSENS_BIT_APPEND);
  1143. calib_tsens_point2_data[1] =
  1144. (((tsens_base2_data + tsens1_point2) << 2) |
  1145. TSENS_BIT_APPEND);
  1146. calib_tsens_point2_data[2] =
  1147. (((tsens_base2_data + tsens2_point2) << 2) |
  1148. TSENS_BIT_APPEND);
  1149. calib_tsens_point2_data[3] =
  1150. (((tsens_base2_data + tsens3_point2) << 2) |
  1151. TSENS_BIT_APPEND);
  1152. calib_tsens_point2_data[4] =
  1153. (((tsens_base2_data + tsens4_point2) << 2) |
  1154. TSENS_BIT_APPEND);
  1155. calib_tsens_point2_data[5] =
  1156. (((tsens_base2_data + tsens5_point2) << 2) |
  1157. TSENS_BIT_APPEND);
  1158. calib_tsens_point2_data[6] =
  1159. (((tsens_base2_data + tsens6_point2) << 2) |
  1160. TSENS_BIT_APPEND);
  1161. calib_tsens_point2_data[7] =
  1162. (((tsens_base2_data + tsens7_point2) << 2) |
  1163. TSENS_BIT_APPEND);
  1164. calib_tsens_point2_data[8] =
  1165. (((tsens_base2_data + tsens8_point2) << 2) |
  1166. TSENS_BIT_APPEND);
  1167. calib_tsens_point2_data[9] =
  1168. (((tsens_base2_data + tsens9_point2) << 2) |
  1169. TSENS_BIT_APPEND);
  1170. calib_tsens_point2_data[10] =
  1171. (((tsens_base2_data + tsens10_point2) << 2) |
  1172. TSENS_BIT_APPEND);
  1173. }
  1174. compute_intercept_slope:
  1175. for (i = 0; i < tmdev->tsens_num_sensor; i++) {
  1176. int32_t num = 0, den = 0;
  1177. tmdev->sensor[i].calib_data_point2 = calib_tsens_point2_data[i];
  1178. tmdev->sensor[i].calib_data_point1 = calib_tsens_point1_data[i];
  1179. pr_debug("sensor:%d - calib_data_point1:0x%x, calib_data_point2:0x%x\n",
  1180. i, tmdev->sensor[i].calib_data_point1,
  1181. tmdev->sensor[i].calib_data_point2);
  1182. if (tsens_calibration_mode == TSENS_TWO_POINT_CALIB) {
  1183. /* slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  1184. temp_120_degc - temp_30_degc (x2 - x1) */
  1185. num = tmdev->sensor[i].calib_data_point2 -
  1186. tmdev->sensor[i].calib_data_point1;
  1187. num *= tmdev->tsens_factor;
  1188. den = TSENS_CAL_DEGC_POINT2 - TSENS_CAL_DEGC_POINT1;
  1189. tmdev->sensor[i].slope_mul_tsens_factor = num/den;
  1190. }
  1191. tmdev->sensor[i].offset = (tmdev->sensor[i].calib_data_point1 *
  1192. tmdev->tsens_factor) - (TSENS_CAL_DEGC_POINT1 *
  1193. tmdev->sensor[i].slope_mul_tsens_factor);
  1194. pr_debug("offset:%d\n", tmdev->sensor[i].offset);
  1195. INIT_WORK(&tmdev->sensor[i].work, notify_uspace_tsens_fn);
  1196. tmdev->prev_reading_avail = false;
  1197. }
  1198. return 0;
  1199. }
  1200. static int tsens_calib_sensors(void)
  1201. {
  1202. int rc = 0;
  1203. if (!tmdev)
  1204. return -ENODEV;
  1205. if (tmdev->calib_mode == TSENS_CALIB_FUSE_MAP_8974)
  1206. rc = tsens_calib_8974_sensors();
  1207. else if (tmdev->calib_mode == TSENS_CALIB_FUSE_MAP_8X26)
  1208. rc = tsens_calib_8x26_sensors();
  1209. else if (tmdev->calib_mode == TSENS_CALIB_FUSE_MAP_8X10)
  1210. rc = tsens_calib_8x10_sensors();
  1211. else
  1212. rc = -ENODEV;
  1213. return rc;
  1214. }
  1215. static int get_device_tree_data(struct platform_device *pdev)
  1216. {
  1217. struct device_node *of_node = pdev->dev.of_node;
  1218. struct resource *res_mem = NULL;
  1219. u32 *tsens_slope_data;
  1220. u32 *sensor_id;
  1221. u32 rc = 0, i, tsens_num_sensors, calib_type;
  1222. const char *tsens_calib_mode;
  1223. rc = of_property_read_u32(of_node,
  1224. "qcom,sensors", &tsens_num_sensors);
  1225. if (rc) {
  1226. dev_err(&pdev->dev, "missing sensor number\n");
  1227. return -ENODEV;
  1228. }
  1229. tsens_slope_data = devm_kzalloc(&pdev->dev,
  1230. tsens_num_sensors * sizeof(u32), GFP_KERNEL);
  1231. if (!tsens_slope_data) {
  1232. dev_err(&pdev->dev, "can not allocate slope data\n");
  1233. return -ENOMEM;
  1234. }
  1235. rc = of_property_read_u32_array(of_node,
  1236. "qcom,slope", tsens_slope_data, tsens_num_sensors);
  1237. if (rc) {
  1238. dev_err(&pdev->dev, "invalid or missing property: tsens-slope\n");
  1239. return rc;
  1240. };
  1241. rc = of_property_read_string(of_node,
  1242. "qcom,calib-mode", &tsens_calib_mode);
  1243. if (rc) {
  1244. dev_err(&pdev->dev, "missing calib-mode\n");
  1245. return -ENODEV;
  1246. }
  1247. if (!strncmp(tsens_calib_mode, "fuse_map1", 9))
  1248. calib_type = TSENS_CALIB_FUSE_MAP_8974;
  1249. else if (!strncmp(tsens_calib_mode, "fuse_map2", 9))
  1250. calib_type = TSENS_CALIB_FUSE_MAP_8X26;
  1251. else if (!strncmp(tsens_calib_mode, "fuse_map3", 9))
  1252. calib_type = TSENS_CALIB_FUSE_MAP_8X10;
  1253. else {
  1254. pr_err("%s: Invalid calibration property\n", __func__);
  1255. return -EINVAL;
  1256. }
  1257. tmdev = devm_kzalloc(&pdev->dev,
  1258. sizeof(struct tsens_tm_device) +
  1259. tsens_num_sensors *
  1260. sizeof(struct tsens_tm_device_sensor),
  1261. GFP_KERNEL);
  1262. if (tmdev == NULL) {
  1263. pr_err("%s: kzalloc() failed.\n", __func__);
  1264. return -ENOMEM;
  1265. }
  1266. for (i = 0; i < tsens_num_sensors; i++)
  1267. tmdev->sensor[i].slope_mul_tsens_factor = tsens_slope_data[i];
  1268. tmdev->tsens_factor = TSENS_SLOPE_FACTOR;
  1269. tmdev->tsens_num_sensor = tsens_num_sensors;
  1270. tmdev->calibration_less_mode = of_property_read_bool(of_node,
  1271. "qcom,calibration-less-mode");
  1272. tmdev->calib_mode = calib_type;
  1273. tmdev->tsens_local_init = of_property_read_bool(of_node,
  1274. "qcom,tsens-local-init");
  1275. sensor_id = devm_kzalloc(&pdev->dev,
  1276. tsens_num_sensors * sizeof(u32), GFP_KERNEL);
  1277. if (!sensor_id) {
  1278. dev_err(&pdev->dev, "can not allocate sensor id\n");
  1279. return -ENOMEM;
  1280. }
  1281. rc = of_property_read_u32_array(of_node,
  1282. "qcom,sensor-id", sensor_id, tsens_num_sensors);
  1283. if (rc) {
  1284. pr_debug("Default sensor id mapping\n");
  1285. for (i = 0; i < tsens_num_sensors; i++) {
  1286. tmdev->sensor[i].sensor_hw_num = i;
  1287. tmdev->sensor[i].sensor_sw_id = i;
  1288. }
  1289. } else {
  1290. pr_debug("Use specified sensor id mapping\n");
  1291. for (i = 0; i < tsens_num_sensors; i++) {
  1292. tmdev->sensor[i].sensor_hw_num = sensor_id[i];
  1293. tmdev->sensor[i].sensor_sw_id = i;
  1294. }
  1295. }
  1296. tmdev->tsens_irq = platform_get_irq(pdev, 0);
  1297. if (tmdev->tsens_irq < 0) {
  1298. pr_err("Invalid get irq\n");
  1299. rc = tmdev->tsens_irq;
  1300. goto fail_tmdev;
  1301. }
  1302. /* TSENS register region */
  1303. tmdev->res_tsens_mem = platform_get_resource_byname(pdev,
  1304. IORESOURCE_MEM, "tsens_physical");
  1305. if (!tmdev->res_tsens_mem) {
  1306. pr_err("Could not get tsens physical address resource\n");
  1307. rc = -EINVAL;
  1308. goto fail_tmdev;
  1309. }
  1310. tmdev->tsens_len = tmdev->res_tsens_mem->end -
  1311. tmdev->res_tsens_mem->start + 1;
  1312. res_mem = request_mem_region(tmdev->res_tsens_mem->start,
  1313. tmdev->tsens_len, tmdev->res_tsens_mem->name);
  1314. if (!res_mem) {
  1315. pr_err("Request tsens physical memory region failed\n");
  1316. rc = -EINVAL;
  1317. goto fail_tmdev;
  1318. }
  1319. tmdev->tsens_addr = ioremap(res_mem->start, tmdev->tsens_len);
  1320. if (!tmdev->tsens_addr) {
  1321. pr_err("Failed to IO map TSENS registers.\n");
  1322. rc = -EINVAL;
  1323. goto fail_unmap_tsens_region;
  1324. }
  1325. /* TSENS calibration region */
  1326. tmdev->res_calib_mem = platform_get_resource_byname(pdev,
  1327. IORESOURCE_MEM, "tsens_eeprom_physical");
  1328. if (!tmdev->res_calib_mem) {
  1329. pr_err("Could not get qfprom physical address resource\n");
  1330. rc = -EINVAL;
  1331. goto fail_unmap_tsens;
  1332. }
  1333. tmdev->calib_len = tmdev->res_calib_mem->end -
  1334. tmdev->res_calib_mem->start + 1;
  1335. res_mem = request_mem_region(tmdev->res_calib_mem->start,
  1336. tmdev->calib_len, tmdev->res_calib_mem->name);
  1337. if (!res_mem) {
  1338. pr_err("Request calibration memory region failed\n");
  1339. rc = -EINVAL;
  1340. goto fail_unmap_tsens;
  1341. }
  1342. tmdev->tsens_calib_addr = ioremap(res_mem->start,
  1343. tmdev->calib_len);
  1344. if (!tmdev->tsens_calib_addr) {
  1345. pr_err("Failed to IO map EEPROM registers.\n");
  1346. rc = -EINVAL;
  1347. goto fail_unmap_calib_region;
  1348. }
  1349. return 0;
  1350. fail_unmap_calib_region:
  1351. if (tmdev->res_calib_mem)
  1352. release_mem_region(tmdev->res_calib_mem->start,
  1353. tmdev->calib_len);
  1354. fail_unmap_tsens:
  1355. if (tmdev->tsens_addr)
  1356. iounmap(tmdev->tsens_addr);
  1357. fail_unmap_tsens_region:
  1358. if (tmdev->res_tsens_mem)
  1359. release_mem_region(tmdev->res_tsens_mem->start,
  1360. tmdev->tsens_len);
  1361. fail_tmdev:
  1362. tmdev = NULL;
  1363. return rc;
  1364. }
  1365. static int __devinit tsens_tm_probe(struct platform_device *pdev)
  1366. {
  1367. int rc;
  1368. if (tmdev) {
  1369. pr_err("TSENS device already in use\n");
  1370. return -EBUSY;
  1371. }
  1372. if (pdev->dev.of_node) {
  1373. rc = get_device_tree_data(pdev);
  1374. if (rc) {
  1375. pr_err("Error reading TSENS DT\n");
  1376. return rc;
  1377. }
  1378. } else
  1379. return -ENODEV;
  1380. tmdev->pdev = pdev;
  1381. tmdev->tsens_wq = alloc_workqueue("tsens_wq", WQ_HIGHPRI, 0);
  1382. if (!tmdev->tsens_wq) {
  1383. rc = -ENOMEM;
  1384. goto fail;
  1385. }
  1386. rc = tsens_calib_sensors();
  1387. if (rc < 0) {
  1388. pr_err("Calibration failed\n");
  1389. goto fail;
  1390. }
  1391. tsens_hw_init();
  1392. tmdev->prev_reading_avail = true;
  1393. platform_set_drvdata(pdev, tmdev);
  1394. return 0;
  1395. fail:
  1396. if (tmdev->tsens_wq)
  1397. destroy_workqueue(tmdev->tsens_wq);
  1398. if (tmdev->tsens_calib_addr)
  1399. iounmap(tmdev->tsens_calib_addr);
  1400. if (tmdev->res_calib_mem)
  1401. release_mem_region(tmdev->res_calib_mem->start,
  1402. tmdev->calib_len);
  1403. if (tmdev->tsens_addr)
  1404. iounmap(tmdev->tsens_addr);
  1405. if (tmdev->res_tsens_mem)
  1406. release_mem_region(tmdev->res_tsens_mem->start,
  1407. tmdev->tsens_len);
  1408. tmdev = NULL;
  1409. return rc;
  1410. }
  1411. static int __devinit _tsens_register_thermal(void)
  1412. {
  1413. struct platform_device *pdev;
  1414. int rc, i;
  1415. if (!tmdev) {
  1416. pr_err("%s: TSENS early init not done\n", __func__);
  1417. return -ENODEV;
  1418. }
  1419. pdev = tmdev->pdev;
  1420. for (i = 0; i < tmdev->tsens_num_sensor; i++) {
  1421. char name[18];
  1422. snprintf(name, sizeof(name), "tsens_tz_sensor%d",
  1423. tmdev->sensor[i].sensor_hw_num);
  1424. tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
  1425. tmdev->sensor[i].tz_dev = thermal_zone_device_register(name,
  1426. TSENS_TRIP_NUM, &tmdev->sensor[i],
  1427. &tsens_thermal_zone_ops, 0, 0, 0, 0);
  1428. if (IS_ERR(tmdev->sensor[i].tz_dev)) {
  1429. pr_err("%s: thermal_zone_device_register() failed.\n",
  1430. __func__);
  1431. rc = -ENODEV;
  1432. goto fail;
  1433. }
  1434. }
  1435. INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
  1436. rc = request_irq(tmdev->tsens_irq, tsens_isr,
  1437. IRQF_TRIGGER_RISING, "tsens_interrupt", tmdev);
  1438. if (rc < 0) {
  1439. pr_err("%s: request_irq FAIL: %d\n", __func__, rc);
  1440. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  1441. thermal_zone_device_unregister(tmdev->sensor[i].tz_dev);
  1442. goto fail;
  1443. } else {
  1444. enable_irq_wake(tmdev->tsens_irq);
  1445. }
  1446. platform_set_drvdata(pdev, tmdev);
  1447. return 0;
  1448. fail:
  1449. if (tmdev->tsens_calib_addr)
  1450. iounmap(tmdev->tsens_calib_addr);
  1451. if (tmdev->res_calib_mem)
  1452. release_mem_region(tmdev->res_calib_mem->start,
  1453. tmdev->calib_len);
  1454. if (tmdev->tsens_addr)
  1455. iounmap(tmdev->tsens_addr);
  1456. if (tmdev->res_tsens_mem)
  1457. release_mem_region(tmdev->res_tsens_mem->start,
  1458. tmdev->tsens_len);
  1459. return rc;
  1460. }
  1461. static int __devexit tsens_tm_remove(struct platform_device *pdev)
  1462. {
  1463. struct tsens_tm_device *tmdev = platform_get_drvdata(pdev);
  1464. int i;
  1465. for (i = 0; i < tmdev->tsens_num_sensor; i++)
  1466. thermal_zone_device_unregister(tmdev->sensor[i].tz_dev);
  1467. if (tmdev->tsens_calib_addr)
  1468. iounmap(tmdev->tsens_calib_addr);
  1469. if (tmdev->res_calib_mem)
  1470. release_mem_region(tmdev->res_calib_mem->start,
  1471. tmdev->calib_len);
  1472. if (tmdev->tsens_addr)
  1473. iounmap(tmdev->tsens_addr);
  1474. if (tmdev->res_tsens_mem)
  1475. release_mem_region(tmdev->res_tsens_mem->start,
  1476. tmdev->tsens_len);
  1477. free_irq(tmdev->tsens_irq, tmdev);
  1478. destroy_workqueue(tmdev->tsens_wq);
  1479. platform_set_drvdata(pdev, NULL);
  1480. return 0;
  1481. }
  1482. static struct of_device_id tsens_match[] = {
  1483. { .compatible = "qcom,msm-tsens",
  1484. },
  1485. {}
  1486. };
  1487. static struct platform_driver tsens_tm_driver = {
  1488. .probe = tsens_tm_probe,
  1489. .remove = tsens_tm_remove,
  1490. .driver = {
  1491. .name = "msm-tsens",
  1492. .owner = THIS_MODULE,
  1493. .of_match_table = tsens_match,
  1494. },
  1495. };
  1496. int __init tsens_tm_init_driver(void)
  1497. {
  1498. return platform_driver_register(&tsens_tm_driver);
  1499. }
  1500. static int __init tsens_thermal_register(void)
  1501. {
  1502. return _tsens_register_thermal();
  1503. }
  1504. module_init(tsens_thermal_register);
  1505. static void __exit _tsens_tm_remove(void)
  1506. {
  1507. platform_driver_unregister(&tsens_tm_driver);
  1508. }
  1509. module_exit(_tsens_tm_remove);
  1510. MODULE_ALIAS("platform:" TSENS_DRIVER_NAME);
  1511. MODULE_LICENSE("GPL v2");