blob: d1f93098a41222b0805ef777be981a80cef2469f [file] [log] [blame]
Arun Murthy13151632012-02-29 21:54:27 +05301/*
2 * Copyright (C) ST-Ericsson AB 2012
3 *
4 * Main and Back-up battery management driver.
5 *
6 * Note: Backup battery management is required in case of Li-Ion battery and not
7 * for capacitive battery. HREF boards have capacitive battery and hence backup
8 * battery management is not used and the supported code is available in this
9 * driver.
10 *
11 * License Terms: GNU General Public License v2
12 * Author:
13 * Johan Palsson <johan.palsson@stericsson.com>
14 * Karl Komierowski <karl.komierowski@stericsson.com>
15 * Arun R Murthy <arun.murthy@stericsson.com>
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/power_supply.h>
24#include <linux/kobject.h>
Arun Murthy13151632012-02-29 21:54:27 +053025#include <linux/slab.h>
Arun Murthy13151632012-02-29 21:54:27 +053026#include <linux/delay.h>
Arun Murthy13151632012-02-29 21:54:27 +053027#include <linux/time.h>
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080028#include <linux/of.h>
Arun Murthy13151632012-02-29 21:54:27 +053029#include <linux/completion.h>
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080030#include <linux/mfd/core.h>
31#include <linux/mfd/abx500.h>
32#include <linux/mfd/abx500/ab8500.h>
33#include <linux/mfd/abx500/ab8500-bm.h>
34#include <linux/mfd/abx500/ab8500-gpadc.h>
pender016eaf8742013-01-11 13:12:59 +000035#include <linux/kernel.h>
Arun Murthy13151632012-02-29 21:54:27 +053036
37#define MILLI_TO_MICRO 1000
38#define FG_LSB_IN_MA 1627
39#define QLSB_NANO_AMP_HOURS_X10 1129
40#define INS_CURR_TIMEOUT (3 * HZ)
41
42#define SEC_TO_SAMPLE(S) (S * 4)
43
44#define NBR_AVG_SAMPLES 20
45
Hakan Berg75f2a212012-05-10 08:43:25 +020046#define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
Arun Murthy13151632012-02-29 21:54:27 +053047
48#define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
49#define BATT_OK_MIN 2360 /* mV */
50#define BATT_OK_INCREMENT 50 /* mV */
51#define BATT_OK_MAX_NR_INCREMENTS 0xE
52
53/* FG constants */
54#define BATT_OVV 0x01
55
56#define interpolate(x, x1, y1, x2, y2) \
57 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
58
59#define to_ab8500_fg_device_info(x) container_of((x), \
60 struct ab8500_fg, fg_psy);
61
62/**
63 * struct ab8500_fg_interrupts - ab8500 fg interupts
64 * @name: name of the interrupt
65 * @isr function pointer to the isr
66 */
67struct ab8500_fg_interrupts {
68 char *name;
69 irqreturn_t (*isr)(int irq, void *data);
70};
71
72enum ab8500_fg_discharge_state {
73 AB8500_FG_DISCHARGE_INIT,
74 AB8500_FG_DISCHARGE_INITMEASURING,
75 AB8500_FG_DISCHARGE_INIT_RECOVERY,
76 AB8500_FG_DISCHARGE_RECOVERY,
77 AB8500_FG_DISCHARGE_READOUT_INIT,
78 AB8500_FG_DISCHARGE_READOUT,
79 AB8500_FG_DISCHARGE_WAKEUP,
80};
81
82static char *discharge_state[] = {
83 "DISCHARGE_INIT",
84 "DISCHARGE_INITMEASURING",
85 "DISCHARGE_INIT_RECOVERY",
86 "DISCHARGE_RECOVERY",
87 "DISCHARGE_READOUT_INIT",
88 "DISCHARGE_READOUT",
89 "DISCHARGE_WAKEUP",
90};
91
92enum ab8500_fg_charge_state {
93 AB8500_FG_CHARGE_INIT,
94 AB8500_FG_CHARGE_READOUT,
95};
96
97static char *charge_state[] = {
98 "CHARGE_INIT",
99 "CHARGE_READOUT",
100};
101
102enum ab8500_fg_calibration_state {
103 AB8500_FG_CALIB_INIT,
104 AB8500_FG_CALIB_WAIT,
105 AB8500_FG_CALIB_END,
106};
107
108struct ab8500_fg_avg_cap {
109 int avg;
110 int samples[NBR_AVG_SAMPLES];
111 __kernel_time_t time_stamps[NBR_AVG_SAMPLES];
112 int pos;
113 int nbr_samples;
114 int sum;
115};
116
Marcus Cooperea402402013-01-11 13:12:54 +0000117struct ab8500_fg_cap_scaling {
118 bool enable;
119 int cap_to_scale[2];
120 int disable_cap_level;
121 int scaled_cap;
122};
123
Arun Murthy13151632012-02-29 21:54:27 +0530124struct ab8500_fg_battery_capacity {
125 int max_mah_design;
126 int max_mah;
127 int mah;
128 int permille;
129 int level;
130 int prev_mah;
131 int prev_percent;
132 int prev_level;
133 int user_mah;
Marcus Cooperea402402013-01-11 13:12:54 +0000134 struct ab8500_fg_cap_scaling cap_scale;
Arun Murthy13151632012-02-29 21:54:27 +0530135};
136
137struct ab8500_fg_flags {
138 bool fg_enabled;
139 bool conv_done;
140 bool charging;
141 bool fully_charged;
142 bool force_full;
143 bool low_bat_delay;
144 bool low_bat;
145 bool bat_ovv;
146 bool batt_unknown;
147 bool calibrate;
148 bool user_cap;
149 bool batt_id_received;
150};
151
152struct inst_curr_result_list {
153 struct list_head list;
154 int *result;
155};
156
157/**
158 * struct ab8500_fg - ab8500 FG device information
159 * @dev: Pointer to the structure device
160 * @node: a list of AB8500 FGs, hence prepared for reentrance
161 * @irq holds the CCEOC interrupt number
162 * @vbat: Battery voltage in mV
163 * @vbat_nom: Nominal battery voltage in mV
164 * @inst_curr: Instantenous battery current in mA
165 * @avg_curr: Average battery current in mA
166 * @bat_temp battery temperature
167 * @fg_samples: Number of samples used in the FG accumulation
168 * @accu_charge: Accumulated charge from the last conversion
169 * @recovery_cnt: Counter for recovery mode
170 * @high_curr_cnt: Counter for high current mode
171 * @init_cnt: Counter for init mode
Hakan Berg75f2a212012-05-10 08:43:25 +0200172 * @low_bat_cnt Counter for number of consecutive low battery measures
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000173 * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
Arun Murthy13151632012-02-29 21:54:27 +0530174 * @recovery_needed: Indicate if recovery is needed
175 * @high_curr_mode: Indicate if we're in high current mode
176 * @init_capacity: Indicate if initial capacity measuring should be done
177 * @turn_off_fg: True if fg was off before current measurement
178 * @calib_state State during offset calibration
179 * @discharge_state: Current discharge state
180 * @charge_state: Current charge state
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000181 * @ab8500_fg_started Completion struct used for the instant current start
Arun Murthy13151632012-02-29 21:54:27 +0530182 * @ab8500_fg_complete Completion struct used for the instant current reading
183 * @flags: Structure for information about events triggered
184 * @bat_cap: Structure for battery capacity specific parameters
185 * @avg_cap: Average capacity filter
186 * @parent: Pointer to the struct ab8500
187 * @gpadc: Pointer to the struct gpadc
Lee Jonesb0284de2012-11-30 10:09:42 +0000188 * @bm: Platform specific battery management information
Arun Murthy13151632012-02-29 21:54:27 +0530189 * @fg_psy: Structure that holds the FG specific battery properties
190 * @fg_wq: Work queue for running the FG algorithm
191 * @fg_periodic_work: Work to run the FG algorithm periodically
192 * @fg_low_bat_work: Work to check low bat condition
193 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
194 * @fg_work: Work to run the FG algorithm instantly
195 * @fg_acc_cur_work: Work to read the FG accumulator
196 * @fg_check_hw_failure_work: Work for checking HW state
197 * @cc_lock: Mutex for locking the CC
198 * @fg_kobject: Structure of type kobject
199 */
200struct ab8500_fg {
201 struct device *dev;
202 struct list_head node;
203 int irq;
204 int vbat;
205 int vbat_nom;
206 int inst_curr;
207 int avg_curr;
208 int bat_temp;
209 int fg_samples;
210 int accu_charge;
211 int recovery_cnt;
212 int high_curr_cnt;
213 int init_cnt;
Hakan Berg75f2a212012-05-10 08:43:25 +0200214 int low_bat_cnt;
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000215 int nbr_cceoc_irq_cnt;
Arun Murthy13151632012-02-29 21:54:27 +0530216 bool recovery_needed;
217 bool high_curr_mode;
218 bool init_capacity;
219 bool turn_off_fg;
220 enum ab8500_fg_calibration_state calib_state;
221 enum ab8500_fg_discharge_state discharge_state;
222 enum ab8500_fg_charge_state charge_state;
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000223 struct completion ab8500_fg_started;
Arun Murthy13151632012-02-29 21:54:27 +0530224 struct completion ab8500_fg_complete;
225 struct ab8500_fg_flags flags;
226 struct ab8500_fg_battery_capacity bat_cap;
227 struct ab8500_fg_avg_cap avg_cap;
228 struct ab8500 *parent;
229 struct ab8500_gpadc *gpadc;
Lee Jonesb0284de2012-11-30 10:09:42 +0000230 struct abx500_bm_data *bm;
Arun Murthy13151632012-02-29 21:54:27 +0530231 struct power_supply fg_psy;
232 struct workqueue_struct *fg_wq;
233 struct delayed_work fg_periodic_work;
234 struct delayed_work fg_low_bat_work;
235 struct delayed_work fg_reinit_work;
236 struct work_struct fg_work;
237 struct work_struct fg_acc_cur_work;
238 struct delayed_work fg_check_hw_failure_work;
239 struct mutex cc_lock;
240 struct kobject fg_kobject;
241};
242static LIST_HEAD(ab8500_fg_list);
243
244/**
245 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
246 * (i.e. the first fuel gauge in the instance list)
247 */
248struct ab8500_fg *ab8500_fg_get(void)
249{
250 struct ab8500_fg *fg;
251
252 if (list_empty(&ab8500_fg_list))
253 return NULL;
254
255 fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
256 return fg;
257}
258
259/* Main battery properties */
260static enum power_supply_property ab8500_fg_props[] = {
261 POWER_SUPPLY_PROP_VOLTAGE_NOW,
262 POWER_SUPPLY_PROP_CURRENT_NOW,
263 POWER_SUPPLY_PROP_CURRENT_AVG,
264 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
265 POWER_SUPPLY_PROP_ENERGY_FULL,
266 POWER_SUPPLY_PROP_ENERGY_NOW,
267 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
268 POWER_SUPPLY_PROP_CHARGE_FULL,
269 POWER_SUPPLY_PROP_CHARGE_NOW,
270 POWER_SUPPLY_PROP_CAPACITY,
271 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
272};
273
274/*
275 * This array maps the raw hex value to lowbat voltage used by the AB8500
276 * Values taken from the UM0836
277 */
278static int ab8500_fg_lowbat_voltage_map[] = {
279 2300 ,
280 2325 ,
281 2350 ,
282 2375 ,
283 2400 ,
284 2425 ,
285 2450 ,
286 2475 ,
287 2500 ,
288 2525 ,
289 2550 ,
290 2575 ,
291 2600 ,
292 2625 ,
293 2650 ,
294 2675 ,
295 2700 ,
296 2725 ,
297 2750 ,
298 2775 ,
299 2800 ,
300 2825 ,
301 2850 ,
302 2875 ,
303 2900 ,
304 2925 ,
305 2950 ,
306 2975 ,
307 3000 ,
308 3025 ,
309 3050 ,
310 3075 ,
311 3100 ,
312 3125 ,
313 3150 ,
314 3175 ,
315 3200 ,
316 3225 ,
317 3250 ,
318 3275 ,
319 3300 ,
320 3325 ,
321 3350 ,
322 3375 ,
323 3400 ,
324 3425 ,
325 3450 ,
326 3475 ,
327 3500 ,
328 3525 ,
329 3550 ,
330 3575 ,
331 3600 ,
332 3625 ,
333 3650 ,
334 3675 ,
335 3700 ,
336 3725 ,
337 3750 ,
338 3775 ,
339 3800 ,
340 3825 ,
341 3850 ,
342 3850 ,
343};
344
345static u8 ab8500_volt_to_regval(int voltage)
346{
347 int i;
348
349 if (voltage < ab8500_fg_lowbat_voltage_map[0])
350 return 0;
351
352 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
353 if (voltage < ab8500_fg_lowbat_voltage_map[i])
354 return (u8) i - 1;
355 }
356
357 /* If not captured above, return index of last element */
358 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
359}
360
361/**
362 * ab8500_fg_is_low_curr() - Low or high current mode
363 * @di: pointer to the ab8500_fg structure
364 * @curr: the current to base or our decision on
365 *
366 * Low current mode if the current consumption is below a certain threshold
367 */
368static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
369{
370 /*
371 * We want to know if we're in low current mode
372 */
Lee Jonesb0284de2012-11-30 10:09:42 +0000373 if (curr > -di->bm->fg_params->high_curr_threshold)
Arun Murthy13151632012-02-29 21:54:27 +0530374 return true;
375 else
376 return false;
377}
378
379/**
380 * ab8500_fg_add_cap_sample() - Add capacity to average filter
381 * @di: pointer to the ab8500_fg structure
382 * @sample: the capacity in mAh to add to the filter
383 *
384 * A capacity is added to the filter and a new mean capacity is calculated and
385 * returned
386 */
387static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
388{
389 struct timespec ts;
390 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
391
392 getnstimeofday(&ts);
393
394 do {
395 avg->sum += sample - avg->samples[avg->pos];
396 avg->samples[avg->pos] = sample;
397 avg->time_stamps[avg->pos] = ts.tv_sec;
398 avg->pos++;
399
400 if (avg->pos == NBR_AVG_SAMPLES)
401 avg->pos = 0;
402
403 if (avg->nbr_samples < NBR_AVG_SAMPLES)
404 avg->nbr_samples++;
405
406 /*
407 * Check the time stamp for each sample. If too old,
408 * replace with latest sample
409 */
410 } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
411
412 avg->avg = avg->sum / avg->nbr_samples;
413
414 return avg->avg;
415}
416
417/**
418 * ab8500_fg_clear_cap_samples() - Clear average filter
419 * @di: pointer to the ab8500_fg structure
420 *
421 * The capacity filter is is reset to zero.
422 */
423static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
424{
425 int i;
426 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
427
428 avg->pos = 0;
429 avg->nbr_samples = 0;
430 avg->sum = 0;
431 avg->avg = 0;
432
433 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
434 avg->samples[i] = 0;
435 avg->time_stamps[i] = 0;
436 }
437}
438
439/**
440 * ab8500_fg_fill_cap_sample() - Fill average filter
441 * @di: pointer to the ab8500_fg structure
442 * @sample: the capacity in mAh to fill the filter with
443 *
444 * The capacity filter is filled with a capacity in mAh
445 */
446static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
447{
448 int i;
449 struct timespec ts;
450 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
451
452 getnstimeofday(&ts);
453
454 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
455 avg->samples[i] = sample;
456 avg->time_stamps[i] = ts.tv_sec;
457 }
458
459 avg->pos = 0;
460 avg->nbr_samples = NBR_AVG_SAMPLES;
461 avg->sum = sample * NBR_AVG_SAMPLES;
462 avg->avg = sample;
463}
464
465/**
466 * ab8500_fg_coulomb_counter() - enable coulomb counter
467 * @di: pointer to the ab8500_fg structure
468 * @enable: enable/disable
469 *
470 * Enable/Disable coulomb counter.
471 * On failure returns negative value.
472 */
473static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
474{
475 int ret = 0;
476 mutex_lock(&di->cc_lock);
477 if (enable) {
478 /* To be able to reprogram the number of samples, we have to
479 * first stop the CC and then enable it again */
480 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
481 AB8500_RTC_CC_CONF_REG, 0x00);
482 if (ret)
483 goto cc_err;
484
485 /* Program the samples */
486 ret = abx500_set_register_interruptible(di->dev,
487 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
488 di->fg_samples);
489 if (ret)
490 goto cc_err;
491
492 /* Start the CC */
493 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
494 AB8500_RTC_CC_CONF_REG,
495 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
496 if (ret)
497 goto cc_err;
498
499 di->flags.fg_enabled = true;
500 } else {
501 /* Clear any pending read requests */
Kalle Komierowskie32ad072012-02-02 16:05:46 +0100502 ret = abx500_mask_and_set_register_interruptible(di->dev,
503 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
504 (RESET_ACCU | READ_REQ), 0);
Arun Murthy13151632012-02-29 21:54:27 +0530505 if (ret)
506 goto cc_err;
507
508 ret = abx500_set_register_interruptible(di->dev,
509 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
510 if (ret)
511 goto cc_err;
512
513 /* Stop the CC */
514 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
515 AB8500_RTC_CC_CONF_REG, 0);
516 if (ret)
517 goto cc_err;
518
519 di->flags.fg_enabled = false;
520
521 }
522 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
523 enable, di->fg_samples);
524
525 mutex_unlock(&di->cc_lock);
526
527 return ret;
528cc_err:
529 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
530 mutex_unlock(&di->cc_lock);
531 return ret;
532}
533
534/**
535 * ab8500_fg_inst_curr_start() - start battery instantaneous current
536 * @di: pointer to the ab8500_fg structure
537 *
538 * Returns 0 or error code
539 * Note: This is part "one" and has to be called before
540 * ab8500_fg_inst_curr_finalize()
541 */
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000542int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
Arun Murthy13151632012-02-29 21:54:27 +0530543{
544 u8 reg_val;
545 int ret;
546
547 mutex_lock(&di->cc_lock);
548
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000549 di->nbr_cceoc_irq_cnt = 0;
Arun Murthy13151632012-02-29 21:54:27 +0530550 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
551 AB8500_RTC_CC_CONF_REG, &reg_val);
552 if (ret < 0)
553 goto fail;
554
555 if (!(reg_val & CC_PWR_UP_ENA)) {
556 dev_dbg(di->dev, "%s Enable FG\n", __func__);
557 di->turn_off_fg = true;
558
559 /* Program the samples */
560 ret = abx500_set_register_interruptible(di->dev,
561 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
562 SEC_TO_SAMPLE(10));
563 if (ret)
564 goto fail;
565
566 /* Start the CC */
567 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
568 AB8500_RTC_CC_CONF_REG,
569 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
570 if (ret)
571 goto fail;
572 } else {
573 di->turn_off_fg = false;
574 }
575
576 /* Return and WFI */
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000577 INIT_COMPLETION(di->ab8500_fg_started);
Arun Murthy13151632012-02-29 21:54:27 +0530578 INIT_COMPLETION(di->ab8500_fg_complete);
579 enable_irq(di->irq);
580
581 /* Note: cc_lock is still locked */
582 return 0;
583fail:
584 mutex_unlock(&di->cc_lock);
585 return ret;
586}
587
588/**
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000589 * ab8500_fg_inst_curr_started() - check if fg conversion has started
590 * @di: pointer to the ab8500_fg structure
591 *
592 * Returns 1 if conversion started, 0 if still waiting
593 */
594int ab8500_fg_inst_curr_started(struct ab8500_fg *di)
595{
596 return completion_done(&di->ab8500_fg_started);
597}
598
599/**
Arun Murthy13151632012-02-29 21:54:27 +0530600 * ab8500_fg_inst_curr_done() - check if fg conversion is done
601 * @di: pointer to the ab8500_fg structure
602 *
603 * Returns 1 if conversion done, 0 if still waiting
604 */
605int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
606{
607 return completion_done(&di->ab8500_fg_complete);
608}
609
610/**
611 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
612 * @di: pointer to the ab8500_fg structure
613 * @res: battery instantenous current(on success)
614 *
615 * Returns 0 or an error code
616 * Note: This is part "two" and has to be called at earliest 250 ms
617 * after ab8500_fg_inst_curr_start()
618 */
619int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
620{
621 u8 low, high;
622 int val;
623 int ret;
624 int timeout;
625
626 if (!completion_done(&di->ab8500_fg_complete)) {
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000627 timeout = wait_for_completion_timeout(
628 &di->ab8500_fg_complete,
Arun Murthy13151632012-02-29 21:54:27 +0530629 INS_CURR_TIMEOUT);
630 dev_dbg(di->dev, "Finalize time: %d ms\n",
631 ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
632 if (!timeout) {
633 ret = -ETIME;
634 disable_irq(di->irq);
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000635 di->nbr_cceoc_irq_cnt = 0;
Arun Murthy13151632012-02-29 21:54:27 +0530636 dev_err(di->dev, "completion timed out [%d]\n",
637 __LINE__);
638 goto fail;
639 }
640 }
641
642 disable_irq(di->irq);
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000643 di->nbr_cceoc_irq_cnt = 0;
Arun Murthy13151632012-02-29 21:54:27 +0530644
645 ret = abx500_mask_and_set_register_interruptible(di->dev,
646 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
647 READ_REQ, READ_REQ);
648
649 /* 100uS between read request and read is needed */
650 usleep_range(100, 100);
651
652 /* Read CC Sample conversion value Low and high */
653 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
654 AB8500_GASG_CC_SMPL_CNVL_REG, &low);
655 if (ret < 0)
656 goto fail;
657
658 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
659 AB8500_GASG_CC_SMPL_CNVH_REG, &high);
660 if (ret < 0)
661 goto fail;
662
663 /*
664 * negative value for Discharging
665 * convert 2's compliment into decimal
666 */
667 if (high & 0x10)
668 val = (low | (high << 8) | 0xFFFFE000);
669 else
670 val = (low | (high << 8));
671
672 /*
673 * Convert to unit value in mA
674 * Full scale input voltage is
675 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
676 * Given a 250ms conversion cycle time the LSB corresponds
677 * to 112.9 nAh. Convert to current by dividing by the conversion
678 * time in hours (250ms = 1 / (3600 * 4)h)
679 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
680 */
681 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
Lee Jonesb0284de2012-11-30 10:09:42 +0000682 (1000 * di->bm->fg_res);
Arun Murthy13151632012-02-29 21:54:27 +0530683
684 if (di->turn_off_fg) {
685 dev_dbg(di->dev, "%s Disable FG\n", __func__);
686
687 /* Clear any pending read requests */
688 ret = abx500_set_register_interruptible(di->dev,
689 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
690 if (ret)
691 goto fail;
692
693 /* Stop the CC */
694 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
695 AB8500_RTC_CC_CONF_REG, 0);
696 if (ret)
697 goto fail;
698 }
699 mutex_unlock(&di->cc_lock);
700 (*res) = val;
701
702 return 0;
703fail:
704 mutex_unlock(&di->cc_lock);
705 return ret;
706}
707
708/**
709 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
710 * @di: pointer to the ab8500_fg structure
711 * @res: battery instantenous current(on success)
712 *
713 * Returns 0 else error code
714 */
715int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
716{
717 int ret;
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000718 int timeout;
Arun Murthy13151632012-02-29 21:54:27 +0530719 int res = 0;
720
721 ret = ab8500_fg_inst_curr_start(di);
722 if (ret) {
723 dev_err(di->dev, "Failed to initialize fg_inst\n");
724 return 0;
725 }
726
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000727 /* Wait for CC to actually start */
728 if (!completion_done(&di->ab8500_fg_started)) {
729 timeout = wait_for_completion_timeout(
730 &di->ab8500_fg_started,
731 INS_CURR_TIMEOUT);
732 dev_dbg(di->dev, "Start time: %d ms\n",
733 ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
734 if (!timeout) {
735 ret = -ETIME;
736 dev_err(di->dev, "completion timed out [%d]\n",
737 __LINE__);
738 goto fail;
739 }
740 }
741
Arun Murthy13151632012-02-29 21:54:27 +0530742 ret = ab8500_fg_inst_curr_finalize(di, &res);
743 if (ret) {
744 dev_err(di->dev, "Failed to finalize fg_inst\n");
745 return 0;
746 }
747
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000748 dev_dbg(di->dev, "%s instant current: %d", __func__, res);
Arun Murthy13151632012-02-29 21:54:27 +0530749 return res;
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000750fail:
Rickard Andersson129d5832013-01-11 13:12:55 +0000751 disable_irq(di->irq);
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000752 mutex_unlock(&di->cc_lock);
753 return ret;
Arun Murthy13151632012-02-29 21:54:27 +0530754}
755
756/**
757 * ab8500_fg_acc_cur_work() - average battery current
758 * @work: pointer to the work_struct structure
759 *
760 * Updated the average battery current obtained from the
761 * coulomb counter.
762 */
763static void ab8500_fg_acc_cur_work(struct work_struct *work)
764{
765 int val;
766 int ret;
767 u8 low, med, high;
768
769 struct ab8500_fg *di = container_of(work,
770 struct ab8500_fg, fg_acc_cur_work);
771
772 mutex_lock(&di->cc_lock);
773 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
774 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
775 if (ret)
776 goto exit;
777
778 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
779 AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
780 if (ret < 0)
781 goto exit;
782
783 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
784 AB8500_GASG_CC_NCOV_ACCU_MED, &med);
785 if (ret < 0)
786 goto exit;
787
788 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
789 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
790 if (ret < 0)
791 goto exit;
792
793 /* Check for sign bit in case of negative value, 2's compliment */
794 if (high & 0x10)
795 val = (low | (med << 8) | (high << 16) | 0xFFE00000);
796 else
797 val = (low | (med << 8) | (high << 16));
798
799 /*
800 * Convert to uAh
801 * Given a 250ms conversion cycle time the LSB corresponds
802 * to 112.9 nAh.
803 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
804 */
805 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
Lee Jonesb0284de2012-11-30 10:09:42 +0000806 (100 * di->bm->fg_res);
Arun Murthy13151632012-02-29 21:54:27 +0530807
808 /*
809 * Convert to unit value in mA
Paer-Olof Haakanssonf902dad2013-01-11 13:13:05 +0000810 * by dividing by the conversion
Arun Murthy13151632012-02-29 21:54:27 +0530811 * time in hours (= samples / (3600 * 4)h)
Paer-Olof Haakanssonf902dad2013-01-11 13:13:05 +0000812 * and multiply with 1000
Arun Murthy13151632012-02-29 21:54:27 +0530813 */
814 di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
Lee Jonesb0284de2012-11-30 10:09:42 +0000815 (1000 * di->bm->fg_res * (di->fg_samples / 4));
Arun Murthy13151632012-02-29 21:54:27 +0530816
817 di->flags.conv_done = true;
818
819 mutex_unlock(&di->cc_lock);
820
821 queue_work(di->fg_wq, &di->fg_work);
822
Paer-Olof Haakanssonf902dad2013-01-11 13:13:05 +0000823 dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
824 di->bm->fg_res, di->fg_samples, val, di->accu_charge);
Arun Murthy13151632012-02-29 21:54:27 +0530825 return;
826exit:
827 dev_err(di->dev,
828 "Failed to read or write gas gauge registers\n");
829 mutex_unlock(&di->cc_lock);
830 queue_work(di->fg_wq, &di->fg_work);
831}
832
833/**
834 * ab8500_fg_bat_voltage() - get battery voltage
835 * @di: pointer to the ab8500_fg structure
836 *
837 * Returns battery voltage(on success) else error code
838 */
839static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
840{
841 int vbat;
842 static int prev;
843
844 vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
845 if (vbat < 0) {
846 dev_err(di->dev,
847 "%s gpadc conversion failed, using previous value\n",
848 __func__);
849 return prev;
850 }
851
852 prev = vbat;
853 return vbat;
854}
855
856/**
857 * ab8500_fg_volt_to_capacity() - Voltage based capacity
858 * @di: pointer to the ab8500_fg structure
859 * @voltage: The voltage to convert to a capacity
860 *
861 * Returns battery capacity in per mille based on voltage
862 */
863static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
864{
865 int i, tbl_size;
Anton Vorontsov450ceb22012-03-14 04:38:32 +0400866 struct abx500_v_to_cap *tbl;
Arun Murthy13151632012-02-29 21:54:27 +0530867 int cap = 0;
868
Lee Jonesb0284de2012-11-30 10:09:42 +0000869 tbl = di->bm->bat_type[di->bm->batt_id].v_to_cap_tbl,
870 tbl_size = di->bm->bat_type[di->bm->batt_id].n_v_cap_tbl_elements;
Arun Murthy13151632012-02-29 21:54:27 +0530871
872 for (i = 0; i < tbl_size; ++i) {
873 if (voltage > tbl[i].voltage)
874 break;
875 }
876
877 if ((i > 0) && (i < tbl_size)) {
878 cap = interpolate(voltage,
879 tbl[i].voltage,
880 tbl[i].capacity * 10,
881 tbl[i-1].voltage,
882 tbl[i-1].capacity * 10);
883 } else if (i == 0) {
884 cap = 1000;
885 } else {
886 cap = 0;
887 }
888
889 dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
890 __func__, voltage, cap);
891
892 return cap;
893}
894
895/**
896 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
897 * @di: pointer to the ab8500_fg structure
898 *
899 * Returns battery capacity based on battery voltage that is not compensated
900 * for the voltage drop due to the load
901 */
902static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
903{
904 di->vbat = ab8500_fg_bat_voltage(di);
905 return ab8500_fg_volt_to_capacity(di, di->vbat);
906}
907
908/**
909 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
910 * @di: pointer to the ab8500_fg structure
911 *
912 * Returns battery inner resistance added with the fuel gauge resistor value
913 * to get the total resistance in the whole link from gnd to bat+ node.
914 */
915static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
916{
917 int i, tbl_size;
918 struct batres_vs_temp *tbl;
919 int resist = 0;
920
Lee Jonesb0284de2012-11-30 10:09:42 +0000921 tbl = di->bm->bat_type[di->bm->batt_id].batres_tbl;
922 tbl_size = di->bm->bat_type[di->bm->batt_id].n_batres_tbl_elements;
Arun Murthy13151632012-02-29 21:54:27 +0530923
924 for (i = 0; i < tbl_size; ++i) {
925 if (di->bat_temp / 10 > tbl[i].temp)
926 break;
927 }
928
929 if ((i > 0) && (i < tbl_size)) {
930 resist = interpolate(di->bat_temp / 10,
931 tbl[i].temp,
932 tbl[i].resist,
933 tbl[i-1].temp,
934 tbl[i-1].resist);
935 } else if (i == 0) {
936 resist = tbl[0].resist;
937 } else {
938 resist = tbl[tbl_size - 1].resist;
939 }
940
941 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
942 " fg resistance %d, total: %d (mOhm)\n",
Lee Jonesb0284de2012-11-30 10:09:42 +0000943 __func__, di->bat_temp, resist, di->bm->fg_res / 10,
944 (di->bm->fg_res / 10) + resist);
Arun Murthy13151632012-02-29 21:54:27 +0530945
946 /* fg_res variable is in 0.1mOhm */
Lee Jonesb0284de2012-11-30 10:09:42 +0000947 resist += di->bm->fg_res / 10;
Arun Murthy13151632012-02-29 21:54:27 +0530948
949 return resist;
950}
951
952/**
953 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
954 * @di: pointer to the ab8500_fg structure
955 *
956 * Returns battery capacity based on battery voltage that is load compensated
957 * for the voltage drop
958 */
959static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
960{
961 int vbat_comp, res;
962 int i = 0;
963 int vbat = 0;
964
965 ab8500_fg_inst_curr_start(di);
966
967 do {
968 vbat += ab8500_fg_bat_voltage(di);
969 i++;
Jonas Aaberg9a0bd072013-01-15 14:09:13 +0000970 usleep_range(5000, 6000);
Arun Murthy13151632012-02-29 21:54:27 +0530971 } while (!ab8500_fg_inst_curr_done(di));
972
973 ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
974
975 di->vbat = vbat / i;
976 res = ab8500_fg_battery_resistance(di);
977
978 /* Use Ohms law to get the load compensated voltage */
979 vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
980
981 dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
982 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
983 __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
984
985 return ab8500_fg_volt_to_capacity(di, vbat_comp);
986}
987
988/**
989 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
990 * @di: pointer to the ab8500_fg structure
991 * @cap_mah: capacity in mAh
992 *
993 * Converts capacity in mAh to capacity in permille
994 */
995static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
996{
997 return (cap_mah * 1000) / di->bat_cap.max_mah_design;
998}
999
1000/**
1001 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
1002 * @di: pointer to the ab8500_fg structure
1003 * @cap_pm: capacity in permille
1004 *
1005 * Converts capacity in permille to capacity in mAh
1006 */
1007static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
1008{
1009 return cap_pm * di->bat_cap.max_mah_design / 1000;
1010}
1011
1012/**
1013 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1014 * @di: pointer to the ab8500_fg structure
1015 * @cap_mah: capacity in mAh
1016 *
1017 * Converts capacity in mAh to capacity in uWh
1018 */
1019static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
1020{
1021 u64 div_res;
1022 u32 div_rem;
1023
1024 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
1025 div_rem = do_div(div_res, 1000);
1026
1027 /* Make sure to round upwards if necessary */
1028 if (div_rem >= 1000 / 2)
1029 div_res++;
1030
1031 return (int) div_res;
1032}
1033
1034/**
1035 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1036 * @di: pointer to the ab8500_fg structure
1037 *
1038 * Return the capacity in mAh based on previous calculated capcity and the FG
1039 * accumulator register value. The filter is filled with this capacity
1040 */
1041static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
1042{
1043 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1044 __func__,
1045 di->bat_cap.mah,
1046 di->accu_charge);
1047
1048 /* Capacity should not be less than 0 */
1049 if (di->bat_cap.mah + di->accu_charge > 0)
1050 di->bat_cap.mah += di->accu_charge;
1051 else
1052 di->bat_cap.mah = 0;
1053 /*
1054 * We force capacity to 100% once when the algorithm
1055 * reports that it's full.
1056 */
1057 if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1058 di->flags.force_full) {
1059 di->bat_cap.mah = di->bat_cap.max_mah_design;
1060 }
1061
1062 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1063 di->bat_cap.permille =
1064 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1065
1066 /* We need to update battery voltage and inst current when charging */
1067 di->vbat = ab8500_fg_bat_voltage(di);
1068 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1069
1070 return di->bat_cap.mah;
1071}
1072
1073/**
1074 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1075 * @di: pointer to the ab8500_fg structure
1076 * @comp: if voltage should be load compensated before capacity calc
1077 *
1078 * Return the capacity in mAh based on the battery voltage. The voltage can
1079 * either be load compensated or not. This value is added to the filter and a
1080 * new mean value is calculated and returned.
1081 */
1082static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1083{
1084 int permille, mah;
1085
1086 if (comp)
1087 permille = ab8500_fg_load_comp_volt_to_capacity(di);
1088 else
1089 permille = ab8500_fg_uncomp_volt_to_capacity(di);
1090
1091 mah = ab8500_fg_convert_permille_to_mah(di, permille);
1092
1093 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1094 di->bat_cap.permille =
1095 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1096
1097 return di->bat_cap.mah;
1098}
1099
1100/**
1101 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1102 * @di: pointer to the ab8500_fg structure
1103 *
1104 * Return the capacity in mAh based on previous calculated capcity and the FG
1105 * accumulator register value. This value is added to the filter and a
1106 * new mean value is calculated and returned.
1107 */
1108static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1109{
1110 int permille_volt, permille;
1111
1112 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1113 __func__,
1114 di->bat_cap.mah,
1115 di->accu_charge);
1116
1117 /* Capacity should not be less than 0 */
1118 if (di->bat_cap.mah + di->accu_charge > 0)
1119 di->bat_cap.mah += di->accu_charge;
1120 else
1121 di->bat_cap.mah = 0;
1122
1123 if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1124 di->bat_cap.mah = di->bat_cap.max_mah_design;
1125
1126 /*
1127 * Check against voltage based capacity. It can not be lower
1128 * than what the uncompensated voltage says
1129 */
1130 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1131 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1132
1133 if (permille < permille_volt) {
1134 di->bat_cap.permille = permille_volt;
1135 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1136 di->bat_cap.permille);
1137
1138 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1139 __func__,
1140 permille,
1141 permille_volt);
1142
1143 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1144 } else {
1145 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1146 di->bat_cap.permille =
1147 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1148 }
1149
1150 return di->bat_cap.mah;
1151}
1152
1153/**
1154 * ab8500_fg_capacity_level() - Get the battery capacity level
1155 * @di: pointer to the ab8500_fg structure
1156 *
1157 * Get the battery capacity level based on the capacity in percent
1158 */
1159static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1160{
1161 int ret, percent;
1162
pender016eaf8742013-01-11 13:12:59 +00001163 percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
Arun Murthy13151632012-02-29 21:54:27 +05301164
Lee Jonesb0284de2012-11-30 10:09:42 +00001165 if (percent <= di->bm->cap_levels->critical ||
Arun Murthy13151632012-02-29 21:54:27 +05301166 di->flags.low_bat)
1167 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
Lee Jonesb0284de2012-11-30 10:09:42 +00001168 else if (percent <= di->bm->cap_levels->low)
Arun Murthy13151632012-02-29 21:54:27 +05301169 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
Lee Jonesb0284de2012-11-30 10:09:42 +00001170 else if (percent <= di->bm->cap_levels->normal)
Arun Murthy13151632012-02-29 21:54:27 +05301171 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Lee Jonesb0284de2012-11-30 10:09:42 +00001172 else if (percent <= di->bm->cap_levels->high)
Arun Murthy13151632012-02-29 21:54:27 +05301173 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1174 else
1175 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1176
1177 return ret;
1178}
1179
1180/**
Marcus Cooperea402402013-01-11 13:12:54 +00001181 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1182 * @di: pointer to the ab8500_fg structure
1183 *
1184 * Calculates the capacity to be shown to upper layers. Scales the capacity
1185 * to have 100% as a reference from the actual capacity upon removal of charger
1186 * when charging is in maintenance mode.
1187 */
1188static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di)
1189{
1190 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1191 int capacity = di->bat_cap.prev_percent;
1192
1193 if (!cs->enable)
1194 return capacity;
1195
1196 /*
1197 * As long as we are in fully charge mode scale the capacity
1198 * to show 100%.
1199 */
1200 if (di->flags.fully_charged) {
1201 cs->cap_to_scale[0] = 100;
1202 cs->cap_to_scale[1] =
1203 max(capacity, di->bm->fg_params->maint_thres);
1204 dev_dbg(di->dev, "Scale cap with %d/%d\n",
1205 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1206 }
1207
1208 /* Calculates the scaled capacity. */
1209 if ((cs->cap_to_scale[0] != cs->cap_to_scale[1])
1210 && (cs->cap_to_scale[1] > 0))
1211 capacity = min(100,
1212 DIV_ROUND_CLOSEST(di->bat_cap.prev_percent *
1213 cs->cap_to_scale[0],
1214 cs->cap_to_scale[1]));
1215
1216 if (di->flags.charging) {
1217 if (capacity < cs->disable_cap_level) {
1218 cs->disable_cap_level = capacity;
1219 dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n",
1220 cs->disable_cap_level);
1221 } else if (!di->flags.fully_charged) {
1222 if (di->bat_cap.prev_percent >=
1223 cs->disable_cap_level) {
1224 dev_dbg(di->dev, "Disabling scaled capacity\n");
1225 cs->enable = false;
1226 capacity = di->bat_cap.prev_percent;
1227 } else {
1228 dev_dbg(di->dev,
1229 "Waiting in cap to level %d%%\n",
1230 cs->disable_cap_level);
1231 capacity = cs->disable_cap_level;
1232 }
1233 }
1234 }
1235
1236 return capacity;
1237}
1238
1239/**
1240 * ab8500_fg_update_cap_scalers() - Capacity scaling
1241 * @di: pointer to the ab8500_fg structure
1242 *
1243 * To be called when state change from charge<->discharge to update
1244 * the capacity scalers.
1245 */
1246static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di)
1247{
1248 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1249
1250 if (!cs->enable)
1251 return;
1252 if (di->flags.charging) {
1253 di->bat_cap.cap_scale.disable_cap_level =
1254 di->bat_cap.cap_scale.scaled_cap;
1255 dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n",
1256 di->bat_cap.cap_scale.disable_cap_level);
1257 } else {
1258 if (cs->scaled_cap != 100) {
1259 cs->cap_to_scale[0] = cs->scaled_cap;
1260 cs->cap_to_scale[1] = di->bat_cap.prev_percent;
1261 } else {
1262 cs->cap_to_scale[0] = 100;
1263 cs->cap_to_scale[1] =
1264 max(di->bat_cap.prev_percent,
1265 di->bm->fg_params->maint_thres);
1266 }
1267
1268 dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n",
1269 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1270 }
1271}
1272
1273/**
Arun Murthy13151632012-02-29 21:54:27 +05301274 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1275 * @di: pointer to the ab8500_fg structure
1276 * @init: capacity is allowed to go up in init mode
1277 *
1278 * Check if capacity or capacity limit has changed and notify the system
1279 * about it using the power_supply framework
1280 */
1281static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1282{
1283 bool changed = false;
pender016eaf8742013-01-11 13:12:59 +00001284 int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
Arun Murthy13151632012-02-29 21:54:27 +05301285
1286 di->bat_cap.level = ab8500_fg_capacity_level(di);
1287
1288 if (di->bat_cap.level != di->bat_cap.prev_level) {
1289 /*
1290 * We do not allow reported capacity level to go up
1291 * unless we're charging or if we're in init
1292 */
1293 if (!(!di->flags.charging && di->bat_cap.level >
1294 di->bat_cap.prev_level) || init) {
1295 dev_dbg(di->dev, "level changed from %d to %d\n",
1296 di->bat_cap.prev_level,
1297 di->bat_cap.level);
1298 di->bat_cap.prev_level = di->bat_cap.level;
1299 changed = true;
1300 } else {
1301 dev_dbg(di->dev, "level not allowed to go up "
1302 "since no charger is connected: %d to %d\n",
1303 di->bat_cap.prev_level,
1304 di->bat_cap.level);
1305 }
1306 }
1307
1308 /*
1309 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1310 * shutdown
1311 */
1312 if (di->flags.low_bat) {
1313 dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1314 di->bat_cap.prev_percent = 0;
1315 di->bat_cap.permille = 0;
pender016eaf8742013-01-11 13:12:59 +00001316 percent = 0;
Arun Murthy13151632012-02-29 21:54:27 +05301317 di->bat_cap.prev_mah = 0;
1318 di->bat_cap.mah = 0;
1319 changed = true;
1320 } else if (di->flags.fully_charged) {
1321 /*
1322 * We report 100% if algorithm reported fully charged
Marcus Cooperea402402013-01-11 13:12:54 +00001323 * and show 100% during maintenance charging (scaling).
Arun Murthy13151632012-02-29 21:54:27 +05301324 */
1325 if (di->flags.force_full) {
pender016eaf8742013-01-11 13:12:59 +00001326 di->bat_cap.prev_percent = percent;
Arun Murthy13151632012-02-29 21:54:27 +05301327 di->bat_cap.prev_mah = di->bat_cap.mah;
Marcus Cooperea402402013-01-11 13:12:54 +00001328
1329 changed = true;
1330
1331 if (!di->bat_cap.cap_scale.enable &&
1332 di->bm->capacity_scaling) {
1333 di->bat_cap.cap_scale.enable = true;
1334 di->bat_cap.cap_scale.cap_to_scale[0] = 100;
1335 di->bat_cap.cap_scale.cap_to_scale[1] =
1336 di->bat_cap.prev_percent;
1337 di->bat_cap.cap_scale.disable_cap_level = 100;
1338 }
pender016eaf8742013-01-11 13:12:59 +00001339 } else if (di->bat_cap.prev_percent != percent) {
Arun Murthy13151632012-02-29 21:54:27 +05301340 dev_dbg(di->dev,
1341 "battery reported full "
1342 "but capacity dropping: %d\n",
pender016eaf8742013-01-11 13:12:59 +00001343 percent);
1344 di->bat_cap.prev_percent = percent;
Arun Murthy13151632012-02-29 21:54:27 +05301345 di->bat_cap.prev_mah = di->bat_cap.mah;
1346
1347 changed = true;
1348 }
pender016eaf8742013-01-11 13:12:59 +00001349 } else if (di->bat_cap.prev_percent != percent) {
1350 if (percent == 0) {
Arun Murthy13151632012-02-29 21:54:27 +05301351 /*
1352 * We will not report 0% unless we've got
1353 * the LOW_BAT IRQ, no matter what the FG
1354 * algorithm says.
1355 */
1356 di->bat_cap.prev_percent = 1;
pender016eaf8742013-01-11 13:12:59 +00001357 percent = 1;
Arun Murthy13151632012-02-29 21:54:27 +05301358
1359 changed = true;
1360 } else if (!(!di->flags.charging &&
pender016eaf8742013-01-11 13:12:59 +00001361 percent > di->bat_cap.prev_percent) || init) {
Arun Murthy13151632012-02-29 21:54:27 +05301362 /*
1363 * We do not allow reported capacity to go up
1364 * unless we're charging or if we're in init
1365 */
1366 dev_dbg(di->dev,
1367 "capacity changed from %d to %d (%d)\n",
1368 di->bat_cap.prev_percent,
pender016eaf8742013-01-11 13:12:59 +00001369 percent,
Arun Murthy13151632012-02-29 21:54:27 +05301370 di->bat_cap.permille);
pender016eaf8742013-01-11 13:12:59 +00001371 di->bat_cap.prev_percent = percent;
Arun Murthy13151632012-02-29 21:54:27 +05301372 di->bat_cap.prev_mah = di->bat_cap.mah;
1373
1374 changed = true;
1375 } else {
1376 dev_dbg(di->dev, "capacity not allowed to go up since "
1377 "no charger is connected: %d to %d (%d)\n",
1378 di->bat_cap.prev_percent,
pender016eaf8742013-01-11 13:12:59 +00001379 percent,
Arun Murthy13151632012-02-29 21:54:27 +05301380 di->bat_cap.permille);
1381 }
1382 }
1383
1384 if (changed) {
Marcus Cooperea402402013-01-11 13:12:54 +00001385 if (di->bm->capacity_scaling) {
1386 di->bat_cap.cap_scale.scaled_cap =
1387 ab8500_fg_calculate_scaled_capacity(di);
1388
1389 dev_info(di->dev, "capacity=%d (%d)\n",
1390 di->bat_cap.prev_percent,
1391 di->bat_cap.cap_scale.scaled_cap);
1392 }
Arun Murthy13151632012-02-29 21:54:27 +05301393 power_supply_changed(&di->fg_psy);
1394 if (di->flags.fully_charged && di->flags.force_full) {
1395 dev_dbg(di->dev, "Battery full, notifying.\n");
1396 di->flags.force_full = false;
1397 sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1398 }
1399 sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1400 }
1401}
1402
1403static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1404 enum ab8500_fg_charge_state new_state)
1405{
1406 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1407 di->charge_state,
1408 charge_state[di->charge_state],
1409 new_state,
1410 charge_state[new_state]);
1411
1412 di->charge_state = new_state;
1413}
1414
1415static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
Anton Vorontsov0fff22e2012-03-14 04:41:37 +04001416 enum ab8500_fg_discharge_state new_state)
Arun Murthy13151632012-02-29 21:54:27 +05301417{
1418 dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
1419 di->discharge_state,
1420 discharge_state[di->discharge_state],
1421 new_state,
1422 discharge_state[new_state]);
1423
1424 di->discharge_state = new_state;
1425}
1426
1427/**
1428 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1429 * @di: pointer to the ab8500_fg structure
1430 *
1431 * Battery capacity calculation state machine for when we're charging
1432 */
1433static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1434{
1435 /*
1436 * If we change to discharge mode
1437 * we should start with recovery
1438 */
1439 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1440 ab8500_fg_discharge_state_to(di,
1441 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1442
1443 switch (di->charge_state) {
1444 case AB8500_FG_CHARGE_INIT:
1445 di->fg_samples = SEC_TO_SAMPLE(
Lee Jonesb0284de2012-11-30 10:09:42 +00001446 di->bm->fg_params->accu_charging);
Arun Murthy13151632012-02-29 21:54:27 +05301447
1448 ab8500_fg_coulomb_counter(di, true);
1449 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1450
1451 break;
1452
1453 case AB8500_FG_CHARGE_READOUT:
1454 /*
1455 * Read the FG and calculate the new capacity
1456 */
1457 mutex_lock(&di->cc_lock);
Marcus Cooperea402402013-01-11 13:12:54 +00001458 if (!di->flags.conv_done && !di->flags.force_full) {
Arun Murthy13151632012-02-29 21:54:27 +05301459 /* Wasn't the CC IRQ that got us here */
1460 mutex_unlock(&di->cc_lock);
1461 dev_dbg(di->dev, "%s CC conv not done\n",
1462 __func__);
1463
1464 break;
1465 }
1466 di->flags.conv_done = false;
1467 mutex_unlock(&di->cc_lock);
1468
1469 ab8500_fg_calc_cap_charging(di);
1470
1471 break;
1472
1473 default:
1474 break;
1475 }
1476
1477 /* Check capacity limits */
1478 ab8500_fg_check_capacity_limits(di, false);
1479}
1480
1481static void force_capacity(struct ab8500_fg *di)
1482{
1483 int cap;
1484
1485 ab8500_fg_clear_cap_samples(di);
1486 cap = di->bat_cap.user_mah;
1487 if (cap > di->bat_cap.max_mah_design) {
1488 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1489 " %d\n", cap, di->bat_cap.max_mah_design);
1490 cap = di->bat_cap.max_mah_design;
1491 }
1492 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1493 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1494 di->bat_cap.mah = cap;
1495 ab8500_fg_check_capacity_limits(di, true);
1496}
1497
1498static bool check_sysfs_capacity(struct ab8500_fg *di)
1499{
1500 int cap, lower, upper;
1501 int cap_permille;
1502
1503 cap = di->bat_cap.user_mah;
1504
1505 cap_permille = ab8500_fg_convert_mah_to_permille(di,
1506 di->bat_cap.user_mah);
1507
Lee Jonesb0284de2012-11-30 10:09:42 +00001508 lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
1509 upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
Arun Murthy13151632012-02-29 21:54:27 +05301510
1511 if (lower < 0)
1512 lower = 0;
1513 /* 1000 is permille, -> 100 percent */
1514 if (upper > 1000)
1515 upper = 1000;
1516
1517 dev_dbg(di->dev, "Capacity limits:"
1518 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1519 lower, cap_permille, upper, cap, di->bat_cap.mah);
1520
1521 /* If within limits, use the saved capacity and exit estimation...*/
1522 if (cap_permille > lower && cap_permille < upper) {
1523 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1524 force_capacity(di);
1525 return true;
1526 }
1527 dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1528 return false;
1529}
1530
1531/**
1532 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1533 * @di: pointer to the ab8500_fg structure
1534 *
1535 * Battery capacity calculation state machine for when we're discharging
1536 */
1537static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1538{
1539 int sleep_time;
1540
1541 /* If we change to charge mode we should start with init */
1542 if (di->charge_state != AB8500_FG_CHARGE_INIT)
1543 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1544
1545 switch (di->discharge_state) {
1546 case AB8500_FG_DISCHARGE_INIT:
1547 /* We use the FG IRQ to work on */
1548 di->init_cnt = 0;
Lee Jonesb0284de2012-11-30 10:09:42 +00001549 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
Arun Murthy13151632012-02-29 21:54:27 +05301550 ab8500_fg_coulomb_counter(di, true);
1551 ab8500_fg_discharge_state_to(di,
1552 AB8500_FG_DISCHARGE_INITMEASURING);
1553
1554 /* Intentional fallthrough */
1555 case AB8500_FG_DISCHARGE_INITMEASURING:
1556 /*
1557 * Discard a number of samples during startup.
1558 * After that, use compensated voltage for a few
1559 * samples to get an initial capacity.
1560 * Then go to READOUT
1561 */
Lee Jonesb0284de2012-11-30 10:09:42 +00001562 sleep_time = di->bm->fg_params->init_timer;
Arun Murthy13151632012-02-29 21:54:27 +05301563
1564 /* Discard the first [x] seconds */
Lee Jonesb0284de2012-11-30 10:09:42 +00001565 if (di->init_cnt > di->bm->fg_params->init_discard_time) {
Arun Murthy13151632012-02-29 21:54:27 +05301566 ab8500_fg_calc_cap_discharge_voltage(di, true);
1567
1568 ab8500_fg_check_capacity_limits(di, true);
1569 }
1570
1571 di->init_cnt += sleep_time;
Lee Jonesb0284de2012-11-30 10:09:42 +00001572 if (di->init_cnt > di->bm->fg_params->init_total_time)
Arun Murthy13151632012-02-29 21:54:27 +05301573 ab8500_fg_discharge_state_to(di,
1574 AB8500_FG_DISCHARGE_READOUT_INIT);
1575
1576 break;
1577
1578 case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1579 di->recovery_cnt = 0;
1580 di->recovery_needed = true;
1581 ab8500_fg_discharge_state_to(di,
1582 AB8500_FG_DISCHARGE_RECOVERY);
1583
1584 /* Intentional fallthrough */
1585
1586 case AB8500_FG_DISCHARGE_RECOVERY:
Lee Jonesb0284de2012-11-30 10:09:42 +00001587 sleep_time = di->bm->fg_params->recovery_sleep_timer;
Arun Murthy13151632012-02-29 21:54:27 +05301588
1589 /*
1590 * We should check the power consumption
1591 * If low, go to READOUT (after x min) or
1592 * RECOVERY_SLEEP if time left.
1593 * If high, go to READOUT
1594 */
1595 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1596
1597 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1598 if (di->recovery_cnt >
Lee Jonesb0284de2012-11-30 10:09:42 +00001599 di->bm->fg_params->recovery_total_time) {
Arun Murthy13151632012-02-29 21:54:27 +05301600 di->fg_samples = SEC_TO_SAMPLE(
Lee Jonesb0284de2012-11-30 10:09:42 +00001601 di->bm->fg_params->accu_high_curr);
Arun Murthy13151632012-02-29 21:54:27 +05301602 ab8500_fg_coulomb_counter(di, true);
1603 ab8500_fg_discharge_state_to(di,
1604 AB8500_FG_DISCHARGE_READOUT);
1605 di->recovery_needed = false;
1606 } else {
1607 queue_delayed_work(di->fg_wq,
1608 &di->fg_periodic_work,
1609 sleep_time * HZ);
1610 }
1611 di->recovery_cnt += sleep_time;
1612 } else {
1613 di->fg_samples = SEC_TO_SAMPLE(
Lee Jonesb0284de2012-11-30 10:09:42 +00001614 di->bm->fg_params->accu_high_curr);
Arun Murthy13151632012-02-29 21:54:27 +05301615 ab8500_fg_coulomb_counter(di, true);
1616 ab8500_fg_discharge_state_to(di,
1617 AB8500_FG_DISCHARGE_READOUT);
1618 }
1619 break;
1620
1621 case AB8500_FG_DISCHARGE_READOUT_INIT:
1622 di->fg_samples = SEC_TO_SAMPLE(
Lee Jonesb0284de2012-11-30 10:09:42 +00001623 di->bm->fg_params->accu_high_curr);
Arun Murthy13151632012-02-29 21:54:27 +05301624 ab8500_fg_coulomb_counter(di, true);
1625 ab8500_fg_discharge_state_to(di,
1626 AB8500_FG_DISCHARGE_READOUT);
1627 break;
1628
1629 case AB8500_FG_DISCHARGE_READOUT:
1630 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1631
1632 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1633 /* Detect mode change */
1634 if (di->high_curr_mode) {
1635 di->high_curr_mode = false;
1636 di->high_curr_cnt = 0;
1637 }
1638
1639 if (di->recovery_needed) {
1640 ab8500_fg_discharge_state_to(di,
Martin Bergströmffaa39d92012-05-04 14:43:50 +02001641 AB8500_FG_DISCHARGE_INIT_RECOVERY);
Arun Murthy13151632012-02-29 21:54:27 +05301642
1643 queue_delayed_work(di->fg_wq,
1644 &di->fg_periodic_work, 0);
1645
1646 break;
1647 }
1648
1649 ab8500_fg_calc_cap_discharge_voltage(di, true);
1650 } else {
1651 mutex_lock(&di->cc_lock);
1652 if (!di->flags.conv_done) {
1653 /* Wasn't the CC IRQ that got us here */
1654 mutex_unlock(&di->cc_lock);
1655 dev_dbg(di->dev, "%s CC conv not done\n",
1656 __func__);
1657
1658 break;
1659 }
1660 di->flags.conv_done = false;
1661 mutex_unlock(&di->cc_lock);
1662
1663 /* Detect mode change */
1664 if (!di->high_curr_mode) {
1665 di->high_curr_mode = true;
1666 di->high_curr_cnt = 0;
1667 }
1668
1669 di->high_curr_cnt +=
Lee Jonesb0284de2012-11-30 10:09:42 +00001670 di->bm->fg_params->accu_high_curr;
Arun Murthy13151632012-02-29 21:54:27 +05301671 if (di->high_curr_cnt >
Lee Jonesb0284de2012-11-30 10:09:42 +00001672 di->bm->fg_params->high_curr_time)
Arun Murthy13151632012-02-29 21:54:27 +05301673 di->recovery_needed = true;
1674
1675 ab8500_fg_calc_cap_discharge_fg(di);
1676 }
1677
1678 ab8500_fg_check_capacity_limits(di, false);
1679
1680 break;
1681
1682 case AB8500_FG_DISCHARGE_WAKEUP:
1683 ab8500_fg_coulomb_counter(di, true);
Arun Murthy13151632012-02-29 21:54:27 +05301684 ab8500_fg_calc_cap_discharge_voltage(di, true);
1685
1686 di->fg_samples = SEC_TO_SAMPLE(
Lee Jonesb0284de2012-11-30 10:09:42 +00001687 di->bm->fg_params->accu_high_curr);
Arun Murthy13151632012-02-29 21:54:27 +05301688 ab8500_fg_coulomb_counter(di, true);
1689 ab8500_fg_discharge_state_to(di,
1690 AB8500_FG_DISCHARGE_READOUT);
1691
1692 ab8500_fg_check_capacity_limits(di, false);
1693
1694 break;
1695
1696 default:
1697 break;
1698 }
1699}
1700
1701/**
1702 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1703 * @di: pointer to the ab8500_fg structure
1704 *
1705 */
1706static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1707{
1708 int ret;
1709
1710 switch (di->calib_state) {
1711 case AB8500_FG_CALIB_INIT:
1712 dev_dbg(di->dev, "Calibration ongoing...\n");
1713
1714 ret = abx500_mask_and_set_register_interruptible(di->dev,
1715 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1716 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1717 if (ret < 0)
1718 goto err;
1719
1720 ret = abx500_mask_and_set_register_interruptible(di->dev,
1721 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1722 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1723 if (ret < 0)
1724 goto err;
1725 di->calib_state = AB8500_FG_CALIB_WAIT;
1726 break;
1727 case AB8500_FG_CALIB_END:
1728 ret = abx500_mask_and_set_register_interruptible(di->dev,
1729 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1730 CC_MUXOFFSET, CC_MUXOFFSET);
1731 if (ret < 0)
1732 goto err;
1733 di->flags.calibrate = false;
1734 dev_dbg(di->dev, "Calibration done...\n");
1735 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1736 break;
1737 case AB8500_FG_CALIB_WAIT:
1738 dev_dbg(di->dev, "Calibration WFI\n");
1739 default:
1740 break;
1741 }
1742 return;
1743err:
1744 /* Something went wrong, don't calibrate then */
1745 dev_err(di->dev, "failed to calibrate the CC\n");
1746 di->flags.calibrate = false;
1747 di->calib_state = AB8500_FG_CALIB_INIT;
1748 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1749}
1750
1751/**
1752 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1753 * @di: pointer to the ab8500_fg structure
1754 *
1755 * Entry point for the battery capacity calculation state machine
1756 */
1757static void ab8500_fg_algorithm(struct ab8500_fg *di)
1758{
1759 if (di->flags.calibrate)
1760 ab8500_fg_algorithm_calibrate(di);
1761 else {
1762 if (di->flags.charging)
1763 ab8500_fg_algorithm_charging(di);
1764 else
1765 ab8500_fg_algorithm_discharging(di);
1766 }
1767
Hakan Berg64277612012-07-23 14:00:50 +02001768 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
Arun Murthy13151632012-02-29 21:54:27 +05301769 "%d %d %d %d %d %d %d\n",
1770 di->bat_cap.max_mah_design,
Hakan Berg64277612012-07-23 14:00:50 +02001771 di->bat_cap.max_mah,
Arun Murthy13151632012-02-29 21:54:27 +05301772 di->bat_cap.mah,
1773 di->bat_cap.permille,
1774 di->bat_cap.level,
1775 di->bat_cap.prev_mah,
1776 di->bat_cap.prev_percent,
1777 di->bat_cap.prev_level,
1778 di->vbat,
1779 di->inst_curr,
1780 di->avg_curr,
1781 di->accu_charge,
1782 di->flags.charging,
1783 di->charge_state,
1784 di->discharge_state,
1785 di->high_curr_mode,
1786 di->recovery_needed);
1787}
1788
1789/**
1790 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1791 * @work: pointer to the work_struct structure
1792 *
1793 * Work queue function for periodic work
1794 */
1795static void ab8500_fg_periodic_work(struct work_struct *work)
1796{
1797 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1798 fg_periodic_work.work);
1799
1800 if (di->init_capacity) {
Arun Murthy13151632012-02-29 21:54:27 +05301801 /* Get an initial capacity calculation */
1802 ab8500_fg_calc_cap_discharge_voltage(di, true);
1803 ab8500_fg_check_capacity_limits(di, true);
1804 di->init_capacity = false;
1805
1806 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1807 } else if (di->flags.user_cap) {
1808 if (check_sysfs_capacity(di)) {
1809 ab8500_fg_check_capacity_limits(di, true);
1810 if (di->flags.charging)
1811 ab8500_fg_charge_state_to(di,
1812 AB8500_FG_CHARGE_INIT);
1813 else
1814 ab8500_fg_discharge_state_to(di,
1815 AB8500_FG_DISCHARGE_READOUT_INIT);
1816 }
1817 di->flags.user_cap = false;
1818 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1819 } else
1820 ab8500_fg_algorithm(di);
1821
1822}
1823
1824/**
1825 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1826 * @work: pointer to the work_struct structure
1827 *
1828 * Work queue function for checking the OVV_BAT condition
1829 */
1830static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1831{
1832 int ret;
1833 u8 reg_value;
1834
1835 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1836 fg_check_hw_failure_work.work);
1837
1838 /*
1839 * If we have had a battery over-voltage situation,
1840 * check ovv-bit to see if it should be reset.
1841 */
Hakan Berg8bcf3b32013-01-11 13:13:01 +00001842 ret = abx500_get_register_interruptible(di->dev,
1843 AB8500_CHARGER, AB8500_CH_STAT_REG,
1844 &reg_value);
1845 if (ret < 0) {
1846 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1847 return;
1848 }
1849 if ((reg_value & BATT_OVV) == BATT_OVV) {
1850 if (!di->flags.bat_ovv) {
1851 dev_dbg(di->dev, "Battery OVV\n");
1852 di->flags.bat_ovv = true;
Arun Murthy13151632012-02-29 21:54:27 +05301853 power_supply_changed(&di->fg_psy);
Arun Murthy13151632012-02-29 21:54:27 +05301854 }
Arun Murthy13151632012-02-29 21:54:27 +05301855 /* Not yet recovered from ovv, reschedule this test */
1856 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
Lee Jones41ce2562013-01-11 13:13:06 +00001857 HZ);
Hakan Berg8bcf3b32013-01-11 13:13:01 +00001858 } else {
1859 dev_dbg(di->dev, "Battery recovered from OVV\n");
1860 di->flags.bat_ovv = false;
1861 power_supply_changed(&di->fg_psy);
Arun Murthy13151632012-02-29 21:54:27 +05301862 }
1863}
1864
1865/**
1866 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1867 * @work: pointer to the work_struct structure
1868 *
1869 * Work queue function for checking the LOW_BAT condition
1870 */
1871static void ab8500_fg_low_bat_work(struct work_struct *work)
1872{
1873 int vbat;
1874
1875 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1876 fg_low_bat_work.work);
1877
1878 vbat = ab8500_fg_bat_voltage(di);
1879
1880 /* Check if LOW_BAT still fulfilled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001881 if (vbat < di->bm->fg_params->lowbat_threshold) {
Hakan Berg75f2a212012-05-10 08:43:25 +02001882 /* Is it time to shut down? */
1883 if (di->low_bat_cnt < 1) {
1884 di->flags.low_bat = true;
1885 dev_warn(di->dev, "Shut down pending...\n");
1886 } else {
1887 /*
1888 * Else we need to re-schedule this check to be able to detect
1889 * if the voltage increases again during charging or
1890 * due to decreasing load.
1891 */
1892 di->low_bat_cnt--;
1893 dev_warn(di->dev, "Battery voltage still LOW\n");
1894 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1895 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1896 }
Arun Murthy13151632012-02-29 21:54:27 +05301897 } else {
Hakan Berg75f2a212012-05-10 08:43:25 +02001898 di->flags.low_bat_delay = false;
1899 di->low_bat_cnt = 10;
Arun Murthy13151632012-02-29 21:54:27 +05301900 dev_warn(di->dev, "Battery voltage OK again\n");
1901 }
1902
1903 /* This is needed to dispatch LOW_BAT */
1904 ab8500_fg_check_capacity_limits(di, false);
Arun Murthy13151632012-02-29 21:54:27 +05301905}
1906
1907/**
1908 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1909 * to the target voltage.
1910 * @di: pointer to the ab8500_fg structure
1911 * @target target voltage
1912 *
1913 * Returns bit pattern closest to the target voltage
1914 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1915 */
1916
1917static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1918{
1919 if (target > BATT_OK_MIN +
1920 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1921 return BATT_OK_MAX_NR_INCREMENTS;
1922 if (target < BATT_OK_MIN)
1923 return 0;
1924 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1925}
1926
1927/**
1928 * ab8500_fg_battok_init_hw_register - init battok levels
1929 * @di: pointer to the ab8500_fg structure
1930 *
1931 */
1932
1933static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1934{
1935 int selected;
1936 int sel0;
1937 int sel1;
1938 int cbp_sel0;
1939 int cbp_sel1;
1940 int ret;
1941 int new_val;
1942
Lee Jonesb0284de2012-11-30 10:09:42 +00001943 sel0 = di->bm->fg_params->battok_falling_th_sel0;
1944 sel1 = di->bm->fg_params->battok_raising_th_sel1;
Arun Murthy13151632012-02-29 21:54:27 +05301945
1946 cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1947 cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1948
1949 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1950
1951 if (selected != sel0)
1952 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1953 sel0, selected, cbp_sel0);
1954
1955 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1956
1957 if (selected != sel1)
1958 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1959 sel1, selected, cbp_sel1);
1960
1961 new_val = cbp_sel0 | (cbp_sel1 << 4);
1962
1963 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1964 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1965 AB8500_BATT_OK_REG, new_val);
1966 return ret;
1967}
1968
1969/**
1970 * ab8500_fg_instant_work() - Run the FG state machine instantly
1971 * @work: pointer to the work_struct structure
1972 *
1973 * Work queue function for instant work
1974 */
1975static void ab8500_fg_instant_work(struct work_struct *work)
1976{
1977 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1978
1979 ab8500_fg_algorithm(di);
1980}
1981
1982/**
1983 * ab8500_fg_cc_data_end_handler() - isr to get battery avg current.
1984 * @irq: interrupt number
1985 * @_di: pointer to the ab8500_fg structure
1986 *
1987 * Returns IRQ status(IRQ_HANDLED)
1988 */
1989static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1990{
1991 struct ab8500_fg *di = _di;
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +00001992 if (!di->nbr_cceoc_irq_cnt) {
1993 di->nbr_cceoc_irq_cnt++;
1994 complete(&di->ab8500_fg_started);
1995 } else {
1996 di->nbr_cceoc_irq_cnt = 0;
1997 complete(&di->ab8500_fg_complete);
1998 }
Arun Murthy13151632012-02-29 21:54:27 +05301999 return IRQ_HANDLED;
2000}
2001
2002/**
2003 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2004 * @irq: interrupt number
2005 * @_di: pointer to the ab8500_fg structure
2006 *
2007 * Returns IRQ status(IRQ_HANDLED)
2008 */
2009static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
2010{
2011 struct ab8500_fg *di = _di;
2012 di->calib_state = AB8500_FG_CALIB_END;
2013 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2014 return IRQ_HANDLED;
2015}
2016
2017/**
2018 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2019 * @irq: interrupt number
2020 * @_di: pointer to the ab8500_fg structure
2021 *
2022 * Returns IRQ status(IRQ_HANDLED)
2023 */
2024static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
2025{
2026 struct ab8500_fg *di = _di;
2027
2028 queue_work(di->fg_wq, &di->fg_acc_cur_work);
2029
2030 return IRQ_HANDLED;
2031}
2032
2033/**
2034 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2035 * @irq: interrupt number
2036 * @_di: pointer to the ab8500_fg structure
2037 *
2038 * Returns IRQ status(IRQ_HANDLED)
2039 */
2040static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
2041{
2042 struct ab8500_fg *di = _di;
2043
2044 dev_dbg(di->dev, "Battery OVV\n");
Arun Murthy13151632012-02-29 21:54:27 +05302045
2046 /* Schedule a new HW failure check */
2047 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
2048
2049 return IRQ_HANDLED;
2050}
2051
2052/**
2053 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2054 * @irq: interrupt number
2055 * @_di: pointer to the ab8500_fg structure
2056 *
2057 * Returns IRQ status(IRQ_HANDLED)
2058 */
2059static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
2060{
2061 struct ab8500_fg *di = _di;
2062
Hakan Berg75f2a212012-05-10 08:43:25 +02002063 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
Arun Murthy13151632012-02-29 21:54:27 +05302064 if (!di->flags.low_bat_delay) {
2065 dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
2066 di->flags.low_bat_delay = true;
2067 /*
2068 * Start a timer to check LOW_BAT again after some time
2069 * This is done to avoid shutdown on single voltage dips
2070 */
2071 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
2072 round_jiffies(LOW_BAT_CHECK_INTERVAL));
2073 }
2074 return IRQ_HANDLED;
2075}
2076
2077/**
2078 * ab8500_fg_get_property() - get the fg properties
2079 * @psy: pointer to the power_supply structure
2080 * @psp: pointer to the power_supply_property structure
2081 * @val: pointer to the power_supply_propval union
2082 *
2083 * This function gets called when an application tries to get the
2084 * fg properties by reading the sysfs files.
2085 * voltage_now: battery voltage
2086 * current_now: battery instant current
2087 * current_avg: battery average current
2088 * charge_full_design: capacity where battery is considered full
2089 * charge_now: battery capacity in nAh
2090 * capacity: capacity in percent
2091 * capacity_level: capacity level
2092 *
2093 * Returns error code in case of failure else 0 on success
2094 */
2095static int ab8500_fg_get_property(struct power_supply *psy,
2096 enum power_supply_property psp,
2097 union power_supply_propval *val)
2098{
2099 struct ab8500_fg *di;
2100
2101 di = to_ab8500_fg_device_info(psy);
2102
2103 /*
2104 * If battery is identified as unknown and charging of unknown
2105 * batteries is disabled, we always report 100% capacity and
2106 * capacity level UNKNOWN, since we can't calculate
2107 * remaining capacity
2108 */
2109
2110 switch (psp) {
2111 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2112 if (di->flags.bat_ovv)
2113 val->intval = BATT_OVV_VALUE * 1000;
2114 else
2115 val->intval = di->vbat * 1000;
2116 break;
2117 case POWER_SUPPLY_PROP_CURRENT_NOW:
2118 val->intval = di->inst_curr * 1000;
2119 break;
2120 case POWER_SUPPLY_PROP_CURRENT_AVG:
2121 val->intval = di->avg_curr * 1000;
2122 break;
2123 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
2124 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2125 di->bat_cap.max_mah_design);
2126 break;
2127 case POWER_SUPPLY_PROP_ENERGY_FULL:
2128 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2129 di->bat_cap.max_mah);
2130 break;
2131 case POWER_SUPPLY_PROP_ENERGY_NOW:
Lee Jonesb0284de2012-11-30 10:09:42 +00002132 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
Arun Murthy13151632012-02-29 21:54:27 +05302133 di->flags.batt_id_received)
2134 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2135 di->bat_cap.max_mah);
2136 else
2137 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2138 di->bat_cap.prev_mah);
2139 break;
2140 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
2141 val->intval = di->bat_cap.max_mah_design;
2142 break;
2143 case POWER_SUPPLY_PROP_CHARGE_FULL:
2144 val->intval = di->bat_cap.max_mah;
2145 break;
2146 case POWER_SUPPLY_PROP_CHARGE_NOW:
Lee Jonesb0284de2012-11-30 10:09:42 +00002147 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
Arun Murthy13151632012-02-29 21:54:27 +05302148 di->flags.batt_id_received)
2149 val->intval = di->bat_cap.max_mah;
2150 else
2151 val->intval = di->bat_cap.prev_mah;
2152 break;
2153 case POWER_SUPPLY_PROP_CAPACITY:
Martin Bergstrome82c5bd2012-06-08 16:21:48 +02002154 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
Arun Murthy13151632012-02-29 21:54:27 +05302155 di->flags.batt_id_received)
2156 val->intval = 100;
2157 else
2158 val->intval = di->bat_cap.prev_percent;
2159 break;
2160 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Lee Jonesb0284de2012-11-30 10:09:42 +00002161 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
Arun Murthy13151632012-02-29 21:54:27 +05302162 di->flags.batt_id_received)
2163 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2164 else
2165 val->intval = di->bat_cap.prev_level;
2166 break;
2167 default:
2168 return -EINVAL;
2169 }
2170 return 0;
2171}
2172
2173static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2174{
2175 struct power_supply *psy;
2176 struct power_supply *ext;
2177 struct ab8500_fg *di;
2178 union power_supply_propval ret;
2179 int i, j;
2180 bool psy_found = false;
2181
2182 psy = (struct power_supply *)data;
2183 ext = dev_get_drvdata(dev);
2184 di = to_ab8500_fg_device_info(psy);
2185
2186 /*
2187 * For all psy where the name of your driver
2188 * appears in any supplied_to
2189 */
2190 for (i = 0; i < ext->num_supplicants; i++) {
2191 if (!strcmp(ext->supplied_to[i], psy->name))
2192 psy_found = true;
2193 }
2194
2195 if (!psy_found)
2196 return 0;
2197
2198 /* Go through all properties for the psy */
2199 for (j = 0; j < ext->num_properties; j++) {
2200 enum power_supply_property prop;
2201 prop = ext->properties[j];
2202
2203 if (ext->get_property(ext, prop, &ret))
2204 continue;
2205
2206 switch (prop) {
2207 case POWER_SUPPLY_PROP_STATUS:
2208 switch (ext->type) {
2209 case POWER_SUPPLY_TYPE_BATTERY:
2210 switch (ret.intval) {
2211 case POWER_SUPPLY_STATUS_UNKNOWN:
2212 case POWER_SUPPLY_STATUS_DISCHARGING:
2213 case POWER_SUPPLY_STATUS_NOT_CHARGING:
2214 if (!di->flags.charging)
2215 break;
2216 di->flags.charging = false;
2217 di->flags.fully_charged = false;
Marcus Cooperea402402013-01-11 13:12:54 +00002218 if (di->bm->capacity_scaling)
2219 ab8500_fg_update_cap_scalers(di);
Arun Murthy13151632012-02-29 21:54:27 +05302220 queue_work(di->fg_wq, &di->fg_work);
2221 break;
2222 case POWER_SUPPLY_STATUS_FULL:
2223 if (di->flags.fully_charged)
2224 break;
2225 di->flags.fully_charged = true;
2226 di->flags.force_full = true;
2227 /* Save current capacity as maximum */
2228 di->bat_cap.max_mah = di->bat_cap.mah;
2229 queue_work(di->fg_wq, &di->fg_work);
2230 break;
2231 case POWER_SUPPLY_STATUS_CHARGING:
Marcus Cooperea402402013-01-11 13:12:54 +00002232 if (di->flags.charging &&
2233 !di->flags.fully_charged)
Arun Murthy13151632012-02-29 21:54:27 +05302234 break;
2235 di->flags.charging = true;
2236 di->flags.fully_charged = false;
Marcus Cooperea402402013-01-11 13:12:54 +00002237 if (di->bm->capacity_scaling)
2238 ab8500_fg_update_cap_scalers(di);
Arun Murthy13151632012-02-29 21:54:27 +05302239 queue_work(di->fg_wq, &di->fg_work);
2240 break;
2241 };
2242 default:
2243 break;
2244 };
2245 break;
2246 case POWER_SUPPLY_PROP_TECHNOLOGY:
2247 switch (ext->type) {
2248 case POWER_SUPPLY_TYPE_BATTERY:
Rajkumar Kasirajan1a793a12012-05-30 14:54:28 +05302249 if (!di->flags.batt_id_received &&
2250 di->bm->batt_id != BATTERY_UNKNOWN) {
Anton Vorontsovc34a61b2012-03-14 04:39:01 +04002251 const struct abx500_battery_type *b;
2252
Lee Jonesb0284de2012-11-30 10:09:42 +00002253 b = &(di->bm->bat_type[di->bm->batt_id]);
Arun Murthy13151632012-02-29 21:54:27 +05302254
2255 di->flags.batt_id_received = true;
2256
2257 di->bat_cap.max_mah_design =
2258 MILLI_TO_MICRO *
2259 b->charge_full_design;
2260
2261 di->bat_cap.max_mah =
2262 di->bat_cap.max_mah_design;
2263
2264 di->vbat_nom = b->nominal_voltage;
2265 }
2266
2267 if (ret.intval)
2268 di->flags.batt_unknown = false;
2269 else
2270 di->flags.batt_unknown = true;
2271 break;
2272 default:
2273 break;
2274 }
2275 break;
2276 case POWER_SUPPLY_PROP_TEMP:
2277 switch (ext->type) {
2278 case POWER_SUPPLY_TYPE_BATTERY:
Marcus Cooperea402402013-01-11 13:12:54 +00002279 if (di->flags.batt_id_received)
2280 di->bat_temp = ret.intval;
Arun Murthy13151632012-02-29 21:54:27 +05302281 break;
2282 default:
2283 break;
2284 }
2285 break;
2286 default:
2287 break;
2288 }
2289 }
2290 return 0;
2291}
2292
2293/**
2294 * ab8500_fg_init_hw_registers() - Set up FG related registers
2295 * @di: pointer to the ab8500_fg structure
2296 *
2297 * Set up battery OVV, low battery voltage registers
2298 */
2299static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2300{
2301 int ret;
2302
2303 /* Set VBAT OVV threshold */
2304 ret = abx500_mask_and_set_register_interruptible(di->dev,
2305 AB8500_CHARGER,
2306 AB8500_BATT_OVV,
2307 BATT_OVV_TH_4P75,
2308 BATT_OVV_TH_4P75);
2309 if (ret) {
2310 dev_err(di->dev, "failed to set BATT_OVV\n");
2311 goto out;
2312 }
2313
2314 /* Enable VBAT OVV detection */
2315 ret = abx500_mask_and_set_register_interruptible(di->dev,
2316 AB8500_CHARGER,
2317 AB8500_BATT_OVV,
2318 BATT_OVV_ENA,
2319 BATT_OVV_ENA);
2320 if (ret) {
2321 dev_err(di->dev, "failed to enable BATT_OVV\n");
2322 goto out;
2323 }
2324
2325 /* Low Battery Voltage */
2326 ret = abx500_set_register_interruptible(di->dev,
2327 AB8500_SYS_CTRL2_BLOCK,
2328 AB8500_LOW_BAT_REG,
2329 ab8500_volt_to_regval(
Lee Jonesb0284de2012-11-30 10:09:42 +00002330 di->bm->fg_params->lowbat_threshold) << 1 |
Arun Murthy13151632012-02-29 21:54:27 +05302331 LOW_BAT_ENABLE);
2332 if (ret) {
2333 dev_err(di->dev, "%s write failed\n", __func__);
2334 goto out;
2335 }
2336
2337 /* Battery OK threshold */
2338 ret = ab8500_fg_battok_init_hw_register(di);
2339 if (ret) {
2340 dev_err(di->dev, "BattOk init write failed.\n");
2341 goto out;
2342 }
Lee Jones93ff7222012-05-31 16:16:36 +02002343
2344 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
2345 abx500_get_chip_id(di->dev) >= AB8500_CUT2P0)
2346 || is_ab8540(di->parent)) {
2347 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2348 AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time);
2349
2350 if (ret) {
2351 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__);
2352 goto out;
2353 };
2354
2355 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2356 AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time);
2357
2358 if (ret) {
2359 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__);
2360 goto out;
2361 };
2362
2363 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2364 AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart);
2365
2366 if (ret) {
2367 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__);
2368 goto out;
2369 };
2370
2371 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2372 AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time);
2373
2374 if (ret) {
2375 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__);
2376 goto out;
2377 };
2378
2379 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2380 AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable);
2381
2382 if (ret) {
2383 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__);
2384 goto out;
2385 };
2386 }
Arun Murthy13151632012-02-29 21:54:27 +05302387out:
2388 return ret;
2389}
2390
2391/**
2392 * ab8500_fg_external_power_changed() - callback for power supply changes
2393 * @psy: pointer to the structure power_supply
2394 *
2395 * This function is the entry point of the pointer external_power_changed
2396 * of the structure power_supply.
2397 * This function gets executed when there is a change in any external power
2398 * supply that this driver needs to be notified of.
2399 */
2400static void ab8500_fg_external_power_changed(struct power_supply *psy)
2401{
2402 struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
2403
2404 class_for_each_device(power_supply_class, NULL,
2405 &di->fg_psy, ab8500_fg_get_ext_psy_data);
2406}
2407
2408/**
2409 * abab8500_fg_reinit_work() - work to reset the FG algorithm
2410 * @work: pointer to the work_struct structure
2411 *
2412 * Used to reset the current battery capacity to be able to
2413 * retrigger a new voltage base capacity calculation. For
2414 * test and verification purpose.
2415 */
2416static void ab8500_fg_reinit_work(struct work_struct *work)
2417{
2418 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2419 fg_reinit_work.work);
2420
2421 if (di->flags.calibrate == false) {
2422 dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2423 ab8500_fg_clear_cap_samples(di);
2424 ab8500_fg_calc_cap_discharge_voltage(di, true);
2425 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2426 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2427 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2428
2429 } else {
2430 dev_err(di->dev, "Residual offset calibration ongoing "
2431 "retrying..\n");
2432 /* Wait one second until next try*/
2433 queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2434 round_jiffies(1));
2435 }
2436}
2437
2438/**
2439 * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values
2440 *
2441 * This function can be used to force the FG algorithm to recalculate a new
2442 * voltage based battery capacity.
2443 */
2444void ab8500_fg_reinit(void)
2445{
2446 struct ab8500_fg *di = ab8500_fg_get();
2447 /* User won't be notified if a null pointer returned. */
2448 if (di != NULL)
2449 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
2450}
2451
2452/* Exposure to the sysfs interface */
2453
2454struct ab8500_fg_sysfs_entry {
2455 struct attribute attr;
2456 ssize_t (*show)(struct ab8500_fg *, char *);
2457 ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2458};
2459
2460static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2461{
2462 return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2463}
2464
2465static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2466 size_t count)
2467{
2468 unsigned long charge_full;
2469 ssize_t ret = -EINVAL;
2470
2471 ret = strict_strtoul(buf, 10, &charge_full);
2472
Anton Vorontsov5ae2b822012-03-26 20:18:33 +04002473 dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
Arun Murthy13151632012-02-29 21:54:27 +05302474
2475 if (!ret) {
2476 di->bat_cap.max_mah = (int) charge_full;
2477 ret = count;
2478 }
2479 return ret;
2480}
2481
2482static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2483{
2484 return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2485}
2486
2487static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2488 size_t count)
2489{
2490 unsigned long charge_now;
2491 ssize_t ret;
2492
2493 ret = strict_strtoul(buf, 10, &charge_now);
2494
Anton Vorontsov5ae2b822012-03-26 20:18:33 +04002495 dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
Arun Murthy13151632012-02-29 21:54:27 +05302496 ret, charge_now, di->bat_cap.prev_mah);
2497
2498 if (!ret) {
2499 di->bat_cap.user_mah = (int) charge_now;
2500 di->flags.user_cap = true;
2501 ret = count;
2502 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2503 }
2504 return ret;
2505}
2506
2507static struct ab8500_fg_sysfs_entry charge_full_attr =
2508 __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2509
2510static struct ab8500_fg_sysfs_entry charge_now_attr =
2511 __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2512
2513static ssize_t
2514ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2515{
2516 struct ab8500_fg_sysfs_entry *entry;
2517 struct ab8500_fg *di;
2518
2519 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2520 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2521
2522 if (!entry->show)
2523 return -EIO;
2524
2525 return entry->show(di, buf);
2526}
2527static ssize_t
2528ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2529 size_t count)
2530{
2531 struct ab8500_fg_sysfs_entry *entry;
2532 struct ab8500_fg *di;
2533
2534 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2535 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2536
2537 if (!entry->store)
2538 return -EIO;
2539
2540 return entry->store(di, buf, count);
2541}
2542
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002543static const struct sysfs_ops ab8500_fg_sysfs_ops = {
Arun Murthy13151632012-02-29 21:54:27 +05302544 .show = ab8500_fg_show,
2545 .store = ab8500_fg_store,
2546};
2547
2548static struct attribute *ab8500_fg_attrs[] = {
2549 &charge_full_attr.attr,
2550 &charge_now_attr.attr,
2551 NULL,
2552};
2553
2554static struct kobj_type ab8500_fg_ktype = {
2555 .sysfs_ops = &ab8500_fg_sysfs_ops,
2556 .default_attrs = ab8500_fg_attrs,
2557};
2558
2559/**
2560 * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
2561 * @di: pointer to the struct ab8500_chargalg
2562 *
2563 * This function removes the entry in sysfs.
2564 */
2565static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2566{
2567 kobject_del(&di->fg_kobject);
2568}
2569
2570/**
2571 * ab8500_chargalg_sysfs_init() - init of sysfs entry
2572 * @di: pointer to the struct ab8500_chargalg
2573 *
2574 * This function adds an entry in sysfs.
2575 * Returns error code in case of failure else 0(on success)
2576 */
2577static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2578{
2579 int ret = 0;
2580
2581 ret = kobject_init_and_add(&di->fg_kobject,
2582 &ab8500_fg_ktype,
2583 NULL, "battery");
2584 if (ret < 0)
2585 dev_err(di->dev, "failed to create sysfs entry\n");
2586
2587 return ret;
2588}
Lee Jones93ff7222012-05-31 16:16:36 +02002589
2590static ssize_t ab8505_powercut_flagtime_read(struct device *dev,
2591 struct device_attribute *attr,
2592 char *buf)
2593{
2594 int ret;
2595 u8 reg_value;
2596 struct power_supply *psy = dev_get_drvdata(dev);
2597 struct ab8500_fg *di;
2598
2599 di = to_ab8500_fg_device_info(psy);
2600
2601 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2602 AB8505_RTC_PCUT_FLAG_TIME_REG, &reg_value);
2603
2604 if (ret < 0) {
2605 dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2606 goto fail;
2607 }
2608
2609 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2610
2611fail:
2612 return ret;
2613}
2614
2615static ssize_t ab8505_powercut_flagtime_write(struct device *dev,
2616 struct device_attribute *attr,
2617 const char *buf, size_t count)
2618{
2619 int ret;
2620 long unsigned reg_value;
2621 struct power_supply *psy = dev_get_drvdata(dev);
2622 struct ab8500_fg *di;
2623
2624 di = to_ab8500_fg_device_info(psy);
2625
2626 reg_value = simple_strtoul(buf, NULL, 10);
2627
2628 if (reg_value > 0x7F) {
2629 dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2630 goto fail;
2631 }
2632
2633 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2634 AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value);
2635
2636 if (ret < 0)
2637 dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2638
2639fail:
2640 return count;
2641}
2642
2643static ssize_t ab8505_powercut_maxtime_read(struct device *dev,
2644 struct device_attribute *attr,
2645 char *buf)
2646{
2647 int ret;
2648 u8 reg_value;
2649 struct power_supply *psy = dev_get_drvdata(dev);
2650 struct ab8500_fg *di;
2651
2652 di = to_ab8500_fg_device_info(psy);
2653
2654 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2655 AB8505_RTC_PCUT_MAX_TIME_REG, &reg_value);
2656
2657 if (ret < 0) {
2658 dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2659 goto fail;
2660 }
2661
2662 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2663
2664fail:
2665 return ret;
2666
2667}
2668
2669static ssize_t ab8505_powercut_maxtime_write(struct device *dev,
2670 struct device_attribute *attr,
2671 const char *buf, size_t count)
2672{
2673 int ret;
2674 int reg_value;
2675 struct power_supply *psy = dev_get_drvdata(dev);
2676 struct ab8500_fg *di;
2677
2678 di = to_ab8500_fg_device_info(psy);
2679
2680 reg_value = simple_strtoul(buf, NULL, 10);
2681 if (reg_value > 0x7F) {
2682 dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2683 goto fail;
2684 }
2685
2686 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2687 AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value);
2688
2689 if (ret < 0)
2690 dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2691
2692fail:
2693 return count;
2694}
2695
2696static ssize_t ab8505_powercut_restart_read(struct device *dev,
2697 struct device_attribute *attr,
2698 char *buf)
2699{
2700 int ret;
2701 u8 reg_value;
2702 struct power_supply *psy = dev_get_drvdata(dev);
2703 struct ab8500_fg *di;
2704
2705 di = to_ab8500_fg_device_info(psy);
2706
2707 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2708 AB8505_RTC_PCUT_RESTART_REG, &reg_value);
2709
2710 if (ret < 0) {
2711 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2712 goto fail;
2713 }
2714
2715 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF));
2716
2717fail:
2718 return ret;
2719}
2720
2721static ssize_t ab8505_powercut_restart_write(struct device *dev,
2722 struct device_attribute *attr,
2723 const char *buf, size_t count)
2724{
2725 int ret;
2726 int reg_value;
2727 struct power_supply *psy = dev_get_drvdata(dev);
2728 struct ab8500_fg *di;
2729
2730 di = to_ab8500_fg_device_info(psy);
2731
2732 reg_value = simple_strtoul(buf, NULL, 10);
2733 if (reg_value > 0xF) {
2734 dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2735 goto fail;
2736 }
2737
2738 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2739 AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value);
2740
2741 if (ret < 0)
2742 dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2743
2744fail:
2745 return count;
2746
2747}
2748
2749static ssize_t ab8505_powercut_timer_read(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
2753 int ret;
2754 u8 reg_value;
2755 struct power_supply *psy = dev_get_drvdata(dev);
2756 struct ab8500_fg *di;
2757
2758 di = to_ab8500_fg_device_info(psy);
2759
2760 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2761 AB8505_RTC_PCUT_TIME_REG, &reg_value);
2762
2763 if (ret < 0) {
2764 dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2765 goto fail;
2766 }
2767
2768 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2769
2770fail:
2771 return ret;
2772}
2773
2774static ssize_t ab8505_powercut_restart_counter_read(struct device *dev,
2775 struct device_attribute *attr,
2776 char *buf)
2777{
2778 int ret;
2779 u8 reg_value;
2780 struct power_supply *psy = dev_get_drvdata(dev);
2781 struct ab8500_fg *di;
2782
2783 di = to_ab8500_fg_device_info(psy);
2784
2785 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2786 AB8505_RTC_PCUT_RESTART_REG, &reg_value);
2787
2788 if (ret < 0) {
2789 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2790 goto fail;
2791 }
2792
2793 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF0) >> 4);
2794
2795fail:
2796 return ret;
2797}
2798
2799static ssize_t ab8505_powercut_read(struct device *dev,
2800 struct device_attribute *attr,
2801 char *buf)
2802{
2803 int ret;
2804 u8 reg_value;
2805 struct power_supply *psy = dev_get_drvdata(dev);
2806 struct ab8500_fg *di;
2807
2808 di = to_ab8500_fg_device_info(psy);
2809
2810 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2811 AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
2812
2813 if (ret < 0)
2814 goto fail;
2815
2816 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x1));
2817
2818fail:
2819 return ret;
2820}
2821
2822static ssize_t ab8505_powercut_write(struct device *dev,
2823 struct device_attribute *attr,
2824 const char *buf, size_t count)
2825{
2826 int ret;
2827 int reg_value;
2828 struct power_supply *psy = dev_get_drvdata(dev);
2829 struct ab8500_fg *di;
2830
2831 di = to_ab8500_fg_device_info(psy);
2832
2833 reg_value = simple_strtoul(buf, NULL, 10);
2834 if (reg_value > 0x1) {
2835 dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2836 goto fail;
2837 }
2838
2839 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2840 AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value);
2841
2842 if (ret < 0)
2843 dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2844
2845fail:
2846 return count;
2847}
2848
2849static ssize_t ab8505_powercut_flag_read(struct device *dev,
2850 struct device_attribute *attr,
2851 char *buf)
2852{
2853
2854 int ret;
2855 u8 reg_value;
2856 struct power_supply *psy = dev_get_drvdata(dev);
2857 struct ab8500_fg *di;
2858
2859 di = to_ab8500_fg_device_info(psy);
2860
2861 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2862 AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
2863
2864 if (ret < 0) {
2865 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2866 goto fail;
2867 }
2868
2869 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x10) >> 4));
2870
2871fail:
2872 return ret;
2873}
2874
2875static ssize_t ab8505_powercut_debounce_read(struct device *dev,
2876 struct device_attribute *attr,
2877 char *buf)
2878{
2879 int ret;
2880 u8 reg_value;
2881 struct power_supply *psy = dev_get_drvdata(dev);
2882 struct ab8500_fg *di;
2883
2884 di = to_ab8500_fg_device_info(psy);
2885
2886 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2887 AB8505_RTC_PCUT_DEBOUNCE_REG, &reg_value);
2888
2889 if (ret < 0) {
2890 dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2891 goto fail;
2892 }
2893
2894 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7));
2895
2896fail:
2897 return ret;
2898}
2899
2900static ssize_t ab8505_powercut_debounce_write(struct device *dev,
2901 struct device_attribute *attr,
2902 const char *buf, size_t count)
2903{
2904 int ret;
2905 int reg_value;
2906 struct power_supply *psy = dev_get_drvdata(dev);
2907 struct ab8500_fg *di;
2908
2909 di = to_ab8500_fg_device_info(psy);
2910
2911 reg_value = simple_strtoul(buf, NULL, 10);
2912 if (reg_value > 0x7) {
2913 dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2914 goto fail;
2915 }
2916
2917 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2918 AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value);
2919
2920 if (ret < 0)
2921 dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2922
2923fail:
2924 return count;
2925}
2926
2927static ssize_t ab8505_powercut_enable_status_read(struct device *dev,
2928 struct device_attribute *attr,
2929 char *buf)
2930{
2931 int ret;
2932 u8 reg_value;
2933 struct power_supply *psy = dev_get_drvdata(dev);
2934 struct ab8500_fg *di;
2935
2936 di = to_ab8500_fg_device_info(psy);
2937
2938 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2939 AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
2940
2941 if (ret < 0) {
2942 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2943 goto fail;
2944 }
2945
2946 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x20) >> 5));
2947
2948fail:
2949 return ret;
2950}
2951
2952static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = {
2953 __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2954 ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write),
2955 __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2956 ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write),
2957 __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP),
2958 ab8505_powercut_restart_read, ab8505_powercut_restart_write),
2959 __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL),
2960 __ATTR(powercut_restart_counter, S_IRUGO,
2961 ab8505_powercut_restart_counter_read, NULL),
2962 __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
2963 ab8505_powercut_read, ab8505_powercut_write),
2964 __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL),
2965 __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP),
2966 ab8505_powercut_debounce_read, ab8505_powercut_debounce_write),
2967 __ATTR(powercut_enable_status, S_IRUGO,
2968 ab8505_powercut_enable_status_read, NULL),
2969};
2970
2971static int ab8500_fg_sysfs_psy_create_attrs(struct device *dev)
2972{
2973 unsigned int i, j;
2974 struct power_supply *psy = dev_get_drvdata(dev);
2975 struct ab8500_fg *di;
2976
2977 di = to_ab8500_fg_device_info(psy);
2978
2979 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
2980 abx500_get_chip_id(dev->parent) >= AB8500_CUT2P0)
2981 || is_ab8540(di->parent)) {
2982 for (j = 0; j < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); j++)
2983 if (device_create_file(dev, &ab8505_fg_sysfs_psy_attrs[j]))
2984 goto sysfs_psy_create_attrs_failed_ab8505;
2985 }
2986 return 0;
2987sysfs_psy_create_attrs_failed_ab8505:
2988 dev_err(dev, "Failed creating sysfs psy attrs for ab8505.\n");
2989 while (j--)
2990 device_remove_file(dev, &ab8505_fg_sysfs_psy_attrs[i]);
2991
2992 return -EIO;
2993}
2994
2995static void ab8500_fg_sysfs_psy_remove_attrs(struct device *dev)
2996{
2997 unsigned int i;
2998 struct power_supply *psy = dev_get_drvdata(dev);
2999 struct ab8500_fg *di;
3000
3001 di = to_ab8500_fg_device_info(psy);
3002
3003 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
3004 abx500_get_chip_id(dev->parent) >= AB8500_CUT2P0)
3005 || is_ab8540(di->parent)) {
3006 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
3007 (void)device_remove_file(dev, &ab8505_fg_sysfs_psy_attrs[i]);
3008 }
3009}
3010
Arun Murthy13151632012-02-29 21:54:27 +05303011/* Exposure to the sysfs interface <<END>> */
3012
3013#if defined(CONFIG_PM)
3014static int ab8500_fg_resume(struct platform_device *pdev)
3015{
3016 struct ab8500_fg *di = platform_get_drvdata(pdev);
3017
3018 /*
3019 * Change state if we're not charging. If we're charging we will wake
3020 * up on the FG IRQ
3021 */
3022 if (!di->flags.charging) {
3023 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
3024 queue_work(di->fg_wq, &di->fg_work);
3025 }
3026
3027 return 0;
3028}
3029
3030static int ab8500_fg_suspend(struct platform_device *pdev,
3031 pm_message_t state)
3032{
3033 struct ab8500_fg *di = platform_get_drvdata(pdev);
3034
3035 flush_delayed_work(&di->fg_periodic_work);
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003036 flush_work(&di->fg_work);
3037 flush_work(&di->fg_acc_cur_work);
3038 flush_delayed_work(&di->fg_reinit_work);
3039 flush_delayed_work(&di->fg_low_bat_work);
3040 flush_delayed_work(&di->fg_check_hw_failure_work);
Arun Murthy13151632012-02-29 21:54:27 +05303041
3042 /*
3043 * If the FG is enabled we will disable it before going to suspend
3044 * only if we're not charging
3045 */
3046 if (di->flags.fg_enabled && !di->flags.charging)
3047 ab8500_fg_coulomb_counter(di, false);
3048
3049 return 0;
3050}
3051#else
3052#define ab8500_fg_suspend NULL
3053#define ab8500_fg_resume NULL
3054#endif
3055
Bill Pemberton415ec692012-11-19 13:26:07 -05003056static int ab8500_fg_remove(struct platform_device *pdev)
Arun Murthy13151632012-02-29 21:54:27 +05303057{
3058 int ret = 0;
3059 struct ab8500_fg *di = platform_get_drvdata(pdev);
3060
3061 list_del(&di->node);
3062
3063 /* Disable coulomb counter */
3064 ret = ab8500_fg_coulomb_counter(di, false);
3065 if (ret)
3066 dev_err(di->dev, "failed to disable coulomb counter\n");
3067
3068 destroy_workqueue(di->fg_wq);
3069 ab8500_fg_sysfs_exit(di);
3070
3071 flush_scheduled_work();
Lee Jones93ff7222012-05-31 16:16:36 +02003072 ab8500_fg_sysfs_psy_remove_attrs(di->fg_psy.dev);
Arun Murthy13151632012-02-29 21:54:27 +05303073 power_supply_unregister(&di->fg_psy);
3074 platform_set_drvdata(pdev, NULL);
Arun Murthy13151632012-02-29 21:54:27 +05303075 return ret;
3076}
3077
3078/* ab8500 fg driver interrupts and their respective isr */
3079static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
3080 {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
3081 {"BATT_OVV", ab8500_fg_batt_ovv_handler},
3082 {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
3083 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
3084 {"CCEOC", ab8500_fg_cc_data_end_handler},
3085};
3086
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003087static char *supply_interface[] = {
3088 "ab8500_chargalg",
3089 "ab8500_usb",
3090};
3091
Bill Pembertonc8afa642012-11-19 13:22:23 -05003092static int ab8500_fg_probe(struct platform_device *pdev)
Arun Murthy13151632012-02-29 21:54:27 +05303093{
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003094 struct device_node *np = pdev->dev.of_node;
Lee Jones195c1c62012-11-30 10:56:51 +00003095 struct abx500_bm_data *plat = pdev->dev.platform_data;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003096 struct ab8500_fg *di;
Arun Murthy13151632012-02-29 21:54:27 +05303097 int i, irq;
3098 int ret = 0;
Arun Murthy13151632012-02-29 21:54:27 +05303099
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003100 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3101 if (!di) {
3102 dev_err(&pdev->dev, "%s no mem for ab8500_fg\n", __func__);
Arun Murthy13151632012-02-29 21:54:27 +05303103 return -ENOMEM;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003104 }
Lee Jones195c1c62012-11-30 10:56:51 +00003105
3106 if (!plat) {
3107 dev_err(&pdev->dev, "no battery management data supplied\n");
3108 return -EINVAL;
3109 }
3110 di->bm = plat;
3111
3112 if (np) {
3113 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3114 if (ret) {
3115 dev_err(&pdev->dev, "failed to get battery information\n");
3116 return ret;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003117 }
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003118 }
Arun Murthy13151632012-02-29 21:54:27 +05303119
3120 mutex_init(&di->cc_lock);
3121
3122 /* get parent data */
3123 di->dev = &pdev->dev;
3124 di->parent = dev_get_drvdata(pdev->dev.parent);
3125 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3126
Arun Murthy13151632012-02-29 21:54:27 +05303127 di->fg_psy.name = "ab8500_fg";
3128 di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
3129 di->fg_psy.properties = ab8500_fg_props;
3130 di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
3131 di->fg_psy.get_property = ab8500_fg_get_property;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003132 di->fg_psy.supplied_to = supply_interface;
3133 di->fg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy13151632012-02-29 21:54:27 +05303134 di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
3135
3136 di->bat_cap.max_mah_design = MILLI_TO_MICRO *
Lee Jonesb0284de2012-11-30 10:09:42 +00003137 di->bm->bat_type[di->bm->batt_id].charge_full_design;
Arun Murthy13151632012-02-29 21:54:27 +05303138
3139 di->bat_cap.max_mah = di->bat_cap.max_mah_design;
3140
Lee Jonesb0284de2012-11-30 10:09:42 +00003141 di->vbat_nom = di->bm->bat_type[di->bm->batt_id].nominal_voltage;
Arun Murthy13151632012-02-29 21:54:27 +05303142
3143 di->init_capacity = true;
3144
3145 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
3146 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
3147
3148 /* Create a work queue for running the FG algorithm */
3149 di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
3150 if (di->fg_wq == NULL) {
3151 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003152 return -ENOMEM;
Arun Murthy13151632012-02-29 21:54:27 +05303153 }
3154
3155 /* Init work for running the fg algorithm instantly */
3156 INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
3157
3158 /* Init work for getting the battery accumulated current */
3159 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
3160
3161 /* Init work for reinitialising the fg algorithm */
Tejun Heo203b42f2012-08-21 13:18:23 -07003162 INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
Arun Murthy13151632012-02-29 21:54:27 +05303163 ab8500_fg_reinit_work);
3164
3165 /* Work delayed Queue to run the state machine */
Tejun Heo203b42f2012-08-21 13:18:23 -07003166 INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
Arun Murthy13151632012-02-29 21:54:27 +05303167 ab8500_fg_periodic_work);
3168
3169 /* Work to check low battery condition */
Tejun Heo203b42f2012-08-21 13:18:23 -07003170 INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
Arun Murthy13151632012-02-29 21:54:27 +05303171 ab8500_fg_low_bat_work);
3172
3173 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003174 INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
Arun Murthy13151632012-02-29 21:54:27 +05303175 ab8500_fg_check_hw_failure_work);
3176
Hakan Berg75f2a212012-05-10 08:43:25 +02003177 /* Reset battery low voltage flag */
3178 di->flags.low_bat = false;
3179
3180 /* Initialize low battery counter */
3181 di->low_bat_cnt = 10;
3182
Arun Murthy13151632012-02-29 21:54:27 +05303183 /* Initialize OVV, and other registers */
3184 ret = ab8500_fg_init_hw_registers(di);
3185 if (ret) {
3186 dev_err(di->dev, "failed to initialize registers\n");
3187 goto free_inst_curr_wq;
3188 }
3189
3190 /* Consider battery unknown until we're informed otherwise */
3191 di->flags.batt_unknown = true;
3192 di->flags.batt_id_received = false;
3193
3194 /* Register FG power supply class */
3195 ret = power_supply_register(di->dev, &di->fg_psy);
3196 if (ret) {
3197 dev_err(di->dev, "failed to register FG psy\n");
3198 goto free_inst_curr_wq;
3199 }
3200
Lee Jonesb0284de2012-11-30 10:09:42 +00003201 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
Arun Murthy13151632012-02-29 21:54:27 +05303202 ab8500_fg_coulomb_counter(di, true);
3203
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +00003204 /*
3205 * Initialize completion used to notify completion and start
3206 * of inst current
3207 */
3208 init_completion(&di->ab8500_fg_started);
Arun Murthy13151632012-02-29 21:54:27 +05303209 init_completion(&di->ab8500_fg_complete);
3210
3211 /* Register interrupts */
3212 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
3213 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
3214 ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
3215 IRQF_SHARED | IRQF_NO_SUSPEND,
3216 ab8500_fg_irq[i].name, di);
3217
3218 if (ret != 0) {
3219 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3220 , ab8500_fg_irq[i].name, irq, ret);
3221 goto free_irq;
3222 }
3223 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3224 ab8500_fg_irq[i].name, irq, ret);
3225 }
3226 di->irq = platform_get_irq_byname(pdev, "CCEOC");
3227 disable_irq(di->irq);
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +00003228 di->nbr_cceoc_irq_cnt = 0;
Arun Murthy13151632012-02-29 21:54:27 +05303229
3230 platform_set_drvdata(pdev, di);
3231
3232 ret = ab8500_fg_sysfs_init(di);
3233 if (ret) {
3234 dev_err(di->dev, "failed to create sysfs entry\n");
3235 goto free_irq;
3236 }
3237
Lee Jones93ff7222012-05-31 16:16:36 +02003238 ret = ab8500_fg_sysfs_psy_create_attrs(di->fg_psy.dev);
3239 if (ret) {
3240 dev_err(di->dev, "failed to create FG psy\n");
3241 ab8500_fg_sysfs_exit(di);
3242 goto free_irq;
3243 }
3244
Arun Murthy13151632012-02-29 21:54:27 +05303245 /* Calibrate the fg first time */
3246 di->flags.calibrate = true;
3247 di->calib_state = AB8500_FG_CALIB_INIT;
3248
3249 /* Use room temp as default value until we get an update from driver. */
3250 di->bat_temp = 210;
3251
3252 /* Run the FG algorithm */
3253 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
3254
3255 list_add_tail(&di->node, &ab8500_fg_list);
3256
3257 return ret;
3258
3259free_irq:
3260 power_supply_unregister(&di->fg_psy);
3261
3262 /* We also have to free all successfully registered irqs */
3263 for (i = i - 1; i >= 0; i--) {
3264 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
3265 free_irq(irq, di);
3266 }
3267free_inst_curr_wq:
3268 destroy_workqueue(di->fg_wq);
Arun Murthy13151632012-02-29 21:54:27 +05303269 return ret;
3270}
3271
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003272static const struct of_device_id ab8500_fg_match[] = {
3273 { .compatible = "stericsson,ab8500-fg", },
3274 { },
3275};
3276
Arun Murthy13151632012-02-29 21:54:27 +05303277static struct platform_driver ab8500_fg_driver = {
3278 .probe = ab8500_fg_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003279 .remove = ab8500_fg_remove,
Arun Murthy13151632012-02-29 21:54:27 +05303280 .suspend = ab8500_fg_suspend,
3281 .resume = ab8500_fg_resume,
3282 .driver = {
3283 .name = "ab8500-fg",
3284 .owner = THIS_MODULE,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08003285 .of_match_table = ab8500_fg_match,
Arun Murthy13151632012-02-29 21:54:27 +05303286 },
3287};
3288
3289static int __init ab8500_fg_init(void)
3290{
3291 return platform_driver_register(&ab8500_fg_driver);
3292}
3293
3294static void __exit ab8500_fg_exit(void)
3295{
3296 platform_driver_unregister(&ab8500_fg_driver);
3297}
3298
3299subsys_initcall_sync(ab8500_fg_init);
3300module_exit(ab8500_fg_exit);
3301
3302MODULE_LICENSE("GPL v2");
3303MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
3304MODULE_ALIAS("platform:ab8500-fg");
3305MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");