da9052-battery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * Batttery Driver for Dialog DA9052 PMICs
  3. *
  4. * Copyright(c) 2011 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/freezer.h>
  15. #include <linux/fs.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/module.h>
  18. #include <linux/timer.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/mfd/da9052/da9052.h>
  23. #include <linux/mfd/da9052/pdata.h>
  24. #include <linux/mfd/da9052/reg.h>
  25. /* STATIC CONFIGURATION */
  26. #define DA9052_BAT_CUTOFF_VOLT 2800
  27. #define DA9052_BAT_TSH 62000
  28. #define DA9052_BAT_LOW_CAP 4
  29. #define DA9052_AVG_SZ 4
  30. #define DA9052_VC_TBL_SZ 68
  31. #define DA9052_VC_TBL_REF_SZ 3
  32. #define DA9052_ISET_USB_MASK 0x0F
  33. #define DA9052_CHG_USB_ILIM_MASK 0x40
  34. #define DA9052_CHG_LIM_COLS 16
  35. #define DA9052_MEAN(x, y) ((x + y) / 2)
  36. enum charger_type_enum {
  37. DA9052_NOCHARGER = 1,
  38. DA9052_CHARGER,
  39. };
  40. static const u16 da9052_chg_current_lim[2][DA9052_CHG_LIM_COLS] = {
  41. {70, 80, 90, 100, 110, 120, 400, 450,
  42. 500, 550, 600, 650, 700, 900, 1100, 1300},
  43. {80, 90, 100, 110, 120, 400, 450, 500,
  44. 550, 600, 800, 1000, 1200, 1400, 1600, 1800},
  45. };
  46. static const u16 vc_tbl_ref[3] = {10, 25, 40};
  47. /* Lookup table for voltage vs capacity */
  48. static u32 const vc_tbl[3][68][2] = {
  49. /* For temperature 10 degree Celsius */
  50. {
  51. {4082, 100}, {4036, 98},
  52. {4020, 96}, {4008, 95},
  53. {3997, 93}, {3983, 91},
  54. {3964, 90}, {3943, 88},
  55. {3926, 87}, {3912, 85},
  56. {3900, 84}, {3890, 82},
  57. {3881, 80}, {3873, 79},
  58. {3865, 77}, {3857, 76},
  59. {3848, 74}, {3839, 73},
  60. {3829, 71}, {3820, 70},
  61. {3811, 68}, {3802, 67},
  62. {3794, 65}, {3785, 64},
  63. {3778, 62}, {3770, 61},
  64. {3763, 59}, {3756, 58},
  65. {3750, 56}, {3744, 55},
  66. {3738, 53}, {3732, 52},
  67. {3727, 50}, {3722, 49},
  68. {3717, 47}, {3712, 46},
  69. {3708, 44}, {3703, 43},
  70. {3700, 41}, {3696, 40},
  71. {3693, 38}, {3691, 37},
  72. {3688, 35}, {3686, 34},
  73. {3683, 32}, {3681, 31},
  74. {3678, 29}, {3675, 28},
  75. {3672, 26}, {3669, 25},
  76. {3665, 23}, {3661, 22},
  77. {3656, 21}, {3651, 19},
  78. {3645, 18}, {3639, 16},
  79. {3631, 15}, {3622, 13},
  80. {3611, 12}, {3600, 10},
  81. {3587, 9}, {3572, 7},
  82. {3548, 6}, {3503, 5},
  83. {3420, 3}, {3268, 2},
  84. {2992, 1}, {2746, 0}
  85. },
  86. /* For temperature 25 degree Celsius */
  87. {
  88. {4102, 100}, {4065, 98},
  89. {4048, 96}, {4034, 95},
  90. {4021, 93}, {4011, 92},
  91. {4001, 90}, {3986, 88},
  92. {3968, 87}, {3952, 85},
  93. {3938, 84}, {3926, 82},
  94. {3916, 81}, {3908, 79},
  95. {3900, 77}, {3892, 76},
  96. {3883, 74}, {3874, 73},
  97. {3864, 71}, {3855, 70},
  98. {3846, 68}, {3836, 67},
  99. {3827, 65}, {3819, 64},
  100. {3810, 62}, {3801, 61},
  101. {3793, 59}, {3786, 58},
  102. {3778, 56}, {3772, 55},
  103. {3765, 53}, {3759, 52},
  104. {3754, 50}, {3748, 49},
  105. {3743, 47}, {3738, 46},
  106. {3733, 44}, {3728, 43},
  107. {3724, 41}, {3720, 40},
  108. {3716, 38}, {3712, 37},
  109. {3709, 35}, {3706, 34},
  110. {3703, 33}, {3701, 31},
  111. {3698, 30}, {3696, 28},
  112. {3693, 27}, {3690, 25},
  113. {3687, 24}, {3683, 22},
  114. {3680, 21}, {3675, 19},
  115. {3671, 18}, {3666, 17},
  116. {3660, 15}, {3654, 14},
  117. {3647, 12}, {3639, 11},
  118. {3630, 9}, {3621, 8},
  119. {3613, 6}, {3606, 5},
  120. {3597, 4}, {3582, 2},
  121. {3546, 1}, {2747, 0}
  122. },
  123. /* For temperature 40 degree Celsius */
  124. {
  125. {4114, 100}, {4081, 98},
  126. {4065, 96}, {4050, 95},
  127. {4036, 93}, {4024, 92},
  128. {4013, 90}, {4002, 88},
  129. {3990, 87}, {3976, 85},
  130. {3962, 84}, {3950, 82},
  131. {3939, 81}, {3930, 79},
  132. {3921, 77}, {3912, 76},
  133. {3902, 74}, {3893, 73},
  134. {3883, 71}, {3874, 70},
  135. {3865, 68}, {3856, 67},
  136. {3847, 65}, {3838, 64},
  137. {3829, 62}, {3820, 61},
  138. {3812, 59}, {3803, 58},
  139. {3795, 56}, {3787, 55},
  140. {3780, 53}, {3773, 52},
  141. {3767, 50}, {3761, 49},
  142. {3756, 47}, {3751, 46},
  143. {3746, 44}, {3741, 43},
  144. {3736, 41}, {3732, 40},
  145. {3728, 38}, {3724, 37},
  146. {3720, 35}, {3716, 34},
  147. {3713, 33}, {3710, 31},
  148. {3707, 30}, {3704, 28},
  149. {3701, 27}, {3698, 25},
  150. {3695, 24}, {3691, 22},
  151. {3686, 21}, {3681, 19},
  152. {3676, 18}, {3671, 17},
  153. {3666, 15}, {3661, 14},
  154. {3655, 12}, {3648, 11},
  155. {3640, 9}, {3632, 8},
  156. {3622, 6}, {3616, 5},
  157. {3611, 4}, {3604, 2},
  158. {3594, 1}, {2747, 0}
  159. }
  160. };
  161. struct da9052_battery {
  162. struct da9052 *da9052;
  163. struct power_supply *psy;
  164. struct notifier_block nb;
  165. int charger_type;
  166. int status;
  167. int health;
  168. };
  169. static inline int volt_reg_to_mV(int value)
  170. {
  171. return ((value * 1000) / 512) + 2500;
  172. }
  173. static inline int ichg_reg_to_mA(int value)
  174. {
  175. return (value * 3900) / 1000;
  176. }
  177. static int da9052_read_chgend_current(struct da9052_battery *bat,
  178. int *current_mA)
  179. {
  180. int ret;
  181. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  182. return -EINVAL;
  183. ret = da9052_reg_read(bat->da9052, DA9052_ICHG_END_REG);
  184. if (ret < 0)
  185. return ret;
  186. *current_mA = ichg_reg_to_mA(ret & DA9052_ICHGEND_ICHGEND);
  187. return 0;
  188. }
  189. static int da9052_read_chg_current(struct da9052_battery *bat, int *current_mA)
  190. {
  191. int ret;
  192. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  193. return -EINVAL;
  194. ret = da9052_reg_read(bat->da9052, DA9052_ICHG_AV_REG);
  195. if (ret < 0)
  196. return ret;
  197. *current_mA = ichg_reg_to_mA(ret & DA9052_ICHGAV_ICHGAV);
  198. return 0;
  199. }
  200. static int da9052_bat_check_status(struct da9052_battery *bat, int *status)
  201. {
  202. u8 v[2] = {0, 0};
  203. u8 bat_status;
  204. u8 chg_end;
  205. int ret;
  206. int chg_current;
  207. int chg_end_current;
  208. bool dcinsel;
  209. bool dcindet;
  210. bool vbussel;
  211. bool vbusdet;
  212. bool dc;
  213. bool vbus;
  214. ret = da9052_group_read(bat->da9052, DA9052_STATUS_A_REG, 2, v);
  215. if (ret < 0)
  216. return ret;
  217. bat_status = v[0];
  218. chg_end = v[1];
  219. dcinsel = bat_status & DA9052_STATUSA_DCINSEL;
  220. dcindet = bat_status & DA9052_STATUSA_DCINDET;
  221. vbussel = bat_status & DA9052_STATUSA_VBUSSEL;
  222. vbusdet = bat_status & DA9052_STATUSA_VBUSDET;
  223. dc = dcinsel && dcindet;
  224. vbus = vbussel && vbusdet;
  225. /* Preference to WALL(DCIN) charger unit */
  226. if (dc || vbus) {
  227. bat->charger_type = DA9052_CHARGER;
  228. /* If charging end flag is set and Charging current is greater
  229. * than charging end limit then battery is charging
  230. */
  231. if ((chg_end & DA9052_STATUSB_CHGEND) != 0) {
  232. ret = da9052_read_chg_current(bat, &chg_current);
  233. if (ret < 0)
  234. return ret;
  235. ret = da9052_read_chgend_current(bat, &chg_end_current);
  236. if (ret < 0)
  237. return ret;
  238. if (chg_current >= chg_end_current)
  239. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  240. else
  241. bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  242. } else {
  243. /* If Charging end flag is cleared then battery is
  244. * charging
  245. */
  246. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  247. }
  248. } else if (dcindet || vbusdet) {
  249. bat->charger_type = DA9052_CHARGER;
  250. bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  251. } else {
  252. bat->charger_type = DA9052_NOCHARGER;
  253. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  254. }
  255. if (status != NULL)
  256. *status = bat->status;
  257. return 0;
  258. }
  259. static int da9052_bat_read_volt(struct da9052_battery *bat, int *volt_mV)
  260. {
  261. int volt;
  262. volt = da9052_adc_manual_read(bat->da9052, DA9052_ADC_MAN_MUXSEL_VBAT);
  263. if (volt < 0)
  264. return volt;
  265. *volt_mV = volt_reg_to_mV(volt);
  266. return 0;
  267. }
  268. static int da9052_bat_check_presence(struct da9052_battery *bat, int *illegal)
  269. {
  270. int bat_temp;
  271. bat_temp = da9052_adc_read_temp(bat->da9052);
  272. if (bat_temp < 0)
  273. return bat_temp;
  274. if (bat_temp > DA9052_BAT_TSH)
  275. *illegal = 1;
  276. else
  277. *illegal = 0;
  278. return 0;
  279. }
  280. static int da9052_bat_interpolate(int vbat_lower, int vbat_upper,
  281. int level_lower, int level_upper,
  282. int bat_voltage)
  283. {
  284. int tmp;
  285. tmp = ((level_upper - level_lower) * 1000) / (vbat_upper - vbat_lower);
  286. tmp = level_lower + (((bat_voltage - vbat_lower) * tmp) / 1000);
  287. return tmp;
  288. }
  289. static unsigned char da9052_determine_vc_tbl_index(unsigned char adc_temp)
  290. {
  291. int i;
  292. if (adc_temp <= vc_tbl_ref[0])
  293. return 0;
  294. if (adc_temp > vc_tbl_ref[DA9052_VC_TBL_REF_SZ - 1])
  295. return DA9052_VC_TBL_REF_SZ - 1;
  296. for (i = 0; i < DA9052_VC_TBL_REF_SZ - 1; i++) {
  297. if ((adc_temp > vc_tbl_ref[i]) &&
  298. (adc_temp <= DA9052_MEAN(vc_tbl_ref[i], vc_tbl_ref[i + 1])))
  299. return i;
  300. if ((adc_temp > DA9052_MEAN(vc_tbl_ref[i], vc_tbl_ref[i + 1]))
  301. && (adc_temp <= vc_tbl_ref[i]))
  302. return i + 1;
  303. }
  304. /*
  305. * For some reason authors of the driver didn't presume that we can
  306. * end up here. It might be OK, but might be not, no one knows for
  307. * sure. Go check your battery, is it on fire?
  308. */
  309. WARN_ON(1);
  310. return 0;
  311. }
  312. static int da9052_bat_read_capacity(struct da9052_battery *bat, int *capacity)
  313. {
  314. int adc_temp;
  315. int bat_voltage;
  316. int vbat_lower;
  317. int vbat_upper;
  318. int level_upper;
  319. int level_lower;
  320. int ret;
  321. int flag;
  322. int i = 0;
  323. int j;
  324. ret = da9052_bat_read_volt(bat, &bat_voltage);
  325. if (ret < 0)
  326. return ret;
  327. adc_temp = da9052_adc_read_temp(bat->da9052);
  328. if (adc_temp < 0)
  329. return adc_temp;
  330. i = da9052_determine_vc_tbl_index(adc_temp);
  331. if (bat_voltage >= vc_tbl[i][0][0]) {
  332. *capacity = 100;
  333. return 0;
  334. }
  335. if (bat_voltage <= vc_tbl[i][DA9052_VC_TBL_SZ - 1][0]) {
  336. *capacity = 0;
  337. return 0;
  338. }
  339. flag = 0;
  340. for (j = 0; j < (DA9052_VC_TBL_SZ-1); j++) {
  341. if ((bat_voltage <= vc_tbl[i][j][0]) &&
  342. (bat_voltage >= vc_tbl[i][j + 1][0])) {
  343. vbat_upper = vc_tbl[i][j][0];
  344. vbat_lower = vc_tbl[i][j + 1][0];
  345. level_upper = vc_tbl[i][j][1];
  346. level_lower = vc_tbl[i][j + 1][1];
  347. flag = 1;
  348. break;
  349. }
  350. }
  351. if (!flag)
  352. return -EIO;
  353. *capacity = da9052_bat_interpolate(vbat_lower, vbat_upper, level_lower,
  354. level_upper, bat_voltage);
  355. return 0;
  356. }
  357. static int da9052_bat_check_health(struct da9052_battery *bat, int *health)
  358. {
  359. int ret;
  360. int bat_illegal;
  361. int capacity;
  362. ret = da9052_bat_check_presence(bat, &bat_illegal);
  363. if (ret < 0)
  364. return ret;
  365. if (bat_illegal) {
  366. bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  367. return 0;
  368. }
  369. if (bat->health != POWER_SUPPLY_HEALTH_OVERHEAT) {
  370. ret = da9052_bat_read_capacity(bat, &capacity);
  371. if (ret < 0)
  372. return ret;
  373. if (capacity < DA9052_BAT_LOW_CAP)
  374. bat->health = POWER_SUPPLY_HEALTH_DEAD;
  375. else
  376. bat->health = POWER_SUPPLY_HEALTH_GOOD;
  377. }
  378. *health = bat->health;
  379. return 0;
  380. }
  381. static irqreturn_t da9052_bat_irq(int irq, void *data)
  382. {
  383. struct da9052_battery *bat = data;
  384. int virq;
  385. virq = regmap_irq_get_virq(bat->da9052->irq_data, irq);
  386. irq -= virq;
  387. if (irq == DA9052_IRQ_CHGEND)
  388. bat->status = POWER_SUPPLY_STATUS_FULL;
  389. else
  390. da9052_bat_check_status(bat, NULL);
  391. if (irq == DA9052_IRQ_CHGEND || irq == DA9052_IRQ_DCIN ||
  392. irq == DA9052_IRQ_VBUS || irq == DA9052_IRQ_TBAT) {
  393. power_supply_changed(bat->psy);
  394. }
  395. return IRQ_HANDLED;
  396. }
  397. static int da9052_USB_current_notifier(struct notifier_block *nb,
  398. unsigned long events, void *data)
  399. {
  400. u8 row;
  401. u8 col;
  402. int *current_mA = data;
  403. int ret;
  404. struct da9052_battery *bat = container_of(nb, struct da9052_battery,
  405. nb);
  406. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  407. return -EPERM;
  408. ret = da9052_reg_read(bat->da9052, DA9052_CHGBUCK_REG);
  409. if (ret & DA9052_CHG_USB_ILIM_MASK)
  410. return -EPERM;
  411. if (bat->da9052->chip_id == DA9052)
  412. row = 0;
  413. else
  414. row = 1;
  415. if (*current_mA < da9052_chg_current_lim[row][0] ||
  416. *current_mA > da9052_chg_current_lim[row][DA9052_CHG_LIM_COLS - 1])
  417. return -EINVAL;
  418. for (col = 0; col <= DA9052_CHG_LIM_COLS - 1 ; col++) {
  419. if (*current_mA <= da9052_chg_current_lim[row][col])
  420. break;
  421. }
  422. return da9052_reg_update(bat->da9052, DA9052_ISET_REG,
  423. DA9052_ISET_USB_MASK, col);
  424. }
  425. static int da9052_bat_get_property(struct power_supply *psy,
  426. enum power_supply_property psp,
  427. union power_supply_propval *val)
  428. {
  429. int ret;
  430. int illegal;
  431. struct da9052_battery *bat = power_supply_get_drvdata(psy);
  432. ret = da9052_bat_check_presence(bat, &illegal);
  433. if (ret < 0)
  434. return ret;
  435. if (illegal && psp != POWER_SUPPLY_PROP_PRESENT)
  436. return -ENODEV;
  437. switch (psp) {
  438. case POWER_SUPPLY_PROP_STATUS:
  439. ret = da9052_bat_check_status(bat, &val->intval);
  440. break;
  441. case POWER_SUPPLY_PROP_ONLINE:
  442. val->intval =
  443. (bat->charger_type == DA9052_NOCHARGER) ? 0 : 1;
  444. break;
  445. case POWER_SUPPLY_PROP_PRESENT:
  446. ret = da9052_bat_check_presence(bat, &val->intval);
  447. break;
  448. case POWER_SUPPLY_PROP_HEALTH:
  449. ret = da9052_bat_check_health(bat, &val->intval);
  450. break;
  451. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  452. val->intval = DA9052_BAT_CUTOFF_VOLT * 1000;
  453. break;
  454. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  455. ret = da9052_bat_read_volt(bat, &val->intval);
  456. break;
  457. case POWER_SUPPLY_PROP_CURRENT_AVG:
  458. ret = da9052_read_chg_current(bat, &val->intval);
  459. break;
  460. case POWER_SUPPLY_PROP_CAPACITY:
  461. ret = da9052_bat_read_capacity(bat, &val->intval);
  462. break;
  463. case POWER_SUPPLY_PROP_TEMP:
  464. val->intval = da9052_adc_read_temp(bat->da9052);
  465. ret = val->intval;
  466. break;
  467. case POWER_SUPPLY_PROP_TECHNOLOGY:
  468. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  469. break;
  470. default:
  471. return -EINVAL;
  472. }
  473. return ret;
  474. }
  475. static enum power_supply_property da9052_bat_props[] = {
  476. POWER_SUPPLY_PROP_STATUS,
  477. POWER_SUPPLY_PROP_ONLINE,
  478. POWER_SUPPLY_PROP_PRESENT,
  479. POWER_SUPPLY_PROP_HEALTH,
  480. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  481. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  482. POWER_SUPPLY_PROP_CURRENT_AVG,
  483. POWER_SUPPLY_PROP_CAPACITY,
  484. POWER_SUPPLY_PROP_TEMP,
  485. POWER_SUPPLY_PROP_TECHNOLOGY,
  486. };
  487. static struct power_supply_desc psy_desc = {
  488. .name = "da9052-bat",
  489. .type = POWER_SUPPLY_TYPE_BATTERY,
  490. .properties = da9052_bat_props,
  491. .num_properties = ARRAY_SIZE(da9052_bat_props),
  492. .get_property = da9052_bat_get_property,
  493. };
  494. static char *da9052_bat_irqs[] = {
  495. "BATT TEMP",
  496. "DCIN DET",
  497. "DCIN REM",
  498. "VBUS DET",
  499. "VBUS REM",
  500. "CHG END",
  501. };
  502. static int da9052_bat_irq_bits[] = {
  503. DA9052_IRQ_TBAT,
  504. DA9052_IRQ_DCIN,
  505. DA9052_IRQ_DCINREM,
  506. DA9052_IRQ_VBUS,
  507. DA9052_IRQ_VBUSREM,
  508. DA9052_IRQ_CHGEND,
  509. };
  510. static s32 da9052_bat_probe(struct platform_device *pdev)
  511. {
  512. struct da9052_pdata *pdata;
  513. struct da9052_battery *bat;
  514. struct power_supply_config psy_cfg = {};
  515. int ret;
  516. int i;
  517. bat = devm_kzalloc(&pdev->dev, sizeof(struct da9052_battery),
  518. GFP_KERNEL);
  519. if (!bat)
  520. return -ENOMEM;
  521. psy_cfg.drv_data = bat;
  522. bat->da9052 = dev_get_drvdata(pdev->dev.parent);
  523. bat->charger_type = DA9052_NOCHARGER;
  524. bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  525. bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  526. bat->nb.notifier_call = da9052_USB_current_notifier;
  527. pdata = bat->da9052->dev->platform_data;
  528. if (pdata != NULL && pdata->use_for_apm)
  529. psy_desc.use_for_apm = pdata->use_for_apm;
  530. else
  531. psy_desc.use_for_apm = 1;
  532. for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++) {
  533. ret = da9052_request_irq(bat->da9052,
  534. da9052_bat_irq_bits[i], da9052_bat_irqs[i],
  535. da9052_bat_irq, bat);
  536. if (ret != 0) {
  537. dev_err(bat->da9052->dev,
  538. "DA9052 failed to request %s IRQ: %d\n",
  539. da9052_bat_irqs[i], ret);
  540. goto err;
  541. }
  542. }
  543. bat->psy = power_supply_register(&pdev->dev, &psy_desc, &psy_cfg);
  544. if (IS_ERR(bat->psy)) {
  545. ret = PTR_ERR(bat->psy);
  546. goto err;
  547. }
  548. platform_set_drvdata(pdev, bat);
  549. return 0;
  550. err:
  551. while (--i >= 0)
  552. da9052_free_irq(bat->da9052, da9052_bat_irq_bits[i], bat);
  553. return ret;
  554. }
  555. static int da9052_bat_remove(struct platform_device *pdev)
  556. {
  557. int i;
  558. struct da9052_battery *bat = platform_get_drvdata(pdev);
  559. for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++)
  560. da9052_free_irq(bat->da9052, da9052_bat_irq_bits[i], bat);
  561. power_supply_unregister(bat->psy);
  562. return 0;
  563. }
  564. static struct platform_driver da9052_bat_driver = {
  565. .probe = da9052_bat_probe,
  566. .remove = da9052_bat_remove,
  567. .driver = {
  568. .name = "da9052-bat",
  569. },
  570. };
  571. module_platform_driver(da9052_bat_driver);
  572. MODULE_DESCRIPTION("DA9052 BAT Device Driver");
  573. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  574. MODULE_LICENSE("GPL");
  575. MODULE_ALIAS("platform:da9052-bat");