sc27xx_fuel_gauge.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
  601. enum power_supply_property psp)
  602. {
  603. return psp == POWER_SUPPLY_PROP_CAPACITY ||
  604. psp == POWER_SUPPLY_PROP_CALIBRATE ||
  605. psp == POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
  606. }
  607. static enum power_supply_property sc27xx_fgu_props[] = {
  608. POWER_SUPPLY_PROP_STATUS,
  609. POWER_SUPPLY_PROP_HEALTH,
  610. POWER_SUPPLY_PROP_PRESENT,
  611. POWER_SUPPLY_PROP_TEMP,
  612. POWER_SUPPLY_PROP_TECHNOLOGY,
  613. POWER_SUPPLY_PROP_CAPACITY,
  614. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  615. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  616. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  617. POWER_SUPPLY_PROP_VOLTAGE_BOOT,
  618. POWER_SUPPLY_PROP_CURRENT_NOW,
  619. POWER_SUPPLY_PROP_CURRENT_AVG,
  620. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  621. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  622. POWER_SUPPLY_PROP_CALIBRATE,
  623. POWER_SUPPLY_PROP_CHARGE_NOW
  624. };
  625. static const struct power_supply_desc sc27xx_fgu_desc = {
  626. .name = "sc27xx-fgu",
  627. .type = POWER_SUPPLY_TYPE_BATTERY,
  628. .properties = sc27xx_fgu_props,
  629. .num_properties = ARRAY_SIZE(sc27xx_fgu_props),
  630. .get_property = sc27xx_fgu_get_property,
  631. .set_property = sc27xx_fgu_set_property,
  632. .external_power_changed = power_supply_changed,
  633. .property_is_writeable = sc27xx_fgu_property_is_writeable,
  634. .no_thermal = true,
  635. };
  636. static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
  637. {
  638. int ret;
  639. data->init_cap = cap;
  640. ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt);
  641. if (ret)
  642. dev_err(data->dev, "failed to get init coulomb counter\n");
  643. }
  644. static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
  645. int cap, bool int_mode)
  646. {
  647. int ret, ocv, chg_sts, adc;
  648. ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
  649. if (ret) {
  650. dev_err(data->dev, "get battery ocv error.\n");
  651. return;
  652. }
  653. ret = sc27xx_fgu_get_status(data, &chg_sts);
  654. if (ret) {
  655. dev_err(data->dev, "get charger status error.\n");
  656. return;
  657. }
  658. /*
  659. * If we are in charging mode, then we do not need to calibrate the
  660. * lower capacity.
  661. */
  662. if (chg_sts == POWER_SUPPLY_STATUS_CHARGING)
  663. return;
  664. if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) {
  665. /*
  666. * If current OCV value is larger than the max OCV value in
  667. * OCV table, or the current capacity is larger than 100,
  668. * we should force the inititial capacity to 100.
  669. */
  670. sc27xx_fgu_adjust_cap(data, 100);
  671. } else if (ocv <= data->cap_table[data->table_len - 1].ocv) {
  672. /*
  673. * If current OCV value is leass than the minimum OCV value in
  674. * OCV table, we should force the inititial capacity to 0.
  675. */
  676. sc27xx_fgu_adjust_cap(data, 0);
  677. } else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) ||
  678. (ocv > data->min_volt && cap <= data->alarm_cap)) {
  679. /*
  680. * If current OCV value is not matchable with current capacity,
  681. * we should re-calculate current capacity by looking up the
  682. * OCV table.
  683. */
  684. int cur_cap = power_supply_ocv2cap_simple(data->cap_table,
  685. data->table_len, ocv);
  686. sc27xx_fgu_adjust_cap(data, cur_cap);
  687. } else if (ocv <= data->min_volt) {
  688. /*
  689. * If current OCV value is less than the low alarm voltage, but
  690. * current capacity is larger than the alarm capacity, we should
  691. * adjust the inititial capacity to alarm capacity.
  692. */
  693. if (cap > data->alarm_cap) {
  694. sc27xx_fgu_adjust_cap(data, data->alarm_cap);
  695. } else {
  696. int cur_cap;
  697. /*
  698. * If current capacity is equal with 0 or less than 0
  699. * (some error occurs), we should adjust inititial
  700. * capacity to the capacity corresponding to current OCV
  701. * value.
  702. */
  703. cur_cap = power_supply_ocv2cap_simple(data->cap_table,
  704. data->table_len,
  705. ocv);
  706. sc27xx_fgu_adjust_cap(data, cur_cap);
  707. }
  708. if (!int_mode)
  709. return;
  710. /*
  711. * After adjusting the battery capacity, we should set the
  712. * lowest alarm voltage instead.
  713. */
  714. data->min_volt = data->cap_table[data->table_len - 1].ocv;
  715. data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
  716. data->table_len,
  717. data->min_volt);
  718. adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
  719. regmap_update_bits(data->regmap,
  720. data->base + SC27XX_FGU_LOW_OVERLOAD,
  721. SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
  722. }
  723. }
  724. static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
  725. {
  726. struct sc27xx_fgu_data *data = dev_id;
  727. int ret, cap;
  728. u32 status;
  729. mutex_lock(&data->lock);
  730. ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
  731. &status);
  732. if (ret)
  733. goto out;
  734. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
  735. status, status);
  736. if (ret)
  737. goto out;
  738. /*
  739. * When low overload voltage interrupt happens, we should calibrate the
  740. * battery capacity in lower voltage stage.
  741. */
  742. if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
  743. goto out;
  744. ret = sc27xx_fgu_get_capacity(data, &cap);
  745. if (ret)
  746. goto out;
  747. sc27xx_fgu_capacity_calibration(data, cap, true);
  748. out:
  749. mutex_unlock(&data->lock);
  750. power_supply_changed(data->battery);
  751. return IRQ_HANDLED;
  752. }
  753. static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id)
  754. {
  755. struct sc27xx_fgu_data *data = dev_id;
  756. int state;
  757. mutex_lock(&data->lock);
  758. state = gpiod_get_value_cansleep(data->gpiod);
  759. if (state < 0) {
  760. dev_err(data->dev, "failed to get gpio state\n");
  761. mutex_unlock(&data->lock);
  762. return IRQ_RETVAL(state);
  763. }
  764. data->bat_present = !!state;
  765. mutex_unlock(&data->lock);
  766. power_supply_changed(data->battery);
  767. return IRQ_HANDLED;
  768. }
  769. static void sc27xx_fgu_disable(void *_data)
  770. {
  771. struct sc27xx_fgu_data *data = _data;
  772. regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
  773. regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
  774. }
  775. static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
  776. {
  777. /*
  778. * Get current capacity (mAh) = battery total capacity (mAh) *
  779. * current capacity percent (capacity / 100).
  780. */
  781. int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100);
  782. /*
  783. * Convert current capacity (mAh) to coulomb counter according to the
  784. * formula: 1 mAh =3.6 coulomb.
  785. */
  786. return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10);
  787. }
  788. static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
  789. {
  790. struct nvmem_cell *cell;
  791. int calib_data, cal_4200mv;
  792. void *buf;
  793. size_t len;
  794. cell = nvmem_cell_get(data->dev, "fgu_calib");
  795. if (IS_ERR(cell))
  796. return PTR_ERR(cell);
  797. buf = nvmem_cell_read(cell, &len);
  798. nvmem_cell_put(cell);
  799. if (IS_ERR(buf))
  800. return PTR_ERR(buf);
  801. memcpy(&calib_data, buf, min(len, sizeof(u32)));
  802. /*
  803. * Get the ADC value corresponding to 4200 mV from eFuse controller
  804. * according to below formula. Then convert to ADC values corresponding
  805. * to 1000 mV and 1000 mA.
  806. */
  807. cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256;
  808. data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42);
  809. data->cur_1000ma_adc =
  810. DIV_ROUND_CLOSEST(data->vol_1000mv_adc * 4 * data->calib_resist,
  811. SC27XX_FGU_IDEAL_RESISTANCE);
  812. kfree(buf);
  813. return 0;
  814. }
  815. static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
  816. {
  817. struct power_supply_battery_info *info;
  818. struct power_supply_battery_ocv_table *table;
  819. int ret, delta_clbcnt, alarm_adc;
  820. ret = power_supply_get_battery_info(data->battery, &info);
  821. if (ret) {
  822. dev_err(data->dev, "failed to get battery information\n");
  823. return ret;
  824. }
  825. data->total_cap = info->charge_full_design_uah / 1000;
  826. data->max_volt = info->constant_charge_voltage_max_uv / 1000;
  827. data->internal_resist = info->factory_internal_resistance_uohm / 1000;
  828. data->min_volt = info->voltage_min_design_uv;
  829. /*
  830. * For SC27XX fuel gauge device, we only use one ocv-capacity
  831. * table in normal temperature 20 Celsius.
  832. */
  833. table = power_supply_find_ocv2cap_table(info, 20, &data->table_len);
  834. if (!table)
  835. return -EINVAL;
  836. data->cap_table = devm_kmemdup(data->dev, table,
  837. data->table_len * sizeof(*table),
  838. GFP_KERNEL);
  839. if (!data->cap_table) {
  840. power_supply_put_battery_info(data->battery, info);
  841. return -ENOMEM;
  842. }
  843. data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
  844. data->table_len,
  845. data->min_volt);
  846. if (!data->alarm_cap)
  847. data->alarm_cap += 1;
  848. data->resist_table_len = info->resist_table_size;
  849. if (data->resist_table_len > 0) {
  850. data->resist_table = devm_kmemdup(data->dev, info->resist_table,
  851. data->resist_table_len *
  852. sizeof(struct power_supply_resistance_temp_table),
  853. GFP_KERNEL);
  854. if (!data->resist_table) {
  855. power_supply_put_battery_info(data->battery, info);
  856. return -ENOMEM;
  857. }
  858. }
  859. power_supply_put_battery_info(data->battery, info);
  860. ret = sc27xx_fgu_calibration(data);
  861. if (ret)
  862. return ret;
  863. /* Enable the FGU module */
  864. ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0,
  865. SC27XX_FGU_EN, SC27XX_FGU_EN);
  866. if (ret) {
  867. dev_err(data->dev, "failed to enable fgu\n");
  868. return ret;
  869. }
  870. /* Enable the FGU RTC clock to make it work */
  871. ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0,
  872. SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN);
  873. if (ret) {
  874. dev_err(data->dev, "failed to enable fgu RTC clock\n");
  875. goto disable_fgu;
  876. }
  877. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
  878. SC27XX_FGU_INT_MASK, SC27XX_FGU_INT_MASK);
  879. if (ret) {
  880. dev_err(data->dev, "failed to clear interrupt status\n");
  881. goto disable_clk;
  882. }
  883. /*
  884. * Set the voltage low overload threshold, which means when the battery
  885. * voltage is lower than this threshold, the controller will generate
  886. * one interrupt to notify.
  887. */
  888. alarm_adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
  889. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
  890. SC27XX_FGU_LOW_OVERLOAD_MASK, alarm_adc);
  891. if (ret) {
  892. dev_err(data->dev, "failed to set fgu low overload\n");
  893. goto disable_clk;
  894. }
  895. /*
  896. * Set the coulomb counter delta threshold, that means when the coulomb
  897. * counter change is multiples of the delta threshold, the controller
  898. * will generate one interrupt to notify the users to update the battery
  899. * capacity. Now we set the delta threshold as a counter value of 1%
  900. * capacity.
  901. */
  902. delta_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, 1);
  903. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTL,
  904. SC27XX_FGU_CLBCNT_MASK, delta_clbcnt);
  905. if (ret) {
  906. dev_err(data->dev, "failed to set low delta coulomb counter\n");
  907. goto disable_clk;
  908. }
  909. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTH,
  910. SC27XX_FGU_CLBCNT_MASK,
  911. delta_clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
  912. if (ret) {
  913. dev_err(data->dev, "failed to set high delta coulomb counter\n");
  914. goto disable_clk;
  915. }
  916. /*
  917. * Get the boot battery capacity when system powers on, which is used to
  918. * initialize the coulomb counter. After that, we can read the coulomb
  919. * counter to measure the battery capacity.
  920. */
  921. ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap);
  922. if (ret) {
  923. dev_err(data->dev, "failed to get boot capacity\n");
  924. goto disable_clk;
  925. }
  926. /*
  927. * Convert battery capacity to the corresponding initial coulomb counter
  928. * and set into coulomb counter registers.
  929. */
  930. data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
  931. ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt);
  932. if (ret) {
  933. dev_err(data->dev, "failed to initialize coulomb counter\n");
  934. goto disable_clk;
  935. }
  936. return 0;
  937. disable_clk:
  938. regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
  939. disable_fgu:
  940. regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
  941. return ret;
  942. }
  943. static int sc27xx_fgu_probe(struct platform_device *pdev)
  944. {
  945. struct device *dev = &pdev->dev;
  946. struct device_node *np = dev->of_node;
  947. struct power_supply_config fgu_cfg = { };
  948. struct sc27xx_fgu_data *data;
  949. int ret, irq;
  950. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  951. if (!data)
  952. return -ENOMEM;
  953. data->regmap = dev_get_regmap(dev->parent, NULL);
  954. if (!data->regmap) {
  955. dev_err(dev, "failed to get regmap\n");
  956. return -ENODEV;
  957. }
  958. ret = device_property_read_u32(dev, "reg", &data->base);
  959. if (ret) {
  960. dev_err(dev, "failed to get fgu address\n");
  961. return ret;
  962. }
  963. ret = device_property_read_u32(&pdev->dev,
  964. "sprd,calib-resistance-micro-ohms",
  965. &data->calib_resist);
  966. if (ret) {
  967. dev_err(&pdev->dev,
  968. "failed to get fgu calibration resistance\n");
  969. return ret;
  970. }
  971. data->channel = devm_iio_channel_get(dev, "bat-temp");
  972. if (IS_ERR(data->channel)) {
  973. dev_err(dev, "failed to get IIO channel\n");
  974. return PTR_ERR(data->channel);
  975. }
  976. data->charge_chan = devm_iio_channel_get(dev, "charge-vol");
  977. if (IS_ERR(data->charge_chan)) {
  978. dev_err(dev, "failed to get charge IIO channel\n");
  979. return PTR_ERR(data->charge_chan);
  980. }
  981. data->gpiod = devm_gpiod_get(dev, "bat-detect", GPIOD_IN);
  982. if (IS_ERR(data->gpiod)) {
  983. dev_err(dev, "failed to get battery detection GPIO\n");
  984. return PTR_ERR(data->gpiod);
  985. }
  986. ret = gpiod_get_value_cansleep(data->gpiod);
  987. if (ret < 0) {
  988. dev_err(dev, "failed to get gpio state\n");
  989. return ret;
  990. }
  991. data->bat_present = !!ret;
  992. mutex_init(&data->lock);
  993. data->dev = dev;
  994. platform_set_drvdata(pdev, data);
  995. fgu_cfg.drv_data = data;
  996. fgu_cfg.of_node = np;
  997. data->battery = devm_power_supply_register(dev, &sc27xx_fgu_desc,
  998. &fgu_cfg);
  999. if (IS_ERR(data->battery)) {
  1000. dev_err(dev, "failed to register power supply\n");
  1001. return PTR_ERR(data->battery);
  1002. }
  1003. ret = sc27xx_fgu_hw_init(data);
  1004. if (ret) {
  1005. dev_err(dev, "failed to initialize fgu hardware\n");
  1006. return ret;
  1007. }
  1008. ret = devm_add_action_or_reset(dev, sc27xx_fgu_disable, data);
  1009. if (ret) {
  1010. dev_err(dev, "failed to add fgu disable action\n");
  1011. return ret;
  1012. }
  1013. irq = platform_get_irq(pdev, 0);
  1014. if (irq < 0)
  1015. return irq;
  1016. ret = devm_request_threaded_irq(data->dev, irq, NULL,
  1017. sc27xx_fgu_interrupt,
  1018. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1019. pdev->name, data);
  1020. if (ret) {
  1021. dev_err(data->dev, "failed to request fgu IRQ\n");
  1022. return ret;
  1023. }
  1024. irq = gpiod_to_irq(data->gpiod);
  1025. if (irq < 0) {
  1026. dev_err(dev, "failed to translate GPIO to IRQ\n");
  1027. return irq;
  1028. }
  1029. ret = devm_request_threaded_irq(dev, irq, NULL,
  1030. sc27xx_fgu_bat_detection,
  1031. IRQF_ONESHOT | IRQF_TRIGGER_RISING |
  1032. IRQF_TRIGGER_FALLING,
  1033. pdev->name, data);
  1034. if (ret) {
  1035. dev_err(dev, "failed to request IRQ\n");
  1036. return ret;
  1037. }
  1038. return 0;
  1039. }
  1040. #ifdef CONFIG_PM_SLEEP
  1041. static int sc27xx_fgu_resume(struct device *dev)
  1042. {
  1043. struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
  1044. int ret;
  1045. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1046. SC27XX_FGU_LOW_OVERLOAD_INT |
  1047. SC27XX_FGU_CLBCNT_DELTA_INT, 0);
  1048. if (ret) {
  1049. dev_err(data->dev, "failed to disable fgu interrupts\n");
  1050. return ret;
  1051. }
  1052. return 0;
  1053. }
  1054. static int sc27xx_fgu_suspend(struct device *dev)
  1055. {
  1056. struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
  1057. int ret, status, ocv;
  1058. ret = sc27xx_fgu_get_status(data, &status);
  1059. if (ret)
  1060. return ret;
  1061. /*
  1062. * If we are charging, then no need to enable the FGU interrupts to
  1063. * adjust the battery capacity.
  1064. */
  1065. if (status != POWER_SUPPLY_STATUS_NOT_CHARGING &&
  1066. status != POWER_SUPPLY_STATUS_DISCHARGING)
  1067. return 0;
  1068. ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1069. SC27XX_FGU_LOW_OVERLOAD_INT,
  1070. SC27XX_FGU_LOW_OVERLOAD_INT);
  1071. if (ret) {
  1072. dev_err(data->dev, "failed to enable low voltage interrupt\n");
  1073. return ret;
  1074. }
  1075. ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
  1076. if (ret)
  1077. goto disable_int;
  1078. /*
  1079. * If current OCV is less than the minimum voltage, we should enable the
  1080. * coulomb counter threshold interrupt to notify events to adjust the
  1081. * battery capacity.
  1082. */
  1083. if (ocv < data->min_volt) {
  1084. ret = regmap_update_bits(data->regmap,
  1085. data->base + SC27XX_FGU_INT_EN,
  1086. SC27XX_FGU_CLBCNT_DELTA_INT,
  1087. SC27XX_FGU_CLBCNT_DELTA_INT);
  1088. if (ret) {
  1089. dev_err(data->dev,
  1090. "failed to enable coulomb threshold int\n");
  1091. goto disable_int;
  1092. }
  1093. }
  1094. return 0;
  1095. disable_int:
  1096. regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
  1097. SC27XX_FGU_LOW_OVERLOAD_INT, 0);
  1098. return ret;
  1099. }
  1100. #endif
  1101. static const struct dev_pm_ops sc27xx_fgu_pm_ops = {
  1102. SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend, sc27xx_fgu_resume)
  1103. };
  1104. static const struct of_device_id sc27xx_fgu_of_match[] = {
  1105. { .compatible = "sprd,sc2731-fgu", },
  1106. { }
  1107. };
  1108. MODULE_DEVICE_TABLE(of, sc27xx_fgu_of_match);
  1109. static struct platform_driver sc27xx_fgu_driver = {
  1110. .probe = sc27xx_fgu_probe,
  1111. .driver = {
  1112. .name = "sc27xx-fgu",
  1113. .of_match_table = sc27xx_fgu_of_match,
  1114. .pm = &sc27xx_fgu_pm_ops,
  1115. }
  1116. };
  1117. module_platform_driver(sc27xx_fgu_driver);
  1118. MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
  1119. MODULE_LICENSE("GPL v2");