qpnp-adc-current.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /* Copyright (c) 2012-2014, 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. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/of.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/mutex.h>
  20. #include <linux/types.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/module.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/spmi.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/wakelock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/completion.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/qpnp/qpnp-adc.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/wakelock.h>
  33. /* QPNP IADC register definition */
  34. #define QPNP_IADC_REVISION1 0x0
  35. #define QPNP_IADC_REVISION2 0x1
  36. #define QPNP_IADC_REVISION3 0x2
  37. #define QPNP_IADC_REVISION4 0x3
  38. #define QPNP_IADC_PERPH_TYPE 0x4
  39. #define QPNP_IADC_PERH_SUBTYPE 0x5
  40. #define QPNP_IADC_SUPPORTED_REVISION2 1
  41. #define QPNP_STATUS1 0x8
  42. #define QPNP_STATUS1_OP_MODE 4
  43. #define QPNP_STATUS1_MULTI_MEAS_EN BIT(3)
  44. #define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2)
  45. #define QPNP_STATUS1_REQ_STS BIT(1)
  46. #define QPNP_STATUS1_EOC BIT(0)
  47. #define QPNP_STATUS1_REQ_STS_EOC_MASK 0x3
  48. #define QPNP_STATUS2 0x9
  49. #define QPNP_STATUS2_CONV_SEQ_STATE_SHIFT 4
  50. #define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1)
  51. #define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0)
  52. #define QPNP_CONV_TIMEOUT_ERR 2
  53. #define QPNP_IADC_MODE_CTL 0x40
  54. #define QPNP_OP_MODE_SHIFT 4
  55. #define QPNP_USE_BMS_DATA BIT(4)
  56. #define QPNP_VADC_SYNCH_EN BIT(2)
  57. #define QPNP_OFFSET_RMV_EN BIT(1)
  58. #define QPNP_ADC_TRIM_EN BIT(0)
  59. #define QPNP_IADC_EN_CTL1 0x46
  60. #define QPNP_IADC_ADC_EN BIT(7)
  61. #define QPNP_ADC_CH_SEL_CTL 0x48
  62. #define QPNP_ADC_DIG_PARAM 0x50
  63. #define QPNP_ADC_CLK_SEL_MASK 0x3
  64. #define QPNP_ADC_DEC_RATIO_SEL_MASK 0xc
  65. #define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
  66. #define QPNP_CONV_REQ 0x52
  67. #define QPNP_CONV_REQ_SET BIT(7)
  68. #define QPNP_CONV_SEQ_CTL 0x54
  69. #define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4
  70. #define QPNP_CONV_SEQ_TRIG_CTL 0x55
  71. #define QPNP_FAST_AVG_CTL 0x5a
  72. #define QPNP_M0_LOW_THR_LSB 0x5c
  73. #define QPNP_M0_LOW_THR_MSB 0x5d
  74. #define QPNP_M0_HIGH_THR_LSB 0x5e
  75. #define QPNP_M0_HIGH_THR_MSB 0x5f
  76. #define QPNP_M1_LOW_THR_LSB 0x69
  77. #define QPNP_M1_LOW_THR_MSB 0x6a
  78. #define QPNP_M1_HIGH_THR_LSB 0x6b
  79. #define QPNP_M1_HIGH_THR_MSB 0x6c
  80. #define QPNP_DATA0 0x60
  81. #define QPNP_DATA1 0x61
  82. #define QPNP_CONV_TIMEOUT_ERR 2
  83. #define QPNP_IADC_SEC_ACCESS 0xD0
  84. #define QPNP_IADC_SEC_ACCESS_DATA 0xA5
  85. #define QPNP_IADC_MSB_OFFSET 0xF2
  86. #define QPNP_IADC_LSB_OFFSET 0xF3
  87. #define QPNP_IADC_NOMINAL_RSENSE 0xF4
  88. #define QPNP_IADC_ATE_GAIN_CALIB_OFFSET 0xF5
  89. #define QPNP_INT_TEST_VAL 0xE1
  90. #define QPNP_IADC_ADC_CH_SEL_CTL 0x48
  91. #define QPNP_IADC_ADC_CHX_SEL_SHIFT 3
  92. #define QPNP_IADC_ADC_DIG_PARAM 0x50
  93. #define QPNP_IADC_CLK_SEL_SHIFT 1
  94. #define QPNP_IADC_DEC_RATIO_SEL 3
  95. #define QPNP_IADC_CONV_REQUEST 0x52
  96. #define QPNP_IADC_CONV_REQ BIT(7)
  97. #define QPNP_IADC_DATA0 0x60
  98. #define QPNP_IADC_DATA1 0x61
  99. #define QPNP_ADC_CONV_TIME_MIN 2000
  100. #define QPNP_ADC_CONV_TIME_MAX 2100
  101. #define QPNP_ADC_ERR_COUNT 20
  102. #define QPNP_ADC_GAIN_NV 17857
  103. #define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0
  104. #define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000
  105. #define QPNP_IADC_NANO_VOLTS_FACTOR 1000000
  106. #define QPNP_IADC_CALIB_SECONDS 300000
  107. #define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625
  108. #define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000
  109. #define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00
  110. #define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff
  111. #define QPNP_BIT_SHIFT_8 8
  112. #define QPNP_RSENSE_MSB_SIGN_CHECK 0x80
  113. #define QPNP_ADC_COMPLETION_TIMEOUT HZ
  114. #define SMBB_BAT_IF_TRIM_CNST_RDS_MASK 0x7
  115. #define SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_0 0
  116. #define SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_2 2
  117. #define QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST 127
  118. #define QPNP_IADC_RSENSE_DEFAULT_VALUE 7800000
  119. #define QPNP_IADC_RSENSE_DEFAULT_TYPEB_GF 9000000
  120. #define QPNP_IADC_RSENSE_DEFAULT_TYPEB_SMIC 9700000
  121. struct qpnp_iadc_comp {
  122. bool ext_rsense;
  123. u8 id;
  124. u8 sys_gain;
  125. u8 revision_dig_major;
  126. u8 revision_ana_minor;
  127. };
  128. struct qpnp_iadc_chip {
  129. struct device *dev;
  130. struct qpnp_adc_drv *adc;
  131. int32_t rsense;
  132. bool external_rsense;
  133. bool default_internal_rsense;
  134. struct device *iadc_hwmon;
  135. struct list_head list;
  136. int64_t die_temp;
  137. struct delayed_work iadc_work;
  138. bool iadc_mode_sel;
  139. struct qpnp_iadc_comp iadc_comp;
  140. struct qpnp_vadc_chip *vadc_dev;
  141. struct work_struct trigger_completion_work;
  142. bool skip_auto_calibrations;
  143. bool iadc_poll_eoc;
  144. u16 batt_id_trim_cnst_rds;
  145. int rds_trim_default_type;
  146. int max_channels_available;
  147. bool rds_trim_default_check;
  148. int32_t rsense_workaround_value;
  149. struct sensor_device_attribute sens_attr[0];
  150. };
  151. LIST_HEAD(qpnp_iadc_device_list);
  152. enum qpnp_iadc_rsense_rds_workaround {
  153. QPNP_IADC_RDS_DEFAULT_TYPEA,
  154. QPNP_IADC_RDS_DEFAULT_TYPEB,
  155. QPNP_IADC_RDS_DEFAULT_TYPEC,
  156. };
  157. static int32_t qpnp_iadc_read_reg(struct qpnp_iadc_chip *iadc,
  158. uint32_t reg, u8 *data)
  159. {
  160. int rc;
  161. rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave,
  162. (iadc->adc->offset + reg), data, 1);
  163. if (rc < 0) {
  164. pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc);
  165. return rc;
  166. }
  167. return 0;
  168. }
  169. static int32_t qpnp_iadc_write_reg(struct qpnp_iadc_chip *iadc,
  170. uint32_t reg, u8 data)
  171. {
  172. int rc;
  173. u8 *buf;
  174. buf = &data;
  175. rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave,
  176. (iadc->adc->offset + reg), buf, 1);
  177. if (rc < 0) {
  178. pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc);
  179. return rc;
  180. }
  181. return 0;
  182. }
  183. static int qpnp_iadc_is_valid(struct qpnp_iadc_chip *iadc)
  184. {
  185. struct qpnp_iadc_chip *iadc_chip = NULL;
  186. list_for_each_entry(iadc_chip, &qpnp_iadc_device_list, list)
  187. if (iadc == iadc_chip)
  188. return 0;
  189. return -EINVAL;
  190. }
  191. static void qpnp_iadc_trigger_completion(struct work_struct *work)
  192. {
  193. struct qpnp_iadc_chip *iadc = container_of(work,
  194. struct qpnp_iadc_chip, trigger_completion_work);
  195. if (qpnp_iadc_is_valid(iadc) < 0)
  196. return;
  197. complete(&iadc->adc->adc_rslt_completion);
  198. return;
  199. }
  200. static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id)
  201. {
  202. struct qpnp_iadc_chip *iadc = dev_id;
  203. schedule_work(&iadc->trigger_completion_work);
  204. return IRQ_HANDLED;
  205. }
  206. static int32_t qpnp_iadc_enable(struct qpnp_iadc_chip *dev, bool state)
  207. {
  208. int rc = 0;
  209. u8 data = 0;
  210. data = QPNP_IADC_ADC_EN;
  211. if (state) {
  212. rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1,
  213. data);
  214. if (rc < 0) {
  215. pr_err("IADC enable failed\n");
  216. return rc;
  217. }
  218. } else {
  219. rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1,
  220. (~data & QPNP_IADC_ADC_EN));
  221. if (rc < 0) {
  222. pr_err("IADC disable failed\n");
  223. return rc;
  224. }
  225. }
  226. return 0;
  227. }
  228. static int32_t qpnp_iadc_status_debug(struct qpnp_iadc_chip *dev)
  229. {
  230. int rc = 0;
  231. u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0;
  232. rc = qpnp_iadc_read_reg(dev, QPNP_IADC_MODE_CTL, &mode);
  233. if (rc < 0) {
  234. pr_err("mode ctl register read failed with %d\n", rc);
  235. return rc;
  236. }
  237. rc = qpnp_iadc_read_reg(dev, QPNP_ADC_DIG_PARAM, &dig);
  238. if (rc < 0) {
  239. pr_err("digital param read failed with %d\n", rc);
  240. return rc;
  241. }
  242. rc = qpnp_iadc_read_reg(dev, QPNP_IADC_ADC_CH_SEL_CTL, &chan);
  243. if (rc < 0) {
  244. pr_err("channel read failed with %d\n", rc);
  245. return rc;
  246. }
  247. rc = qpnp_iadc_read_reg(dev, QPNP_STATUS1, &status1);
  248. if (rc < 0) {
  249. pr_err("status1 read failed with %d\n", rc);
  250. return rc;
  251. }
  252. rc = qpnp_iadc_read_reg(dev, QPNP_IADC_EN_CTL1, &en);
  253. if (rc < 0) {
  254. pr_err("en read failed with %d\n", rc);
  255. return rc;
  256. }
  257. pr_debug("EOC not set with status:%x, dig:%x, ch:%x, mode:%x, en:%x\n",
  258. status1, dig, chan, mode, en);
  259. rc = qpnp_iadc_enable(dev, false);
  260. if (rc < 0) {
  261. pr_err("IADC disable failed with %d\n", rc);
  262. return rc;
  263. }
  264. return 0;
  265. }
  266. static int32_t qpnp_iadc_read_conversion_result(struct qpnp_iadc_chip *iadc,
  267. int16_t *data)
  268. {
  269. uint8_t rslt_lsb, rslt_msb;
  270. uint16_t rslt;
  271. int32_t rc;
  272. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA0, &rslt_lsb);
  273. if (rc < 0) {
  274. pr_err("qpnp adc result read failed with %d\n", rc);
  275. return rc;
  276. }
  277. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA1, &rslt_msb);
  278. if (rc < 0) {
  279. pr_err("qpnp adc result read failed with %d\n", rc);
  280. return rc;
  281. }
  282. rslt = (rslt_msb << 8) | rslt_lsb;
  283. *data = rslt;
  284. rc = qpnp_iadc_enable(iadc, false);
  285. if (rc)
  286. return rc;
  287. return 0;
  288. }
  289. #define QPNP_IADC_PM8026_2_REV2 4
  290. #define QPNP_IADC_PM8026_2_REV3 2
  291. #define QPNP_COEFF_1 969000
  292. #define QPNP_COEFF_2 32
  293. #define QPNP_COEFF_3_TYPEA 1700000
  294. #define QPNP_COEFF_3_TYPEB 1000000
  295. #define QPNP_COEFF_4 100
  296. #define QPNP_COEFF_5 15
  297. #define QPNP_COEFF_6 100000
  298. #define QPNP_COEFF_7 21
  299. #define QPNP_COEFF_8 100000000
  300. #define QPNP_COEFF_9 38
  301. #define QPNP_COEFF_10 40
  302. #define QPNP_COEFF_11 7
  303. #define QPNP_COEFF_12 11
  304. #define QPNP_COEFF_13 37
  305. #define QPNP_COEFF_14 39
  306. #define QPNP_COEFF_15 9
  307. #define QPNP_COEFF_16 11
  308. #define QPNP_COEFF_17 851200
  309. #define QPNP_COEFF_18 296500
  310. #define QPNP_COEFF_19 222400
  311. #define QPNP_COEFF_20 813800
  312. #define QPNP_COEFF_21 1059100
  313. #define QPNP_COEFF_22 5000000
  314. #define QPNP_COEFF_23 3722500
  315. #define QPNP_COEFF_24 84
  316. #define QPNP_COEFF_25 33
  317. #define QPNP_COEFF_26 22
  318. #define QPNP_COEFF_27 53
  319. #define QPNP_COEFF_28 48
  320. static int32_t qpnp_iadc_comp(int64_t *result, struct qpnp_iadc_chip *iadc,
  321. int64_t die_temp)
  322. {
  323. int64_t temp_var = 0, sys_gain_coeff = 0, old;
  324. int32_t coeff_a = 0, coeff_b = 0;
  325. int version = 0;
  326. version = qpnp_adc_get_revid_version(iadc->dev);
  327. if (version == -EINVAL)
  328. return 0;
  329. old = *result;
  330. *result = *result * 1000000;
  331. if (iadc->iadc_comp.sys_gain > 127)
  332. sys_gain_coeff = -QPNP_COEFF_6 *
  333. (iadc->iadc_comp.sys_gain - 128);
  334. else
  335. sys_gain_coeff = QPNP_COEFF_6 *
  336. iadc->iadc_comp.sys_gain;
  337. switch (version) {
  338. case QPNP_REV_ID_8941_3_1:
  339. switch (iadc->iadc_comp.id) {
  340. case COMP_ID_GF:
  341. if (!iadc->iadc_comp.ext_rsense) {
  342. /* internal rsense */
  343. coeff_a = QPNP_COEFF_2;
  344. coeff_b = -QPNP_COEFF_3_TYPEA;
  345. } else {
  346. if (*result < 0) {
  347. /* charge */
  348. coeff_a = QPNP_COEFF_5;
  349. coeff_b = QPNP_COEFF_6;
  350. } else {
  351. /* discharge */
  352. coeff_a = -QPNP_COEFF_7;
  353. coeff_b = QPNP_COEFF_6;
  354. }
  355. }
  356. break;
  357. case COMP_ID_TSMC:
  358. default:
  359. if (!iadc->iadc_comp.ext_rsense) {
  360. /* internal rsense */
  361. coeff_a = QPNP_COEFF_2;
  362. coeff_b = -QPNP_COEFF_3_TYPEB;
  363. } else {
  364. if (*result < 0) {
  365. /* charge */
  366. coeff_a = QPNP_COEFF_5;
  367. coeff_b = QPNP_COEFF_6;
  368. } else {
  369. /* discharge */
  370. coeff_a = -QPNP_COEFF_7;
  371. coeff_b = QPNP_COEFF_6;
  372. }
  373. }
  374. break;
  375. }
  376. break;
  377. case QPNP_REV_ID_8026_2_1:
  378. case QPNP_REV_ID_8026_2_2:
  379. /* pm8026 rev 2.1 and 2.2 */
  380. switch (iadc->iadc_comp.id) {
  381. case COMP_ID_GF:
  382. if (!iadc->iadc_comp.ext_rsense) {
  383. /* internal rsense */
  384. if (*result < 0) {
  385. /* charge */
  386. coeff_a = 0;
  387. coeff_b = 0;
  388. } else {
  389. coeff_a = QPNP_COEFF_25;
  390. coeff_b = 0;
  391. }
  392. } else {
  393. if (*result < 0) {
  394. /* charge */
  395. coeff_a = 0;
  396. coeff_b = 0;
  397. } else {
  398. /* discharge */
  399. coeff_a = 0;
  400. coeff_b = 0;
  401. }
  402. }
  403. break;
  404. case COMP_ID_TSMC:
  405. default:
  406. if (!iadc->iadc_comp.ext_rsense) {
  407. /* internal rsense */
  408. if (*result < 0) {
  409. /* charge */
  410. coeff_a = 0;
  411. coeff_b = 0;
  412. } else {
  413. coeff_a = QPNP_COEFF_26;
  414. coeff_b = 0;
  415. }
  416. } else {
  417. if (*result < 0) {
  418. /* charge */
  419. coeff_a = 0;
  420. coeff_b = 0;
  421. } else {
  422. /* discharge */
  423. coeff_a = 0;
  424. coeff_b = 0;
  425. }
  426. }
  427. break;
  428. }
  429. break;
  430. case QPNP_REV_ID_8026_1_0:
  431. /* pm8026 rev 1.0 */
  432. switch (iadc->iadc_comp.id) {
  433. case COMP_ID_GF:
  434. if (!iadc->iadc_comp.ext_rsense) {
  435. /* internal rsense */
  436. if (*result < 0) {
  437. /* charge */
  438. coeff_a = QPNP_COEFF_9;
  439. coeff_b = -QPNP_COEFF_17;
  440. } else {
  441. coeff_a = QPNP_COEFF_10;
  442. coeff_b = QPNP_COEFF_18;
  443. }
  444. } else {
  445. if (*result < 0) {
  446. /* charge */
  447. coeff_a = -QPNP_COEFF_11;
  448. coeff_b = 0;
  449. } else {
  450. /* discharge */
  451. coeff_a = -QPNP_COEFF_17;
  452. coeff_b = -QPNP_COEFF_19;
  453. }
  454. }
  455. break;
  456. case COMP_ID_TSMC:
  457. default:
  458. if (!iadc->iadc_comp.ext_rsense) {
  459. /* internal rsense */
  460. if (*result < 0) {
  461. /* charge */
  462. coeff_a = QPNP_COEFF_13;
  463. coeff_b = -QPNP_COEFF_20;
  464. } else {
  465. coeff_a = QPNP_COEFF_14;
  466. coeff_b = QPNP_COEFF_21;
  467. }
  468. } else {
  469. if (*result < 0) {
  470. /* charge */
  471. coeff_a = -QPNP_COEFF_15;
  472. coeff_b = 0;
  473. } else {
  474. /* discharge */
  475. coeff_a = -QPNP_COEFF_12;
  476. coeff_b = -QPNP_COEFF_19;
  477. }
  478. }
  479. break;
  480. }
  481. break;
  482. case QPNP_REV_ID_8110_1_0:
  483. /* pm8110 rev 1.0 */
  484. switch (iadc->iadc_comp.id) {
  485. case COMP_ID_GF:
  486. if (!iadc->iadc_comp.ext_rsense) {
  487. /* internal rsense */
  488. if (*result < 0) {
  489. /* charge */
  490. coeff_a = QPNP_COEFF_24;
  491. coeff_b = -QPNP_COEFF_22;
  492. } else {
  493. coeff_a = QPNP_COEFF_24;
  494. coeff_b = -QPNP_COEFF_23;
  495. }
  496. }
  497. break;
  498. case COMP_ID_SMIC:
  499. default:
  500. if (!iadc->iadc_comp.ext_rsense) {
  501. /* internal rsense */
  502. if (*result < 0) {
  503. /* charge */
  504. coeff_a = QPNP_COEFF_24;
  505. coeff_b = -QPNP_COEFF_22;
  506. } else {
  507. coeff_a = QPNP_COEFF_24;
  508. coeff_b = -QPNP_COEFF_23;
  509. }
  510. }
  511. break;
  512. }
  513. break;
  514. case QPNP_REV_ID_8110_2_0:
  515. die_temp -= 25000;
  516. /* pm8110 rev 2.0 */
  517. switch (iadc->iadc_comp.id) {
  518. case COMP_ID_GF:
  519. if (!iadc->iadc_comp.ext_rsense) {
  520. /* internal rsense */
  521. if (*result < 0) {
  522. /* charge */
  523. coeff_a = 0;
  524. coeff_b = 0;
  525. } else {
  526. coeff_a = QPNP_COEFF_27;
  527. coeff_b = 0;
  528. }
  529. }
  530. break;
  531. case COMP_ID_SMIC:
  532. default:
  533. if (!iadc->iadc_comp.ext_rsense) {
  534. /* internal rsense */
  535. if (*result < 0) {
  536. /* charge */
  537. coeff_a = 0;
  538. coeff_b = 0;
  539. } else {
  540. coeff_a = QPNP_COEFF_28;
  541. coeff_b = 0;
  542. }
  543. }
  544. break;
  545. }
  546. break;
  547. default:
  548. case QPNP_REV_ID_8026_2_0:
  549. /* pm8026 rev 1.0 */
  550. coeff_a = 0;
  551. coeff_b = 0;
  552. break;
  553. }
  554. temp_var = (coeff_a * die_temp) + coeff_b;
  555. temp_var = div64_s64(temp_var, QPNP_COEFF_4);
  556. temp_var = 1000 * (1000000 - temp_var);
  557. if (!iadc->iadc_comp.ext_rsense) {
  558. /* internal rsense */
  559. *result = div64_s64(*result * 1000, temp_var);
  560. }
  561. if (iadc->iadc_comp.ext_rsense) {
  562. /* external rsense */
  563. sys_gain_coeff = (1000000 +
  564. div64_s64(sys_gain_coeff, QPNP_COEFF_4));
  565. temp_var = div64_s64(temp_var * sys_gain_coeff, 1000000);
  566. *result = div64_s64(*result * 1000, temp_var);
  567. }
  568. pr_debug("%lld compensated into %lld, a: %d, b: %d, sys_gain: %lld\n",
  569. old, *result, coeff_a, coeff_b, sys_gain_coeff);
  570. return 0;
  571. }
  572. int32_t qpnp_iadc_comp_result(struct qpnp_iadc_chip *iadc, int64_t *result)
  573. {
  574. return qpnp_iadc_comp(result, iadc, iadc->die_temp);
  575. }
  576. EXPORT_SYMBOL(qpnp_iadc_comp_result);
  577. static int qpnp_iadc_rds_trim_update_check(struct qpnp_iadc_chip *iadc)
  578. {
  579. int rc = 0;
  580. u8 trim2_val = 0, smbb_batt_trm_data = 0;
  581. u8 smbb_batt_trm_cnst_rds = 0;
  582. if (!iadc->rds_trim_default_check) {
  583. pr_debug("No internal rds trim check needed\n");
  584. return 0;
  585. }
  586. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &trim2_val);
  587. if (rc < 0) {
  588. pr_err("qpnp adc trim2_fullscale1 reg read failed %d\n", rc);
  589. return rc;
  590. }
  591. rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave,
  592. iadc->batt_id_trim_cnst_rds, &smbb_batt_trm_data, 1);
  593. if (rc < 0) {
  594. pr_err("batt_id trim_cnst rds reg read failed %d\n", rc);
  595. return rc;
  596. }
  597. smbb_batt_trm_cnst_rds = smbb_batt_trm_data &
  598. SMBB_BAT_IF_TRIM_CNST_RDS_MASK;
  599. pr_debug("n_trim:0x%x smb_trm:0x%x\n", trim2_val, smbb_batt_trm_data);
  600. if (iadc->rds_trim_default_type == QPNP_IADC_RDS_DEFAULT_TYPEA) {
  601. if ((smbb_batt_trm_cnst_rds ==
  602. SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_2) &&
  603. (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) {
  604. iadc->rsense_workaround_value =
  605. QPNP_IADC_RSENSE_DEFAULT_VALUE;
  606. iadc->default_internal_rsense = true;
  607. }
  608. } else if (iadc->rds_trim_default_type ==
  609. QPNP_IADC_RDS_DEFAULT_TYPEB) {
  610. if ((smbb_batt_trm_cnst_rds >=
  611. SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_2) &&
  612. (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) {
  613. iadc->rsense_workaround_value =
  614. QPNP_IADC_RSENSE_DEFAULT_VALUE;
  615. iadc->default_internal_rsense = true;
  616. } else if ((smbb_batt_trm_cnst_rds <
  617. SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_2) &&
  618. (trim2_val ==
  619. QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) {
  620. if (iadc->iadc_comp.id == COMP_ID_GF) {
  621. iadc->rsense_workaround_value =
  622. QPNP_IADC_RSENSE_DEFAULT_TYPEB_GF;
  623. iadc->default_internal_rsense = true;
  624. } else if (iadc->iadc_comp.id == COMP_ID_SMIC) {
  625. iadc->rsense_workaround_value =
  626. QPNP_IADC_RSENSE_DEFAULT_TYPEB_SMIC;
  627. iadc->default_internal_rsense = true;
  628. }
  629. }
  630. } else if (iadc->rds_trim_default_type == QPNP_IADC_RDS_DEFAULT_TYPEC) {
  631. if ((smbb_batt_trm_cnst_rds >
  632. SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_0) &&
  633. (smbb_batt_trm_cnst_rds <=
  634. SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST_2) &&
  635. (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) {
  636. iadc->rsense_workaround_value =
  637. QPNP_IADC_RSENSE_DEFAULT_VALUE;
  638. iadc->default_internal_rsense = true;
  639. }
  640. }
  641. return 0;
  642. }
  643. static int32_t qpnp_iadc_comp_info(struct qpnp_iadc_chip *iadc)
  644. {
  645. int rc = 0;
  646. rc = qpnp_iadc_read_reg(iadc, QPNP_INT_TEST_VAL, &iadc->iadc_comp.id);
  647. if (rc < 0) {
  648. pr_err("qpnp adc comp id failed with %d\n", rc);
  649. return rc;
  650. }
  651. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2,
  652. &iadc->iadc_comp.revision_dig_major);
  653. if (rc < 0) {
  654. pr_err("qpnp adc revision2 read failed with %d\n", rc);
  655. return rc;
  656. }
  657. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION3,
  658. &iadc->iadc_comp.revision_ana_minor);
  659. if (rc < 0) {
  660. pr_err("qpnp adc revision3 read failed with %d\n", rc);
  661. return rc;
  662. }
  663. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_ATE_GAIN_CALIB_OFFSET,
  664. &iadc->iadc_comp.sys_gain);
  665. if (rc < 0) {
  666. pr_err("full scale read failed with %d\n", rc);
  667. return rc;
  668. }
  669. if (iadc->external_rsense)
  670. iadc->iadc_comp.ext_rsense = true;
  671. pr_debug("fab id = %u, revision_dig_major = %u, revision_ana_minor = %u sys gain = %u, external_rsense = %d\n",
  672. iadc->iadc_comp.id,
  673. iadc->iadc_comp.revision_dig_major,
  674. iadc->iadc_comp.revision_ana_minor,
  675. iadc->iadc_comp.sys_gain,
  676. iadc->iadc_comp.ext_rsense);
  677. return rc;
  678. }
  679. static int32_t qpnp_iadc_configure(struct qpnp_iadc_chip *iadc,
  680. enum qpnp_iadc_channels channel,
  681. uint16_t *raw_code, uint32_t mode_sel)
  682. {
  683. u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0;
  684. u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0;
  685. u8 status1 = 0;
  686. uint32_t count = 0;
  687. int32_t rc = 0;
  688. qpnp_iadc_ch_sel_reg = channel;
  689. qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation <<
  690. QPNP_IADC_DEC_RATIO_SEL;
  691. if (iadc->iadc_mode_sel)
  692. qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN);
  693. else
  694. qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN;
  695. qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ;
  696. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg);
  697. if (rc) {
  698. pr_err("qpnp adc read adc failed with %d\n", rc);
  699. return rc;
  700. }
  701. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_ADC_CH_SEL_CTL,
  702. qpnp_iadc_ch_sel_reg);
  703. if (rc) {
  704. pr_err("qpnp adc read adc failed with %d\n", rc);
  705. return rc;
  706. }
  707. rc = qpnp_iadc_write_reg(iadc, QPNP_ADC_DIG_PARAM,
  708. qpnp_iadc_dig_param_reg);
  709. if (rc) {
  710. pr_err("qpnp adc read adc failed with %d\n", rc);
  711. return rc;
  712. }
  713. rc = qpnp_iadc_write_reg(iadc, QPNP_FAST_AVG_CTL,
  714. iadc->adc->amux_prop->fast_avg_setup);
  715. if (rc < 0) {
  716. pr_err("qpnp adc fast averaging configure error\n");
  717. return rc;
  718. }
  719. if (!iadc->iadc_poll_eoc)
  720. INIT_COMPLETION(iadc->adc->adc_rslt_completion);
  721. rc = qpnp_iadc_enable(iadc, true);
  722. if (rc)
  723. return rc;
  724. rc = qpnp_iadc_write_reg(iadc, QPNP_CONV_REQ, qpnp_iadc_conv_req);
  725. if (rc) {
  726. pr_err("qpnp adc read adc failed with %d\n", rc);
  727. return rc;
  728. }
  729. if (iadc->iadc_poll_eoc) {
  730. while (status1 != QPNP_STATUS1_EOC) {
  731. rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1);
  732. if (rc < 0)
  733. return rc;
  734. status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK;
  735. usleep_range(QPNP_ADC_CONV_TIME_MIN,
  736. QPNP_ADC_CONV_TIME_MAX);
  737. count++;
  738. if (count > QPNP_ADC_ERR_COUNT) {
  739. pr_err("retry error exceeded\n");
  740. rc = qpnp_iadc_status_debug(iadc);
  741. if (rc < 0)
  742. pr_err("IADC status debug failed\n");
  743. rc = -EINVAL;
  744. return rc;
  745. }
  746. }
  747. } else {
  748. rc = wait_for_completion_timeout(
  749. &iadc->adc->adc_rslt_completion,
  750. QPNP_ADC_COMPLETION_TIMEOUT);
  751. if (!rc) {
  752. rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1);
  753. if (rc < 0)
  754. return rc;
  755. status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK;
  756. if (status1 == QPNP_STATUS1_EOC)
  757. pr_debug("End of conversion status set\n");
  758. else {
  759. rc = qpnp_iadc_status_debug(iadc);
  760. if (rc < 0) {
  761. pr_err("status debug failed %d\n", rc);
  762. return rc;
  763. }
  764. return -EINVAL;
  765. }
  766. }
  767. }
  768. rc = qpnp_iadc_read_conversion_result(iadc, raw_code);
  769. if (rc) {
  770. pr_err("qpnp adc read adc failed with %d\n", rc);
  771. return rc;
  772. }
  773. return 0;
  774. }
  775. #define IADC_CENTER 0xC000
  776. #define IADC_READING_RESOLUTION_N 542535
  777. #define IADC_READING_RESOLUTION_D 100000
  778. static int32_t qpnp_convert_raw_offset_voltage(struct qpnp_iadc_chip *iadc)
  779. {
  780. s64 numerator;
  781. if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) {
  782. pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n",
  783. iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw);
  784. return -EINVAL;
  785. }
  786. numerator = iadc->adc->calib.offset_raw - IADC_CENTER;
  787. numerator *= IADC_READING_RESOLUTION_N;
  788. iadc->adc->calib.offset_uv = div_s64(numerator,
  789. IADC_READING_RESOLUTION_D);
  790. numerator = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw;
  791. numerator *= IADC_READING_RESOLUTION_N;
  792. iadc->adc->calib.gain_uv = div_s64(numerator,
  793. IADC_READING_RESOLUTION_D);
  794. pr_debug("gain_uv:%d offset_uv:%d\n",
  795. iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv);
  796. return 0;
  797. }
  798. #define IADC_IDEAL_RAW_GAIN 3291
  799. int32_t qpnp_iadc_calibrate_for_trim(struct qpnp_iadc_chip *iadc,
  800. bool batfet_closed)
  801. {
  802. uint8_t rslt_lsb, rslt_msb;
  803. int32_t rc = 0, version = 0;
  804. uint16_t raw_data;
  805. uint32_t mode_sel = 0;
  806. bool iadc_offset_ch_batfet_check;
  807. if (qpnp_iadc_is_valid(iadc) < 0)
  808. return -EPROBE_DEFER;
  809. mutex_lock(&iadc->adc->adc_lock);
  810. if (iadc->iadc_poll_eoc) {
  811. pr_debug("acquiring iadc eoc wakelock\n");
  812. pm_stay_awake(iadc->dev);
  813. }
  814. iadc->adc->amux_prop->decimation = DECIMATION_TYPE1;
  815. iadc->adc->amux_prop->fast_avg_setup = ADC_FAST_AVG_SAMPLE_1;
  816. rc = qpnp_iadc_configure(iadc, GAIN_CALIBRATION_17P857MV,
  817. &raw_data, mode_sel);
  818. if (rc < 0) {
  819. pr_err("qpnp adc result read failed with %d\n", rc);
  820. goto fail;
  821. }
  822. iadc->adc->calib.gain_raw = raw_data;
  823. /*
  824. * there is a features on PM8941 in the BMS where if the batfet is
  825. * opened the BMS reads from INTERNAL_RSENSE (channel 0) actually go to
  826. * OFFSET_CALIBRATION_CSP_CSN (channel 5). Hence if batfet is opened
  827. * we have to calibrate based on OFFSET_CALIBRATION_CSP_CSN even for
  828. * internal rsense.
  829. */
  830. version = qpnp_adc_get_revid_version(iadc->dev);
  831. if ((version == QPNP_REV_ID_8941_3_1) ||
  832. (version == QPNP_REV_ID_8941_3_0) ||
  833. (version == QPNP_REV_ID_8941_2_0))
  834. iadc_offset_ch_batfet_check = true;
  835. else
  836. iadc_offset_ch_batfet_check = false;
  837. if ((iadc_offset_ch_batfet_check && !batfet_closed) ||
  838. (iadc->external_rsense)) {
  839. /* external offset calculation */
  840. rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP_CSN,
  841. &raw_data, mode_sel);
  842. if (rc < 0) {
  843. pr_err("qpnp adc result read failed with %d\n", rc);
  844. goto fail;
  845. }
  846. } else {
  847. /* internal offset calculation */
  848. rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP2_CSN2,
  849. &raw_data, mode_sel);
  850. if (rc < 0) {
  851. pr_err("qpnp adc result read failed with %d\n", rc);
  852. goto fail;
  853. }
  854. }
  855. iadc->adc->calib.offset_raw = raw_data;
  856. if (rc < 0) {
  857. pr_err("qpnp adc offset/gain calculation failed\n");
  858. goto fail;
  859. }
  860. if (iadc->iadc_comp.revision_dig_major == QPNP_IADC_PM8026_2_REV2
  861. && iadc->iadc_comp.revision_ana_minor ==
  862. QPNP_IADC_PM8026_2_REV3)
  863. iadc->adc->calib.gain_raw =
  864. iadc->adc->calib.offset_raw + IADC_IDEAL_RAW_GAIN;
  865. pr_debug("raw gain:0x%x, raw offset:0x%x\n",
  866. iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw);
  867. rc = qpnp_convert_raw_offset_voltage(iadc);
  868. if (rc < 0) {
  869. pr_err("qpnp raw_voltage conversion failed\n");
  870. goto fail;
  871. }
  872. rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >>
  873. QPNP_BIT_SHIFT_8;
  874. rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK;
  875. pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb);
  876. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS,
  877. QPNP_IADC_SEC_ACCESS_DATA);
  878. if (rc < 0) {
  879. pr_err("qpnp iadc configure error for sec access\n");
  880. goto fail;
  881. }
  882. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MSB_OFFSET,
  883. rslt_msb);
  884. if (rc < 0) {
  885. pr_err("qpnp iadc configure error for MSB write\n");
  886. goto fail;
  887. }
  888. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS,
  889. QPNP_IADC_SEC_ACCESS_DATA);
  890. if (rc < 0) {
  891. pr_err("qpnp iadc configure error for sec access\n");
  892. goto fail;
  893. }
  894. rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_LSB_OFFSET,
  895. rslt_lsb);
  896. if (rc < 0) {
  897. pr_err("qpnp iadc configure error for LSB write\n");
  898. goto fail;
  899. }
  900. fail:
  901. if (iadc->iadc_poll_eoc) {
  902. pr_debug("releasing iadc eoc wakelock\n");
  903. pm_relax(iadc->dev);
  904. }
  905. mutex_unlock(&iadc->adc->adc_lock);
  906. return rc;
  907. }
  908. EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim);
  909. static void qpnp_iadc_work(struct work_struct *work)
  910. {
  911. struct qpnp_iadc_chip *iadc = container_of(work,
  912. struct qpnp_iadc_chip, iadc_work.work);
  913. int rc = 0;
  914. if (!iadc->skip_auto_calibrations) {
  915. rc = qpnp_iadc_calibrate_for_trim(iadc, true);
  916. if (rc)
  917. pr_debug("periodic IADC calibration failed\n");
  918. }
  919. schedule_delayed_work(&iadc->iadc_work,
  920. round_jiffies_relative(msecs_to_jiffies
  921. (QPNP_IADC_CALIB_SECONDS)));
  922. return;
  923. }
  924. static int32_t qpnp_iadc_version_check(struct qpnp_iadc_chip *iadc)
  925. {
  926. uint8_t revision;
  927. int rc;
  928. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, &revision);
  929. if (rc < 0) {
  930. pr_err("qpnp adc result read failed with %d\n", rc);
  931. return rc;
  932. }
  933. if (revision < QPNP_IADC_SUPPORTED_REVISION2) {
  934. pr_err("IADC Version not supported\n");
  935. return -EINVAL;
  936. }
  937. return 0;
  938. }
  939. struct qpnp_iadc_chip *qpnp_get_iadc(struct device *dev, const char *name)
  940. {
  941. struct qpnp_iadc_chip *iadc;
  942. struct device_node *node = NULL;
  943. char prop_name[QPNP_MAX_PROP_NAME_LEN];
  944. snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-iadc", name);
  945. node = of_parse_phandle(dev->of_node, prop_name, 0);
  946. if (node == NULL)
  947. return ERR_PTR(-ENODEV);
  948. list_for_each_entry(iadc, &qpnp_iadc_device_list, list)
  949. if (iadc->adc->spmi->dev.of_node == node)
  950. return iadc;
  951. return ERR_PTR(-EPROBE_DEFER);
  952. }
  953. EXPORT_SYMBOL(qpnp_get_iadc);
  954. int32_t qpnp_iadc_get_rsense(struct qpnp_iadc_chip *iadc, int32_t *rsense)
  955. {
  956. uint8_t rslt_rsense = 0;
  957. int32_t rc = 0, sign_bit = 0;
  958. if (qpnp_iadc_is_valid(iadc) < 0)
  959. return -EPROBE_DEFER;
  960. if (iadc->external_rsense) {
  961. *rsense = iadc->rsense;
  962. } else if (iadc->default_internal_rsense) {
  963. *rsense = iadc->rsense_workaround_value;
  964. } else {
  965. rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE,
  966. &rslt_rsense);
  967. if (rc < 0) {
  968. pr_err("qpnp adc rsense read failed with %d\n", rc);
  969. return rc;
  970. }
  971. pr_debug("rsense:0%x\n", rslt_rsense);
  972. if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK)
  973. sign_bit = 1;
  974. rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK;
  975. if (sign_bit)
  976. *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR -
  977. (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
  978. else
  979. *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR +
  980. (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
  981. }
  982. pr_debug("rsense value is %d\n", *rsense);
  983. if (*rsense == 0)
  984. pr_err("incorrect rsens value:%d rslt_rsense:%d\n",
  985. *rsense, rslt_rsense);
  986. return rc;
  987. }
  988. EXPORT_SYMBOL(qpnp_iadc_get_rsense);
  989. static int32_t qpnp_check_pmic_temp(struct qpnp_iadc_chip *iadc)
  990. {
  991. struct qpnp_vadc_result result_pmic_therm;
  992. int64_t die_temp_offset;
  993. int rc = 0;
  994. rc = qpnp_vadc_read(iadc->vadc_dev, DIE_TEMP, &result_pmic_therm);
  995. if (rc < 0)
  996. return rc;
  997. die_temp_offset = result_pmic_therm.physical -
  998. iadc->die_temp;
  999. if (die_temp_offset < 0)
  1000. die_temp_offset = -die_temp_offset;
  1001. if (die_temp_offset > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) {
  1002. iadc->die_temp = result_pmic_therm.physical;
  1003. if (!iadc->skip_auto_calibrations) {
  1004. rc = qpnp_iadc_calibrate_for_trim(iadc, true);
  1005. if (rc)
  1006. pr_err("IADC calibration failed rc = %d\n", rc);
  1007. }
  1008. }
  1009. return rc;
  1010. }
  1011. int32_t qpnp_iadc_read(struct qpnp_iadc_chip *iadc,
  1012. enum qpnp_iadc_channels channel,
  1013. struct qpnp_iadc_result *result)
  1014. {
  1015. int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0;
  1016. int32_t rsense_u_ohms = 0;
  1017. int64_t result_current;
  1018. uint16_t raw_data;
  1019. int dt_index = 0;
  1020. if (qpnp_iadc_is_valid(iadc) < 0)
  1021. return -EPROBE_DEFER;
  1022. if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) {
  1023. pr_err("raw offset errors! run iadc calibration again\n");
  1024. return -EINVAL;
  1025. }
  1026. rc = qpnp_check_pmic_temp(iadc);
  1027. if (rc) {
  1028. pr_err("Error checking pmic therm temp\n");
  1029. return rc;
  1030. }
  1031. mutex_lock(&iadc->adc->adc_lock);
  1032. while (((enum qpnp_iadc_channels)
  1033. iadc->adc->adc_channels[dt_index].channel_num
  1034. != channel) && (dt_index < iadc->max_channels_available))
  1035. dt_index++;
  1036. if (dt_index >= iadc->max_channels_available) {
  1037. pr_err("not a valid IADC channel\n");
  1038. rc = -EINVAL;
  1039. goto fail;
  1040. }
  1041. iadc->adc->amux_prop->decimation =
  1042. iadc->adc->adc_channels[dt_index].adc_decimation;
  1043. iadc->adc->amux_prop->fast_avg_setup =
  1044. iadc->adc->adc_channels[dt_index].fast_avg_setup;
  1045. if (iadc->iadc_poll_eoc) {
  1046. pr_debug("acquiring iadc eoc wakelock\n");
  1047. pm_stay_awake(iadc->dev);
  1048. }
  1049. rc = qpnp_iadc_configure(iadc, channel, &raw_data, mode_sel);
  1050. if (rc < 0) {
  1051. pr_err("qpnp adc result read failed with %d\n", rc);
  1052. goto fail;
  1053. }
  1054. rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms);
  1055. pr_debug("current raw:0%x and rsense:%d\n",
  1056. raw_data, rsense_n_ohms);
  1057. rsense_u_ohms = rsense_n_ohms/1000;
  1058. num = raw_data - iadc->adc->calib.offset_raw;
  1059. if (num < 0) {
  1060. sign = 1;
  1061. num = -num;
  1062. }
  1063. result->result_uv = (num * QPNP_ADC_GAIN_NV)/
  1064. (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
  1065. result_current = result->result_uv;
  1066. result_current *= QPNP_IADC_NANO_VOLTS_FACTOR;
  1067. /* Intentional fall through. Process the result w/o comp */
  1068. do_div(result_current, rsense_u_ohms);
  1069. if (sign) {
  1070. result->result_uv = -result->result_uv;
  1071. result_current = -result_current;
  1072. }
  1073. result_current *= -1;
  1074. rc = qpnp_iadc_comp_result(iadc, &result_current);
  1075. if (rc < 0)
  1076. pr_err("Error during compensating the IADC\n");
  1077. rc = 0;
  1078. result_current *= -1;
  1079. result->result_ua = (int32_t) result_current;
  1080. fail:
  1081. if (iadc->iadc_poll_eoc) {
  1082. pr_debug("releasing iadc eoc wakelock\n");
  1083. pm_relax(iadc->dev);
  1084. }
  1085. mutex_unlock(&iadc->adc->adc_lock);
  1086. return rc;
  1087. }
  1088. EXPORT_SYMBOL(qpnp_iadc_read);
  1089. int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_chip *iadc,
  1090. struct qpnp_iadc_calib *result)
  1091. {
  1092. int rc;
  1093. if (qpnp_iadc_is_valid(iadc) < 0)
  1094. return -EPROBE_DEFER;
  1095. rc = qpnp_check_pmic_temp(iadc);
  1096. if (rc) {
  1097. pr_err("Error checking pmic therm temp\n");
  1098. return rc;
  1099. }
  1100. mutex_lock(&iadc->adc->adc_lock);
  1101. result->gain_raw = iadc->adc->calib.gain_raw;
  1102. result->ideal_gain_nv = QPNP_ADC_GAIN_NV;
  1103. result->gain_uv = iadc->adc->calib.gain_uv;
  1104. result->offset_raw = iadc->adc->calib.offset_raw;
  1105. result->ideal_offset_uv =
  1106. QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL;
  1107. result->offset_uv = iadc->adc->calib.offset_uv;
  1108. pr_debug("raw gain:0%x, raw offset:0%x\n",
  1109. result->gain_raw, result->offset_raw);
  1110. pr_debug("gain_uv:%d offset_uv:%d\n",
  1111. result->gain_uv, result->offset_uv);
  1112. mutex_unlock(&iadc->adc->adc_lock);
  1113. return 0;
  1114. }
  1115. EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset);
  1116. int qpnp_iadc_skip_calibration(struct qpnp_iadc_chip *iadc)
  1117. {
  1118. iadc->skip_auto_calibrations = true;
  1119. return 0;
  1120. }
  1121. EXPORT_SYMBOL(qpnp_iadc_skip_calibration);
  1122. int qpnp_iadc_resume_calibration(struct qpnp_iadc_chip *iadc)
  1123. {
  1124. iadc->skip_auto_calibrations = false;
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL(qpnp_iadc_resume_calibration);
  1128. int32_t qpnp_iadc_vadc_sync_read(struct qpnp_iadc_chip *iadc,
  1129. enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result,
  1130. enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result)
  1131. {
  1132. int rc = 0, mode_sel = 0, num = 0, rsense_n_ohms = 0, sign = 0;
  1133. int dt_index = 0;
  1134. uint16_t raw_data;
  1135. int32_t rsense_u_ohms = 0;
  1136. int64_t result_current;
  1137. if (qpnp_iadc_is_valid(iadc) < 0)
  1138. return -EPROBE_DEFER;
  1139. if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) {
  1140. pr_err("raw offset errors! run iadc calibration again\n");
  1141. return -EINVAL;
  1142. }
  1143. mutex_lock(&iadc->adc->adc_lock);
  1144. if (iadc->iadc_poll_eoc) {
  1145. pr_debug("acquiring iadc eoc wakelock\n");
  1146. pm_stay_awake(iadc->dev);
  1147. }
  1148. iadc->iadc_mode_sel = true;
  1149. rc = qpnp_vadc_iadc_sync_request(iadc->vadc_dev, v_channel);
  1150. if (rc) {
  1151. pr_err("Configuring VADC failed\n");
  1152. goto fail;
  1153. }
  1154. while (((enum qpnp_iadc_channels)
  1155. iadc->adc->adc_channels[dt_index].channel_num
  1156. != i_channel) && (dt_index < iadc->max_channels_available))
  1157. dt_index++;
  1158. if (dt_index >= iadc->max_channels_available) {
  1159. pr_err("not a valid IADC channel\n");
  1160. rc = -EINVAL;
  1161. goto fail;
  1162. }
  1163. iadc->adc->amux_prop->decimation =
  1164. iadc->adc->adc_channels[dt_index].adc_decimation;
  1165. iadc->adc->amux_prop->fast_avg_setup =
  1166. iadc->adc->adc_channels[dt_index].fast_avg_setup;
  1167. rc = qpnp_iadc_configure(iadc, i_channel, &raw_data, mode_sel);
  1168. if (rc < 0) {
  1169. pr_err("qpnp adc result read failed with %d\n", rc);
  1170. goto fail_release_vadc;
  1171. }
  1172. rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms);
  1173. pr_debug("current raw:0%x and rsense:%d\n",
  1174. raw_data, rsense_n_ohms);
  1175. rsense_u_ohms = rsense_n_ohms/1000;
  1176. num = raw_data - iadc->adc->calib.offset_raw;
  1177. if (num < 0) {
  1178. sign = 1;
  1179. num = -num;
  1180. }
  1181. i_result->result_uv = (num * QPNP_ADC_GAIN_NV)/
  1182. (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
  1183. result_current = i_result->result_uv;
  1184. result_current *= QPNP_IADC_NANO_VOLTS_FACTOR;
  1185. /* Intentional fall through. Process the result w/o comp */
  1186. if (!rsense_u_ohms) {
  1187. pr_err("rsense error=%d\n", rsense_u_ohms);
  1188. goto fail_release_vadc;
  1189. }
  1190. do_div(result_current, rsense_u_ohms);
  1191. if (sign) {
  1192. i_result->result_uv = -i_result->result_uv;
  1193. result_current = -result_current;
  1194. }
  1195. result_current *= -1;
  1196. rc = qpnp_iadc_comp_result(iadc, &result_current);
  1197. if (rc < 0)
  1198. pr_err("Error during compensating the IADC\n");
  1199. rc = 0;
  1200. result_current *= -1;
  1201. i_result->result_ua = (int32_t) result_current;
  1202. fail_release_vadc:
  1203. rc = qpnp_vadc_iadc_sync_complete_request(iadc->vadc_dev, v_channel,
  1204. v_result);
  1205. if (rc)
  1206. pr_err("Releasing VADC failed\n");
  1207. fail:
  1208. iadc->iadc_mode_sel = false;
  1209. if (iadc->iadc_poll_eoc) {
  1210. pr_debug("releasing iadc eoc wakelock\n");
  1211. pm_relax(iadc->dev);
  1212. }
  1213. mutex_unlock(&iadc->adc->adc_lock);
  1214. return rc;
  1215. }
  1216. EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read);
  1217. static ssize_t qpnp_iadc_show(struct device *dev,
  1218. struct device_attribute *devattr, char *buf)
  1219. {
  1220. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  1221. struct qpnp_iadc_chip *iadc = dev_get_drvdata(dev);
  1222. struct qpnp_iadc_result result;
  1223. int rc = -1;
  1224. rc = qpnp_iadc_read(iadc, attr->index, &result);
  1225. if (rc)
  1226. return 0;
  1227. return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH,
  1228. "Result:%d\n", result.result_ua);
  1229. }
  1230. static struct sensor_device_attribute qpnp_adc_attr =
  1231. SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0);
  1232. static int32_t qpnp_iadc_init_hwmon(struct qpnp_iadc_chip *iadc,
  1233. struct spmi_device *spmi)
  1234. {
  1235. struct device_node *child;
  1236. struct device_node *node = spmi->dev.of_node;
  1237. int rc = 0, i = 0, channel;
  1238. for_each_child_of_node(node, child) {
  1239. channel = iadc->adc->adc_channels[i].channel_num;
  1240. qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num;
  1241. qpnp_adc_attr.dev_attr.attr.name =
  1242. iadc->adc->adc_channels[i].name;
  1243. memcpy(&iadc->sens_attr[i], &qpnp_adc_attr,
  1244. sizeof(qpnp_adc_attr));
  1245. sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr);
  1246. rc = device_create_file(&spmi->dev,
  1247. &iadc->sens_attr[i].dev_attr);
  1248. if (rc) {
  1249. dev_err(&spmi->dev,
  1250. "device_create_file failed for dev %s\n",
  1251. iadc->adc->adc_channels[i].name);
  1252. goto hwmon_err_sens;
  1253. }
  1254. i++;
  1255. }
  1256. return 0;
  1257. hwmon_err_sens:
  1258. pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc);
  1259. return rc;
  1260. }
  1261. static int __devinit qpnp_iadc_probe(struct spmi_device *spmi)
  1262. {
  1263. struct qpnp_iadc_chip *iadc;
  1264. struct qpnp_adc_drv *adc_qpnp;
  1265. struct device_node *node = spmi->dev.of_node;
  1266. struct device_node *child;
  1267. struct resource *res;
  1268. int rc, count_adc_channel_list = 0, i = 0;
  1269. for_each_child_of_node(node, child)
  1270. count_adc_channel_list++;
  1271. if (!count_adc_channel_list) {
  1272. pr_err("No channel listing\n");
  1273. return -EINVAL;
  1274. }
  1275. iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_chip) +
  1276. (sizeof(struct sensor_device_attribute) *
  1277. count_adc_channel_list), GFP_KERNEL);
  1278. if (!iadc) {
  1279. dev_err(&spmi->dev, "Unable to allocate memory\n");
  1280. return -ENOMEM;
  1281. }
  1282. adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv),
  1283. GFP_KERNEL);
  1284. if (!adc_qpnp) {
  1285. dev_err(&spmi->dev, "Unable to allocate memory\n");
  1286. return -ENOMEM;
  1287. }
  1288. iadc->dev = &(spmi->dev);
  1289. iadc->adc = adc_qpnp;
  1290. rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc);
  1291. if (rc) {
  1292. dev_err(&spmi->dev, "failed to read device tree\n");
  1293. return rc;
  1294. }
  1295. res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM,
  1296. "batt-id-trim-cnst-rds");
  1297. if (!res) {
  1298. dev_err(&spmi->dev, "failed to read batt_id trim register\n");
  1299. return -EINVAL;
  1300. }
  1301. iadc->batt_id_trim_cnst_rds = res->start;
  1302. rc = of_property_read_u32(node, "qcom,use-default-rds-trim",
  1303. &iadc->rds_trim_default_type);
  1304. if (rc)
  1305. pr_debug("No trim workaround needed\n");
  1306. else {
  1307. pr_debug("Use internal RDS trim workaround\n");
  1308. iadc->rds_trim_default_check = true;
  1309. }
  1310. iadc->vadc_dev = qpnp_get_vadc(&spmi->dev, "iadc");
  1311. if (IS_ERR(iadc->vadc_dev)) {
  1312. rc = PTR_ERR(iadc->vadc_dev);
  1313. if (rc != -EPROBE_DEFER)
  1314. pr_err("vadc property missing, rc=%d\n", rc);
  1315. return rc;
  1316. }
  1317. mutex_init(&iadc->adc->adc_lock);
  1318. rc = of_property_read_u32(node, "qcom,rsense",
  1319. &iadc->rsense);
  1320. if (rc)
  1321. pr_debug("Defaulting to internal rsense\n");
  1322. else {
  1323. pr_debug("Use external rsense\n");
  1324. iadc->external_rsense = true;
  1325. }
  1326. iadc->iadc_poll_eoc = of_property_read_bool(node,
  1327. "qcom,iadc-poll-eoc");
  1328. if (!iadc->iadc_poll_eoc) {
  1329. rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc,
  1330. qpnp_iadc_isr, IRQF_TRIGGER_RISING,
  1331. "qpnp_iadc_interrupt", iadc);
  1332. if (rc) {
  1333. dev_err(&spmi->dev, "failed to request adc irq\n");
  1334. return rc;
  1335. } else
  1336. enable_irq_wake(iadc->adc->adc_irq_eoc);
  1337. }
  1338. rc = qpnp_iadc_init_hwmon(iadc, spmi);
  1339. if (rc) {
  1340. dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n");
  1341. return rc;
  1342. }
  1343. iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev);
  1344. rc = qpnp_iadc_version_check(iadc);
  1345. if (rc) {
  1346. dev_err(&spmi->dev, "IADC version not supported\n");
  1347. goto fail;
  1348. }
  1349. iadc->max_channels_available = count_adc_channel_list;
  1350. INIT_WORK(&iadc->trigger_completion_work, qpnp_iadc_trigger_completion);
  1351. INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work);
  1352. rc = qpnp_iadc_comp_info(iadc);
  1353. if (rc) {
  1354. dev_err(&spmi->dev, "abstracting IADC comp info failed!\n");
  1355. goto fail;
  1356. }
  1357. rc = qpnp_iadc_rds_trim_update_check(iadc);
  1358. if (rc) {
  1359. dev_err(&spmi->dev, "Rds trim update failed!\n");
  1360. goto fail;
  1361. }
  1362. dev_set_drvdata(&spmi->dev, iadc);
  1363. list_add(&iadc->list, &qpnp_iadc_device_list);
  1364. rc = qpnp_iadc_calibrate_for_trim(iadc, true);
  1365. if (rc)
  1366. dev_err(&spmi->dev, "failed to calibrate for USR trim\n");
  1367. if (iadc->iadc_poll_eoc)
  1368. device_init_wakeup(iadc->dev, 1);
  1369. schedule_delayed_work(&iadc->iadc_work,
  1370. round_jiffies_relative(msecs_to_jiffies
  1371. (QPNP_IADC_CALIB_SECONDS)));
  1372. return 0;
  1373. fail:
  1374. for_each_child_of_node(node, child) {
  1375. device_remove_file(&spmi->dev,
  1376. &iadc->sens_attr[i].dev_attr);
  1377. i++;
  1378. }
  1379. hwmon_device_unregister(iadc->iadc_hwmon);
  1380. mutex_destroy(&iadc->adc->adc_lock);
  1381. return rc;
  1382. }
  1383. static int __devexit qpnp_iadc_remove(struct spmi_device *spmi)
  1384. {
  1385. struct qpnp_iadc_chip *iadc = dev_get_drvdata(&spmi->dev);
  1386. struct device_node *node = spmi->dev.of_node;
  1387. struct device_node *child;
  1388. int i = 0;
  1389. cancel_delayed_work(&iadc->iadc_work);
  1390. for_each_child_of_node(node, child) {
  1391. device_remove_file(&spmi->dev,
  1392. &iadc->sens_attr[i].dev_attr);
  1393. i++;
  1394. }
  1395. hwmon_device_unregister(iadc->iadc_hwmon);
  1396. if (iadc->iadc_poll_eoc)
  1397. pm_relax(iadc->dev);
  1398. dev_set_drvdata(&spmi->dev, NULL);
  1399. return 0;
  1400. }
  1401. static const struct of_device_id qpnp_iadc_match_table[] = {
  1402. { .compatible = "qcom,qpnp-iadc",
  1403. },
  1404. {}
  1405. };
  1406. static struct spmi_driver qpnp_iadc_driver = {
  1407. .driver = {
  1408. .name = "qcom,qpnp-iadc",
  1409. .of_match_table = qpnp_iadc_match_table,
  1410. },
  1411. .probe = qpnp_iadc_probe,
  1412. .remove = qpnp_iadc_remove,
  1413. };
  1414. static int __init qpnp_iadc_init(void)
  1415. {
  1416. return spmi_driver_register(&qpnp_iadc_driver);
  1417. }
  1418. module_init(qpnp_iadc_init);
  1419. static void __exit qpnp_iadc_exit(void)
  1420. {
  1421. spmi_driver_unregister(&qpnp_iadc_driver);
  1422. }
  1423. module_exit(qpnp_iadc_exit);
  1424. MODULE_DESCRIPTION("QPNP PMIC current ADC driver");
  1425. MODULE_LICENSE("GPL v2");