da9030_battery.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Battery charger driver for Dialog Semiconductor DA9030
  3. *
  4. * Copyright (C) 2008 Compulab, Ltd.
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/device.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/mfd/da903x.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/seq_file.h>
  23. #define DA9030_FAULT_LOG 0x0a
  24. #define DA9030_FAULT_LOG_OVER_TEMP (1 << 7)
  25. #define DA9030_FAULT_LOG_VBAT_OVER (1 << 4)
  26. #define DA9030_CHARGE_CONTROL 0x28
  27. #define DA9030_CHRG_CHARGER_ENABLE (1 << 7)
  28. #define DA9030_ADC_MAN_CONTROL 0x30
  29. #define DA9030_ADC_TBATREF_ENABLE (1 << 5)
  30. #define DA9030_ADC_LDO_INT_ENABLE (1 << 4)
  31. #define DA9030_ADC_AUTO_CONTROL 0x31
  32. #define DA9030_ADC_TBAT_ENABLE (1 << 5)
  33. #define DA9030_ADC_VBAT_IN_TXON (1 << 4)
  34. #define DA9030_ADC_VCH_ENABLE (1 << 3)
  35. #define DA9030_ADC_ICH_ENABLE (1 << 2)
  36. #define DA9030_ADC_VBAT_ENABLE (1 << 1)
  37. #define DA9030_ADC_AUTO_SLEEP_ENABLE (1 << 0)
  38. #define DA9030_VBATMON 0x32
  39. #define DA9030_VBATMONTXON 0x33
  40. #define DA9030_TBATHIGHP 0x34
  41. #define DA9030_TBATHIGHN 0x35
  42. #define DA9030_TBATLOW 0x36
  43. #define DA9030_VBAT_RES 0x41
  44. #define DA9030_VBATMIN_RES 0x42
  45. #define DA9030_VBATMINTXON_RES 0x43
  46. #define DA9030_ICHMAX_RES 0x44
  47. #define DA9030_ICHMIN_RES 0x45
  48. #define DA9030_ICHAVERAGE_RES 0x46
  49. #define DA9030_VCHMAX_RES 0x47
  50. #define DA9030_VCHMIN_RES 0x48
  51. #define DA9030_TBAT_RES 0x49
  52. struct da9030_adc_res {
  53. uint8_t vbat_res;
  54. uint8_t vbatmin_res;
  55. uint8_t vbatmintxon;
  56. uint8_t ichmax_res;
  57. uint8_t ichmin_res;
  58. uint8_t ichaverage_res;
  59. uint8_t vchmax_res;
  60. uint8_t vchmin_res;
  61. uint8_t tbat_res;
  62. uint8_t adc_in4_res;
  63. uint8_t adc_in5_res;
  64. };
  65. struct da9030_battery_thresholds {
  66. int tbat_low;
  67. int tbat_high;
  68. int tbat_restart;
  69. int vbat_low;
  70. int vbat_crit;
  71. int vbat_charge_start;
  72. int vbat_charge_stop;
  73. int vbat_charge_restart;
  74. int vcharge_min;
  75. int vcharge_max;
  76. };
  77. struct da9030_charger {
  78. struct power_supply psy;
  79. struct device *master;
  80. struct da9030_adc_res adc;
  81. struct delayed_work work;
  82. unsigned int interval;
  83. struct power_supply_info *battery_info;
  84. struct da9030_battery_thresholds thresholds;
  85. unsigned int charge_milliamp;
  86. unsigned int charge_millivolt;
  87. /* charger status */
  88. bool chdet;
  89. uint8_t fault;
  90. int mA;
  91. int mV;
  92. bool is_on;
  93. struct notifier_block nb;
  94. /* platform callbacks for battery low and critical events */
  95. void (*battery_low)(void);
  96. void (*battery_critical)(void);
  97. struct dentry *debug_file;
  98. };
  99. static inline int da9030_reg_to_mV(int reg)
  100. {
  101. return ((reg * 2650) >> 8) + 2650;
  102. }
  103. static inline int da9030_millivolt_to_reg(int mV)
  104. {
  105. return ((mV - 2650) << 8) / 2650;
  106. }
  107. static inline int da9030_reg_to_mA(int reg)
  108. {
  109. return ((reg * 24000) >> 8) / 15;
  110. }
  111. #ifdef CONFIG_DEBUG_FS
  112. static int bat_debug_show(struct seq_file *s, void *data)
  113. {
  114. struct da9030_charger *charger = s->private;
  115. seq_printf(s, "charger is %s\n", charger->is_on ? "on" : "off");
  116. if (charger->chdet) {
  117. seq_printf(s, "iset = %dmA, vset = %dmV\n",
  118. charger->mA, charger->mV);
  119. }
  120. seq_printf(s, "vbat_res = %d (%dmV)\n",
  121. charger->adc.vbat_res,
  122. da9030_reg_to_mV(charger->adc.vbat_res));
  123. seq_printf(s, "vbatmin_res = %d (%dmV)\n",
  124. charger->adc.vbatmin_res,
  125. da9030_reg_to_mV(charger->adc.vbatmin_res));
  126. seq_printf(s, "vbatmintxon = %d (%dmV)\n",
  127. charger->adc.vbatmintxon,
  128. da9030_reg_to_mV(charger->adc.vbatmintxon));
  129. seq_printf(s, "ichmax_res = %d (%dmA)\n",
  130. charger->adc.ichmax_res,
  131. da9030_reg_to_mV(charger->adc.ichmax_res));
  132. seq_printf(s, "ichmin_res = %d (%dmA)\n",
  133. charger->adc.ichmin_res,
  134. da9030_reg_to_mA(charger->adc.ichmin_res));
  135. seq_printf(s, "ichaverage_res = %d (%dmA)\n",
  136. charger->adc.ichaverage_res,
  137. da9030_reg_to_mA(charger->adc.ichaverage_res));
  138. seq_printf(s, "vchmax_res = %d (%dmV)\n",
  139. charger->adc.vchmax_res,
  140. da9030_reg_to_mA(charger->adc.vchmax_res));
  141. seq_printf(s, "vchmin_res = %d (%dmV)\n",
  142. charger->adc.vchmin_res,
  143. da9030_reg_to_mV(charger->adc.vchmin_res));
  144. return 0;
  145. }
  146. static int debug_open(struct inode *inode, struct file *file)
  147. {
  148. return single_open(file, bat_debug_show, inode->i_private);
  149. }
  150. static const struct file_operations bat_debug_fops = {
  151. .open = debug_open,
  152. .read = seq_read,
  153. .llseek = seq_lseek,
  154. .release = single_release,
  155. };
  156. static struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger)
  157. {
  158. charger->debug_file = debugfs_create_file("charger", 0666, 0, charger,
  159. &bat_debug_fops);
  160. return charger->debug_file;
  161. }
  162. static void da9030_bat_remove_debugfs(struct da9030_charger *charger)
  163. {
  164. debugfs_remove(charger->debug_file);
  165. }
  166. #else
  167. static inline struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger)
  168. {
  169. return NULL;
  170. }
  171. static inline void da9030_bat_remove_debugfs(struct da9030_charger *charger)
  172. {
  173. }
  174. #endif
  175. static inline void da9030_read_adc(struct da9030_charger *charger,
  176. struct da9030_adc_res *adc)
  177. {
  178. da903x_reads(charger->master, DA9030_VBAT_RES,
  179. sizeof(*adc), (uint8_t *)adc);
  180. }
  181. static void da9030_charger_update_state(struct da9030_charger *charger)
  182. {
  183. uint8_t val;
  184. da903x_read(charger->master, DA9030_CHARGE_CONTROL, &val);
  185. charger->is_on = (val & DA9030_CHRG_CHARGER_ENABLE) ? 1 : 0;
  186. charger->mA = ((val >> 3) & 0xf) * 100;
  187. charger->mV = (val & 0x7) * 50 + 4000;
  188. da9030_read_adc(charger, &charger->adc);
  189. da903x_read(charger->master, DA9030_FAULT_LOG, &charger->fault);
  190. charger->chdet = da903x_query_status(charger->master,
  191. DA9030_STATUS_CHDET);
  192. }
  193. static void da9030_set_charge(struct da9030_charger *charger, int on)
  194. {
  195. uint8_t val;
  196. if (on) {
  197. val = DA9030_CHRG_CHARGER_ENABLE;
  198. val |= (charger->charge_milliamp / 100) << 3;
  199. val |= (charger->charge_millivolt - 4000) / 50;
  200. charger->is_on = 1;
  201. } else {
  202. val = 0;
  203. charger->is_on = 0;
  204. }
  205. da903x_write(charger->master, DA9030_CHARGE_CONTROL, val);
  206. power_supply_changed(&charger->psy);
  207. }
  208. static void da9030_charger_check_state(struct da9030_charger *charger)
  209. {
  210. da9030_charger_update_state(charger);
  211. /* we wake or boot with external power on */
  212. if (!charger->is_on) {
  213. if ((charger->chdet) &&
  214. (charger->adc.vbat_res <
  215. charger->thresholds.vbat_charge_start)) {
  216. da9030_set_charge(charger, 1);
  217. }
  218. } else {
  219. /* Charger has been pulled out */
  220. if (!charger->chdet) {
  221. da9030_set_charge(charger, 0);
  222. return;
  223. }
  224. if (charger->adc.vbat_res >=
  225. charger->thresholds.vbat_charge_stop) {
  226. da9030_set_charge(charger, 0);
  227. da903x_write(charger->master, DA9030_VBATMON,
  228. charger->thresholds.vbat_charge_restart);
  229. } else if (charger->adc.vbat_res >
  230. charger->thresholds.vbat_low) {
  231. /* we are charging and passed LOW_THRESH,
  232. so upate DA9030 VBAT threshold
  233. */
  234. da903x_write(charger->master, DA9030_VBATMON,
  235. charger->thresholds.vbat_low);
  236. }
  237. if (charger->adc.vchmax_res > charger->thresholds.vcharge_max ||
  238. charger->adc.vchmin_res < charger->thresholds.vcharge_min ||
  239. /* Tempreture readings are negative */
  240. charger->adc.tbat_res < charger->thresholds.tbat_high ||
  241. charger->adc.tbat_res > charger->thresholds.tbat_low) {
  242. /* disable charger */
  243. da9030_set_charge(charger, 0);
  244. }
  245. }
  246. }
  247. static void da9030_charging_monitor(struct work_struct *work)
  248. {
  249. struct da9030_charger *charger;
  250. charger = container_of(work, struct da9030_charger, work.work);
  251. da9030_charger_check_state(charger);
  252. /* reschedule for the next time */
  253. schedule_delayed_work(&charger->work, charger->interval);
  254. }
  255. static enum power_supply_property da9030_battery_props[] = {
  256. POWER_SUPPLY_PROP_MODEL_NAME,
  257. POWER_SUPPLY_PROP_STATUS,
  258. POWER_SUPPLY_PROP_HEALTH,
  259. POWER_SUPPLY_PROP_TECHNOLOGY,
  260. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  261. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  262. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  263. POWER_SUPPLY_PROP_CURRENT_AVG,
  264. };
  265. static void da9030_battery_check_status(struct da9030_charger *charger,
  266. union power_supply_propval *val)
  267. {
  268. if (charger->chdet) {
  269. if (charger->is_on)
  270. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  271. else
  272. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  273. } else {
  274. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  275. }
  276. }
  277. static void da9030_battery_check_health(struct da9030_charger *charger,
  278. union power_supply_propval *val)
  279. {
  280. if (charger->fault & DA9030_FAULT_LOG_OVER_TEMP)
  281. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  282. else if (charger->fault & DA9030_FAULT_LOG_VBAT_OVER)
  283. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  284. else
  285. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  286. }
  287. static int da9030_battery_get_property(struct power_supply *psy,
  288. enum power_supply_property psp,
  289. union power_supply_propval *val)
  290. {
  291. struct da9030_charger *charger;
  292. charger = container_of(psy, struct da9030_charger, psy);
  293. switch (psp) {
  294. case POWER_SUPPLY_PROP_STATUS:
  295. da9030_battery_check_status(charger, val);
  296. break;
  297. case POWER_SUPPLY_PROP_HEALTH:
  298. da9030_battery_check_health(charger, val);
  299. break;
  300. case POWER_SUPPLY_PROP_TECHNOLOGY:
  301. val->intval = charger->battery_info->technology;
  302. break;
  303. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  304. val->intval = charger->battery_info->voltage_max_design;
  305. break;
  306. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  307. val->intval = charger->battery_info->voltage_min_design;
  308. break;
  309. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  310. val->intval = da9030_reg_to_mV(charger->adc.vbat_res) * 1000;
  311. break;
  312. case POWER_SUPPLY_PROP_CURRENT_AVG:
  313. val->intval =
  314. da9030_reg_to_mA(charger->adc.ichaverage_res) * 1000;
  315. break;
  316. case POWER_SUPPLY_PROP_MODEL_NAME:
  317. val->strval = charger->battery_info->name;
  318. break;
  319. default:
  320. break;
  321. }
  322. return 0;
  323. }
  324. static void da9030_battery_vbat_event(struct da9030_charger *charger)
  325. {
  326. da9030_read_adc(charger, &charger->adc);
  327. if (charger->is_on)
  328. return;
  329. if (charger->adc.vbat_res < charger->thresholds.vbat_low) {
  330. /* set VBAT threshold for critical */
  331. da903x_write(charger->master, DA9030_VBATMON,
  332. charger->thresholds.vbat_crit);
  333. if (charger->battery_low)
  334. charger->battery_low();
  335. } else if (charger->adc.vbat_res <
  336. charger->thresholds.vbat_crit) {
  337. /* notify the system of battery critical */
  338. if (charger->battery_critical)
  339. charger->battery_critical();
  340. }
  341. }
  342. static int da9030_battery_event(struct notifier_block *nb, unsigned long event,
  343. void *data)
  344. {
  345. struct da9030_charger *charger =
  346. container_of(nb, struct da9030_charger, nb);
  347. switch (event) {
  348. case DA9030_EVENT_CHDET:
  349. cancel_delayed_work_sync(&charger->work);
  350. schedule_work(&charger->work.work);
  351. break;
  352. case DA9030_EVENT_VBATMON:
  353. da9030_battery_vbat_event(charger);
  354. break;
  355. case DA9030_EVENT_CHIOVER:
  356. case DA9030_EVENT_TBAT:
  357. da9030_set_charge(charger, 0);
  358. break;
  359. }
  360. return 0;
  361. }
  362. static void da9030_battery_convert_thresholds(struct da9030_charger *charger,
  363. struct da9030_battery_info *pdata)
  364. {
  365. charger->thresholds.tbat_low = pdata->tbat_low;
  366. charger->thresholds.tbat_high = pdata->tbat_high;
  367. charger->thresholds.tbat_restart = pdata->tbat_restart;
  368. charger->thresholds.vbat_low =
  369. da9030_millivolt_to_reg(pdata->vbat_low);
  370. charger->thresholds.vbat_crit =
  371. da9030_millivolt_to_reg(pdata->vbat_crit);
  372. charger->thresholds.vbat_charge_start =
  373. da9030_millivolt_to_reg(pdata->vbat_charge_start);
  374. charger->thresholds.vbat_charge_stop =
  375. da9030_millivolt_to_reg(pdata->vbat_charge_stop);
  376. charger->thresholds.vbat_charge_restart =
  377. da9030_millivolt_to_reg(pdata->vbat_charge_restart);
  378. charger->thresholds.vcharge_min =
  379. da9030_millivolt_to_reg(pdata->vcharge_min);
  380. charger->thresholds.vcharge_max =
  381. da9030_millivolt_to_reg(pdata->vcharge_max);
  382. }
  383. static void da9030_battery_setup_psy(struct da9030_charger *charger)
  384. {
  385. struct power_supply *psy = &charger->psy;
  386. struct power_supply_info *info = charger->battery_info;
  387. psy->name = info->name;
  388. psy->use_for_apm = info->use_for_apm;
  389. psy->type = POWER_SUPPLY_TYPE_BATTERY;
  390. psy->get_property = da9030_battery_get_property;
  391. psy->properties = da9030_battery_props;
  392. psy->num_properties = ARRAY_SIZE(da9030_battery_props);
  393. };
  394. static int da9030_battery_charger_init(struct da9030_charger *charger)
  395. {
  396. char v[5];
  397. int ret;
  398. v[0] = v[1] = charger->thresholds.vbat_low;
  399. v[2] = charger->thresholds.tbat_high;
  400. v[3] = charger->thresholds.tbat_restart;
  401. v[4] = charger->thresholds.tbat_low;
  402. ret = da903x_writes(charger->master, DA9030_VBATMON, 5, v);
  403. if (ret)
  404. return ret;
  405. /*
  406. * Enable reference voltage supply for ADC from the LDO_INTERNAL
  407. * regulator. Must be set before ADC measurements can be made.
  408. */
  409. ret = da903x_write(charger->master, DA9030_ADC_MAN_CONTROL,
  410. DA9030_ADC_LDO_INT_ENABLE |
  411. DA9030_ADC_TBATREF_ENABLE);
  412. if (ret)
  413. return ret;
  414. /* enable auto ADC measuremnts */
  415. return da903x_write(charger->master, DA9030_ADC_AUTO_CONTROL,
  416. DA9030_ADC_TBAT_ENABLE | DA9030_ADC_VBAT_IN_TXON |
  417. DA9030_ADC_VCH_ENABLE | DA9030_ADC_ICH_ENABLE |
  418. DA9030_ADC_VBAT_ENABLE |
  419. DA9030_ADC_AUTO_SLEEP_ENABLE);
  420. }
  421. static int da9030_battery_probe(struct platform_device *pdev)
  422. {
  423. struct da9030_charger *charger;
  424. struct da9030_battery_info *pdata = pdev->dev.platform_data;
  425. int ret;
  426. if (pdata == NULL)
  427. return -EINVAL;
  428. if (pdata->charge_milliamp >= 1500 ||
  429. pdata->charge_millivolt < 4000 ||
  430. pdata->charge_millivolt > 4350)
  431. return -EINVAL;
  432. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  433. if (charger == NULL)
  434. return -ENOMEM;
  435. charger->master = pdev->dev.parent;
  436. /* 10 seconds between monitor runs unless platform defines other
  437. interval */
  438. charger->interval = msecs_to_jiffies(
  439. (pdata->batmon_interval ? : 10) * 1000);
  440. charger->charge_milliamp = pdata->charge_milliamp;
  441. charger->charge_millivolt = pdata->charge_millivolt;
  442. charger->battery_info = pdata->battery_info;
  443. charger->battery_low = pdata->battery_low;
  444. charger->battery_critical = pdata->battery_critical;
  445. da9030_battery_convert_thresholds(charger, pdata);
  446. ret = da9030_battery_charger_init(charger);
  447. if (ret)
  448. goto err_charger_init;
  449. INIT_DELAYED_WORK(&charger->work, da9030_charging_monitor);
  450. schedule_delayed_work(&charger->work, charger->interval);
  451. charger->nb.notifier_call = da9030_battery_event;
  452. ret = da903x_register_notifier(charger->master, &charger->nb,
  453. DA9030_EVENT_CHDET |
  454. DA9030_EVENT_VBATMON |
  455. DA9030_EVENT_CHIOVER |
  456. DA9030_EVENT_TBAT);
  457. if (ret)
  458. goto err_notifier;
  459. da9030_battery_setup_psy(charger);
  460. ret = power_supply_register(&pdev->dev, &charger->psy);
  461. if (ret)
  462. goto err_ps_register;
  463. charger->debug_file = da9030_bat_create_debugfs(charger);
  464. platform_set_drvdata(pdev, charger);
  465. return 0;
  466. err_ps_register:
  467. da903x_unregister_notifier(charger->master, &charger->nb,
  468. DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON |
  469. DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT);
  470. err_notifier:
  471. cancel_delayed_work(&charger->work);
  472. err_charger_init:
  473. kfree(charger);
  474. return ret;
  475. }
  476. static int da9030_battery_remove(struct platform_device *dev)
  477. {
  478. struct da9030_charger *charger = platform_get_drvdata(dev);
  479. da9030_bat_remove_debugfs(charger);
  480. da903x_unregister_notifier(charger->master, &charger->nb,
  481. DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON |
  482. DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT);
  483. cancel_delayed_work_sync(&charger->work);
  484. da9030_set_charge(charger, 0);
  485. power_supply_unregister(&charger->psy);
  486. kfree(charger);
  487. return 0;
  488. }
  489. static struct platform_driver da903x_battery_driver = {
  490. .driver = {
  491. .name = "da903x-battery",
  492. .owner = THIS_MODULE,
  493. },
  494. .probe = da9030_battery_probe,
  495. .remove = da9030_battery_remove,
  496. };
  497. module_platform_driver(da903x_battery_driver);
  498. MODULE_DESCRIPTION("DA9030 battery charger driver");
  499. MODULE_AUTHOR("Mike Rapoport, CompuLab");
  500. MODULE_LICENSE("GPL");