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