sc27xx_fuel_gauge.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Spreadtrum Communications Inc.
  3. #include <linux/gpio/consumer.h>
  4. #include <linux/iio/consumer.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/kernel.h>
  7. #include <linux/math64.h>
  8. #include <linux/module.h>
  9. #include <linux/nvmem-consumer.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/regmap.h>
  14. #include <linux/slab.h>
  15. /* PMIC global control registers definition */
  16. #define SC27XX_MODULE_EN0 0xc08
  17. #define SC27XX_CLK_EN0 0xc18
  18. #define SC27XX_FGU_EN BIT(7)
  19. #define SC27XX_FGU_RTC_EN BIT(6)
  20. /* FGU registers definition */
  21. #define SC27XX_FGU_START 0x0
  22. #define SC27XX_FGU_CONFIG 0x4
  23. #define SC27XX_FGU_ADC_CONFIG 0x8
  24. #define SC27XX_FGU_STATUS 0xc
  25. #define SC27XX_FGU_INT_EN 0x10
  26. #define SC27XX_FGU_INT_CLR 0x14
  27. #define SC27XX_FGU_INT_STS 0x1c
  28. #define SC27XX_FGU_VOLTAGE 0x20
  29. #define SC27XX_FGU_OCV 0x24
  30. #define SC27XX_FGU_POCV 0x28
  31. #define SC27XX_FGU_CURRENT 0x2c
  32. #define SC27XX_FGU_LOW_OVERLOAD 0x34
  33. #define SC27XX_FGU_CLBCNT_SETH 0x50
  34. #define SC27XX_FGU_CLBCNT_SETL 0x54
  35. #define SC27XX_FGU_CLBCNT_DELTH 0x58
  36. #define SC27XX_FGU_CLBCNT_DELTL 0x5c
  37. #define SC27XX_FGU_CLBCNT_VALH 0x68
  38. #define SC27XX_FGU_CLBCNT_VALL 0x6c
  39. #define SC27XX_FGU_CLBCNT_QMAXL 0x74
  40. #define SC27XX_FGU_USER_AREA_SET 0xa0
  41. #define SC27XX_FGU_USER_AREA_CLEAR 0xa4
  42. #define SC27XX_FGU_USER_AREA_STATUS 0xa8
  43. #define SC27XX_FGU_VOLTAGE_BUF 0xd0
  44. #define SC27XX_FGU_CURRENT_BUF 0xf0
  45. #define SC27XX_WRITE_SELCLB_EN BIT(0)
  46. #define SC27XX_FGU_CLBCNT_MASK GENMASK(15, 0)
  47. #define SC27XX_FGU_CLBCNT_SHIFT 16
  48. #define SC27XX_FGU_LOW_OVERLOAD_MASK GENMASK(12, 0)
  49. #define SC27XX_FGU_INT_MASK GENMASK(9, 0)
  50. #define SC27XX_FGU_LOW_OVERLOAD_INT BIT(0)
  51. #define SC27XX_FGU_CLBCNT_DELTA_INT BIT(2)
  52. #define SC27XX_FGU_MODE_AREA_MASK GENMASK(15, 12)
  53. #define SC27XX_FGU_CAP_AREA_MASK GENMASK(11, 0)
  54. #define SC27XX_FGU_MODE_AREA_SHIFT 12
  55. #define SC27XX_FGU_FIRST_POWERTON GENMASK(3, 0)
  56. #define SC27XX_FGU_DEFAULT_CAP GENMASK(11, 0)
  57. #define SC27XX_FGU_NORMAIL_POWERTON 0x5
  58. #define SC27XX_FGU_CUR_BASIC_ADC 8192
  59. #define SC27XX_FGU_SAMPLE_HZ 2
  60. /* micro Ohms */
  61. #define SC27XX_FGU_IDEAL_RESISTANCE 20000
  62. /*
  63. * struct sc27xx_fgu_data: describe the FGU device
  64. * @regmap: regmap for register access
  65. * @dev: platform device
  66. * @battery: battery power supply
  67. * @base: the base offset for the controller
  68. * @lock: protect the structure
  69. * @gpiod: GPIO for battery detection
  70. * @channel: IIO channel to get battery temperature
  71. * @charge_chan: IIO channel to get charge voltage
  72. * @internal_resist: the battery internal resistance in mOhm
  73. * @total_cap: the total capacity of the battery in mAh
  74. * @init_cap: the initial capacity of the battery in mAh
  75. * @alarm_cap: the alarm capacity
  76. * @init_clbcnt: the initial coulomb counter
  77. * @max_volt: the maximum constant input voltage in millivolt
  78. * @min_volt: the minimum drained battery voltage in microvolt
  79. * @boot_volt: the voltage measured during boot in microvolt
  80. * @table_len: the capacity table length
  81. * @resist_table_len: the resistance table length
  82. * @cur_1000ma_adc: ADC value corresponding to 1000 mA
  83. * @vol_1000mv_adc: ADC value corresponding to 1000 mV
  84. * @calib_resist: the real resistance of coulomb counter chip in uOhm
  85. * @cap_table: capacity table with corresponding ocv
  86. * @resist_table: resistance percent table with corresponding temperature
  87. */
  88. struct sc27xx_fgu_data {
  89. struct regmap *regmap;
  90. struct device *dev;
  91. struct power_supply *battery;
  92. u32 base;
  93. struct mutex lock;
  94. struct gpio_desc *gpiod;
  95. struct iio_channel *channel;
  96. struct iio_channel *charge_chan;
  97. bool bat_present;
  98. int internal_resist;
  99. int total_cap;
  100. int init_cap;
  101. int alarm_cap;
  102. int init_clbcnt;
  103. int max_volt;
  104. int min_volt;
  105. int boot_volt;
  106. int table_len;
  107. int resist_table_len;
  108. int cur_1000ma_adc;
  109. int vol_1000mv_adc;
  110. int calib_resist;
  111. struct power_supply_battery_ocv_table *cap_table;
  112. struct power_supply_resistance_temp_table *resist_table;
  113. };
  114. static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
  115. static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
  116. int cap, bool int_mode);
  117. static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap);
  118. static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp);
  119. static const char * const sc27xx_charger_supply_name[] = {
  120. "sc2731_charger",
  121. "sc2720_charger",
  122. "sc2721_charger",
  123. "sc2723_charger",
  124. };
  125. static int sc27xx_fgu_adc_to_current(struct sc27xx_fgu_data *data, s64 adc)
  126. {
  127. return DIV_S64_ROUND_CLOSEST(adc * 1000, data->cur_1000ma_adc);
  128. }
  129. static int sc27xx_fgu_adc_to_voltage(struct sc27xx_fgu_data *data, s64 adc)
  130. {
  131. return DIV_S64_ROUND_CLOSEST(adc * 1000, data->vol_1000mv_adc);
  132. }
  133. static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol)
  134. {
  135. return DIV_ROUND_CLOSEST(vol * data->vol_1000mv_adc, 1000);
  136. }
  137. static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data *data)
  138. {
  139. int ret, status, cap, mode;
  140. ret = regmap_read(data->regmap,
  141. data->base + SC27XX_FGU_USER_AREA_STATUS, &status);
  142. if (ret)
  143. return false;
  144. /*
  145. * We use low 4 bits to save the last battery capacity and high 12 bits
  146. * to save the system boot mode.
  147. */
  148. mode = (status & SC27XX_FGU_MODE_AREA_MASK) >> SC27XX_FGU_MODE_AREA_SHIFT;
  149. cap = status & SC27XX_FGU_CAP_AREA_MASK;
  150. /*
  151. * When FGU has been powered down, the user area registers became
  152. * default value (0xffff), which can be used to valid if the system is
  153. * first power on or not.
  154. */
  155. if (mode == SC27XX_FGU_FIRST_POWERTON || cap == SC27XX_FGU_DEFAULT_CAP)
  156. return true;
  157. return false;
  158. }
  159. static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data,
  160. int boot_mode)
  161. {
  162. int ret;
  163. ret = regmap_update_bits(data->regmap,
  164. data->base + SC27XX_FGU_USER_AREA_CLEAR,
  165. SC27XX_FGU_MODE_AREA_MASK,
  166. SC27XX_FGU_MODE_AREA_MASK);
  167. if (ret)
  168. return ret;
  169. /*
  170. * Since the user area registers are put on power always-on region,
  171. * then these registers changing time will be a little long. Thus
  172. * here we should delay 200us to wait until values are updated
  173. * successfully according to the datasheet.
  174. */
  175. udelay(200);
  176. ret = regmap_update_bits(data->regmap,
  177. data->base + SC27XX_FGU_USER_AREA_SET,
  178. SC27XX_FGU_MODE_AREA_MASK,
  179. boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
  180. if (ret)
  181. return ret;
  182. /*
  183. * Since the user area registers are put on power always-on region,
  184. * then these registers changing time will be a little long. Thus
  185. * here we should delay 200us to wait until values are updated
  186. * successfully according to the datasheet.
  187. */
  188. udelay(200);
  189. /*
  190. * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
  191. * make the user area data available, otherwise we can not save the user
  192. * area data.
  193. */
  194. return regmap_update_bits(data->regmap,
  195. data->base + SC27XX_FGU_USER_AREA_CLEAR,
  196. SC27XX_FGU_MODE_AREA_MASK, 0);
  197. }
  198. static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
  199. {
  200. int ret;
  201. ret = regmap_update_bits(data->regmap,
  202. data->base + SC27XX_FGU_USER_AREA_CLEAR,
  203. SC27XX_FGU_CAP_AREA_MASK,
  204. SC27XX_FGU_CAP_AREA_MASK);
  205. if (ret)
  206. return ret;
  207. /*
  208. * Since the user area registers are put on power always-on region,
  209. * then these registers changing time will be a little long. Thus
  210. * here we should delay 200us to wait until values are updated
  211. * successfully according to the datasheet.
  212. */
  213. udelay(200);
  214. ret = regmap_update_bits(data->regmap,
  215. data->base + SC27XX_FGU_USER_AREA_SET,
  216. SC27XX_FGU_CAP_AREA_MASK, cap);
  217. if (ret)
  218. return ret;
  219. /*
  220. * Since the user area registers are put on power always-on region,
  221. * then these registers changing time will be a little long. Thus
  222. * here we should delay 200us to wait until values are updated
  223. * successfully according to the datasheet.
  224. */
  225. udelay(200);
  226. /*
  227. * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
  228. * make the user area data available, otherwise we can not save the user
  229. * area data.
  230. */
  231. return regmap_update_bits(data->regmap,
  232. data->base + SC27XX_FGU_USER_AREA_CLEAR,
  233. SC27XX_FGU_CAP_AREA_MASK, 0);
  234. }
  235. static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap)
  236. {
  237. int ret, value;
  238. ret = regmap_read(data->regmap,
  239. data->base + SC27XX_FGU_USER_AREA_STATUS, &value);
  240. if (ret)
  241. return ret;
  242. *cap = value & SC27XX_FGU_CAP_AREA_MASK;
  243. return 0;
  244. }
  245. /*
  246. * When system boots on, we can not read battery capacity from coulomb
  247. * registers, since now the coulomb registers are invalid. So we should
  248. * calculate the battery open circuit voltage, and get current battery
  249. * capacity according to the capacity table.
  250. */
  251. static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap)
  252. {
  253. int volt, cur, oci, ocv, ret;
  254. bool is_first_poweron = sc27xx_fgu_is_first_poweron(data);
  255. /*
  256. * If system is not the first power on, we should use the last saved
  257. * battery capacity as the initial battery capacity. Otherwise we should
  258. * re-calculate the initial battery capacity.
  259. */
  260. if (!is_first_poweron) {
  261. ret = sc27xx_fgu_read_last_cap(data, cap);
  262. if (ret)
  263. return ret;
  264. return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
  265. }
  266. /*
  267. * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved
  268. * the first sampled open circuit current.
  269. */
  270. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_QMAXL,
  271. &cur);
  272. if (ret)
  273. return ret;
  274. cur <<= 1;
  275. oci = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
  276. /*
  277. * Should get the OCV from SC27XX_FGU_POCV register at the system
  278. * beginning. It is ADC values reading from registers which need to
  279. * convert the corresponding voltage.
  280. */
  281. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_POCV, &volt);
  282. if (ret)
  283. return ret;
  284. volt = sc27xx_fgu_adc_to_voltage(data, volt);
  285. ocv = volt * 1000 - oci * data->internal_resist;
  286. data->boot_volt = ocv;
  287. /*
  288. * Parse the capacity table to look up the correct capacity percent
  289. * according to current battery's corresponding OCV values.
  290. */
  291. *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len,
  292. ocv);
  293. ret = sc27xx_fgu_save_last_cap(data, *cap);
  294. if (ret)
  295. return ret;
  296. return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
  297. }
  298. static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt)
  299. {
  300. int ret;
  301. ret = regmap_update_bits(data->regmap,
  302. data->base + SC27XX_FGU_CLBCNT_SETL,
  303. SC27XX_FGU_CLBCNT_MASK, clbcnt);
  304. if (ret)
  305. return ret;
  306. ret = regmap_update_bits(data->regmap,
  307. data->base + SC27XX_FGU_CLBCNT_SETH,
  308. SC27XX_FGU_CLBCNT_MASK,
  309. clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
  310. if (ret)
  311. return ret;
  312. return regmap_update_bits(data->regmap, data->base + SC27XX_FGU_START,
  313. SC27XX_WRITE_SELCLB_EN,
  314. SC27XX_WRITE_SELCLB_EN);
  315. }
  316. static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt)
  317. {
  318. int ccl, cch, ret;
  319. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALL,
  320. &ccl);
  321. if (ret)
  322. return ret;
  323. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALH,
  324. &cch);
  325. if (ret)
  326. return ret;
  327. *clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK;
  328. *clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT;
  329. return 0;
  330. }
  331. static int sc27xx_fgu_get_vol_now(struct sc27xx_fgu_data *data, int *val)
  332. {
  333. int ret;
  334. u32 vol;
  335. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE_BUF,
  336. &vol);
  337. if (ret)
  338. return ret;
  339. /*
  340. * It is ADC values reading from registers which need to convert to
  341. * corresponding voltage values.
  342. */
  343. *val = sc27xx_fgu_adc_to_voltage(data, vol);
  344. return 0;
  345. }
  346. static int sc27xx_fgu_get_cur_now(struct sc27xx_fgu_data *data, int *val)
  347. {
  348. int ret;
  349. u32 cur;
  350. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT_BUF,
  351. &cur);
  352. if (ret)
  353. return ret;
  354. /*
  355. * It is ADC values reading from registers which need to convert to
  356. * corresponding current values.
  357. */
  358. *val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
  359. return 0;
  360. }
  361. static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
  362. {
  363. int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp;
  364. /* Get current coulomb counters firstly */
  365. ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt);
  366. if (ret)
  367. return ret;
  368. delta_clbcnt = cur_clbcnt - data->init_clbcnt;
  369. /*
  370. * Convert coulomb counter to delta capacity (mAh), and set multiplier
  371. * as 10 to improve the precision.
  372. */
  373. temp = DIV_ROUND_CLOSEST(delta_clbcnt * 10, 36 * SC27XX_FGU_SAMPLE_HZ);
  374. temp = sc27xx_fgu_adc_to_current(data, temp / 1000);
  375. /*
  376. * Convert to capacity percent of the battery total capacity,
  377. * and multiplier is 100 too.
  378. */
  379. delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap);
  380. *cap = delta_cap + data->init_cap;
  381. /* Calibrate the battery capacity in a normal range. */
  382. sc27xx_fgu_capacity_calibration(data, *cap, false);
  383. return 0;
  384. }
  385. static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data *data, int *val)
  386. {
  387. int ret, vol;
  388. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE, &vol);
  389. if (ret)
  390. return ret;
  391. /*
  392. * It is ADC values reading from registers which need to convert to
  393. * corresponding voltage values.
  394. */
  395. *val = sc27xx_fgu_adc_to_voltage(data, vol);
  396. return 0;
  397. }
  398. static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val)
  399. {
  400. int ret, cur;
  401. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT, &cur);
  402. if (ret)
  403. return ret;
  404. /*
  405. * It is ADC values reading from registers which need to convert to
  406. * corresponding current values.
  407. */
  408. *val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
  409. return 0;
  410. }
  411. static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val)
  412. {
  413. int vol, cur, ret, temp, resistance;
  414. ret = sc27xx_fgu_get_vbat_vol(data, &vol);
  415. if (ret)
  416. return ret;
  417. ret = sc27xx_fgu_get_current(data, &cur);
  418. if (ret)
  419. return ret;
  420. resistance = data->internal_resist;
  421. if (data->resist_table_len > 0) {
  422. ret = sc27xx_fgu_get_temp(data, &temp);
  423. if (ret)
  424. return ret;
  425. resistance = power_supply_temp2resist_simple(data->resist_table,
  426. data->resist_table_len, temp);
  427. resistance = data->internal_resist * resistance / 100;
  428. }
  429. /* Return the battery OCV in micro volts. */
  430. *val = vol * 1000 - cur * resistance;
  431. return 0;
  432. }
  433. static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data *data, int *val)
  434. {
  435. int ret, vol;
  436. ret = iio_read_channel_processed(data->charge_chan, &vol);
  437. if (ret < 0)
  438. return ret;
  439. *val = vol * 1000;
  440. return 0;
  441. }
  442. static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp)
  443. {
  444. return iio_read_channel_processed(data->channel, temp);
  445. }
  446. static int sc27xx_fgu_get_health(struct sc27xx_fgu_data *data, int *health)
  447. {
  448. int ret, vol;
  449. ret = sc27xx_fgu_get_vbat_vol(data, &vol);
  450. if (ret)
  451. return ret;
  452. if (vol > data->max_volt)
  453. *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  454. else
  455. *health = POWER_SUPPLY_HEALTH_GOOD;
  456. return 0;
  457. }
  458. static int sc27xx_fgu_get_status(struct sc27xx_fgu_data *data, int *status)
  459. {
  460. union power_supply_propval val;
  461. struct power_supply *psy;
  462. int i, ret = -EINVAL;
  463. for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) {
  464. psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]);
  465. if (!psy)
  466. continue;
  467. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
  468. &val);
  469. power_supply_put(psy);
  470. if (ret)
  471. return ret;
  472. *status = val.intval;
  473. }
  474. return ret;
  475. }
  476. static int sc27xx_fgu_get_property(struct power_supply *psy,
  477. enum power_supply_property psp,
  478. union power_supply_propval *val)
  479. {
  480. struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
  481. int ret = 0;
  482. int value;
  483. mutex_lock(&data->lock);
  484. switch (psp) {
  485. case POWER_SUPPLY_PROP_STATUS:
  486. ret = sc27xx_fgu_get_status(data, &value);
  487. if (ret)
  488. goto error;
  489. val->intval = value;
  490. break;
  491. case POWER_SUPPLY_PROP_HEALTH:
  492. ret = sc27xx_fgu_get_health(data, &value);
  493. if (ret)
  494. goto error;
  495. val->intval = value;
  496. break;
  497. case POWER_SUPPLY_PROP_PRESENT:
  498. val->intval = data->bat_present;
  499. break;
  500. case POWER_SUPPLY_PROP_TEMP:
  501. ret = sc27xx_fgu_get_temp(data, &value);
  502. if (ret)
  503. goto error;
  504. val->intval = value;
  505. break;
  506. case POWER_SUPPLY_PROP_TECHNOLOGY:
  507. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  508. break;
  509. case POWER_SUPPLY_PROP_CAPACITY:
  510. ret = sc27xx_fgu_get_capacity(data, &value);
  511. if (ret)
  512. goto error;
  513. val->intval = value;
  514. break;
  515. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  516. ret = sc27xx_fgu_get_vbat_vol(data, &value);
  517. if (ret)
  518. goto error;
  519. val->intval = value * 1000;
  520. break;
  521. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  522. ret = sc27xx_fgu_get_vbat_ocv(data, &value);
  523. if (ret)
  524. goto error;
  525. val->intval = value;
  526. break;
  527. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  528. ret = sc27xx_fgu_get_charge_vol(data, &value);
  529. if (ret)
  530. goto error;
  531. val->intval = value;
  532. break;
  533. case POWER_SUPPLY_PROP_CURRENT_AVG:
  534. ret = sc27xx_fgu_get_current(data, &value);
  535. if (ret)
  536. goto error;
  537. val->intval = value * 1000;
  538. break;
  539. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  540. val->intval = data->total_cap * 1000;
  541. break;
  542. case POWER_SUPPLY_PROP_CHARGE_NOW:
  543. ret = sc27xx_fgu_get_clbcnt(data, &value);
  544. if (ret)
  545. goto error;
  546. value = DIV_ROUND_CLOSEST(value * 10,
  547. 36 * SC27XX_FGU_SAMPLE_HZ);
  548. val->intval = sc27xx_fgu_adc_to_current(data, value);
  549. break;
  550. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  551. ret = sc27xx_fgu_get_vol_now(data, &value);
  552. if (ret)
  553. goto error;
  554. val->intval = value * 1000;
  555. break;
  556. case POWER_SUPPLY_PROP_CURRENT_NOW:
  557. ret = sc27xx_fgu_get_cur_now(data, &value);
  558. if (ret)
  559. goto error;
  560. val->intval = value * 1000;
  561. break;
  562. case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
  563. val->intval = data->boot_volt;
  564. break;
  565. default:
  566. ret = -EINVAL;
  567. break;
  568. }
  569. error:
  570. mutex_unlock(&data->lock);
  571. return ret;
  572. }
  573. static int sc27xx_fgu_set_property(struct power_supply *psy,
  574. enum power_supply_property psp,
  575. const union power_supply_propval *val)
  576. {
  577. struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
  578. int ret;
  579. mutex_lock(&data->lock);
  580. switch (psp) {
  581. case POWER_SUPPLY_PROP_CAPACITY:
  582. ret = sc27xx_fgu_save_last_cap(data, val->intval);
  583. if (ret < 0)
  584. dev_err(data->dev, "failed to save battery capacity\n");
  585. break;
  586. case POWER_SUPPLY_PROP_CALIBRATE:
  587. sc27xx_fgu_adjust_cap(data, val->intval);
  588. ret = 0;
  589. break;
  590. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  591. data->total_cap = val->intval / 1000;
  592. ret = 0;
  593. break;
  594. default:
  595. ret = -EINVAL;
  596. }
  597. mutex_unlock(&data->lock);
  598. return ret;
  599. }
  600. static void sc27xx_fgu_external_power_changed(struct power_supply *psy)
  601. {
  602. struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
  603. power_supply_changed(data->battery);
  604. }
  605. static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
  606. enum power_supply_property psp)
  607. {
  608. return psp == POWER_SUPPLY_PROP_CAPACITY ||
  609. psp == POWER_SUPPLY_PROP_CALIBRATE ||
  610. psp == POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
  611. }
  612. static enum power_supply_property sc27xx_fgu_props[] = {
  613. POWER_SUPPLY_PROP_STATUS,
  614. POWER_SUPPLY_PROP_HEALTH,
  615. POWER_SUPPLY_PROP_PRESENT,
  616. POWER_SUPPLY_PROP_TEMP,
  617. POWER_SUPPLY_PROP_TECHNOLOGY,
  618. POWER_SUPPLY_PROP_CAPACITY,
  619. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  620. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  621. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  622. POWER_SUPPLY_PROP_VOLTAGE_BOOT,
  623. POWER_SUPPLY_PROP_CURRENT_NOW,
  624. POWER_SUPPLY_PROP_CURRENT_AVG,
  625. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  626. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  627. POWER_SUPPLY_PROP_CALIBRATE,
  628. POWER_SUPPLY_PROP_CHARGE_NOW
  629. };
  630. static const struct power_supply_desc sc27xx_fgu_desc = {
  631. .name = "sc27xx-fgu",
  632. .type = POWER_SUPPLY_TYPE_BATTERY,
  633. .properties = sc27xx_fgu_props,
  634. .num_properties = ARRAY_SIZE(sc27xx_fgu_props),
  635. .get_property = sc27xx_fgu_get_property,
  636. .set_property = sc27xx_fgu_set_property,
  637. .external_power_changed = sc27xx_fgu_external_power_changed,
  638. .property_is_writeable = sc27xx_fgu_property_is_writeable,
  639. .no_thermal = true,
  640. };
  641. static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
  642. {
  643. int ret;
  644. data->init_cap = cap;
  645. ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt);
  646. if (ret)
  647. dev_err(data->dev, "failed to get init coulomb counter\n");
  648. }
  649. static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
  650. int cap, bool int_mode)
  651. {
  652. int ret, ocv, chg_sts, adc;
  653. ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
  654. if (ret) {
  655. dev_err(data->dev, "get battery ocv error.\n");
  656. return;
  657. }
  658. ret = sc27xx_fgu_get_status(data, &chg_sts);
  659. if (ret) {
  660. dev_err(data->dev, "get charger status error.\n");
  661. return;
  662. }
  663. /*
  664. * If we are in charging mode, then we do not need to calibrate the
  665. * lower capacity.
  666. */
  667. if (chg_sts == POWER_SUPPLY_STATUS_CHARGING)
  668. return;
  669. if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) {
  670. /*
  671. * If current OCV value is larger than the max OCV value in
  672. * OCV table, or the current capacity is larger than 100,
  673. * we should force the inititial capacity to 100.
  674. */
  675. sc27xx_fgu_adjust_cap(data, 100);
  676. } else if (ocv <= data->cap_table[data->table_len - 1].ocv) {
  677. /*
  678. * If current OCV value is leass than the minimum OCV value in
  679. * OCV table, we should force the inititial capacity to 0.
  680. */
  681. sc27xx_fgu_adjust_cap(data, 0);
  682. } else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) ||
  683. (ocv > data->min_volt && cap <= data->alarm_cap)) {
  684. /*
  685. * If current OCV value is not matchable with current capacity,
  686. * we should re-calculate current capacity by looking up the
  687. * OCV table.
  688. */
  689. int cur_cap = power_supply_ocv2cap_simple(data->cap_table,
  690. data->table_len, ocv);
  691. sc27xx_fgu_adjust_cap(data, cur_cap);
  692. } else if (ocv <= data->min_volt) {
  693. /*
  694. * If current OCV value is less than the low alarm voltage, but
  695. * current capacity is larger than the alarm capacity, we should
  696. * adjust the inititial capacity to alarm capacity.
  697. */
  698. if (cap > data->alarm_cap) {
  699. sc27xx_fgu_adjust_cap(data, data->alarm_cap);
  700. } else {
  701. int cur_cap;
  702. /*
  703. * If current capacity is equal with 0 or less than 0
  704. * (some error occurs), we should adjust inititial
  705. * capacity to the capacity corresponding to current OCV
  706. * value.
  707. */
  708. cur_cap = power_supply_ocv2cap_simple(data->cap_table,
  709. data->table_len,
  710. ocv);
  711. sc27xx_fgu_adjust_cap(data, cur_cap);
  712. }
  713. if (!int_mode)
  714. return;
  715. /*
  716. * After adjusting the battery capacity, we should set the
  717. * lowest alarm voltage instead.
  718. */
  719. data->min_volt = data->cap_table[data->table_len - 1].ocv;
  720. data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
  721. data->table_len,
  722. data->min_volt);
  723. adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
  724. regmap_update_bits(data->regmap,
  725. data->base + SC27XX_FGU_LOW_OVERLOAD,
  726. SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
  727. }
  728. }
  729. static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
  730. {
  731. struct sc27xx_fgu_data *data = dev_id;
  732. int ret, cap;
  733. u32 status;
  734. mutex_lock(&data->lock);
  735. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
  736. &status);
  737. if (ret)
  738. goto out;
  739. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
  740. status, status);
  741. if (ret)
  742. goto out;
  743. /*
  744. * When low overload voltage interrupt happens, we should calibrate the
  745. * battery capacity in lower voltage stage.
  746. */
  747. if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
  748. goto out;
  749. ret = sc27xx_fgu_get_capacity(data, &cap);
  750. if (ret)
  751. goto out;
  752. sc27xx_fgu_capacity_calibration(data, cap, true);
  753. out:
  754. mutex_unlock(&data->lock);
  755. power_supply_changed(data->battery);
  756. return IRQ_HANDLED;
  757. }
  758. static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id)
  759. {
  760. struct sc27xx_fgu_data *data = dev_id;
  761. int state;
  762. mutex_lock(&data->lock);
  763. state = gpiod_get_value_cansleep(data->gpiod);
  764. if (state < 0) {
  765. dev_err(data->dev, "failed to get gpio state\n");
  766. mutex_unlock(&data->lock);
  767. return IRQ_RETVAL(state);
  768. }
  769. data->bat_present = !!state;
  770. mutex_unlock(&data->lock);
  771. power_supply_changed(data->battery);
  772. return IRQ_HANDLED;
  773. }
  774. static void sc27xx_fgu_disable(void *_data)
  775. {
  776. struct sc27xx_fgu_data *data = _data;
  777. regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
  778. regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
  779. }
  780. static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
  781. {
  782. /*
  783. * Get current capacity (mAh) = battery total capacity (mAh) *
  784. * current capacity percent (capacity / 100).
  785. */
  786. int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100);
  787. /*
  788. * Convert current capacity (mAh) to coulomb counter according to the
  789. * formula: 1 mAh =3.6 coulomb.
  790. */
  791. return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10);
  792. }
  793. static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
  794. {
  795. struct nvmem_cell *cell;
  796. int calib_data, cal_4200mv;
  797. void *buf;
  798. size_t len;
  799. cell = nvmem_cell_get(data->dev, "fgu_calib");
  800. if (IS_ERR(cell))
  801. return PTR_ERR(cell);
  802. buf = nvmem_cell_read(cell, &len);
  803. nvmem_cell_put(cell);
  804. if (IS_ERR(buf))
  805. return PTR_ERR(buf);
  806. memcpy(&calib_data, buf, min(len, sizeof(u32)));
  807. /*
  808. * Get the ADC value corresponding to 4200 mV from eFuse controller
  809. * according to below formula. Then convert to ADC values corresponding
  810. * to 1000 mV and 1000 mA.
  811. */
  812. cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256;
  813. data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42);
  814. data->cur_1000ma_adc =
  815. DIV_ROUND_CLOSEST(data->vol_1000mv_adc * 4 * data->calib_resist,
  816. SC27XX_FGU_IDEAL_RESISTANCE);
  817. kfree(buf);
  818. return 0;
  819. }
  820. static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
  821. {
  822. struct power_supply_battery_info info = { };
  823. struct power_supply_battery_ocv_table *table;
  824. int ret, delta_clbcnt, alarm_adc;
  825. ret = power_supply_get_battery_info(data->battery, &info);
  826. if (ret) {
  827. dev_err(data->dev, "failed to get battery information\n");
  828. return ret;
  829. }
  830. data->total_cap = info.charge_full_design_uah / 1000;
  831. data->max_volt = info.constant_charge_voltage_max_uv / 1000;
  832. data->internal_resist = info.factory_internal_resistance_uohm / 1000;
  833. data->min_volt = info.voltage_min_design_uv;
  834. /*
  835. * For SC27XX fuel gauge device, we only use one ocv-capacity
  836. * table in normal temperature 20 Celsius.
  837. */
  838. table = power_supply_find_ocv2cap_table(&info, 20, &data->table_len);
  839. if (!table)
  840. return -EINVAL;
  841. data->cap_table = devm_kmemdup(data->dev, table,
  842. data->table_len * sizeof(*table),
  843. GFP_KERNEL);
  844. if (!data->cap_table) {
  845. power_supply_put_battery_info(data->battery, &info);
  846. return -ENOMEM;
  847. }
  848. data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
  849. data->table_len,
  850. data->min_volt);
  851. if (!data->alarm_cap)
  852. data->alarm_cap += 1;
  853. data->resist_table_len = info.resist_table_size;
  854. if (data->resist_table_len > 0) {
  855. data->resist_table = devm_kmemdup(data->dev, info.resist_table,
  856. data->resist_table_len *
  857. sizeof(struct power_supply_resistance_temp_table),
  858. GFP_KERNEL);
  859. if (!data->resist_table) {
  860. power_supply_put_battery_info(data->battery, &info);
  861. return -ENOMEM;
  862. }
  863. }
  864. power_supply_put_battery_info(data->battery, &info);
  865. ret = sc27xx_fgu_calibration(data);
  866. if (ret)
  867. return ret;
  868. /* Enable the FGU module */
  869. ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0,
  870. SC27XX_FGU_EN, SC27XX_FGU_EN);
  871. if (ret) {
  872. dev_err(data->dev, "failed to enable fgu\n");
  873. return ret;
  874. }
  875. /* Enable the FGU RTC clock to make it work */
  876. ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0,
  877. SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN);
  878. if (ret) {
  879. dev_err(data->dev, "failed to enable fgu RTC clock\n");
  880. goto disable_fgu;
  881. }
  882. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
  883. SC27XX_FGU_INT_MASK, SC27XX_FGU_INT_MASK);
  884. if (ret) {
  885. dev_err(data->dev, "failed to clear interrupt status\n");
  886. goto disable_clk;
  887. }
  888. /*
  889. * Set the voltage low overload threshold, which means when the battery
  890. * voltage is lower than this threshold, the controller will generate
  891. * one interrupt to notify.
  892. */
  893. alarm_adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
  894. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
  895. SC27XX_FGU_LOW_OVERLOAD_MASK, alarm_adc);
  896. if (ret) {
  897. dev_err(data->dev, "failed to set fgu low overload\n");
  898. goto disable_clk;
  899. }
  900. /*
  901. * Set the coulomb counter delta threshold, that means when the coulomb
  902. * counter change is multiples of the delta threshold, the controller
  903. * will generate one interrupt to notify the users to update the battery
  904. * capacity. Now we set the delta threshold as a counter value of 1%
  905. * capacity.
  906. */
  907. delta_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, 1);
  908. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTL,
  909. SC27XX_FGU_CLBCNT_MASK, delta_clbcnt);
  910. if (ret) {
  911. dev_err(data->dev, "failed to set low delta coulomb counter\n");
  912. goto disable_clk;
  913. }
  914. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTH,
  915. SC27XX_FGU_CLBCNT_MASK,
  916. delta_clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
  917. if (ret) {
  918. dev_err(data->dev, "failed to set high delta coulomb counter\n");
  919. goto disable_clk;
  920. }
  921. /*
  922. * Get the boot battery capacity when system powers on, which is used to
  923. * initialize the coulomb counter. After that, we can read the coulomb
  924. * counter to measure the battery capacity.
  925. */
  926. ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap);
  927. if (ret) {
  928. dev_err(data->dev, "failed to get boot capacity\n");
  929. goto disable_clk;
  930. }
  931. /*
  932. * Convert battery capacity to the corresponding initial coulomb counter
  933. * and set into coulomb counter registers.
  934. */
  935. data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
  936. ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt);
  937. if (ret) {
  938. dev_err(data->dev, "failed to initialize coulomb counter\n");
  939. goto disable_clk;
  940. }
  941. return 0;
  942. disable_clk:
  943. regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
  944. disable_fgu:
  945. regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
  946. return ret;
  947. }
  948. static int sc27xx_fgu_probe(struct platform_device *pdev)
  949. {
  950. struct device *dev = &pdev->dev;
  951. struct device_node *np = dev->of_node;
  952. struct power_supply_config fgu_cfg = { };
  953. struct sc27xx_fgu_data *data;
  954. int ret, irq;
  955. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  956. if (!data)
  957. return -ENOMEM;
  958. data->regmap = dev_get_regmap(dev->parent, NULL);
  959. if (!data->regmap) {
  960. dev_err(dev, "failed to get regmap\n");
  961. return -ENODEV;
  962. }
  963. ret = device_property_read_u32(dev, "reg", &data->base);
  964. if (ret) {
  965. dev_err(dev, "failed to get fgu address\n");
  966. return ret;
  967. }
  968. ret = device_property_read_u32(&pdev->dev,
  969. "sprd,calib-resistance-micro-ohms",
  970. &data->calib_resist);
  971. if (ret) {
  972. dev_err(&pdev->dev,
  973. "failed to get fgu calibration resistance\n");
  974. return ret;
  975. }
  976. data->channel = devm_iio_channel_get(dev, "bat-temp");
  977. if (IS_ERR(data->channel)) {
  978. dev_err(dev, "failed to get IIO channel\n");
  979. return PTR_ERR(data->channel);
  980. }
  981. data->charge_chan = devm_iio_channel_get(dev, "charge-vol");
  982. if (IS_ERR(data->charge_chan)) {
  983. dev_err(dev, "failed to get charge IIO channel\n");
  984. return PTR_ERR(data->charge_chan);
  985. }
  986. data->gpiod = devm_gpiod_get(dev, "bat-detect", GPIOD_IN);
  987. if (IS_ERR(data->gpiod)) {
  988. dev_err(dev, "failed to get battery detection GPIO\n");
  989. return PTR_ERR(data->gpiod);
  990. }
  991. ret = gpiod_get_value_cansleep(data->gpiod);
  992. if (ret < 0) {
  993. dev_err(dev, "failed to get gpio state\n");
  994. return ret;
  995. }
  996. data->bat_present = !!ret;
  997. mutex_init(&data->lock);
  998. data->dev = dev;
  999. platform_set_drvdata(pdev, data);
  1000. fgu_cfg.drv_data = data;
  1001. fgu_cfg.of_node = np;
  1002. data->battery = devm_power_supply_register(dev, &sc27xx_fgu_desc,
  1003. &fgu_cfg);
  1004. if (IS_ERR(data->battery)) {
  1005. dev_err(dev, "failed to register power supply\n");
  1006. return PTR_ERR(data->battery);
  1007. }
  1008. ret = sc27xx_fgu_hw_init(data);
  1009. if (ret) {
  1010. dev_err(dev, "failed to initialize fgu hardware\n");
  1011. return ret;
  1012. }
  1013. ret = devm_add_action_or_reset(dev, sc27xx_fgu_disable, data);
  1014. if (ret) {
  1015. dev_err(dev, "failed to add fgu disable action\n");
  1016. return ret;
  1017. }
  1018. irq = platform_get_irq(pdev, 0);
  1019. if (irq < 0) {
  1020. dev_err(dev, "no irq resource specified\n");
  1021. return irq;
  1022. }
  1023. ret = devm_request_threaded_irq(data->dev, irq, NULL,
  1024. sc27xx_fgu_interrupt,
  1025. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1026. pdev->name, data);
  1027. if (ret) {
  1028. dev_err(data->dev, "failed to request fgu IRQ\n");
  1029. return ret;
  1030. }
  1031. irq = gpiod_to_irq(data->gpiod);
  1032. if (irq < 0) {
  1033. dev_err(dev, "failed to translate GPIO to IRQ\n");
  1034. return irq;
  1035. }
  1036. ret = devm_request_threaded_irq(dev, irq, NULL,
  1037. sc27xx_fgu_bat_detection,
  1038. IRQF_ONESHOT | IRQF_TRIGGER_RISING |
  1039. IRQF_TRIGGER_FALLING,
  1040. pdev->name, data);
  1041. if (ret) {
  1042. dev_err(dev, "failed to request IRQ\n");
  1043. return ret;
  1044. }
  1045. return 0;
  1046. }
  1047. #ifdef CONFIG_PM_SLEEP
  1048. static int sc27xx_fgu_resume(struct device *dev)
  1049. {
  1050. struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
  1051. int ret;
  1052. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1053. SC27XX_FGU_LOW_OVERLOAD_INT |
  1054. SC27XX_FGU_CLBCNT_DELTA_INT, 0);
  1055. if (ret) {
  1056. dev_err(data->dev, "failed to disable fgu interrupts\n");
  1057. return ret;
  1058. }
  1059. return 0;
  1060. }
  1061. static int sc27xx_fgu_suspend(struct device *dev)
  1062. {
  1063. struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
  1064. int ret, status, ocv;
  1065. ret = sc27xx_fgu_get_status(data, &status);
  1066. if (ret)
  1067. return ret;
  1068. /*
  1069. * If we are charging, then no need to enable the FGU interrupts to
  1070. * adjust the battery capacity.
  1071. */
  1072. if (status != POWER_SUPPLY_STATUS_NOT_CHARGING &&
  1073. status != POWER_SUPPLY_STATUS_DISCHARGING)
  1074. return 0;
  1075. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1076. SC27XX_FGU_LOW_OVERLOAD_INT,
  1077. SC27XX_FGU_LOW_OVERLOAD_INT);
  1078. if (ret) {
  1079. dev_err(data->dev, "failed to enable low voltage interrupt\n");
  1080. return ret;
  1081. }
  1082. ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
  1083. if (ret)
  1084. goto disable_int;
  1085. /*
  1086. * If current OCV is less than the minimum voltage, we should enable the
  1087. * coulomb counter threshold interrupt to notify events to adjust the
  1088. * battery capacity.
  1089. */
  1090. if (ocv < data->min_volt) {
  1091. ret = regmap_update_bits(data->regmap,
  1092. data->base + SC27XX_FGU_INT_EN,
  1093. SC27XX_FGU_CLBCNT_DELTA_INT,
  1094. SC27XX_FGU_CLBCNT_DELTA_INT);
  1095. if (ret) {
  1096. dev_err(data->dev,
  1097. "failed to enable coulomb threshold int\n");
  1098. goto disable_int;
  1099. }
  1100. }
  1101. return 0;
  1102. disable_int:
  1103. regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1104. SC27XX_FGU_LOW_OVERLOAD_INT, 0);
  1105. return ret;
  1106. }
  1107. #endif
  1108. static const struct dev_pm_ops sc27xx_fgu_pm_ops = {
  1109. SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend, sc27xx_fgu_resume)
  1110. };
  1111. static const struct of_device_id sc27xx_fgu_of_match[] = {
  1112. { .compatible = "sprd,sc2731-fgu", },
  1113. { }
  1114. };
  1115. MODULE_DEVICE_TABLE(of, sc27xx_fgu_of_match);
  1116. static struct platform_driver sc27xx_fgu_driver = {
  1117. .probe = sc27xx_fgu_probe,
  1118. .driver = {
  1119. .name = "sc27xx-fgu",
  1120. .of_match_table = sc27xx_fgu_of_match,
  1121. .pm = &sc27xx_fgu_pm_ops,
  1122. }
  1123. };
  1124. module_platform_driver(sc27xx_fgu_driver);
  1125. MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
  1126. MODULE_LICENSE("GPL v2");