pm8xxx-ccadc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /* Copyright (c) 2011-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/moduleparam.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/errno.h>
  18. #include <linux/mfd/pm8xxx/core.h>
  19. #include <linux/mfd/pm8xxx/pm8xxx-adc.h>
  20. #include <linux/mfd/pm8xxx/ccadc.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/ioport.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/rtc.h>
  28. #define CCADC_ANA_PARAM 0x240
  29. #define CCADC_DIG_PARAM 0x241
  30. #define CCADC_RSV 0x242
  31. #define CCADC_DATA0 0x244
  32. #define CCADC_DATA1 0x245
  33. #define CCADC_OFFSET_TRIM1 0x34A
  34. #define CCADC_OFFSET_TRIM0 0x34B
  35. #define CCADC_FULLSCALE_TRIM1 0x34C
  36. #define CCADC_FULLSCALE_TRIM0 0x34D
  37. /* note : TRIM1 is the msb and TRIM0 is the lsb */
  38. #define ADC_ARB_SECP_CNTRL 0x190
  39. #define ADC_ARB_SECP_AMUX_CNTRL 0x191
  40. #define ADC_ARB_SECP_ANA_PARAM 0x192
  41. #define ADC_ARB_SECP_DIG_PARAM 0x193
  42. #define ADC_ARB_SECP_RSV 0x194
  43. #define ADC_ARB_SECP_DATA1 0x195
  44. #define ADC_ARB_SECP_DATA0 0x196
  45. #define ADC_ARB_BMS_CNTRL 0x18D
  46. #define START_CONV_BIT BIT(7)
  47. #define EOC_CONV_BIT BIT(6)
  48. #define SEL_CCADC_BIT BIT(1)
  49. #define EN_ARB_BIT BIT(0)
  50. #define CCADC_CALIB_DIG_PARAM 0xE3
  51. #define CCADC_CALIB_RSV_GND 0x40
  52. #define CCADC_CALIB_RSV_25MV 0x80
  53. #define CCADC_CALIB_ANA_PARAM 0x1B
  54. #define SAMPLE_COUNT 16
  55. #define ADC_WAIT_COUNT 10
  56. #define CCADC_MAX_25MV 30000
  57. #define CCADC_MIN_25MV 20000
  58. #define CCADC_MAX_0UV -4000
  59. #define CCADC_MIN_0UV -7000
  60. #define CCADC_INTRINSIC_OFFSET 0xC000
  61. struct pm8xxx_ccadc_chip {
  62. struct device *dev;
  63. struct dentry *dent;
  64. unsigned int batt_temp_channel;
  65. u16 ccadc_offset;
  66. int ccadc_gain_uv;
  67. unsigned int revision;
  68. unsigned int calib_delay_ms;
  69. unsigned long last_calib_time;
  70. int last_calib_temp;
  71. int eoc_irq;
  72. int r_sense_uohm;
  73. struct delayed_work calib_ccadc_work;
  74. struct mutex calib_mutex;
  75. bool periodic_wakeup;
  76. };
  77. static struct pm8xxx_ccadc_chip *the_chip;
  78. #ifdef DEBUG
  79. static s64 microvolt_to_ccadc_reading(struct pm8xxx_ccadc_chip *chip, s64 cc)
  80. {
  81. return div_s64(uv * CCADC_READING_RESOLUTION_D,
  82. CCADC_READING_RESOLUTION_N);
  83. }
  84. #endif
  85. static int cc_adjust_for_offset(u16 raw)
  86. {
  87. /* this has the intrinsic offset */
  88. return (int)raw - the_chip->ccadc_offset;
  89. }
  90. #define GAIN_REFERENCE_UV 25000
  91. /*
  92. * gain compensation for ccadc readings - common for vsense based and
  93. * couloumb counter based readings
  94. */
  95. s64 pm8xxx_cc_adjust_for_gain(s64 uv)
  96. {
  97. if (the_chip == NULL || the_chip->ccadc_gain_uv == 0)
  98. return uv;
  99. return div_s64(uv * GAIN_REFERENCE_UV, the_chip->ccadc_gain_uv);
  100. }
  101. EXPORT_SYMBOL(pm8xxx_cc_adjust_for_gain);
  102. static int pm_ccadc_masked_write(struct pm8xxx_ccadc_chip *chip, u16 addr,
  103. u8 mask, u8 val)
  104. {
  105. int rc;
  106. u8 reg;
  107. rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
  108. if (rc) {
  109. pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
  110. return rc;
  111. }
  112. reg &= ~mask;
  113. reg |= val & mask;
  114. rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
  115. if (rc) {
  116. pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
  117. return rc;
  118. }
  119. return 0;
  120. }
  121. #define REG_SBI_CONFIG 0x04F
  122. #define PAGE3_ENABLE_MASK 0x6
  123. static int calib_ccadc_enable_trim_access(struct pm8xxx_ccadc_chip *chip,
  124. u8 *sbi_config)
  125. {
  126. u8 reg;
  127. int rc;
  128. rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
  129. if (rc) {
  130. pr_err("error = %d reading sbi config reg\n", rc);
  131. return rc;
  132. }
  133. reg = *sbi_config | PAGE3_ENABLE_MASK;
  134. return pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
  135. }
  136. static int calib_ccadc_restore_trim_access(struct pm8xxx_ccadc_chip *chip,
  137. u8 sbi_config)
  138. {
  139. return pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
  140. }
  141. static int calib_ccadc_enable_arbiter(struct pm8xxx_ccadc_chip *chip)
  142. {
  143. int rc;
  144. /* enable Arbiter, must be sent twice */
  145. rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL,
  146. SEL_CCADC_BIT | EN_ARB_BIT, SEL_CCADC_BIT | EN_ARB_BIT);
  147. if (rc < 0) {
  148. pr_err("error = %d enabling arbiter for offset\n", rc);
  149. return rc;
  150. }
  151. rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL,
  152. SEL_CCADC_BIT | EN_ARB_BIT, SEL_CCADC_BIT | EN_ARB_BIT);
  153. if (rc < 0) {
  154. pr_err("error = %d writing ADC_ARB_SECP_CNTRL\n", rc);
  155. return rc;
  156. }
  157. return 0;
  158. }
  159. static int calib_start_conv(struct pm8xxx_ccadc_chip *chip,
  160. u16 *result)
  161. {
  162. int rc, i;
  163. u8 data_msb, data_lsb, reg;
  164. /* Start conversion */
  165. rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL,
  166. START_CONV_BIT, START_CONV_BIT);
  167. if (rc < 0) {
  168. pr_err("error = %d starting offset meas\n", rc);
  169. return rc;
  170. }
  171. /* Wait for End of conversion */
  172. for (i = 0; i < ADC_WAIT_COUNT; i++) {
  173. rc = pm8xxx_readb(chip->dev->parent,
  174. ADC_ARB_SECP_CNTRL, &reg);
  175. if (rc < 0) {
  176. pr_err("error = %d read eoc for offset\n", rc);
  177. return rc;
  178. }
  179. if ((reg & (START_CONV_BIT | EOC_CONV_BIT)) != EOC_CONV_BIT)
  180. msleep(20);
  181. else
  182. break;
  183. }
  184. if (i == ADC_WAIT_COUNT) {
  185. pr_err("waited too long for offset eoc returning -EBUSY\n");
  186. return -EBUSY;
  187. }
  188. rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_SECP_DATA0, &data_lsb);
  189. if (rc < 0) {
  190. pr_err("error = %d reading offset lsb\n", rc);
  191. return rc;
  192. }
  193. rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_SECP_DATA1, &data_msb);
  194. if (rc < 0) {
  195. pr_err("error = %d reading offset msb\n", rc);
  196. return rc;
  197. }
  198. *result = (data_msb << 8) | data_lsb;
  199. return 0;
  200. }
  201. static int calib_ccadc_read_trim(struct pm8xxx_ccadc_chip *chip,
  202. int addr, u8 *data_msb, u8 *data_lsb)
  203. {
  204. int rc;
  205. u8 sbi_config;
  206. calib_ccadc_enable_trim_access(chip, &sbi_config);
  207. rc = pm8xxx_readb(chip->dev->parent, addr, data_msb);
  208. if (rc < 0) {
  209. pr_err("error = %d read msb\n", rc);
  210. return rc;
  211. }
  212. rc = pm8xxx_readb(chip->dev->parent, addr + 1, data_lsb);
  213. if (rc < 0) {
  214. pr_err("error = %d read lsb\n", rc);
  215. return rc;
  216. }
  217. calib_ccadc_restore_trim_access(chip, sbi_config);
  218. return 0;
  219. }
  220. static void calib_ccadc_read_offset_and_gain(struct pm8xxx_ccadc_chip *chip,
  221. int *gain, u16 *offset)
  222. {
  223. u8 data_msb;
  224. u8 data_lsb;
  225. int rc;
  226. rc = calib_ccadc_read_trim(chip, CCADC_FULLSCALE_TRIM1,
  227. &data_msb, &data_lsb);
  228. *gain = (data_msb << 8) | data_lsb;
  229. rc = calib_ccadc_read_trim(chip, CCADC_OFFSET_TRIM1,
  230. &data_msb, &data_lsb);
  231. *offset = (data_msb << 8) | data_lsb;
  232. pr_debug("raw gain trim = 0x%x offset trim =0x%x\n", *gain, *offset);
  233. *gain = pm8xxx_ccadc_reading_to_microvolt(chip->revision,
  234. (s64)*gain - *offset);
  235. pr_debug("gain uv = %duV offset=0x%x\n", *gain, *offset);
  236. }
  237. #define CCADC_PROGRAM_TRIM_COUNT 2
  238. #define ADC_ARB_BMS_CNTRL_CCADC_SHIFT 4
  239. #define ADC_ARB_BMS_CNTRL_CONV_MASK 0x03
  240. #define BMS_CONV_IN_PROGRESS 0x2
  241. static int calib_ccadc_program_trim(struct pm8xxx_ccadc_chip *chip,
  242. int addr, u8 data_msb, u8 data_lsb,
  243. int wait)
  244. {
  245. int i, rc, loop;
  246. u8 cntrl, sbi_config;
  247. bool in_progress = 0;
  248. loop = wait ? CCADC_PROGRAM_TRIM_COUNT : 0;
  249. calib_ccadc_enable_trim_access(chip, &sbi_config);
  250. for (i = 0; i < loop; i++) {
  251. rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_BMS_CNTRL, &cntrl);
  252. if (rc < 0) {
  253. pr_err("error = %d reading ADC_ARB_BMS_CNTRL\n", rc);
  254. return rc;
  255. }
  256. /* break if a ccadc conversion is not happening */
  257. in_progress = (((cntrl >> ADC_ARB_BMS_CNTRL_CCADC_SHIFT)
  258. & ADC_ARB_BMS_CNTRL_CONV_MASK) == BMS_CONV_IN_PROGRESS);
  259. if (!in_progress)
  260. break;
  261. }
  262. if (in_progress) {
  263. pr_debug("conv in progress cannot write trim,returing EBUSY\n");
  264. return -EBUSY;
  265. }
  266. rc = pm8xxx_writeb(chip->dev->parent, addr, data_msb);
  267. if (rc < 0) {
  268. pr_err("error = %d write msb = 0x%x\n", rc, data_msb);
  269. return rc;
  270. }
  271. rc = pm8xxx_writeb(chip->dev->parent, addr + 1, data_lsb);
  272. if (rc < 0) {
  273. pr_err("error = %d write lsb = 0x%x\n", rc, data_lsb);
  274. return rc;
  275. }
  276. calib_ccadc_restore_trim_access(chip, sbi_config);
  277. return 0;
  278. }
  279. static int get_batt_temp(struct pm8xxx_ccadc_chip *chip, int *batt_temp)
  280. {
  281. int rc;
  282. struct pm8xxx_adc_chan_result result;
  283. rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
  284. if (rc) {
  285. pr_err("error reading batt_temp_channel = %d, rc = %d\n",
  286. chip->batt_temp_channel, rc);
  287. return rc;
  288. }
  289. *batt_temp = result.physical;
  290. pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
  291. result.measurement);
  292. return 0;
  293. }
  294. static int get_current_time(unsigned long *now_tm_sec)
  295. {
  296. struct rtc_time tm;
  297. struct rtc_device *rtc;
  298. int rc;
  299. rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
  300. if (rtc == NULL) {
  301. pr_err("%s: unable to open rtc device (%s)\n",
  302. __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
  303. return -EINVAL;
  304. }
  305. rc = rtc_read_time(rtc, &tm);
  306. if (rc) {
  307. pr_err("Error reading rtc device (%s) : %d\n",
  308. CONFIG_RTC_HCTOSYS_DEVICE, rc);
  309. return rc;
  310. }
  311. rc = rtc_valid_tm(&tm);
  312. if (rc) {
  313. pr_err("Invalid RTC time (%s): %d\n",
  314. CONFIG_RTC_HCTOSYS_DEVICE, rc);
  315. return rc;
  316. }
  317. rtc_tm_to_time(&tm, now_tm_sec);
  318. return 0;
  319. }
  320. static void __pm8xxx_calib_ccadc(int sample_count)
  321. {
  322. u8 data_msb, data_lsb, sec_cntrl;
  323. int result_offset, result_gain;
  324. u16 result;
  325. int i, rc;
  326. if (!the_chip) {
  327. pr_err("chip not initialized\n");
  328. return;
  329. }
  330. pr_debug("sample_count = %d\n", sample_count);
  331. mutex_lock(&the_chip->calib_mutex);
  332. rc = pm8xxx_readb(the_chip->dev->parent,
  333. ADC_ARB_SECP_CNTRL, &sec_cntrl);
  334. if (rc < 0) {
  335. pr_err("error = %d reading ADC_ARB_SECP_CNTRL\n", rc);
  336. goto calibration_unlock;
  337. }
  338. rc = calib_ccadc_enable_arbiter(the_chip);
  339. if (rc < 0) {
  340. pr_err("error = %d enabling arbiter for offset\n", rc);
  341. goto bail;
  342. }
  343. /*
  344. * Set decimation ratio to 4k, lower ratio may be used in order to speed
  345. * up, pending verification through bench
  346. */
  347. rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM,
  348. CCADC_CALIB_DIG_PARAM);
  349. if (rc < 0) {
  350. pr_err("error = %d writing ADC_ARB_SECP_DIG_PARAM\n", rc);
  351. goto bail;
  352. }
  353. result_offset = 0;
  354. for (i = 0; i < sample_count; i++) {
  355. /* Short analog inputs to CCADC internally to ground */
  356. rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_RSV,
  357. CCADC_CALIB_RSV_GND);
  358. if (rc < 0) {
  359. pr_err("error = %d selecting gnd voltage\n", rc);
  360. goto bail;
  361. }
  362. /* Enable CCADC */
  363. rc = pm8xxx_writeb(the_chip->dev->parent,
  364. ADC_ARB_SECP_ANA_PARAM, CCADC_CALIB_ANA_PARAM);
  365. if (rc < 0) {
  366. pr_err("error = %d enabling ccadc\n", rc);
  367. goto bail;
  368. }
  369. rc = calib_start_conv(the_chip, &result);
  370. if (rc < 0) {
  371. pr_err("error = %d for zero volt measurement\n", rc);
  372. goto bail;
  373. }
  374. result_offset += result;
  375. }
  376. result_offset = result_offset / sample_count;
  377. pr_debug("offset result_offset = 0x%x, voltage = %llduV\n",
  378. result_offset,
  379. pm8xxx_ccadc_reading_to_microvolt(the_chip->revision,
  380. ((s64)result_offset - CCADC_INTRINSIC_OFFSET)));
  381. the_chip->ccadc_offset = result_offset;
  382. data_msb = the_chip->ccadc_offset >> 8;
  383. data_lsb = the_chip->ccadc_offset;
  384. rc = calib_ccadc_program_trim(the_chip, CCADC_OFFSET_TRIM1,
  385. data_msb, data_lsb, 1);
  386. if (rc) {
  387. pr_debug("error = %d programming offset trim 0x%02x 0x%02x\n",
  388. rc, data_msb, data_lsb);
  389. /* enable the interrupt and write it when it fires */
  390. enable_irq(the_chip->eoc_irq);
  391. }
  392. rc = calib_ccadc_enable_arbiter(the_chip);
  393. if (rc < 0) {
  394. pr_err("error = %d enabling arbiter for gain\n", rc);
  395. goto bail;
  396. }
  397. /*
  398. * Set decimation ratio to 4k, lower ratio may be used in order to speed
  399. * up, pending verification through bench
  400. */
  401. rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM,
  402. CCADC_CALIB_DIG_PARAM);
  403. if (rc < 0) {
  404. pr_err("error = %d enabling decimation ration for gain\n", rc);
  405. goto bail;
  406. }
  407. result_gain = 0;
  408. for (i = 0; i < sample_count; i++) {
  409. rc = pm8xxx_writeb(the_chip->dev->parent,
  410. ADC_ARB_SECP_RSV, CCADC_CALIB_RSV_25MV);
  411. if (rc < 0) {
  412. pr_err("error = %d selecting 25mV for gain\n", rc);
  413. goto bail;
  414. }
  415. /* Enable CCADC */
  416. rc = pm8xxx_writeb(the_chip->dev->parent,
  417. ADC_ARB_SECP_ANA_PARAM, CCADC_CALIB_ANA_PARAM);
  418. if (rc < 0) {
  419. pr_err("error = %d enabling ccadc\n", rc);
  420. goto bail;
  421. }
  422. rc = calib_start_conv(the_chip, &result);
  423. if (rc < 0) {
  424. pr_err("error = %d for adc reading 25mV\n", rc);
  425. goto bail;
  426. }
  427. result_gain += result;
  428. }
  429. result_gain = result_gain / sample_count;
  430. /*
  431. * result_offset includes INTRINSIC OFFSET
  432. * the_chip->ccadc_gain_uv will be the actual voltage
  433. * measured for 25000UV
  434. */
  435. the_chip->ccadc_gain_uv = pm8xxx_ccadc_reading_to_microvolt(
  436. the_chip->revision,
  437. ((s64)result_gain - result_offset));
  438. pr_debug("gain result_gain = 0x%x, voltage = %d microVolts\n",
  439. result_gain, the_chip->ccadc_gain_uv);
  440. data_msb = result_gain >> 8;
  441. data_lsb = result_gain;
  442. rc = calib_ccadc_program_trim(the_chip, CCADC_FULLSCALE_TRIM1,
  443. data_msb, data_lsb, 0);
  444. if (rc)
  445. pr_debug("error = %d programming gain trim\n", rc);
  446. bail:
  447. pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_CNTRL, sec_cntrl);
  448. calibration_unlock:
  449. mutex_unlock(&the_chip->calib_mutex);
  450. }
  451. static void pm8xxx_calib_ccadc_quick(void)
  452. {
  453. __pm8xxx_calib_ccadc(2);
  454. }
  455. void pm8xxx_calib_ccadc(void)
  456. {
  457. __pm8xxx_calib_ccadc(SAMPLE_COUNT);
  458. }
  459. EXPORT_SYMBOL(pm8xxx_calib_ccadc);
  460. static void calibrate_ccadc_work(struct work_struct *work)
  461. {
  462. struct pm8xxx_ccadc_chip *chip = container_of(work,
  463. struct pm8xxx_ccadc_chip, calib_ccadc_work.work);
  464. pm8xxx_calib_ccadc();
  465. schedule_delayed_work(&chip->calib_ccadc_work,
  466. round_jiffies_relative(msecs_to_jiffies
  467. (chip->calib_delay_ms)));
  468. }
  469. static irqreturn_t pm8921_bms_ccadc_eoc_handler(int irq, void *data)
  470. {
  471. u8 data_msb, data_lsb;
  472. struct pm8xxx_ccadc_chip *chip = data;
  473. int rc;
  474. if (!the_chip)
  475. goto out;
  476. pr_debug("irq = %d triggered\n", irq);
  477. data_msb = chip->ccadc_offset >> 8;
  478. data_lsb = chip->ccadc_offset;
  479. rc = calib_ccadc_program_trim(chip, CCADC_OFFSET_TRIM1,
  480. data_msb, data_lsb, 0);
  481. disable_irq_nosync(chip->eoc_irq);
  482. out:
  483. return IRQ_HANDLED;
  484. }
  485. #define CCADC_IBAT_DIG_PARAM 0xA3
  486. #define CCADC_IBAT_RSV 0x10
  487. #define CCADC_IBAT_ANA_PARAM 0x1A
  488. static int ccadc_get_rsense_voltage(int *voltage_uv)
  489. {
  490. u16 raw;
  491. int result;
  492. int rc = 0;
  493. rc = calib_ccadc_enable_arbiter(the_chip);
  494. if (rc < 0) {
  495. pr_err("error = %d enabling arbiter for offset\n", rc);
  496. return rc;
  497. }
  498. rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM,
  499. CCADC_IBAT_DIG_PARAM);
  500. if (rc < 0) {
  501. pr_err("error = %d writing ADC_ARB_SECP_DIG_PARAM\n", rc);
  502. return rc;
  503. }
  504. rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_RSV,
  505. CCADC_IBAT_RSV);
  506. if (rc < 0) {
  507. pr_err("error = %d selecting rsense\n", rc);
  508. return rc;
  509. }
  510. rc = pm8xxx_writeb(the_chip->dev->parent,
  511. ADC_ARB_SECP_ANA_PARAM, CCADC_IBAT_ANA_PARAM);
  512. if (rc < 0) {
  513. pr_err("error = %d enabling ccadc\n", rc);
  514. return rc;
  515. }
  516. rc = calib_start_conv(the_chip, &raw);
  517. if (rc < 0) {
  518. pr_err("error = %d for zero volt measurement\n", rc);
  519. return rc;
  520. }
  521. pr_debug("Vsense raw = 0x%x\n", raw);
  522. result = cc_adjust_for_offset(raw);
  523. pr_debug("Vsense after offset raw = 0x%x offset=0x%x\n",
  524. result,
  525. the_chip->ccadc_offset);
  526. *voltage_uv = pm8xxx_ccadc_reading_to_microvolt(the_chip->revision,
  527. ((s64)result));
  528. pr_debug("Vsense before gain of %d = %d uV\n", the_chip->ccadc_gain_uv,
  529. *voltage_uv);
  530. *voltage_uv = pm8xxx_cc_adjust_for_gain(*voltage_uv);
  531. pr_debug("Vsense = %d uV\n", *voltage_uv);
  532. return 0;
  533. }
  534. int pm8xxx_ccadc_get_battery_current(int *bat_current_ua)
  535. {
  536. int voltage_uv = 0, rc;
  537. rc = ccadc_get_rsense_voltage(&voltage_uv);
  538. if (rc) {
  539. pr_err("cant get voltage across rsense rc = %d\n", rc);
  540. return rc;
  541. }
  542. *bat_current_ua = div_s64((s64)voltage_uv * 1000000LL,
  543. the_chip->r_sense_uohm);
  544. /*
  545. * ccadc reads +ve current when the battery is charging
  546. * We need to return -ve if the battery is charging
  547. */
  548. *bat_current_ua = -1 * (*bat_current_ua);
  549. pr_debug("bat current = %d ma\n", *bat_current_ua);
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(pm8xxx_ccadc_get_battery_current);
  553. static int get_reg(void *data, u64 * val)
  554. {
  555. int addr = (int)data;
  556. int ret;
  557. u8 temp;
  558. ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
  559. if (ret) {
  560. pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
  561. addr, temp, ret);
  562. return -EAGAIN;
  563. }
  564. *val = temp;
  565. return 0;
  566. }
  567. static int set_reg(void *data, u64 val)
  568. {
  569. int addr = (int)data;
  570. int ret;
  571. u8 temp;
  572. temp = (u8) val;
  573. ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
  574. if (ret) {
  575. pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
  576. addr, temp, ret);
  577. return -EAGAIN;
  578. }
  579. return 0;
  580. }
  581. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
  582. static int get_calc(void *data, u64 * val)
  583. {
  584. int ibat, rc;
  585. rc = pm8xxx_ccadc_get_battery_current(&ibat);
  586. *val = ibat;
  587. return rc;
  588. }
  589. DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%lld\n");
  590. static void create_debugfs_entries(struct pm8xxx_ccadc_chip *chip)
  591. {
  592. chip->dent = debugfs_create_dir("pm8xxx-ccadc", NULL);
  593. if (IS_ERR(chip->dent)) {
  594. pr_err("ccadc couldnt create debugfs dir\n");
  595. return;
  596. }
  597. debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
  598. (void *)CCADC_ANA_PARAM, &reg_fops);
  599. debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
  600. (void *)CCADC_DIG_PARAM, &reg_fops);
  601. debugfs_create_file("CCADC_RSV", 0644, chip->dent,
  602. (void *)CCADC_RSV, &reg_fops);
  603. debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
  604. (void *)CCADC_DATA0, &reg_fops);
  605. debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
  606. (void *)CCADC_DATA1, &reg_fops);
  607. debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
  608. (void *)CCADC_OFFSET_TRIM1, &reg_fops);
  609. debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
  610. (void *)CCADC_OFFSET_TRIM0, &reg_fops);
  611. debugfs_create_file("CCADC_FULLSCALE_TRIM1", 0644, chip->dent,
  612. (void *)CCADC_FULLSCALE_TRIM1, &reg_fops);
  613. debugfs_create_file("CCADC_FULLSCALE_TRIM0", 0644, chip->dent,
  614. (void *)CCADC_FULLSCALE_TRIM0, &reg_fops);
  615. debugfs_create_file("show_ibatt", 0644, chip->dent,
  616. (void *)0, &calc_fops);
  617. }
  618. static int __devinit pm8xxx_ccadc_probe(struct platform_device *pdev)
  619. {
  620. int rc = 0;
  621. struct pm8xxx_ccadc_chip *chip;
  622. struct resource *res;
  623. const struct pm8xxx_ccadc_platform_data *pdata
  624. = pdev->dev.platform_data;
  625. if (!pdata) {
  626. pr_err("missing platform data\n");
  627. return -EINVAL;
  628. }
  629. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  630. "PM8921_BMS_CCADC_EOC");
  631. if (!res) {
  632. pr_err("failed to get irq\n");
  633. return -EINVAL;
  634. }
  635. chip = kzalloc(sizeof(struct pm8xxx_ccadc_chip), GFP_KERNEL);
  636. if (!chip) {
  637. pr_err("Cannot allocate pm_bms_chip\n");
  638. return -ENOMEM;
  639. }
  640. chip->dev = &pdev->dev;
  641. chip->revision = pm8xxx_get_revision(chip->dev->parent);
  642. chip->eoc_irq = res->start;
  643. chip->r_sense_uohm = pdata->r_sense_uohm;
  644. chip->calib_delay_ms = pdata->calib_delay_ms;
  645. chip->batt_temp_channel = pdata->ccadc_cdata.batt_temp_channel;
  646. chip->periodic_wakeup = pdata->periodic_wakeup;
  647. mutex_init(&chip->calib_mutex);
  648. calib_ccadc_read_offset_and_gain(chip,
  649. &chip->ccadc_gain_uv,
  650. &chip->ccadc_offset);
  651. irq_set_status_flags(chip->eoc_irq, IRQ_NOAUTOEN);
  652. rc = request_irq(chip->eoc_irq,
  653. pm8921_bms_ccadc_eoc_handler, IRQF_TRIGGER_RISING,
  654. "bms_eoc_ccadc", chip);
  655. if (rc) {
  656. pr_err("failed to request %d irq rc= %d\n", chip->eoc_irq, rc);
  657. goto free_chip;
  658. }
  659. platform_set_drvdata(pdev, chip);
  660. the_chip = chip;
  661. INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work);
  662. schedule_delayed_work(&chip->calib_ccadc_work, 0);
  663. create_debugfs_entries(chip);
  664. return 0;
  665. free_chip:
  666. mutex_destroy(&chip->calib_mutex);
  667. kfree(chip);
  668. return rc;
  669. }
  670. static int __devexit pm8xxx_ccadc_remove(struct platform_device *pdev)
  671. {
  672. struct pm8xxx_ccadc_chip *chip = platform_get_drvdata(pdev);
  673. debugfs_remove_recursive(chip->dent);
  674. the_chip = NULL;
  675. kfree(chip);
  676. return 0;
  677. }
  678. static int pm8xxx_ccadc_suspend(struct device *dev)
  679. {
  680. struct pm8xxx_ccadc_chip *chip = dev_get_drvdata(dev);
  681. cancel_delayed_work_sync(&chip->calib_ccadc_work);
  682. return 0;
  683. }
  684. #define CCADC_CALIB_TEMP_THRESH 20
  685. static int pm8xxx_ccadc_resume(struct device *dev)
  686. {
  687. int rc, batt_temp, delta_temp;
  688. unsigned long current_time_sec;
  689. unsigned long time_since_last_calib;
  690. rc = get_batt_temp(the_chip, &batt_temp);
  691. if (rc) {
  692. pr_err("unable to get batt_temp: %d\n", rc);
  693. return 0;
  694. }
  695. rc = get_current_time(&current_time_sec);
  696. if (rc) {
  697. pr_err("unable to get current time: %d\n", rc);
  698. return 0;
  699. }
  700. if (the_chip->periodic_wakeup) {
  701. pm8xxx_calib_ccadc_quick();
  702. return 0;
  703. }
  704. if (current_time_sec > the_chip->last_calib_time) {
  705. time_since_last_calib = current_time_sec -
  706. the_chip->last_calib_time;
  707. delta_temp = abs(batt_temp - the_chip->last_calib_temp);
  708. pr_debug("time since last calib: %lu, delta_temp = %d\n",
  709. time_since_last_calib, delta_temp);
  710. if (time_since_last_calib >= the_chip->calib_delay_ms/1000
  711. || delta_temp > CCADC_CALIB_TEMP_THRESH) {
  712. the_chip->last_calib_time = current_time_sec;
  713. the_chip->last_calib_temp = batt_temp;
  714. schedule_delayed_work(&the_chip->calib_ccadc_work, 0);
  715. } else {
  716. schedule_delayed_work(&the_chip->calib_ccadc_work,
  717. msecs_to_jiffies(the_chip->calib_delay_ms -
  718. (time_since_last_calib * 1000)));
  719. }
  720. }
  721. return 0;
  722. }
  723. static const struct dev_pm_ops pm8xxx_ccadc_pm_ops = {
  724. .suspend = pm8xxx_ccadc_suspend,
  725. .resume = pm8xxx_ccadc_resume,
  726. };
  727. static struct platform_driver pm8xxx_ccadc_driver = {
  728. .probe = pm8xxx_ccadc_probe,
  729. .remove = __devexit_p(pm8xxx_ccadc_remove),
  730. .driver = {
  731. .name = PM8XXX_CCADC_DEV_NAME,
  732. .owner = THIS_MODULE,
  733. .pm = &pm8xxx_ccadc_pm_ops,
  734. },
  735. };
  736. static int __init pm8xxx_ccadc_init(void)
  737. {
  738. return platform_driver_register(&pm8xxx_ccadc_driver);
  739. }
  740. static void __exit pm8xxx_ccadc_exit(void)
  741. {
  742. platform_driver_unregister(&pm8xxx_ccadc_driver);
  743. }
  744. module_init(pm8xxx_ccadc_init);
  745. module_exit(pm8xxx_ccadc_exit);
  746. MODULE_LICENSE("GPL v2");
  747. MODULE_DESCRIPTION("PMIC8XXX ccadc driver");
  748. MODULE_VERSION("1.0");
  749. MODULE_ALIAS("platform:" PM8XXX_CCADC_DEV_NAME);