blob: 79ed2b164787081f685d1991c4fe09fd51fbfe87 [file] [log] [blame]
Eugene Yasmand72132d2013-01-20 13:00:52 +02001/* Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
Amir Samuelov02df2322012-10-25 10:28:07 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * High Level description:
16 * http://www.ti.com/lit/ds/symlink/bq28400.pdf
17 * Thechnical Reference:
18 * http://www.ti.com/lit/ug/sluu431/sluu431.pdf
19 */
20
21#define pr_fmt(fmt) "%s: " fmt, __func__
22
23#include <linux/i2c.h>
24#include <linux/gpio.h>
25#include <linux/errno.h>
26#include <linux/delay.h>
27#include <linux/module.h>
28#include <linux/debugfs.h>
29#include <linux/workqueue.h>
30#include <linux/interrupt.h>
31#include <linux/slab.h>
32#include <linux/power_supply.h>
33#include <linux/bitops.h>
34#include <linux/regulator/consumer.h>
35#include <linux/printk.h>
36
37#define BQ28400_NAME "bq28400"
38#define BQ28400_REV "1.0"
39
40/* SBS Commands (page 63) */
41
42#define SBS_MANUFACTURER_ACCESS 0x00
43#define SBS_BATTERY_MODE 0x03
44#define SBS_TEMPERATURE 0x08
45#define SBS_VOLTAGE 0x09
46#define SBS_CURRENT 0x0A
47#define SBS_AVG_CURRENT 0x0B
48#define SBS_MAX_ERROR 0x0C
49#define SBS_RSOC 0x0D /* Relative State Of Charge */
50#define SBS_REMAIN_CAPACITY 0x0F
51#define SBS_FULL_CAPACITY 0x10
52#define SBS_CHG_CURRENT 0x14
53#define SBS_CHG_VOLTAGE 0x15
54#define SBS_BATTERY_STATUS 0x16
55#define SBS_CYCLE_COUNT 0x17
56#define SBS_DESIGN_CAPACITY 0x18
57#define SBS_DESIGN_VOLTAGE 0x19
58#define SBS_SPEC_INFO 0x1A
59#define SBS_MANUFACTURE_DATE 0x1B
60#define SBS_SERIAL_NUMBER 0x1C
61#define SBS_MANUFACTURER_NAME 0x20
62#define SBS_DEVICE_NAME 0x21
63#define SBS_DEVICE_CHEMISTRY 0x22
64#define SBS_MANUFACTURER_DATA 0x23
65#define SBS_AUTHENTICATE 0x2F
66#define SBS_CELL_VOLTAGE1 0x3E
67#define SBS_CELL_VOLTAGE2 0x3F
68
69/* Extended SBS Commands (page 71) */
70
71#define SBS_FET_CONTROL 0x46
72#define SBS_SAFETY_ALERT 0x50
73#define SBS_SAFETY_STATUS 0x51
74#define SBS_PE_ALERT 0x52
75#define SBS_PE_STATUS 0x53
76#define SBS_OPERATION_STATUS 0x54
77#define SBS_CHARGING_STATUS 0x55
78#define SBS_FET_STATUS 0x56
79#define SBS_PACK_VOLTAGE 0x5A
80#define SBS_TS0_TEMPERATURE 0x5E
81#define SBS_FULL_ACCESS_KEY 0x61
82#define SBS_PF_KEY 0x62
83#define SBS_AUTH_KEY3 0x63
84#define SBS_AUTH_KEY2 0x64
85#define SBS_AUTH_KEY1 0x65
86#define SBS_AUTH_KEY0 0x66
87#define SBS_MANUFACTURER_INFO 0x70
88#define SBS_SENSE_RESISTOR 0x71
89#define SBS_TEMP_RANGE 0x72
90
91/* SBS Sub-Commands (16 bits) */
92/* SBS_MANUFACTURER_ACCESS CMD */
93#define SUBCMD_DEVICE_TYPE 0x01
94#define SUBCMD_FIRMWARE_VERSION 0x02
95#define SUBCMD_HARDWARE_VERSION 0x03
96#define SUBCMD_DF_CHECKSUM 0x04
97#define SUBCMD_EDV 0x05
98#define SUBCMD_CHEMISTRY_ID 0x08
99
100/* SBS_CHARGING_STATUS */
101#define CHG_STATUS_BATTERY_DEPLETED BIT(0)
102#define CHG_STATUS_OVERCHARGE BIT(1)
103#define CHG_STATUS_OVERCHARGE_CURRENT BIT(2)
104#define CHG_STATUS_OVERCHARGE_VOLTAGE BIT(3)
105#define CHG_STATUS_CELL_BALANCING BIT(6)
106#define CHG_STATUS_HOT_TEMP_CHARGING BIT(8)
107#define CHG_STATUS_STD1_TEMP_CHARGING BIT(9)
108#define CHG_STATUS_STD2_TEMP_CHARGING BIT(10)
109#define CHG_STATUS_LOW_TEMP_CHARGING BIT(11)
110#define CHG_STATUS_PRECHARGING_EXIT BIT(13)
111#define CHG_STATUS_SUSPENDED BIT(14)
112#define CHG_STATUS_DISABLED BIT(15)
113
114/* SBS_FET_STATUS */
115#define FET_STATUS_DISCHARGE BIT(1)
116#define FET_STATUS_CHARGE BIT(2)
117#define FET_STATUS_PRECHARGE BIT(3)
118
119/* SBS_BATTERY_STATUS */
120#define BAT_STATUS_SBS_ERROR 0x0F
121#define BAT_STATUS_EMPTY BIT(4)
122#define BAT_STATUS_FULL BIT(5)
123#define BAT_STATUS_DISCHARGING BIT(6)
124#define BAT_STATUS_OVER_TEMPERATURE BIT(12)
125#define BAT_STATUS_OVER_CHARGED BIT(15)
126
127#define ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN (-2731)
128#define BQ_TERMINATION_CURRENT_MA 200
129
130#define BQ_MAX_STR_LEN 32
131
132struct bq28400_device {
133 struct i2c_client *client;
134 struct delayed_work periodic_user_space_update_work;
135 struct dentry *dent;
136 struct power_supply batt_psy;
137 struct power_supply *dc_psy;
138 bool is_charging_enabled;
Eugene Yasmand72132d2013-01-20 13:00:52 +0200139 u32 temp_cold; /* in degree celsius */
140 u32 temp_hot; /* in degree celsius */
Amir Samuelov02df2322012-10-25 10:28:07 +0200141};
142
143static struct bq28400_device *bq28400_dev;
144
145static enum power_supply_property pm_power_props[] = {
146 POWER_SUPPLY_PROP_STATUS,
147 POWER_SUPPLY_PROP_CHARGE_TYPE,
148 POWER_SUPPLY_PROP_PRESENT,
149 POWER_SUPPLY_PROP_TECHNOLOGY,
150 POWER_SUPPLY_PROP_VOLTAGE_NOW,
151 POWER_SUPPLY_PROP_CAPACITY,
152 POWER_SUPPLY_PROP_CURRENT_NOW,
153 POWER_SUPPLY_PROP_CURRENT_AVG,
154 POWER_SUPPLY_PROP_TEMP,
155 POWER_SUPPLY_PROP_CHARGE_FULL,
156 POWER_SUPPLY_PROP_CHARGE_NOW,
157 POWER_SUPPLY_PROP_MODEL_NAME,
158 POWER_SUPPLY_PROP_MANUFACTURER,
159};
160
161struct debug_reg {
162 char *name;
163 u8 reg;
164 u16 subcmd;
165};
166
Anirudh Ghayal9af716e2013-04-25 16:33:34 +0530167static int fake_battery = -EINVAL;
168module_param(fake_battery, int, 0644);
169
Amir Samuelov02df2322012-10-25 10:28:07 +0200170#define BQ28400_DEBUG_REG(x) {#x, SBS_##x, 0}
171#define BQ28400_DEBUG_SUBREG(x, y) {#y, SBS_##x, SUBCMD_##y}
172
173/* Note: Some register can be read only in Unsealed mode */
174static struct debug_reg bq28400_debug_regs[] = {
175 BQ28400_DEBUG_REG(MANUFACTURER_ACCESS),
176 BQ28400_DEBUG_REG(BATTERY_MODE),
177 BQ28400_DEBUG_REG(TEMPERATURE),
178 BQ28400_DEBUG_REG(VOLTAGE),
179 BQ28400_DEBUG_REG(CURRENT),
180 BQ28400_DEBUG_REG(AVG_CURRENT),
181 BQ28400_DEBUG_REG(MAX_ERROR),
182 BQ28400_DEBUG_REG(RSOC),
183 BQ28400_DEBUG_REG(REMAIN_CAPACITY),
184 BQ28400_DEBUG_REG(FULL_CAPACITY),
185 BQ28400_DEBUG_REG(CHG_CURRENT),
186 BQ28400_DEBUG_REG(CHG_VOLTAGE),
187 BQ28400_DEBUG_REG(BATTERY_STATUS),
188 BQ28400_DEBUG_REG(CYCLE_COUNT),
189 BQ28400_DEBUG_REG(DESIGN_CAPACITY),
190 BQ28400_DEBUG_REG(DESIGN_VOLTAGE),
191 BQ28400_DEBUG_REG(SPEC_INFO),
192 BQ28400_DEBUG_REG(MANUFACTURE_DATE),
193 BQ28400_DEBUG_REG(SERIAL_NUMBER),
194 BQ28400_DEBUG_REG(MANUFACTURER_NAME),
195 BQ28400_DEBUG_REG(DEVICE_NAME),
196 BQ28400_DEBUG_REG(DEVICE_CHEMISTRY),
197 BQ28400_DEBUG_REG(MANUFACTURER_DATA),
198 BQ28400_DEBUG_REG(AUTHENTICATE),
199 BQ28400_DEBUG_REG(CELL_VOLTAGE1),
200 BQ28400_DEBUG_REG(CELL_VOLTAGE2),
201 BQ28400_DEBUG_REG(SAFETY_ALERT),
202 BQ28400_DEBUG_REG(SAFETY_STATUS),
203 BQ28400_DEBUG_REG(PE_ALERT),
204 BQ28400_DEBUG_REG(PE_STATUS),
205 BQ28400_DEBUG_REG(OPERATION_STATUS),
206 BQ28400_DEBUG_REG(CHARGING_STATUS),
207 BQ28400_DEBUG_REG(FET_STATUS),
208 BQ28400_DEBUG_REG(FULL_ACCESS_KEY),
209 BQ28400_DEBUG_REG(PF_KEY),
210 BQ28400_DEBUG_REG(MANUFACTURER_INFO),
211 BQ28400_DEBUG_REG(SENSE_RESISTOR),
212 BQ28400_DEBUG_REG(TEMP_RANGE),
213 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, DEVICE_TYPE),
214 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, FIRMWARE_VERSION),
215 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, HARDWARE_VERSION),
216 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, DF_CHECKSUM),
217 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, EDV),
218 BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, CHEMISTRY_ID),
219};
220
221static int bq28400_read_reg(struct i2c_client *client, u8 reg)
222{
223 int val;
224
225 val = i2c_smbus_read_word_data(client, reg);
226 if (val < 0)
227 pr_err("i2c read fail. reg = 0x%x.ret = %d.\n", reg, val);
228 else
229 pr_debug("reg = 0x%02X.val = 0x%04X.\n", reg , val);
230
231 return val;
232}
233
234static int bq28400_write_reg(struct i2c_client *client, u8 reg, u16 val)
235{
236 int ret;
237
238 ret = i2c_smbus_write_word_data(client, reg, val);
239 if (ret < 0)
240 pr_err("i2c read fail. reg = 0x%x.val = 0x%x.ret = %d.\n",
241 reg, val, ret);
242 else
243 pr_debug("reg = 0x%02X.val = 0x%02X.\n", reg , val);
244
245 return ret;
246}
247
248static int bq28400_read_subcmd(struct i2c_client *client, u8 reg, u16 subcmd)
249{
250 int ret;
251 u8 buf[4];
252 u16 val = 0;
253
254 buf[0] = reg;
255 buf[1] = subcmd & 0xFF;
256 buf[2] = (subcmd >> 8) & 0xFF;
257
258 /* Control sub-command */
259 ret = i2c_master_send(client, buf, 3);
260 if (ret < 0) {
261 pr_err("i2c tx fail. reg = 0x%x.ret = %d.\n", reg, ret);
262 return ret;
263 }
264 udelay(66);
265
266 /* Read Result of subcmd */
267 ret = i2c_master_send(client, buf, 1);
268 memset(buf, 0xAA, sizeof(buf));
269 ret = i2c_master_recv(client, buf, 2);
270 if (ret < 0) {
271 pr_err("i2c rx fail. reg = 0x%x.ret = %d.\n", reg, ret);
272 return ret;
273 }
274 val = (buf[1] << 8) + buf[0];
275
276 pr_debug("reg = 0x%02X.subcmd = 0x%x.val = 0x%04X.\n",
277 reg , subcmd, val);
278
279 return val;
280}
281
282static int bq28400_read_block(struct i2c_client *client, u8 reg,
283 u8 len, u8 *buf)
284{
285 int ret;
286 u32 val;
287
288 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
289 val = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
290
291 if (ret < 0)
292 pr_err("i2c read fail. reg = 0x%x.ret = %d.\n", reg, ret);
293 else
294 pr_debug("reg = 0x%02X.val = 0x%04X.\n", reg , val);
295
296 return val;
297}
298
299/*
300 * Read a string from a device.
301 * Returns string length on success or error on failure (negative value).
302 */
303static int bq28400_read_string(struct i2c_client *client, u8 reg, char *str,
304 u8 max_len)
305{
306 int ret;
307 int len;
308
309 ret = bq28400_read_block(client, reg, max_len, str);
310 if (ret < 0)
311 return ret;
312
313 len = str[0]; /* Actual length */
314 if (len > max_len - 2) { /* reduce len byte and null */
315 pr_err("len = %d invalid.\n", len);
316 return -EINVAL;
317 }
318
319 memcpy(&str[0], &str[1], len); /* Move sting to the start */
320 str[len] = 0; /* put NULL after actual size */
321
322 pr_debug("len = %d.str = %s.\n", len, str);
323
324 return len;
325}
326
327#define BQ28400_INVALID_TEMPERATURE -999
328/*
329 * Return the battery temperature in tenths of degree Celsius
330 * Or -99.9 C if something fails.
331 */
332static int bq28400_read_temperature(struct i2c_client *client)
333{
334 int temp;
335
336 /* temperature resolution 0.1 Kelvin */
337 temp = bq28400_read_reg(client, SBS_TEMPERATURE);
338 if (temp < 0)
339 return BQ28400_INVALID_TEMPERATURE;
340
341 temp = temp + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
342
343 pr_debug("temp = %d C\n", temp/10);
344
345 return temp;
346}
347
348/*
349 * Return the battery Voltage in milivolts 0..20 V
350 * Or < 0 if something fails.
351 */
352static int bq28400_read_voltage(struct i2c_client *client)
353{
354 int mvolt = 0;
355
356 mvolt = bq28400_read_reg(client, SBS_VOLTAGE);
357 if (mvolt < 0)
358 return mvolt;
359
360 pr_debug("volt = %d mV.\n", mvolt);
361
362 return mvolt;
363}
364
365/*
366 * Return the battery Current in miliamps
367 * Or 0 if something fails.
368 * Positive current indicates charging
369 * Negative current indicates discharging.
370 * Current-now is calculated every second.
371 */
372static int bq28400_read_current(struct i2c_client *client)
373{
374 s16 current_ma = 0;
375
376 current_ma = bq28400_read_reg(client, SBS_CURRENT);
377
378 pr_debug("current = %d mA.\n", current_ma);
379
380 return current_ma;
381}
382
383/*
384 * Return the Average battery Current in miliamps
385 * Or 0 if something fails.
386 * Positive current indicates charging
387 * Negative current indicates discharging.
388 * Average Current is the rolling 1 minute average current.
389 */
390static int bq28400_read_avg_current(struct i2c_client *client)
391{
392 s16 current_ma = 0;
393
394 current_ma = bq28400_read_reg(client, SBS_AVG_CURRENT);
395
396 pr_debug("avg_current=%d mA.\n", current_ma);
397
398 return current_ma;
399}
400
401/*
402 * Return the battery Relative-State-Of-Charge 0..100 %
Amir Samuelov920aa852012-12-11 19:12:21 +0200403 * Or negative value if something fails.
Amir Samuelov02df2322012-10-25 10:28:07 +0200404 */
405static int bq28400_read_rsoc(struct i2c_client *client)
406{
407 int percentage = 0;
408
Anirudh Ghayal9af716e2013-04-25 16:33:34 +0530409 if (fake_battery != -EINVAL) {
410 pr_debug("Reporting Fake SOC = %d\n", fake_battery);
411 return fake_battery;
412 }
413
Amir Samuelov02df2322012-10-25 10:28:07 +0200414 /* This register is only 1 byte */
415 percentage = i2c_smbus_read_byte_data(client, SBS_RSOC);
416
Amir Samuelov920aa852012-12-11 19:12:21 +0200417 if (percentage < 0) {
418 pr_err("I2C failure when reading rsoc.\n");
419 return percentage;
420 }
Amir Samuelov02df2322012-10-25 10:28:07 +0200421
422 pr_debug("percentage = %d.\n", percentage);
423
424 return percentage;
425}
426
427/*
428 * Return the battery Capacity in mAh.
429 * Or 0 if something fails.
430 */
431static int bq28400_read_full_capacity(struct i2c_client *client)
432{
433 int capacity = 0;
434
435 capacity = bq28400_read_reg(client, SBS_FULL_CAPACITY);
436 if (capacity < 0)
437 return 0;
438
439 pr_debug("full-capacity = %d mAh.\n", capacity);
440
441 return capacity;
442}
443
444/*
445 * Return the battery Capacity in mAh.
446 * Or 0 if something fails.
447 */
448static int bq28400_read_remain_capacity(struct i2c_client *client)
449{
450 int capacity = 0;
451
452 capacity = bq28400_read_reg(client, SBS_REMAIN_CAPACITY);
453 if (capacity < 0)
454 return 0;
455
456 pr_debug("remain-capacity = %d mAh.\n", capacity);
457
458 return capacity;
459}
460
461static int bq28400_enable_charging(struct bq28400_device *bq28400_dev,
462 bool enable)
463{
464 int ret;
465 static bool is_charging_enabled;
466
467 if (bq28400_dev->dc_psy == NULL) {
468 bq28400_dev->dc_psy = power_supply_get_by_name("dc");
469 if (bq28400_dev->dc_psy == NULL) {
470 pr_err("fail to get dc-psy.\n");
471 return -ENODEV;
472 }
473 }
474
475 if (is_charging_enabled == enable) {
476 pr_debug("Charging enable already = %d.\n", enable);
477 return 0;
478 }
479
480 ret = power_supply_set_online(bq28400_dev->dc_psy, enable);
481 if (ret < 0) {
482 pr_err("fail to set dc-psy online to %d.\n", enable);
483 return ret;
484 }
485
486 is_charging_enabled = enable;
487
488 pr_debug("Charging enable = %d.\n", enable);
489
490 return 0;
491}
492
493static int bq28400_get_prop_status(struct i2c_client *client)
494{
495 int status = POWER_SUPPLY_STATUS_UNKNOWN;
496 int rsoc;
497 s16 current_ma = 0;
498 u16 battery_status;
Eugene Yasmand72132d2013-01-20 13:00:52 +0200499 int temperature;
500 struct bq28400_device *dev = i2c_get_clientdata(client);
Amir Samuelov02df2322012-10-25 10:28:07 +0200501
502 battery_status = bq28400_read_reg(client, SBS_BATTERY_STATUS);
Amir Samuelov0f0af182012-11-11 13:39:00 +0200503 rsoc = bq28400_read_rsoc(client);
504 current_ma = bq28400_read_current(client);
Eugene Yasmand72132d2013-01-20 13:00:52 +0200505 temperature = bq28400_read_temperature(client);
506 temperature = temperature / 10; /* in degree celsius */
Amir Samuelov02df2322012-10-25 10:28:07 +0200507
508 if (battery_status & BAT_STATUS_EMPTY)
509 pr_debug("Battery report Empty.\n");
510
511 /* Battery may report FULL before rsoc is 100%
512 * for protection and cell-balancing.
513 * The FULL report may remain when rsoc drops from 100%.
Amir Samuelov0f0af182012-11-11 13:39:00 +0200514 * If battery is full but DC-Jack is removed then report discahrging.
Amir Samuelov02df2322012-10-25 10:28:07 +0200515 */
516 if (battery_status & BAT_STATUS_FULL) {
517 pr_debug("Battery report Full.\n");
518 bq28400_enable_charging(bq28400_dev, false);
Amir Samuelov0f0af182012-11-11 13:39:00 +0200519 if (current_ma < 0)
520 return POWER_SUPPLY_STATUS_DISCHARGING;
Amir Samuelov02df2322012-10-25 10:28:07 +0200521 return POWER_SUPPLY_STATUS_FULL;
522 }
523
Amir Samuelov02df2322012-10-25 10:28:07 +0200524 if (rsoc == 100) {
525 bq28400_enable_charging(bq28400_dev, false);
526 pr_debug("Full.\n");
527 return POWER_SUPPLY_STATUS_FULL;
528 }
529
Eugene Yasmand72132d2013-01-20 13:00:52 +0200530 /* Enable charging when battery is not full and temperature is ok */
531 if ((temperature > dev->temp_cold) && (temperature < dev->temp_hot))
532 bq28400_enable_charging(bq28400_dev, true);
533 else
534 bq28400_enable_charging(bq28400_dev, false);
Amir Samuelov9157d5f2012-11-01 14:05:31 +0200535
Amir Samuelov02df2322012-10-25 10:28:07 +0200536 /*
537 * Positive current indicates charging
538 * Negative current indicates discharging.
539 * Charging is stopped at termination-current.
540 */
541 if (current_ma < 0) {
Amir Samuelov02df2322012-10-25 10:28:07 +0200542 pr_debug("Discharging.\n");
543 status = POWER_SUPPLY_STATUS_DISCHARGING;
544 } else if (current_ma > BQ_TERMINATION_CURRENT_MA) {
545 pr_debug("Charging.\n");
546 status = POWER_SUPPLY_STATUS_CHARGING;
547 } else {
548 pr_debug("Not Charging.\n");
549 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
550 }
551
552 return status;
553}
554
555static int bq28400_get_prop_charge_type(struct i2c_client *client)
556{
557 u16 battery_status;
558 u16 chg_status;
559 u16 fet_status;
560
561 battery_status = bq28400_read_reg(client, SBS_BATTERY_STATUS);
562 chg_status = bq28400_read_reg(client, SBS_CHARGING_STATUS);
563 fet_status = bq28400_read_reg(client, SBS_FET_STATUS);
564
565 if (battery_status & BAT_STATUS_DISCHARGING) {
566 pr_debug("Discharging.\n");
567 return POWER_SUPPLY_CHARGE_TYPE_NONE;
568 }
569
570 if (fet_status & FET_STATUS_PRECHARGE) {
571 pr_debug("Pre-Charging.\n");
572 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
573 }
574
575 if (chg_status & CHG_STATUS_HOT_TEMP_CHARGING) {
576 pr_debug("Hot-Temp-Charging.\n");
577 return POWER_SUPPLY_CHARGE_TYPE_FAST;
578 }
579
580 if (chg_status & CHG_STATUS_LOW_TEMP_CHARGING) {
581 pr_debug("Low-Temp-Charging.\n");
582 return POWER_SUPPLY_CHARGE_TYPE_FAST;
583 }
584
585 if (chg_status & CHG_STATUS_STD1_TEMP_CHARGING) {
586 pr_debug("STD1-Temp-Charging.\n");
587 return POWER_SUPPLY_CHARGE_TYPE_FAST;
588 }
589
590 if (chg_status & CHG_STATUS_STD2_TEMP_CHARGING) {
591 pr_debug("STD2-Temp-Charging.\n");
592 return POWER_SUPPLY_CHARGE_TYPE_FAST;
593 }
594
595 if (chg_status & CHG_STATUS_BATTERY_DEPLETED)
596 pr_debug("battery_depleted.\n");
597
598 if (chg_status & CHG_STATUS_CELL_BALANCING)
599 pr_debug("cell_balancing.\n");
600
601 if (chg_status & CHG_STATUS_OVERCHARGE) {
602 pr_err("overcharge fault.\n");
603 return POWER_SUPPLY_CHARGE_TYPE_NONE;
604 }
605
606 if (chg_status & CHG_STATUS_SUSPENDED) {
607 pr_info("Suspended.\n");
608 return POWER_SUPPLY_CHARGE_TYPE_NONE;
609 }
610
611 if (chg_status & CHG_STATUS_DISABLED) {
612 pr_info("Disabled.\n");
613 return POWER_SUPPLY_CHARGE_TYPE_NONE;
614 }
615
616 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
617}
618
619static bool bq28400_get_prop_present(struct i2c_client *client)
620{
621 int val;
622
623 val = bq28400_read_reg(client, SBS_BATTERY_STATUS);
624
625 /* If the bq28400 is inside the battery pack
626 * then when battery is removed the i2c transfer will fail.
627 */
628
629 if (val < 0)
630 return false;
631
632 /* TODO - support when bq28400 is not embedded in battery pack */
633
634 return true;
635}
636
637/*
638 * User sapce read the battery info.
639 * Get data online via I2C from the battery gauge.
640 */
641static int bq28400_get_property(struct power_supply *psy,
642 enum power_supply_property psp,
643 union power_supply_propval *val)
644{
645 int ret = 0;
646 struct bq28400_device *dev = container_of(psy,
647 struct bq28400_device,
648 batt_psy);
649 struct i2c_client *client = dev->client;
650 static char str[BQ_MAX_STR_LEN];
651
652 switch (psp) {
653 case POWER_SUPPLY_PROP_STATUS:
654 val->intval = bq28400_get_prop_status(client);
655 break;
656 case POWER_SUPPLY_PROP_CHARGE_TYPE:
657 val->intval = bq28400_get_prop_charge_type(client);
658 break;
659 case POWER_SUPPLY_PROP_PRESENT:
660 val->intval = bq28400_get_prop_present(client);
661 break;
662 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
663 val->intval = bq28400_read_voltage(client);
664 val->intval *= 1000; /* mV to uV */
665 break;
666 case POWER_SUPPLY_PROP_CAPACITY:
667 val->intval = bq28400_read_rsoc(client);
Amir Samuelov920aa852012-12-11 19:12:21 +0200668 if (val->intval < 0)
669 ret = -EINVAL;
Amir Samuelov02df2322012-10-25 10:28:07 +0200670 break;
671 case POWER_SUPPLY_PROP_CURRENT_NOW:
672 /* Positive current indicates drawing */
673 val->intval = -bq28400_read_current(client);
674 val->intval *= 1000; /* mA to uA */
675 break;
676 case POWER_SUPPLY_PROP_CURRENT_AVG:
677 /* Positive current indicates drawing */
678 val->intval = -bq28400_read_avg_current(client);
679 val->intval *= 1000; /* mA to uA */
680 break;
681 case POWER_SUPPLY_PROP_TEMP:
682 val->intval = bq28400_read_temperature(client);
683 break;
684 case POWER_SUPPLY_PROP_CHARGE_FULL:
685 val->intval = bq28400_read_full_capacity(client);
686 break;
687 case POWER_SUPPLY_PROP_CHARGE_NOW:
688 val->intval = bq28400_read_remain_capacity(client);
689 break;
690 case POWER_SUPPLY_PROP_TECHNOLOGY:
691 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
692 break;
693 case POWER_SUPPLY_PROP_MODEL_NAME:
694 bq28400_read_string(client, SBS_DEVICE_NAME, str, 20);
695 val->strval = str;
696 break;
697 case POWER_SUPPLY_PROP_MANUFACTURER:
698 bq28400_read_string(client, SBS_MANUFACTURER_NAME, str, 20);
699 val->strval = str;
700 break;
701 default:
702 pr_err(" psp %d Not supoprted.\n", psp);
703 ret = -EINVAL;
704 break;
705 }
706
707 return ret;
708}
709
710static int bq28400_set_reg(void *data, u64 val)
711{
712 struct debug_reg *dbg = data;
713 u8 reg = dbg->reg;
714 int ret;
715 struct i2c_client *client = bq28400_dev->client;
716
717 ret = bq28400_write_reg(client, reg, val);
718
719 return ret;
720}
721
722static int bq28400_get_reg(void *data, u64 *val)
723{
724 struct debug_reg *dbg = data;
725 u8 reg = dbg->reg;
726 u16 subcmd = dbg->subcmd;
727 int ret;
728 struct i2c_client *client = bq28400_dev->client;
729
730 if (subcmd)
731 ret = bq28400_read_subcmd(client, reg, subcmd);
732 else
733 ret = bq28400_read_reg(client, reg);
734 if (ret < 0)
735 return ret;
736
737 *val = ret;
738
739 return 0;
740}
741
742DEFINE_SIMPLE_ATTRIBUTE(reg_fops, bq28400_get_reg, bq28400_set_reg,
743 "0x%04llx\n");
744
745static int bq28400_create_debugfs_entries(struct bq28400_device *bq28400_dev)
746{
747 int i;
748
749 bq28400_dev->dent = debugfs_create_dir(BQ28400_NAME, NULL);
750 if (IS_ERR(bq28400_dev->dent)) {
751 pr_err("bq28400 driver couldn't create debugfs dir\n");
752 return -EFAULT;
753 }
754
755 for (i = 0 ; i < ARRAY_SIZE(bq28400_debug_regs) ; i++) {
756 char *name = bq28400_debug_regs[i].name;
757 struct dentry *file;
758 void *data = &bq28400_debug_regs[i];
759
760 file = debugfs_create_file(name, 0644, bq28400_dev->dent,
761 data, &reg_fops);
762 if (IS_ERR(file)) {
763 pr_err("debugfs_create_file %s failed.\n", name);
764 return -EFAULT;
765 }
766 }
767
768 return 0;
769}
770
771static int bq28400_set_property(struct power_supply *psy,
772 enum power_supply_property psp,
773 const union power_supply_propval *val)
774{
775 pr_debug("psp = %d.val = %d.\n", psp, val->intval);
776
777 return -EINVAL;
778}
779
780static void bq28400_external_power_changed(struct power_supply *psy)
781{
782 pr_debug("Notify power_supply_changed.\n");
Amir Samuelov0031e5f2012-11-04 14:14:21 +0200783
784 /* The battery gauge monitors the current and voltage every 1 second.
785 * Therefore a delay from the time that the charger start/stop charging
786 * until the battery gauge detects it.
787 */
788 msleep(1000);
Amir Samuelov02df2322012-10-25 10:28:07 +0200789 /* Update LEDs and notify uevents */
790 power_supply_changed(&bq28400_dev->batt_psy);
791}
792
793static int __devinit bq28400_register_psy(struct bq28400_device *bq28400_dev)
794{
795 int ret;
796
797 bq28400_dev->batt_psy.name = "battery";
798 bq28400_dev->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY;
799 bq28400_dev->batt_psy.num_supplicants = 0;
800 bq28400_dev->batt_psy.properties = pm_power_props;
801 bq28400_dev->batt_psy.num_properties = ARRAY_SIZE(pm_power_props);
802 bq28400_dev->batt_psy.get_property = bq28400_get_property;
803 bq28400_dev->batt_psy.set_property = bq28400_set_property;
804 bq28400_dev->batt_psy.external_power_changed =
805 bq28400_external_power_changed;
806
807 ret = power_supply_register(&bq28400_dev->client->dev,
808 &bq28400_dev->batt_psy);
809 if (ret) {
810 pr_err("failed to register power_supply. ret=%d.\n", ret);
811 return ret;
812 }
813
814 return 0;
815}
816
817/**
818 * Update userspace every 1 minute.
819 * Normally it takes more than 120 minutes (two hours) to
820 * charge/discahrge the battery,
821 * so updating every 1 minute should be enough for 1% change
822 * detection.
823 * Any immidiate change detected by the DC charger is notified
824 * by the bq28400_external_power_changed callback, which notify
825 * the user space.
826 */
827static void bq28400_periodic_user_space_update_worker(struct work_struct *work)
828{
829 u32 delay_msec = 60*1000;
830
831 pr_debug("Notify user space.\n");
832
833 /* Notify user space via kobject_uevent change notification */
834 power_supply_changed(&bq28400_dev->batt_psy);
835
836 schedule_delayed_work(&bq28400_dev->periodic_user_space_update_work,
837 round_jiffies_relative(msecs_to_jiffies
838 (delay_msec)));
839}
840
841static int __devinit bq28400_probe(struct i2c_client *client,
842 const struct i2c_device_id *id)
843{
844 int ret = 0;
Eugene Yasmand72132d2013-01-20 13:00:52 +0200845 struct device_node *dev_node = client->dev.of_node;
846
847 if (dev_node == NULL) {
848 pr_err("Device Tree node doesn't exist.\n");
849 return -ENODEV;
850 }
Amir Samuelov02df2322012-10-25 10:28:07 +0200851
852 if (!i2c_check_functionality(client->adapter,
853 I2C_FUNC_SMBUS_BYTE_DATA)) {
854 pr_err(" i2c func fail.\n");
855 return -EIO;
856 }
857
Amir Samuelov729f8412012-10-30 14:34:20 +0200858 if (bq28400_read_reg(client, SBS_BATTERY_STATUS) < 0) {
859 pr_err("Device doesn't exist.\n");
860 return -ENODEV;
861 }
862
Amir Samuelov02df2322012-10-25 10:28:07 +0200863 bq28400_dev = kzalloc(sizeof(*bq28400_dev), GFP_KERNEL);
864 if (!bq28400_dev) {
865 pr_err(" alloc fail.\n");
866 return -ENOMEM;
867 }
868
Eugene Yasmand72132d2013-01-20 13:00:52 +0200869 /* Note: Lithium-ion battery normal temperature range 0..40 C */
870 ret = of_property_read_u32(dev_node, "ti,temp-cold",
871 &(bq28400_dev->temp_cold));
872 if (ret) {
873 pr_err("Unable to read cold temperature. ret=%d.\n", ret);
874 goto err_dev_node;
875 }
876 pr_debug("cold temperature limit = %d C.\n", bq28400_dev->temp_cold);
877
878 ret = of_property_read_u32(dev_node, "ti,temp-hot",
879 &(bq28400_dev->temp_hot));
880 if (ret) {
881 pr_err("Unable to read hot temperature. ret=%d.\n", ret);
882 goto err_dev_node;
883 }
884 pr_debug("hot temperature limit = %d C.\n", bq28400_dev->temp_hot);
885
Amir Samuelov02df2322012-10-25 10:28:07 +0200886 bq28400_dev->client = client;
887 i2c_set_clientdata(client, bq28400_dev);
888
889 ret = bq28400_register_psy(bq28400_dev);
890 if (ret) {
891 pr_err(" bq28400_register_psy fail.\n");
892 goto err_register_psy;
893 }
894
895 ret = bq28400_create_debugfs_entries(bq28400_dev);
896 if (ret) {
897 pr_err(" bq28400_create_debugfs_entries fail.\n");
898 goto err_debugfs;
899 }
900
901 INIT_DELAYED_WORK(&bq28400_dev->periodic_user_space_update_work,
902 bq28400_periodic_user_space_update_worker);
903
904 schedule_delayed_work(&bq28400_dev->periodic_user_space_update_work,
905 msecs_to_jiffies(1000));
906
Eugene Yasmand72132d2013-01-20 13:00:52 +0200907 pr_debug("Device is ready.\n");
Amir Samuelov02df2322012-10-25 10:28:07 +0200908
909 return 0;
910
911err_debugfs:
912 if (bq28400_dev->dent)
913 debugfs_remove_recursive(bq28400_dev->dent);
914 power_supply_unregister(&bq28400_dev->batt_psy);
915err_register_psy:
Eugene Yasmand72132d2013-01-20 13:00:52 +0200916err_dev_node:
Amir Samuelov02df2322012-10-25 10:28:07 +0200917 kfree(bq28400_dev);
918 bq28400_dev = NULL;
919
920 pr_info("FAIL.\n");
921
922 return ret;
923}
924
925static int __devexit bq28400_remove(struct i2c_client *client)
926{
927 struct bq28400_device *bq28400_dev = i2c_get_clientdata(client);
928
929 power_supply_unregister(&bq28400_dev->batt_psy);
930 if (bq28400_dev->dent)
931 debugfs_remove_recursive(bq28400_dev->dent);
932 kfree(bq28400_dev);
933 bq28400_dev = NULL;
934
935 return 0;
936}
937
938static const struct of_device_id bq28400_match[] = {
Amir Samuelov1c23a7c2012-11-12 16:16:19 +0200939 { .compatible = "ti,bq28400-battery" },
940 { .compatible = "ti,bq30z55-battery" },
Amir Samuelov02df2322012-10-25 10:28:07 +0200941 { },
942 };
943
944static const struct i2c_device_id bq28400_id[] = {
945 {BQ28400_NAME, 0},
946 {},
947};
948
949MODULE_DEVICE_TABLE(i2c, bq28400_id);
950
951static struct i2c_driver bq28400_driver = {
952 .driver = {
953 .name = BQ28400_NAME,
954 .owner = THIS_MODULE,
955 .of_match_table = of_match_ptr(bq28400_match),
956 },
957 .probe = bq28400_probe,
958 .remove = __devexit_p(bq28400_remove),
959 .id_table = bq28400_id,
960};
961
962static int __init bq28400_init(void)
963{
964 pr_info(" bq28400 driver rev %s.\n", BQ28400_REV);
965
966 return i2c_add_driver(&bq28400_driver);
967}
968module_init(bq28400_init);
969
970static void __exit bq28400_exit(void)
971{
972 return i2c_del_driver(&bq28400_driver);
973}
974module_exit(bq28400_exit);
975
976MODULE_DESCRIPTION("Driver for BQ28400 charger chip");
977MODULE_LICENSE("GPL v2");
978MODULE_ALIAS("i2c:" BQ28400_NAME);