Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/power/da9030_battery.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us