da9052-battery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. 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; 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. static int da9052_bat_read_capacity(struct da9052_battery *bat, int *capacity)
  306. {
  307. int adc_temp;
  308. int bat_voltage;
  309. int vbat_lower;
  310. int vbat_upper;
  311. int level_upper;
  312. int level_lower;
  313. int ret;
  314. int flag;
  315. int i = 0;
  316. int j;
  317. ret = da9052_bat_read_volt(bat, &bat_voltage);
  318. if (ret < 0)
  319. return ret;
  320. adc_temp = da9052_adc_read_temp(bat->da9052);
  321. if (adc_temp < 0)
  322. return adc_temp;
  323. i = da9052_determine_vc_tbl_index(adc_temp);
  324. if (bat_voltage >= vc_tbl[i][0][0]) {
  325. *capacity = 100;
  326. return 0;
  327. }
  328. if (bat_voltage <= vc_tbl[i][DA9052_VC_TBL_SZ - 1][0]) {
  329. *capacity = 0;
  330. return 0;
  331. }
  332. flag = 0;
  333. for (j = 0; j < (DA9052_VC_TBL_SZ-1); j++) {
  334. if ((bat_voltage <= vc_tbl[i][j][0]) &&
  335. (bat_voltage >= vc_tbl[i][j + 1][0])) {
  336. vbat_upper = vc_tbl[i][j][0];
  337. vbat_lower = vc_tbl[i][j + 1][0];
  338. level_upper = vc_tbl[i][j][1];
  339. level_lower = vc_tbl[i][j + 1][1];
  340. flag = 1;
  341. break;
  342. }
  343. }
  344. if (!flag)
  345. return -EIO;
  346. *capacity = da9052_bat_interpolate(vbat_lower, vbat_upper, level_lower,
  347. level_upper, bat_voltage);
  348. return 0;
  349. }
  350. static int da9052_bat_check_health(struct da9052_battery *bat, int *health)
  351. {
  352. int ret;
  353. int bat_illegal;
  354. int capacity;
  355. ret = da9052_bat_check_presence(bat, &bat_illegal);
  356. if (ret < 0)
  357. return ret;
  358. if (bat_illegal) {
  359. bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  360. return 0;
  361. }
  362. if (bat->health != POWER_SUPPLY_HEALTH_OVERHEAT) {
  363. ret = da9052_bat_read_capacity(bat, &capacity);
  364. if (ret < 0)
  365. return ret;
  366. if (capacity < DA9052_BAT_LOW_CAP)
  367. bat->health = POWER_SUPPLY_HEALTH_DEAD;
  368. else
  369. bat->health = POWER_SUPPLY_HEALTH_GOOD;
  370. }
  371. *health = bat->health;
  372. return 0;
  373. }
  374. static irqreturn_t da9052_bat_irq(int irq, void *data)
  375. {
  376. struct da9052_battery *bat = data;
  377. irq -= bat->da9052->irq_base;
  378. if (irq == DA9052_IRQ_CHGEND)
  379. bat->status = POWER_SUPPLY_STATUS_FULL;
  380. else
  381. da9052_bat_check_status(bat, NULL);
  382. if (irq == DA9052_IRQ_CHGEND || irq == DA9052_IRQ_DCIN ||
  383. irq == DA9052_IRQ_VBUS || irq == DA9052_IRQ_TBAT) {
  384. power_supply_changed(&bat->psy);
  385. }
  386. return IRQ_HANDLED;
  387. }
  388. static int da9052_USB_current_notifier(struct notifier_block *nb,
  389. unsigned long events, void *data)
  390. {
  391. u8 row;
  392. u8 col;
  393. int *current_mA = data;
  394. int ret;
  395. struct da9052_battery *bat = container_of(nb, struct da9052_battery,
  396. nb);
  397. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  398. return -EPERM;
  399. ret = da9052_reg_read(bat->da9052, DA9052_CHGBUCK_REG);
  400. if (ret & DA9052_CHG_USB_ILIM_MASK)
  401. return -EPERM;
  402. if (bat->da9052->chip_id == DA9052)
  403. row = 0;
  404. else
  405. row = 1;
  406. if (*current_mA < da9052_chg_current_lim[row][0] ||
  407. *current_mA > da9052_chg_current_lim[row][DA9052_CHG_LIM_COLS - 1])
  408. return -EINVAL;
  409. for (col = 0; col <= DA9052_CHG_LIM_COLS - 1 ; col++) {
  410. if (*current_mA <= da9052_chg_current_lim[row][col])
  411. break;
  412. }
  413. return da9052_reg_update(bat->da9052, DA9052_ISET_REG,
  414. DA9052_ISET_USB_MASK, col);
  415. }
  416. static int da9052_bat_get_property(struct power_supply *psy,
  417. enum power_supply_property psp,
  418. union power_supply_propval *val)
  419. {
  420. int ret;
  421. int illegal;
  422. struct da9052_battery *bat = container_of(psy, struct da9052_battery,
  423. psy);
  424. ret = da9052_bat_check_presence(bat, &illegal);
  425. if (ret < 0)
  426. return ret;
  427. if (illegal && psp != POWER_SUPPLY_PROP_PRESENT)
  428. return -ENODEV;
  429. switch (psp) {
  430. case POWER_SUPPLY_PROP_STATUS:
  431. ret = da9052_bat_check_status(bat, &val->intval);
  432. break;
  433. case POWER_SUPPLY_PROP_ONLINE:
  434. val->intval =
  435. (bat->charger_type == DA9052_NOCHARGER) ? 0 : 1;
  436. break;
  437. case POWER_SUPPLY_PROP_PRESENT:
  438. ret = da9052_bat_check_presence(bat, &val->intval);
  439. break;
  440. case POWER_SUPPLY_PROP_HEALTH:
  441. ret = da9052_bat_check_health(bat, &val->intval);
  442. break;
  443. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  444. val->intval = DA9052_BAT_CUTOFF_VOLT * 1000;
  445. break;
  446. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  447. ret = da9052_bat_read_volt(bat, &val->intval);
  448. break;
  449. case POWER_SUPPLY_PROP_CURRENT_AVG:
  450. ret = da9052_read_chg_current(bat, &val->intval);
  451. break;
  452. case POWER_SUPPLY_PROP_CAPACITY:
  453. ret = da9052_bat_read_capacity(bat, &val->intval);
  454. break;
  455. case POWER_SUPPLY_PROP_TEMP:
  456. val->intval = da9052_adc_read_temp(bat->da9052);
  457. ret = val->intval;
  458. break;
  459. case POWER_SUPPLY_PROP_TECHNOLOGY:
  460. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. return ret;
  466. }
  467. static enum power_supply_property da9052_bat_props[] = {
  468. POWER_SUPPLY_PROP_STATUS,
  469. POWER_SUPPLY_PROP_ONLINE,
  470. POWER_SUPPLY_PROP_PRESENT,
  471. POWER_SUPPLY_PROP_HEALTH,
  472. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  473. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  474. POWER_SUPPLY_PROP_CURRENT_AVG,
  475. POWER_SUPPLY_PROP_CAPACITY,
  476. POWER_SUPPLY_PROP_TEMP,
  477. POWER_SUPPLY_PROP_TECHNOLOGY,
  478. };
  479. static struct power_supply template_battery = {
  480. .name = "da9052-bat",
  481. .type = POWER_SUPPLY_TYPE_BATTERY,
  482. .properties = da9052_bat_props,
  483. .num_properties = ARRAY_SIZE(da9052_bat_props),
  484. .get_property = da9052_bat_get_property,
  485. };
  486. static const char *const da9052_bat_irqs[] = {
  487. "BATT TEMP",
  488. "DCIN DET",
  489. "DCIN REM",
  490. "VBUS DET",
  491. "VBUS REM",
  492. "CHG END",
  493. };
  494. static s32 __devinit da9052_bat_probe(struct platform_device *pdev)
  495. {
  496. struct da9052_pdata *pdata;
  497. struct da9052_battery *bat;
  498. int ret;
  499. int irq;
  500. int i;
  501. bat = kzalloc(sizeof(struct da9052_battery), GFP_KERNEL);
  502. if (!bat)
  503. return -ENOMEM;
  504. bat->da9052 = dev_get_drvdata(pdev->dev.parent);
  505. bat->psy = template_battery;
  506. bat->charger_type = DA9052_NOCHARGER;
  507. bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  508. bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  509. bat->nb.notifier_call = da9052_USB_current_notifier;
  510. pdata = bat->da9052->dev->platform_data;
  511. if (pdata != NULL && pdata->use_for_apm)
  512. bat->psy.use_for_apm = pdata->use_for_apm;
  513. else
  514. bat->psy.use_for_apm = 1;
  515. for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++) {
  516. irq = platform_get_irq_byname(pdev, da9052_bat_irqs[i]);
  517. ret = request_threaded_irq(bat->da9052->irq_base + irq,
  518. NULL, da9052_bat_irq,
  519. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  520. da9052_bat_irqs[i], bat);
  521. if (ret != 0) {
  522. dev_err(bat->da9052->dev,
  523. "DA9052 failed to request %s IRQ %d: %d\n",
  524. da9052_bat_irqs[i], irq, ret);
  525. goto err;
  526. }
  527. }
  528. ret = power_supply_register(&pdev->dev, &bat->psy);
  529. if (ret)
  530. goto err;
  531. platform_set_drvdata(pdev, bat);
  532. return 0;
  533. err:
  534. for (; i >= 0; i--) {
  535. irq = platform_get_irq_byname(pdev, da9052_bat_irqs[i]);
  536. free_irq(bat->da9052->irq_base + irq, bat);
  537. }
  538. kfree(bat);
  539. return ret;
  540. }
  541. static int __devexit da9052_bat_remove(struct platform_device *pdev)
  542. {
  543. int i;
  544. int irq;
  545. struct da9052_battery *bat = platform_get_drvdata(pdev);
  546. for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++) {
  547. irq = platform_get_irq_byname(pdev, da9052_bat_irqs[i]);
  548. free_irq(bat->da9052->irq_base + irq, bat);
  549. }
  550. power_supply_unregister(&bat->psy);
  551. kfree(bat);
  552. return 0;
  553. }
  554. static struct platform_driver da9052_bat_driver = {
  555. .probe = da9052_bat_probe,
  556. .remove = __devexit_p(da9052_bat_remove),
  557. .driver = {
  558. .name = "da9052-bat",
  559. .owner = THIS_MODULE,
  560. },
  561. };
  562. module_platform_driver(da9052_bat_driver);
  563. MODULE_DESCRIPTION("DA9052 BAT Device Driver");
  564. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  565. MODULE_LICENSE("GPL");
  566. MODULE_ALIAS("platform:da9052-bat");